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 some NLEIGS interface functions.\n\n"
12 : "Based on ex27.c. The command line options are:\n"
13 : " -n <n>, where <n> = matrix dimension.\n";
14 :
15 : /*
16 : Solve T(lambda)x=0 using NLEIGS solver
17 : with T(lambda) = -D+sqrt(lambda)*I
18 : where D is the Laplacian operator in 1 dimension
19 : and with the interpolation interval [.01,16]
20 : */
21 :
22 : #include <slepcnep.h>
23 :
24 : /*
25 : User-defined routines
26 : */
27 : PetscErrorCode ComputeSingularities(NEP,PetscInt*,PetscScalar*,void*);
28 :
29 1 : int main(int argc,char **argv)
30 : {
31 1 : NEP nep; /* nonlinear eigensolver context */
32 1 : Mat A[2];
33 1 : PetscInt n=100,Istart,Iend,i,ns,nsin;
34 1 : PetscBool terse,fb;
35 1 : RG rg;
36 1 : FN f[2];
37 1 : PetscScalar coeffs,shifts[]={1.06,1.1,1.12,1.15},*rkshifts,val;
38 1 : PetscErrorCode (*fsing)(NEP,PetscInt*,PetscScalar*,void*);
39 :
40 1 : PetscFunctionBeginUser;
41 1 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
42 1 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
43 1 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSquare root eigenproblem, n=%" PetscInt_FMT "\n\n",n));
44 :
45 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 : Create nonlinear eigensolver and set some options
47 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
48 :
49 1 : PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
50 1 : PetscCall(NEPSetType(nep,NEPNLEIGS));
51 1 : PetscCall(NEPNLEIGSSetSingularitiesFunction(nep,ComputeSingularities,NULL));
52 1 : PetscCall(NEPGetRG(nep,&rg));
53 1 : PetscCall(RGSetType(rg,RGINTERVAL));
54 : #if defined(PETSC_USE_COMPLEX)
55 1 : PetscCall(RGIntervalSetEndpoints(rg,0.01,16.0,-0.001,0.001));
56 : #else
57 : PetscCall(RGIntervalSetEndpoints(rg,0.01,16.0,0,0));
58 : #endif
59 1 : PetscCall(NEPSetTarget(nep,1.1));
60 :
61 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62 : Define the nonlinear problem in split form
63 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
64 :
65 : /* Create matrices */
66 1 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A[0]));
67 1 : PetscCall(MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n));
68 1 : PetscCall(MatSetFromOptions(A[0]));
69 1 : PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
70 101 : for (i=Istart;i<Iend;i++) {
71 100 : if (i>0) PetscCall(MatSetValue(A[0],i,i-1,1.0,INSERT_VALUES));
72 100 : if (i<n-1) PetscCall(MatSetValue(A[0],i,i+1,1.0,INSERT_VALUES));
73 100 : PetscCall(MatSetValue(A[0],i,i,-2.0,INSERT_VALUES));
74 : }
75 1 : PetscCall(MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY));
76 1 : PetscCall(MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY));
77 :
78 1 : PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,&A[1]));
79 :
80 : /* Define functions */
81 1 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f[0]));
82 1 : PetscCall(FNSetType(f[0],FNRATIONAL));
83 1 : coeffs = 1.0;
84 1 : PetscCall(FNRationalSetNumerator(f[0],1,&coeffs));
85 1 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f[1]));
86 1 : PetscCall(FNSetType(f[1],FNSQRT));
87 1 : PetscCall(NEPSetSplitOperator(nep,2,A,f,SUBSET_NONZERO_PATTERN));
88 :
89 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90 : Set some options
91 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
92 :
93 1 : PetscCall(NEPNLEIGSSetFullBasis(nep,PETSC_FALSE));
94 1 : PetscCall(NEPNLEIGSSetRKShifts(nep,4,shifts));
95 1 : PetscCall(NEPSetFromOptions(nep));
96 :
97 1 : PetscCall(NEPNLEIGSGetFullBasis(nep,&fb));
98 2 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using full basis = %s\n",fb?"true":"false"));
99 1 : PetscCall(NEPNLEIGSGetRKShifts(nep,&ns,&rkshifts));
100 1 : if (ns) {
101 1 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using %" PetscInt_FMT " RK shifts =",ns));
102 5 : for (i=0;i<ns;i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %g",(double)PetscRealPart(rkshifts[i])));
103 1 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
104 1 : PetscCall(PetscFree(rkshifts));
105 : }
106 1 : PetscCall(NEPNLEIGSGetSingularitiesFunction(nep,&fsing,NULL));
107 1 : nsin = 1;
108 1 : PetscCall((*fsing)(nep,&nsin,&val,NULL));
109 1 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," First returned singularity = %g\n",(double)PetscRealPart(val)));
110 :
111 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112 : Solve the eigensystem
113 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114 1 : PetscCall(NEPSolve(nep));
115 :
116 : /* show detailed info unless -terse option is given by user */
117 1 : PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
118 1 : if (terse) PetscCall(NEPErrorView(nep,NEP_ERROR_BACKWARD,NULL));
119 : else {
120 0 : PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
121 0 : PetscCall(NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD));
122 0 : PetscCall(NEPErrorView(nep,NEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD));
123 0 : PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
124 : }
125 1 : PetscCall(NEPDestroy(&nep));
126 1 : PetscCall(MatDestroy(&A[0]));
127 1 : PetscCall(MatDestroy(&A[1]));
128 1 : PetscCall(FNDestroy(&f[0]));
129 1 : PetscCall(FNDestroy(&f[1]));
130 1 : PetscCall(SlepcFinalize());
131 : return 0;
132 : }
133 :
134 : /* ------------------------------------------------------------------- */
135 : /*
136 : ComputeSingularities - Computes maxnp points (at most) in the complex plane where
137 : the function T(.) is not analytic.
138 :
139 : In this case, we discretize the singularity region (-inf,0)~(-10e+6,-10e-6)
140 : */
141 2 : PetscErrorCode ComputeSingularities(NEP nep,PetscInt *maxnp,PetscScalar *xi,void *pt)
142 : {
143 2 : PetscReal h;
144 2 : PetscInt i;
145 :
146 2 : PetscFunctionBeginUser;
147 2 : h = 11.0/(*maxnp-1);
148 2 : xi[0] = -1e-5; xi[*maxnp-1] = -1e+6;
149 10000 : for (i=1;i<*maxnp-1;i++) xi[i] = -PetscPowReal(10,-5+h*i);
150 2 : PetscFunctionReturn(PETSC_SUCCESS);
151 : }
152 :
153 : /*TEST
154 :
155 : test:
156 : suffix: 1
157 : args: -nep_nev 3 -nep_nleigs_interpolation_degree 20 -terse -nep_view
158 : requires: double
159 : filter: grep -v tolerance | sed -e "s/[+-]0\.0*i//g"
160 :
161 : TEST*/
|