Actual source code: pepsolve.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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:    PEP routines related to the solution process
 12: */

 14: #include <slepc/private/pepimpl.h>       /*I "slepcpep.h" I*/
 15: #include <slepc/private/bvimpl.h>        /*I "slepcbv.h" I*/
 16: #include <petscdraw.h>

 18: static PetscBool  cited = PETSC_FALSE;
 19: static const char citation[] =
 20:   "@Article{slepc-pep-refine,\n"
 21:   "   author = \"C. Campos and J. E. Roman\",\n"
 22:   "   title = \"Parallel iterative refinement in polynomial eigenvalue problems\",\n"
 23:   "   journal = \"Numer. Linear Algebra Appl.\",\n"
 24:   "   volume = \"23\",\n"
 25:   "   number = \"4\",\n"
 26:   "   pages = \"730--745\",\n"
 27:   "   year = \"2016,\"\n"
 28:   "   doi = \"https://doi.org/10.1002/nla.2052\"\n"
 29:   "}\n";

 31: PetscErrorCode PEPComputeVectors(PEP pep)
 32: {

 36:   PEPCheckSolved(pep,1);
 37:   if (pep->state==PEP_STATE_SOLVED && pep->ops->computevectors) {
 38:     (*pep->ops->computevectors)(pep);
 39:   }
 40:   pep->state = PEP_STATE_EIGENVECTORS;
 41:   return(0);
 42: }

 44: PetscErrorCode PEPExtractVectors(PEP pep)
 45: {

 49:   PEPCheckSolved(pep,1);
 50:   if (pep->state==PEP_STATE_SOLVED && pep->ops->extractvectors) {
 51:     (*pep->ops->extractvectors)(pep);
 52:   }
 53:   return(0);
 54: }

 56: /*@
 57:    PEPSolve - Solves the polynomial eigensystem.

 59:    Collective on PEP

 61:    Input Parameter:
 62: .  pep - eigensolver context obtained from PEPCreate()

 64:    Options Database Keys:
 65: +  -pep_view - print information about the solver used
 66: .  -pep_view_matk binary - save any of the coefficient matrices (Ak) to the
 67:                 default binary viewer (replace k by an integer from 0 to nmat-1)
 68: .  -pep_view_vectors binary - save the computed eigenvectors to the default binary viewer
 69: .  -pep_view_values - print computed eigenvalues
 70: .  -pep_converged_reason - print reason for convergence, and number of iterations
 71: .  -pep_error_absolute - print absolute errors of each eigenpair
 72: .  -pep_error_relative - print relative errors of each eigenpair
 73: -  -pep_error_backward - print backward errors of each eigenpair

 75:    Level: beginner

 77: .seealso: PEPCreate(), PEPSetUp(), PEPDestroy(), PEPSetTolerances()
 78: @*/
 79: PetscErrorCode PEPSolve(PEP pep)
 80: {
 82:   PetscInt       i,k;
 83:   PetscBool      flg,islinear;
 84: #define OPTLEN 16
 85:   char           str[OPTLEN];

 89:   if (pep->state>=PEP_STATE_SOLVED) return(0);
 90:   PetscLogEventBegin(PEP_Solve,pep,0,0,0);

 92:   /* call setup */
 93:   PEPSetUp(pep);
 94:   pep->nconv = 0;
 95:   pep->its   = 0;
 96:   k = pep->lineariz? pep->ncv: pep->ncv*(pep->nmat-1);
 97:   for (i=0;i<k;i++) {
 98:     pep->eigr[i]   = 0.0;
 99:     pep->eigi[i]   = 0.0;
100:     pep->errest[i] = 0.0;
101:     pep->perm[i]   = i;
102:   }
103:   PEPViewFromOptions(pep,NULL,"-pep_view_pre");
104:   RGViewFromOptions(pep->rg,NULL,"-rg_view");

106:   (*pep->ops->solve)(pep);

108:   if (!pep->reason) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");

110:   PetscObjectTypeCompare((PetscObject)pep,PEPLINEAR,&islinear);
111:   if (!islinear) {
112:     STPostSolve(pep->st);
113:     /* Map eigenvalues back to the original problem */
114:     STGetTransform(pep->st,&flg);
115:     if (flg && pep->ops->backtransform) {
116:       (*pep->ops->backtransform)(pep);
117:     }
118:   }

120:   pep->state = PEP_STATE_SOLVED;

122: #if !defined(PETSC_USE_COMPLEX)
123:   /* reorder conjugate eigenvalues (positive imaginary first) */
124:   for (i=0;i<pep->nconv-1;i++) {
125:     if (pep->eigi[i] != 0) {
126:       if (pep->eigi[i] < 0) {
127:         pep->eigi[i] = -pep->eigi[i];
128:         pep->eigi[i+1] = -pep->eigi[i+1];
129:         /* the next correction only works with eigenvectors */
130:         PEPComputeVectors(pep);
131:         BVScaleColumn(pep->V,i+1,-1.0);
132:       }
133:       i++;
134:     }
135:   }
136: #endif

138:   if (pep->refine!=PEP_REFINE_NONE) {
139:     PetscCitationsRegister(citation,&cited);
140:   }

142:   if (pep->refine==PEP_REFINE_SIMPLE && pep->rits>0 && pep->nconv>0) {
143:     PEPComputeVectors(pep);
144:     PEPNewtonRefinementSimple(pep,&pep->rits,pep->rtol,pep->nconv);
145:   }

147:   /* sort eigenvalues according to pep->which parameter */
148:   SlepcSortEigenvalues(pep->sc,pep->nconv,pep->eigr,pep->eigi,pep->perm);
149:   PetscLogEventEnd(PEP_Solve,pep,0,0,0);

151:   /* various viewers */
152:   PEPViewFromOptions(pep,NULL,"-pep_view");
153:   PEPReasonViewFromOptions(pep);
154:   PEPErrorViewFromOptions(pep);
155:   PEPValuesViewFromOptions(pep);
156:   PEPVectorsViewFromOptions(pep);
157:   for (i=0;i<pep->nmat;i++) {
158:     PetscSNPrintf(str,OPTLEN,"-pep_view_mat%d",(int)i);
159:     MatViewFromOptions(pep->A[i],(PetscObject)pep,str);
160:   }

162:   /* Remove the initial subspace */
163:   pep->nini = 0;
164:   return(0);
165: }

