Actual source code: test6.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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;
 47:   PetscReal      tau=0.001,h,a=20,xi;
 48:   PetscBool      terse,initv=PETSC_FALSE;

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

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

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

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

 72:   NEPCreate(PETSC_COMM_WORLD,&nep);

 74:   /* Identity matrix */
 75:   MatCreate(PETSC_COMM_WORLD,&Id);
 76:   MatSetSizes(Id,PETSC_DECIDE,PETSC_DECIDE,n,n);
 77:   MatSetFromOptions(Id);
 78:   MatSetUp(Id);
 79:   MatGetOwnershipRange(Id,&Istart,&Iend);
 80:   for (i=Istart;i<Iend;i++) {
 81:     MatSetValue(Id,i,i,1.0,INSERT_VALUES);
 82:   }
 83:   MatAssemblyBegin(Id,MAT_FINAL_ASSEMBLY);
 84:   MatAssemblyEnd(Id,MAT_FINAL_ASSEMBLY);
 85:   MatSetOption(Id,MAT_HERMITIAN,PETSC_TRUE);

 87:   /* A = 1/h^2*tridiag(1,-2,1) + a*I */
 88:   MatCreate(PETSC_COMM_WORLD,&A);
 89:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 90:   MatSetFromOptions(A);
 91:   MatSetUp(A);
 92:   MatGetOwnershipRange(A,&Istart,&Iend);
 93:   for (i=Istart;i<Iend;i++) {
 94:     if (i>0) { MatSetValue(A,i,i-1,1.0/(h*h),INSERT_VALUES); }
 95:     if (i<n-1) { MatSetValue(A,i,i+1,1.0/(h*h),INSERT_VALUES); }
 96:     MatSetValue(A,i,i,-2.0/(h*h)+a,INSERT_VALUES);
 97:   }
 98:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 99:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
100:   MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE);

102:   /* B = diag(b(xi)) */
103:   MatCreate(PETSC_COMM_WORLD,&B);
104:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);
105:   MatSetFromOptions(B);
106:   MatSetUp(B);
107:   MatGetOwnershipRange(B,&Istart,&Iend);
108:   for (i=Istart;i<Iend;i++) {
109:     xi = (i+1)*h;
110:     b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
111:     MatSetValues(B,1,&i,1,&i,&b,INSERT_VALUES);
112:   }
113:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
114:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
115:   MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);

117:   /* Functions: f1=-lambda, f2=1.0, f3=exp(-tau*lambda) */
118:   FNCreate(PETSC_COMM_WORLD,&f1);
119:   FNSetType(f1,FNRATIONAL);
120:   coeffs[0] = -1.0; coeffs[1] = 0.0;
121:   FNRationalSetNumerator(f1,2,coeffs);

123:   FNCreate(PETSC_COMM_WORLD,&f2);
124:   FNSetType(f2,FNRATIONAL);
125:   coeffs[0] = 1.0;
126:   FNRationalSetNumerator(f2,1,coeffs);

128:   FNCreate(PETSC_COMM_WORLD,&f3);
129:   FNSetType(f3,FNEXP);
130:   FNSetScale(f3,-tau,1.0);

132:   /* Set the split operator */
133:   mats[0] = A;  funs[0] = f2;
134:   mats[1] = Id; funs[1] = f1;
135:   mats[2] = B;  funs[2] = f3;
136:   NEPSetSplitOperator(nep,3,mats,funs,SUBSET_NONZERO_PATTERN);

138:   /* Customize nonlinear solver; set runtime options */
139:   NEPSetOptionsPrefix(nep,"myprefix_");
140:   NEPSetType(nep,NEPNARNOLDI);
141:   NEPNArnoldiSetKSP(nep,ksp);
142:   if (initv) { /* initial vector */
143:     MatCreateVecs(A,&v0,NULL);
144:     VecGetArray(v0,&pv);
145:     for (i=Istart;i<Iend;i++) pv[i-Istart] = PetscSinReal((4.0*PETSC_PI*i)/n);
146:     VecRestoreArray(v0,&pv);
147:     NEPSetInitialSpace(nep,1,&v0);
148:     VecDestroy(&v0);
149:   }
150:   NEPSetFromOptions(nep);

152:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153:                       Solve the eigensystem
154:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

156:   NEPSolve(nep);
157:   NEPGetDimensions(nep,&nev,NULL,NULL);
158:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);

160:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161:                     Display solution and clean up
162:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

186: /*TEST

188:    test:
189:       suffix: 1
190:       args: -myprefix_nep_view -myprefix_nep_monitor_conv -initv -terse
191:       filter: grep -v "tolerance" | sed -e "s/[0-9]\.[0-9]*e[+-]\([0-9]*\)/removed/g"
192:       requires: double !complex !define(PETSC_USE_64BIT_INDICES)

194: TEST*/