Actual source code: ex46.c
slepc-3.15.0 2021-03-31
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2021, 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 passing a sparser matrix to build the preconditioner.\n\n"
12: "The command line options are:\n"
13: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
14: " -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
16: #include <slepceps.h>
18: int main(int argc,char **argv)
19: {
20: Mat A,A0; /* operator matrix */
21: EPS eps; /* eigenproblem solver context */
22: ST st;
23: PetscInt N,n=24,m,Istart,Iend,II,i,j;
24: PetscBool flag,terse;
27: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
29: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
30: PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
31: if (!flag) m=n;
32: N = n*m;
33: PetscPrintf(PETSC_COMM_WORLD,"\nModified 2-D Laplacian Eigenproblem, N=%D (%Dx%D grid)\n\n",N,n,m);
35: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36: Compute the operator matrix A and a sparser approximation A0
37: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
39: MatCreate(PETSC_COMM_WORLD,&A);
40: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
41: MatSetFromOptions(A);
42: MatSetUp(A);
43: MatGetOwnershipRange(A,&Istart,&Iend);
44: for (II=Istart;II<Iend;II++) {
45: i = II/n; j = II-i*n;
46: if (i>0) { MatSetValue(A,II,II-n,-0.2,INSERT_VALUES); }
47: if (i<m-1) { MatSetValue(A,II,II+n,-0.2,INSERT_VALUES); }
48: if (j>0) { MatSetValue(A,II,II-1,-3.0,INSERT_VALUES); }
49: if (j<n-1) { MatSetValue(A,II,II+1,-3.0,INSERT_VALUES); }
50: MatSetValue(A,II,II,7.0,INSERT_VALUES);
51: }
52: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
53: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
55: MatCreate(PETSC_COMM_WORLD,&A0);
56: MatSetSizes(A0,PETSC_DECIDE,PETSC_DECIDE,N,N);
57: MatSetFromOptions(A0);
58: MatSetUp(A0);
59: MatGetOwnershipRange(A0,&Istart,&Iend);
60: for (II=Istart;II<Iend;II++) {
61: i = II/n; j = II-i*n;
62: if (j>0) { MatSetValue(A0,II,II-1,-3.0,INSERT_VALUES); }
63: if (j<n-1) { MatSetValue(A0,II,II+1,-3.0,INSERT_VALUES); }
64: MatSetValue(A0,II,II,7.0,INSERT_VALUES);
65: }
66: MatAssemblyBegin(A0,MAT_FINAL_ASSEMBLY);
67: MatAssemblyEnd(A0,MAT_FINAL_ASSEMBLY);
69: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70: Create the eigensolver and set various options
71: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
73: EPSCreate(PETSC_COMM_WORLD,&eps);
74: EPSSetOperators(eps,A,NULL);
75: EPSSetProblemType(eps,EPS_HEP);
76: EPSGetST(eps,&st);
77: STSetType(st,STSINVERT);
78: STSetPreconditionerMat(st,A0);
79: EPSSetTarget(eps,0.0);
80: EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
81: EPSSetFromOptions(eps);
83: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84: Solve the eigensystem and display solution
85: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
87: EPSSolve(eps);
89: /* show detailed info unless -terse option is given by user */
90: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
91: if (terse) {
92: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
93: } else {
94: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
95: EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
96: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
97: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
98: }
99: EPSDestroy(&eps);
100: MatDestroy(&A);
101: MatDestroy(&A0);
102: SlepcFinalize();
103: return ierr;
104: }
106: /*TEST
108: testset:
109: args: -eps_nev 4 -terse
110: output_file: output/ex46_1.out
111: requires: !single
112: test:
113: suffix: 1
114: test:
115: suffix: 2
116: args: -st_ksp_type bcgs -st_pc_type bjacobi
118: TEST*/