Actual source code: mfnexpokit.c

slepc-3.15.2 2021-09-20
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, 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:    SLEPc matrix function solver: "expokit"

 13:    Method: Arnoldi method tailored for the matrix exponential

 15:    Algorithm:

 17:        Uses Arnoldi relations to compute exp(t_step*A)*v_last for
 18:        several time steps.

 20:    References:

 22:        [1] R. Sidje, "Expokit: a software package for computing matrix
 23:            exponentials", ACM Trans. Math. Softw. 24(1):130-156, 1998.
 24: */

 26: #include <slepc/private/mfnimpl.h>

 28: PetscErrorCode MFNSetUp_Expokit(MFN mfn)
 29: {
 31:   PetscInt       N;
 32:   PetscBool      isexp;

 35:   MatGetSize(mfn->A,&N,NULL);
 36:   if (mfn->ncv==PETSC_DEFAULT) mfn->ncv = PetscMin(30,N);
 37:   if (mfn->max_it==PETSC_DEFAULT) mfn->max_it = 100;
 38:   MFNAllocateSolution(mfn,2);

 40:   PetscObjectTypeCompare((PetscObject)mfn->fn,FNEXP,&isexp);
 41:   if (!isexp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This solver only supports the exponential function");
 42:   return(0);
 43: }

 45: PetscErrorCode MFNSolve_Expokit(MFN mfn,Vec b,Vec x)
 46: {
 47:   PetscErrorCode    ierr;
 48:   PetscInt          mxstep,mxrej,m,mb,ld,i,j,ireject,mx,k1;
 49:   Vec               v,r;
 50:   Mat               M=NULL,K=NULL;
 51:   FN                fn;
 52:   PetscScalar       *H,*B,*F,*betaF,t,sgn,sfactor;
 53:   const PetscScalar *pK;
 54:   PetscReal         anorm,avnorm,tol,err_loc,rndoff,t_out,t_new,t_now,t_step;
 55:   PetscReal         xm,fact,s,p1,p2,beta,beta2,gamma,delta;
 56:   PetscBool         breakdown;

 59:   m   = mfn->ncv;
 60:   tol = mfn->tol;
 61:   mxstep = mfn->max_it;
 62:   mxrej = 10;
 63:   gamma = 0.9;
 64:   delta = 1.2;
 65:   mb    = m;
 66:   FNGetScale(mfn->fn,&t,&sfactor);
 67:   FNDuplicate(mfn->fn,PetscObjectComm((PetscObject)mfn->fn),&fn);
 68:   FNSetScale(fn,1.0,1.0);
 69:   t_out = PetscAbsScalar(t);
 70:   t_now = 0.0;
 71:   MatNorm(mfn->A,NORM_INFINITY,&anorm);
 72:   rndoff = anorm*PETSC_MACHINE_EPSILON;

 74:   k1 = 2;
 75:   xm = 1.0/(PetscReal)m;
 76:   beta = mfn->bnorm;
 77:   fact = PetscPowRealInt((m+1)/2.72,m+1)*PetscSqrtReal(2.0*PETSC_PI*(m+1));
 78:   t_new = (1.0/anorm)*PetscPowReal((fact*tol)/(4.0*beta*anorm),xm);
 79:   s = PetscPowReal(10.0,PetscFloorReal(PetscLog10Real(t_new))-1);
 80:   t_new = PetscCeilReal(t_new/s)*s;
 81:   sgn = t/PetscAbsScalar(t);

 83:   VecCopy(b,x);
 84:   ld = m+2;
 85:   PetscCalloc3(m+1,&betaF,ld*ld,&H,ld*ld,&B);

 87:   while (mfn->reason == MFN_CONVERGED_ITERATING) {
 88:     mfn->its++;
 89:     if (PetscIsInfOrNanReal(t_new)) t_new = PETSC_MAX_REAL;
 90:     t_step = PetscMin(t_out-t_now,t_new);
 91:     BVInsertVec(mfn->V,0,x);
 92:     BVScaleColumn(mfn->V,0,1.0/beta);
 93:     BVMatArnoldi(mfn->V,mfn->transpose_solve?mfn->AT:mfn->A,H,ld,0,&mb,&beta2,&breakdown);
 94:     if (breakdown) {
 95:       k1 = 0;
 96:       t_step = t_out-t_now;
 97:     }
 98:     if (k1!=0) {
 99:       H[m+1+ld*m] = 1.0;
100:       BVGetColumn(mfn->V,m,&v);
101:       BVGetColumn(mfn->V,m+1,&r);
102:       MatMult(mfn->transpose_solve?mfn->AT:mfn->A,v,r);
103:       BVRestoreColumn(mfn->V,m,&v);
104:       BVRestoreColumn(mfn->V,m+1,&r);
105:       BVNormColumn(mfn->V,m+1,NORM_2,&avnorm);
106:     }
107:     PetscArraycpy(B,H,ld*ld);

109:     ireject = 0;
110:     while (ireject <= mxrej) {
111:       mx = mb + k1;
112:       for (i=0;i<mx;i++) {
113:         for (j=0;j<mx;j++) {
114:           H[i+j*ld] = sgn*B[i+j*ld]*t_step;
115:         }
116:       }
117:       MFN_CreateDenseMat(mx,&M);
118:       MFN_CreateDenseMat(mx,&K);
119:       MatDenseGetArray(M,&F);
120:       for (j=0;j<mx;j++) {
121:         PetscArraycpy(F+j*mx,H+j*ld,mx);
122:       }
123:       MatDenseRestoreArray(M,&F);
124:       FNEvaluateFunctionMat(fn,M,K);

126:       if (k1==0) {
127:         err_loc = tol;
128:         break;
129:       } else {
130:         MatDenseGetArrayRead(K,&pK);
131:         p1 = PetscAbsScalar(beta*pK[m]);
132:         p2 = PetscAbsScalar(beta*pK[m+1]*avnorm);
133:         MatDenseRestoreArrayRead(K,&pK);
134:         if (p1 > 10*p2) {
135:           err_loc = p2;
136:           xm = 1.0/(PetscReal)m;
137:         } else if (p1 > p2) {
138:           err_loc = (p1*p2)/(p1-p2);
139:           xm = 1.0/(PetscReal)m;
140:         } else {
141:           err_loc = p1;
142:           xm = 1.0/(PetscReal)(m-1);
143:         }
144:       }
145:       if (err_loc <= delta*t_step*tol) break;
146:       else {
147:         t_step = gamma*t_step*PetscPowReal(t_step*tol/err_loc,xm);
148:         s = PetscPowReal(10.0,PetscFloorReal(PetscLog10Real(t_step))-1);
149:         t_step = PetscCeilReal(t_step/s)*s;
150:         ireject = ireject+1;
151:       }
152:     }

154:     mx = mb + PetscMax(0,k1-1);
155:     MatDenseGetArrayRead(K,&pK);
156:     for (j=0;j<mx;j++) betaF[j] = beta*pK[j];
157:     MatDenseRestoreArrayRead(K,&pK);
158:     BVSetActiveColumns(mfn->V,0,mx);
159:     BVMultVec(mfn->V,1.0,0.0,x,betaF);
160:     VecNorm(x,NORM_2,&beta);

162:     t_now = t_now+t_step;
163:     if (t_now>=t_out) mfn->reason = MFN_CONVERGED_TOL;
164:     else {
165:       t_new = gamma*t_step*PetscPowReal((t_step*tol)/err_loc,xm);
166:       s = PetscPowReal(10.0,PetscFloorReal(PetscLog10Real(t_new))-1);
167:       t_new = PetscCeilReal(t_new/s)*s;
168:     }
169:     err_loc = PetscMax(err_loc,rndoff);
170:     if (mfn->its==mxstep) mfn->reason = MFN_DIVERGED_ITS;
171:     MFNMonitor(mfn,mfn->its,err_loc);
172:   }
173:   VecScale(x,sfactor);

175:   MatDestroy(&M);
176:   MatDestroy(&K);
177:   FNDestroy(&fn);
178:   PetscFree3(betaF,H,B);
179:   return(0);
180: }

182: SLEPC_EXTERN PetscErrorCode MFNCreate_Expokit(MFN mfn)
183: {
185:   mfn->ops->solve          = MFNSolve_Expokit;
186:   mfn->ops->setup          = MFNSetUp_Expokit;
187:   return(0);
188: }