Actual source code: test2.c

slepc-3.21.1 2024-04-26
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: */
 10: /*
 11:    Example based on spring problem in NLEVP collection [1]. See the parameters
 12:    meaning at Example 2 in [2].

 14:    [1] T. Betcke, N. J. Higham, V. Mehrmann, C. Schroder, and F. Tisseur,
 15:        NLEVP: A Collection of Nonlinear Eigenvalue Problems, MIMS EPrint
 16:        2010.98, November 2010.
 17:    [2] F. Tisseur, Backward error and condition of polynomial eigenvalue
 18:        problems, Linear Algebra and its Applications, 309 (2000), pp. 339--361,
 19:        April 2000.
 20: */

 22: static char help[] = "Test the solution of a PEP from a finite element model of "
 23:   "damped mass-spring system (problem from NLEVP collection).\n\n"
 24:   "The command line options are:\n"
 25:   "  -n <n> ... number of grid subdivisions.\n"
 26:   "  -mu <value> ... mass (default 1).\n"
 27:   "  -tau <value> ... damping constant of the dampers (default 10).\n"
 28:   "  -kappa <value> ... damping constant of the springs (default 5).\n"
 29:   "  -initv ... set an initial vector.\n\n";

 31: #include <slepcpep.h>

 33: /*
 34:    Check if computed eigenvectors have unit norm
 35: */
 36: PetscErrorCode CheckNormalizedVectors(PEP pep)
 37: {
 38:   PetscInt       i,nconv;
 39:   Mat            A;
 40:   Vec            xr,xi;
 41:   PetscReal      error=0.0,normr;
 42: #if !defined(PETSC_USE_COMPLEX)
 43:   PetscReal      normi;
 44: #endif

 46:   PetscFunctionBeginUser;
 47:   PetscCall(PEPGetConverged(pep,&nconv));
 48:   if (nconv>0) {
 49:     PetscCall(PEPGetOperators(pep,0,&A));
 50:     PetscCall(MatCreateVecs(A,&xr,&xi));
 51:     for (i=0;i<nconv;i++) {
 52:       PetscCall(PEPGetEigenpair(pep,i,NULL,NULL,xr,xi));
 53: #if defined(PETSC_USE_COMPLEX)
 54:       PetscCall(VecNorm(xr,NORM_2,&normr));
 55:       error = PetscMax(error,PetscAbsReal(normr-PetscRealConstant(1.0)));
 56: #else
 57:       PetscCall(VecNormBegin(xr,NORM_2,&normr));
 58:       PetscCall(VecNormBegin(xi,NORM_2,&normi));
 59:       PetscCall(VecNormEnd(xr,NORM_2,&normr));
 60:       PetscCall(VecNormEnd(xi,NORM_2,&normi));
 61:       error = PetscMax(error,PetscAbsReal(SlepcAbsEigenvalue(normr,normi)-PetscRealConstant(1.0)));
 62: #endif
 63:     }
 64:     PetscCall(VecDestroy(&xr));
 65:     PetscCall(VecDestroy(&xi));
 66:     if (error>100*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Vectors are not normalized. Error=%g\n",(double)error));
 67:   }
 68:   PetscFunctionReturn(PETSC_SUCCESS);
 69: }

 71: int main(int argc,char **argv)
 72: {
 73:   Mat            M,C,K,A[3];      /* problem matrices */
 74:   PEP            pep;             /* polynomial eigenproblem solver context */
 75:   PetscInt       n=30,Istart,Iend,i,nev;
 76:   PetscReal      mu=1.0,tau=10.0,kappa=5.0;
 77:   PetscBool      initv=PETSC_FALSE,skipnorm=PETSC_FALSE;
 78:   Vec            IV[2];

 80:   PetscFunctionBeginUser;
 81:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));

 83:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
 84:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-mu",&mu,NULL));
 85:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
 86:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL));
 87:   PetscCall(PetscOptionsGetBool(NULL,NULL,"-initv",&initv,NULL));
 88:   PetscCall(PetscOptionsGetBool(NULL,NULL,"-skipnorm",&skipnorm,NULL));

 90:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 91:      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
 92:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 94:   /* K is a tridiagonal */
 95:   PetscCall(MatCreate(PETSC_COMM_WORLD,&K));
 96:   PetscCall(MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,n,n));
 97:   PetscCall(MatSetFromOptions(K));

 99:   PetscCall(MatGetOwnershipRange(K,&Istart,&Iend));
100:   for (i=Istart;i<Iend;i++) {
101:     if (i>0) PetscCall(MatSetValue(K,i,i-1,-kappa,INSERT_VALUES));
102:     PetscCall(MatSetValue(K,i,i,kappa*3.0,INSERT_VALUES));
103:     if (i<n-1) PetscCall(MatSetValue(K,i,i+1,-kappa,INSERT_VALUES));
104:   }

106:   PetscCall(MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY));
107:   PetscCall(MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY));

109:   /* C is a tridiagonal */
110:   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
111:   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n));
112:   PetscCall(MatSetFromOptions(C));

