Actual source code: test11.c

slepc-3.17.0 2022-03-31
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-, 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[] = "Solves the same problem as in ex5, but with a user-defined sorting criterion."
 12:   "It is a standard nonsymmetric eigenproblem with real eigenvalues and the rightmost eigenvalue is known to be 1.\n"
 13:   "This example illustrates how the user can set a custom spectrum selection.\n\n"
 14:   "The command line options are:\n"
 15:   "  -m <m>, where <m> = number of grid subdivisions in each dimension.\n\n";

 17: #include <slepceps.h>

 19: /*
 20:    User-defined routines
 21: */

 23: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx);
 24: PetscErrorCode MatMarkovModel(PetscInt m,Mat A);

 26: int main(int argc,char **argv)
 27: {
 28:   Vec            v0;              /* initial vector */
 29:   Mat            A;               /* operator matrix */
 30:   EPS            eps;             /* eigenproblem solver context */
 31:   ST             st;              /* spectral transformation associated */
 32:   PetscReal      tol=PETSC_SMALL;
 33:   PetscScalar    target=0.5;
 34:   PetscInt       N,m=15,nev;
 35:   char           str[50];

 37:   SlepcInitialize(&argc,&argv,(char*)0,help);

 39:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 40:   N = m*(m+1)/2;
 41:   PetscPrintf(PETSC_COMM_WORLD,"\nMarkov Model, N=%" PetscInt_FMT " (m=%" PetscInt_FMT ")\n",N,m);
 42:   PetscOptionsGetScalar(NULL,NULL,"-target",&target,NULL);
 43:   SlepcSNPrintfScalar(str,sizeof(str),target,PETSC_FALSE);
 44:   PetscPrintf(PETSC_COMM_WORLD,"Searching closest eigenvalues to the right of %s.\n\n",str);

 46:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 47:      Compute the operator matrix that defines the eigensystem, Ax=kx
 48:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 50:   MatCreate(PETSC_COMM_WORLD,&A);
 51:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 52:   MatSetFromOptions(A);
 53:   MatSetUp(A);
 54:   MatMarkovModel(m,A);

 56:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 57:                 Create the eigensolver and set various options
 58:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 60:   /*
 61:      Create eigensolver context
 62:   */
 63:   EPSCreate(PETSC_COMM_WORLD,&eps);

 65:   /*
 66:      Set operators. In this case, it is a standard eigenvalue problem
 67:   */
 68:   EPSSetOperators(eps,A,NULL);
 69:   EPSSetProblemType(eps,EPS_NHEP);
 70:   EPSSetTolerances(eps,tol,PETSC_DEFAULT);

 72:   /*
 73:      Set the custom comparing routine in order to obtain the eigenvalues
 74:      closest to the target on the right only
 75:   */
 76:   EPSSetEigenvalueComparison(eps,MyEigenSort,&target);

 78:   /*
 79:      Set solver parameters at runtime
 80:   */
 81:   EPSSetFromOptions(eps);

 83:   /*
 84:      Set the preconditioner based on A - target * I
 85:   */
 86:   EPSGetST(eps,&st);
 87:   STSetShift(st,target);

 89:   /*
 90:      Set the initial vector. This is optional, if not done the initial
 91:      vector is set to random values
 92:   */
 93:   MatCreateVecs(A,&v0,NULL);
 94:   VecSet(v0,1.0);
 95:   EPSSetInitialSpace(eps,1,&v0);

 97:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 98:                       Solve the eigensystem
 99:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

101:   EPSSolve(eps);
102:   EPSGetDimensions(eps,&nev,NULL,NULL);
103:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);

105:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106:                     Display solution and clean up
107:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

109:   EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
110:   EPSDestroy(&eps);
111:   MatDestroy(&A);
112:   VecDestroy(&v0);
113:   SlepcFinalize();
114:   return 0;
115: }

117: PetscErrorCode MatMarkovModel(PetscInt m,Mat A)
118: {
119:   const PetscReal cst = 0.5/(PetscReal)(m-1);
120:   PetscReal       pd,pu;
121:   PetscInt        Istart,Iend,i,j,jmax,ix=0;

124:   MatGetOwnershipRange(A,&Istart,&Iend);
125:   for (i=1;i<=m;i++) {
126:     jmax = m-i+1;
127:     for (j=1;j<=jmax;j++) {
128:       ix = ix + 1;
129:       if (ix-1<Istart || ix>Iend) continue;  /* compute only owned rows */
130:       if (j!=jmax) {
131:         pd = cst*(PetscReal)(i+j-1);
132:         /* north */
133:         if (i==1) MatSetValue(A,ix-1,ix,2*pd,INSERT_VALUES);
134:         else MatSetValue(A,ix-1,ix,pd,INSERT_VALUES);
135:         /* east */
136:         if (j==1) MatSetValue(A,ix-1,ix+jmax-1,2*pd,INSERT_VALUES);
137:         else MatSetValue(A,ix-1,ix+jmax-1,pd,INSERT_VALUES);
138:       }
139:       /* south */
140:       pu = 0.5 - cst*(PetscReal)(i+j-3);
141:       if (j>1) MatSetValue(A,ix-1,ix-2,pu,INSERT_VALUES);
142:       /* west */
143:       if (i>1) MatSetValue(A,ix-1,ix-jmax-2,pu,INSERT_VALUES);
144:     }
145:   }
146:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
147:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
148:   PetscFunctionReturn(0);
149: }

151: /*
152:     Function for user-defined eigenvalue ordering criterion.

154:     Given two eigenvalues ar+i*ai and br+i*bi, the subroutine must choose
155:     one of them as the preferred one according to the criterion.
156:     In this example, the preferred value is the one closest to the target,
157:     but on the right side.
158: */
159: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx)
160: {
161:   PetscScalar target = *(PetscScalar*)ctx;
162:   PetscReal   da,db;
163:   PetscBool   aisright,bisright;

166:   if (PetscRealPart(target) < PetscRealPart(ar)) aisright = PETSC_TRUE;
167:   else aisright = PETSC_FALSE;
168:   if (PetscRealPart(target) < PetscRealPart(br)) bisright = PETSC_TRUE;
169:   else bisright = PETSC_FALSE;
170:   if (aisright == bisright) {
171:     /* both are on the same side of the target */
172:     da = SlepcAbsEigenvalue(ar-target,ai);
173:     db = SlepcAbsEigenvalue(br-target,bi);
174:     if (da < db) *r = -1;
175:     else if (da > db) *r = 1;
176:     else *r = 0;
177:   } else if (aisright && !bisright) *r = -1; /* 'a' is on the right */
178:   else *r = 1;  /* 'b' is on the right */
179:   PetscFunctionReturn(0);
180: }

182: /*TEST

184:    testset:
185:       args: -eps_nev 4
186:       requires: !single
187:       output_file: output/test11_1.out
188:       test:
189:          suffix: 1
190:          args: -eps_type {{krylovschur arnoldi lapack}} -st_type sinvert
191:       test:
192:          suffix: 1_ks_cayley
193:          args: -st_type cayley -st_cayley_antishift 1

195:    test:
196:       suffix: 2
197:       args: -target 0.77 -eps_type gd -eps_nev 4 -eps_tol 1e-7 -eps_gd_krylov_start -eps_gd_blocksize 3
198:       requires: double
199:       filter: sed -e "s/[+-]0\.0*i//g"

201: TEST*/