Actual source code: tseig.c


  2: #include <petsc/private/tsimpl.h>
  3: #include <petscdraw.h>

  5: /* ------------------------------------------------------------------------*/
  6: struct _n_TSMonitorSPEigCtx {
  7:   PetscDrawSP drawsp;
  8:   KSP         ksp;
  9:   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
 10:   PetscBool   computeexplicitly;
 11:   MPI_Comm    comm;
 12:   PetscRandom rand;
 13:   PetscReal   xmin,xmax,ymin,ymax;
 14: };

 16: /*@C
 17:    TSMonitorSPEigCtxCreate - Creates a context for use with TS to monitor the eigenvalues of the linearized operator

 19:    Collective on TS

 21:    Input Parameters:
 22: +  host - the X display to open, or null for the local machine
 23: .  label - the title to put in the title bar
 24: .  x, y - the screen coordinates of the upper left coordinate of the window
 25: .  m, n - the screen width and height in pixels
 26: -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time

 28:    Output Parameter:
 29: .  ctx - the context

 31:    Options Database Key:
 32: .  -ts_monitor_sp_eig - plot egienvalues of linearized right hand side

 34:    Notes:
 35:    Use TSMonitorSPEigCtxDestroy() to destroy.

 37:    Currently only works if the Jacobian is provided explicitly.

 39:    Currently only works for ODEs u_t - F(t,u) = 0; that is with no mass matrix.

 41:    Level: intermediate

 43: .seealso: TSMonitorSPEigTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()

 45: @*/
 46: PetscErrorCode  TSMonitorSPEigCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPEigCtx *ctx)
 47: {
 48:   PetscDraw      win;
 49:   PC             pc;

 51:   PetscNew(ctx);
 52:   PetscRandomCreate(comm,&(*ctx)->rand);
 53:   PetscRandomSetFromOptions((*ctx)->rand);
 54:   PetscDrawCreate(comm,host,label,x,y,m,n,&win);
 55:   PetscDrawSetFromOptions(win);
 56:   PetscDrawSPCreate(win,1,&(*ctx)->drawsp);
 57:   KSPCreate(comm,&(*ctx)->ksp);
 58:   KSPSetOptionsPrefix((*ctx)->ksp,"ts_monitor_sp_eig_"); /* this is wrong, used use also prefix from the TS */
 59:   KSPSetType((*ctx)->ksp,KSPGMRES);
 60:   KSPGMRESSetRestart((*ctx)->ksp,200);
 61:   KSPSetTolerances((*ctx)->ksp,1.e-10,PETSC_DEFAULT,PETSC_DEFAULT,200);
 62:   KSPSetComputeSingularValues((*ctx)->ksp,PETSC_TRUE);
 63:   KSPSetFromOptions((*ctx)->ksp);
 64:   KSPGetPC((*ctx)->ksp,&pc);
 65:   PCSetType(pc,PCNONE);

 67:   (*ctx)->howoften          = howoften;
 68:   (*ctx)->computeexplicitly = PETSC_FALSE;

 70:   PetscOptionsGetBool(NULL,NULL,"-ts_monitor_sp_eig_explicitly",&(*ctx)->computeexplicitly,NULL);

 72:   (*ctx)->comm = comm;
 73:   (*ctx)->xmin = -2.1;
 74:   (*ctx)->xmax = 1.1;
 75:   (*ctx)->ymin = -1.1;
 76:   (*ctx)->ymax = 1.1;
 77:   return 0;
 78: }

 80: static PetscErrorCode TSLinearStabilityIndicator(TS ts, PetscReal xr,PetscReal xi,PetscBool *flg)
 81: {
 82:   PetscReal      yr,yi;

 84:   TSComputeLinearStability(ts,xr,xi,&yr,&yi);
 85:   if ((yr*yr + yi*yi) <= 1.0) *flg = PETSC_TRUE;
 86:   else *flg = PETSC_FALSE;
 87:   return 0;
 88: }

 90: PetscErrorCode TSMonitorSPEig(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
 91: {
 92:   TSMonitorSPEigCtx ctx = (TSMonitorSPEigCtx) monctx;
 93:   KSP               ksp = ctx->ksp;
 94:   PetscInt          n,N,nits,neig,i,its = 200;
 95:   PetscReal         *r,*c,time_step_save;
 96:   PetscDrawSP       drawsp = ctx->drawsp;
 97:   Mat               A,B;
 98:   Vec               xdot;
 99:   SNES              snes;

101:   if (step < 0) return 0; /* -1 indicates interpolated solution */
102:   if (!step) return 0;
103:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
104:     VecDuplicate(v,&xdot);
105:     TSGetSNES(ts,&snes);
106:     SNESGetJacobian(snes,&A,&B,NULL,NULL);
107:     MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&B);
108:     /*
109:        This doesn't work because methods keep and use internal information about the shift so it
110:        seems we would need code for each method to trick the correct Jacobian in being computed.
111:      */
112:     time_step_save = ts->time_step;
113:     ts->time_step  = PETSC_MAX_REAL;

115:     SNESComputeJacobian(snes,v,A,B);

117:     ts->time_step  = time_step_save;

119:     KSPSetOperators(ksp,B,B);
120:     VecGetSize(v,&n);
121:     if (n < 200) its = n;
122:     KSPSetTolerances(ksp,1.e-10,PETSC_DEFAULT,PETSC_DEFAULT,its);
123:     VecSetRandom(xdot,ctx->rand);
124:     KSPSolve(ksp,xdot,xdot);
125:     VecDestroy(&xdot);
126:     KSPGetIterationNumber(ksp,&nits);
127:     N    = nits+2;

129:     if (nits) {
130:       PetscDraw     draw;
131:       PetscReal     pause;
132:       PetscDrawAxis axis;
133:       PetscReal     xmin,xmax,ymin,ymax;

135:       PetscDrawSPReset(drawsp);
136:       PetscDrawSPSetLimits(drawsp,ctx->xmin,ctx->xmax,ctx->ymin,ctx->ymax);
137:       PetscMalloc2(PetscMax(n,N),&r,PetscMax(n,N),&c);
138:       if (ctx->computeexplicitly) {
139:         KSPComputeEigenvaluesExplicitly(ksp,n,r,c);
140:         neig = n;
141:       } else {
142:         KSPComputeEigenvalues(ksp,N,r,c,&neig);
143:       }
144:       /* We used the positive operator to be able to reuse KSPs that require positive definiteness, now flip the spectrum as is conventional for ODEs */
145:       for (i=0; i<neig; i++) r[i] = -r[i];
146:       for (i=0; i<neig; i++) {
147:         if (ts->ops->linearstability) {
148:           PetscReal fr,fi;
149:           TSComputeLinearStability(ts,r[i],c[i],&fr,&fi);
150:           if ((fr*fr + fi*fi) > 1.0) {
151:             PetscPrintf(ctx->comm,"Linearized Eigenvalue %g + %g i linear stability function %g norm indicates unstable scheme \n",(double)r[i],(double)c[i],(double)(fr*fr + fi*fi));
152:           }
153:         }
154:         PetscDrawSPAddPoint(drawsp,r+i,c+i);
155:       }
156:       PetscFree2(r,c);
157:       PetscDrawSPGetDraw(drawsp,&draw);
158:       PetscDrawGetPause(draw,&pause);
159:       PetscDrawSetPause(draw,0.0);
160:       PetscDrawSPDraw(drawsp,PETSC_TRUE);
161:       PetscDrawSetPause(draw,pause);
162:       if (ts->ops->linearstability) {
163:         PetscDrawSPGetAxis(drawsp,&axis);
164:         PetscDrawAxisGetLimits(axis,&xmin,&xmax,&ymin,&ymax);
165:         PetscDrawIndicatorFunction(draw,xmin,xmax,ymin,ymax,PETSC_DRAW_CYAN,(PetscErrorCode (*)(void*,PetscReal,PetscReal,PetscBool*))TSLinearStabilityIndicator,ts);
166:         PetscDrawSPDraw(drawsp,PETSC_FALSE);
167:       }
168:       PetscDrawSPSave(drawsp);
169:     }
170:     MatDestroy(&B);
171:   }
172:   return 0;
173: }

175: /*@C
176:    TSMonitorSPEigCtxDestroy - Destroys a scatter plot context that was created with TSMonitorSPEigCtxCreate().

178:    Collective on TSMonitorSPEigCtx

180:    Input Parameter:
181: .  ctx - the monitor context

183:    Level: intermediate

185: .seealso: TSMonitorSPEigCtxCreate(),  TSMonitorSet(), TSMonitorSPEig();
186: @*/
187: PetscErrorCode  TSMonitorSPEigCtxDestroy(TSMonitorSPEigCtx *ctx)
188: {
189:   PetscDraw      draw;

191:   PetscDrawSPGetDraw((*ctx)->drawsp,&draw);
192:   PetscDrawDestroy(&draw);
193:   PetscDrawSPDestroy(&(*ctx)->drawsp);
194:   KSPDestroy(&(*ctx)->ksp);
195:   PetscRandomDestroy(&(*ctx)->rand);
196:   PetscFree(*ctx);
197:   return 0;
198: }