114:   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
115:   for (i=Istart;i<Iend;i++) {
116:     if (i>0) PetscCall(MatSetValue(C,i,i-1,-tau,INSERT_VALUES));
117:     PetscCall(MatSetValue(C,i,i,tau*3.0,INSERT_VALUES));
118:     if (i<n-1) PetscCall(MatSetValue(C,i,i+1,-tau,INSERT_VALUES));
119:   }

121:   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
122:   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));

124:   /* M is a diagonal matrix */
125:   PetscCall(MatCreate(PETSC_COMM_WORLD,&M));
126:   PetscCall(MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,n,n));
127:   PetscCall(MatSetFromOptions(M));
128:   PetscCall(MatGetOwnershipRange(M,&Istart,&Iend));
129:   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(M,i,i,mu,INSERT_VALUES));
130:   PetscCall(MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY));
131:   PetscCall(MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY));

133:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134:                 Create the eigensolver and set various options
135:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

137:   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
138:   A[0] = K; A[1] = C; A[2] = M;
139:   PetscCall(PEPSetOperators(pep,3,A));
140:   PetscCall(PEPSetProblemType(pep,PEP_GENERAL));
141:   PetscCall(PEPSetTolerances(pep,PETSC_SMALL,PETSC_DEFAULT));
142:   if (initv) { /* initial vector */
143:     PetscCall(MatCreateVecs(K,&IV[0],NULL));
144:     PetscCall(VecSetValue(IV[0],0,-1.0,INSERT_VALUES));
145:     PetscCall(VecSetValue(IV[0],1,0.5,INSERT_VALUES));
146:     PetscCall(VecAssemblyBegin(IV[0]));
147:     PetscCall(VecAssemblyEnd(IV[0]));
148:     PetscCall(MatCreateVecs(K,&IV[1],NULL));
149:     PetscCall(VecSetValue(IV[1],0,4.0,INSERT_VALUES));
150:     PetscCall(VecSetValue(IV[1],2,1.5,INSERT_VALUES));
151:     PetscCall(VecAssemblyBegin(IV[1]));
152:     PetscCall(VecAssemblyEnd(IV[1]));
153:     PetscCall(PEPSetInitialSpace(pep,2,IV));
154:     PetscCall(VecDestroy(&IV[0]));
155:     PetscCall(VecDestroy(&IV[1]));
156:   }
157:   PetscCall(PEPSetFromOptions(pep));

159:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160:                       Solve the eigensystem
161:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

163:   PetscCall(PEPSolve(pep));
164:   PetscCall(PEPGetDimensions(pep,&nev,NULL,NULL));
165:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));

167:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168:                     Display solution and clean up
169:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

171:   PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
172:   if (!skipnorm) PetscCall(CheckNormalizedVectors(pep));
173:   PetscCall(PEPDestroy(&pep));
174:   PetscCall(MatDestroy(&M));
175:   PetscCall(MatDestroy(&C));
176:   PetscCall(MatDestroy(&K));
177:   PetscCall(SlepcFinalize());
178:   return 0;
179: }

