Actual source code: nepimpl.h
slepc-3.17.0 2022-03-31
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: #if !defined(SLEPCNEPIMPL_H)
12: #define SLEPCNEPIMPL_H
14: #include <slepcnep.h>
15: #include <slepc/private/slepcimpl.h>
17: SLEPC_EXTERN PetscBool NEPRegisterAllCalled;
18: SLEPC_EXTERN PetscBool NEPMonitorRegisterAllCalled;
19: SLEPC_EXTERN PetscErrorCode NEPRegisterAll(void);
20: SLEPC_EXTERN PetscErrorCode NEPMonitorRegisterAll(void);
21: SLEPC_EXTERN PetscLogEvent NEP_SetUp,NEP_Solve,NEP_Refine,NEP_FunctionEval,NEP_JacobianEval,NEP_Resolvent,NEP_CISS_SVD;
23: typedef struct _NEPOps *NEPOps;
25: struct _NEPOps {
26: PetscErrorCode (*solve)(NEP);
27: PetscErrorCode (*setup)(NEP);
28: PetscErrorCode (*setfromoptions)(PetscOptionItems*,NEP);
29: PetscErrorCode (*publishoptions)(NEP);
30: PetscErrorCode (*destroy)(NEP);
31: PetscErrorCode (*reset)(NEP);
32: PetscErrorCode (*view)(NEP,PetscViewer);
33: PetscErrorCode (*computevectors)(NEP);
34: };
36: /*
37: Maximum number of monitors you can run with a single NEP
38: */
39: #define MAXNEPMONITORS 5
41: typedef enum { NEP_STATE_INITIAL,
42: NEP_STATE_SETUP,
43: NEP_STATE_SOLVED,
44: NEP_STATE_EIGENVECTORS } NEPStateType;
46: /*
47: How the problem function T(lambda) has been defined by the user
48: - Callback: one callback to build the function matrix, another one for the Jacobian
49: - Split: in split form sum_j(A_j*f_j(lambda))
50: */
51: typedef enum { NEP_USER_INTERFACE_CALLBACK=1,
52: NEP_USER_INTERFACE_SPLIT } NEPUserInterface;
54: /*
55: To check for unsupported features at NEPSetUp_XXX()
56: */
57: typedef enum { NEP_FEATURE_CALLBACK=1, /* callback user interface */
58: NEP_FEATURE_REGION=4, /* nontrivial region for filtering */
59: NEP_FEATURE_CONVERGENCE=16, /* convergence test selected by user */
60: NEP_FEATURE_STOPPING=32, /* stopping test */
61: NEP_FEATURE_TWOSIDED=64 /* two-sided variant */
62: } NEPFeatureType;
64: /*
65: Defines the NEP data structure.
66: */
67: struct _p_NEP {
68: PETSCHEADER(struct _NEPOps);
69: /*------------------------- User parameters ---------------------------*/
70: PetscInt max_it; /* maximum number of iterations */
71: PetscInt nev; /* number of eigenvalues to compute */
72: PetscInt ncv; /* number of basis vectors */
73: PetscInt mpd; /* maximum dimension of projected problem */
74: PetscInt nini; /* number of initial vectors (negative means not copied yet) */
75: PetscScalar target; /* target value */
76: PetscReal tol; /* tolerance */
77: NEPConv conv; /* convergence test */
78: NEPStop stop; /* stopping test */
79: NEPWhich which; /* which part of the spectrum to be sought */
80: NEPProblemType problem_type; /* which kind of problem to be solved */
81: NEPRefine refine; /* type of refinement to be applied after solve */
82: PetscInt npart; /* number of partitions of the communicator */
83: PetscReal rtol; /* tolerance for refinement */
84: PetscInt rits; /* number of iterations of the refinement method */
85: NEPRefineScheme scheme; /* scheme for solving linear systems within refinement */
86: PetscBool trackall; /* whether all the residuals must be computed */
87: PetscBool twosided; /* whether to compute left eigenvectors (two-sided solver) */
89: /*-------------- User-provided functions and contexts -----------------*/
90: PetscErrorCode (*computefunction)(NEP,PetscScalar,Mat,Mat,void*);
91: PetscErrorCode (*computejacobian)(NEP,PetscScalar,Mat,void*);
92: void *functionctx;
93: void *jacobianctx;
94: PetscErrorCode (*converged)(NEP,PetscScalar,PetscScalar,PetscReal,PetscReal*,void*);
95: PetscErrorCode (*convergeduser)(NEP,PetscScalar,PetscScalar,PetscReal,PetscReal*,void*);
96: PetscErrorCode (*convergeddestroy)(void*);
97: PetscErrorCode (*stopping)(NEP,PetscInt,PetscInt,PetscInt,PetscInt,NEPConvergedReason*,void*);
98: PetscErrorCode (*stoppinguser)(NEP,PetscInt,PetscInt,PetscInt,PetscInt,NEPConvergedReason*,void*);
99: PetscErrorCode (*stoppingdestroy)(void*);
100: void *convergedctx;
101: void *stoppingctx;
102: PetscErrorCode (*monitor[MAXNEPMONITORS])(NEP,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*);
103: PetscErrorCode (*monitordestroy[MAXNEPMONITORS])(void**);
104: void *monitorcontext[MAXNEPMONITORS];
105: PetscInt numbermonitors;
107: /*----------------- Child objects and working data -------------------*/
108: DS ds; /* direct solver object */
109: BV V; /* set of basis vectors and computed eigenvectors */
110: BV W; /* left basis vectors (if left eigenvectors requested) */
111: RG rg; /* optional region for filtering */
112: SlepcSC sc; /* sorting criterion data */
113: Mat function; /* function matrix */
114: Mat function_pre; /* function matrix (preconditioner) */
115: Mat jacobian; /* Jacobian matrix */
116: Mat *A; /* matrix coefficients of split form */
117: FN *f; /* matrix functions of split form */
118: PetscInt nt; /* number of terms in split form */
119: MatStructure mstr; /* pattern of split matrices */
120: Mat *P; /* matrix coefficients of split form (preconditioner) */
121: MatStructure mstrp; /* pattern of split matrices (preconditioner) */
122: Vec *IS; /* references to user-provided initial space */
123: PetscScalar *eigr,*eigi; /* real and imaginary parts of eigenvalues */
124: PetscReal *errest; /* error estimates */
125: PetscInt *perm; /* permutation for eigenvalue ordering */
126: PetscInt nwork; /* number of work vectors */
127: Vec *work; /* work vectors */
128: KSP refineksp; /* ksp used in refinement */
129: PetscSubcomm refinesubc; /* context for sub-communicators */
130: void *data; /* placeholder for solver-specific stuff */
132: /* ----------------------- Status variables --------------------------*/
133: NEPStateType state; /* initial -> setup -> solved -> eigenvectors */
134: PetscInt nconv; /* number of converged eigenvalues */
135: PetscInt its; /* number of iterations so far computed */
136: PetscInt n,nloc; /* problem dimensions (global, local) */
137: PetscReal *nrma; /* computed matrix norms */
138: NEPUserInterface fui; /* how the user has defined the nonlinear operator */
139: PetscBool useds; /* whether the solver uses the DS object or not */
140: Mat resolvent; /* shell matrix to be used in NEPApplyResolvent */
141: NEPConvergedReason reason;
142: };
144: /*
145: Macros to test valid NEP arguments
146: */
147: #if !defined(PETSC_USE_DEBUG)
149: #define NEPCheckProblem(h,arg) do {(void)(h);} while (0)
150: #define NEPCheckCallback(h,arg) do {(void)(h);} while (0)
151: #define NEPCheckSplit(h,arg) do {(void)(h);} while (0)
152: #define NEPCheckDerivatives(h,arg) do {(void)(h);} while (0)
153: #define NEPCheckSolved(h,arg) do {(void)(h);} while (0)
155: #else
157: #define NEPCheckProblem(h,arg) \
158: do { \
160: } while (0)
162: #define NEPCheckCallback(h,arg) \
163: do { \
165: } while (0)
167: #define NEPCheckSplit(h,arg) \
168: do { \
170: } while (0)
172: #define NEPCheckSolved(h,arg) \
173: do { \
175: } while (0)
177: #endif
179: /* Check for unsupported features */
180: #define NEPCheckUnsupportedCondition(nep,mask,condition,msg) \
181: do { \
182: if (condition) { \
184: if ((mask) & NEP_FEATURE_REGION) { \
185: PetscBool __istrivial; \
186: RGIsTrivial((nep)->rg,&__istrivial); \
188: } \
192: } \
193: } while (0)
194: #define NEPCheckUnsupported(nep,mask) NEPCheckUnsupportedCondition(nep,mask,PETSC_TRUE,"")
196: /* Check for ignored features */
197: #define NEPCheckIgnoredCondition(nep,mask,condition,msg) \
198: do { \
199: if (condition) { \
200: if (((mask) & NEP_FEATURE_CALLBACK) && (nep)->fui==NEP_USER_INTERFACE_CALLBACK) PetscInfo((nep),"The solver '%s'%s ignores the user interface settings\n",((PetscObject)(nep))->type_name,(msg)); \
201: if ((mask) & NEP_FEATURE_REGION) { \
202: PetscBool __istrivial; \
203: RGIsTrivial((nep)->rg,&__istrivial); \
204: if (!__istrivial) PetscInfo((nep),"The solver '%s'%s ignores the specified region\n",((PetscObject)(nep))->type_name,(msg)); \
205: } \
206: if (((mask) & NEP_FEATURE_CONVERGENCE) && (nep)->converged!=NEPConvergedRelative) PetscInfo((nep),"The solver '%s'%s ignores the convergence test settings\n",((PetscObject)(nep))->type_name,(msg)); \
207: if (((mask) & NEP_FEATURE_STOPPING) && (nep)->stopping!=NEPStoppingBasic) PetscInfo((nep),"The solver '%s'%s ignores the stopping test settings\n",((PetscObject)(nep))->type_name,(msg)); \
208: if (((mask) & NEP_FEATURE_TWOSIDED) && (nep)->twosided) PetscInfo((nep),"The solver '%s'%s ignores the two-sided flag\n",((PetscObject)(nep))->type_name,(msg)); \
209: } \
210: } while (0)
211: #define NEPCheckIgnored(nep,mask) NEPCheckIgnoredCondition(nep,mask,PETSC_TRUE,"")
213: /*
214: NEP_KSPSetOperators - Sets the KSP matrices
215: */
216: static inline PetscErrorCode NEP_KSPSetOperators(KSP ksp,Mat A,Mat B)
217: {
218: const char *prefix;
220: KSPSetOperators(ksp,A,B);
221: MatGetOptionsPrefix(B,&prefix);
222: if (!prefix) {
223: /* set Mat prefix to be the same as KSP to enable setting command-line options (e.g. MUMPS)
224: only applies if the Mat has no user-defined prefix */
225: KSPGetOptionsPrefix(ksp,&prefix);
226: MatSetOptionsPrefix(B,prefix);
227: }
228: PetscFunctionReturn(0);
229: }
231: SLEPC_INTERN PetscErrorCode NEPSetDimensions_Default(NEP,PetscInt,PetscInt*,PetscInt*);
232: SLEPC_INTERN PetscErrorCode NEPComputeVectors(NEP);
233: SLEPC_INTERN PetscErrorCode NEPReset_Problem(NEP);
234: SLEPC_INTERN PetscErrorCode NEPGetDefaultShift(NEP,PetscScalar*);
235: SLEPC_INTERN PetscErrorCode NEPComputeVectors_Schur(NEP);
236: SLEPC_INTERN PetscErrorCode NEPComputeResidualNorm_Private(NEP,PetscBool,PetscScalar,Vec,Vec*,PetscReal*);
237: SLEPC_INTERN PetscErrorCode NEPNewtonRefinementSimple(NEP,PetscInt*,PetscReal,PetscInt);
239: #endif