Actual source code: nepsetup.c

slepc-3.9.1 2018-05-02
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2018, 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: */
 10: /*
 11:    NEP routines related to problem setup
 12: */

 14: #include <slepc/private/nepimpl.h>       /*I "slepcnep.h" I*/

 16: /*@
 17:    NEPSetUp - Sets up all the internal data structures necessary for the
 18:    execution of the NEP solver.

 20:    Collective on NEP

 22:    Input Parameter:
 23: .  nep   - solver context

 25:    Notes:
 26:    This function need not be called explicitly in most cases, since NEPSolve()
 27:    calls it. It can be useful when one wants to measure the set-up time
 28:    separately from the solve time.

 30:    Level: developer

 32: .seealso: NEPCreate(), NEPSolve(), NEPDestroy()
 33: @*/
 34: PetscErrorCode NEPSetUp(NEP nep)
 35: {
 37:   PetscInt       k;
 38:   SlepcSC        sc;
 39:   Mat            T;
 40:   PetscBool      flg;
 41:   KSP            ksp;
 42:   PC             pc;
 43:   PetscMPIInt    size;
 44:   MatSolverType  stype;

 48:   NEPCheckProblem(nep,1);
 49:   if (nep->state) return(0);
 50:   PetscLogEventBegin(NEP_SetUp,nep,0,0,0);

 52:   /* reset the convergence flag from the previous solves */
 53:   nep->reason = NEP_CONVERGED_ITERATING;

 55:   /* set default solver type (NEPSetFromOptions was not called) */
 56:   if (!((PetscObject)nep)->type_name) {
 57:     NEPSetType(nep,NEPRII);
 58:   }
 59:   if (!nep->ds) { NEPGetDS(nep,&nep->ds); }
 60:   if (!nep->rg) { NEPGetRG(nep,&nep->rg); }
 61:   if (!((PetscObject)nep->rg)->type_name) {
 62:     RGSetType(nep->rg,RGINTERVAL);
 63:   }

 65:   /* set problem dimensions */
 66:   switch (nep->fui) {
 67:   case NEP_USER_INTERFACE_CALLBACK:
 68:     NEPGetFunction(nep,&T,NULL,NULL,NULL);
 69:     MatGetSize(T,&nep->n,NULL);
 70:     MatGetLocalSize(T,&nep->nloc,NULL);
 71:     break;
 72:   case NEP_USER_INTERFACE_SPLIT:
 73:     MatDuplicate(nep->A[0],MAT_DO_NOT_COPY_VALUES,&nep->function);
 74:     MatDuplicate(nep->A[0],MAT_DO_NOT_COPY_VALUES,&nep->jacobian);
 75:     PetscLogObjectParent((PetscObject)nep,(PetscObject)nep->function);
 76:     PetscLogObjectParent((PetscObject)nep,(PetscObject)nep->jacobian);
 77:     MatGetSize(nep->A[0],&nep->n,NULL);
 78:     MatGetLocalSize(nep->A[0],&nep->nloc,NULL);
 79:     break;
 80:   case NEP_USER_INTERFACE_DERIVATIVES:
 81:     NEPGetDerivatives(nep,&T,NULL,NULL);
 82:     MatGetSize(T,&nep->n,NULL);
 83:     MatGetLocalSize(T,&nep->nloc,NULL);
 84:     break;
 85:   }

 87:   /* set default problem type */
 88:   if (!nep->problem_type) {
 89:     NEPSetProblemType(nep,NEP_GENERAL);
 90:   }

 92:   /* check consistency of refinement options */
 93:   if (nep->refine) {
 94:     if (nep->fui!=NEP_USER_INTERFACE_SPLIT) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"Iterative refinement only implemented in split form");
 95:     if (!nep->scheme) {  /* set default scheme */
 96:       NEPRefineGetKSP(nep,&ksp);
 97:       KSPGetPC(ksp,&pc);
 98:       PetscObjectTypeCompare((PetscObject)ksp,KSPPREONLY,&flg);
 99:       if (flg) {
100:         PetscObjectTypeCompareAny((PetscObject)pc,&flg,PCLU,PCCHOLESKY,"");
101:       }
102:       nep->scheme = flg? NEP_REFINE_SCHEME_MBE: NEP_REFINE_SCHEME_SCHUR;
103:     }
104:     if (nep->scheme==NEP_REFINE_SCHEME_MBE) {
105:       NEPRefineGetKSP(nep,&ksp);
106:       KSPGetPC(ksp,&pc);
107:       PetscObjectTypeCompare((PetscObject)ksp,KSPPREONLY,&flg);
108:       if (flg) {
109:         PetscObjectTypeCompareAny((PetscObject)pc,&flg,PCLU,PCCHOLESKY,"");
110:       }
111:       if (!flg) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"The MBE scheme for refinement requires a direct solver in KSP");
112:       MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
113:       if (size>1) {   /* currently selected PC is a factorization */
114:         PCFactorGetMatSolverType(pc,&stype);
115:         PetscStrcmp(stype,MATSOLVERPETSC,&flg);
116:         if (flg) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"For Newton refinement, you chose to solve linear systems with a factorization, but in parallel runs you need to select an external package");
117:       }
118:     }
119:     if (nep->scheme==NEP_REFINE_SCHEME_SCHUR) {
120:       if (nep->npart>1) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"The Schur scheme for refinement does not support subcommunicators");
121:     }
122:   }
123:   /* call specific solver setup */
124:   (*nep->ops->setup)(nep);

