Actual source code: ex41.c
slepc-3.14.0 2020-09-30
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2020, 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 the computation of left eigenvectors.\n\n"
12: "The problem is the Markov model as in ex5.c.\n"
13: "The command line options are:\n"
14: " -m <m>, where <m> = number of grid subdivisions in each dimension.\n\n";
16: #include <slepceps.h>
18: /*
19: User-defined routines
20: */
21: PetscErrorCode MatMarkovModel(PetscInt,Mat);
22: PetscErrorCode ComputeResidualNorm(Mat,PetscBool,PetscScalar,PetscScalar,Vec,Vec,Vec,PetscReal*);
24: int main(int argc,char **argv)
25: {
26: Vec v0,w0; /* initial vectors */
27: Mat A; /* operator matrix */
28: EPS eps; /* eigenproblem solver context */
29: EPSType type;
30: PetscInt i,N,m=15,nconv;
31: PetscBool twosided;
32: PetscReal nrmr,nrml=0.0,re,im,lev;
33: PetscScalar *kr,*ki;
34: Vec t,*xr,*xi,*yr,*yi;
35: PetscMPIInt rank;
38: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
40: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
41: N = m*(m+1)/2;
42: PetscPrintf(PETSC_COMM_WORLD,"\nMarkov Model, N=%D (m=%D)\n\n",N,m);
44: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45: Compute the operator matrix that defines the eigensystem, Ax=kx
46: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
48: MatCreate(PETSC_COMM_WORLD,&A);
49: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
50: MatSetFromOptions(A);
51: MatSetUp(A);
52: MatMarkovModel(m,A);
53: MatCreateVecs(A,NULL,&t);
55: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
56: Create the eigensolver and set various options
57: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
59: EPSCreate(PETSC_COMM_WORLD,&eps);
60: EPSSetOperators(eps,A,NULL);
61: EPSSetProblemType(eps,EPS_NHEP);
63: /* use a two-sided algorithm to compute left eigenvectors as well */
64: EPSSetTwoSided(eps,PETSC_TRUE);
66: /* allow user to change settings at run time */
67: EPSSetFromOptions(eps);
68: EPSGetTwoSided(eps,&twosided);
70: /*
71: Set the initial vectors. This is optional, if not done the initial
72: vectors are set to random values
73: */
74: MatCreateVecs(A,&v0,&w0);
75: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
76: if (!rank) {
77: VecSetValue(v0,0,1.0,INSERT_VALUES);
78: VecSetValue(v0,1,1.0,INSERT_VALUES);
79: VecSetValue(v0,2,1.0,INSERT_VALUES);
80: VecSetValue(w0,0,2.0,INSERT_VALUES);
81: VecSetValue(w0,2,0.5,INSERT_VALUES);
82: }
83: VecAssemblyBegin(v0);
84: VecAssemblyBegin(w0);
85: VecAssemblyEnd(v0);
86: VecAssemblyEnd(w0);
87: EPSSetInitialSpace(eps,1,&v0);
88: EPSSetLeftInitialSpace(eps,1,&w0);
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);
102: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103: Display solution and clean up
104: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
106: /*
107: Get number of converged approximate eigenpairs
108: */
109: EPSGetConverged(eps,&nconv);
110: PetscPrintf(PETSC_COMM_WORLD," Number of converged eigenpairs: %D\n\n",nconv);
111: PetscMalloc2(nconv,&kr,nconv,&ki);
112: VecDuplicateVecs(t,nconv,&xr);
113: VecDuplicateVecs(t,nconv,&xi);
114: if (twosided) {
115: VecDuplicateVecs(t,nconv,&yr);
116: VecDuplicateVecs(t,nconv,&yi);
117: }
119: if (nconv>0) {
120: /*
121: Display eigenvalues and relative errors
122: */
123: PetscPrintf(PETSC_COMM_WORLD,
124: " k ||Ax-kx|| ||y'A-y'k||\n"
125: " ---------------- ------------------ ------------------\n");
127: for (i=0;i<nconv;i++) {
128: /*
129: Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
130: ki (imaginary part)
131: */
132: EPSGetEigenpair(eps,i,&kr[i],&ki[i],xr[i],xi[i]);
133: if (twosided) {
134: EPSGetLeftEigenvector(eps,i,yr[i],yi[i]);
135: }
136: /*
137: Compute the residual norms associated to each eigenpair
138: */
139: ComputeResidualNorm(A,PETSC_FALSE,kr[i],ki[i],xr[i],xi[i],t,&nrmr);
140: if (twosided) {
141: ComputeResidualNorm(A,PETSC_TRUE,kr[i],ki[i],yr[i],yi[i],t,&nrml);
142: }
144: #if defined(PETSC_USE_COMPLEX)
145: re = PetscRealPart(kr[i]);
146: im = PetscImaginaryPart(kr[i]);
147: #else
148: re = kr[i];
149: im = ki[i];
150: #endif
151: if (im!=0.0) {
152: PetscPrintf(PETSC_COMM_WORLD," %8f%+8fi %12g %12g\n",(double)re,(double)im,(double)nrmr,(double)nrml);
153: } else {
154: PetscPrintf(PETSC_COMM_WORLD," %12f %12g %12g\n",(double)re,(double)nrmr,(double)nrml);
155: }
156: }
157: PetscPrintf(PETSC_COMM_WORLD,"\n");
158: /*
159: Check bi-orthogonality of eigenvectors
160: */
161: if (twosided) {
162: VecCheckOrthogonality(xr,nconv,yr,nconv,NULL,NULL,&lev);
163: if (lev<100*PETSC_MACHINE_EPSILON) {
164: PetscPrintf(PETSC_COMM_WORLD," Level of bi-orthogonality of eigenvectors < 100*eps\n\n");
165: } else {
166: PetscPrintf(PETSC_COMM_WORLD," Level of bi-orthogonality of eigenvectors: %g\n\n",(double)lev);
167: }
168: }
169: }
171: EPSDestroy(&eps);
172: MatDestroy(&A);
173: VecDestroy(&v0);
174: VecDestroy(&w0);
175: VecDestroy(&t);
176: PetscFree2(kr,ki);
177: VecDestroyVecs(nconv,&xr);
178: VecDestroyVecs(nconv,&xi);
179: if (twosided) {
180: VecDestroyVecs(nconv,&yr);
181: VecDestroyVecs(nconv,&yi);
182: }
183: SlepcFinalize();
184: return ierr;
185: }
187: /*
188: Matrix generator for a Markov model of a random walk on a triangular grid.
190: This subroutine generates a test matrix that models a random walk on a
191: triangular grid. This test example was used by G. W. Stewart ["{SRRIT} - a
192: FORTRAN subroutine to calculate the dominant invariant subspaces of a real
193: matrix", Tech. report. TR-514, University of Maryland (1978).] and in a few
194: papers on eigenvalue problems by Y. Saad [see e.g. LAA, vol. 34, pp. 269-295
195: (1980) ]. These matrices provide reasonably easy test problems for eigenvalue
196: algorithms. The transpose of the matrix is stochastic and so it is known
197: that one is an exact eigenvalue. One seeks the eigenvector of the transpose
198: associated with the eigenvalue unity. The problem is to calculate the steady
199: state probability distribution of the system, which is the eigevector
200: associated with the eigenvalue one and scaled in such a way that the sum all
201: the components is equal to one.
203: Note: the code will actually compute the transpose of the stochastic matrix
204: that contains the transition probabilities.
205: */
206: PetscErrorCode MatMarkovModel(PetscInt m,Mat A)
207: {
208: const PetscReal cst = 0.5/(PetscReal)(m-1);
209: PetscReal pd,pu;
210: PetscInt Istart,Iend,i,j,jmax,ix=0;
211: PetscErrorCode ierr;
214: MatGetOwnershipRange(A,&Istart,&Iend);
215: for (i=1;i<=m;i++) {
216: jmax = m-i+1;
217: for (j=1;j<=jmax;j++) {
218: ix = ix + 1;
219: if (ix-1<Istart || ix>Iend) continue; /* compute only owned rows */
220: if (j!=jmax) {
221: pd = cst*(PetscReal)(i+j-1);
222: /* north */
223: if (i==1) {
224: MatSetValue(A,ix-1,ix,2*pd,INSERT_VALUES);
225: } else {
226: MatSetValue(A,ix-1,ix,pd,INSERT_VALUES);
227: }
228: /* east */
229: if (j==1) {
230: MatSetValue(A,ix-1,ix+jmax-1,2*pd,INSERT_VALUES);
231: } else {
232: MatSetValue(A,ix-1,ix+jmax-1,pd,INSERT_VALUES);
233: }
234: }
235: /* south */
236: pu = 0.5 - cst*(PetscReal)(i+j-3);
237: if (j>1) {
238: MatSetValue(A,ix-1,ix-2,pu,INSERT_VALUES);
239: }
240: /* west */
241: if (i>1) {
242: MatSetValue(A,ix-1,ix-jmax-2,pu,INSERT_VALUES);
243: }
244: }
245: }
246: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
247: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
248: return(0);
249: }
251: /*
252: ComputeResidualNorm - Computes the norm of the residual vector
253: associated with an eigenpair.
255: Input Parameters:
256: trans - whether A' must be used instead of A
257: kr,ki - eigenvalue
258: xr,xi - eigenvector
259: u - work vector
260: */
261: PetscErrorCode ComputeResidualNorm(Mat A,PetscBool trans,PetscScalar kr,PetscScalar ki,Vec xr,Vec xi,Vec u,PetscReal *norm)
262: {
264: #if !defined(PETSC_USE_COMPLEX)
265: PetscReal ni,nr;
266: #endif
267: PetscErrorCode (*matmult)(Mat,Vec,Vec) = trans? MatMultTranspose: MatMult;
270: #if !defined(PETSC_USE_COMPLEX)
271: if (ki == 0 || PetscAbsScalar(ki) < PetscAbsScalar(kr*PETSC_MACHINE_EPSILON)) {
272: #endif
273: (*matmult)(A,xr,u);
274: if (PetscAbsScalar(kr) > PETSC_MACHINE_EPSILON) {
275: VecAXPY(u,-kr,xr);
276: }
277: VecNorm(u,NORM_2,norm);
278: #if !defined(PETSC_USE_COMPLEX)
279: } else {
280: (*matmult)(A,xr,u);
281: if (SlepcAbsEigenvalue(kr,ki) > PETSC_MACHINE_EPSILON) {
282: VecAXPY(u,-kr,xr);
283: VecAXPY(u,ki,xi);
284: }
285: VecNorm(u,NORM_2,&nr);
286: (*matmult)(A,xi,u);
287: if (SlepcAbsEigenvalue(kr,ki) > PETSC_MACHINE_EPSILON) {
288: VecAXPY(u,-kr,xi);
289: VecAXPY(u,-ki,xr);
290: }
291: VecNorm(u,NORM_2,&ni);
292: *norm = SlepcAbsEigenvalue(nr,ni);
293: }
294: #endif
295: return(0);
296: }
298: /*TEST
300: testset:
301: args: -st_type sinvert -eps_target 1.1 -eps_nev 4
302: filter: grep -v method | sed -e "s/[+-]0\.0*i//g" | sed -e "s/[0-9]\.[0-9]*e[+-]\([0-9]*\)/removed/g"
303: requires: !single
304: output_file: output/ex41_1.out
305: test:
306: suffix: 1
307: args: -eps_type {{power krylovschur}}
308: test:
309: suffix: 1_balance
310: args: -eps_balance {{oneside twoside}} -eps_ncv 18 -eps_krylovschur_locking 0
312: TEST*/