Actual source code: test2.c

slepc-3.15.0 2021-03-31
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, 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 NEP interface functions.\n\n";

 13: #include <slepcnep.h>

 15: int main(int argc,char **argv)
 16: {
 17:   Mat                  A[3],B;      /* problem matrices */
 18:   FN                   f[3],g;      /* problem functions */
 19:   NEP                  nep;         /* eigenproblem solver context */
 20:   DS                   ds;
 21:   RG                   rg;
 22:   PetscReal            tol;
 23:   PetscScalar          coeffs[2],target;
 24:   PetscInt             n=20,i,its,nev,ncv,mpd,Istart,Iend,nterm;
 25:   PetscBool            twoside;
 26:   NEPWhich             which;
 27:   NEPConvergedReason   reason;
 28:   NEPType              type;
 29:   NEPRefine            refine;
 30:   NEPRefineScheme      rscheme;
 31:   NEPConv              conv;
 32:   NEPStop              stop;
 33:   NEPProblemType       ptype;
 34:   MatStructure         mstr;
 35:   PetscErrorCode       ierr;
 36:   PetscViewerAndFormat *vf;

 38:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 39:   PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Nonlinear Eigenproblem, n=%D\n\n",n);

 41:   /*
 42:      Matrices
 43:   */
 44:   MatCreate(PETSC_COMM_WORLD,&A[0]);
 45:   MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n);
 46:   MatSetFromOptions(A[0]);
 47:   MatSetUp(A[0]);
 48:   MatGetOwnershipRange(A[0],&Istart,&Iend);
 49:   for (i=Istart;i<Iend;i++) {
 50:     MatSetValue(A[0],i,i,i+1,INSERT_VALUES);
 51:   }
 52:   MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY);
 53:   MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY);

 55:   MatCreate(PETSC_COMM_WORLD,&A[1]);
 56:   MatSetSizes(A[1],PETSC_DECIDE,PETSC_DECIDE,n,n);
 57:   MatSetFromOptions(A[1]);
 58:   MatSetUp(A[1]);
 59:   MatGetOwnershipRange(A[1],&Istart,&Iend);
 60:   for (i=Istart;i<Iend;i++) {
 61:     MatSetValue(A[1],i,i,1.0,INSERT_VALUES);
 62:   }
 63:   MatAssemblyBegin(A[1],MAT_FINAL_ASSEMBLY);
 64:   MatAssemblyEnd(A[1],MAT_FINAL_ASSEMBLY);

 66:   MatCreate(PETSC_COMM_WORLD,&A[2]);
 67:   MatSetSizes(A[2],PETSC_DECIDE,PETSC_DECIDE,n,n);
 68:   MatSetFromOptions(A[2]);
 69:   MatSetUp(A[2]);
 70:   MatGetOwnershipRange(A[1],&Istart,&Iend);
 71:   for (i=Istart;i<Iend;i++) {
 72:     MatSetValue(A[2],i,i,n/(PetscReal)(i+1),INSERT_VALUES);
 73:   }
 74:   MatAssemblyBegin(A[2],MAT_FINAL_ASSEMBLY);
 75:   MatAssemblyEnd(A[2],MAT_FINAL_ASSEMBLY);

 77:   /*
 78:      Functions: f0=-lambda, f1=1.0, f2=sqrt(lambda)
 79:   */
 80:   FNCreate(PETSC_COMM_WORLD,&f[0]);
 81:   FNSetType(f[0],FNRATIONAL);
 82:   coeffs[0] = -1.0; coeffs[1] = 0.0;
 83:   FNRationalSetNumerator(f[0],2,coeffs);

 85:   FNCreate(PETSC_COMM_WORLD,&f[1]);
 86:   FNSetType(f[1],FNRATIONAL);
 87:   coeffs[0] = 1.0;
 88:   FNRationalSetNumerator(f[1],1,coeffs);

 90:   FNCreate(PETSC_COMM_WORLD,&f[2]);
 91:   FNSetType(f[2],FNSQRT);

 93:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 94:              Create eigensolver and test interface functions
 95:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 96:   NEPCreate(PETSC_COMM_WORLD,&nep);
 97:   NEPSetSplitOperator(nep,3,A,f,SAME_NONZERO_PATTERN);
 98:   NEPGetSplitOperatorInfo(nep,&nterm,&mstr);
 99:   PetscPrintf(PETSC_COMM_WORLD," Nonlinear function with %d terms, with %s\n",(int)nterm,MatStructures[mstr]);
