Actual source code: ex46.c
slepc-3.19.2 2023-09-05
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[] = "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;
26: PetscFunctionBeginUser;
27: PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
29: PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
30: PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
31: if (!flag) m=n;
32: N = n*m;
33: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nModified 2-D Laplacian Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));
35: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36: Compute the operator matrix A and a sparser approximation A0
37: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
39: PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
40: PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
41: PetscCall(MatSetFromOptions(A));
42: PetscCall(MatSetUp(A));
43: PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
44: for (II=Istart;II<Iend;II++) {
45: i = II/n; j = II-i*n;
46: if (i>0) PetscCall(MatSetValue(A,II,II-n,-0.2,INSERT_VALUES));
47: if (i<m-1) PetscCall(MatSetValue(A,II,II+n,-0.2,INSERT_VALUES));
48: if (j>0) PetscCall(MatSetValue(A,II,II-1,-3.0,INSERT_VALUES));
49: if (j<n-1) PetscCall(MatSetValue(A,II,II+1,-3.0,INSERT_VALUES));
50: PetscCall(MatSetValue(A,II,II,7.0,INSERT_VALUES));
51: }
52: PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
53: PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
55: PetscCall(MatCreate(PETSC_COMM_WORLD,&A0));
56: PetscCall(MatSetSizes(A0,PETSC_DECIDE,PETSC_DECIDE,N,N));
57: PetscCall(MatSetFromOptions(A0));
58: PetscCall(MatSetUp(A0));
59: PetscCall(MatGetOwnershipRange(A0,&Istart,&Iend));
60: for (II=Istart;II<Iend;II++) {
61: i = II/n; j = II-i*n;
62: if (j>0) PetscCall(MatSetValue(A0,II,II-1,-3.0,INSERT_VALUES));
63: if (j<n-1) PetscCall(MatSetValue(A0,II,II+1,-3.0,INSERT_VALUES));
64: PetscCall(MatSetValue(A0,II,II,7.0,INSERT_VALUES));
65: }
66: PetscCall(MatAssemblyBegin(A0,MAT_FINAL_ASSEMBLY));
67: PetscCall(MatAssemblyEnd(A0,MAT_FINAL_ASSEMBLY));
69: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70: Create the eigensolver and set various options
71: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
73: PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
74: PetscCall(EPSSetOperators(eps,A,NULL));
75: PetscCall(EPSSetProblemType(eps,EPS_HEP));
76: PetscCall(EPSGetST(eps,&st));
77: PetscCall(STSetType(st,STSINVERT));
78: PetscCall(STSetPreconditionerMat(st,A0));
79: PetscCall(EPSSetTarget(eps,0.0));
80: PetscCall(EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE));
81: PetscCall(EPSSetFromOptions(eps));
83: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84: Solve the eigensystem and display solution
85: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
87: PetscCall(EPSSolve(eps));
89: /* show detailed info unless -terse option is given by user */
90: PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
91: if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
92: else {
93: PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
94: PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
95: PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
96: PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
97: }
98: PetscCall(EPSDestroy(&eps));
99: PetscCall(MatDestroy(&A));
100: PetscCall(MatDestroy(&A0));
101: PetscCall(SlepcFinalize());
102: return 0;
103: }
105: /*TEST
107: testset:
108: args: -eps_nev 4 -terse
109: output_file: output/ex46_1.out
110: requires: !single
111: test:
112: suffix: 1
113: test:
114: suffix: 2
115: args: -st_ksp_type bcgs -st_pc_type bjacobi
117: TEST*/