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[] = "Tests a user-provided preconditioner.\n\n"
12 : "The command line options are:\n"
13 : " -n <n>, where <n> = number of grid subdivisions.\n"
14 : " -tau <tau>, where <tau> is the delay parameter.\n"
15 : " -a <a>, where <a> is the coefficient that multiplies u in the equation.\n"
16 : " -split <0/1>, to select the split form in the problem definition (enabled by default).\n";
17 :
18 : /* Based on ex22.c (delay) */
19 :
20 : #include <slepcnep.h>
21 :
22 : /*
23 : User-defined application context
24 : */
25 : typedef struct {
26 : PetscScalar tau;
27 : PetscReal a;
28 : } ApplicationCtx;
29 :
30 : /*
31 : Create problem matrices in split form
32 : */
33 7 : PetscErrorCode BuildSplitMatrices(PetscInt n,PetscReal a,Mat *Id,Mat *A,Mat *B)
34 : {
35 7 : PetscInt i,Istart,Iend;
36 7 : PetscReal h,xi;
37 7 : PetscScalar b;
38 :
39 7 : PetscFunctionBeginUser;
40 7 : h = PETSC_PI/(PetscReal)(n+1);
41 :
42 : /* Identity matrix */
43 7 : PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,Id));
44 7 : PetscCall(MatSetOption(*Id,MAT_HERMITIAN,PETSC_TRUE));
45 :
46 : /* A = 1/h^2*tridiag(1,-2,1) + a*I */
47 7 : PetscCall(MatCreate(PETSC_COMM_WORLD,A));
48 7 : PetscCall(MatSetSizes(*A,PETSC_DECIDE,PETSC_DECIDE,n,n));
49 7 : PetscCall(MatSetFromOptions(*A));
50 7 : PetscCall(MatGetOwnershipRange(*A,&Istart,&Iend));
51 775 : for (i=Istart;i<Iend;i++) {
52 768 : if (i>0) PetscCall(MatSetValue(*A,i,i-1,1.0/(h*h),INSERT_VALUES));
53 768 : if (i<n-1) PetscCall(MatSetValue(*A,i,i+1,1.0/(h*h),INSERT_VALUES));
54 768 : PetscCall(MatSetValue(*A,i,i,-2.0/(h*h)+a,INSERT_VALUES));
55 : }
56 7 : PetscCall(MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY));
57 7 : PetscCall(MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY));
58 7 : PetscCall(MatSetOption(*A,MAT_HERMITIAN,PETSC_TRUE));
59 :
60 : /* B = diag(b(xi)) */
61 7 : PetscCall(MatCreate(PETSC_COMM_WORLD,B));
62 7 : PetscCall(MatSetSizes(*B,PETSC_DECIDE,PETSC_DECIDE,n,n));
63 7 : PetscCall(MatSetFromOptions(*B));
64 7 : PetscCall(MatGetOwnershipRange(*B,&Istart,&Iend));
65 775 : for (i=Istart;i<Iend;i++) {
66 768 : xi = (i+1)*h;
67 768 : b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
68 768 : PetscCall(MatSetValue(*B,i,i,b,INSERT_VALUES));
69 : }
70 7 : PetscCall(MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY));
71 7 : PetscCall(MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY));
72 7 : PetscCall(MatSetOption(*B,MAT_HERMITIAN,PETSC_TRUE));
73 7 : PetscFunctionReturn(PETSC_SUCCESS);
74 : }
75 :
76 : /*
77 : Create preconditioner matrices (only Ap=diag(A))
78 : */
79 7 : PetscErrorCode BuildSplitPreconditioner(PetscInt n,PetscReal a,Mat *Ap)
80 : {
81 7 : PetscInt i,Istart,Iend;
82 7 : PetscReal h;
83 :
84 7 : PetscFunctionBeginUser;
85 7 : h = PETSC_PI/(PetscReal)(n+1);
86 :
87 : /* Ap = diag(A) */
88 7 : PetscCall(MatCreate(PETSC_COMM_WORLD,Ap));
89 7 : PetscCall(MatSetSizes(*Ap,PETSC_DECIDE,PETSC_DECIDE,n,n));
90 7 : PetscCall(MatSetFromOptions(*Ap));
91 7 : PetscCall(MatGetOwnershipRange(*Ap,&Istart,&Iend));
92 775 : for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(*Ap,i,i,-2.0/(h*h)+a,INSERT_VALUES));
93 7 : PetscCall(MatAssemblyBegin(*Ap,MAT_FINAL_ASSEMBLY));
94 7 : PetscCall(MatAssemblyEnd(*Ap,MAT_FINAL_ASSEMBLY));
95 7 : PetscCall(MatSetOption(*Ap,MAT_HERMITIAN,PETSC_TRUE));
96 7 : PetscFunctionReturn(PETSC_SUCCESS);
97 : }
98 :
99 : /*
100 : Compute Function matrix T(lambda)
101 : */
102 233 : PetscErrorCode FormFunction(NEP nep,PetscScalar lambda,Mat fun,Mat B,void *ctx)
103 : {
104 233 : ApplicationCtx *user = (ApplicationCtx*)ctx;
105 233 : PetscInt i,n,Istart,Iend;
106 233 : PetscReal h,xi;
107 233 : PetscScalar b;
108 :
109 233 : PetscFunctionBeginUser;
110 233 : PetscCall(MatGetSize(fun,&n,NULL));
111 233 : h = PETSC_PI/(PetscReal)(n+1);
112 233 : PetscCall(MatGetOwnershipRange(fun,&Istart,&Iend));
113 30057 : for (i=Istart;i<Iend;i++) {
114 29824 : if (i>0) PetscCall(MatSetValue(fun,i,i-1,1.0/(h*h),INSERT_VALUES));
115 29824 : if (i<n-1) PetscCall(MatSetValue(fun,i,i+1,1.0/(h*h),INSERT_VALUES));
116 29824 : xi = (i+1)*h;
117 29824 : b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
118 29824 : PetscCall(MatSetValue(fun,i,i,-lambda-2.0/(h*h)+user->a+PetscExpScalar(-user->tau*lambda)*b,INSERT_VALUES));
119 29824 : if (B!=fun) PetscCall(MatSetValue(B,i,i,-lambda-2.0/(h*h)+user->a+PetscExpScalar(-user->tau*lambda)*b,INSERT_VALUES));
120 : }
121 233 : PetscCall(MatAssemblyBegin(fun,MAT_FINAL_ASSEMBLY));
122 233 : PetscCall(MatAssemblyEnd(fun,MAT_FINAL_ASSEMBLY));
123 233 : if (fun != B) {
124 46 : PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
125 46 : PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
126 : }
127 233 : PetscFunctionReturn(PETSC_SUCCESS);
128 : }
129 :
130 : /*
131 : Compute Jacobian matrix T'(lambda)
132 : */
133 38 : PetscErrorCode FormJacobian(NEP nep,PetscScalar lambda,Mat jac,void *ctx)
134 : {
135 38 : ApplicationCtx *user = (ApplicationCtx*)ctx;
136 38 : PetscInt i,n,Istart,Iend;
137 38 : PetscReal h,xi;
138 38 : PetscScalar b;
139 :
140 38 : PetscFunctionBeginUser;
141 38 : PetscCall(MatGetSize(jac,&n,NULL));
142 38 : h = PETSC_PI/(PetscReal)(n+1);
143 38 : PetscCall(MatGetOwnershipRange(jac,&Istart,&Iend));
144 4902 : for (i=Istart;i<Iend;i++) {
145 4864 : xi = (i+1)*h;
146 4864 : b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
147 4864 : PetscCall(MatSetValue(jac,i,i,-1.0-user->tau*PetscExpScalar(-user->tau*lambda)*b,INSERT_VALUES));
148 : }
149 38 : PetscCall(MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY));
150 38 : PetscCall(MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY));
151 38 : PetscFunctionReturn(PETSC_SUCCESS);
152 : }
153 :
154 11 : int main(int argc,char **argv)
155 : {
156 11 : NEP nep; /* nonlinear eigensolver context */
157 11 : Mat Id,A,B,Ap,J,F,P; /* problem matrices */
158 11 : FN f1,f2,f3; /* functions to define the nonlinear operator */
159 11 : ApplicationCtx ctx; /* user-defined context */
160 11 : Mat mats[3],M;
161 11 : FN funs[3];
162 11 : PetscScalar coeffs[2];
163 11 : PetscInt n=128,nterm;
164 11 : PetscReal tau=0.001,a=20;
165 11 : PetscBool split=PETSC_TRUE;
166 11 : MatStructure mstr;
167 :
168 11 : PetscFunctionBeginUser;
169 11 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
170 11 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
171 11 : PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
172 11 : PetscCall(PetscOptionsGetReal(NULL,NULL,"-a",&a,NULL));
173 11 : PetscCall(PetscOptionsGetBool(NULL,NULL,"-split",&split,NULL));
174 11 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Delay Eigenproblem, n=%" PetscInt_FMT ", tau=%g, a=%g\n\n",n,(double)tau,(double)a));
175 :
176 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177 : Create nonlinear eigensolver and solve the problem
178 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
179 :
180 11 : PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
181 11 : if (split) {
182 7 : PetscCall(BuildSplitMatrices(n,a,&Id,&A,&B));
183 : /* f1=-lambda */
184 7 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f1));
185 7 : PetscCall(FNSetType(f1,FNRATIONAL));
186 7 : coeffs[0] = -1.0; coeffs[1] = 0.0;
187 7 : PetscCall(FNRationalSetNumerator(f1,2,coeffs));
188 : /* f2=1.0 */
189 7 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f2));
190 7 : PetscCall(FNSetType(f2,FNRATIONAL));
191 7 : coeffs[0] = 1.0;
192 7 : PetscCall(FNRationalSetNumerator(f2,1,coeffs));
193 : /* f3=exp(-tau*lambda) */
194 7 : PetscCall(FNCreate(PETSC_COMM_WORLD,&f3));
195 7 : PetscCall(FNSetType(f3,FNEXP));
196 7 : PetscCall(FNSetScale(f3,-tau,1.0));
197 7 : mats[0] = A; funs[0] = f2;
198 7 : mats[1] = Id; funs[1] = f1;
199 7 : mats[2] = B; funs[2] = f3;
200 7 : PetscCall(NEPSetSplitOperator(nep,3,mats,funs,SUBSET_NONZERO_PATTERN));
201 7 : PetscCall(BuildSplitPreconditioner(n,a,&Ap));
202 7 : mats[0] = Ap;
203 7 : mats[1] = Id;
204 7 : mats[2] = B;
205 7 : PetscCall(NEPSetSplitPreconditioner(nep,3,mats,SAME_NONZERO_PATTERN));
206 : } else {
207 : /* callback form */
208 4 : ctx.tau = tau;
209 4 : ctx.a = a;
210 4 : PetscCall(MatCreate(PETSC_COMM_WORLD,&F));
211 4 : PetscCall(MatSetSizes(F,PETSC_DECIDE,PETSC_DECIDE,n,n));
212 4 : PetscCall(MatSetFromOptions(F));
213 4 : PetscCall(MatSeqAIJSetPreallocation(F,3,NULL));
214 4 : PetscCall(MatMPIAIJSetPreallocation(F,3,NULL,1,NULL));
215 4 : PetscCall(MatDuplicate(F,MAT_DO_NOT_COPY_VALUES,&P));
216 4 : PetscCall(NEPSetFunction(nep,F,P,FormFunction,&ctx));
217 4 : PetscCall(MatCreate(PETSC_COMM_WORLD,&J));
218 4 : PetscCall(MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,n,n));
219 4 : PetscCall(MatSetFromOptions(J));
220 4 : PetscCall(MatSeqAIJSetPreallocation(J,3,NULL));
221 4 : PetscCall(MatMPIAIJSetPreallocation(F,3,NULL,1,NULL));
222 4 : PetscCall(NEPSetJacobian(nep,J,FormJacobian,&ctx));
223 : }
224 :
225 : /* Set solver parameters at runtime */
226 11 : PetscCall(NEPSetFromOptions(nep));
227 :
228 11 : if (split) {
229 7 : PetscCall(NEPGetSplitPreconditionerInfo(nep,&nterm,&mstr));
230 7 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Nonlinear preconditioner with %" PetscInt_FMT " terms, with %s nonzero pattern\n",nterm,MatStructures[mstr]));
231 7 : PetscCall(NEPGetSplitPreconditionerTerm(nep,0,&M));
232 : }
233 :
234 : /* Solve the eigensystem */
235 11 : PetscCall(NEPSolve(nep));
236 11 : PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
237 :
238 11 : PetscCall(NEPDestroy(&nep));
239 11 : if (split) {
240 7 : PetscCall(MatDestroy(&Id));
241 7 : PetscCall(MatDestroy(&A));
242 7 : PetscCall(MatDestroy(&B));
243 7 : PetscCall(MatDestroy(&Ap));
244 7 : PetscCall(FNDestroy(&f1));
245 7 : PetscCall(FNDestroy(&f2));
246 7 : PetscCall(FNDestroy(&f3));
247 : } else {
248 4 : PetscCall(MatDestroy(&F));
249 4 : PetscCall(MatDestroy(&P));
250 4 : PetscCall(MatDestroy(&J));
251 : }
252 11 : PetscCall(SlepcFinalize());
253 : return 0;
254 : }
255 :
256 : /*TEST
257 :
258 : testset:
259 : args: -a 90000 -nep_nev 2
260 : requires: double !defined(PETSCTEST_VALGRIND)
261 : output_file: output/test17_1.out
262 : timeoutfactor: 2
263 : filter: grep -v "with 3 terms, with SAME"
264 : test:
265 : suffix: 1
266 : args: -nep_type slp -nep_two_sided {{0 1}} -split {{0 1}}
267 :
268 : testset:
269 : args: -nep_nev 2 -rg_type interval -rg_interval_endpoints .5,15,-.1,.1 -nep_target .7
270 : requires: !single
271 : output_file: output/test17_2.out
272 : filter: sed -e "s/[+-]0\.0*i//g" | grep -v "with 3 terms, with SAME"
273 : test:
274 : suffix: 2_interpol
275 : args: -nep_type interpol -nep_interpol_st_ksp_type bcgs -nep_interpol_st_pc_type sor -nep_tol 1e-6 -nep_interpol_st_ksp_rtol 1e-7
276 : test:
277 : suffix: 2_nleigs
278 : args: -nep_type nleigs -split {{0 1}}
279 : requires: complex
280 : test:
281 : suffix: 2_nleigs_real
282 : args: -nep_type nleigs -rg_interval_endpoints .5,11 -split {{0 1}} -nep_nleigs_ksp_type tfqmr
283 : requires: !complex !__float128
284 :
285 : testset:
286 : args: -nep_type ciss -rg_type ellipse -rg_ellipse_center 10 -rg_ellipse_radius 9.5 -rg_ellipse_vscale 0.1 -nep_ciss_ksp_type bcgs -nep_ciss_pc_type sor
287 : output_file: output/test17_3.out
288 : requires: complex !single !defined(PETSCTEST_VALGRIND)
289 : filter: grep -v "with 3 terms, with SAME"
290 : test:
291 : suffix: 3
292 : args: -split {{0 1}}
293 : test:
294 : suffix: 3_par
295 : nsize: 2
296 : args: -nep_ciss_partitions 2
297 :
298 : TEST*/
|