Actual source code: test4.c
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2013, 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[] = "Test the solution of a HEP without calling EPSSetFromOptions (based on ex1.c).\n\n"
23: "The command line options are:\n"
24: " -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n"
25: " -type <eps_type> = eps type to test.\n\n";
27: #include <slepceps.h>
31: int main(int argc,char **argv)
32: {
33: Mat A; /* problem matrix */
34: EPS eps; /* eigenproblem solver context */
35: EPSType type;
36: PetscReal tol=1000*PETSC_MACHINE_EPSILON;
37: PetscScalar value[3];
38: PetscInt n=30,i,Istart,Iend,col[3],nev;
39: PetscBool FirstBlock=PETSC_FALSE,LastBlock=PETSC_FALSE,isgd2;
40: char epstype[30] = "krylovschur";
43: SlepcInitialize(&argc,&argv,(char*)0,help);
45: PetscOptionsGetInt(NULL,"-n",&n,NULL);
46: PetscOptionsGetString(NULL,"-type",epstype,30,NULL);
47: PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%D",n);
48: PetscPrintf(PETSC_COMM_WORLD,"\nEPS type: %s\n\n",epstype);
50: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51: Compute the operator matrix that defines the eigensystem, Ax=kx
52: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
54: MatCreate(PETSC_COMM_WORLD,&A);
55: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
56: MatSetFromOptions(A);
57: MatSetUp(A);
59: MatGetOwnershipRange(A,&Istart,&Iend);
60: if (Istart==0) FirstBlock=PETSC_TRUE;
61: if (Iend==n) LastBlock=PETSC_TRUE;
62: value[0]=-1.0; value[1]=2.0; value[2]=-1.0;
63: for (i=(FirstBlock? Istart+1: Istart); i<(LastBlock? Iend-1: Iend); i++) {
64: col[0]=i-1; col[1]=i; col[2]=i+1;
65: MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
66: }
67: if (LastBlock) {
68: i=n-1; col[0]=n-2; col[1]=n-1;
69: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
70: }
71: if (FirstBlock) {
72: i=0; col[0]=0; col[1]=1; value[0]=2.0; value[1]=-1.0;
73: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
74: }
76: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
77: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
79: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80: Create the eigensolver and set various options
81: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
82: /*
83: Create eigensolver context
84: */
85: EPSCreate(PETSC_COMM_WORLD,&eps);
87: /*
88: Set operators. In this case, it is a standard eigenvalue problem
89: */
90: EPSSetOperators(eps,A,NULL);
91: EPSSetProblemType(eps,EPS_HEP);
92: EPSSetDimensions(eps,4,PETSC_DEFAULT,PETSC_DEFAULT);
93: EPSSetTolerances(eps,tol,PETSC_DEFAULT);
95: /*
96: Set solver parameters at runtime
97: */
98: PetscStrcmp(epstype,"gd2",&isgd2);
99: if (isgd2) {
100: EPSSetType(eps,EPSGD);
101: EPSGDSetDoubleExpansion(eps,PETSC_TRUE);
102: } else {
103: EPSSetType(eps,epstype);
104: }
106: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107: Solve the eigensystem
108: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
110: EPSSolve(eps);
111: /*
112: Optional: Get some information from the solver and display it
113: */
114: EPSGetType(eps,&type);
115: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
116: EPSGetDimensions(eps,&nev,NULL,NULL);
117: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
119: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120: Display solution and clean up
121: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
123: EPSPrintSolution(eps,NULL);
124: EPSDestroy(&eps);
125: MatDestroy(&A);
126: SlepcFinalize();
127: return 0;
128: }