Actual source code: ex18.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[] = "Solves the same problem as in ex5, but with a user-defined sorting criterion."
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 a custom spectrum selection.\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: */
34: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx);
35: PetscErrorCode MatMarkovModel(PetscInt m,Mat A);
39: int main(int argc,char **argv)
40: {
41: Vec v0; /* initial vector */
42: Mat A; /* operator matrix */
43: EPS eps; /* eigenproblem solver context */
44: EPSType type;
45: PetscScalar target=0.5;
46: PetscInt N,m=15,nev;
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,m);
54: PetscOptionsGetScalar(NULL,"-target",&target,NULL);
55: PetscPrintf(PETSC_COMM_WORLD,"Searching closest eigenvalues to the right of %G.\n\n",target);
57: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58: Compute the operator matrix that defines the eigensystem, Ax=kx
59: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
61: MatCreate(PETSC_COMM_WORLD,&A);
62: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
63: MatSetFromOptions(A);
64: MatSetUp(A);
65: MatMarkovModel(m,A);
67: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68: Create the eigensolver and set various options
69: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
71: /*
72: Create eigensolver context
73: */
74: EPSCreate(PETSC_COMM_WORLD,&eps);
76: /*
77: Set operators. In this case, it is a standard eigenvalue problem
78: */
79: EPSSetOperators(eps,A,NULL);
80: EPSSetProblemType(eps,EPS_NHEP);
82: /*
83: Set the custom comparing routine in order to obtain the eigenvalues
84: closest to the target on the right only
85: */
86: EPSSetEigenvalueComparison(eps,MyEigenSort,&target);
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 closest to the target,
201: but on the right side.
202: */
203: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx)
204: {
205: PetscScalar target = *(PetscScalar*)ctx;
206: PetscReal da,db;
207: PetscBool aisright,bisright;
210: if (PetscRealPart(target) < PetscRealPart(ar)) aisright = PETSC_TRUE;
211: else aisright = PETSC_FALSE;
212: if (PetscRealPart(target) < PetscRealPart(br)) bisright = PETSC_TRUE;
213: else bisright = PETSC_FALSE;
214: if (aisright == bisright) {
215: /* both are on the same side of the target */
216: da = SlepcAbsEigenvalue(ar-target,ai);
217: db = SlepcAbsEigenvalue(br-target,bi);
218: if (da < db) *r = -1;
219: else if (da > db) *r = 1;
220: else *r = 0;
221: } else if (aisright && !bisright) *r = -1; /* 'a' is on the right */
222: else *r = 1; /* 'b' is on the right */
223: return(0);
224: }