Actual source code: ex30.c

slepc-3.16.0 2021-09-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */

 11: static char help[] = "Illustrates the use of a region for filtering; the number of wanted eigenvalues is not known a priori.\n\n"
 12:   "The problem is the Brusselator wave model as in ex9.c.\n"
 13:   "The command line options are:\n"
 14:   "  -n <n>, where <n> = block dimension of the 2x2 block matrix.\n"
 15:   "  -L <L>, where <L> = bifurcation parameter.\n"
 16:   "  -alpha <alpha>, -beta <beta>, -delta1 <delta1>,  -delta2 <delta2>,\n"
 17:   "       where <alpha> <beta> <delta1> <delta2> = model parameters.\n\n";

 19: #include <slepceps.h>

 21: /*
 22:    This example tries to compute all eigenvalues lying outside the real axis.
 23:    This could be achieved by computing LARGEST_IMAGINARY eigenvalues, but
 24:    here we take a different route: define a region of the complex plane where
 25:    eigenvalues must be emphasized (eigenvalues outside the region are filtered
 26:    out). In this case, we select the region as the complement of a thin stripe
 27:    around the real axis.
 28:  */

 30: PetscErrorCode MatMult_Brussel(Mat,Vec,Vec);
 31: PetscErrorCode MatGetDiagonal_Brussel(Mat,Vec);
 32: PetscErrorCode MyStoppingTest(EPS,PetscInt,PetscInt,PetscInt,PetscInt,EPSConvergedReason*,void*);

 34: typedef struct {
 35:   Mat         T;
 36:   Vec         x1,x2,y1,y2;
 37:   PetscScalar alpha,beta,tau1,tau2,sigma;
 38:   PetscInt    lastnconv;      /* last value of nconv; used in stopping test */
 39:   PetscInt    nreps;          /* number of repetitions of nconv; used in stopping test */
 40: } CTX_BRUSSEL;

 42: int main(int argc,char **argv)
 43: {
 44:   Mat            A;               /* eigenvalue problem matrix */
 45:   EPS            eps;             /* eigenproblem solver context */
 46:   RG             rg;              /* region object */
 47:   PetscScalar    delta1,delta2,L,h;
 48:   PetscInt       N=30,n,i,Istart,Iend,mpd;
 49:   CTX_BRUSSEL    *ctx;
 50:   PetscBool      terse;
 51:   PetscViewer    viewer;

 54:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 56:   PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL);
 57:   PetscPrintf(PETSC_COMM_WORLD,"\nBrusselator wave model, n=%D\n\n",N);

 59:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 60:         Generate the matrix
 61:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 63:   /*
 64:      Create shell matrix context and set default parameters
 65:   */
 66:   PetscNew(&ctx);
 67:   ctx->alpha = 2.0;
 68:   ctx->beta  = 5.45;
 69:   delta1     = 0.008;
 70:   delta2     = 0.004;
 71:   L          = 0.51302;

 73:   /*
 74:      Look the command line for user-provided parameters
 75:   */
 76:   PetscOptionsGetScalar(NULL,NULL,"-L",&L,NULL);
 77:   PetscOptionsGetScalar(NULL,NULL,"-alpha",&ctx->alpha,NULL);
 78:   PetscOptionsGetScalar(NULL,NULL,"-beta",&ctx->beta,NULL);
 79:   PetscOptionsGetScalar(NULL,NULL,"-delta1",&delta1,NULL);
 80:   PetscOptionsGetScalar(NULL,NULL,"-delta2",&delta2,NULL);

 82:   /*
 83:      Create matrix T
 84:   */
 85:   MatCreate(PETSC_COMM_WORLD,&ctx->T);
 86:   MatSetSizes(ctx->T,PETSC_DECIDE,PETSC_DECIDE,N,N);
 87:   MatSetFromOptions(ctx->T);
 88:   MatSetUp(ctx->T);

 90:   MatGetOwnershipRange(ctx->T,&Istart,&Iend);
 91:   for (i=Istart;i<Iend;i++) {
 92:     if (i>0) { MatSetValue(ctx->T,i,i-1,1.0,INSERT_VALUES); }
 93:     if (i<N-1) { MatSetValue(ctx->T,i,i+1,1.0,INSERT_VALUES); }
 94:     MatSetValue(ctx->T,i,i,-2.0,INSERT_VALUES);
 95:   }
 96:   MatAssemblyBegin(ctx->T,MAT_FINAL_ASSEMBLY);
 97:   MatAssemblyEnd(ctx->T,MAT_FINAL_ASSEMBLY);
 98:   MatGetLocalSize(ctx->T,&n,NULL);

