Actual source code: loaded_string.c

slepc-3.17.2 2022-08-09
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: */
 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[] = "Finite element model of a loaded vibrating string.\n\n"
 23:   "The command line options are:\n"
 24:   "  -n <n>, dimension of the matrices.\n"
 25:   "  -kappa <kappa>, stiffness of elastic spring.\n"
 26:   "  -mass <m>, mass of the attached load.\n\n";

 28: #include <slepcnep.h>

 30: #define NMAT 3

 32: int main(int argc,char **argv)
 33: {
 34:   Mat            A[NMAT];         /* problem matrices */
 35:   FN             f[NMAT];         /* functions to define the nonlinear operator */
 36:   NEP            nep;             /* nonlinear eigensolver context */
 37:   PetscInt       n=100,Istart,Iend,i;
 38:   PetscReal      kappa=1.0,m=1.0;
 39:   PetscScalar    sigma,numer[2],denom[2];
 40:   PetscBool      terse;

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

 44:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 45:   PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL);
 46:   PetscOptionsGetReal(NULL,NULL,"-mass",&m,NULL);
 47:   sigma = kappa/m;
 48:   PetscPrintf(PETSC_COMM_WORLD,"Loaded vibrating string, n=%" PetscInt_FMT " kappa=%g m=%g\n\n",n,(double)kappa,(double)m);

 50:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 51:                        Build the problem matrices
 52:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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

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

 77:   /* A2 */
 78:   if (Istart<=n-1 && n-1<Iend) MatSetValue(A[2],n-1,n-1,kappa,INSERT_VALUES);

 80:   /* assemble matrices */
 81:   for (i=0;i<NMAT;i++) MatAssemblyBegin(A[i],MAT_FINAL_ASSEMBLY);
 82:   for (i=0;i<NMAT;i++) MatAssemblyEnd(A[i],MAT_FINAL_ASSEMBLY);

 84:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 85:                        Create the problem functions
 86:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 88:   /* f1=1 */
 89:   FNCreate(PETSC_COMM_WORLD,&f[0]);
 90:   FNSetType(f[0],FNRATIONAL);
 91:   numer[0] = 1.0;
 92:   FNRationalSetNumerator(f[0],1,numer);

 94:   /* f2=-lambda */
 95:   FNCreate(PETSC_COMM_WORLD,&f[1]);
 96:   FNSetType(f[1],FNRATIONAL);
 97:   numer[0] = -1.0; numer[1] = 0.0;
 98:   FNRationalSetNumerator(f[1],2,numer);

100:   /* f3=lambda/(lambda-sigma) */
101:   FNCreate(PETSC_COMM_WORLD,&f[2]);
102:   FNSetType(f[2],FNRATIONAL);
103:   numer[0] = 1.0; numer[1] = 0.0;
104:   denom[0] = 1.0; denom[1] = -sigma;
105:   FNRationalSetNumerator(f[2],2,numer);
106:   FNRationalSetDenominator(f[2],2,denom);

108:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109:                 Create the eigensolver and solve the problem
110:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

112:   NEPCreate(PETSC_COMM_WORLD,&nep);
113:   NEPSetSplitOperator(nep,3,A,f,SUBSET_NONZERO_PATTERN);
114:   NEPSetProblemType(nep,NEP_RATIONAL);
115:   NEPSetFromOptions(nep);
116:   NEPSolve(nep);

118:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119:                     Display solution and clean up
120:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

122:   /* show detailed info unless -terse option is given by user */
123:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
124:   if (terse) NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL);
125:   else {
126:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
127:     NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD);
128:     NEPErrorView(nep,NEP_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
129:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
130:   }
131:   NEPDestroy(&nep);
132:   for (i=0;i<NMAT;i++) {
133:     MatDestroy(&A[i]);
134:     FNDestroy(&f[i]);
135:   }
136:   SlepcFinalize();
137:   return 0;
138: }

