Actual source code: test22.c
slepc-3.18.0 2022-10-01
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;
58: SlepcInitialize(&argc,&argv,(char*)0,help);
59: PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL);
60: PetscPrintf(PETSC_COMM_WORLD,"\nBrusselator wave model, n=%" PetscInt_FMT "\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) 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: EPSGetErrorEstimate(eps,i,&errest);
140: 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) PetscPrintf(PETSC_COMM_WORLD," Problem: some of the first %" PetscInt_FMT " relative errors are higher than the tolerance\n\n",nev);
144: else {
145: PetscPrintf(PETSC_COMM_WORLD," All requested eigenvalues computed up to the required tolerance:");
146: for (i=0;i<=(nev-1)/8;i++) {
147: PetscPrintf(PETSC_COMM_WORLD,"\n ");
148: for (j=0;j<PetscMin(8,nev-8*i);j++) {
149: 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) PetscPrintf(PETSC_COMM_WORLD,"%.5f%+.5fi",(double)re,(double)im);
160: else PetscPrintf(PETSC_COMM_WORLD,"%.5f",(double)re);
161: if (8*i+j+1<nev) PetscPrintf(PETSC_COMM_WORLD,", ");
162: }
163: }
164: 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: MatCreateVecs(A,&v,NULL);
172: VecDuplicateVecs(v,nconv,&Q);
173: EPSGetInvariantSubspace(eps,Q);
174: VecCheckOrthonormality(Q,nconv,NULL,nconv,NULL,NULL,&lev);
175: if (lev<10*tol) PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality below the tolerance\n");
176: else PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality: %g\n",(double)lev);
177: VecDestroyVecs(nconv,&Q);
178: VecDestroy(&v);
179: }
181: EPSDestroy(&eps);
182: MatDestroy(&A);
183: MatDestroy(&ctx->T);
184: VecDestroy(&ctx->x1);
185: VecDestroy(&ctx->x2);
186: VecDestroy(&ctx->y1);
187: VecDestroy(&ctx->y2);
188: PetscFree(ctx);
189: 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;
201: MatShellGetContext(A,&ctx);
202: MatGetLocalSize(ctx->T,&n,NULL);
203: VecGetArrayRead(x,&px);
204: VecGetArray(y,&py);
205: VecPlaceArray(ctx->x1,px);
206: VecPlaceArray(ctx->x2,px+n);
207: VecPlaceArray(ctx->y1,py);
208: VecPlaceArray(ctx->y2,py+n);
210: MatMult(ctx->T,ctx->x1,ctx->y1);
211: VecScale(ctx->y1,ctx->tau1);
212: VecAXPY(ctx->y1,ctx->beta - 1.0 + ctx->sigma,ctx->x1);
213: VecAXPY(ctx->y1,ctx->alpha * ctx->alpha,ctx->x2);
215: MatMult(ctx->T,ctx->x2,ctx->y2);
216: VecScale(ctx->y2,ctx->tau2);
217: VecAXPY(ctx->y2,-ctx->beta,ctx->x1);
218: VecAXPY(ctx->y2,-ctx->alpha * ctx->alpha + ctx->sigma,ctx->x2);
220: VecRestoreArrayRead(x,&px);
221: VecRestoreArray(y,&py);
222: VecResetArray(ctx->x1);
223: VecResetArray(ctx->x2);
224: VecResetArray(ctx->y1);
225: VecResetArray(ctx->y2);
226: return 0;
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;
238: MatShellGetContext(A,&ctx);
239: PetscObjectGetComm((PetscObject)A,&comm);
240: MatGetLocalSize(ctx->T,&n,NULL);
241: VecGetArray(diag,&pd);
242: VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd,&d1);
243: VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd+n,&d2);
245: VecSet(d1,-2.0*ctx->tau1 + ctx->beta - 1.0 + ctx->sigma);
246: VecSet(d2,-2.0*ctx->tau2 - ctx->alpha*ctx->alpha + ctx->sigma);
248: VecDestroy(&d1);
249: VecDestroy(&d2);
250: VecRestoreArray(diag,&pd);
251: return 0;
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*/