181: /*TEST

183:    testset:
184:       args: -pep_nev 4 -initv
185:       requires: !single
186:       output_file: output/test2_1.out
187:       test:
188:          suffix: 1
189:          args: -pep_type {{toar linear}}
190:       test:
191:          suffix: 1_toar_mgs
192:          args: -pep_type toar -bv_orthog_type mgs
193:       test:
194:          suffix: 1_qarnoldi
195:          args: -pep_type qarnoldi -bv_orthog_refine never
196:       test:
197:          suffix: 1_linear_gd
198:          args: -pep_type linear -pep_linear_eps_type gd -pep_linear_explicitmatrix

200:    testset:
201:       args: -pep_target -0.43 -pep_nev 4 -pep_ncv 20 -st_type sinvert
202:       output_file: output/test2_2.out
203:       test:
204:          suffix: 2
205:          args: -pep_type {{toar linear}}
206:       test:
207:          suffix: 2_toar_scaleboth
208:          args: -pep_type toar -pep_scale both
209:       test:
210:          suffix: 2_toar_transform
211:          args: -pep_type toar -st_transform
212:       test:
213:          suffix: 2_qarnoldi
214:          args: -pep_type qarnoldi -bv_orthog_refine always
215:       test:
216:          suffix: 2_linear_explicit
217:          args: -pep_type linear -pep_linear_explicitmatrix -pep_linear_linearization 0,1
218:       test:
219:          suffix: 2_linear_explicit_her
220:          args: -pep_type linear -pep_linear_explicitmatrix -pep_hermitian -pep_linear_linearization 0,1
221:       test:
222:          suffix: 2_stoar
223:          args: -pep_type stoar -pep_hermitian
224:       test:
225:          suffix: 2_jd
226:          args: -pep_type jd -st_type precond -pep_max_it 200 -pep_ncv 24
227:          requires: !single

229:    test:
230:       suffix: 3
231:       args: -pep_nev 12 -pep_extract {{none norm residual structured}} -pep_monitor_cancel
232:       requires: !single

234:    testset:
235:       args: -st_type sinvert -pep_target -0.43 -pep_nev 4
236:       output_file: output/test2_2.out
237:       test:
238:          suffix: 4_schur
239:          args: -pep_refine simple -pep_refine_scheme schur
240:       test:
241:          suffix: 4_mbe
242:          args: -pep_refine simple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
243:       test:
244:          suffix: 4_explicit
245:          args: -pep_refine simple -pep_refine_scheme explicit
246:       test:
247:          suffix: 4_multiple_schur
248:          args: -pep_refine multiple -pep_refine_scheme schur
249:          requires: !single
250:       test:
251:          suffix: 4_multiple_mbe
252:          args: -pep_refine multiple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu -pep_refine_pc_factor_shift_type nonzero
253:       test:
254:          suffix: 4_multiple_explicit
255:          args: -pep_refine multiple -pep_refine_scheme explicit
256:          requires: !single

258:    test:
259:       suffix: 5
260:       nsize: 2
261:       args: -pep_type linear -pep_linear_explicitmatrix -pep_general -pep_target -0.43 -pep_nev 4 -pep_ncv 20 -st_type sinvert -pep_linear_st_ksp_type bcgs -pep_linear_st_pc_type bjacobi
262:       output_file: output/test2_2.out

264:    test:
265:       suffix: 6
266:       args: -pep_type linear -pep_nev 12 -pep_extract {{none norm residual}}
267:       requires: !single
268:       output_file: output/test2_3.out

270:    test:
271:       suffix: 7
272:       args: -pep_nev 12 -pep_extract {{none norm residual structured}} -pep_refine multiple
273:       requires: !single
274:       output_file: output/test2_3.out

276:    testset:
277:       args: -st_type sinvert -pep_target -0.43 -pep_nev 4 -st_transform
278:       output_file: output/test2_2.out
279:       test:
280:          suffix: 8_schur
281:          args: -pep_refine simple -pep_refine_scheme schur
282:       test:
283:          suffix: 8_mbe
284:          args: -pep_refine simple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
285:       test:
286:          suffix: 8_explicit
287:          args: -pep_refine simple -pep_refine_scheme explicit
288:       test:
289:          suffix: 8_multiple_schur
290:          args: -pep_refine multiple -pep_refine_scheme schur
291:       test:
292:          suffix: 8_multiple_mbe
293:          args: -pep_refine multiple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
294:       test:
295:          suffix: 8_multiple_explicit
296:          args: -pep_refine multiple -pep_refine_scheme explicit

298:    testset:
299:       nsize: 2
300:       args: -st_type sinvert -pep_target -0.49 -pep_nev 4 -pep_refine_partitions 2 -st_ksp_type bcgs -st_pc_type bjacobi -pep_scale diagonal -pep_scale_its 4
301:       output_file: output/test2_2.out
302:       test:
303:          suffix: 9_mbe
304:          args: -pep_refine simple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
305:       test:
306:          suffix: 9_explicit
307:          args: -pep_refine simple -pep_refine_scheme explicit
308:       test:
309:          suffix: 9_multiple_mbe
310:          args: -pep_refine multiple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
311:          requires: !single
312:       test:
313:          suffix: 9_multiple_explicit
314:          args: -pep_refine multiple -pep_refine_scheme explicit
315:          requires: !single

317:    test:
318:       suffix: 10
319:       nsize: 4
320:       args: -st_type sinvert -pep_target -0.43 -pep_nev 4 -pep_refine simple -pep_refine_scheme explicit -pep_refine_partitions 2 -st_ksp_type bcgs -st_pc_type bjacobi -pep_scale diagonal -pep_scale_its 4
321:       output_file: output/test2_2.out

323:    testset:
324:       args: -pep_nev 4 -initv -mat_type aijcusparse
325:       output_file: output/test2_1.out
326:       requires: cuda !single
327:       test:
328:          suffix: 11_cuda
329:          args: -pep_type {{toar linear}}
330:       test:
331:          suffix: 11_cuda_qarnoldi
332:          args: -pep_type qarnoldi -bv_orthog_refine never
333:       test:
334:          suffix: 11_cuda_linear_gd
335:          args: -pep_type linear -pep_linear_eps_type gd -pep_linear_explicitmatrix

337:    test:
338:       suffix: 12
339:       nsize: 2
340:       args: -pep_type jd -ds_parallel synchronized -pep_target -0.43 -pep_nev 4 -pep_ncv 20
341:       requires: !single

343:    test:
344:       suffix: 13
345:       args: -pep_nev 12 -pep_view_values draw -pep_monitor draw::draw_lg
346:       requires: x !single
347:       output_file: output/test2_3.out

349:    test:
350:       suffix: 14
351:       requires: complex double
352:       args: -pep_type ciss -rg_type ellipse -rg_ellipse_center -48.5 -rg_ellipse_radius 1.5 -pep_ciss_delta 1e-10

354: TEST*/