Actual source code: ex43.c
slepc-3.16.0 2021-09-30
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[] = "Generalized eigenproblem, illustrates setting MUMPS options.\n\n"
12: "The problem is Ax = lambda Bx, with:\n"
13: " A = Laplacian operator in 2-D\n"
14: " B = diagonal matrix with all values equal to 4\n\n"
15: "The command line options are:\n"
16: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
17: " -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
19: #include <slepceps.h>
21: int main(int argc,char **argv)
22: {
23: Mat A,B;
24: #if defined(PETSC_HAVE_MUMPS)
25: Mat K;
26: #endif
27: EPS eps;
28: EPSType type;
29: ST st;
30: KSP ksp;
31: PC pc;
32: PetscInt N,n=10,m=12,Istart,Iend,II,nev,i,j;
33: PetscBool flag,terse;
36: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
38: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
39: PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
40: N = n*m;
41: PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized Eigenproblem, N=%D (%Dx%D grid)\n\n",N,n,m);
43: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44: Compute the matrices that define the eigensystem, Ax=kBx
45: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
47: MatCreate(PETSC_COMM_WORLD,&A);
48: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
49: MatSetFromOptions(A);
50: MatSetUp(A);
52: MatCreate(PETSC_COMM_WORLD,&B);
53: MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
54: MatSetFromOptions(B);
55: MatSetUp(B);
57: MatGetOwnershipRange(A,&Istart,&Iend);
58: for (II=Istart;II<Iend;II++) {
59: i = II/n; j = II-i*n;
60: if (i>0) { MatSetValue(A,II,II-n,-1.0,INSERT_VALUES); }
61: if (i<m-1) { MatSetValue(A,II,II+n,-1.0,INSERT_VALUES); }
62: if (j>0) { MatSetValue(A,II,II-1,-1.0,INSERT_VALUES); }
63: if (j<n-1) { MatSetValue(A,II,II+1,-1.0,INSERT_VALUES); }
64: MatSetValue(A,II,II,4.0,INSERT_VALUES);
65: MatSetValue(B,II,II,4.0,INSERT_VALUES);
66: }
68: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
69: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
70: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
71: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
73: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74: Create the eigensolver and set various options
75: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
77: /*
78: Create eigensolver context
79: */
80: EPSCreate(PETSC_COMM_WORLD,&eps);
82: /*
83: Set operators. In this case, it is a generalized eigenvalue problem
84: */
85: EPSSetOperators(eps,A,B);
86: EPSSetProblemType(eps,EPS_GNHEP);
88: /*
89: Set some solver options
90: */
91: EPSSetTarget(eps,1.3);
92: EPSSetDimensions(eps,2,PETSC_DEFAULT,PETSC_DEFAULT);
93: EPSGetST(eps,&st);
94: STSetType(st,STSINVERT);
96: STGetKSP(st,&ksp);
97: KSPSetType(ksp,KSPPREONLY);
98: KSPGetPC(ksp,&pc);
99: PCSetType(pc,PCLU);
101: /*
102: Set MUMPS options if available
103: */
104: #if defined(PETSC_HAVE_MUMPS)
105: PCFactorSetMatSolverType(pc,MATSOLVERMUMPS);
106: /* the next line is required to force the creation of the ST operator and its passing to KSP */
107: STGetOperator(st,NULL);
108: PCFactorSetUpMatSolverType(pc);
109: PCFactorGetMatrix(pc,&K);
110: MatMumpsSetIcntl(K,14,50);
111: MatMumpsSetCntl(K,3,1e-12);
112: #endif
114: /*
115: Let the user change settings at runtime
116: */
117: EPSSetFromOptions(eps);
119: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120: Solve the eigensystem
121: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
123: EPSSolve(eps);
125: /*
126: Optional: Get some information from the solver and display it
127: */
128: EPSGetType(eps,&type);
129: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
130: EPSGetDimensions(eps,&nev,NULL,NULL);
131: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
133: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134: Display solution and clean up
135: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
137: /* show detailed info unless -terse option is given by user */
138: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
139: if (terse) {
140: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
141: } else {
142: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
143: EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
144: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
145: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
146: }
147: EPSDestroy(&eps);
148: MatDestroy(&A);
149: MatDestroy(&B);
150: SlepcFinalize();
151: return ierr;
152: }
154: /*TEST
156: testset:
157: args: -terse
158: output_file: output/ex43_1.out
159: test:
160: suffix: 1
161: test:
162: suffix: 2
163: nsize: 2
164: args: -st_pc_factor_mat_solver_type mumps
165: requires: mumps
167: TEST*/