Actual source code: test16.c
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: */
11: static char help[] = "Illustrates use of NEPSetEigenvalueComparison().\n\n"
12: "This is a simplified version of ex20.\n"
13: "The command line options are:\n"
14: " -n <n>, where <n> = number of grid subdivisions.\n";
16: /*
17: Solve 1-D PDE
18: -u'' = lambda*u
19: on [0,1] subject to
20: u(0)=0, u'(1)=u(1)*lambda*kappa/(kappa-lambda)
21: */
23: #include <slepcnep.h>
25: /*
26: User-defined routines
27: */
28: PetscErrorCode FormFunction(NEP,PetscScalar,Mat,Mat,void*);
29: PetscErrorCode MyEigenSort(PetscScalar,PetscScalar,PetscScalar,PetscScalar,PetscInt*,void*);
31: /*
32: User-defined application context
33: */
34: typedef struct {
35: PetscScalar kappa; /* ratio between stiffness of spring and attached mass */
36: PetscReal h; /* mesh spacing */
37: } ApplicationCtx;
39: int main(int argc,char **argv)
40: {
41: NEP nep; /* nonlinear eigensolver context */
42: Mat F; /* Function matrix */
43: ApplicationCtx ctx; /* user-defined context */
44: PetscScalar target;
45: RG rg;
46: PetscInt n=128;
47: PetscBool terse;
49: PetscFunctionBeginUser;
50: PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
51: PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
52: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Nonlinear Eigenproblem, n=%" PetscInt_FMT "\n\n",n));
53: ctx.h = 1.0/(PetscReal)n;
54: ctx.kappa = 1.0;
56: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57: Prepare nonlinear eigensolver context
58: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
60: PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
62: PetscCall(MatCreate(PETSC_COMM_WORLD,&F));
63: PetscCall(MatSetSizes(F,PETSC_DECIDE,PETSC_DECIDE,n,n));
64: PetscCall(MatSetFromOptions(F));
65: PetscCall(MatSeqAIJSetPreallocation(F,3,NULL));
66: PetscCall(MatMPIAIJSetPreallocation(F,3,NULL,1,NULL));
67: PetscCall(NEPSetFunction(nep,F,F,FormFunction,&ctx));
69: PetscCall(NEPSetType(nep,NEPNLEIGS));
70: PetscCall(NEPGetRG(nep,&rg));
71: PetscCall(RGSetType(rg,RGINTERVAL));
72: #if defined(PETSC_USE_COMPLEX)
73: PetscCall(RGIntervalSetEndpoints(rg,2.0,400.0,-0.001,0.001));
74: #else
75: PetscCall(RGIntervalSetEndpoints(rg,2.0,400.0,0,0));
76: #endif
77: PetscCall(NEPSetTarget(nep,25.0));
78: PetscCall(NEPSetEigenvalueComparison(nep,MyEigenSort,&target));
79: PetscCall(NEPSetTolerances(nep,PETSC_SMALL,PETSC_CURRENT));
80: PetscCall(NEPSetFromOptions(nep));
81: PetscCall(NEPGetTarget(nep,&target));
83: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84: Solve the eigensystem and display the solution
85: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
87: PetscCall(NEPSolve(nep));
89: /* show detailed info unless -terse option is given by user */
90: PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
91: if (terse) PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
92: else {
93: PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
94: PetscCall(NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD));
95: PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
96: PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
97: }
99: PetscCall(NEPDestroy(&nep));
100: PetscCall(MatDestroy(&F));
101: PetscCall(SlepcFinalize());
102: return 0;
103: }
105: /* ------------------------------------------------------------------- */
106: /*
107: FormFunction - Computes Function matrix T(lambda)
109: Input Parameters:
110: . nep - the NEP context
111: . lambda - the scalar argument
112: . ctx - optional user-defined context, as set by NEPSetFunction()
114: Output Parameters:
115: . fun - Function matrix
116: . B - optionally different preconditioning matrix
117: */
118: PetscErrorCode FormFunction(NEP nep,PetscScalar lambda,Mat fun,Mat B,void *ctx)
119: {
120: ApplicationCtx *user = (ApplicationCtx*)ctx;
121: PetscScalar A[3],c,d;
122: PetscReal h;
123: PetscInt i,n,j[3],Istart,Iend;
124: PetscBool FirstBlock=PETSC_FALSE,LastBlock=PETSC_FALSE;
126: PetscFunctionBeginUser;
127: /*
128: Compute Function entries and insert into matrix
129: */
130: PetscCall(MatGetSize(fun,&n,NULL));
131: PetscCall(MatGetOwnershipRange(fun,&Istart,&Iend));
132: if (Istart==0) FirstBlock=PETSC_TRUE;
133: if (Iend==n) LastBlock=PETSC_TRUE;
134: h = user->h;
135: c = user->kappa/(lambda-user->kappa);
136: d = n;
138: /*
139: Interior grid points
140: */
141: for (i=(FirstBlock? Istart+1: Istart);i<(LastBlock? Iend-1: Iend);i++) {
142: j[0] = i-1; j[1] = i; j[2] = i+1;
143: A[0] = A[2] = -d-lambda*h/6.0; A[1] = 2.0*(d-lambda*h/3.0);
144: PetscCall(MatSetValues(fun,1,&i,3,j,A,INSERT_VALUES));
145: }
147: /*
148: Boundary points
149: */
150: if (FirstBlock) {
151: i = 0;
152: j[0] = 0; j[1] = 1;
153: A[0] = 2.0*(d-lambda*h/3.0); A[1] = -d-lambda*h/6.0;
154: PetscCall(MatSetValues(fun,1,&i,2,j,A,INSERT_VALUES));
155: }
157: if (LastBlock) {
158: i = n-1;
159: j[0] = n-2; j[1] = n-1;
160: A[0] = -d-lambda*h/6.0; A[1] = d-lambda*h/3.0+lambda*c;
161: PetscCall(MatSetValues(fun,1,&i,2,j,A,INSERT_VALUES));
162: }
164: /*
165: Assemble matrix
166: */
167: PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
168: PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
169: if (fun != B) {
170: PetscCall(MatAssemblyBegin(fun,MAT_FINAL_ASSEMBLY));
171: PetscCall(MatAssemblyEnd(fun,MAT_FINAL_ASSEMBLY));
172: }
173: PetscFunctionReturn(PETSC_SUCCESS);
174: }
176: /*
177: Function for user-defined eigenvalue ordering criterion.
179: Given two eigenvalues ar+i*ai and br+i*bi, the subroutine must choose
180: one of them as the preferred one according to the criterion.
181: In this example, eigenvalues are sorted with respect to the target,
182: but those on the right of the target are preferred.
183: */
184: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx)
185: {
186: PetscReal a,b;
187: PetscScalar target = *(PetscScalar*)ctx;
189: PetscFunctionBeginUser;
190: if (PetscRealPart(ar-target)<0.0 && PetscRealPart(br-target)>0.0) *r = 1;
191: else {
192: a = SlepcAbsEigenvalue(ar-target,ai);
193: b = SlepcAbsEigenvalue(br-target,bi);
194: if (a>b) *r = 1;
195: else if (a<b) *r = -1;
196: else *r = 0;
197: }
198: PetscFunctionReturn(PETSC_SUCCESS);
199: }
201: /*TEST
203: test:
204: suffix: 1
205: args: -nep_nev 4 -nep_ncv 8 -terse
206: requires: double !complex
208: TEST*/