Actual source code: test6.c

slepc-3.19.2 2023-09-05
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[] = "Test the NArnoldi solver with a user-provided KSP.\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"
 16:   "  -initv ... set an initial vector.\n\n";

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

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

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

 26:    Discretization leads to a DDE of dimension n

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

 30:    which results in the nonlinear eigenproblem

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

 35: #include <slepcnep.h>

 37: int main(int argc,char **argv)
 38: {
 39:   NEP            nep;
 40:   KSP            ksp;
 41:   PC             pc;
 42:   Mat            Id,A,B,mats[3];
 43:   FN             f1,f2,f3,funs[3];
 44:   Vec            v0;
 45:   PetscScalar    coeffs[2],b,*pv;
 46:   PetscInt       n=128,nev,Istart,Iend,i,lag;
 47:   PetscReal      tau=0.001,h,a=20,xi;
 48:   PetscBool      terse,initv=PETSC_FALSE;
 49:   const char     *prefix;

 51:   PetscFunctionBeginUser;
 52:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
 53:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
 54:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
 55:   PetscCall(PetscOptionsGetBool(NULL,NULL,"-initv",&initv,NULL));
 56:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Delay Eigenproblem, n=%" PetscInt_FMT ", tau=%g\n\n",n,(double)tau));
 57:   h = PETSC_PI/(PetscReal)(n+1);

 59:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 60:       Create a standalone KSP with appropriate settings
 61:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 63:   PetscCall(KSPCreate(PETSC_COMM_WORLD,&ksp));
 64:   PetscCall(KSPSetType(ksp,KSPBCGS));
 65:   PetscCall(KSPGetPC(ksp,&pc));
 66:   PetscCall(PCSetType(pc,PCBJACOBI));
 67:   PetscCall(KSPSetFromOptions(ksp));

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

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

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

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

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

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

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

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

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

130:   /* Customize nonlinear solver; set runtime options */
131:   PetscCall(NEPSetOptionsPrefix(nep,"check_"));
132:   PetscCall(NEPAppendOptionsPrefix(nep,"myprefix_"));
133:   PetscCall(NEPGetOptionsPrefix(nep,&prefix));
134:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"NEP prefix is currently: %s\n\n",prefix));
135:   PetscCall(NEPSetType(nep,NEPNARNOLDI));
136:   PetscCall(NEPNArnoldiSetKSP(nep,ksp));
137:   if (initv) { /* initial vector */
138:     PetscCall(MatCreateVecs(A,&v0,NULL));
139:     PetscCall(VecGetArray(v0,&pv));
140:     for (i=Istart;i<Iend;i++) pv[i-Istart] = PetscSinReal((4.0*PETSC_PI*i)/n);
141:     PetscCall(VecRestoreArray(v0,&pv));
142:     PetscCall(NEPSetInitialSpace(nep,1,&v0));
143:     PetscCall(VecDestroy(&v0));
144:   }
145:   PetscCall(NEPSetFromOptions(nep));

147:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148:                       Solve the eigensystem
149:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

151:   PetscCall(NEPSolve(nep));
152:   PetscCall(NEPGetDimensions(nep,&nev,NULL,NULL));
153:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
154:   PetscCall(NEPNArnoldiGetLagPreconditioner(nep,&lag));
155:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," N-Arnoldi lag parameter: %" PetscInt_FMT "\n",lag));

157:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158:                     Display solution and clean up
159:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

161:   /* show detailed info unless -terse option is given by user */
162:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
163:   if (terse) PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
164:   else {
165:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
166:     PetscCall(NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD));
167:     PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
168:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
169:   }
170:   PetscCall(NEPDestroy(&nep));
171:   PetscCall(KSPDestroy(&ksp));
172:   PetscCall(MatDestroy(&Id));
173:   PetscCall(MatDestroy(&A));
174:   PetscCall(MatDestroy(&B));
175:   PetscCall(FNDestroy(&f1));
176:   PetscCall(FNDestroy(&f2));
177:   PetscCall(FNDestroy(&f3));
178:   PetscCall(SlepcFinalize());
179:   return 0;
180: }

182: /*TEST

184:    test:
185:       suffix: 1
186:       args: -check_myprefix_nep_view -check_myprefix_nep_monitor_conv -initv -terse
187:       filter: grep -v "tolerance" | sed -e "s/[0-9]\.[0-9]*e[+-]\([0-9]*\)/removed/g" -e "s/+0i//g"
188:       requires: double

190: TEST*/