Actual source code: test15.c

slepc-3.20.1 2023-11-27
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[] = "Illustrates the use of a user-defined stopping test.\n\n"
 12:   "This is based on ex22.\n"
 13:   "The command line options are:\n"
 14:   "  -n <n>, where <n> = number of grid subdivisions.\n"
 15:   "  -tau <tau>, where <tau> is the delay parameter.\n\n";

 17: /*
 18:    Solve parabolic partial differential equation with time delay tau

 20:             u_t = u_xx + a*u(t) + b*u(t-tau)
 21:             u(0,t) = u(pi,t) = 0

 23:    with a = 20 and b(x) = -4.1+x*(1-exp(x-pi)).

 25:    Discretization leads to a DDE of dimension n

 27:             -u' = A*u(t) + B*u(t-tau)

 29:    which results in the nonlinear eigenproblem

 31:             (-lambda*I + A + exp(-tau*lambda)*B)*u = 0
 32: */

 34: #include <slepcnep.h>

 36: /*
 37:    User-defined routines
 38: */
 39: PetscErrorCode MyStoppingTest(NEP,PetscInt,PetscInt,PetscInt,PetscInt,NEPConvergedReason*,void*);

 41: typedef struct {
 42:   PetscInt    lastnconv;      /* last value of nconv; used in stopping test */
 43:   PetscInt    nreps;          /* number of repetitions of nconv; used in stopping test */
 44: } CTX_DELAY;

 46: int main(int argc,char **argv)
 47: {
 48:   NEP            nep;
 49:   Mat            Id,A,B;
 50:   FN             f1,f2,f3;
 51:   RG             rg;
 52:   CTX_DELAY      *ctx;
 53:   Mat            mats[3];
 54:   FN             funs[3];
 55:   PetscScalar    coeffs[2],b;
 56:   PetscInt       n=128,Istart,Iend,i,mpd;
 57:   PetscReal      tau=0.001,h,a=20,xi;
 58:   PetscBool      terse;
 59:   PetscViewer    viewer;

 61:   PetscFunctionBeginUser;
 62:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
 63:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
 64:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
 65:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Delay Eigenproblem, n=%" PetscInt_FMT ", tau=%g\n\n",n,(double)tau));
 66:   h = PETSC_PI/(PetscReal)(n+1);

 68:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 69:      Create nonlinear eigensolver context
 70:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 72:   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));

 74:   /* Identity matrix */
 75:   PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,&Id));
 76:   PetscCall(MatSetOption(Id,MAT_HERMITIAN,PETSC_TRUE));

 78:   /* A = 1/h^2*tridiag(1,-2,1) + a*I */
 79:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
 80:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
 81:   PetscCall(MatSetFromOptions(A));
 82:   PetscCall(MatSetUp(A));
 83:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
 84:   for (i=Istart;i<Iend;i++) {
 85:     if (i>0) PetscCall(MatSetValue(A,i,i-1,1.0/(h*h),INSERT_VALUES));
 86:     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,1.0/(h*h),INSERT_VALUES));
 87:     PetscCall(MatSetValue(A,i,i,-2.0/(h*h)+a,INSERT_VALUES));
 88:   }
 89:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
 90:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
 91:   PetscCall(MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE));

 93:   /* B = diag(b(xi)) */
 94:   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
 95:   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n));
 96:   PetscCall(MatSetFromOptions(B));
 97:   PetscCall(MatSetUp(B));
 98:   PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
 99:   for (i=Istart;i<Iend;i++) {
100:     xi = (i+1)*h;
101:     b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
102:     PetscCall(MatSetValues(B,1,&i,1,&i,&b,INSERT_VALUES));
103:   }
104:   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
105:   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
106:   PetscCall(MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE));

108:   /* Functions: f1=-lambda, f2=1.0, f3=exp(-tau*lambda) */
109:   PetscCall(FNCreate(PETSC_COMM_WORLD,&f1));
110:   PetscCall(FNSetType(f1,FNRATIONAL));
111:   coeffs[0] = -1.0; coeffs[1] = 0.0;
112:   PetscCall(FNRationalSetNumerator(f1,2,coeffs));

114:   PetscCall(FNCreate(PETSC_COMM_WORLD,&f2));
115:   PetscCall(FNSetType(f2,FNRATIONAL));
116:   coeffs[0] = 1.0;
117:   PetscCall(FNRationalSetNumerator(f2,1,coeffs));

