Actual source code: ex42.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: */
 10: /*
 11:    This example implements one of the problems found at
 12:        NLEVP: A Collection of Nonlinear Eigenvalue Problems,
 13:        The University of Manchester.
 14:    The details of the collection can be found at:
 15:        [1] T. Betcke et al., "NLEVP: A Collection of Nonlinear Eigenvalue
 16:            Problems", ACM Trans. Math. Software 39(2), Article 7, 2013.

 18:    The loaded_string problem is a rational eigenvalue problem for the
 19:    finite element model of a loaded vibrating string.
 20: */

 22: static char help[] = "Illustrates computation of left eigenvectors and resolvent.\n\n"
 23:   "This is based on loaded_string from the NLEVP collection.\n"
 24:   "The command line options are:\n"
 25:   "  -n <n>, dimension of the matrices.\n"
 26:   "  -kappa <kappa>, stiffness of elastic spring.\n"
 27:   "  -mass <m>, mass of the attached load.\n\n";

 29: #include <slepcnep.h>

 31: #define NMAT 3

 33: int main(int argc,char **argv)
 34: {
 35:   Mat            A[NMAT];         /* problem matrices */
 36:   FN             f[NMAT];         /* functions to define the nonlinear operator */
 37:   NEP            nep;             /* nonlinear eigensolver context */
 38:   RG             rg;
 39:   Vec            v,r,z,w;
 40:   PetscInt       n=100,Istart,Iend,i,nconv;
 41:   PetscReal      kappa=1.0,m=1.0,nrm,tol;
 42:   PetscScalar    lambda,sigma,numer[2],denom[2],omega1,omega2;

 45:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 47:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 48:   PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL);
 49:   PetscOptionsGetReal(NULL,NULL,"-mass",&m,NULL);
 50:   sigma = kappa/m;
 51:   PetscPrintf(PETSC_COMM_WORLD,"Loaded vibrating string, n=%D kappa=%g m=%g\n\n",n,(double)kappa,(double)m);

 53:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 54:                        Build the problem matrices
 55:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 57:   /* initialize matrices */
 58:   for (i=0;i<NMAT;i++) {
 59:     MatCreate(PETSC_COMM_WORLD,&A[i]);
 60:     MatSetSizes(A[i],PETSC_DECIDE,PETSC_DECIDE,n,n);
 61:     MatSetFromOptions(A[i]);
 62:     MatSetUp(A[i]);
 63:   }
 64:   MatGetOwnershipRange(A[0],&Istart,&Iend);

 66:   /* A0 */
 67:   for (i=Istart;i<Iend;i++) {
 68:     MatSetValue(A[0],i,i,(i==n-1)?1.0*n:2.0*n,INSERT_VALUES);
 69:     if (i>0) { MatSetValue(A[0],i,i-1,-1.0*n,INSERT_VALUES); }
 70:     if (i<n-1) { MatSetValue(A[0],i,i+1,-1.0*n,INSERT_VALUES); }
 71:   }

 73:   /* A1 */
 74:   for (i=Istart;i<Iend;i++) {
 75:     MatSetValue(A[1],i,i,(i==n-1)?2.0/(6.0*n):4.0/(6.0*n),INSERT_VALUES);
 76:     if (i>0) { MatSetValue(A[1],i,i-1,1.0/(6.0*n),INSERT_VALUES); }
 77:     if (i<n-1) { MatSetValue(A[1],i,i+1,1.0/(6.0*n),INSERT_VALUES); }
 78:   }

 80:   /* A2 */
 81:   if (Istart<=n-1 && n-1<Iend) {
 82:     MatSetValue(A[2],n-1,n-1,kappa,INSERT_VALUES); 
 83:   }

 85:   /* assemble matrices */
 86:   for (i=0;i<NMAT;i++) {
 87:     MatAssemblyBegin(A[i],MAT_FINAL_ASSEMBLY);
 88:   }
 89:   for (i=0;i<NMAT;i++) {
 90:     MatAssemblyEnd(A[i],MAT_FINAL_ASSEMBLY);
 91:   }

 93:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 94:                        Create the problem functions
 95:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 97:   /* f1=1 */
 98:   FNCreate(PETSC_COMM_WORLD,&f[0]);
 99:   FNSetType(f[0],FNRATIONAL);
100:   numer[0] = 1.0;
101:   FNRationalSetNumerator(f[0],1,numer);

103:   /* f2=-lambda */
104:   FNCreate(PETSC_COMM_WORLD,&f[1]);
105:   FNSetType(f[1],FNRATIONAL);
106:   numer[0] = -1.0; numer[1] = 0.0;
107:   FNRationalSetNumerator(f[1],2,numer);

