Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
11 : static char help[] = "Test NEP view and monitor functionality.\n\n";
12 :
13 : #include <slepcnep.h>
14 :
15 3 : int main(int argc,char **argv)
16 : {
17 3 : Mat A[3];
18 3 : FN f[3];
19 3 : NEP nep;
20 3 : Vec xr,xi;
21 3 : PetscScalar kr,ki,coeffs[3];
22 3 : PetscInt n=6,i,Istart,Iend,nconv,its;
23 3 : PetscReal errest;
24 3 : PetscBool checkfile;
25 3 : char filename[PETSC_MAX_PATH_LEN];
26 3 : PetscViewer viewer;
27 :
28 3 : PetscFunctionBeginUser;
29 3 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
30 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Nonlinear Eigenproblem, n=%" PetscInt_FMT "\n\n",n));
31 :
32 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33 : Generate the matrices
34 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35 :
36 3 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A[0]));
37 3 : PetscCall(MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n));
38 3 : PetscCall(MatSetFromOptions(A[0]));
39 3 : PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
40 21 : for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[0],i,i,i+1,INSERT_VALUES));
41 3 : PetscCall(MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY));
42 3 : PetscCall(MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY));
43 :
44 3 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A[1]));
45 3 : PetscCall(MatSetSizes(A[1],PETSC_DECIDE,PETSC_DECIDE,n,n));
46 3 : PetscCall(MatSetFromOptions(A[1]));
47 21 : for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[1],i,i,-1.5,INSERT_VALUES));
48 3 : PetscCall(MatAssemblyBegin(A[1],MAT_FINAL_ASSEMBLY));
49 3 : PetscCall(MatAssemblyEnd(A[1],MAT_FINAL_ASSEMBLY));
50 :
51 3 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A[2]));
52 3 : PetscCall(MatSetSizes(A[2],PETSC_DECIDE,PETSC_DECIDE,n,n));
53 3 : PetscCall(MatSetFromOptions(A[2]));
54 21 : for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[2],i,i,-1.0/(i+1),INSERT_VALUES));
55 3 : PetscCall(MatAssemblyBegin(A[2],MAT_FINAL_ASSEMBLY));
56 3 : PetscCall(MatAssemblyEnd(A[2],MAT_FINAL_ASSEMBLY));
57 :
58 : /*
59 : Functions: f0=1.0, f1=lambda, f2=lambda^2
60 : */
61 3 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f[0]));
62 3 : PetscCall(FNSetType(f[0],FNRATIONAL));
63 3 : coeffs[0] = 1.0;
64 3 : PetscCall(FNRationalSetNumerator(f[0],1,coeffs));
65 :
66 3 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f[1]));
67 3 : PetscCall(FNSetType(f[1],FNRATIONAL));
68 3 : coeffs[0] = 1.0; coeffs[1] = 0.0;
69 3 : PetscCall(FNRationalSetNumerator(f[1],2,coeffs));
70 :
71 3 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f[2]));
72 3 : PetscCall(FNSetType(f[2],FNRATIONAL));
73 3 : coeffs[0] = 1.0; coeffs[1] = 0.0; coeffs[2] = 0.0;
74 3 : PetscCall(FNRationalSetNumerator(f[2],3,coeffs));
75 :
76 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 : Create the NEP solver
78 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
79 3 : PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
80 3 : PetscCall(PetscObjectSetName((PetscObject)nep,"nep"));
81 3 : PetscCall(NEPSetSplitOperator(nep,3,A,f,SAME_NONZERO_PATTERN));
82 3 : PetscCall(NEPSetTarget(nep,1.1));
83 3 : PetscCall(NEPSetWhichEigenpairs(nep,NEP_TARGET_MAGNITUDE));
84 3 : PetscCall(NEPSetFromOptions(nep));
85 :
86 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87 : Solve the eigensystem and display solution
88 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
89 3 : PetscCall(NEPSolve(nep));
90 3 : PetscCall(NEPGetConverged(nep,&nconv));
91 3 : PetscCall(NEPGetIterationNumber(nep,&its));
92 3 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," %" PetscInt_FMT " converged eigenpairs after %" PetscInt_FMT " iterations\n",nconv,its));
93 3 : if (nconv>0) {
94 3 : PetscCall(MatCreateVecs(A[0],&xr,&xi));
95 3 : PetscCall(NEPGetEigenpair(nep,0,&kr,&ki,xr,xi));
96 3 : PetscCall(VecDestroy(&xr));
97 3 : PetscCall(VecDestroy(&xi));
98 3 : PetscCall(NEPGetErrorEstimate(nep,0,&errest));
99 : }
100 3 : PetscCall(NEPErrorView(nep,NEP_ERROR_BACKWARD,NULL));
101 :
102 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103 : Check file containing the eigenvalues
104 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
105 3 : PetscCall(PetscOptionsGetString(NULL,NULL,"-checkfile",filename,sizeof(filename),&checkfile));
106 3 : if (checkfile) {
107 : #if defined(PETSC_HAVE_COMPLEX)
108 1 : PetscComplex *eigs,eval;
109 1 : PetscCall(PetscMalloc1(nconv,&eigs));
110 1 : PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
111 1 : PetscCall(PetscViewerBinaryRead(viewer,eigs,nconv,NULL,PETSC_COMPLEX));
112 1 : PetscCall(PetscViewerDestroy(&viewer));
113 5 : for (i=0;i<nconv;i++) {
114 4 : PetscCall(NEPGetEigenpair(nep,i,&kr,&ki,NULL,NULL));
115 : #if defined(PETSC_USE_COMPLEX)
116 : eval = kr;
117 : #else
118 4 : eval = PetscCMPLX(kr,ki);
119 : #endif
120 4 : PetscCheck(eval==eigs[i],PETSC_COMM_WORLD,PETSC_ERR_FILE_UNEXPECTED,"Eigenvalues in the file do not match");
121 : }
122 1 : PetscCall(PetscFree(eigs));
123 : #else
124 : SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"The -checkfile option requires C99 complex numbers");
125 : #endif
126 : }
127 :
128 3 : PetscCall(NEPDestroy(&nep));
129 3 : PetscCall(MatDestroy(&A[0]));
130 3 : PetscCall(MatDestroy(&A[1]));
131 3 : PetscCall(MatDestroy(&A[2]));
132 3 : PetscCall(FNDestroy(&f[0]));
133 3 : PetscCall(FNDestroy(&f[1]));
134 3 : PetscCall(FNDestroy(&f[2]));
135 3 : PetscCall(SlepcFinalize());
136 : return 0;
137 : }
138 :
139 : /*TEST
140 :
141 : test:
142 : suffix: 1
143 : args: -nep_type slp -nep_target -.5 -nep_error_backward ::ascii_info_detail -nep_view_values -nep_error_absolute ::ascii_matlab -nep_monitor_all -nep_converged_reason -nep_view
144 : filter: grep -v "tolerance" | grep -v "problem type" | sed -e "s/[+-]0\.0*i//g" -e "s/+0i//" -e "s/[+-][0-9]\.[0-9]*e-[0-9]*i//g" -e "s/[0-9]\.[0-9]*e[+-]\([0-9]*\)/removed/g"
145 : requires: double
146 :
147 : test:
148 : suffix: 2
149 : args: -nep_type rii -nep_target -.5 -nep_rii_hermitian -nep_monitor -nep_view_values ::ascii_matlab
150 : filter: sed -e "s/[+-][0-9]\.[0-9]*e-[0-9]*i//" -e "s/([0-9]\.[0-9]*e[+-]\([0-9]*\))/(removed)/g"
151 : requires: double
152 :
153 : test:
154 : suffix: 3
155 : args: -nep_type slp -nep_nev 4 -nep_view_values binary:myvalues.bin -checkfile myvalues.bin -nep_error_relative ::ascii_matlab
156 : filter: sed -e "s/[0-9]\.[0-9]*e[+-]\([0-9]*\)/removed/g"
157 : requires: double c99_complex
158 :
159 : test:
160 : suffix: 4
161 : args: -nep_type slp -nep_nev 4 -nep_monitor draw::draw_lg -nep_monitor_all draw::draw_lg -nep_view_values draw -draw_save myeigen.ppm -draw_virtual
162 : requires: x double
163 :
164 : TEST*/
|