Actual source code: ex2.c
slepc-3.7.3 2016-09-29
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
8: SLEPc is free software: you can redistribute it and/or modify it under the
9: terms of version 3 of the GNU Lesser General Public License as published by
10: the Free Software Foundation.
12: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
13: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15: more details.
17: You should have received a copy of the GNU Lesser General Public License
18: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
19: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: */
22: static char help[] = "Standard symmetric eigenproblem corresponding to the Laplacian operator in 2 dimensions.\n\n"
23: "The command line options are:\n"
24: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
25: " -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
27: #include <slepceps.h>
31: int main(int argc,char **argv)
32: {
33: Mat A; /* operator matrix */
34: EPS eps; /* eigenproblem solver context */
35: EPSType type;
36: PetscInt N,n=10,m,Istart,Iend,II,nev,i,j;
37: PetscBool flag,terse;
40: SlepcInitialize(&argc,&argv,(char*)0,help);
42: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
43: PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
44: if (!flag) m=n;
45: N = n*m;
46: PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%D (%Dx%D grid)\n\n",N,n,m);
48: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49: Compute the operator matrix that defines the eigensystem, Ax=kx
50: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
52: MatCreate(PETSC_COMM_WORLD,&A);
53: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
54: MatSetFromOptions(A);
55: MatSetUp(A);
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: }
67: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
68: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
70: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71: Create the eigensolver and set various options
72: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
74: /*
75: Create eigensolver context
76: */
77: EPSCreate(PETSC_COMM_WORLD,&eps);
79: /*
80: Set operators. In this case, it is a standard eigenvalue problem
81: */
82: EPSSetOperators(eps,A,NULL);
83: EPSSetProblemType(eps,EPS_HEP);
85: /*
86: Set solver parameters at runtime
87: */
88: EPSSetFromOptions(eps);
90: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
91: Solve the eigensystem
92: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
94: EPSSolve(eps);
96: /*
97: Optional: Get some information from the solver and display it
98: */
99: EPSGetType(eps,&type);
100: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
101: EPSGetDimensions(eps,&nev,NULL,NULL);
102: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
104: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105: Display solution and clean up
106: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
108: /* show detailed info unless -terse option is given by user */
109: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
110: if (terse) {
111: EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
112: } else {
113: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
114: EPSReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
115: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
116: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
117: }
118: EPSDestroy(&eps);
119: MatDestroy(&A);
120: SlepcFinalize();
121: return ierr;
122: }