109:   /* f3=lambda/(lambda-sigma) */
110:   FNCreate(PETSC_COMM_WORLD,&f[2]);
111:   FNSetType(f[2],FNRATIONAL);
112:   numer[0] = 1.0; numer[1] = 0.0;
113:   denom[0] = 1.0; denom[1] = -sigma;
114:   FNRationalSetNumerator(f[2],2,numer);
115:   FNRationalSetDenominator(f[2],2,denom);

117:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118:                 Create the eigensolver and solve the problem
119:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

121:   NEPCreate(PETSC_COMM_WORLD,&nep);
122:   NEPSetSplitOperator(nep,3,A,f,SUBSET_NONZERO_PATTERN);
123:   NEPSetProblemType(nep,NEP_RATIONAL);
124:   NEPSetDimensions(nep,8,PETSC_DEFAULT,PETSC_DEFAULT);

126:   /* set two-sided NLEIGS solver */
127:   NEPSetType(nep,NEPNLEIGS);
128:   NEPNLEIGSSetFullBasis(nep,PETSC_TRUE);
129:   NEPSetTwoSided(nep,PETSC_TRUE);
130:   NEPGetRG(nep,&rg);
131:   RGSetType(rg,RGINTERVAL);
132: #if defined(PETSC_USE_COMPLEX)
133:   RGIntervalSetEndpoints(rg,4.0,700.0,-0.001,0.001);
134: #else
135:   RGIntervalSetEndpoints(rg,4.0,700.0,0,0);
136: #endif
137:   NEPSetTarget(nep,5.0);

139:   NEPSetFromOptions(nep);
140:   NEPSolve(nep);

142:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143:                        Check left residual
144:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
145:   MatCreateVecs(A[0],&v,&r);
146:   VecDuplicate(v,&w);
147:   VecDuplicate(v,&z);
148:   NEPGetConverged(nep,&nconv);
149:   NEPGetTolerances(nep,&tol,NULL);
150:   for (i=0;i<nconv;i++) {
151:     NEPGetEigenpair(nep,i,&lambda,NULL,NULL,NULL);
152:     NEPGetLeftEigenvector(nep,i,v,NULL);
153:     NEPApplyAdjoint(nep,lambda,v,w,r,NULL,NULL);
154:     VecNorm(r,NORM_2,&nrm);
155:     if (nrm>tol*PetscAbsScalar(lambda)) {
156:       PetscPrintf(PETSC_COMM_WORLD,"Left residual i=%D is above tolerance --> %g\n",i,nrm/PetscAbsScalar(lambda));
157:     }
158:   }

160:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161:                       Operate with resolvent
162:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
163:   omega1 = 20.0;
164:   omega2 = 150.0;
165:   VecSet(v,0.0);
166:   VecSetValue(v,0,-1.0,INSERT_VALUES);
167:   VecSetValue(v,1,3.0,INSERT_VALUES);
168:   VecAssemblyBegin(v);
169:   VecAssemblyEnd(v);
170:   NEPApplyResolvent(nep,NULL,omega1,v,r);
171:   VecNorm(r,NORM_2,&nrm);
172:   PetscPrintf(PETSC_COMM_WORLD,"resolvent, omega=%g: norm of computed vector=%g\n",(double)PetscRealPart(omega1),(double)nrm);
173:   NEPApplyResolvent(nep,NULL,omega2,v,r);
174:   VecNorm(r,NORM_2,&nrm);
175:   PetscPrintf(PETSC_COMM_WORLD,"resolvent, omega=%g: norm of computed vector=%g\n",(double)PetscRealPart(omega2),(double)nrm);
176:   VecSet(v,1.0);
177:   NEPApplyResolvent(nep,NULL,omega1,v,r);
178:   VecNorm(r,NORM_2,&nrm);
179:   PetscPrintf(PETSC_COMM_WORLD,"resolvent, omega=%g: norm of computed vector=%g\n",(double)PetscRealPart(omega1),(double)nrm);
180:   NEPApplyResolvent(nep,NULL,omega2,v,r);
181:   VecNorm(r,NORM_2,&nrm);
182:   PetscPrintf(PETSC_COMM_WORLD,"resolvent, omega=%g: norm of computed vector=%g\n",(double)PetscRealPart(omega2),(double)nrm);

184:   /* clean up */
185:   NEPDestroy(&nep);
186:   for (i=0;i<NMAT;i++) {
187:     MatDestroy(&A[i]);
188:     FNDestroy(&f[i]);
189:   }
190:   VecDestroy(&v);
191:   VecDestroy(&r);
192:   VecDestroy(&w);
193:   VecDestroy(&z);
194:   SlepcFinalize();
195:   return ierr;
196: }

198: /*TEST

200:    test:
201:       suffix: 1
202:       requires: !single

204: TEST*/