167: /*@
168:    PEPGetIterationNumber - Gets the current iteration number. If the
169:    call to PEPSolve() is complete, then it returns the number of iterations
170:    carried out by the solution method.

172:    Not Collective

174:    Input Parameter:
175: .  pep - the polynomial eigensolver context

177:    Output Parameter:
178: .  its - number of iterations

180:    Note:
181:    During the i-th iteration this call returns i-1. If PEPSolve() is
182:    complete, then parameter "its" contains either the iteration number at
183:    which convergence was successfully reached, or failure was detected.
184:    Call PEPGetConvergedReason() to determine if the solver converged or
185:    failed and why.

187:    Level: intermediate

189: .seealso: PEPGetConvergedReason(), PEPSetTolerances()
190: @*/
191: PetscErrorCode PEPGetIterationNumber(PEP pep,PetscInt *its)
192: {
196:   *its = pep->its;
197:   return(0);
198: }

200: /*@
201:    PEPGetConverged - Gets the number of converged eigenpairs.

203:    Not Collective

205:    Input Parameter:
206: .  pep - the polynomial eigensolver context

208:    Output Parameter:
209: .  nconv - number of converged eigenpairs

211:    Note:
212:    This function should be called after PEPSolve() has finished.

214:    Level: beginner

216: .seealso: PEPSetDimensions(), PEPSolve()
217: @*/
218: PetscErrorCode PEPGetConverged(PEP pep,PetscInt *nconv)
219: {
223:   PEPCheckSolved(pep,1);
224:   *nconv = pep->nconv;
225:   return(0);
226: }

228: /*@
229:    PEPGetConvergedReason - Gets the reason why the PEPSolve() iteration was
230:    stopped.

232:    Not Collective

234:    Input Parameter:
235: .  pep - the polynomial eigensolver context

237:    Output Parameter:
238: .  reason - negative value indicates diverged, positive value converged

240:    Notes:

242:    Possible values for reason are
243: +  PEP_CONVERGED_TOL - converged up to tolerance
244: .  PEP_CONVERGED_USER - converged due to a user-defined condition
245: .  PEP_DIVERGED_ITS - required more than max_it iterations to reach convergence
246: .  PEP_DIVERGED_BREAKDOWN - generic breakdown in method
247: -  PEP_DIVERGED_SYMMETRY_LOST - pseudo-Lanczos was not able to keep symmetry

249:    Can only be called after the call to PEPSolve() is complete.

251:    Level: intermediate

253: .seealso: PEPSetTolerances(), PEPSolve(), PEPConvergedReason
254: @*/
255: PetscErrorCode PEPGetConvergedReason(PEP pep,PEPConvergedReason *reason)
256: {
260:   PEPCheckSolved(pep,1);
261:   *reason = pep->reason;
262:   return(0);
263: }

