Actual source code: ex35.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[] = "Shell spectral transformations with a non-injective mapping. "
12: "Implements spectrum folding for the 2-D Laplacian, as in ex24.c.\n\n"
13: "The command line options are:\n"
14: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
15: " -m <m>, where <m> = number of grid subdivisions in y dimension.\n";
17: #include <slepceps.h>
19: /* Context for spectrum folding spectral transformation */
20: typedef struct {
21: Mat A;
22: Vec w;
23: PetscScalar target;
24: } FoldShellST;
26: /* Routines for shell spectral transformation */
27: PetscErrorCode STCreate_Fold(Mat,PetscScalar,FoldShellST**);
28: PetscErrorCode STApply_Fold(ST,Vec,Vec);
29: PetscErrorCode STDestroy_Fold(FoldShellST*);
31: int main (int argc,char **argv)
32: {
33: Mat A; /* operator matrix */
34: EPS eps; /* eigenproblem solver context */
35: ST st; /* spectral transformation context */
36: FoldShellST *fold; /* user-defined spectral transform context */
37: EPSType type;
38: PetscInt N,n=10,m,i,j,II,Istart,Iend,nev;
39: PetscBool isShell,terse,flag;
40: PetscScalar target=1.1;
43: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
45: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
46: PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
47: if (!flag) m = n;
48: PetscOptionsGetScalar(NULL,NULL,"-target",&target,NULL);
49: N = n*m;
50: PetscPrintf(PETSC_COMM_WORLD,"\nSpectrum Folding via shell ST, N=%D (%Dx%D grid) target=%3.2f\n\n",N,n,m,(double)PetscRealPart(target));
52: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53: Compute the 5-point stencil Laplacian
54: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
56: MatCreate(PETSC_COMM_WORLD,&A);
57: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
58: MatSetFromOptions(A);
59: MatSetUp(A);
61: MatGetOwnershipRange(A,&Istart,&Iend);
62: for (II=Istart;II<Iend;II++) {
63: i = II/n; j = II-i*n;
64: if (i>0) { MatSetValue(A,II,II-n,-1.0,INSERT_VALUES); }
65: if (i<m-1) { MatSetValue(A,II,II+n,-1.0,INSERT_VALUES); }
66: if (j>0) { MatSetValue(A,II,II-1,-1.0,INSERT_VALUES); }
67: if (j<n-1) { MatSetValue(A,II,II+1,-1.0,INSERT_VALUES); }
68: MatSetValue(A,II,II,4.0,INSERT_VALUES);
69: }
70: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
71: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
73: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74: Create the eigensolver and set various options
75: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
77: EPSCreate(PETSC_COMM_WORLD,&eps);
78: EPSSetOperators(eps,A,NULL);
79: EPSSetProblemType(eps,EPS_HEP);
80: EPSSetTarget(eps,target);
81: EPSGetST(eps,&st);
82: STSetType(st,STSHELL);
83: EPSSetFromOptions(eps);
85: /*
86: Initialize shell spectral transformation
87: */
88: PetscObjectTypeCompare((PetscObject)st,STSHELL,&isShell);
89: if (isShell) {
90: /* Change sorting criterion since this shell ST computes eigenvalues
91: of the transformed operator closest to 0 */
92: EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);
94: /* Create the context for the user-defined spectral transform */
95: STCreate_Fold(A,target,&fold);
97: /* Set callback function for applying the operator (in this case we do not
98: provide a back-transformation callback since the mapping is not one-to-one) */
99: STShellSetApply(st,STApply_Fold);
100: STShellSetContext(st,fold);
101: PetscObjectSetName((PetscObject)st,"STFOLD");
102: }
104: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105: Solve the eigensystem
106: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
108: EPSSolve(eps);
109: EPSGetType(eps,&type);
110: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
111: EPSGetDimensions(eps,&nev,NULL,NULL);
112: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
114: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115: Display solution and clean up
116: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
118: /* show detailed info unless -terse option is given by user */
119: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
120: if (terse) {
121: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
122: } else {
123: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
124: EPSReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
125: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
126: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
127: }
128: if (isShell) {
129: STDestroy_Fold(fold);
130: }
131: EPSDestroy(&eps);
132: MatDestroy(&A);
133: SlepcFinalize();
134: return ierr;
135: }
137: /*
138: STCreate_Fold - Creates the spectrum folding ST context.
140: Input Parameter:
141: + A - problem matrix
142: - target - target value
144: Output Parameter:
145: . fold - user-defined spectral transformation context
146: */
147: PetscErrorCode STCreate_Fold(Mat A,PetscScalar target,FoldShellST **fold)
148: {
149: FoldShellST *newctx;
153: PetscNew(&newctx);
154: newctx->A = A;
155: PetscObjectReference((PetscObject)A);
156: newctx->target = target;
157: MatCreateVecs(A,&newctx->w,NULL);
158: *fold = newctx;
159: return(0);
160: }
162: /*
163: STApply_Fold - Applies the operator (A-target*I)^2 to a given vector.
165: Input Parameters:
166: + st - spectral transformation context
167: - x - input vector
169: Output Parameter:
170: . y - output vector
171: */
172: PetscErrorCode STApply_Fold(ST st,Vec x,Vec y)
173: {
174: FoldShellST *fold;
175: PetscScalar sigma;
179: STShellGetContext(st,(void**)&fold);
180: sigma = -fold->target;
181: MatMult(fold->A,x,fold->w);
182: VecAXPY(fold->w,sigma,x);
183: MatMult(fold->A,fold->w,y);
184: VecAXPY(y,sigma,fold->w);
185: return(0);
186: }
188: /*
189: STDestroy_Fold - This routine destroys the shell ST context.
191: Input Parameter:
192: . fold - user-defined spectral transformation context
193: */
194: PetscErrorCode STDestroy_Fold(FoldShellST *fold)
195: {
199: MatDestroy(&fold->A);
200: VecDestroy(&fold->w);
201: PetscFree(fold);
202: return(0);
203: }
205: /*TEST
207: testset:
208: args: -m 11 -eps_nev 4 -terse
209: output_file: output/ex35_1.out
210: test:
211: suffix: 1
212: requires: !single
213: test:
214: suffix: 1_single
215: args: -eps_tol 1e-5
216: requires: single
218: TEST*/