Actual source code: ex9.c

slepc-3.20.1 2023-11-27
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-, 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[] = "Solves a problem associated to the Brusselator wave model in chemical reactions, illustrating the use of shell matrices.\n\n"
 12:   "The command line options are:\n"
 13:   "  -n <n>, where <n> = block dimension of the 2x2 block matrix.\n"
 14:   "  -L <L>, where <L> = bifurcation parameter.\n"
 15:   "  -alpha <alpha>, -beta <beta>, -delta1 <delta1>,  -delta2 <delta2>,\n"
 16:   "       where <alpha> <beta> <delta1> <delta2> = model parameters.\n\n";

 18: #include <slepceps.h>

 20: /*
 21:    This example computes the eigenvalues with largest real part of the
 22:    following matrix

 24:         A = [ tau1*T+(beta-1)*I     alpha^2*I
 25:                   -beta*I        tau2*T-alpha^2*I ],

 27:    where

 29:         T = tridiag{1,-2,1}
 30:         h = 1/(n+1)
 31:         tau1 = delta1/(h*L)^2
 32:         tau2 = delta2/(h*L)^2
 33: */

 35: /*
 36:    Matrix operations
 37: */
 38: PetscErrorCode MatMult_Brussel(Mat,Vec,Vec);
 39: PetscErrorCode MatMultTranspose_Brussel(Mat,Vec,Vec);
 40: PetscErrorCode MatGetDiagonal_Brussel(Mat,Vec);

 42: typedef struct {
 43:   Mat         T;
 44:   Vec         x1,x2,y1,y2;
 45:   PetscScalar alpha,beta,tau1,tau2,sigma;
 46: } CTX_BRUSSEL;

 48: int main(int argc,char **argv)
 49: {
 50:   Mat            A;               /* eigenvalue problem matrix */
 51:   EPS            eps;             /* eigenproblem solver context */
 52:   EPSType        type;
 53:   PetscScalar    delta1,delta2,L,h;
 54:   PetscInt       N=30,n,i,Istart,Iend,nev;
 55:   CTX_BRUSSEL    *ctx;
 56:   PetscBool      terse;
 57:   PetscViewer    viewer;

 59:   PetscFunctionBeginUser;
 60:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));

 62:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL));
 63:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nBrusselator wave model, n=%" PetscInt_FMT "\n\n",N));

 65:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 66:         Generate the matrix
 67:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 69:   /*
 70:      Create shell matrix context and set default parameters
 71:   */
 72:   PetscCall(PetscNew(&ctx));
 73:   ctx->alpha = 2.0;
 74:   ctx->beta  = 5.45;
 75:   delta1     = 0.008;
 76:   delta2     = 0.004;
 77:   L          = 0.51302;

 79:   /*
 80:      Look the command line for user-provided parameters
 81:   */
 82:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-L",&L,NULL));
 83:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-alpha",&ctx->alpha,NULL));
 84:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-beta",&ctx->beta,NULL));
 85:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-delta1",&delta1,NULL));
 86:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-delta2",&delta2,NULL));

 88:   /*
 89:      Create matrix T
 90:   */
 91:   PetscCall(MatCreate(PETSC_COMM_WORLD,&ctx->T));
 92:   PetscCall(MatSetSizes(ctx->T,PETSC_DECIDE,PETSC_DECIDE,N,N));
 93:   PetscCall(MatSetFromOptions(ctx->T));
 94:   PetscCall(MatSetUp(ctx->T));

 96:   PetscCall(MatGetOwnershipRange(ctx->T,&Istart,&Iend));
 97:   for (i=Istart;i<Iend;i++) {
 98:     if (i>0) PetscCall(MatSetValue(ctx->T,i,i-1,1.0,INSERT_VALUES));
 99:     if (i<N-1) PetscCall(MatSetValue(ctx->T,i,i+1,1.0,INSERT_VALUES));
100:     PetscCall(MatSetValue(ctx->T,i,i,-2.0,INSERT_VALUES));
101:   }
102:   PetscCall(MatAssemblyBegin(ctx->T,MAT_FINAL_ASSEMBLY));
103:   PetscCall(MatAssemblyEnd(ctx->T,MAT_FINAL_ASSEMBLY));
104:   PetscCall(MatGetLocalSize(ctx->T,&n,NULL));

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

119:   /*
120:      Create the shell matrix
121:   */
122:   PetscCall(MatCreateShell(PETSC_COMM_WORLD,2*n,2*n,2*N,2*N,(void*)ctx,&A));
123:   PetscCall(MatShellSetOperation(A,MATOP_MULT,(void(*)(void))MatMult_Brussel));
124:   PetscCall(MatShellSetOperation(A,MATOP_MULT_TRANSPOSE,(void(*)(void))MatMultTranspose_Brussel));
125:   PetscCall(MatShellSetOperation(A,MATOP_GET_DIAGONAL,(void(*)(void))MatGetDiagonal_Brussel));

127:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128:                 Create the eigensolver and set various options
129:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

131:   /*
132:      Create eigensolver context
133:   */
134:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));

136:   /*
137:      Set operators. In this case, it is a standard eigenvalue problem
138:   */
139:   PetscCall(EPSSetOperators(eps,A,NULL));
140:   PetscCall(EPSSetProblemType(eps,EPS_NHEP));

142:   /*
143:      Ask for the rightmost eigenvalues
144:   */
145:   PetscCall(EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL));

147:   /*
148:      Set other solver options at runtime
149:   */
150:   PetscCall(EPSSetFromOptions(eps));

152:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153:                       Solve the eigensystem
154:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

156:   PetscCall(EPSSolve(eps));

158:   /*
159:      Optional: Get some information from the solver and display it
160:   */
161:   PetscCall(EPSGetType(eps,&type));
162:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
163:   PetscCall(EPSGetDimensions(eps,&nev,NULL,NULL));
164:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));

166:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
167:                     Display solution and clean up
168:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

170:   /* show detailed info unless -terse option is given by user */
171:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
172:   if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
173:   else {
174:     PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
175:     PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL));
176:     PetscCall(EPSConvergedReasonView(eps,viewer));
177:     PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,viewer));
178:     PetscCall(PetscViewerPopFormat(viewer));
179:   }
180:   PetscCall(EPSDestroy(&eps));
181:   PetscCall(MatDestroy(&A));
182:   PetscCall(MatDestroy(&ctx->T));
183:   PetscCall(VecDestroy(&ctx->x1));
184:   PetscCall(VecDestroy(&ctx->x2));
185:   PetscCall(VecDestroy(&ctx->y1));
186:   PetscCall(VecDestroy(&ctx->y2));
187:   PetscCall(PetscFree(ctx));
188:   PetscCall(SlepcFinalize());
189:   return 0;
190: }

192: PetscErrorCode MatMult_Brussel(Mat A,Vec x,Vec y)
193: {
194:   PetscInt          n;
195:   const PetscScalar *px;
196:   PetscScalar       *py;
197:   CTX_BRUSSEL       *ctx;

199:   PetscFunctionBeginUser;
200:   PetscCall(MatShellGetContext(A,&ctx));
201:   PetscCall(MatGetLocalSize(ctx->T,&n,NULL));
202:   PetscCall(VecGetArrayRead(x,&px));
203:   PetscCall(VecGetArray(y,&py));
204:   PetscCall(VecPlaceArray(ctx->x1,px));
205:   PetscCall(VecPlaceArray(ctx->x2,px+n));
206:   PetscCall(VecPlaceArray(ctx->y1,py));
207:   PetscCall(VecPlaceArray(ctx->y2,py+n));

209:   PetscCall(MatMult(ctx->T,ctx->x1,ctx->y1));
210:   PetscCall(VecScale(ctx->y1,ctx->tau1));
211:   PetscCall(VecAXPY(ctx->y1,ctx->beta-1.0+ctx->sigma,ctx->x1));
212:   PetscCall(VecAXPY(ctx->y1,ctx->alpha*ctx->alpha,ctx->x2));

214:   PetscCall(MatMult(ctx->T,ctx->x2,ctx->y2));
215:   PetscCall(VecScale(ctx->y2,ctx->tau2));
216:   PetscCall(VecAXPY(ctx->y2,-ctx->beta,ctx->x1));
217:   PetscCall(VecAXPY(ctx->y2,-ctx->alpha*ctx->alpha+ctx->sigma,ctx->x2));

219:   PetscCall(VecRestoreArrayRead(x,&px));
220:   PetscCall(VecRestoreArray(y,&py));
221:   PetscCall(VecResetArray(ctx->x1));
222:   PetscCall(VecResetArray(ctx->x2));
223:   PetscCall(VecResetArray(ctx->y1));
224:   PetscCall(VecResetArray(ctx->y2));
225:   PetscFunctionReturn(PETSC_SUCCESS);
226: }