126:   /* by default, compute eigenvalues close to target */
127:   /* nep->target should contain the initial guess for the eigenvalue */
128:   if (!nep->which) nep->which = NEP_TARGET_MAGNITUDE;

130:   /* set tolerance if not yet set */
131:   if (nep->tol==PETSC_DEFAULT) nep->tol = SLEPC_DEFAULT_TOL;
132:   if (nep->refine) {
133:     if (nep->rtol==PETSC_DEFAULT) nep->rtol = PetscMax(nep->tol/1000,PETSC_MACHINE_EPSILON);
134:     if (nep->rits==PETSC_DEFAULT) nep->rits = (nep->refine==NEP_REFINE_SIMPLE)? 10: 1;
135:   }

137:   /* fill sorting criterion context */
138:   switch (nep->which) {
139:     case NEP_LARGEST_MAGNITUDE:
140:       nep->sc->comparison    = SlepcCompareLargestMagnitude;
141:       nep->sc->comparisonctx = NULL;
142:       break;
143:     case NEP_SMALLEST_MAGNITUDE:
144:       nep->sc->comparison    = SlepcCompareSmallestMagnitude;
145:       nep->sc->comparisonctx = NULL;
146:       break;
147:     case NEP_LARGEST_REAL:
148:       nep->sc->comparison    = SlepcCompareLargestReal;
149:       nep->sc->comparisonctx = NULL;
150:       break;
151:     case NEP_SMALLEST_REAL:
152:       nep->sc->comparison    = SlepcCompareSmallestReal;
153:       nep->sc->comparisonctx = NULL;
154:       break;
155:     case NEP_LARGEST_IMAGINARY:
156:       nep->sc->comparison    = SlepcCompareLargestImaginary;
157:       nep->sc->comparisonctx = NULL;
158:       break;
159:     case NEP_SMALLEST_IMAGINARY:
160:       nep->sc->comparison    = SlepcCompareSmallestImaginary;
161:       nep->sc->comparisonctx = NULL;
162:       break;
163:     case NEP_TARGET_MAGNITUDE:
164:       nep->sc->comparison    = SlepcCompareTargetMagnitude;
165:       nep->sc->comparisonctx = &nep->target;
166:       break;
167:     case NEP_TARGET_REAL:
168:       nep->sc->comparison    = SlepcCompareTargetReal;
169:       nep->sc->comparisonctx = &nep->target;
170:       break;
171:     case NEP_TARGET_IMAGINARY:
172: #if defined(PETSC_USE_COMPLEX)
173:       nep->sc->comparison    = SlepcCompareTargetImaginary;
174:       nep->sc->comparisonctx = &nep->target;
175: #endif
176:       break;
177:     case NEP_ALL:
178:       nep->sc->comparison    = SlepcCompareSmallestReal;
179:       nep->sc->comparisonctx = NULL;
180:       break;
181:     case NEP_WHICH_USER:
182:       break;
183:   }

185:   nep->sc->map    = NULL;
186:   nep->sc->mapobj = NULL;

188:   /* fill sorting criterion for DS */
189:   DSGetSlepcSC(nep->ds,&sc);
190:   sc->comparison    = nep->sc->comparison;
191:   sc->comparisonctx = nep->sc->comparisonctx;
192:   PetscObjectTypeCompare((PetscObject)nep,NEPNLEIGS,&flg);
193:   if (!flg) {
194:     sc->map    = NULL;
195:     sc->mapobj = NULL;
196:   }
197:   if (nep->ncv > nep->n) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"ncv must be the problem size at most");
198:   if (nep->nev > nep->ncv) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"nev bigger than ncv");

200:   /* process initial vectors */
201:   if (nep->nini<0) {
202:     k = -nep->nini;
203:     if (k>nep->ncv) SETERRQ(PetscObjectComm((PetscObject)nep),1,"The number of initial vectors is larger than ncv");
204:     BVInsertVecs(nep->V,0,&k,nep->IS,PETSC_TRUE);
205:     SlepcBasisDestroy_Private(&nep->nini,&nep->IS);
206:     nep->nini = k;
207:   }
208:   PetscLogEventEnd(NEP_SetUp,nep,0,0,0);
209:   nep->state = NEP_STATE_SETUP;
210:   return(0);
211: }