119:   PetscCall(FNCreate(PETSC_COMM_WORLD,&f3));
120:   PetscCall(FNSetType(f3,FNEXP));
121:   PetscCall(FNSetScale(f3,-tau,1.0));

123:   /* Set the split operator */
124:   mats[0] = A;  funs[0] = f2;
125:   mats[1] = Id; funs[1] = f1;
126:   mats[2] = B;  funs[2] = f3;
127:   PetscCall(NEPSetSplitOperator(nep,3,mats,funs,SUBSET_NONZERO_PATTERN));

129:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
130:                 Customize nonlinear solver; set runtime options
131:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

133:   PetscCall(NEPSetType(nep,NEPNLEIGS));
134:   PetscCall(NEPGetRG(nep,&rg));
135:   PetscCall(RGSetType(rg,RGINTERVAL));
136: #if defined(PETSC_USE_COMPLEX)
137:   PetscCall(RGIntervalSetEndpoints(rg,5,20,-0.001,0.001));
138: #else
139:   PetscCall(RGIntervalSetEndpoints(rg,5,20,-0.0,0.0));
140: #endif
141:   PetscCall(NEPSetTarget(nep,15.0));
142:   PetscCall(NEPSetWhichEigenpairs(nep,NEP_TARGET_MAGNITUDE));

144:   /*
145:      Set solver options. In particular, we must allocate sufficient
146:      storage for all eigenpairs that may converge (ncv). This is
147:      application-dependent.
148:   */
149:   mpd = 40;
150:   PetscCall(NEPSetDimensions(nep,2*mpd,3*mpd,mpd));
151:   PetscCall(NEPSetTolerances(nep,PETSC_DEFAULT,2000));
152:   PetscCall(PetscNew(&ctx));
153:   ctx->lastnconv = 0;
154:   ctx->nreps     = 0;
155:   PetscCall(NEPSetStoppingTestFunction(nep,MyStoppingTest,(void*)ctx,NULL));

157:   PetscCall(NEPSetFromOptions(nep));

159:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160:                       Solve the eigensystem
161:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

163:   PetscCall(NEPSolve(nep));

165:   /* show detailed info unless -terse option is given by user */
166:   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
167:   PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL));
168:   PetscCall(NEPConvergedReasonView(nep,viewer));
169:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
170:   if (!terse) PetscCall(NEPErrorView(nep,NEP_ERROR_BACKWARD,viewer));
171:   PetscCall(PetscViewerPopFormat(viewer));

173:   PetscCall(NEPDestroy(&nep));
174:   PetscCall(MatDestroy(&Id));
175:   PetscCall(MatDestroy(&A));
176:   PetscCall(MatDestroy(&B));
177:   PetscCall(FNDestroy(&f1));
178:   PetscCall(FNDestroy(&f2));
179:   PetscCall(FNDestroy(&f3));
180:   PetscCall(PetscFree(ctx));
181:   PetscCall(SlepcFinalize());
182:   return 0;
183: }

185: /*
186:     Function for user-defined stopping test.

188:     Ignores the value of nev. It only takes into account the number of
189:     eigenpairs that have converged in recent outer iterations (restarts);
190:     if no new eigenvalues have converged in the last few restarts,
191:     we stop the iteration, assuming that no more eigenvalues are present
192:     inside the region.
193: */
194: PetscErrorCode MyStoppingTest(NEP nep,PetscInt its,PetscInt max_it,PetscInt nconv,PetscInt nev,NEPConvergedReason *reason,void *ptr)
195: {
196:   CTX_DELAY      *ctx = (CTX_DELAY*)ptr;

198:   PetscFunctionBeginUser;
199:   /* check usual termination conditions, but ignoring the case nconv>=nev */
200:   PetscCall(NEPStoppingBasic(nep,its,max_it,nconv,PETSC_MAX_INT,reason,NULL));
201:   if (*reason==NEP_CONVERGED_ITERATING) {
202:     /* check if nconv is the same as before */
203:     if (nconv==ctx->lastnconv) ctx->nreps++;
204:     else {
205:       ctx->lastnconv = nconv;
206:       ctx->nreps     = 0;
207:     }
208:     /* check if no eigenvalues converged in last 10 restarts */
209:     if (nconv && ctx->nreps>10) *reason = NEP_CONVERGED_USER;
210:   }
211:   PetscFunctionReturn(PETSC_SUCCESS);
212: }

214: /*TEST

216:    test:
217:       suffix: 1
218:       args: -terse

220: TEST*/