100:   /*
101:      Fill the remaining information in the shell matrix context
102:      and create auxiliary vectors
103:   */
104:   h = 1.0 / (PetscReal)(N+1);
105:   ctx->tau1 = delta1 / ((h*L)*(h*L));
106:   ctx->tau2 = delta2 / ((h*L)*(h*L));
107:   ctx->sigma = 0.0;
108:   VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x1);
109:   VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x2);
110:   VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y1);
111:   VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y2);

113:   /*
114:      Create the shell matrix
115:   */
116:   MatCreateShell(PETSC_COMM_WORLD,2*n,2*n,2*N,2*N,(void*)ctx,&A);
117:   MatShellSetOperation(A,MATOP_MULT,(void(*)(void))MatMult_Brussel);
118:   MatShellSetOperation(A,MATOP_GET_DIAGONAL,(void(*)(void))MatGetDiagonal_Brussel);

120:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
121:                 Create the eigensolver and configure the region
122:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

124:   EPSCreate(PETSC_COMM_WORLD,&eps);
125:   EPSSetOperators(eps,A,NULL);
126:   EPSSetProblemType(eps,EPS_NHEP);

128:   /*
129:      Define the region containing the eigenvalues of interest
130:   */
131:   EPSGetRG(eps,&rg);
132:   RGSetType(rg,RGINTERVAL);
133:   RGIntervalSetEndpoints(rg,-PETSC_INFINITY,PETSC_INFINITY,-0.01,0.01);
134:   RGSetComplement(rg,PETSC_TRUE);
135:   /* sort eigenvalue approximations wrt a target, otherwise convergence will be erratic */
136:   EPSSetTarget(eps,0.0);
137:   EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);

139:   /*
140:      Set solver options. In particular, we must allocate sufficient
141:      storage for all eigenpairs that may converge (ncv). This is
142:      application-dependent.
143:   */
144:   mpd = 40;
145:   EPSSetDimensions(eps,2*mpd,3*mpd,mpd);
146:   EPSSetTolerances(eps,1e-7,2000);
147:   ctx->lastnconv = 0;
148:   ctx->nreps     = 0;
149:   EPSSetStoppingTestFunction(eps,MyStoppingTest,(void*)ctx,NULL);
150:   EPSSetFromOptions(eps);

152:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153:                 Solve the eigensystem and display solution
154:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

156:   EPSSolve(eps);

158:   /* show detailed info unless -terse option is given by user */
159:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
160:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
161:   EPSConvergedReasonView(eps,viewer);
162:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
163:   if (!terse) {
164:     EPSErrorView(eps,EPS_ERROR_RELATIVE,viewer);
165:   }
166:   PetscViewerPopFormat(viewer);

168:   EPSDestroy(&eps);
169:   MatDestroy(&A);
170:   MatDestroy(&ctx->T);
171:   VecDestroy(&ctx->x1);
172:   VecDestroy(&ctx->x2);
173:   VecDestroy(&ctx->y1);
174:   VecDestroy(&ctx->y2);
175:   PetscFree(ctx);
176:   SlepcFinalize();
177:   return ierr;
178: }