140: /*TEST

142:    test:
143:       suffix: 1
144:       args: -nep_type rii -nep_target 4 -terse
145:       requires: !single
146:       filter: sed -e "s/[+-]0\.0*i//g"

148:    testset:
149:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 7 -nep_target 5 -nep_interpol_interpolation_degree 12 -nep_refine simple -terse
150:       requires: !single
151:       output_file: output/loaded_string_2.out
152:       test:
153:          suffix: 2
154:          args: -nep_refine_scheme {{schur explicit}}
155:       test:
156:          suffix: 2_mbe
157:          args: -nep_refine_scheme mbe -nep_refine_ksp_type preonly -nep_refine_pc_type lu

159:    testset:
160:       nsize: 2
161:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 7 -nep_target 5 -nep_interpol_interpolation_degree 12 -nep_refine simple -nep_refine_partitions 2 -nep_interpol_st_ksp_type bcgs -nep_interpol_st_pc_type bjacobi -terse
162:       requires: !single
163:       output_file: output/loaded_string_2.out
164:       timeoutfactor: 2
165:       test:
166:          suffix: 3_explicit
167:          args: -nep_refine_scheme explicit
168:       test:
169:          suffix: 3_mbe
170:          args: -nep_refine_scheme mbe -nep_refine_ksp_type preonly -nep_refine_pc_type cholesky

172:    test:
173:       suffix: 4
174:       nsize: 4
175:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 7 -nep_target 5 -nep_interpol_interpolation_degree 10 -nep_refine simple -nep_refine_partitions 2 -nep_refine_scheme explicit -nep_interpol_st_ksp_type bcgs -nep_interpol_st_pc_type bjacobi -terse -log_exclude nep,pep,fn
176:       requires: !single
177:       output_file: output/loaded_string_2.out
178:       timeoutfactor: 4

180:    test:
181:       suffix: 5
182:       args: -nep_type nleigs -rg_type interval -rg_interval_endpoints 4,700,-.1,.1 -nep_nev 8 -nep_target 5 -terse
183:       filter: sed -e "s/[+-]0\.0*i//g"
184:       requires: !single

186:    test:
187:       suffix: 6
188:       args: -nep_type nleigs -rg_type interval -rg_interval_endpoints 100,700 -nep_nev 5 -nep_tol 1e-9 -nep_target 140 -nep_nleigs_interpolation_degree 15 -nep_general -terse
189:       requires: !complex !single

191:    test:
192:       suffix: 6_complex
193:       args: -nep_type nleigs -rg_type interval -rg_interval_endpoints 100,700,-.1,.1 -nep_nev 5 -nep_tol 1e-9 -nep_target 140 -nep_nleigs_interpolation_degree 15 -nep_general -terse
194:       filter: sed -e "s/[+-]0\.0*i//g"
195:       requires: complex !single
196:       output_file: output/loaded_string_6.out

198:    test:
199:       suffix: 7
200:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700 -nep_nev 5 -nep_target 100 -nep_interpol_interpolation_degree 20 -nep_ncv 20 -n 20 -nep_refine simple -nep_refine_its 1 -terse
201:       requires: !complex double

203:    test:
204:       suffix: 7_complex
205:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 5 -nep_target 100 -nep_interpol_interpolation_degree 20 -nep_ncv 20 -n 20 -nep_refine simple -nep_refine_its 1 -terse
206:       requires: complex double
207:       output_file: output/loaded_string_7.out

209:    testset:
210:       args: -nep_target 10 -nep_nev 3 -nep_tol 5e-10 -terse
211:       requires: !single
212:       output_file: output/loaded_string_8.out
213:       filter: sed -e "s/[+-]0\.0*i//g"
214:       test:
215:          suffix: 8
216:          args: -nep_type {{rii slp narnoldi}}
217:       test:
218:          suffix: 8_rii_thres
219:          args: -nep_type rii -nep_rii_deflation_threshold 5e-10
220:       test:
221:          suffix: 8_slp_thres
222:          args: -nep_type slp -nep_slp_deflation_threshold 5e-10

224:       test:
225:          suffix: 8_slp_two_thres
226:          args: -nep_type slp -nep_slp_deflation_threshold 5e-10 -nep_two_sided

228:    test:
229:       suffix: 9
230:       args: -nep_type ciss -rg_type ellipse -rg_ellipse_center 500 -rg_ellipse_radius 500 -rg_ellipse_vscale .1 -nep_ciss_moments 4 -nep_ciss_blocksize 5 -terse
231:       requires: complex double

233: TEST*/