Actual source code: ex43.c
slepc-3.17.1 2022-04-11
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: */
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;
35: SlepcInitialize(&argc,&argv,(char*)0,help);
37: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
38: PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
39: N = n*m;
40: PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);
42: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43: Compute the matrices that define the eigensystem, Ax=kBx
44: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
46: MatCreate(PETSC_COMM_WORLD,&A);
47: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
48: MatSetFromOptions(A);
49: MatSetUp(A);
51: MatCreate(PETSC_COMM_WORLD,&B);
52: MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
53: MatSetFromOptions(B);
54: MatSetUp(B);
56: MatGetOwnershipRange(A,&Istart,&Iend);
57: for (II=Istart;II<Iend;II++) {
58: i = II/n; j = II-i*n;
59: if (i>0) MatSetValue(A,II,II-n,-1.0,INSERT_VALUES);
60: if (i<m-1) MatSetValue(A,II,II+n,-1.0,INSERT_VALUES);
61: if (j>0) MatSetValue(A,II,II-1,-1.0,INSERT_VALUES);
62: if (j<n-1) MatSetValue(A,II,II+1,-1.0,INSERT_VALUES);
63: MatSetValue(A,II,II,4.0,INSERT_VALUES);
64: MatSetValue(B,II,II,4.0,INSERT_VALUES);
65: }
67: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
68: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
69: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
70: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
72: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73: Create the eigensolver and set various options
74: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
76: /*
77: Create eigensolver context
78: */
79: EPSCreate(PETSC_COMM_WORLD,&eps);
81: /*
82: Set operators. In this case, it is a generalized eigenvalue problem
83: */
84: EPSSetOperators(eps,A,B);
85: EPSSetProblemType(eps,EPS_GNHEP);
87: /*
88: Set some solver options
89: */
90: EPSSetTarget(eps,1.3);
91: EPSSetDimensions(eps,2,PETSC_DEFAULT,PETSC_DEFAULT);
92: EPSGetST(eps,&st);
93: STSetType(st,STSINVERT);
95: STGetKSP(st,&ksp);
96: KSPSetType(ksp,KSPPREONLY);
97: KSPGetPC(ksp,&pc);
98: PCSetType(pc,PCLU);
100: /*
101: Set MUMPS options if available
102: */
103: #if defined(PETSC_HAVE_MUMPS)
104: PCFactorSetMatSolverType(pc,MATSOLVERMUMPS);
105: /* the next line is required to force the creation of the ST operator and its passing to KSP */
106: STGetOperator(st,NULL);
107: PCFactorSetUpMatSolverType(pc);
108: PCFactorGetMatrix(pc,&K);
109: MatMumpsSetIcntl(K,14,50);
110: MatMumpsSetCntl(K,3,1e-12);
111: #endif
113: /*
114: Let the user change settings at runtime
115: */
116: EPSSetFromOptions(eps);
118: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119: Solve the eigensystem
120: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
122: EPSSolve(eps);
124: /*
125: Optional: Get some information from the solver and display it
126: */
127: EPSGetType(eps,&type);
128: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
129: EPSGetDimensions(eps,&nev,NULL,NULL);
130: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);
132: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133: Display solution and clean up
134: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136: /* show detailed info unless -terse option is given by user */
137: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
138: if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
139: else {
140: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
141: EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
142: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
143: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
144: }
145: EPSDestroy(&eps);
146: MatDestroy(&A);
147: MatDestroy(&B);
148: SlepcFinalize();
149: return 0;
150: }
152: /*TEST
154: testset:
155: args: -terse
156: output_file: output/ex43_1.out
157: test:
158: suffix: 1
159: test:
160: suffix: 2
161: nsize: 2
162: args: -st_pc_factor_mat_solver_type mumps
163: requires: mumps
165: TEST*/