228: PetscErrorCode MatMultTranspose_Brussel(Mat A,Vec x,Vec y)
229: {
230:   PetscInt          n;
231:   const PetscScalar *px;
232:   PetscScalar       *py;
233:   CTX_BRUSSEL       *ctx;

235:   PetscFunctionBeginUser;
236:   PetscCall(MatShellGetContext(A,&ctx));
237:   PetscCall(MatGetLocalSize(ctx->T,&n,NULL));
238:   PetscCall(VecGetArrayRead(x,&px));
239:   PetscCall(VecGetArray(y,&py));
240:   PetscCall(VecPlaceArray(ctx->x1,px));
241:   PetscCall(VecPlaceArray(ctx->x2,px+n));
242:   PetscCall(VecPlaceArray(ctx->y1,py));
243:   PetscCall(VecPlaceArray(ctx->y2,py+n));

245:   PetscCall(MatMultTranspose(ctx->T,ctx->x1,ctx->y1));
246:   PetscCall(VecScale(ctx->y1,ctx->tau1));
247:   PetscCall(VecAXPY(ctx->y1,ctx->beta-1.0+ctx->sigma,ctx->x1));
248:   PetscCall(VecAXPY(ctx->y1,-ctx->beta,ctx->x2));

250:   PetscCall(MatMultTranspose(ctx->T,ctx->x2,ctx->y2));
251:   PetscCall(VecScale(ctx->y2,ctx->tau2));
252:   PetscCall(VecAXPY(ctx->y2,ctx->alpha*ctx->alpha,ctx->x1));
253:   PetscCall(VecAXPY(ctx->y2,-ctx->alpha*ctx->alpha+ctx->sigma,ctx->x2));

255:   PetscCall(VecRestoreArrayRead(x,&px));
256:   PetscCall(VecRestoreArray(y,&py));
257:   PetscCall(VecResetArray(ctx->x1));
258:   PetscCall(VecResetArray(ctx->x2));
259:   PetscCall(VecResetArray(ctx->y1));
260:   PetscCall(VecResetArray(ctx->y2));
261:   PetscFunctionReturn(PETSC_SUCCESS);
262: }

264: PetscErrorCode MatGetDiagonal_Brussel(Mat A,Vec diag)
265: {
266:   Vec            d1,d2;
267:   PetscInt       n;
268:   PetscScalar    *pd;
269:   MPI_Comm       comm;
270:   CTX_BRUSSEL    *ctx;

272:   PetscFunctionBeginUser;
273:   PetscCall(MatShellGetContext(A,&ctx));
274:   PetscCall(PetscObjectGetComm((PetscObject)A,&comm));
275:   PetscCall(MatGetLocalSize(ctx->T,&n,NULL));
276:   PetscCall(VecGetArray(diag,&pd));
277:   PetscCall(VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd,&d1));
278:   PetscCall(VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd+n,&d2));

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

283:   PetscCall(VecDestroy(&d1));
284:   PetscCall(VecDestroy(&d2));
285:   PetscCall(VecRestoreArray(diag,&pd));
286:   PetscFunctionReturn(PETSC_SUCCESS);
287: }

289: /*TEST

291:    test:
292:       suffix: 1
293:       args: -n 50 -eps_nev 4 -eps_two_sided {{0 1}} -eps_type {{krylovschur lapack}} -terse
294:       requires: !single
295:       filter: grep -v method

297:    test:
298:       suffix: 2
299:       args: -eps_nev 8 -eps_max_it 300 -eps_target -28 -rg_type interval -rg_interval_endpoints -40,-20,-.1,.1 -terse
300:       requires: !single

302:    test:
303:       suffix: 3
304:       args: -n 50 -eps_nev 4 -eps_balance twoside -terse
305:       requires: double
306:       filter: grep -v method
307:       output_file: output/ex9_1.out

309:    test:
310:       suffix: 4
311:       args: -eps_smallest_imaginary -eps_ncv 24 -terse
312:       requires: !complex !single

314:    test:
315:       suffix: 4_complex
316:       args: -eps_smallest_imaginary -eps_ncv 24 -terse
317:       requires: complex !single

319:    test:
320:       suffix: 5
321:       args: -eps_nev 4 -eps_target_real -eps_target -3 -terse
322:       requires: !single

324:    test:
325:       suffix: 6
326:       args: -eps_nev 2 -eps_target_imaginary -eps_target 3i -terse
327:       requires: complex !single

329:    test:
330:       suffix: 7
331:       args: -n 40 -eps_nev 1 -eps_type arnoldi -eps_smallest_real -eps_refined -eps_ncv 40 -eps_max_it 300 -terse
332:       requires: double

334:    test:
335:       suffix: 8
336:       args: -eps_nev 2 -eps_target -30 -eps_type jd -st_matmode shell -eps_jd_fix 0.0001 -eps_jd_const_correction_tol 0 -terse
337:       requires: !single
338:       filter: sed -e "s/[+-]0\.0*i//g"
339:       timeoutfactor: 2

341:    test:
342:       suffix: 9
343:       args: -eps_largest_imaginary -eps_ncv 24 -terse
344:       requires: !single

346: TEST*/