Actual source code: test9.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[] = "Eigenvalue problem associated with a Markov model of a random walk on a triangular grid. "
23: "It is a standard nonsymmetric eigenproblem with real eigenvalues and the rightmost eigenvalue is known to be 1.\n"
24: "This example illustrates how the user can set the initial vector.\n\n"
25: "The command line options are:\n"
26: " -m <m>, where <m> = number of grid subdivisions in each dimension.\n\n";
28: #include <slepceps.h>
30: /*
31: User-defined routines
32: */
33: PetscErrorCode MatMarkovModel(PetscInt m,Mat A);
34: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx);
38: int main(int argc,char **argv)
39: {
40: Vec v0; /* initial vector */
41: Mat A; /* operator matrix */
42: EPS eps; /* eigenproblem solver context */
43: EPSType type;
44: PetscReal tol=1000*PETSC_MACHINE_EPSILON;
45: PetscInt N,m=15,nev;
46: PetscScalar origin=0.0;
49: SlepcInitialize(&argc,&argv,(char*)0,help);
51: PetscOptionsGetInt(NULL,"-m",&m,NULL);
52: N = m*(m+1)/2;
53: PetscPrintf(PETSC_COMM_WORLD,"\nMarkov Model, N=%D (m=%D)\n\n",N,m);
55: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
56: Compute the operator matrix that defines the eigensystem, Ax=kx
57: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
59: MatCreate(PETSC_COMM_WORLD,&A);
60: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
61: MatSetFromOptions(A);
62: MatSetUp(A);
63: MatMarkovModel(m,A);
65: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66: Create the eigensolver and set various options
67: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
69: /*
70: Create eigensolver context
71: */
72: EPSCreate(PETSC_COMM_WORLD,&eps);
74: /*
75: Set operators. In this case, it is a standard eigenvalue problem
76: */
77: EPSSetOperators(eps,A,NULL);
78: EPSSetProblemType(eps,EPS_NHEP);
79: EPSSetTolerances(eps,tol,PETSC_DECIDE);
81: /*
82: Set the custom comparing routine in order to obtain the eigenvalues
83: closest to the target on the right only
84: */
85: EPSSetEigenvalueComparison(eps,MyEigenSort,&origin);
88: /*
89: Set solver parameters at runtime
90: */
91: EPSSetFromOptions(eps);
93: /*
94: Set the initial vector. This is optional, if not done the initial
95: vector is set to random values
96: */
97: MatGetVecs(A,&v0,NULL);
98: VecSet(v0,1.0);
99: EPSSetInitialSpace(eps,1,&v0);
101: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102: Solve the eigensystem
103: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
105: EPSSolve(eps);
107: /*
108: Optional: Get some information from the solver and display it
109: */
110: EPSGetType(eps,&type);
111: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
112: EPSGetDimensions(eps,&nev,NULL,NULL);
113: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
115: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116: Display solution and clean up
117: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
119: EPSPrintSolution(eps,NULL);
120: EPSDestroy(&eps);
121: MatDestroy(&A);
122: VecDestroy(&v0);
123: SlepcFinalize();
124: return 0;
125: }
129: /*
130: Matrix generator for a Markov model of a random walk on a triangular grid.
132: This subroutine generates a test matrix that models a random walk on a
133: triangular grid. This test example was used by G. W. Stewart ["{SRRIT} - a
134: FORTRAN subroutine to calculate the dominant invariant subspaces of a real
135: matrix", Tech. report. TR-514, University of Maryland (1978).] and in a few
136: papers on eigenvalue problems by Y. Saad [see e.g. LAA, vol. 34, pp. 269-295
137: (1980) ]. These matrices provide reasonably easy test problems for eigenvalue
138: algorithms. The transpose of the matrix is stochastic and so it is known
139: that one is an exact eigenvalue. One seeks the eigenvector of the transpose
140: associated with the eigenvalue unity. The problem is to calculate the steady
141: state probability distribution of the system, which is the eigevector
142: associated with the eigenvalue one and scaled in such a way that the sum all
143: the components is equal to one.
145: Note: the code will actually compute the transpose of the stochastic matrix
146: that contains the transition probabilities.
147: */
148: PetscErrorCode MatMarkovModel(PetscInt m,Mat A)
149: {
150: const PetscReal cst = 0.5/(PetscReal)(m-1);
151: PetscReal pd,pu;
152: PetscInt Istart,Iend,i,j,jmax,ix=0;
153: PetscErrorCode ierr;
156: MatGetOwnershipRange(A,&Istart,&Iend);
157: for (i=1;i<=m;i++) {
158: jmax = m-i+1;
159: for (j=1;j<=jmax;j++) {
160: ix = ix + 1;
161: if (ix-1<Istart || ix>Iend) continue; /* compute only owned rows */
162: if (j!=jmax) {
163: pd = cst*(PetscReal)(i+j-1);
164: /* north */
165: if (i==1) {
166: MatSetValue(A,ix-1,ix,2*pd,INSERT_VALUES);
167: } else {
168: MatSetValue(A,ix-1,ix,pd,INSERT_VALUES);
169: }
170: /* east */
171: if (j==1) {
172: MatSetValue(A,ix-1,ix+jmax-1,2*pd,INSERT_VALUES);
173: } else {
174: MatSetValue(A,ix-1,ix+jmax-1,pd,INSERT_VALUES);
175: }
176: }
177: /* south */
178: pu = 0.5 - cst*(PetscReal)(i+j-3);
179: if (j>1) {
180: MatSetValue(A,ix-1,ix-2,pu,INSERT_VALUES);
181: }
182: /* west */
183: if (i>1) {
184: MatSetValue(A,ix-1,ix-jmax-2,pu,INSERT_VALUES);
185: }
186: }
187: }
188: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
189: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
190: return(0);
191: }
195: /*
196: Function for user-defined eigenvalue ordering criterion.
198: Given two eigenvalues ar+i*ai and br+i*bi, the subroutine must choose
199: one of them as the preferred one according to the criterion.
200: In this example, the preferred value is the one furthest to the origin.
201: */
202: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx)
203: {
204: PetscScalar origin = *(PetscScalar*)ctx;
205: PetscReal d;
208: d = (SlepcAbsEigenvalue(br-origin,bi) - SlepcAbsEigenvalue(ar-origin,ai))/PetscMax(SlepcAbsEigenvalue(ar-origin,ai),SlepcAbsEigenvalue(br-origin,bi));
209: *r = d > PETSC_SQRT_MACHINE_EPSILON ? 1 : (d < -PETSC_SQRT_MACHINE_EPSILON ? -1 : PetscSign(PetscRealPart(br)));
210: return(0);
211: }