213: /*@C
214:    NEPSetInitialSpace - Specify a basis of vectors that constitute the initial
215:    space, that is, the subspace from which the solver starts to iterate.

217:    Collective on NEP and Vec

219:    Input Parameter:
220: +  nep   - the nonlinear eigensolver context
221: .  n     - number of vectors
222: -  is    - set of basis vectors of the initial space

224:    Notes:
225:    Some solvers start to iterate on a single vector (initial vector). In that case,
226:    the other vectors are ignored.

228:    These vectors do not persist from one NEPSolve() call to the other, so the
229:    initial space should be set every time.

231:    The vectors do not need to be mutually orthonormal, since they are explicitly
232:    orthonormalized internally.

234:    Common usage of this function is when the user can provide a rough approximation
235:    of the wanted eigenspace. Then, convergence may be faster.

237:    Level: intermediate
238: @*/
239: PetscErrorCode NEPSetInitialSpace(NEP nep,PetscInt n,Vec *is)
240: {

246:   if (n<0) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"Argument n cannot be negative");
247:   SlepcBasisReference_Private(n,is,&nep->nini,&nep->IS);
248:   if (n>0) nep->state = NEP_STATE_INITIAL;
249:   return(0);
250: }

252: /*
253:   NEPSetDimensions_Default - Set reasonable values for ncv, mpd if not set
254:   by the user. This is called at setup.
255:  */
256: PetscErrorCode NEPSetDimensions_Default(NEP nep,PetscInt nev,PetscInt *ncv,PetscInt *mpd)
257: {
259:   if (*ncv) { /* ncv set */
260:     if (*ncv<nev) SETERRQ(PetscObjectComm((PetscObject)nep),1,"The value of ncv must be at least nev");
261:   } else if (*mpd) { /* mpd set */
262:     *ncv = PetscMin(nep->n,nev+(*mpd));
263:   } else { /* neither set: defaults depend on nev being small or large */
264:     if (nev<500) *ncv = PetscMin(nep->n,PetscMax(2*nev,nev+15));
265:     else {
266:       *mpd = 500;
267:       *ncv = PetscMin(nep->n,nev+(*mpd));
268:     }
269:   }
270:   if (!*mpd) *mpd = *ncv;
271:   return(0);
272: }

274: /*@
275:    NEPAllocateSolution - Allocate memory storage for common variables such
276:    as eigenvalues and eigenvectors.

278:    Collective on NEP

280:    Input Parameters:
281: +  nep   - eigensolver context
282: -  extra - number of additional positions, used for methods that require a
283:            working basis slightly larger than ncv

285:    Developers Note:
286:    This is PETSC_EXTERN because it may be required by user plugin NEP
287:    implementations.

289:    Level: developer
290: @*/
291: PetscErrorCode NEPAllocateSolution(NEP nep,PetscInt extra)
292: {
294:   PetscInt       oldsize,newc,requested;
295:   PetscLogDouble cnt;
296:   Mat            T;
297:   Vec            t;

300:   requested = nep->ncv + extra;

302:   /* oldsize is zero if this is the first time setup is called */
303:   BVGetSizes(nep->V,NULL,NULL,&oldsize);
304:   newc = PetscMax(0,requested-oldsize);

306:   /* allocate space for eigenvalues and friends */
307:   if (requested != oldsize || !nep->eigr) {
308:     PetscFree4(nep->eigr,nep->eigi,nep->errest,nep->perm);
309:     PetscMalloc4(requested,&nep->eigr,requested,&nep->eigi,requested,&nep->errest,requested,&nep->perm);
310:     cnt = newc*sizeof(PetscScalar) + newc*sizeof(PetscReal) + newc*sizeof(PetscInt);
311:     PetscLogObjectMemory((PetscObject)nep,cnt);
312:   }

314:   /* allocate V */
315:   if (!nep->V) { NEPGetBV(nep,&nep->V); }
316:   if (!oldsize) {
317:     if (!((PetscObject)(nep->V))->type_name) {
318:       BVSetType(nep->V,BVSVEC);
319:     }
320:     if (nep->fui==NEP_USER_INTERFACE_SPLIT) T = nep->A[0];
321:     else {
322:       NEPGetFunction(nep,&T,NULL,NULL,NULL);
323:     }
324:     MatCreateVecsEmpty(T,&t,NULL);
325:     BVSetSizesFromVec(nep->V,t,requested);
326:     VecDestroy(&t);
327:   } else {
328:     BVResize(nep->V,requested,PETSC_FALSE);
329:   }
330:   return(0);
331: }