265: /*@C
266:    PEPGetEigenpair - Gets the i-th solution of the eigenproblem as computed by
267:    PEPSolve(). The solution consists in both the eigenvalue and the eigenvector.

269:    Logically Collective on EPS

271:    Input Parameters:
272: +  pep - polynomial eigensolver context
273: -  i   - index of the solution

275:    Output Parameters:
276: +  eigr - real part of eigenvalue
277: .  eigi - imaginary part of eigenvalue
278: .  Vr   - real part of eigenvector
279: -  Vi   - imaginary part of eigenvector

281:    Notes:
282:    It is allowed to pass NULL for Vr and Vi, if the eigenvector is not
283:    required. Otherwise, the caller must provide valid Vec objects, i.e.,
284:    they must be created by the calling program with e.g. MatCreateVecs().

286:    If the eigenvalue is real, then eigi and Vi are set to zero. If PETSc is
287:    configured with complex scalars the eigenvalue is stored
288:    directly in eigr (eigi is set to zero) and the eigenvector in Vr (Vi is
289:    set to zero). In any case, the user can pass NULL in Vr or Vi if one of
290:    them is not required.

292:    The index i should be a value between 0 and nconv-1 (see PEPGetConverged()).
293:    Eigenpairs are indexed according to the ordering criterion established
294:    with PEPSetWhichEigenpairs().

296:    Level: beginner

298: .seealso: PEPSolve(), PEPGetConverged(), PEPSetWhichEigenpairs()
299: @*/
300: PetscErrorCode PEPGetEigenpair(PEP pep,PetscInt i,PetscScalar *eigr,PetscScalar *eigi,Vec Vr,Vec Vi)
301: {
302:   PetscInt       k;

310:   PEPCheckSolved(pep,1);
311:   if (i<0 || i>=pep->nconv) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"Argument 2 out of range");

313:   PEPComputeVectors(pep);
314:   k = pep->perm[i];

316:   /* eigenvalue */
317: #if defined(PETSC_USE_COMPLEX)
318:   if (eigr) *eigr = pep->eigr[k];
319:   if (eigi) *eigi = 0;
320: #else
321:   if (eigr) *eigr = pep->eigr[k];
322:   if (eigi) *eigi = pep->eigi[k];
323: #endif

325:   /* eigenvector */
326:   BV_GetEigenvector(pep->V,k,pep->eigi[k],Vr,Vi);
327:   return(0);
328: }

330: /*@
331:    PEPGetErrorEstimate - Returns the error estimate associated to the i-th
332:    computed eigenpair.

334:    Not Collective

336:    Input Parameter:
337: +  pep - polynomial eigensolver context
338: -  i   - index of eigenpair

340:    Output Parameter:
341: .  errest - the error estimate

343:    Notes:
344:    This is the error estimate used internally by the eigensolver. The actual
345:    error bound can be computed with PEPComputeError(). See also the users
346:    manual for details.

348:    Level: advanced

350: .seealso: PEPComputeError()
351: @*/
352: PetscErrorCode PEPGetErrorEstimate(PEP pep,PetscInt i,PetscReal *errest)
353: {
357:   PEPCheckSolved(pep,1);
358:   if (i<0 || i>=pep->nconv) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"Argument 2 out of range");
359:   *errest = pep->errest[pep->perm[i]];
360:   return(0);
361: }

