| 1376 |
slepc |
1 |
/*
|
|
|
2 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 1672 |
slepc |
3 |
SLEPc - Scalable Library for Eigenvalue Problem Computations
|
| 2116 |
eromero |
4 |
Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain
|
| 6 |
dsic.upv.es!jroman |
5 |
|
| 1672 |
slepc |
6 |
This file is part of SLEPc.
|
|
|
7 |
|
|
|
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.
|
|
|
11 |
|
|
|
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.
|
|
|
16 |
|
|
|
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/>.
|
| 1376 |
slepc |
19 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
20 |
*/
|
|
|
21 |
|
| 765 |
dsic.upv.es!jroman |
22 |
static char help[] = "Standard symmetric eigenproblem corresponding to the Laplacian operator in 2 dimensions.\n\n"
|
| 1156 |
slepc |
23 |
"The command line options are:\n"
|
|
|
24 |
" -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
|
| 6 |
dsic.upv.es!jroman |
25 |
" -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
|
|
|
26 |
|
|
|
27 |
#include "slepceps.h"
|
|
|
28 |
|
|
|
29 |
#undef __FUNCT__
|
|
|
30 |
#define __FUNCT__ "main"
|
|
|
31 |
int main( int argc, char **argv )
|
|
|
32 |
{
|
| 983 |
slepc |
33 |
Mat A; /* operator matrix */
|
|
|
34 |
EPS eps; /* eigenproblem solver context */
|
| 1507 |
slepc |
35 |
const EPSType type;
|
| 983 |
slepc |
36 |
PetscReal error, tol, re, im;
|
|
|
37 |
PetscScalar kr, ki;
|
|
|
38 |
PetscErrorCode ierr;
|
| 1961 |
antodo |
39 |
PetscInt N, n=10, m, Istart, Iend, II, nev, maxit, i, j, its, nconv;
|
| 983 |
slepc |
40 |
PetscTruth flag;
|
| 6 |
dsic.upv.es!jroman |
41 |
|
|
|
42 |
SlepcInitialize(&argc,&argv,(char*)0,help);
|
|
|
43 |
|
|
|
44 |
ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
|
|
|
45 |
ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,&flag);CHKERRQ(ierr);
|
|
|
46 |
if( flag==PETSC_FALSE ) m=n;
|
|
|
47 |
N = n*m;
|
|
|
48 |
ierr = PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%d (%dx%d grid)\n\n",N,n,m);CHKERRQ(ierr);
|
|
|
49 |
|
|
|
50 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
51 |
Compute the operator matrix that defines the eigensystem, Ax=kx
|
|
|
52 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
|
|
53 |
|
| 828 |
dsic.upv.es!antodo |
54 |
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
|
|
|
55 |
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);CHKERRQ(ierr);
|
| 6 |
dsic.upv.es!jroman |
56 |
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
|
|
|
57 |
|
|
|
58 |
ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
|
| 1210 |
slepc |
59 |
for( II=Istart; II<Iend; II++ ) {
|
| 1961 |
antodo |
60 |
i = II/n; j = II-i*n;
|
|
|
61 |
if(i>0) { ierr = MatSetValue(A,II,II-n,-1.0,INSERT_VALUES);CHKERRQ(ierr); }
|
|
|
62 |
if(i<m-1) { ierr = MatSetValue(A,II,II+n,-1.0,INSERT_VALUES);CHKERRQ(ierr); }
|
|
|
63 |
if(j>0) { ierr = MatSetValue(A,II,II-1,-1.0,INSERT_VALUES);CHKERRQ(ierr); }
|
|
|
64 |
if(j<n-1) { ierr = MatSetValue(A,II,II+1,-1.0,INSERT_VALUES);CHKERRQ(ierr); }
|
|
|
65 |
ierr = MatSetValue(A,II,II,4.0,INSERT_VALUES);CHKERRQ(ierr);
|
| 6 |
dsic.upv.es!jroman |
66 |
}
|
|
|
67 |
|
|
|
68 |
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
|
|
|
69 |
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
|
|
|
70 |
|
|
|
71 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
72 |
Create the eigensolver and set various options
|
|
|
73 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
|
|
74 |
|
|
|
75 |
/*
|
|
|
76 |
Create eigensolver context
|
|
|
77 |
*/
|
|
|
78 |
ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRQ(ierr);
|
|
|
79 |
|
|
|
80 |
/*
|
|
|
81 |
Set operators. In this case, it is a standard eigenvalue problem
|
|
|
82 |
*/
|
|
|
83 |
ierr = EPSSetOperators(eps,A,PETSC_NULL);CHKERRQ(ierr);
|
| 408 |
dsic.upv.es!antodo |
84 |
ierr = EPSSetProblemType(eps,EPS_HEP);CHKERRQ(ierr);
|
| 6 |
dsic.upv.es!jroman |
85 |
|
|
|
86 |
/*
|
|
|
87 |
Set solver parameters at runtime
|
|
|
88 |
*/
|
|
|
89 |
ierr = EPSSetFromOptions(eps);CHKERRQ(ierr);
|
|
|
90 |
|
|
|
91 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
92 |
Solve the eigensystem
|
|
|
93 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
|
|
94 |
|
| 78 |
dsic.upv.es!antodo |
95 |
ierr = EPSSolve(eps);CHKERRQ(ierr);
|
|
|
96 |
ierr = EPSGetIterationNumber(eps, &its);CHKERRQ(ierr);
|
| 6 |
dsic.upv.es!jroman |
97 |
ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);CHKERRQ(ierr);
|
|
|
98 |
|
|
|
99 |
/*
|
|
|
100 |
Optional: Get some information from the solver and display it
|
|
|
101 |
*/
|
|
|
102 |
ierr = EPSGetType(eps,&type);CHKERRQ(ierr);
|
|
|
103 |
ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr);
|
| 1575 |
slepc |
104 |
ierr = EPSGetDimensions(eps,&nev,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
|
| 6 |
dsic.upv.es!jroman |
105 |
ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);CHKERRQ(ierr);
|
|
|
106 |
ierr = EPSGetTolerances(eps,&tol,&maxit);CHKERRQ(ierr);
|
|
|
107 |
ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);CHKERRQ(ierr);
|
|
|
108 |
|
|
|
109 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
110 |
Display solution and clean up
|
|
|
111 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
|
|
112 |
|
|
|
113 |
/*
|
| 97 |
dsic.upv.es!antodo |
114 |
Get number of converged approximate eigenpairs
|
| 6 |
dsic.upv.es!jroman |
115 |
*/
|
| 132 |
dsic.upv.es!antodo |
116 |
ierr = EPSGetConverged(eps,&nconv);CHKERRQ(ierr);
|
|
|
117 |
ierr = PetscPrintf(PETSC_COMM_WORLD," Number of converged approximate eigenpairs: %d\n\n",nconv);
|
| 97 |
dsic.upv.es!antodo |
118 |
CHKERRQ(ierr);
|
| 6 |
dsic.upv.es!jroman |
119 |
|
| 132 |
dsic.upv.es!antodo |
120 |
if (nconv>0) {
|
| 6 |
dsic.upv.es!jroman |
121 |
/*
|
|
|
122 |
Display eigenvalues and relative errors
|
|
|
123 |
*/
|
|
|
124 |
ierr = PetscPrintf(PETSC_COMM_WORLD,
|
| 97 |
dsic.upv.es!antodo |
125 |
" k ||Ax-kx||/||kx||\n"
|
|
|
126 |
" ----------------- ------------------\n" );CHKERRQ(ierr);
|
|
|
127 |
|
| 132 |
dsic.upv.es!antodo |
128 |
for( i=0; i<nconv; i++ ) {
|
| 97 |
dsic.upv.es!antodo |
129 |
/*
|
|
|
130 |
Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
|
|
|
131 |
ki (imaginary part)
|
|
|
132 |
*/
|
|
|
133 |
ierr = EPSGetEigenpair(eps,i,&kr,&ki,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
|
|
|
134 |
/*
|
|
|
135 |
Compute the relative error associated to each eigenpair
|
|
|
136 |
*/
|
|
|
137 |
ierr = EPSComputeRelativeError(eps,i,&error);CHKERRQ(ierr);
|
|
|
138 |
|
|
|
139 |
#ifdef PETSC_USE_COMPLEX
|
| 132 |
dsic.upv.es!antodo |
140 |
re = PetscRealPart(kr);
|
|
|
141 |
im = PetscImaginaryPart(kr);
|
|
|
142 |
#else
|
|
|
143 |
re = kr;
|
|
|
144 |
im = ki;
|
| 97 |
dsic.upv.es!antodo |
145 |
#endif
|
| 132 |
dsic.upv.es!antodo |
146 |
if (im!=0.0) {
|
| 1162 |
slepc |
147 |
ierr = PetscPrintf(PETSC_COMM_WORLD," %9f%+9f j %12g\n",re,im,error);CHKERRQ(ierr);
|
| 97 |
dsic.upv.es!antodo |
148 |
} else {
|
| 1162 |
slepc |
149 |
ierr = PetscPrintf(PETSC_COMM_WORLD," %12f %12g\n",re,error);CHKERRQ(ierr);
|
| 97 |
dsic.upv.es!antodo |
150 |
}
|
| 6 |
dsic.upv.es!jroman |
151 |
}
|
|
|
152 |
ierr = PetscPrintf(PETSC_COMM_WORLD,"\n" );CHKERRQ(ierr);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/*
|
|
|
156 |
Free work space
|
|
|
157 |
*/
|
|
|
158 |
ierr = EPSDestroy(eps);CHKERRQ(ierr);
|
|
|
159 |
ierr = MatDestroy(A);CHKERRQ(ierr);
|
|
|
160 |
ierr = SlepcFinalize();CHKERRQ(ierr);
|
|
|
161 |
return 0;
|
|
|
162 |
}
|
|
|
163 |
|