Actual source code: test22.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[] = "Illustrates how to obtain invariant subspaces. "
 12:   "Based on ex9.\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 computes the eigenvalues with largest real part of the
 23:    following matrix

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

 28:    where

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

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

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

 46: int main(int argc,char **argv)
 47: {
 48:   EPS            eps;
 49:   Mat            A;
 50:   Vec            *Q,v;
 51:   PetscScalar    delta1,delta2,L,h,kr,ki;
 52:   PetscReal      errest,tol,re,im,lev;
 53:   PetscInt       N=30,n,i,j,Istart,Iend,nev,nconv;
 54:   CTX_BRUSSEL    *ctx;
 55:   PetscBool      errok,trueres;

 57:   PetscFunctionBeginUser;
 58:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
 59:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL));
 60:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nBrusselator wave model, n=%" PetscInt_FMT "\n\n",N));

 62:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 63:         Generate the matrix
 64:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 66:   PetscCall(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:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-L",&L,NULL));
 74:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-alpha",&ctx->alpha,NULL));
 75:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-beta",&ctx->beta,NULL));
 76:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-delta1",&delta1,NULL));
 77:   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-delta2",&delta2,NULL));

 79:   /* Create matrix T */
 80:   PetscCall(MatCreate(PETSC_COMM_WORLD,&ctx->T));
 81:   PetscCall(MatSetSizes(ctx->T,PETSC_DECIDE,PETSC_DECIDE,N,N));
 82:   PetscCall(MatSetFromOptions(ctx->T));
 83:   PetscCall(MatSetUp(ctx->T));
 84:   PetscCall(MatGetOwnershipRange(ctx->T,&Istart,&Iend));
 85:   for (i=Istart;i<Iend;i++) {
 86:     if (i>0) PetscCall(MatSetValue(ctx->T,i,i-1,1.0,INSERT_VALUES));
 87:     if (i<N-1) PetscCall(MatSetValue(ctx->T,i,i+1,1.0,INSERT_VALUES));
 88:     PetscCall(MatSetValue(ctx->T,i,i,-2.0,INSERT_VALUES));
 89:   }
 90:   PetscCall(MatAssemblyBegin(ctx->T,MAT_FINAL_ASSEMBLY));
 91:   PetscCall(MatAssemblyEnd(ctx->T,MAT_FINAL_ASSEMBLY));
 92:   PetscCall(MatGetLocalSize(ctx->T,&n,NULL));

 94:   /* Fill the remaining information in the shell matrix context
 95:      and create auxiliary vectors */
 96:   h = 1.0 / (PetscReal)(N+1);
 97:   ctx->tau1 = delta1 / ((h*L)*(h*L));
 98:   ctx->tau2 = delta2 / ((h*L)*(h*L));
 99:   ctx->sigma = 0.0;
100:   PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x1));
101:   PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x2));
102:   PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y1));
103:   PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y2));

105:   /* Create the shell matrix */
106:   PetscCall(MatCreateShell(PETSC_COMM_WORLD,2*n,2*n,2*N,2*N,(void*)ctx,&A));
107:   PetscCall(MatShellSetOperation(A,MATOP_MULT,(void(*)(void))MatMult_Brussel));
108:   PetscCall(MatShellSetOperation(A,MATOP_GET_DIAGONAL,(void(*)(void))MatGetDiagonal_Brussel));

110:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111:                 Create the eigensolver and solve the problem
112:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

114:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
115:   PetscCall(EPSSetOperators(eps,A,NULL));
116:   PetscCall(EPSSetProblemType(eps,EPS_NHEP));
117:   PetscCall(EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL));
118:   PetscCall(EPSSetTrueResidual(eps,PETSC_FALSE));
119:   PetscCall(EPSSetFromOptions(eps));
120:   PetscCall(EPSSolve(eps));

122:   PetscCall(EPSGetTrueResidual(eps,&trueres));
123:   /*if (trueres) PetscCall(PetscPrintf(PETSC_COMM_WORLD," Computing true residuals explicitly\n\n"));*/

125:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126:                     Display solution and clean up
127:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