363: /*
364:    PEPComputeResidualNorm_Private - Computes the norm of the residual vector
365:    associated with an eigenpair.

367:    Input Parameters:
368:      kr,ki - eigenvalue
369:      xr,xi - eigenvector
370:      z     - array of 4 work vectors (z[2],z[3] not referenced in complex scalars)
371: */
372: PetscErrorCode PEPComputeResidualNorm_Private(PEP pep,PetscScalar kr,PetscScalar ki,Vec xr,Vec xi,Vec *z,PetscReal *norm)
373: {
375:   Mat            *A=pep->A;
376:   PetscInt       i,nmat=pep->nmat;
377:   PetscScalar    t[20],*vals=t,*ivals=NULL;
378:   Vec            u,w;
379: #if !defined(PETSC_USE_COMPLEX)
380:   Vec            ui,wi;
381:   PetscReal      ni;
382:   PetscBool      imag;
383:   PetscScalar    it[20];
384: #endif

387:   u = z[0]; w = z[1];
388:   VecSet(u,0.0);
389: #if !defined(PETSC_USE_COMPLEX)
390:   ui = z[2]; wi = z[3];
391:   ivals = it;
392: #endif
393:   if (nmat>20) {
394:     PetscMalloc1(nmat,&vals);
395: #if !defined(PETSC_USE_COMPLEX)
396:     PetscMalloc1(nmat,&ivals);
397: #endif
398:   }
399:   PEPEvaluateBasis(pep,kr,ki,vals,ivals);
400: #if !defined(PETSC_USE_COMPLEX)
401:   if (ki == 0 || PetscAbsScalar(ki) < PetscAbsScalar(kr*PETSC_MACHINE_EPSILON))
402:     imag = PETSC_FALSE;
403:   else {
404:     imag = PETSC_TRUE;
405:     VecSet(ui,0.0);
406:   }
407: #endif
408:   for (i=0;i<nmat;i++) {
409:     if (vals[i]!=0.0) {
410:       MatMult(A[i],xr,w);
411:       VecAXPY(u,vals[i],w);
412:     }
413: #if !defined(PETSC_USE_COMPLEX)
414:     if (imag) {
415:       if (ivals[i]!=0 || vals[i]!=0) {
416:         MatMult(A[i],xi,wi);
417:         if (vals[i]==0) {
418:           MatMult(A[i],xr,w);
419:         }
420:       }
421:       if (ivals[i]!=0){
422:         VecAXPY(u,-ivals[i],wi);
423:         VecAXPY(ui,ivals[i],w);
424:       }
425:       if (vals[i]!=0) {
426:         VecAXPY(ui,vals[i],wi);
427:       }
428:     }
429: #endif
430:   }
431:   VecNorm(u,NORM_2,norm);
432: #if !defined(PETSC_USE_COMPLEX)
433:   if (imag) {
434:     VecNorm(ui,NORM_2,&ni);
435:     *norm = SlepcAbsEigenvalue(*norm,ni);
436:   }
437: #endif
438:   if (nmat>20) {
439:     PetscFree(vals);
440: #if !defined(PETSC_USE_COMPLEX)
441:     PetscFree(ivals);
442: #endif
443:   }
444:   return(0);
445: }

447: /*@
448:    PEPComputeError - Computes the error (based on the residual norm) associated
449:    with the i-th computed eigenpair.

451:    Collective on PEP

453:    Input Parameter:
454: +  pep  - the polynomial eigensolver context
455: .  i    - the solution index
456: -  type - the type of error to compute

458:    Output Parameter:
459: .  error - the error

461:    Notes:
462:    The error can be computed in various ways, all of them based on the residual
463:    norm ||P(l)x||_2 where l is the eigenvalue and x is the eigenvector.
464:    See the users guide for additional details.

466:    Level: beginner

468: .seealso: PEPErrorType, PEPSolve(), PEPGetErrorEstimate()
469: @*/
470: PetscErrorCode PEPComputeError(PEP pep,PetscInt i,PEPErrorType type,PetscReal *error)
471: {
473:   Vec            xr,xi,w[4];
474:   PetscScalar    kr,ki;
475:   PetscReal      t,z=0.0;
476:   PetscInt       j;
477:   PetscBool      flg;

484:   PEPCheckSolved(pep,1);

486:   /* allocate work vectors */
487: #if defined(PETSC_USE_COMPLEX)
488:   PEPSetWorkVecs(pep,3);
489:   xi   = NULL;
490:   w[2] = NULL;
491:   w[3] = NULL;
492: #else
493:   PEPSetWorkVecs(pep,6);
494:   xi   = pep->work[3];
495:   w[2] = pep->work[4];
496:   w[3] = pep->work[5];
497: #endif
498:   xr   = pep->work[0];
499:   w[0] = pep->work[1];
500:   w[1] = pep->work[2];

502:   /* compute residual norms */
503:   PEPGetEigenpair(pep,i,&kr,&ki,xr,xi);
504:   PEPComputeResidualNorm_Private(pep,kr,ki,xr,xi,w,error);

506:   /* compute error */
507:   switch (type) {
508:     case PEP_ERROR_ABSOLUTE:
509:       break;
510:     case PEP_ERROR_RELATIVE:
511:       *error /= SlepcAbsEigenvalue(kr,ki);
512:       break;
513:     case PEP_ERROR_BACKWARD:
514:       /* initialization of matrix norms */
515:       if (!pep->nrma[pep->nmat-1]) {
516:         for (j=0;j<pep->nmat;j++) {
517:           MatHasOperation(pep->A[j],MATOP_NORM,&flg);
518:           if (!flg) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_WRONG,"The computation of backward errors requires a matrix norm operation");
519:           MatNorm(pep->A[j],NORM_INFINITY,&pep->nrma[j]);
520:         }
521:       }
522:       t = SlepcAbsEigenvalue(kr,ki);
523:       for (j=pep->nmat-1;j>=0;j--) {
524:         z = z*t+pep->nrma[j];
525:       }
526:       *error /= z;
527:       break;
528:     default:
529:       SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"Invalid error type");
530:   }
531:   return(0);
532: }