Actual source code: test22.c
slepc-3.11.2 2019-07-30
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2019, 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;
58: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
59: PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL);
60: PetscPrintf(PETSC_COMM_WORLD,"\nBrusselator wave model, n=%D\n\n",N);
62: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63: Generate the matrix
64: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
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: PetscOptionsGetScalar(NULL,NULL,"-L",&L,NULL);
74: PetscOptionsGetScalar(NULL,NULL,"-alpha",&ctx->alpha,NULL);
75: PetscOptionsGetScalar(NULL,NULL,"-beta",&ctx->beta,NULL);
76: PetscOptionsGetScalar(NULL,NULL,"-delta1",&delta1,NULL);
77: PetscOptionsGetScalar(NULL,NULL,"-delta2",&delta2,NULL);
79: /* Create matrix T */
80: MatCreate(PETSC_COMM_WORLD,&ctx->T);
81: MatSetSizes(ctx->T,PETSC_DECIDE,PETSC_DECIDE,N,N);
82: MatSetFromOptions(ctx->T);
83: MatSetUp(ctx->T);
84: MatGetOwnershipRange(ctx->T,&Istart,&Iend);
85: for (i=Istart;i<Iend;i++) {
86: if (i>0) { MatSetValue(ctx->T,i,i-1,1.0,INSERT_VALUES); }
87: if (i<N-1) { MatSetValue(ctx->T,i,i+1,1.0,INSERT_VALUES); }
88: MatSetValue(ctx->T,i,i,-2.0,INSERT_VALUES);
89: }
90: MatAssemblyBegin(ctx->T,MAT_FINAL_ASSEMBLY);
91: MatAssemblyEnd(ctx->T,MAT_FINAL_ASSEMBLY);
92: 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: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x1);
101: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x2);
102: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y1);
103: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y2);
105: /* Create the shell matrix */
106: MatCreateShell(PETSC_COMM_WORLD,2*n,2*n,2*N,2*N,(void*)ctx,&A);
107: MatShellSetOperation(A,MATOP_MULT,(void(*)(void))MatMult_Brussel);
108: MatShellSetOperation(A,MATOP_GET_DIAGONAL,(void(*)(void))MatGetDiagonal_Brussel);
110: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111: Create the eigensolver and solve the problem
112: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114: EPSCreate(PETSC_COMM_WORLD,&eps);
115: EPSSetOperators(eps,A,NULL);
116: EPSSetProblemType(eps,EPS_NHEP);
117: EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL);
118: EPSSetTrueResidual(eps,PETSC_FALSE);
119: EPSSetFromOptions(eps);
120: EPSSolve(eps);
122: EPSGetTrueResidual(eps,&trueres);
123: /*if (trueres) { PetscPrintf(PETSC_COMM_WORLD," Computing true residuals explicitly\n\n"); }*/
125: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126: Display solution and clean up
127: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
129: EPSGetDimensions(eps,&nev,NULL,NULL);
130: EPSGetTolerances(eps,&tol,NULL);
131: EPSGetConverged(eps,&nconv);
132: if (nconv<nev) {
133: PetscPrintf(PETSC_COMM_WORLD," Problem: less than %D eigenvalues converged\n\n",nev);
134: } else {
135: /* Check that all converged eigenpairs satisfy the requested tolerance
136: (in this example we use the solver's error estimate instead of computing
137: the residual norm explicitly) */
138: errok = PETSC_TRUE;
139: for (i=0;i<nev;i++) {
140: EPSGetErrorEstimate(eps,i,&errest);
141: errok = (errok && errest<5.0*tol)? PETSC_TRUE: PETSC_FALSE;
142: }
143: if (!errok) {
144: PetscPrintf(PETSC_COMM_WORLD," Problem: some of the first %D relative errors are higher than the tolerance\n\n",nev);
145: } else {
146: PetscPrintf(PETSC_COMM_WORLD," All requested eigenvalues computed up to the required tolerance:");
147: for (i=0;i<=(nev-1)/8;i++) {
148: PetscPrintf(PETSC_COMM_WORLD,"\n ");
149: for (j=0;j<PetscMin(8,nev-8*i);j++) {
150: EPSGetEigenpair(eps,8*i+j,&kr,&ki,NULL,NULL);
151: #if defined(PETSC_USE_COMPLEX)
152: re = PetscRealPart(kr);
153: im = PetscImaginaryPart(kr);
154: #else
155: re = kr;
156: im = ki;
157: #endif
158: if (PetscAbs(re)/PetscAbs(im)<PETSC_SMALL) re = 0.0;
159: if (PetscAbs(im)/PetscAbs(re)<PETSC_SMALL) im = 0.0;
160: if (im!=0.0) {
161: PetscPrintf(PETSC_COMM_WORLD,"%.5f%+.5fi",(double)re,(double)im);
162: } else {
163: PetscPrintf(PETSC_COMM_WORLD,"%.5f",(double)re);
164: }
165: if (8*i+j+1<nev) { PetscPrintf(PETSC_COMM_WORLD,", "); }
166: }
167: }
168: PetscPrintf(PETSC_COMM_WORLD,"\n\n");
169: }
170: }
172: /* Get an orthogonal basis of the invariant subspace and check it is indeed
173: orthogonal (note that eigenvectors are not orthogonal in this case) */
174: if (nconv>1) {
175: MatCreateVecs(A,&v,NULL);
176: VecDuplicateVecs(v,nconv,&Q);
177: EPSGetInvariantSubspace(eps,Q);
178: VecCheckOrthogonality(Q,nconv,NULL,nconv,NULL,NULL,&lev);
179: if (lev<10*tol) {
180: PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality below the tolerance\n");
181: } else {
182: PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality: %g\n",(double)lev);
183: }
184: VecDestroyVecs(nconv,&Q);
185: VecDestroy(&v);
186: }
188: EPSDestroy(&eps);
189: MatDestroy(&A);
190: MatDestroy(&ctx->T);
191: VecDestroy(&ctx->x1);
192: VecDestroy(&ctx->x2);
193: VecDestroy(&ctx->y1);
194: VecDestroy(&ctx->y2);
195: PetscFree(ctx);
196: SlepcFinalize();
197: return ierr;
198: }
200: PetscErrorCode MatMult_Brussel(Mat A,Vec x,Vec y)
201: {
202: PetscInt n;
203: const PetscScalar *px;
204: PetscScalar *py;
205: CTX_BRUSSEL *ctx;
206: PetscErrorCode ierr;
209: MatShellGetContext(A,(void**)&ctx);
210: MatGetLocalSize(ctx->T,&n,NULL);
211: VecGetArrayRead(x,&px);
212: VecGetArray(y,&py);
213: VecPlaceArray(ctx->x1,px);
214: VecPlaceArray(ctx->x2,px+n);
215: VecPlaceArray(ctx->y1,py);
216: VecPlaceArray(ctx->y2,py+n);
218: MatMult(ctx->T,ctx->x1,ctx->y1);
219: VecScale(ctx->y1,ctx->tau1);
220: VecAXPY(ctx->y1,ctx->beta - 1.0 + ctx->sigma,ctx->x1);
221: VecAXPY(ctx->y1,ctx->alpha * ctx->alpha,ctx->x2);
223: MatMult(ctx->T,ctx->x2,ctx->y2);
224: VecScale(ctx->y2,ctx->tau2);
225: VecAXPY(ctx->y2,-ctx->beta,ctx->x1);
226: VecAXPY(ctx->y2,-ctx->alpha * ctx->alpha + ctx->sigma,ctx->x2);
228: VecRestoreArrayRead(x,&px);
229: VecRestoreArray(y,&py);
230: VecResetArray(ctx->x1);
231: VecResetArray(ctx->x2);
232: VecResetArray(ctx->y1);
233: VecResetArray(ctx->y2);
234: return(0);
235: }
237: PetscErrorCode MatGetDiagonal_Brussel(Mat A,Vec diag)
238: {
239: Vec d1,d2;
240: PetscInt n;
241: PetscScalar *pd;
242: MPI_Comm comm;
243: CTX_BRUSSEL *ctx;
247: MatShellGetContext(A,(void**)&ctx);
248: PetscObjectGetComm((PetscObject)A,&comm);
249: MatGetLocalSize(ctx->T,&n,NULL);
250: VecGetArray(diag,&pd);
251: VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd,&d1);
252: VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd+n,&d2);
254: VecSet(d1,-2.0*ctx->tau1 + ctx->beta - 1.0 + ctx->sigma);
255: VecSet(d2,-2.0*ctx->tau2 - ctx->alpha*ctx->alpha + ctx->sigma);
257: VecDestroy(&d1);
258: VecDestroy(&d2);
259: VecRestoreArray(diag,&pd);
260: return(0);
261: }
263: /*TEST
265: test:
266: suffix: 1
267: args: -eps_nev 4 -eps_true_residual {{0 1}}
268: requires: !complex !single
269: output_file: output/test22_1.out
271: test:
272: suffix: 2
273: args: -eps_nev 4 -eps_true_residual -eps_balance oneside -eps_tol 1e-7
274: requires: !complex !single
276: TEST*/