129:   PetscCall(EPSGetDimensions(eps,&nev,NULL,NULL));
130:   PetscCall(EPSGetTolerances(eps,&tol,NULL));
131:   PetscCall(EPSGetConverged(eps,&nconv));
132:   if (nconv<nev) PetscCall(PetscPrintf(PETSC_COMM_WORLD," Problem: less than %" PetscInt_FMT " eigenvalues converged\n\n",nev));
133:   else {
134:     /* Check that all converged eigenpairs satisfy the requested tolerance
135:        (in this example we use the solver's error estimate instead of computing
136:        the residual norm explicitly) */
137:     errok = PETSC_TRUE;
138:     for (i=0;i<nev;i++) {
139:       PetscCall(EPSGetErrorEstimate(eps,i,&errest));
140:       PetscCall(EPSGetEigenpair(eps,i,&kr,&ki,NULL,NULL));
141:       errok = (errok && errest<5.0*SlepcAbsEigenvalue(kr,ki)*tol)? PETSC_TRUE: PETSC_FALSE;
142:     }
143:     if (!errok) PetscCall(PetscPrintf(PETSC_COMM_WORLD," Problem: some of the first %" PetscInt_FMT " relative errors are higher than the tolerance\n\n",nev));
144:     else {
145:       PetscCall(PetscPrintf(PETSC_COMM_WORLD," All requested eigenvalues computed up to the required tolerance:"));
146:       for (i=0;i<=(nev-1)/8;i++) {
147:         PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n     "));
148:         for (j=0;j<PetscMin(8,nev-8*i);j++) {
149:           PetscCall(EPSGetEigenpair(eps,8*i+j,&kr,&ki,NULL,NULL));
150: #if defined(PETSC_USE_COMPLEX)
151:           re = PetscRealPart(kr);
152:           im = PetscImaginaryPart(kr);
153: #else
154:           re = kr;
155:           im = ki;
156: #endif
157:           if (PetscAbs(re)/PetscAbs(im)<PETSC_SMALL) re = 0.0;
158:           if (PetscAbs(im)/PetscAbs(re)<PETSC_SMALL) im = 0.0;
159:           if (im!=0.0) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"%.5f%+.5fi",(double)re,(double)im));
160:           else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"%.5f",(double)re));
161:           if (8*i+j+1<nev) PetscCall(PetscPrintf(PETSC_COMM_WORLD,", "));
162:         }
163:       }
164:       PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n\n"));
165:     }
166:   }

168:   /* Get an orthogonal basis of the invariant subspace and check it is indeed
169:      orthogonal (note that eigenvectors are not orthogonal in this case) */
170:   if (nconv>1) {
171:     PetscCall(MatCreateVecs(A,&v,NULL));
172:     PetscCall(VecDuplicateVecs(v,nconv,&Q));
173:     PetscCall(EPSGetInvariantSubspace(eps,Q));
174:     PetscCall(VecCheckOrthonormality(Q,nconv,NULL,nconv,NULL,NULL,&lev));
175:     if (lev<10*tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality below the tolerance\n"));
176:     else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality: %g\n",(double)lev));
177:     PetscCall(VecDestroyVecs(nconv,&Q));
178:     PetscCall(VecDestroy(&v));
179:   }

181:   PetscCall(EPSDestroy(&eps));
182:   PetscCall(MatDestroy(&A));
183:   PetscCall(MatDestroy(&ctx->T));
184:   PetscCall(VecDestroy(&ctx->x1));
185:   PetscCall(VecDestroy(&ctx->x2));
186:   PetscCall(VecDestroy(&ctx->y1));
187:   PetscCall(VecDestroy(&ctx->y2));
188:   PetscCall(PetscFree(ctx));
189:   PetscCall(SlepcFinalize());
190:   return 0;
191: }

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

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

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

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

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

229: PetscErrorCode MatGetDiagonal_Brussel(Mat A,Vec diag)
230: {
231:   Vec            d1,d2;
232:   PetscInt       n;
233:   PetscScalar    *pd;
234:   MPI_Comm       comm;
235:   CTX_BRUSSEL    *ctx;

237:   PetscFunctionBeginUser;
238:   PetscCall(MatShellGetContext(A,&ctx));
239:   PetscCall(PetscObjectGetComm((PetscObject)A,&comm));
240:   PetscCall(MatGetLocalSize(ctx->T,&n,NULL));
241:   PetscCall(VecGetArray(diag,&pd));
242:   PetscCall(VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd,&d1));
243:   PetscCall(VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd+n,&d2));

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

248:   PetscCall(VecDestroy(&d1));
249:   PetscCall(VecDestroy(&d2));
250:   PetscCall(VecRestoreArray(diag,&pd));
251:   PetscFunctionReturn(PETSC_SUCCESS);
252: }

254: /*TEST

256:    test:
257:       suffix: 1
258:       args: -eps_nev 4 -eps_true_residual {{0 1}}
259:       requires: !single

261:    test:
262:       suffix: 2
263:       args: -eps_nev 4 -eps_true_residual -eps_balance oneside -eps_tol 1e-7
264:       requires: !single

266:    test:
267:       suffix: 3
268:       args: -n 50 -eps_nev 4 -eps_ncv 16 -eps_type subspace -eps_largest_magnitude -bv_orthog_block {{gs tsqr chol tsqrchol svqb}}
269:       requires: !single

271: TEST*/