Actual source code: ex30.c
slepc-3.7.0 2016-05-16
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
8: SLEPc is free software: you can redistribute it and/or modify it under the
9: terms of version 3 of the GNU Lesser General Public License as published by
10: the Free Software Foundation.
12: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
13: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15: more details.
17: You should have received a copy of the GNU Lesser General Public License
18: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
19: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: */
22: static char help[] = "Illustrates the use of a region for filtering; the number of wanted eigenvalues in not known a priori.\n\n"
23: "The problem is the Brusselator wave model as in ex9.c.\n"
24: "The command line options are:\n"
25: " -n <n>, where <n> = block dimension of the 2x2 block matrix.\n"
26: " -L <L>, where <L> = bifurcation parameter.\n"
27: " -alpha <alpha>, -beta <beta>, -delta1 <delta1>, -delta2 <delta2>,\n"
28: " where <alpha> <beta> <delta1> <delta2> = model parameters.\n\n";
30: #include <slepceps.h>
32: /*
33: This example tries to compute all eigenvalues lying outside the real axis.
34: This could be achieved by computing LARGEST_IMAGINARY eigenvalues, but
35: here we take a different route: define a region of the complex plane where
36: eigenvalues must be emphasized (eigenvalues outside the region are filtered
37: out). In this case, we select the region as the complement of a thin stripe
38: around the real axis.
39: */
41: PetscErrorCode MatMult_Brussel(Mat,Vec,Vec);
42: PetscErrorCode MatShift_Brussel(PetscScalar*,Mat);
43: PetscErrorCode MatGetDiagonal_Brussel(Mat,Vec);
44: PetscErrorCode MyStoppingTest(EPS,PetscInt,PetscInt,PetscInt,PetscInt,EPSConvergedReason*,void*);
46: typedef struct {
47: Mat T;
48: Vec x1,x2,y1,y2;
49: PetscScalar alpha,beta,tau1,tau2,sigma;
50: PetscInt lastnconv; /* last value of nconv; used in stopping test */
51: PetscInt nreps; /* number of repetitions of nconv; used in stopping test */
52: } CTX_BRUSSEL;
56: int main(int argc,char **argv)
57: {
58: Mat A; /* eigenvalue problem matrix */
59: EPS eps; /* eigenproblem solver context */
60: RG rg; /* region object */
61: PetscScalar delta1,delta2,L,h;
62: PetscInt N=30,n,i,Istart,Iend,mpd;
63: CTX_BRUSSEL *ctx;
64: PetscBool terse;
65: PetscViewer viewer;
68: SlepcInitialize(&argc,&argv,(char*)0,help);
70: PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL);
71: PetscPrintf(PETSC_COMM_WORLD,"\nBrusselator wave model, n=%D\n\n",N);
73: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74: Generate the matrix
75: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
77: /*
78: Create shell matrix context and set default parameters
79: */
80: PetscNew(&ctx);
81: ctx->alpha = 2.0;
82: ctx->beta = 5.45;
83: delta1 = 0.008;
84: delta2 = 0.004;
85: L = 0.51302;
87: /*
88: Look the command line for user-provided parameters
89: */
90: PetscOptionsGetScalar(NULL,NULL,"-L",&L,NULL);
91: PetscOptionsGetScalar(NULL,NULL,"-alpha",&ctx->alpha,NULL);
92: PetscOptionsGetScalar(NULL,NULL,"-beta",&ctx->beta,NULL);
93: PetscOptionsGetScalar(NULL,NULL,"-delta1",&delta1,NULL);
94: PetscOptionsGetScalar(NULL,NULL,"-delta2",&delta2,NULL);
96: /*
97: Create matrix T
98: */
99: MatCreate(PETSC_COMM_WORLD,&ctx->T);
100: MatSetSizes(ctx->T,PETSC_DECIDE,PETSC_DECIDE,N,N);
101: MatSetFromOptions(ctx->T);
102: MatSetUp(ctx->T);
104: MatGetOwnershipRange(ctx->T,&Istart,&Iend);
105: for (i=Istart;i<Iend;i++) {
106: if (i>0) { MatSetValue(ctx->T,i,i-1,1.0,INSERT_VALUES); }
107: if (i<N-1) { MatSetValue(ctx->T,i,i+1,1.0,INSERT_VALUES); }
108: MatSetValue(ctx->T,i,i,-2.0,INSERT_VALUES);
109: }
110: MatAssemblyBegin(ctx->T,MAT_FINAL_ASSEMBLY);
111: MatAssemblyEnd(ctx->T,MAT_FINAL_ASSEMBLY);
112: MatGetLocalSize(ctx->T,&n,NULL);
114: /*
115: Fill the remaining information in the shell matrix context
116: and create auxiliary vectors
117: */
118: h = 1.0 / (PetscReal)(N+1);
119: ctx->tau1 = delta1 / ((h*L)*(h*L));
120: ctx->tau2 = delta2 / ((h*L)*(h*L));
121: ctx->sigma = 0.0;
122: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x1);
123: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->x2);
124: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y1);
125: VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&ctx->y2);
127: /*
128: Create the shell matrix
129: */
130: MatCreateShell(PETSC_COMM_WORLD,2*n,2*n,2*N,2*N,(void*)ctx,&A);
131: MatShellSetOperation(A,MATOP_MULT,(void(*)())MatMult_Brussel);
132: MatShellSetOperation(A,MATOP_SHIFT,(void(*)())MatShift_Brussel);
133: MatShellSetOperation(A,MATOP_GET_DIAGONAL,(void(*)())MatGetDiagonal_Brussel);
135: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
136: Create the eigensolver and configure the region
137: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
139: EPSCreate(PETSC_COMM_WORLD,&eps);
140: EPSSetOperators(eps,A,NULL);
141: EPSSetProblemType(eps,EPS_NHEP);
143: /*
144: Define the region containing the eigenvalues of interest
145: */
146: EPSGetRG(eps,&rg);
147: RGSetType(rg,RGINTERVAL);
148: RGIntervalSetEndpoints(rg,-PETSC_INFINITY,PETSC_INFINITY,-0.01,0.01);
149: RGSetComplement(rg,PETSC_TRUE);
150: /* sort eigenvalue approximations wrt a target, otherwise convergence will be erratic */
151: EPSSetTarget(eps,0.0);
152: EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
154: /*
155: Set solver options. In particular, we must allocate sufficient
156: storage for all eigenpairs that may converge (ncv). This is
157: application-dependent.
158: */
159: mpd = 40;
160: EPSSetDimensions(eps,2*mpd,3*mpd,mpd);
161: EPSSetTolerances(eps,1e-7,2000);
162: ctx->lastnconv = 0;
163: ctx->nreps = 0;
164: EPSSetStoppingTestFunction(eps,MyStoppingTest,(void*)ctx,NULL);
165: EPSSetFromOptions(eps);
167: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168: Solve the eigensystem and display solution
169: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
171: EPSSolve(eps);
173: /* show detailed info unless -terse option is given by user */
174: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
175: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
176: EPSReasonView(eps,viewer);
177: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
178: if (!terse) {
179: EPSErrorView(eps,EPS_ERROR_RELATIVE,viewer);
180: }
181: PetscViewerPopFormat(viewer);
183: EPSDestroy(&eps);
184: MatDestroy(&A);
185: MatDestroy(&ctx->T);
186: VecDestroy(&ctx->x1);
187: VecDestroy(&ctx->x2);
188: VecDestroy(&ctx->y1);
189: VecDestroy(&ctx->y2);
190: PetscFree(ctx);
191: SlepcFinalize();
192: return ierr;
193: }
197: PetscErrorCode MatMult_Brussel(Mat A,Vec x,Vec y)
198: {
199: PetscInt n;
200: const PetscScalar *px;
201: PetscScalar *py;
202: CTX_BRUSSEL *ctx;
203: PetscErrorCode ierr;
206: MatShellGetContext(A,(void**)&ctx);
207: MatGetLocalSize(ctx->T,&n,NULL);
208: VecGetArrayRead(x,&px);
209: VecGetArray(y,&py);
210: VecPlaceArray(ctx->x1,px);
211: VecPlaceArray(ctx->x2,px+n);
212: VecPlaceArray(ctx->y1,py);
213: VecPlaceArray(ctx->y2,py+n);
215: MatMult(ctx->T,ctx->x1,ctx->y1);
216: VecScale(ctx->y1,ctx->tau1);
217: VecAXPY(ctx->y1,ctx->beta - 1.0 + ctx->sigma,ctx->x1);
218: VecAXPY(ctx->y1,ctx->alpha * ctx->alpha,ctx->x2);
220: MatMult(ctx->T,ctx->x2,ctx->y2);
221: VecScale(ctx->y2,ctx->tau2);
222: VecAXPY(ctx->y2,-ctx->beta,ctx->x1);
223: VecAXPY(ctx->y2,-ctx->alpha * ctx->alpha + ctx->sigma,ctx->x2);
225: VecRestoreArrayRead(x,&px);
226: VecRestoreArray(y,&py);
227: VecResetArray(ctx->x1);
228: VecResetArray(ctx->x2);
229: VecResetArray(ctx->y1);
230: VecResetArray(ctx->y2);
231: return(0);
232: }
236: PetscErrorCode MatShift_Brussel(PetscScalar* a,Mat Y)
237: {
238: CTX_BRUSSEL *ctx;
242: MatShellGetContext(Y,(void**)&ctx);
243: ctx->sigma += *a;
244: return(0);
245: }
249: PetscErrorCode MatGetDiagonal_Brussel(Mat A,Vec diag)
250: {
251: Vec d1,d2;
252: PetscInt n;
253: PetscScalar *pd;
254: MPI_Comm comm;
255: CTX_BRUSSEL *ctx;
259: MatShellGetContext(A,(void**)&ctx);
260: PetscObjectGetComm((PetscObject)A,&comm);
261: MatGetLocalSize(ctx->T,&n,NULL);
262: VecGetArray(diag,&pd);
263: VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd,&d1);
264: VecCreateMPIWithArray(comm,1,n,PETSC_DECIDE,pd+n,&d2);
266: VecSet(d1,-2.0*ctx->tau1 + ctx->beta - 1.0 + ctx->sigma);
267: VecSet(d2,-2.0*ctx->tau2 - ctx->alpha*ctx->alpha + ctx->sigma);
269: VecDestroy(&d1);
270: VecDestroy(&d2);
271: VecRestoreArray(diag,&pd);
272: return(0);
273: }
277: /*
278: Function for user-defined stopping test.
280: Ignores the value of nev. It only takes into account the number of
281: eigenpairs that have converged in recent outer iterations (restarts);
282: if no new eigenvalus have converged in the last few restarts,
283: we stop the iteration, assuming that no more eigenvalues are present
284: inside the region.
285: */
286: PetscErrorCode MyStoppingTest(EPS eps,PetscInt its,PetscInt max_it,PetscInt nconv,PetscInt nev,EPSConvergedReason *reason,void *ptr)
287: {
289: CTX_BRUSSEL *ctx = (CTX_BRUSSEL*)ptr;
292: /* check usual termination conditions, but ignoring the case nconv>=nev */
293: EPSStoppingBasic(eps,its,max_it,nconv,PETSC_MAX_INT,reason,NULL);
294: if (*reason==EPS_CONVERGED_ITERATING) {
295: /* check if nconv is the same as before */
296: if (nconv==ctx->lastnconv) ctx->nreps++;
297: else {
298: ctx->lastnconv = nconv;
299: ctx->nreps = 0;
300: }
301: /* check if no eigenvalues converged in last 10 restarts */
302: if (nconv && ctx->nreps>10) *reason = EPS_CONVERGED_USER;
303: }
304: return(0);
305: }