100:   NEPGetSplitOperatorTerm(nep,0,&B,&g);
101:   MatView(B,NULL);
102:   FNView(g,NULL);

104:   NEPSetType(nep,NEPRII);
105:   NEPGetType(nep,&type);
106:   PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type);
107:   NEPGetTwoSided(nep,&twoside);
108:   PetscPrintf(PETSC_COMM_WORLD," Two-sided flag = %s\n",twoside?"true":"false");

110:   NEPGetProblemType(nep,&ptype);
111:   PetscPrintf(PETSC_COMM_WORLD," Problem type before changing = %d",(int)ptype);
112:   NEPSetProblemType(nep,NEP_RATIONAL);
113:   NEPGetProblemType(nep,&ptype);
114:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %d.\n",(int)ptype);

116:   NEPSetRefine(nep,NEP_REFINE_SIMPLE,1,1e-9,2,NEP_REFINE_SCHEME_EXPLICIT);
117:   NEPGetRefine(nep,&refine,NULL,&tol,&its,&rscheme);
118:   PetscPrintf(PETSC_COMM_WORLD," Refinement: %s, tol=%g, its=%D, scheme=%s\n",NEPRefineTypes[refine],(double)tol,its,NEPRefineSchemes[rscheme]);

120:   NEPSetTarget(nep,1.1);
121:   NEPGetTarget(nep,&target);
122:   NEPSetWhichEigenpairs(nep,NEP_TARGET_MAGNITUDE);
123:   NEPGetWhichEigenpairs(nep,&which);
124:   PetscPrintf(PETSC_COMM_WORLD," Which = %d, target = %g\n",(int)which,(double)PetscRealPart(target));

126:   NEPSetDimensions(nep,1,12,PETSC_DEFAULT);
127:   NEPGetDimensions(nep,&nev,&ncv,&mpd);
128:   PetscPrintf(PETSC_COMM_WORLD," Dimensions: nev=%D, ncv=%D, mpd=%D\n",nev,ncv,mpd);

130:   NEPSetTolerances(nep,1.0e-6,200);
131:   NEPGetTolerances(nep,&tol,&its);
132:   PetscPrintf(PETSC_COMM_WORLD," Tolerance = %.6f, max_its = %D\n",(double)tol,its);

134:   NEPSetConvergenceTest(nep,NEP_CONV_ABS);
135:   NEPGetConvergenceTest(nep,&conv);
136:   NEPSetStoppingTest(nep,NEP_STOP_BASIC);
137:   NEPGetStoppingTest(nep,&stop);
138:   PetscPrintf(PETSC_COMM_WORLD," Convergence test = %d, stopping test = %d\n",(int)conv,(int)stop);

140:   PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf);
141:   NEPMonitorSet(nep,(PetscErrorCode (*)(NEP,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*))NEPMonitorFirst,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
142:   NEPMonitorCancel(nep);

144:   NEPGetDS(nep,&ds);
145:   DSView(ds,NULL);
146:   NEPSetFromOptions(nep);

148:   NEPGetRG(nep,&rg);
149:   RGView(rg,NULL);

151:   NEPSolve(nep);
152:   NEPGetConvergedReason(nep,&reason);
153:   PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason);

155:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
156:                     Display solution and clean up
157:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
158:   NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL);
159:   NEPDestroy(&nep);
160:   MatDestroy(&A[0]);
161:   MatDestroy(&A[1]);
162:   MatDestroy(&A[2]);
163:   FNDestroy(&f[0]);
164:   FNDestroy(&f[1]);
165:   FNDestroy(&f[2]);
166:   SlepcFinalize();
167:   return ierr;
168: }

170: /*TEST

172:    test:
173:       suffix: 1

175: TEST*/