180: PetscErrorCode MatMult_Brussel(Mat A,Vec x,Vec y)
181: {
182:   PetscInt          n;
183:   const PetscScalar *px;
184:   PetscScalar       *py;
185:   CTX_BRUSSEL       *ctx;
186:   PetscErrorCode    ierr;

189:   MatShellGetContext(A,&ctx);
190:   MatGetLocalSize(ctx->T,&n,NULL);
191:   VecGetArrayRead(x,&px);
192:   VecGetArray(y,&py);
193:   VecPlaceArray(ctx->x1,px);
194:   VecPlaceArray(ctx->x2,px+n);
195:   VecPlaceArray(ctx->y1,py);
196:   VecPlaceArray(ctx->y2,py+n);

198:   MatMult(ctx->T,ctx->x1,ctx->y1);
199:   VecScale(ctx->y1,ctx->tau1);
200:   VecAXPY(ctx->y1,ctx->beta - 1.0 + ctx->sigma,ctx->x1);
201:   VecAXPY(ctx->y1,ctx->alpha * ctx->alpha,ctx->x2);

203:   MatMult(ctx->T,ctx->x2,ctx->y2);
204:   VecScale(ctx->y2,ctx->tau2);
205:   VecAXPY(ctx->y2,-ctx->beta,ctx->x1);
206:   VecAXPY(ctx->y2,-ctx->alpha * ctx->alpha + ctx->sigma,ctx->x2);

208:   VecRestoreArrayRead(x,&px);
209:   VecRestoreArray(y,&py);
210:   VecResetArray(ctx->x1);
211:   VecResetArray(ctx->x2);
212:   VecResetArray(ctx->y1);
213:   VecResetArray(ctx->y2);
214:   return(0);
215: }

217: PetscErrorCode MatGetDiagonal_Brussel(Mat A,Vec diag)
218: {
219:   Vec            d1,d2;
220:   PetscInt       n;
221:   PetscScalar    *pd;
222:   MPI_Comm       comm;
223:   CTX_BRUSSEL    *ctx;

227:   MatShellGetContext(A,&ctx);
228:   PetscObjectGetComm((PetscObject)A,&comm);
229:   MatGetLocalSize(ctx->T,&n,NULL);
230:   VecGetArray(diag,&pd);
231:   VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd,&d1);
232:   VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd+n,&d2);

234:   VecSet(d1,-2.0*ctx->tau1 + ctx->beta - 1.0 + ctx->sigma);
235:   VecSet(d2,-2.0*ctx->tau2 - ctx->alpha*ctx->alpha + ctx->sigma);

237:   VecDestroy(&d1);
238:   VecDestroy(&d2);
239:   VecRestoreArray(diag,&pd);
240:   return(0);
241: }

243: /*
244:     Function for user-defined stopping test.

246:     Ignores the value of nev. It only takes into account the number of
247:     eigenpairs that have converged in recent outer iterations (restarts);
248:     if no new eigenvalues have converged in the last few restarts,
249:     we stop the iteration, assuming that no more eigenvalues are present
250:     inside the region.
251: */
252: PetscErrorCode MyStoppingTest(EPS eps,PetscInt its,PetscInt max_it,PetscInt nconv,PetscInt nev,EPSConvergedReason *reason,void *ptr)
253: {
255:   CTX_BRUSSEL    *ctx = (CTX_BRUSSEL*)ptr;

258:   /* check usual termination conditions, but ignoring the case nconv>=nev */
259:   EPSStoppingBasic(eps,its,max_it,nconv,PETSC_MAX_INT,reason,NULL);
260:   if (*reason==EPS_CONVERGED_ITERATING) {
261:     /* check if nconv is the same as before */
262:     if (nconv==ctx->lastnconv) ctx->nreps++;
263:     else {
264:       ctx->lastnconv = nconv;
265:       ctx->nreps     = 0;
266:     }
267:     /* check if no eigenvalues converged in last 10 restarts */
268:     if (nconv && ctx->nreps>10) *reason = EPS_CONVERGED_USER;
269:   }
270:   return(0);
271: }

273: /*TEST

275:    test:
276:       suffix: 1
277:       args: -n 100 -terse
278:       requires: !single

280: TEST*/