Actual source code: ts.c

petsc-master 2017-11-16
Report Typos and Errors
  1:  #include <petsc/private/tsimpl.h>
  2:  #include <petscdmshell.h>
  3:  #include <petscdmda.h>
  4:  #include <petscviewer.h>
  5:  #include <petscdraw.h>

  7: /* Logging support */
  8: PetscClassId  TS_CLASSID, DMTS_CLASSID;
  9: PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;

 11: const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};

 13: struct _n_TSMonitorDrawCtx {
 14:   PetscViewer   viewer;
 15:   Vec           initialsolution;
 16:   PetscBool     showinitial;
 17:   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
 18:   PetscBool     showtimestepandtime;
 19: };

 21: /*@C
 22:    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user

 24:    Collective on TS

 26:    Input Parameters:
 27: +  ts - TS object you wish to monitor
 28: .  name - the monitor type one is seeking
 29: .  help - message indicating what monitoring is done
 30: .  manual - manual page for the monitor
 31: .  monitor - the monitor function
 32: -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects

 34:    Level: developer

 36: .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
 37:           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
 38:           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
 39:           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
 40:           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
 41:           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
 42:           PetscOptionsFList(), PetscOptionsEList()
 43: @*/
 44: PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
 45: {
 46:   PetscErrorCode    ierr;
 47:   PetscViewer       viewer;
 48:   PetscViewerFormat format;
 49:   PetscBool         flg;

 52:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);
 53:   if (flg) {
 54:     PetscViewerAndFormat *vf;
 55:     PetscViewerAndFormatCreate(viewer,format,&vf);
 56:     PetscObjectDereference((PetscObject)viewer);
 57:     if (monitorsetup) {
 58:       (*monitorsetup)(ts,vf);
 59:     }
 60:     TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
 61:   }
 62:   return(0);
 63: }

 65: /*@C
 66:    TSAdjointMonitorSensi - monitors the first lambda sensitivity

 68:    Level: intermediate

 70: .keywords: TS, set, monitor

 72: .seealso: TSAdjointMonitorSet()
 73: @*/
 74: PetscErrorCode TSAdjointMonitorSensi(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
 75: {
 77:   PetscViewer    viewer = vf->viewer;

 81:   PetscViewerPushFormat(viewer,vf->format);
 82:   VecView(lambda[0],viewer);
 83:   PetscViewerPopFormat(viewer);
 84:   return(0);
 85: }

 87: /*@C
 88:    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user

 90:    Collective on TS

 92:    Input Parameters:
 93: +  ts - TS object you wish to monitor
 94: .  name - the monitor type one is seeking
 95: .  help - message indicating what monitoring is done
 96: .  manual - manual page for the monitor
 97: .  monitor - the monitor function
 98: -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects

100:    Level: developer

102: .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
103:           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
104:           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
105:           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
106:           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
107:           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
108:           PetscOptionsFList(), PetscOptionsEList()
109: @*/
110: PetscErrorCode  TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
111: {
112:   PetscErrorCode    ierr;
113:   PetscViewer       viewer;
114:   PetscViewerFormat format;
115:   PetscBool         flg;

118:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);
119:   if (flg) {
120:     PetscViewerAndFormat *vf;
121:     PetscViewerAndFormatCreate(viewer,format,&vf);
122:     PetscObjectDereference((PetscObject)viewer);
123:     if (monitorsetup) {
124:       (*monitorsetup)(ts,vf);
125:     }
126:     TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
127:   }
128:   return(0);
129: }

131: static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
132: {

138:   if (!((PetscObject)adapt)->type_name) {
139:     TSAdaptSetType(adapt,default_type);
140:   }
141:   return(0);
142: }

144: /*@
145:    TSSetFromOptions - Sets various TS parameters from user options.

147:    Collective on TS

149:    Input Parameter:
150: .  ts - the TS context obtained from TSCreate()

152:    Options Database Keys:
153: +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
154: .  -ts_save_trajectory - checkpoint the solution at each time-step
155: .  -ts_max_time <time> - maximum time to compute to
156: .  -ts_max_steps <steps> - maximum number of time-steps to take
157: .  -ts_init_time <time> - initial time to start computation
158: .  -ts_final_time <time> - final time to compute to
159: .  -ts_dt <dt> - initial time step
160: .  -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e
161: .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
162: .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
163: .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
164: .  -ts_rtol <rtol> - relative tolerance for local truncation error
165: .  -ts_atol <atol> Absolute tolerance for local truncation error
166: .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
167: .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
168: .  -ts_monitor - print information at each timestep
169: .  -ts_monitor_lg_solution - Monitor solution graphically
170: .  -ts_monitor_lg_error - Monitor error graphically
171: .  -ts_monitor_lg_timestep - Monitor timestep size graphically
172: .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
173: .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
174: .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
175: .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
176: .  -ts_monitor_draw_solution - Monitor solution graphically
177: .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
178: .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
179: .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
180: .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
181: .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
182: .  -ts_adjoint_monitor - print information at each adjoint time step
183: -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically

185:    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified

187:    Level: beginner

189: .keywords: TS, timestep, set, options, database

191: .seealso: TSGetType()
192: @*/
193: PetscErrorCode  TSSetFromOptions(TS ts)
194: {
195:   PetscBool              opt,flg,tflg;
196:   PetscErrorCode         ierr;
197:   char                   monfilename[PETSC_MAX_PATH_LEN];
198:   PetscReal              time_step;
199:   TSExactFinalTimeOption eftopt;
200:   char                   dir[16];
201:   TSIFunction            ifun;
202:   const char             *defaultType;
203:   char                   typeName[256];


208:   TSRegisterAll();
209:   TSGetIFunction(ts,NULL,&ifun,NULL);

211:   PetscObjectOptionsBegin((PetscObject)ts);
212:   if (((PetscObject)ts)->type_name)
213:     defaultType = ((PetscObject)ts)->type_name;
214:   else
215:     defaultType = ifun ? TSBEULER : TSEULER;
216:   PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);
217:   if (opt) {
218:     TSSetType(ts,typeName);
219:   } else {
220:     TSSetType(ts,defaultType);
221:   }

223:   /* Handle generic TS options */
224:   PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);
225:   PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);
226:   PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);
227:   PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);
228:   PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);
229:   if (flg) {TSSetTimeStep(ts,time_step);}
230:   PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);
231:   if (flg) {TSSetExactFinalTime(ts,eftopt);}
232:   PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);
233:   PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);
234:   PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);
235:   PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);
236:   PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);

238: #if defined(PETSC_HAVE_SAWS)
239:   {
240:   PetscBool set;
241:   flg  = PETSC_FALSE;
242:   PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);
243:   if (set) {
244:     PetscObjectSAWsSetBlock((PetscObject)ts,flg);
245:   }
246:   }
247: #endif

249:   /* Monitor options */
250:   TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);
251:   TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);
252:   TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);
253:   TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor_sensi","Monitor sensitivity in the adjoint computation","TSAdjointMonitorSensi",TSAdjointMonitorSensi,NULL);

255:   PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);
256:   if (flg) {PetscPythonMonitorSet((PetscObject)ts,monfilename);}

258:   PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);
259:   if (opt) {
260:     TSMonitorLGCtx ctx;
261:     PetscInt       howoften = 1;

263:     PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);
264:     TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
265:     TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
266:   }

268:   PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);
269:   if (opt) {
270:     TSMonitorLGCtx ctx;
271:     PetscInt       howoften = 1;

273:     PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);
274:     TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
275:     TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
276:   }

278:   PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);
279:   if (opt) {
280:     TSMonitorLGCtx ctx;
281:     PetscInt       howoften = 1;

283:     PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);
284:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
285:     TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
286:   }
287:   PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);
288:   if (opt) {
289:     TSMonitorLGCtx ctx;
290:     PetscInt       howoften = 1;

292:     PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);
293:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
294:     TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
295:     ctx->semilogy = PETSC_TRUE;
296:   }

298:   PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);
299:   if (opt) {
300:     TSMonitorLGCtx ctx;
301:     PetscInt       howoften = 1;

303:     PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);
304:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
305:     TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
306:   }
307:   PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);
308:   if (opt) {
309:     TSMonitorLGCtx ctx;
310:     PetscInt       howoften = 1;

312:     PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);
313:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
314:     TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
315:   }
316:   PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);
317:   if (opt) {
318:     TSMonitorSPEigCtx ctx;
319:     PetscInt          howoften = 1;

321:     PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);
322:     TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
323:     TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);
324:   }
325:   opt  = PETSC_FALSE;
326:   PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);
327:   if (opt) {
328:     TSMonitorDrawCtx ctx;
329:     PetscInt         howoften = 1;

331:     PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);
332:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
333:     TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
334:   }
335:   opt  = PETSC_FALSE;
336:   PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);
337:   if (opt) {
338:     TSMonitorDrawCtx ctx;
339:     PetscInt         howoften = 1;

341:     PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);
342:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
343:     TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
344:   }
345:   opt  = PETSC_FALSE;
346:   PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);
347:   if (opt) {
348:     TSMonitorDrawCtx ctx;
349:     PetscReal        bounds[4];
350:     PetscInt         n = 4;
351:     PetscDraw        draw;
352:     PetscDrawAxis    axis;

354:     PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);
355:     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
356:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);
357:     PetscViewerDrawGetDraw(ctx->viewer,0,&draw);
358:     PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);
359:     PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);
360:     PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");
361:     TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
362:   }
363:   opt  = PETSC_FALSE;
364:   PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);
365:   if (opt) {
366:     TSMonitorDrawCtx ctx;
367:     PetscInt         howoften = 1;

369:     PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);
370:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
371:     TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
372:   }

374:   opt  = PETSC_FALSE;
375:   PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);
376:   if (flg) {
377:     const char *ptr,*ptr2;
378:     char       *filetemplate;
379:     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
380:     /* Do some cursory validation of the input. */
381:     PetscStrstr(monfilename,"%",(char**)&ptr);
382:     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
383:     for (ptr++; ptr && *ptr; ptr++) {
384:       PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);
385:       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
386:       if (ptr2) break;
387:     }
388:     PetscStrallocpy(monfilename,&filetemplate);
389:     TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);
390:   }

392:   PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);
393:   if (flg) {
394:     TSMonitorDMDARayCtx *rayctx;
395:     int                  ray = 0;
396:     DMDADirection        ddir;
397:     DM                   da;
398:     PetscMPIInt          rank;

400:     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
401:     if (dir[0] == 'x') ddir = DMDA_X;
402:     else if (dir[0] == 'y') ddir = DMDA_Y;
403:     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
404:     sscanf(dir+2,"%d",&ray);

406:     PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);
407:     PetscNew(&rayctx);
408:     TSGetDM(ts,&da);
409:     DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);
410:     MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);
411:     if (!rank) {
412:       PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);
413:     }
414:     rayctx->lgctx = NULL;
415:     TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);
416:   }
417:   PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);
418:   if (flg) {
419:     TSMonitorDMDARayCtx *rayctx;
420:     int                 ray = 0;
421:     DMDADirection       ddir;
422:     DM                  da;
423:     PetscInt            howoften = 1;

425:     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
426:     if      (dir[0] == 'x') ddir = DMDA_X;
427:     else if (dir[0] == 'y') ddir = DMDA_Y;
428:     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
429:     sscanf(dir+2, "%d", &ray);

431:     PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);
432:     PetscNew(&rayctx);
433:     TSGetDM(ts, &da);
434:     DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);
435:     TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);
436:     TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);
437:   }

439:   PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);
440:   if (opt) {
441:     TSMonitorEnvelopeCtx ctx;

443:     TSMonitorEnvelopeCtxCreate(ts,&ctx);
444:     TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);
445:   }

447:   flg  = PETSC_FALSE;
448:   PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);
449:   if (flg) {
450:     DM   dm;
451:     DMTS tdm;

453:     TSGetDM(ts, &dm);
454:     DMGetDMTS(dm, &tdm);
455:     tdm->ijacobianctx = NULL;
456:     TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);
457:     PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");
458:   }

460:   /* Handle specific TS options */
461:   if (ts->ops->setfromoptions) {
462:     (*ts->ops->setfromoptions)(PetscOptionsObject,ts);
463:   }

465:   /* Handle TSAdapt options */
466:   TSGetAdapt(ts,&ts->adapt);
467:   TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);
468:   TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);

470:   /* TS trajectory must be set after TS, since it may use some TS options above */
471:   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
472:   PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);
473:   if (tflg) {
474:     TSSetSaveTrajectory(ts);
475:   }
476:   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
477:   PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);
478:   if (flg) {
479:     TSSetSaveTrajectory(ts);
480:     ts->adjoint_solve = tflg;
481:   }

483:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
484:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);
485:   PetscOptionsEnd();

487:   if (ts->trajectory) {
488:     TSTrajectorySetFromOptions(ts->trajectory,ts);
489:   }

491:   TSGetSNES(ts,&ts->snes);
492:   if (ts->problem_type == TS_LINEAR) {SNESSetType(ts->snes,SNESKSPONLY);}
493:   SNESSetFromOptions(ts->snes);
494:   return(0);
495: }

497: /*@
498:    TSGetTrajectory - Gets the trajectory from a TS if it exists

500:    Collective on TS

502:    Input Parameters:
503: .  ts - the TS context obtained from TSCreate()

505:    Output Parameters;
506: .  tr - the TSTrajectory object, if it exists

508:    Note: This routine should be called after all TS options have been set

510:    Level: advanced

512: .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()

514: .keywords: TS, set, checkpoint,
515: @*/
516: PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
517: {
520:   *tr = ts->trajectory;
521:   return(0);
522: }

524: /*@
525:    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object

527:    Collective on TS

529:    Input Parameters:
530: .  ts - the TS context obtained from TSCreate()

532:    Options Database:
533: +  -ts_save_trajectory - saves the trajectory to a file
534: -  -ts_trajectory_type type

536: Note: This routine should be called after all TS options have been set

538:     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and 
539:    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m

541:    Level: intermediate

543: .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()

545: .keywords: TS, set, checkpoint,
546: @*/
547: PetscErrorCode  TSSetSaveTrajectory(TS ts)
548: {

553:   if (!ts->trajectory) {
554:     TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);
555:   }
556:   return(0);
557: }

559: /*@
560:    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
561:       set with TSSetRHSJacobian().

563:    Collective on TS and Vec

565:    Input Parameters:
566: +  ts - the TS context
567: .  t - current timestep
568: -  U - input vector

570:    Output Parameters:
571: +  A - Jacobian matrix
572: .  B - optional preconditioning matrix
573: -  flag - flag indicating matrix structure

575:    Notes:
576:    Most users should not need to explicitly call this routine, as it
577:    is used internally within the nonlinear solvers.

579:    See KSPSetOperators() for important information about setting the
580:    flag parameter.

582:    Level: developer

584: .keywords: SNES, compute, Jacobian, matrix

586: .seealso:  TSSetRHSJacobian(), KSPSetOperators()
587: @*/
588: PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
589: {
590:   PetscErrorCode   ierr;
591:   PetscObjectState Ustate;
592:   PetscObjectId    Uid;
593:   DM               dm;
594:   DMTS             tsdm;
595:   TSRHSJacobian    rhsjacobianfunc;
596:   void             *ctx;
597:   TSIJacobian      ijacobianfunc;
598:   TSRHSFunction    rhsfunction;

604:   TSGetDM(ts,&dm);
605:   DMGetDMTS(dm,&tsdm);
606:   DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);
607:   DMTSGetIJacobian(dm,&ijacobianfunc,NULL);
608:   DMTSGetRHSFunction(dm,&rhsfunction,&ctx);
609:   PetscObjectStateGet((PetscObject)U,&Ustate);
610:   PetscObjectGetId((PetscObject)U,&Uid);
611:   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
612:     return(0);
613:   }

615:   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");

617:   if (ts->rhsjacobian.reuse) {
618:     MatShift(A,-ts->rhsjacobian.shift);
619:     MatScale(A,1./ts->rhsjacobian.scale);
620:     if (B && A != B) {
621:       MatShift(B,-ts->rhsjacobian.shift);
622:       MatScale(B,1./ts->rhsjacobian.scale);
623:     }
624:     ts->rhsjacobian.shift = 0;
625:     ts->rhsjacobian.scale = 1.;
626:   }

628:   if (rhsjacobianfunc) {
629:     PetscBool missing;
630:     PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);
631:     PetscStackPush("TS user Jacobian function");
632:     (*rhsjacobianfunc)(ts,t,U,A,B,ctx);
633:     PetscStackPop;
634:     PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);
635:     if (A) {
636:       MatMissingDiagonal(A,&missing,NULL);
637:       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
638:     }
639:     if (B && B != A) {
640:       MatMissingDiagonal(B,&missing,NULL);
641:       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
642:     }
643:   } else {
644:     MatZeroEntries(A);
645:     if (A != B) {MatZeroEntries(B);}
646:   }
647:   ts->rhsjacobian.time       = t;
648:   PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);
649:   PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);
650:   return(0);
651: }

653: /*@
654:    TSComputeRHSFunction - Evaluates the right-hand-side function.

656:    Collective on TS and Vec

658:    Input Parameters:
659: +  ts - the TS context
660: .  t - current time
661: -  U - state vector

663:    Output Parameter:
664: .  y - right hand side

666:    Note:
667:    Most users should not need to explicitly call this routine, as it
668:    is used internally within the nonlinear solvers.

670:    Level: developer

672: .keywords: TS, compute

674: .seealso: TSSetRHSFunction(), TSComputeIFunction()
675: @*/
676: PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
677: {
679:   TSRHSFunction  rhsfunction;
680:   TSIFunction    ifunction;
681:   void           *ctx;
682:   DM             dm;

688:   TSGetDM(ts,&dm);
689:   DMTSGetRHSFunction(dm,&rhsfunction,&ctx);
690:   DMTSGetIFunction(dm,&ifunction,NULL);

692:   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");

694:   PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);
695:   if (rhsfunction) {
696:     PetscStackPush("TS user right-hand-side function");
697:     (*rhsfunction)(ts,t,U,y,ctx);
698:     PetscStackPop;
699:   } else {
700:     VecZeroEntries(y);
701:   }

703:   PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);
704:   return(0);
705: }

707: /*@
708:    TSComputeSolutionFunction - Evaluates the solution function.

710:    Collective on TS and Vec

712:    Input Parameters:
713: +  ts - the TS context
714: -  t - current time

716:    Output Parameter:
717: .  U - the solution

719:    Note:
720:    Most users should not need to explicitly call this routine, as it
721:    is used internally within the nonlinear solvers.

723:    Level: developer

725: .keywords: TS, compute

727: .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
728: @*/
729: PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
730: {
731:   PetscErrorCode     ierr;
732:   TSSolutionFunction solutionfunction;
733:   void               *ctx;
734:   DM                 dm;

739:   TSGetDM(ts,&dm);
740:   DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);

742:   if (solutionfunction) {
743:     PetscStackPush("TS user solution function");
744:     (*solutionfunction)(ts,t,U,ctx);
745:     PetscStackPop;
746:   }
747:   return(0);
748: }
749: /*@
750:    TSComputeForcingFunction - Evaluates the forcing function.

752:    Collective on TS and Vec

754:    Input Parameters:
755: +  ts - the TS context
756: -  t - current time

758:    Output Parameter:
759: .  U - the function value

761:    Note:
762:    Most users should not need to explicitly call this routine, as it
763:    is used internally within the nonlinear solvers.

765:    Level: developer

767: .keywords: TS, compute

769: .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
770: @*/
771: PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
772: {
773:   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
774:   void               *ctx;
775:   DM                 dm;

780:   TSGetDM(ts,&dm);
781:   DMTSGetForcingFunction(dm,&forcing,&ctx);

783:   if (forcing) {
784:     PetscStackPush("TS user forcing function");
785:     (*forcing)(ts,t,U,ctx);
786:     PetscStackPop;
787:   }
788:   return(0);
789: }

791: static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
792: {
793:   Vec            F;

797:   *Frhs = NULL;
798:   TSGetIFunction(ts,&F,NULL,NULL);
799:   if (!ts->Frhs) {
800:     VecDuplicate(F,&ts->Frhs);
801:   }
802:   *Frhs = ts->Frhs;
803:   return(0);
804: }

806: static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
807: {
808:   Mat            A,B;
810:   TSIJacobian    ijacobian;

813:   if (Arhs) *Arhs = NULL;
814:   if (Brhs) *Brhs = NULL;
815:   TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);
816:   if (Arhs) {
817:     if (!ts->Arhs) {
818:       if (ijacobian) {
819:         MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);
820:       } else {
821:         ts->Arhs = A;
822:         PetscObjectReference((PetscObject)A);
823:       }
824:     } else {
825:       PetscBool flg;
826:       SNESGetUseMatrixFree(ts->snes,NULL,&flg);
827:       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
828:       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
829:         PetscObjectDereference((PetscObject)ts->Arhs);
830:         ts->Arhs = A;
831:         PetscObjectReference((PetscObject)A);
832:       }
833:     }
834:     *Arhs = ts->Arhs;
835:   }
836:   if (Brhs) {
837:     if (!ts->Brhs) {
838:       if (A != B) {
839:         if (ijacobian) {
840:           MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);
841:         } else {
842:           ts->Brhs = B;
843:           PetscObjectReference((PetscObject)B);
844:         }
845:       } else {
846:         PetscObjectReference((PetscObject)ts->Arhs);
847:         ts->Brhs = ts->Arhs;
848:       }
849:     }
850:     *Brhs = ts->Brhs;
851:   }
852:   return(0);
853: }

855: /*@
856:    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0

858:    Collective on TS and Vec

860:    Input Parameters:
861: +  ts - the TS context
862: .  t - current time
863: .  U - state vector
864: .  Udot - time derivative of state vector
865: -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate

867:    Output Parameter:
868: .  Y - right hand side

870:    Note:
871:    Most users should not need to explicitly call this routine, as it
872:    is used internally within the nonlinear solvers.

874:    If the user did did not write their equations in implicit form, this
875:    function recasts them in implicit form.

877:    Level: developer

879: .keywords: TS, compute

881: .seealso: TSSetIFunction(), TSComputeRHSFunction()
882: @*/
883: PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
884: {
886:   TSIFunction    ifunction;
887:   TSRHSFunction  rhsfunction;
888:   void           *ctx;
889:   DM             dm;


897:   TSGetDM(ts,&dm);
898:   DMTSGetIFunction(dm,&ifunction,&ctx);
899:   DMTSGetRHSFunction(dm,&rhsfunction,NULL);

901:   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");

903:   PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);
904:   if (ifunction) {
905:     PetscStackPush("TS user implicit function");
906:     (*ifunction)(ts,t,U,Udot,Y,ctx);
907:     PetscStackPop;
908:   }
909:   if (imex) {
910:     if (!ifunction) {
911:       VecCopy(Udot,Y);
912:     }
913:   } else if (rhsfunction) {
914:     if (ifunction) {
915:       Vec Frhs;
916:       TSGetRHSVec_Private(ts,&Frhs);
917:       TSComputeRHSFunction(ts,t,U,Frhs);
918:       VecAXPY(Y,-1,Frhs);
919:     } else {
920:       TSComputeRHSFunction(ts,t,U,Y);
921:       VecAYPX(Y,-1,Udot);
922:     }
923:   }
924:   PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);
925:   return(0);
926: }

928: /*@
929:    TSComputeIJacobian - Evaluates the Jacobian of the DAE

931:    Collective on TS and Vec

933:    Input
934:       Input Parameters:
935: +  ts - the TS context
936: .  t - current timestep
937: .  U - state vector
938: .  Udot - time derivative of state vector
939: .  shift - shift to apply, see note below
940: -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate

942:    Output Parameters:
943: +  A - Jacobian matrix
944: -  B - matrix from which the preconditioner is constructed; often the same as A

946:    Notes:
947:    If F(t,U,Udot)=0 is the DAE, the required Jacobian is

949:    dF/dU + shift*dF/dUdot

951:    Most users should not need to explicitly call this routine, as it
952:    is used internally within the nonlinear solvers.

954:    Level: developer

956: .keywords: TS, compute, Jacobian, matrix

958: .seealso:  TSSetIJacobian()
959: @*/
960: PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
961: {
963:   TSIJacobian    ijacobian;
964:   TSRHSJacobian  rhsjacobian;
965:   DM             dm;
966:   void           *ctx;


977:   TSGetDM(ts,&dm);
978:   DMTSGetIJacobian(dm,&ijacobian,&ctx);
979:   DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);

981:   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");

983:   PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);
984:   if (ijacobian) {
985:     PetscBool missing;
986:     PetscStackPush("TS user implicit Jacobian");
987:     (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);
988:     PetscStackPop;
989:     MatMissingDiagonal(A,&missing,NULL);
990:     if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
991:     if (B != A) {
992:       MatMissingDiagonal(B,&missing,NULL);
993:       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
994:     }
995:   }
996:   if (imex) {
997:     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
998:       PetscBool assembled;
999:       MatZeroEntries(A);
1000:       MatAssembled(A,&assembled);
1001:       if (!assembled) {
1002:         MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1003:         MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1004:       }
1005:       MatShift(A,shift);
1006:       if (A != B) {
1007:         MatZeroEntries(B);
1008:         MatAssembled(B,&assembled);
1009:         if (!assembled) {
1010:           MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1011:           MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1012:         }
1013:         MatShift(B,shift);
1014:       }
1015:     }
1016:   } else {
1017:     Mat Arhs = NULL,Brhs = NULL;
1018:     if (rhsjacobian) {
1019:       TSGetRHSMats_Private(ts,&Arhs,&Brhs);
1020:       TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);
1021:     }
1022:     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
1023:       PetscBool flg;
1024:       ts->rhsjacobian.scale = -1;
1025:       ts->rhsjacobian.shift = shift;
1026:       SNESGetUseMatrixFree(ts->snes,NULL,&flg);
1027:       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
1028:       if (!flg) {
1029:         MatScale(A,-1);
1030:         MatShift(A,shift);
1031:       }
1032:       if (A != B) {
1033:         MatScale(B,-1);
1034:         MatShift(B,shift);
1035:       }
1036:     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1037:       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1038:       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
1039:         MatZeroEntries(A);
1040:         MatShift(A,shift);
1041:         if (A != B) {
1042:           MatZeroEntries(B);
1043:           MatShift(B,shift);
1044:         }
1045:       }
1046:       MatAXPY(A,-1,Arhs,axpy);
1047:       if (A != B) {
1048:         MatAXPY(B,-1,Brhs,axpy);
1049:       }
1050:     }
1051:   }
1052:   PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);
1053:   return(0);
1054: }

1056: /*@C
1057:     TSSetRHSFunction - Sets the routine for evaluating the function,
1058:     where U_t = G(t,u).

1060:     Logically Collective on TS

1062:     Input Parameters:
1063: +   ts - the TS context obtained from TSCreate()
1064: .   r - vector to put the computed right hand side (or NULL to have it created)
1065: .   f - routine for evaluating the right-hand-side function
1066: -   ctx - [optional] user-defined context for private data for the
1067:           function evaluation routine (may be NULL)

1069:     Calling sequence of func:
1070: $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);

1072: +   t - current timestep
1073: .   u - input vector
1074: .   F - function vector
1075: -   ctx - [optional] user-defined function context

1077:     Level: beginner

1079:     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.

1081: .keywords: TS, timestep, set, right-hand-side, function

1083: .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1084: @*/
1085: PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1086: {
1088:   SNES           snes;
1089:   Vec            ralloc = NULL;
1090:   DM             dm;


1096:   TSGetDM(ts,&dm);
1097:   DMTSSetRHSFunction(dm,f,ctx);
1098:   TSGetSNES(ts,&snes);
1099:   if (!r && !ts->dm && ts->vec_sol) {
1100:     VecDuplicate(ts->vec_sol,&ralloc);
1101:     r = ralloc;
1102:   }
1103:   SNESSetFunction(snes,r,SNESTSFormFunction,ts);
1104:   VecDestroy(&ralloc);
1105:   return(0);
1106: }

1108: /*@C
1109:     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE

1111:     Logically Collective on TS

1113:     Input Parameters:
1114: +   ts - the TS context obtained from TSCreate()
1115: .   f - routine for evaluating the solution
1116: -   ctx - [optional] user-defined context for private data for the
1117:           function evaluation routine (may be NULL)

1119:     Calling sequence of func:
1120: $     func (TS ts,PetscReal t,Vec u,void *ctx);

1122: +   t - current timestep
1123: .   u - output vector
1124: -   ctx - [optional] user-defined function context

1126:     Notes:
1127:     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1128:     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1129:     create closed-form solutions with non-physical forcing terms.

1131:     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.

1133:     Level: beginner

1135: .keywords: TS, timestep, set, right-hand-side, function

1137: .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1138: @*/
1139: PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1140: {
1142:   DM             dm;

1146:   TSGetDM(ts,&dm);
1147:   DMTSSetSolutionFunction(dm,f,ctx);
1148:   return(0);
1149: }

1151: /*@C
1152:     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE

1154:     Logically Collective on TS

1156:     Input Parameters:
1157: +   ts - the TS context obtained from TSCreate()
1158: .   func - routine for evaluating the forcing function
1159: -   ctx - [optional] user-defined context for private data for the
1160:           function evaluation routine (may be NULL)

1162:     Calling sequence of func:
1163: $     func (TS ts,PetscReal t,Vec f,void *ctx);

1165: +   t - current timestep
1166: .   f - output vector
1167: -   ctx - [optional] user-defined function context

1169:     Notes:
1170:     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1171:     create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the
1172:     definition of the problem you are solving and hence possibly introducing bugs.

1174:     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0

1176:     This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the
1177:     parameters can be passed in the ctx variable.

1179:     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.

1181:     Level: beginner

1183: .keywords: TS, timestep, set, right-hand-side, function

1185: .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
1186: @*/
1187: PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
1188: {
1190:   DM             dm;

1194:   TSGetDM(ts,&dm);
1195:   DMTSSetForcingFunction(dm,func,ctx);
1196:   return(0);
1197: }

1199: /*@C
1200:    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1201:    where U_t = G(U,t), as well as the location to store the matrix.

1203:    Logically Collective on TS

1205:    Input Parameters:
1206: +  ts  - the TS context obtained from TSCreate()
1207: .  Amat - (approximate) Jacobian matrix
1208: .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1209: .  f   - the Jacobian evaluation routine
1210: -  ctx - [optional] user-defined context for private data for the
1211:          Jacobian evaluation routine (may be NULL)

1213:    Calling sequence of f:
1214: $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);

1216: +  t - current timestep
1217: .  u - input vector
1218: .  Amat - (approximate) Jacobian matrix
1219: .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1220: -  ctx - [optional] user-defined context for matrix evaluation routine

1222:    Notes:
1223:    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value

1225:    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1226:    You should not assume the values are the same in the next call to f() as you set them in the previous call.

1228:    Level: beginner

1230: .keywords: TS, timestep, set, right-hand-side, Jacobian

1232: .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()

1234: @*/
1235: PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1236: {
1238:   SNES           snes;
1239:   DM             dm;
1240:   TSIJacobian    ijacobian;


1249:   TSGetDM(ts,&dm);
1250:   DMTSSetRHSJacobian(dm,f,ctx);
1251:   if (f == TSComputeRHSJacobianConstant) {
1252:     /* Handle this case automatically for the user; otherwise user should call themselves. */
1253:     TSRHSJacobianSetReuse(ts,PETSC_TRUE);
1254:   }
1255:   DMTSGetIJacobian(dm,&ijacobian,NULL);
1256:   TSGetSNES(ts,&snes);
1257:   if (!ijacobian) {
1258:     SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);
1259:   }
1260:   if (Amat) {
1261:     PetscObjectReference((PetscObject)Amat);
1262:     MatDestroy(&ts->Arhs);
1263:     ts->Arhs = Amat;
1264:   }
1265:   if (Pmat) {
1266:     PetscObjectReference((PetscObject)Pmat);
1267:     MatDestroy(&ts->Brhs);
1268:     ts->Brhs = Pmat;
1269:   }
1270:   return(0);
1271: }

1273: /*@C
1274:    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.

1276:    Logically Collective on TS

1278:    Input Parameters:
1279: +  ts  - the TS context obtained from TSCreate()
1280: .  r   - vector to hold the residual (or NULL to have it created internally)
1281: .  f   - the function evaluation routine
1282: -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)

1284:    Calling sequence of f:
1285: $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);

1287: +  t   - time at step/stage being solved
1288: .  u   - state vector
1289: .  u_t - time derivative of state vector
1290: .  F   - function vector
1291: -  ctx - [optional] user-defined context for matrix evaluation routine

1293:    Important:
1294:    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.

1296:    Level: beginner

1298: .keywords: TS, timestep, set, DAE, Jacobian

1300: .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1301: @*/
1302: PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1303: {
1305:   SNES           snes;
1306:   Vec            ralloc = NULL;
1307:   DM             dm;


1313:   TSGetDM(ts,&dm);
1314:   DMTSSetIFunction(dm,f,ctx);

1316:   TSGetSNES(ts,&snes);
1317:   if (!r && !ts->dm && ts->vec_sol) {
1318:     VecDuplicate(ts->vec_sol,&ralloc);
1319:     r  = ralloc;
1320:   }
1321:   SNESSetFunction(snes,r,SNESTSFormFunction,ts);
1322:   VecDestroy(&ralloc);
1323:   return(0);
1324: }

1326: /*@C
1327:    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.

1329:    Not Collective

1331:    Input Parameter:
1332: .  ts - the TS context

1334:    Output Parameter:
1335: +  r - vector to hold residual (or NULL)
1336: .  func - the function to compute residual (or NULL)
1337: -  ctx - the function context (or NULL)

1339:    Level: advanced

1341: .keywords: TS, nonlinear, get, function

1343: .seealso: TSSetIFunction(), SNESGetFunction()
1344: @*/
1345: PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1346: {
1348:   SNES           snes;
1349:   DM             dm;

1353:   TSGetSNES(ts,&snes);
1354:   SNESGetFunction(snes,r,NULL,NULL);
1355:   TSGetDM(ts,&dm);
1356:   DMTSGetIFunction(dm,func,ctx);
1357:   return(0);
1358: }

1360: /*@C
1361:    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.

1363:    Not Collective

1365:    Input Parameter:
1366: .  ts - the TS context

1368:    Output Parameter:
1369: +  r - vector to hold computed right hand side (or NULL)
1370: .  func - the function to compute right hand side (or NULL)
1371: -  ctx - the function context (or NULL)

1373:    Level: advanced

1375: .keywords: TS, nonlinear, get, function

1377: .seealso: TSSetRHSFunction(), SNESGetFunction()
1378: @*/
1379: PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1380: {
1382:   SNES           snes;
1383:   DM             dm;

1387:   TSGetSNES(ts,&snes);
1388:   SNESGetFunction(snes,r,NULL,NULL);
1389:   TSGetDM(ts,&dm);
1390:   DMTSGetRHSFunction(dm,func,ctx);
1391:   return(0);
1392: }

1394: /*@C
1395:    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1396:         provided with TSSetIFunction().

1398:    Logically Collective on TS

1400:    Input Parameters:
1401: +  ts  - the TS context obtained from TSCreate()
1402: .  Amat - (approximate) Jacobian matrix
1403: .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1404: .  f   - the Jacobian evaluation routine
1405: -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)

1407:    Calling sequence of f:
1408: $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);

1410: +  t    - time at step/stage being solved
1411: .  U    - state vector
1412: .  U_t  - time derivative of state vector
1413: .  a    - shift
1414: .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1415: .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1416: -  ctx  - [optional] user-defined context for matrix evaluation routine

1418:    Notes:
1419:    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.

1421:    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1422:    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.

1424:    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1425:    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1426:    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1427:    a and vector W depend on the integration method, step size, and past states. For example with
1428:    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1429:    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt

1431:    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value

1433:    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1434:    You should not assume the values are the same in the next call to f() as you set them in the previous call.

1436:    Level: beginner

1438: .keywords: TS, timestep, DAE, Jacobian

1440: .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()

1442: @*/
1443: PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1444: {
1446:   SNES           snes;
1447:   DM             dm;


1456:   TSGetDM(ts,&dm);
1457:   DMTSSetIJacobian(dm,f,ctx);

1459:   TSGetSNES(ts,&snes);
1460:   SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);
1461:   return(0);
1462: }

1464: /*@
1465:    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1466:    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1467:    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1468:    not been changed by the TS.

1470:    Logically Collective

1472:    Input Arguments:
1473: +  ts - TS context obtained from TSCreate()
1474: -  reuse - PETSC_TRUE if the RHS Jacobian

1476:    Level: intermediate

1478: .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1479: @*/
1480: PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1481: {
1483:   ts->rhsjacobian.reuse = reuse;
1484:   return(0);
1485: }

1487: /*@C
1488:    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.

1490:    Logically Collective on TS

1492:    Input Parameters:
1493: +  ts  - the TS context obtained from TSCreate()
1494: .  F   - vector to hold the residual (or NULL to have it created internally)
1495: .  fun - the function evaluation routine
1496: -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)

1498:    Calling sequence of fun:
1499: $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);

1501: +  t    - time at step/stage being solved
1502: .  U    - state vector
1503: .  U_t  - time derivative of state vector
1504: .  U_tt - second time derivative of state vector
1505: .  F    - function vector
1506: -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)

1508:    Level: beginner

1510: .keywords: TS, timestep, set, ODE, DAE, Function

1512: .seealso: TSSetI2Jacobian()
1513: @*/
1514: PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1515: {
1516:   DM             dm;

1522:   TSSetIFunction(ts,F,NULL,NULL);
1523:   TSGetDM(ts,&dm);
1524:   DMTSSetI2Function(dm,fun,ctx);
1525:   return(0);
1526: }

1528: /*@C
1529:   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.

1531:   Not Collective

1533:   Input Parameter:
1534: . ts - the TS context

1536:   Output Parameter:
1537: + r - vector to hold residual (or NULL)
1538: . fun - the function to compute residual (or NULL)
1539: - ctx - the function context (or NULL)

1541:   Level: advanced

1543: .keywords: TS, nonlinear, get, function

1545: .seealso: TSSetI2Function(), SNESGetFunction()
1546: @*/
1547: PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1548: {
1550:   SNES           snes;
1551:   DM             dm;

1555:   TSGetSNES(ts,&snes);
1556:   SNESGetFunction(snes,r,NULL,NULL);
1557:   TSGetDM(ts,&dm);
1558:   DMTSGetI2Function(dm,fun,ctx);
1559:   return(0);
1560: }

1562: /*@C
1563:    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1564:         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().

1566:    Logically Collective on TS

1568:    Input Parameters:
1569: +  ts  - the TS context obtained from TSCreate()
1570: .  J   - Jacobian matrix
1571: .  P   - preconditioning matrix for J (may be same as J)
1572: .  jac - the Jacobian evaluation routine
1573: -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)

1575:    Calling sequence of jac:
1576: $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);

1578: +  t    - time at step/stage being solved
1579: .  U    - state vector
1580: .  U_t  - time derivative of state vector
1581: .  U_tt - second time derivative of state vector
1582: .  v    - shift for U_t
1583: .  a    - shift for U_tt
1584: .  J    - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t  + a*dF/dU_tt
1585: .  P    - preconditioning matrix for J, may be same as J
1586: -  ctx  - [optional] user-defined context for matrix evaluation routine

1588:    Notes:
1589:    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.

1591:    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1592:    the Jacobian of G(U) = F(t,U,W+v*U,W'+a*U) where F(t,U,U_t,U_tt) = 0 is the DAE to be solved.
1593:    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1594:    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.

1596:    Level: beginner

1598: .keywords: TS, timestep, set, ODE, DAE, Jacobian

1600: .seealso: TSSetI2Function()
1601: @*/
1602: PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1603: {
1604:   DM             dm;

1611:   TSSetIJacobian(ts,J,P,NULL,NULL);
1612:   TSGetDM(ts,&dm);
1613:   DMTSSetI2Jacobian(dm,jac,ctx);
1614:   return(0);
1615: }

1617: /*@C
1618:   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.

1620:   Not Collective, but parallel objects are returned if TS is parallel

1622:   Input Parameter:
1623: . ts  - The TS context obtained from TSCreate()

1625:   Output Parameters:
1626: + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1627: . P - The matrix from which the preconditioner is constructed, often the same as J
1628: . jac - The function to compute the Jacobian matrices
1629: - ctx - User-defined context for Jacobian evaluation routine

1631:   Notes: You can pass in NULL for any return argument you do not need.

1633:   Level: advanced

1635: .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()

1637: .keywords: TS, timestep, get, matrix, Jacobian
1638: @*/
1639: PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1640: {
1642:   SNES           snes;
1643:   DM             dm;

1646:   TSGetSNES(ts,&snes);
1647:   SNESSetUpMatrices(snes);
1648:   SNESGetJacobian(snes,J,P,NULL,NULL);
1649:   TSGetDM(ts,&dm);
1650:   DMTSGetI2Jacobian(dm,jac,ctx);
1651:   return(0);
1652: }

1654: /*@
1655:   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0

1657:   Collective on TS and Vec

1659:   Input Parameters:
1660: + ts - the TS context
1661: . t - current time
1662: . U - state vector
1663: . V - time derivative of state vector (U_t)
1664: - A - second time derivative of state vector (U_tt)

1666:   Output Parameter:
1667: . F - the residual vector

1669:   Note:
1670:   Most users should not need to explicitly call this routine, as it
1671:   is used internally within the nonlinear solvers.

1673:   Level: developer

1675: .keywords: TS, compute, function, vector

1677: .seealso: TSSetI2Function()
1678: @*/
1679: PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1680: {
1681:   DM             dm;
1682:   TSI2Function   I2Function;
1683:   void           *ctx;
1684:   TSRHSFunction  rhsfunction;


1694:   TSGetDM(ts,&dm);
1695:   DMTSGetI2Function(dm,&I2Function,&ctx);
1696:   DMTSGetRHSFunction(dm,&rhsfunction,NULL);

1698:   if (!I2Function) {
1699:     TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);
1700:     return(0);
1701:   }

1703:   PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);

1705:   PetscStackPush("TS user implicit function");
1706:   I2Function(ts,t,U,V,A,F,ctx);
1707:   PetscStackPop;

1709:   if (rhsfunction) {
1710:     Vec Frhs;
1711:     TSGetRHSVec_Private(ts,&Frhs);
1712:     TSComputeRHSFunction(ts,t,U,Frhs);
1713:     VecAXPY(F,-1,Frhs);
1714:   }

1716:   PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);
1717:   return(0);
1718: }

1720: /*@
1721:   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE

1723:   Collective on TS and Vec

1725:   Input Parameters:
1726: + ts - the TS context
1727: . t - current timestep
1728: . U - state vector
1729: . V - time derivative of state vector
1730: . A - second time derivative of state vector
1731: . shiftV - shift to apply, see note below
1732: - shiftA - shift to apply, see note below

1734:   Output Parameters:
1735: + J - Jacobian matrix
1736: - P - optional preconditioning matrix

1738:   Notes:
1739:   If F(t,U,V,A)=0 is the DAE, the required Jacobian is

1741:   dF/dU + shiftV*dF/dV + shiftA*dF/dA

1743:   Most users should not need to explicitly call this routine, as it
1744:   is used internally within the nonlinear solvers.

1746:   Level: developer

1748: .keywords: TS, compute, Jacobian, matrix

1750: .seealso:  TSSetI2Jacobian()
1751: @*/
1752: PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1753: {
1754:   DM             dm;
1755:   TSI2Jacobian   I2Jacobian;
1756:   void           *ctx;
1757:   TSRHSJacobian  rhsjacobian;


1768:   TSGetDM(ts,&dm);
1769:   DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);
1770:   DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);

1772:   if (!I2Jacobian) {
1773:     TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);
1774:     return(0);
1775:   }

1777:   PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);

1779:   PetscStackPush("TS user implicit Jacobian");
1780:   I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);
1781:   PetscStackPop;

1783:   if (rhsjacobian) {
1784:     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1785:     TSGetRHSMats_Private(ts,&Jrhs,&Prhs);
1786:     TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);
1787:     MatAXPY(J,-1,Jrhs,axpy);
1788:     if (P != J) {MatAXPY(P,-1,Prhs,axpy);}
1789:   }

1791:   PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);
1792:   return(0);
1793: }

1795: /*@
1796:    TS2SetSolution - Sets the initial solution and time derivative vectors
1797:    for use by the TS routines handling second order equations.

1799:    Logically Collective on TS and Vec

1801:    Input Parameters:
1802: +  ts - the TS context obtained from TSCreate()
1803: .  u - the solution vector
1804: -  v - the time derivative vector

1806:    Level: beginner

1808: .keywords: TS, timestep, set, solution, initial conditions
1809: @*/
1810: PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1811: {

1818:   TSSetSolution(ts,u);
1819:   PetscObjectReference((PetscObject)v);
1820:   VecDestroy(&ts->vec_dot);
1821:   ts->vec_dot = v;
1822:   return(0);
1823: }

1825: /*@
1826:    TS2GetSolution - Returns the solution and time derivative at the present timestep
1827:    for second order equations. It is valid to call this routine inside the function
1828:    that you are evaluating in order to move to the new timestep. This vector not
1829:    changed until the solution at the next timestep has been calculated.

1831:    Not Collective, but Vec returned is parallel if TS is parallel

1833:    Input Parameter:
1834: .  ts - the TS context obtained from TSCreate()

1836:    Output Parameter:
1837: +  u - the vector containing the solution
1838: -  v - the vector containing the time derivative

1840:    Level: intermediate

1842: .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()

1844: .keywords: TS, timestep, get, solution
1845: @*/
1846: PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1847: {
1852:   if (u) *u = ts->vec_sol;
1853:   if (v) *v = ts->vec_dot;
1854:   return(0);
1855: }

1857: /*@C
1858:   TSLoad - Loads a KSP that has been stored in binary  with KSPView().

1860:   Collective on PetscViewer

1862:   Input Parameters:
1863: + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
1864:            some related function before a call to TSLoad().
1865: - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()

1867:    Level: intermediate

1869:   Notes:
1870:    The type is determined by the data in the file, any type set into the TS before this call is ignored.

1872:   Notes for advanced users:
1873:   Most users should not need to know the details of the binary storage
1874:   format, since TSLoad() and TSView() completely hide these details.
1875:   But for anyone who's interested, the standard binary matrix storage
1876:   format is
1877: .vb
1878:      has not yet been determined
1879: .ve

1881: .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
1882: @*/
1883: PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
1884: {
1886:   PetscBool      isbinary;
1887:   PetscInt       classid;
1888:   char           type[256];
1889:   DMTS           sdm;
1890:   DM             dm;

1895:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1896:   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");

1898:   PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);
1899:   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1900:   PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);
1901:   TSSetType(ts, type);
1902:   if (ts->ops->load) {
1903:     (*ts->ops->load)(ts,viewer);
1904:   }
1905:   DMCreate(PetscObjectComm((PetscObject)ts),&dm);
1906:   DMLoad(dm,viewer);
1907:   TSSetDM(ts,dm);
1908:   DMCreateGlobalVector(ts->dm,&ts->vec_sol);
1909:   VecLoad(ts->vec_sol,viewer);
1910:   DMGetDMTS(ts->dm,&sdm);
1911:   DMTSLoad(sdm,viewer);
1912:   return(0);
1913: }

1915:  #include <petscdraw.h>
1916: #if defined(PETSC_HAVE_SAWS)
1917:  #include <petscviewersaws.h>
1918: #endif
1919: /*@C
1920:     TSView - Prints the TS data structure.

1922:     Collective on TS

1924:     Input Parameters:
1925: +   ts - the TS context obtained from TSCreate()
1926: -   viewer - visualization context

1928:     Options Database Key:
1929: .   -ts_view - calls TSView() at end of TSStep()

1931:     Notes:
1932:     The available visualization contexts include
1933: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1934: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1935:          output where only the first processor opens
1936:          the file.  All other processors send their
1937:          data to the first processor to print.

1939:     The user can open an alternative visualization context with
1940:     PetscViewerASCIIOpen() - output to a specified file.

1942:     Level: beginner

1944: .keywords: TS, timestep, view

1946: .seealso: PetscViewerASCIIOpen()
1947: @*/
1948: PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1949: {
1951:   TSType         type;
1952:   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
1953:   DMTS           sdm;
1954: #if defined(PETSC_HAVE_SAWS)
1955:   PetscBool      issaws;
1956: #endif

1960:   if (!viewer) {
1961:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);
1962:   }

1966:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1967:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
1968:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1969:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1970: #if defined(PETSC_HAVE_SAWS)
1971:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
1972: #endif
1973:   if (iascii) {
1974:     PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);
1975:     if (ts->ops->view) {
1976:       PetscViewerASCIIPushTab(viewer);
1977:       (*ts->ops->view)(ts,viewer);
1978:       PetscViewerASCIIPopTab(viewer);
1979:     }
1980:     if (ts->max_steps < PETSC_MAX_INT) {
1981:       PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);
1982:     }
1983:     if (ts->max_time < PETSC_MAX_REAL) {
1984:       PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);
1985:     }
1986:     if (ts->usessnes) {
1987:       PetscBool lin;
1988:       if (ts->problem_type == TS_NONLINEAR) {
1989:         PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);
1990:       }
1991:       PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);
1992:       PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);
1993:       PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);
1994:     }
1995:     PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);
1996:     if (ts->vrtol) {
1997:       PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");
1998:     } else {
1999:       PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);
2000:     }
2001:     if (ts->vatol) {
2002:       PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");
2003:     } else {
2004:       PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);
2005:     }
2006:     TSAdaptView(ts->adapt,viewer);
2007:     if (ts->snes && ts->usessnes)  {SNESView(ts->snes,viewer);}
2008:     DMGetDMTS(ts->dm,&sdm);
2009:     DMTSView(sdm,viewer);
2010:   } else if (isstring) {
2011:     TSGetType(ts,&type);
2012:     PetscViewerStringSPrintf(viewer," %-7.7s",type);
2013:   } else if (isbinary) {
2014:     PetscInt    classid = TS_FILE_CLASSID;
2015:     MPI_Comm    comm;
2016:     PetscMPIInt rank;
2017:     char        type[256];

2019:     PetscObjectGetComm((PetscObject)ts,&comm);
2020:     MPI_Comm_rank(comm,&rank);
2021:     if (!rank) {
2022:       PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);
2023:       PetscStrncpy(type,((PetscObject)ts)->type_name,256);
2024:       PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);
2025:     }
2026:     if (ts->ops->view) {
2027:       (*ts->ops->view)(ts,viewer);
2028:     }
2029:     if (ts->adapt) {TSAdaptView(ts->adapt,viewer);}
2030:     DMView(ts->dm,viewer);
2031:     VecView(ts->vec_sol,viewer);
2032:     DMGetDMTS(ts->dm,&sdm);
2033:     DMTSView(sdm,viewer);
2034:   } else if (isdraw) {
2035:     PetscDraw draw;
2036:     char      str[36];
2037:     PetscReal x,y,bottom,h;

2039:     PetscViewerDrawGetDraw(viewer,0,&draw);
2040:     PetscDrawGetCurrentPoint(draw,&x,&y);
2041:     PetscStrcpy(str,"TS: ");
2042:     PetscStrcat(str,((PetscObject)ts)->type_name);
2043:     PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);
2044:     bottom = y - h;
2045:     PetscDrawPushCurrentPoint(draw,x,bottom);
2046:     if (ts->ops->view) {
2047:       (*ts->ops->view)(ts,viewer);
2048:     }
2049:     if (ts->adapt) {TSAdaptView(ts->adapt,viewer);}
2050:     if (ts->snes)  {SNESView(ts->snes,viewer);}
2051:     PetscDrawPopCurrentPoint(draw);
2052: #if defined(PETSC_HAVE_SAWS)
2053:   } else if (issaws) {
2054:     PetscMPIInt rank;
2055:     const char  *name;

2057:     PetscObjectGetName((PetscObject)ts,&name);
2058:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
2059:     if (!((PetscObject)ts)->amsmem && !rank) {
2060:       char       dir[1024];

2062:       PetscObjectViewSAWs((PetscObject)ts,viewer);
2063:       PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);
2064:       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
2065:       PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);
2066:       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2067:     }
2068:     if (ts->ops->view) {
2069:       (*ts->ops->view)(ts,viewer);
2070:     }
2071: #endif
2072:   }

2074:   PetscViewerASCIIPushTab(viewer);
2075:   PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);
2076:   PetscViewerASCIIPopTab(viewer);
2077:   return(0);
2078: }

2080: /*@
2081:    TSSetApplicationContext - Sets an optional user-defined context for
2082:    the timesteppers.

2084:    Logically Collective on TS

2086:    Input Parameters:
2087: +  ts - the TS context obtained from TSCreate()
2088: -  usrP - optional user context

2090:    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2091:     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.

2093:    Level: intermediate

2095: .keywords: TS, timestep, set, application, context

2097: .seealso: TSGetApplicationContext()
2098: @*/
2099: PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2100: {
2103:   ts->user = usrP;
2104:   return(0);
2105: }

2107: /*@
2108:     TSGetApplicationContext - Gets the user-defined context for the
2109:     timestepper.

2111:     Not Collective

2113:     Input Parameter:
2114: .   ts - the TS context obtained from TSCreate()

2116:     Output Parameter:
2117: .   usrP - user context

2119:    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2120:     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.

2122:     Level: intermediate

2124: .keywords: TS, timestep, get, application, context

2126: .seealso: TSSetApplicationContext()
2127: @*/
2128: PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2129: {
2132:   *(void**)usrP = ts->user;
2133:   return(0);
2134: }

2136: /*@
2137:    TSGetStepNumber - Gets the number of steps completed.

2139:    Not Collective

2141:    Input Parameter:
2142: .  ts - the TS context obtained from TSCreate()

2144:    Output Parameter:
2145: .  steps - number of steps completed so far

2147:    Level: intermediate

2149: .keywords: TS, timestep, get, iteration, number
2150: .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2151: @*/
2152: PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2153: {
2157:   *steps = ts->steps;
2158:   return(0);
2159: }

2161: /*@
2162:    TSSetStepNumber - Sets the number of steps completed.

2164:    Logically Collective on TS

2166:    Input Parameters:
2167: +  ts - the TS context
2168: -  steps - number of steps completed so far

2170:    Notes:
2171:    For most uses of the TS solvers the user need not explicitly call
2172:    TSSetStepNumber(), as the step counter is appropriately updated in
2173:    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
2174:    reinitialize timestepping by setting the step counter to zero (and time
2175:    to the initial time) to solve a similar problem with different initial
2176:    conditions or parameters. Other possible use case is to continue
2177:    timestepping from a previously interrupted run in such a way that TS
2178:    monitors will be called with a initial nonzero step counter.

2180:    Level: advanced

2182: .keywords: TS, timestep, set, iteration, number
2183: .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
2184: @*/
2185: PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
2186: {
2190:   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
2191:   ts->steps = steps;
2192:   return(0);
2193: }

2195: /*@
2196:    TSSetTimeStep - Allows one to reset the timestep at any time,
2197:    useful for simple pseudo-timestepping codes.

2199:    Logically Collective on TS

2201:    Input Parameters:
2202: +  ts - the TS context obtained from TSCreate()
2203: -  time_step - the size of the timestep

2205:    Level: intermediate

2207: .seealso: TSGetTimeStep(), TSSetTime()

2209: .keywords: TS, set, timestep
2210: @*/
2211: PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2212: {
2216:   ts->time_step = time_step;
2217:   return(0);
2218: }

2220: /*@
2221:    TSSetExactFinalTime - Determines whether to adapt the final time step to
2222:      match the exact final time, interpolate solution to the exact final time,
2223:      or just return at the final time TS computed.

2225:   Logically Collective on TS

2227:    Input Parameter:
2228: +   ts - the time-step context
2229: -   eftopt - exact final time option

2231: $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2232: $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2233: $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time

2235:    Options Database:
2236: .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime

2238:    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2239:     then the final time you selected.

2241:    Level: beginner

2243: .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2244: @*/
2245: PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2246: {
2250:   ts->exact_final_time = eftopt;
2251:   return(0);
2252: }

2254: /*@
2255:    TSGetExactFinalTime - Gets the exact final time option.

2257:    Not Collective

2259:    Input Parameter:
2260: .  ts - the TS context

2262:    Output Parameter:
2263: .  eftopt - exact final time option

2265:    Level: beginner

2267: .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2268: @*/
2269: PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2270: {
2274:   *eftopt = ts->exact_final_time;
2275:   return(0);
2276: }

2278: /*@
2279:    TSGetTimeStep - Gets the current timestep size.

2281:    Not Collective

2283:    Input Parameter:
2284: .  ts - the TS context obtained from TSCreate()

2286:    Output Parameter:
2287: .  dt - the current timestep size

2289:    Level: intermediate

2291: .seealso: TSSetTimeStep(), TSGetTime()

2293: .keywords: TS, get, timestep
2294: @*/
2295: PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2296: {
2300:   *dt = ts->time_step;
2301:   return(0);
2302: }

2304: /*@
2305:    TSGetSolution - Returns the solution at the present timestep. It
2306:    is valid to call this routine inside the function that you are evaluating
2307:    in order to move to the new timestep. This vector not changed until
2308:    the solution at the next timestep has been calculated.

2310:    Not Collective, but Vec returned is parallel if TS is parallel

2312:    Input Parameter:
2313: .  ts - the TS context obtained from TSCreate()

2315:    Output Parameter:
2316: .  v - the vector containing the solution

2318:    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
2319:    final time. It returns the solution at the next timestep.

2321:    Level: intermediate

2323: .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()

2325: .keywords: TS, timestep, get, solution
2326: @*/
2327: PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2328: {
2332:   *v = ts->vec_sol;
2333:   return(0);
2334: }

2336: /*@
2337:    TSGetSolutionComponents - Returns any solution components at the present 
2338:    timestep, if available for the time integration method being used. 
2339:    Solution components are quantities that share the same size and 
2340:    structure as the solution vector.

2342:    Not Collective, but Vec returned is parallel if TS is parallel

2344:    Parameters :
2345: .  ts - the TS context obtained from TSCreate() (input parameter).
2346: .  n - If v is PETSC_NULL, then the number of solution components is
2347:        returned through n, else the n-th solution component is 
2348:        returned in v.
2349: .  v - the vector containing the n-th solution component 
2350:        (may be PETSC_NULL to use this function to find out
2351:         the number of solutions components).

2353:    Level: advanced

2355: .seealso: TSGetSolution()

2357: .keywords: TS, timestep, get, solution
2358: @*/
2359: PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
2360: {

2365:   if (!ts->ops->getsolutioncomponents) *n = 0;
2366:   else {
2367:     (*ts->ops->getsolutioncomponents)(ts,n,v);
2368:   }
2369:   return(0);
2370: }

2372: /*@
2373:    TSGetAuxSolution - Returns an auxiliary solution at the present 
2374:    timestep, if available for the time integration method being used.

2376:    Not Collective, but Vec returned is parallel if TS is parallel

2378:    Parameters :
2379: .  ts - the TS context obtained from TSCreate() (input parameter).
2380: .  v - the vector containing the auxiliary solution 

2382:    Level: intermediate

2384: .seealso: TSGetSolution()

2386: .keywords: TS, timestep, get, solution
2387: @*/
2388: PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
2389: {

2394:   if (ts->ops->getauxsolution) {
2395:     (*ts->ops->getauxsolution)(ts,v);
2396:   } else {
2397:     VecZeroEntries(*v);
2398:   }
2399:   return(0);
2400: }

2402: /*@
2403:    TSGetTimeError - Returns the estimated error vector, if the chosen
2404:    TSType has an error estimation functionality.

2406:    Not Collective, but Vec returned is parallel if TS is parallel

2408:    Note: MUST call after TSSetUp()

2410:    Parameters :
2411: .  ts - the TS context obtained from TSCreate() (input parameter).
2412: .  n - current estimate (n=0) or previous one (n=-1)
2413: .  v - the vector containing the error (same size as the solution).

2415:    Level: intermediate

2417: .seealso: TSGetSolution(), TSSetTimeError()

2419: .keywords: TS, timestep, get, error
2420: @*/
2421: PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
2422: {

2427:   if (ts->ops->gettimeerror) {
2428:     (*ts->ops->gettimeerror)(ts,n,v);
2429:   } else {
2430:     VecZeroEntries(*v);
2431:   }
2432:   return(0);
2433: }

2435: /*@
2436:    TSSetTimeError - Sets the estimated error vector, if the chosen
2437:    TSType has an error estimation functionality. This can be used
2438:    to restart such a time integrator with a given error vector.

2440:    Not Collective, but Vec returned is parallel if TS is parallel

2442:    Parameters :
2443: .  ts - the TS context obtained from TSCreate() (input parameter).
2444: .  v - the vector containing the error (same size as the solution).

2446:    Level: intermediate

2448: .seealso: TSSetSolution(), TSGetTimeError)

2450: .keywords: TS, timestep, get, error
2451: @*/
2452: PetscErrorCode  TSSetTimeError(TS ts,Vec v)
2453: {

2458:   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
2459:   if (ts->ops->settimeerror) {
2460:     (*ts->ops->settimeerror)(ts,v);
2461:   }
2462:   return(0);
2463: }

2465: /*@
2466:    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()

2468:    Not Collective, but Vec returned is parallel if TS is parallel

2470:    Input Parameter:
2471: .  ts - the TS context obtained from TSCreate()

2473:    Output Parameter:
2474: +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2475: -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters

2477:    Level: intermediate

2479: .seealso: TSGetTimeStep()

2481: .keywords: TS, timestep, get, sensitivity
2482: @*/
2483: PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2484: {
2487:   if (numcost) *numcost = ts->numcost;
2488:   if (lambda)  *lambda  = ts->vecs_sensi;
2489:   if (mu)      *mu      = ts->vecs_sensip;
2490:   return(0);
2491: }

2493: /* ----- Routines to initialize and destroy a timestepper ---- */
2494: /*@
2495:   TSSetProblemType - Sets the type of problem to be solved.

2497:   Not collective

2499:   Input Parameters:
2500: + ts   - The TS
2501: - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2502: .vb
2503:          U_t - A U = 0      (linear)
2504:          U_t - A(t) U = 0   (linear)
2505:          F(t,U,U_t) = 0     (nonlinear)
2506: .ve

2508:    Level: beginner

2510: .keywords: TS, problem type
2511: .seealso: TSSetUp(), TSProblemType, TS
2512: @*/
2513: PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2514: {

2519:   ts->problem_type = type;
2520:   if (type == TS_LINEAR) {
2521:     SNES snes;
2522:     TSGetSNES(ts,&snes);
2523:     SNESSetType(snes,SNESKSPONLY);
2524:   }
2525:   return(0);
2526: }

2528: /*@C
2529:   TSGetProblemType - Gets the type of problem to be solved.

2531:   Not collective

2533:   Input Parameter:
2534: . ts   - The TS

2536:   Output Parameter:
2537: . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2538: .vb
2539:          M U_t = A U
2540:          M(t) U_t = A(t) U
2541:          F(t,U,U_t)
2542: .ve

2544:    Level: beginner

2546: .keywords: TS, problem type
2547: .seealso: TSSetUp(), TSProblemType, TS
2548: @*/
2549: PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2550: {
2554:   *type = ts->problem_type;
2555:   return(0);
2556: }

2558: /*@
2559:    TSSetUp - Sets up the internal data structures for the later use
2560:    of a timestepper.

2562:    Collective on TS

2564:    Input Parameter:
2565: .  ts - the TS context obtained from TSCreate()

2567:    Notes:
2568:    For basic use of the TS solvers the user need not explicitly call
2569:    TSSetUp(), since these actions will automatically occur during
2570:    the call to TSStep() or TSSolve().  However, if one wishes to control this
2571:    phase separately, TSSetUp() should be called after TSCreate()
2572:    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().

2574:    Level: advanced

2576: .keywords: TS, timestep, setup

2578: .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2579: @*/
2580: PetscErrorCode  TSSetUp(TS ts)
2581: {
2583:   DM             dm;
2584:   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2585:   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2586:   TSIFunction    ifun;
2587:   TSIJacobian    ijac;
2588:   TSI2Jacobian   i2jac;
2589:   TSRHSJacobian  rhsjac;
2590:   PetscBool      isnone;

2594:   if (ts->setupcalled) return(0);

2596:   if (!((PetscObject)ts)->type_name) {
2597:     TSGetIFunction(ts,NULL,&ifun,NULL);
2598:     TSSetType(ts,ifun ? TSBEULER : TSEULER);
2599:   }

2601:   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");

2603:   if (ts->rhsjacobian.reuse) {
2604:     Mat Amat,Pmat;
2605:     SNES snes;
2606:     TSGetSNES(ts,&snes);
2607:     SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);
2608:     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2609:      * have displaced the RHS matrix */
2610:     if (Amat == ts->Arhs) {
2611:       /* we need to copy the values of the matrix because for the constant Jacobian case the user will never set the numerical values in this new location */
2612:       MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);
2613:       SNESSetJacobian(snes,Amat,NULL,NULL,NULL);
2614:       MatDestroy(&Amat);
2615:     }
2616:     if (Pmat == ts->Brhs) {
2617:       MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);
2618:       SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);
2619:       MatDestroy(&Pmat);
2620:     }
2621:   }

2623:   TSGetAdapt(ts,&ts->adapt);
2624:   TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);

2626:   if (ts->ops->setup) {
2627:     (*ts->ops->setup)(ts);
2628:   }

2630:   /* Attempt to check/preset a default value for the exact final time option */
2631:   PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);
2632:   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
2633:     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;

2635:   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
2636:      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
2637:    */
2638:   TSGetDM(ts,&dm);
2639:   DMSNESGetFunction(dm,&func,NULL);
2640:   if (!func) {
2641:     DMSNESSetFunction(dm,SNESTSFormFunction,ts);
2642:   }
2643:   /* If the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
2644:      Otherwise, the SNES will use coloring internally to form the Jacobian.
2645:    */
2646:   DMSNESGetJacobian(dm,&jac,NULL);
2647:   DMTSGetIJacobian(dm,&ijac,NULL);
2648:   DMTSGetI2Jacobian(dm,&i2jac,NULL);
2649:   DMTSGetRHSJacobian(dm,&rhsjac,NULL);
2650:   if (!jac && (ijac || i2jac || rhsjac)) {
2651:     DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);
2652:   }

2654:   /* if time integration scheme has a starting method, call it */
2655:   if (ts->ops->startingmethod) {
2656:     (*ts->ops->startingmethod)(ts);
2657:   }

2659:   ts->setupcalled = PETSC_TRUE;
2660:   return(0);
2661: }

2663: /*@
2664:    TSAdjointSetUp - Sets up the internal data structures for the later use
2665:    of an adjoint solver

2667:    Collective on TS

2669:    Input Parameter:
2670: .  ts - the TS context obtained from TSCreate()

2672:    Level: advanced

2674: .keywords: TS, timestep, setup

2676: .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2677: @*/
2678: PetscErrorCode  TSAdjointSetUp(TS ts)
2679: {

2684:   if (ts->adjointsetupcalled) return(0);
2685:   if (!ts->vecs_sensi) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2686:   if (ts->vecs_sensip && !ts->Jacp) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetRHSJacobian() first");

2688:   if (ts->vec_costintegral) { /* if there is integral in the cost function */
2689:     VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);
2690:     if (ts->vecs_sensip){
2691:       VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);
2692:     }
2693:   }

2695:   if (ts->ops->adjointsetup) {
2696:     (*ts->ops->adjointsetup)(ts);
2697:   }
2698:   ts->adjointsetupcalled = PETSC_TRUE;
2699:   return(0);
2700: }

2702: /*@
2703:    TSReset - Resets a TS context and removes any allocated Vecs and Mats.

2705:    Collective on TS

2707:    Input Parameter:
2708: .  ts - the TS context obtained from TSCreate()

2710:    Level: beginner

2712: .keywords: TS, timestep, reset

2714: .seealso: TSCreate(), TSSetup(), TSDestroy()
2715: @*/
2716: PetscErrorCode  TSReset(TS ts)
2717: {


2723:   if (ts->ops->reset) {
2724:     (*ts->ops->reset)(ts);
2725:   }
2726:   if (ts->snes) {SNESReset(ts->snes);}
2727:   if (ts->adapt) {TSAdaptReset(ts->adapt);}

2729:   MatDestroy(&ts->Arhs);
2730:   MatDestroy(&ts->Brhs);
2731:   VecDestroy(&ts->Frhs);
2732:   VecDestroy(&ts->vec_sol);
2733:   VecDestroy(&ts->vec_dot);
2734:   VecDestroy(&ts->vatol);
2735:   VecDestroy(&ts->vrtol);
2736:   VecDestroyVecs(ts->nwork,&ts->work);

2738:   VecDestroyVecs(ts->numcost,&ts->vecs_drdy);
2739:   VecDestroyVecs(ts->numcost,&ts->vecs_drdp);

2741:   MatDestroy(&ts->Jacp);
2742:   VecDestroy(&ts->vec_costintegral);
2743:   VecDestroy(&ts->vec_costintegrand);

2745:   PetscFree(ts->vecs_fwdsensipacked);

2747:   ts->setupcalled = PETSC_FALSE;
2748:   return(0);
2749: }

2751: /*@
2752:    TSDestroy - Destroys the timestepper context that was created
2753:    with TSCreate().

2755:    Collective on TS

2757:    Input Parameter:
2758: .  ts - the TS context obtained from TSCreate()

2760:    Level: beginner

2762: .keywords: TS, timestepper, destroy

2764: .seealso: TSCreate(), TSSetUp(), TSSolve()
2765: @*/
2766: PetscErrorCode  TSDestroy(TS *ts)
2767: {

2771:   if (!*ts) return(0);
2773:   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; return(0);}

2775:   TSReset((*ts));

2777:   /* if memory was published with SAWs then destroy it */
2778:   PetscObjectSAWsViewOff((PetscObject)*ts);
2779:   if ((*ts)->ops->destroy) {(*(*ts)->ops->destroy)((*ts));}

2781:   TSTrajectoryDestroy(&(*ts)->trajectory);

2783:   TSAdaptDestroy(&(*ts)->adapt);
2784:   TSEventDestroy(&(*ts)->event);

2786:   SNESDestroy(&(*ts)->snes);
2787:   DMDestroy(&(*ts)->dm);
2788:   TSMonitorCancel((*ts));
2789:   TSAdjointMonitorCancel((*ts));

2791:   PetscHeaderDestroy(ts);
2792:   return(0);
2793: }

2795: /*@
2796:    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2797:    a TS (timestepper) context. Valid only for nonlinear problems.

2799:    Not Collective, but SNES is parallel if TS is parallel

2801:    Input Parameter:
2802: .  ts - the TS context obtained from TSCreate()

2804:    Output Parameter:
2805: .  snes - the nonlinear solver context

2807:    Notes:
2808:    The user can then directly manipulate the SNES context to set various
2809:    options, etc.  Likewise, the user can then extract and manipulate the
2810:    KSP, KSP, and PC contexts as well.

2812:    TSGetSNES() does not work for integrators that do not use SNES; in
2813:    this case TSGetSNES() returns NULL in snes.

2815:    Level: beginner

2817: .keywords: timestep, get, SNES
2818: @*/
2819: PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2820: {

2826:   if (!ts->snes) {
2827:     SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);
2828:     SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);
2829:     PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);
2830:     PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);
2831:     if (ts->dm) {SNESSetDM(ts->snes,ts->dm);}
2832:     if (ts->problem_type == TS_LINEAR) {
2833:       SNESSetType(ts->snes,SNESKSPONLY);
2834:     }
2835:   }
2836:   *snes = ts->snes;
2837:   return(0);
2838: }

2840: /*@
2841:    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context

2843:    Collective

2845:    Input Parameter:
2846: +  ts - the TS context obtained from TSCreate()
2847: -  snes - the nonlinear solver context

2849:    Notes:
2850:    Most users should have the TS created by calling TSGetSNES()

2852:    Level: developer

2854: .keywords: timestep, set, SNES
2855: @*/
2856: PetscErrorCode TSSetSNES(TS ts,SNES snes)
2857: {
2859:   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);

2864:   PetscObjectReference((PetscObject)snes);
2865:   SNESDestroy(&ts->snes);

2867:   ts->snes = snes;

2869:   SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);
2870:   SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);
2871:   if (func == SNESTSFormJacobian) {
2872:     SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);
2873:   }
2874:   return(0);
2875: }

2877: /*@
2878:    TSGetKSP - Returns the KSP (linear solver) associated with
2879:    a TS (timestepper) context.

2881:    Not Collective, but KSP is parallel if TS is parallel

2883:    Input Parameter:
2884: .  ts - the TS context obtained from TSCreate()

2886:    Output Parameter:
2887: .  ksp - the nonlinear solver context

2889:    Notes:
2890:    The user can then directly manipulate the KSP context to set various
2891:    options, etc.  Likewise, the user can then extract and manipulate the
2892:    KSP and PC contexts as well.

2894:    TSGetKSP() does not work for integrators that do not use KSP;
2895:    in this case TSGetKSP() returns NULL in ksp.

2897:    Level: beginner

2899: .keywords: timestep, get, KSP
2900: @*/
2901: PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2902: {
2904:   SNES           snes;

2909:   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2910:   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2911:   TSGetSNES(ts,&snes);
2912:   SNESGetKSP(snes,ksp);
2913:   return(0);
2914: }

2916: /* ----------- Routines to set solver parameters ---------- */

2918: /*@
2919:    TSSetMaxSteps - Sets the maximum number of steps to use.

2921:    Logically Collective on TS

2923:    Input Parameters:
2924: +  ts - the TS context obtained from TSCreate()
2925: -  maxsteps - maximum number of steps to use

2927:    Options Database Keys:
2928: .  -ts_max_steps <maxsteps> - Sets maxsteps

2930:    Notes:
2931:    The default maximum number of steps is 5000

2933:    Level: intermediate

2935: .keywords: TS, timestep, set, maximum, steps

2937: .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2938: @*/
2939: PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2940: {
2944:   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2945:   ts->max_steps = maxsteps;
2946:   return(0);
2947: }

2949: /*@
2950:    TSGetMaxSteps - Gets the maximum number of steps to use.

2952:    Not Collective

2954:    Input Parameters:
2955: .  ts - the TS context obtained from TSCreate()

2957:    Output Parameter:
2958: .  maxsteps - maximum number of steps to use

2960:    Level: advanced

2962: .keywords: TS, timestep, get, maximum, steps

2964: .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2965: @*/
2966: PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2967: {
2971:   *maxsteps = ts->max_steps;
2972:   return(0);
2973: }

2975: /*@
2976:    TSSetMaxTime - Sets the maximum (or final) time for timestepping.

2978:    Logically Collective on TS

2980:    Input Parameters:
2981: +  ts - the TS context obtained from TSCreate()
2982: -  maxtime - final time to step to

2984:    Options Database Keys:
2985: .  -ts_max_time <maxtime> - Sets maxtime

2987:    Notes:
2988:    The default maximum time is 5.0

2990:    Level: intermediate

2992: .keywords: TS, timestep, set, maximum, time

2994: .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2995: @*/
2996: PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2997: {
3001:   ts->max_time = maxtime;
3002:   return(0);
3003: }

3005: /*@
3006:    TSGetMaxTime - Gets the maximum (or final) time for timestepping.

3008:    Not Collective

3010:    Input Parameters:
3011: .  ts - the TS context obtained from TSCreate()

3013:    Output Parameter:
3014: .  maxtime - final time to step to

3016:    Level: advanced

3018: .keywords: TS, timestep, get, maximum, time

3020: .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3021: @*/
3022: PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3023: {
3027:   *maxtime = ts->max_time;
3028:   return(0);
3029: }

3031: /*@
3032:    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().

3034:    Level: deprecated

3036: @*/
3037: PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3038: {
3042:   TSSetTime(ts,initial_time);
3043:   TSSetTimeStep(ts,time_step);
3044:   return(0);
3045: }

3047: /*@
3048:    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().

3050:    Level: deprecated

3052: @*/
3053: PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3054: {
3057:   if (maxsteps) {
3059:     *maxsteps = ts->max_steps;
3060:   }
3061:   if (maxtime) {
3063:     *maxtime = ts->max_time;
3064:   }
3065:   return(0);
3066: }

3068: /*@
3069:    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().

3071:    Level: deprecated

3073: @*/
3074: PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3075: {
3080:   if (maxsteps >= 0) ts->max_steps = maxsteps;
3081:   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3082:   return(0);
3083: }

3085: /*@
3086:    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().

3088:    Level: deprecated

3090: @*/
3091: PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }

3093: /*@
3094:    TSGetTotalSteps - Deprecated, use TSGetStepNumber().

3096:    Level: deprecated

3098: @*/
3099: PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }

3101: /*@
3102:    TSSetSolution - Sets the initial solution vector
3103:    for use by the TS routines.

3105:    Logically Collective on TS and Vec

3107:    Input Parameters:
3108: +  ts - the TS context obtained from TSCreate()
3109: -  u - the solution vector

3111:    Level: beginner

3113: .keywords: TS, timestep, set, solution, initial values
3114: @*/
3115: PetscErrorCode  TSSetSolution(TS ts,Vec u)
3116: {
3118:   DM             dm;

3123:   PetscObjectReference((PetscObject)u);
3124:   VecDestroy(&ts->vec_sol);
3125:   ts->vec_sol = u;

3127:   TSGetDM(ts,&dm);
3128:   DMShellSetGlobalVector(dm,u);
3129:   return(0);
3130: }

3132: /*@
3133:    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time

3135:    Logically Collective on TS

3137:    Input Parameters:
3138: +  ts - the TS context obtained from TSCreate()
3139: .  steps - number of steps to use

3141:    Level: intermediate

3143:    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
3144:           so as to integrate back to less than the original timestep

3146: .keywords: TS, timestep, set, maximum, iterations

3148: .seealso: TSSetExactFinalTime()
3149: @*/
3150: PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
3151: {
3155:   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
3156:   if (steps > ts->steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps");
3157:   ts->adjoint_max_steps = steps;
3158:   return(0);
3159: }

3161: /*@
3162:    TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial values and w.r.t. the problem parameters
3163:       for use by the TSAdjoint routines.

3165:    Logically Collective on TS and Vec

3167:    Input Parameters:
3168: +  ts - the TS context obtained from TSCreate()
3169: .  lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector
3170: -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters

3172:    Level: beginner

3174:    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime

3176: .keywords: TS, timestep, set, sensitivity, initial values
3177: @*/
3178: PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
3179: {
3183:   ts->vecs_sensi  = lambda;
3184:   ts->vecs_sensip = mu;
3185:   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand");
3186:   ts->numcost  = numcost;
3187:   return(0);
3188: }

3190: /*@C
3191:   TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix.

3193:   Logically Collective on TS

3195:   Input Parameters:
3196: + ts   - The TS context obtained from TSCreate()
3197: - func - The function

3199:   Calling sequence of func:
3200: $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
3201: +   t - current timestep
3202: .   y - input vector (current ODE solution)
3203: .   A - output matrix
3204: -   ctx - [optional] user-defined function context

3206:   Level: intermediate

3208:   Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p

3210: .keywords: TS, sensitivity
3211: .seealso:
3212: @*/
3213: PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
3214: {


3221:   ts->rhsjacobianp    = func;
3222:   ts->rhsjacobianpctx = ctx;
3223:   if(Amat) {
3224:     PetscObjectReference((PetscObject)Amat);
3225:     MatDestroy(&ts->Jacp);
3226:     ts->Jacp = Amat;
3227:   }
3228:   return(0);
3229: }

3231: /*@C
3232:   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.

3234:   Collective on TS

3236:   Input Parameters:
3237: . ts   - The TS context obtained from TSCreate()

3239:   Level: developer

3241: .keywords: TS, sensitivity
3242: .seealso: TSAdjointSetRHSJacobian()
3243: @*/
3244: PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
3245: {


3253:   PetscStackPush("TS user JacobianP function for sensitivity analysis");
3254:   (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx);
3255:   PetscStackPop;
3256:   return(0);
3257: }

3259: /*@C
3260:     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions

3262:     Logically Collective on TS

3264:     Input Parameters:
3265: +   ts - the TS context obtained from TSCreate()
3266: .   numcost - number of gradients to be computed, this is the number of cost functions
3267: .   costintegral - vector that stores the integral values
3268: .   rf - routine for evaluating the integrand function
3269: .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3270: .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3271: .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3272: -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)

3274:     Calling sequence of rf:
3275: $   PetscErrorCode rf(TS ts,PetscReal t,Vec y,Vec f,void *ctx);

3277:     Calling sequence of drdyf:
3278: $   PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);

3280:     Calling sequence of drdpf:
3281: $   PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);

3283:     Level: intermediate

3285:     Notes: For optimization there is usually a single cost function (numcost = 1). For sensitivities there may be multiple cost functions

3287: .keywords: TS, sensitivity analysis, timestep, set, quadrature, function

3289: .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
3290: @*/
3291: PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,Vec costintegral,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3292:                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3293:                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3294:                                                           PetscBool fwd,void *ctx)
3295: {

3301:   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients() or TSForwardSetIntegralGradients()");
3302:   if (!ts->numcost) ts->numcost=numcost;

3304:   if (costintegral) {
3305:     PetscObjectReference((PetscObject)costintegral);
3306:     VecDestroy(&ts->vec_costintegral);
3307:     ts->vec_costintegral = costintegral;
3308:   } else {
3309:     if (!ts->vec_costintegral) { /* Create a seq vec if user does not provide one */
3310:       VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);
3311:     } else {
3312:       VecSet(ts->vec_costintegral,0.0);
3313:     }
3314:   }
3315:   if (!ts->vec_costintegrand) {
3316:     VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);
3317:   } else {
3318:     VecSet(ts->vec_costintegrand,0.0);
3319:   }
3320:   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
3321:   ts->costintegrand    = rf;
3322:   ts->costintegrandctx = ctx;
3323:   ts->drdyfunction     = drdyf;
3324:   ts->drdpfunction     = drdpf;
3325:   return(0);
3326: }

3328: /*@
3329:    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
3330:    It is valid to call the routine after a backward run.

3332:    Not Collective

3334:    Input Parameter:
3335: .  ts - the TS context obtained from TSCreate()

3337:    Output Parameter:
3338: .  v - the vector containing the integrals for each cost function

3340:    Level: intermediate

3342: .seealso: TSSetCostIntegrand()

3344: .keywords: TS, sensitivity analysis
3345: @*/
3346: PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
3347: {
3351:   *v = ts->vec_costintegral;
3352:   return(0);
3353: }

3355: /*@
3356:    TSComputeCostIntegrand - Evaluates the integral function in the cost functions.

3358:    Input Parameters:
3359: +  ts - the TS context
3360: .  t - current time
3361: -  y - state vector, i.e. current solution

3363:    Output Parameter:
3364: .  q - vector of size numcost to hold the outputs

3366:    Note:
3367:    Most users should not need to explicitly call this routine, as it
3368:    is used internally within the sensitivity analysis context.

3370:    Level: developer

3372: .keywords: TS, compute

3374: .seealso: TSSetCostIntegrand()
3375: @*/
3376: PetscErrorCode TSComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
3377: {


3385:   PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);
3386:   if (ts->costintegrand) {
3387:     PetscStackPush("TS user integrand in the cost function");
3388:     (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);
3389:     PetscStackPop;
3390:   } else {
3391:     VecZeroEntries(q);
3392:   }

3394:   PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);
3395:   return(0);
3396: }

3398: /*@
3399:   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.

3401:   Collective on TS

3403:   Input Parameters:
3404: . ts   - The TS context obtained from TSCreate()

3406:   Notes:
3407:   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
3408:   so most users would not generally call this routine themselves.

3410:   Level: developer

3412: .keywords: TS, sensitivity
3413: .seealso: TSAdjointComputeDRDYFunction()
3414: @*/
3415: PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
3416: {


3423:   PetscStackPush("TS user DRDY function for sensitivity analysis");
3424:   (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx);
3425:   PetscStackPop;
3426:   return(0);
3427: }

3429: /*@
3430:   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.

3432:   Collective on TS

3434:   Input Parameters:
3435: . ts   - The TS context obtained from TSCreate()

3437:   Notes:
3438:   TSDRDPFunction() is typically used for sensitivity implementation,
3439:   so most users would not generally call this routine themselves.

3441:   Level: developer

3443: .keywords: TS, sensitivity
3444: .seealso: TSAdjointSetDRDPFunction()
3445: @*/
3446: PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
3447: {


3454:   PetscStackPush("TS user DRDP function for sensitivity analysis");
3455:   (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx);
3456:   PetscStackPop;
3457:   return(0);
3458: }

3460: /*@C
3461:   TSSetPreStep - Sets the general-purpose function
3462:   called once at the beginning of each time step.

3464:   Logically Collective on TS

3466:   Input Parameters:
3467: + ts   - The TS context obtained from TSCreate()
3468: - func - The function

3470:   Calling sequence of func:
3471: . func (TS ts);

3473:   Level: intermediate

3475: .keywords: TS, timestep
3476: .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3477: @*/
3478: PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3479: {
3482:   ts->prestep = func;
3483:   return(0);
3484: }

3486: /*@
3487:   TSPreStep - Runs the user-defined pre-step function.

3489:   Collective on TS

3491:   Input Parameters:
3492: . ts   - The TS context obtained from TSCreate()

3494:   Notes:
3495:   TSPreStep() is typically used within time stepping implementations,
3496:   so most users would not generally call this routine themselves.

3498:   Level: developer

3500: .keywords: TS, timestep
3501: .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
3502: @*/
3503: PetscErrorCode  TSPreStep(TS ts)
3504: {

3509:   if (ts->prestep) {
3510:     Vec              U;
3511:     PetscObjectState sprev,spost;

3513:     TSGetSolution(ts,&U);
3514:     PetscObjectStateGet((PetscObject)U,&sprev);
3515:     PetscStackCallStandard((*ts->prestep),(ts));
3516:     PetscObjectStateGet((PetscObject)U,&spost);
3517:     if (sprev != spost) {TSRestartStep(ts);}
3518:   }
3519:   return(0);
3520: }

3522: /*@C
3523:   TSSetPreStage - Sets the general-purpose function
3524:   called once at the beginning of each stage.

3526:   Logically Collective on TS

3528:   Input Parameters:
3529: + ts   - The TS context obtained from TSCreate()
3530: - func - The function

3532:   Calling sequence of func:
3533: . PetscErrorCode func(TS ts, PetscReal stagetime);

3535:   Level: intermediate

3537:   Note:
3538:   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3539:   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3540:   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().

3542: .keywords: TS, timestep
3543: .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3544: @*/
3545: PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3546: {
3549:   ts->prestage = func;
3550:   return(0);
3551: }

3553: /*@C
3554:   TSSetPostStage - Sets the general-purpose function
3555:   called once at the end of each stage.

3557:   Logically Collective on TS

3559:   Input Parameters:
3560: + ts   - The TS context obtained from TSCreate()
3561: - func - The function

3563:   Calling sequence of func:
3564: . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);

3566:   Level: intermediate

3568:   Note:
3569:   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3570:   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3571:   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().

3573: .keywords: TS, timestep
3574: .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3575: @*/
3576: PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
3577: {
3580:   ts->poststage = func;
3581:   return(0);
3582: }

3584: /*@C
3585:   TSSetPostEvaluate - Sets the general-purpose function
3586:   called once at the end of each step evaluation.

3588:   Logically Collective on TS

3590:   Input Parameters:
3591: + ts   - The TS context obtained from TSCreate()
3592: - func - The function

3594:   Calling sequence of func:
3595: . PetscErrorCode func(TS ts);

3597:   Level: intermediate

3599:   Note:
3600:   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 
3601:   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 
3602:   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 
3603:   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 
3604:   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()

3606: .keywords: TS, timestep
3607: .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3608: @*/
3609: PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3610: {
3613:   ts->postevaluate = func;
3614:   return(0);
3615: }

3617: /*@
3618:   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()

3620:   Collective on TS

3622:   Input Parameters:
3623: . ts          - The TS context obtained from TSCreate()
3624:   stagetime   - The absolute time of the current stage

3626:   Notes:
3627:   TSPreStage() is typically used within time stepping implementations,
3628:   most users would not generally call this routine themselves.

3630:   Level: developer

3632: .keywords: TS, timestep
3633: .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3634: @*/
3635: PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3636: {

3641:   if (ts->prestage) {
3642:     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3643:   }
3644:   return(0);
3645: }

3647: /*@
3648:   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()

3650:   Collective on TS

3652:   Input Parameters:
3653: . ts          - The TS context obtained from TSCreate()
3654:   stagetime   - The absolute time of the current stage
3655:   stageindex  - Stage number
3656:   Y           - Array of vectors (of size = total number
3657:                 of stages) with the stage solutions

3659:   Notes:
3660:   TSPostStage() is typically used within time stepping implementations,
3661:   most users would not generally call this routine themselves.

3663:   Level: developer

3665: .keywords: TS, timestep
3666: .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3667: @*/
3668: PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
3669: {

3674:   if (ts->poststage) {
3675:     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
3676:   }
3677:   return(0);
3678: }

3680: /*@
3681:   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()

3683:   Collective on TS

3685:   Input Parameters:
3686: . ts          - The TS context obtained from TSCreate()

3688:   Notes:
3689:   TSPostEvaluate() is typically used within time stepping implementations,
3690:   most users would not generally call this routine themselves.

3692:   Level: developer

3694: .keywords: TS, timestep
3695: .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3696: @*/
3697: PetscErrorCode  TSPostEvaluate(TS ts)
3698: {

3703:   if (ts->postevaluate) {
3704:     Vec              U;
3705:     PetscObjectState sprev,spost;

3707:     TSGetSolution(ts,&U);
3708:     PetscObjectStateGet((PetscObject)U,&sprev);
3709:     PetscStackCallStandard((*ts->postevaluate),(ts));
3710:     PetscObjectStateGet((PetscObject)U,&spost);
3711:     if (sprev != spost) {TSRestartStep(ts);}
3712:   }
3713:   return(0);
3714: }

3716: /*@C
3717:   TSSetPostStep - Sets the general-purpose function
3718:   called once at the end of each time step.

3720:   Logically Collective on TS

3722:   Input Parameters:
3723: + ts   - The TS context obtained from TSCreate()
3724: - func - The function

3726:   Calling sequence of func:
3727: $ func (TS ts);

3729:   Notes:
3730:   The function set by TSSetPostStep() is called after each successful step. The solution vector X
3731:   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
3732:   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.

3734:   Level: intermediate

3736: .keywords: TS, timestep
3737: .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3738: @*/
3739: PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3740: {
3743:   ts->poststep = func;
3744:   return(0);
3745: }

3747: /*@
3748:   TSPostStep - Runs the user-defined post-step function.

3750:   Collective on TS

3752:   Input Parameters:
3753: . ts   - The TS context obtained from TSCreate()

3755:   Notes:
3756:   TSPostStep() is typically used within time stepping implementations,
3757:   so most users would not generally call this routine themselves.

3759:   Level: developer

3761: .keywords: TS, timestep
3762: @*/
3763: PetscErrorCode  TSPostStep(TS ts)
3764: {

3769:   if (ts->poststep) {
3770:     Vec              U;
3771:     PetscObjectState sprev,spost;

3773:     TSGetSolution(ts,&U);
3774:     PetscObjectStateGet((PetscObject)U,&sprev);
3775:     PetscStackCallStandard((*ts->poststep),(ts));
3776:     PetscObjectStateGet((PetscObject)U,&spost);
3777:     if (sprev != spost) {TSRestartStep(ts);}
3778:   }
3779:   return(0);
3780: }

3782: /* ------------ Routines to set performance monitoring options ----------- */

3784: /*@C
3785:    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3786:    timestep to display the iteration's  progress.

3788:    Logically Collective on TS

3790:    Input Parameters:
3791: +  ts - the TS context obtained from TSCreate()
3792: .  monitor - monitoring routine
3793: .  mctx - [optional] user-defined context for private data for the
3794:              monitor routine (use NULL if no context is desired)
3795: -  monitordestroy - [optional] routine that frees monitor context
3796:           (may be NULL)

3798:    Calling sequence of monitor:
3799: $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)

3801: +    ts - the TS context
3802: .    steps - iteration number (after the final time step the monitor routine may be called with a step of -1, this indicates the solution has been interpolated to this time)
3803: .    time - current time
3804: .    u - current iterate
3805: -    mctx - [optional] monitoring context

3807:    Notes:
3808:    This routine adds an additional monitor to the list of monitors that
3809:    already has been loaded.

3811:    Fortran notes: Only a single monitor function can be set for each TS object

3813:    Level: intermediate

3815: .keywords: TS, timestep, set, monitor

3817: .seealso: TSMonitorDefault(), TSMonitorCancel()
3818: @*/
3819: PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3820: {
3822:   PetscInt       i;
3823:   PetscBool      identical;
3824: 
3827:   for (i=0; i<ts->numbermonitors;i++) {
3828:     PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);
3829:     if (identical) return(0);
3830:   }
3831:   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3832:   ts->monitor[ts->numbermonitors]          = monitor;
3833:   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3834:   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3835:   return(0);
3836: }

3838: /*@C
3839:    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.

3841:    Logically Collective on TS

3843:    Input Parameters:
3844: .  ts - the TS context obtained from TSCreate()

3846:    Notes:
3847:    There is no way to remove a single, specific monitor.

3849:    Level: intermediate

3851: .keywords: TS, timestep, set, monitor

3853: .seealso: TSMonitorDefault(), TSMonitorSet()
3854: @*/
3855: PetscErrorCode  TSMonitorCancel(TS ts)
3856: {
3858:   PetscInt       i;

3862:   for (i=0; i<ts->numbermonitors; i++) {
3863:     if (ts->monitordestroy[i]) {
3864:       (*ts->monitordestroy[i])(&ts->monitorcontext[i]);
3865:     }
3866:   }
3867:   ts->numbermonitors = 0;
3868:   return(0);
3869: }

3871: /*@C
3872:    TSMonitorDefault - The Default monitor, prints the timestep and time for each step

3874:    Level: intermediate

3876: .keywords: TS, set, monitor

3878: .seealso:  TSMonitorSet()
3879: @*/
3880: PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3881: {
3883:   PetscViewer    viewer =  vf->viewer;
3884:   PetscBool      iascii,ibinary;

3888:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
3889:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);
3890:   PetscViewerPushFormat(viewer,vf->format);
3891:   if (iascii) {
3892:     PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);
3893:     if (step == -1){ /* this indicates it is an interpolated solution */
3894:       PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);
3895:     } else {
3896:       PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");
3897:     }
3898:     PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);
3899:   } else if (ibinary) {
3900:     PetscMPIInt rank;
3901:     MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
3902:     if (!rank) {
3903:       PetscBool skipHeader;
3904:       PetscInt  classid = REAL_FILE_CLASSID;

3906:       PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);
3907:       if (!skipHeader) {
3908:          PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);
3909:        }
3910:       PetscRealView(1,&ptime,viewer);
3911:     } else {
3912:       PetscRealView(0,&ptime,viewer);
3913:     }
3914:   }
3915:   PetscViewerPopFormat(viewer);
3916:   return(0);
3917: }

3919: /*@C
3920:    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
3921:    timestep to display the iteration's  progress.

3923:    Logically Collective on TS

3925:    Input Parameters:
3926: +  ts - the TS context obtained from TSCreate()
3927: .  adjointmonitor - monitoring routine
3928: .  adjointmctx - [optional] user-defined context for private data for the
3929:              monitor routine (use NULL if no context is desired)
3930: -  adjointmonitordestroy - [optional] routine that frees monitor context
3931:           (may be NULL)

3933:    Calling sequence of monitor:
3934: $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)

3936: +    ts - the TS context
3937: .    steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have
3938:                                been interpolated to)
3939: .    time - current time
3940: .    u - current iterate
3941: .    numcost - number of cost functionos
3942: .    lambda - sensitivities to initial conditions
3943: .    mu - sensitivities to parameters
3944: -    adjointmctx - [optional] adjoint monitoring context

3946:    Notes:
3947:    This routine adds an additional monitor to the list of monitors that
3948:    already has been loaded.

3950:    Fortran notes: Only a single monitor function can be set for each TS object

3952:    Level: intermediate

3954: .keywords: TS, timestep, set, adjoint, monitor

3956: .seealso: TSAdjointMonitorCancel()
3957: @*/
3958: PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
3959: {
3961:   PetscInt       i;
3962:   PetscBool      identical;

3966:   for (i=0; i<ts->numbermonitors;i++) {
3967:     PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);
3968:     if (identical) return(0);
3969:   }
3970:   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
3971:   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
3972:   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
3973:   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
3974:   return(0);
3975: }

3977: /*@C
3978:    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.

3980:    Logically Collective on TS

3982:    Input Parameters:
3983: .  ts - the TS context obtained from TSCreate()

3985:    Notes:
3986:    There is no way to remove a single, specific monitor.

3988:    Level: intermediate

3990: .keywords: TS, timestep, set, adjoint, monitor

3992: .seealso: TSAdjointMonitorSet()
3993: @*/
3994: PetscErrorCode  TSAdjointMonitorCancel(TS ts)
3995: {
3997:   PetscInt       i;

4001:   for (i=0; i<ts->numberadjointmonitors; i++) {
4002:     if (ts->adjointmonitordestroy[i]) {
4003:       (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);
4004:     }
4005:   }
4006:   ts->numberadjointmonitors = 0;
4007:   return(0);
4008: }

4010: /*@C
4011:    TSAdjointMonitorDefault - the default monitor of adjoint computations

4013:    Level: intermediate

4015: .keywords: TS, set, monitor

4017: .seealso: TSAdjointMonitorSet()
4018: @*/
4019: PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
4020: {
4022:   PetscViewer    viewer = vf->viewer;

4026:   PetscViewerPushFormat(viewer,vf->format);
4027:   PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);
4028:   PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");
4029:   PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);
4030:   PetscViewerPopFormat(viewer);
4031:   return(0);
4032: }

4034: /*@
4035:    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval

4037:    Collective on TS

4039:    Input Argument:
4040: +  ts - time stepping context
4041: -  t - time to interpolate to

4043:    Output Argument:
4044: .  U - state at given time

4046:    Level: intermediate

4048:    Developer Notes:
4049:    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.

4051: .keywords: TS, set

4053: .seealso: TSSetExactFinalTime(), TSSolve()
4054: @*/
4055: PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
4056: {

4062:   if (t < ts->ptime_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)ts->ptime_prev,(double)ts->ptime);
4063:   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
4064:   (*ts->ops->interpolate)(ts,t,U);
4065:   return(0);
4066: }

4068: /*@
4069:    TSStep - Steps one time step

4071:    Collective on TS

4073:    Input Parameter:
4074: .  ts - the TS context obtained from TSCreate()

4076:    Level: developer

4078:    Notes:
4079:    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.

4081:    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
4082:    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.

4084:    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
4085:    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.

4087: .keywords: TS, timestep, solve

4089: .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
4090: @*/
4091: PetscErrorCode  TSStep(TS ts)
4092: {
4093:   PetscErrorCode   ierr;
4094:   static PetscBool cite = PETSC_FALSE;
4095:   PetscReal        ptime;

4099:   PetscCitationsRegister("@techreport{tspaper,\n"
4100:                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
4101:                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
4102:                                 "  type        = {Preprint},\n"
4103:                                 "  number      = {ANL/MCS-P5061-0114},\n"
4104:                                 "  institution = {Argonne National Laboratory},\n"
4105:                                 "  year        = {2014}\n}\n",&cite);

4107:   TSSetUp(ts);
4108:   TSTrajectorySetUp(ts->trajectory,ts);

4110:   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
4111:   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()");
4112:   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");

4114:   if (!ts->steps) ts->ptime_prev = ts->ptime;
4115:   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
4116:   ts->reason = TS_CONVERGED_ITERATING;
4117:   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
4118:   PetscLogEventBegin(TS_Step,ts,0,0,0);
4119:   (*ts->ops->step)(ts);
4120:   PetscLogEventEnd(TS_Step,ts,0,0,0);
4121:   ts->ptime_prev = ptime;
4122:   ts->steps++;
4123:   ts->steprollback = PETSC_FALSE;
4124:   ts->steprestart  = PETSC_FALSE;

4126:   if (ts->reason < 0) {
4127:     if (ts->errorifstepfailed) {
4128:       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
4129:       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
4130:     }
4131:   } else if (!ts->reason) {
4132:     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4133:     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4134:   }
4135:   return(0);
4136: }

4138: /*@
4139:    TSAdjointStep - Steps one time step backward in the adjoint run

4141:    Collective on TS

4143:    Input Parameter:
4144: .  ts - the TS context obtained from TSCreate()

4146:    Level: intermediate

4148: .keywords: TS, adjoint, step

4150: .seealso: TSAdjointSetUp(), TSAdjointSolve()
4151: @*/
4152: PetscErrorCode  TSAdjointStep(TS ts)
4153: {
4154:   DM               dm;
4155:   PetscErrorCode   ierr;

4159:   TSGetDM(ts,&dm);
4160:   TSAdjointSetUp(ts);

4162:   VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");

4164:   ts->reason = TS_CONVERGED_ITERATING;
4165:   ts->ptime_prev = ts->ptime;
4166:   if (!ts->ops->adjointstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of  %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name);
4167:   PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);
4168:   (*ts->ops->adjointstep)(ts);
4169:   PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);
4170:   ts->adjoint_steps++; ts->steps--;

4172:   if (ts->reason < 0) {
4173:     if (ts->errorifstepfailed) {
4174:       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
4175:       else if (ts->reason == TS_DIVERGED_STEP_REJECTED) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
4176:       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
4177:     }
4178:   } else if (!ts->reason) {
4179:     if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
4180:   }
4181:   return(0);
4182: }

4184: /*@
4185:    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
4186:    at the end of a time step with a given order of accuracy.

4188:    Collective on TS

4190:    Input Arguments:
4191: +  ts - time stepping context
4192: .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
4193: -  order - optional, desired order for the error evaluation or PETSC_DECIDE

4195:    Output Arguments:
4196: +  order - optional, the actual order of the error evaluation
4197: -  wlte - the weighted local truncation error norm

4199:    Level: advanced

4201:    Notes:
4202:    If the timestepper cannot evaluate the error in a particular step
4203:    (eg. in the first step or restart steps after event handling),
4204:    this routine returns wlte=-1.0 .

4206: .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
4207: @*/
4208: PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
4209: {

4219:   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
4220:   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
4221:   (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);
4222:   return(0);
4223: }

4225: /*@
4226:    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.

4228:    Collective on TS

4230:    Input Arguments:
4231: +  ts - time stepping context
4232: .  order - desired order of accuracy
4233: -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)

4235:    Output Arguments:
4236: .  U - state at the end of the current step

4238:    Level: advanced

4240:    Notes:
4241:    This function cannot be called until all stages have been evaluated.
4242:    It is normally called by adaptive controllers before a step has been accepted and may also be called by the user after TSStep() has returned.

4244: .seealso: TSStep(), TSAdapt
4245: @*/
4246: PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
4247: {

4254:   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
4255:   (*ts->ops->evaluatestep)(ts,order,U,done);
4256:   return(0);
4257: }

4259: /*@
4260:    TSForwardCostIntegral - Evaluate the cost integral in the forward run.

4262:    Collective on TS

4264:    Input Arguments:
4265: .  ts - time stepping context

4267:    Level: advanced

4269:    Notes:
4270:    This function cannot be called until TSStep() has been completed.

4272: .seealso: TSSolve(), TSAdjointCostIntegral()
4273: @*/
4274: PetscErrorCode TSForwardCostIntegral(TS ts)
4275: {
4278:   if (!ts->ops->forwardintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the forward run",((PetscObject)ts)->type_name);
4279:   (*ts->ops->forwardintegral)(ts);
4280:   return(0);
4281: }

4283: /*@
4284:    TSSolve - Steps the requested number of timesteps.

4286:    Collective on TS

4288:    Input Parameter:
4289: +  ts - the TS context obtained from TSCreate()
4290: -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
4291:                              otherwise must contain the initial conditions and will contain the solution at the final requested time

4293:    Level: beginner

4295:    Notes:
4296:    The final time returned by this function may be different from the time of the internally
4297:    held state accessible by TSGetSolution() and TSGetTime() because the method may have
4298:    stepped over the final time.

4300: .keywords: TS, timestep, solve

4302: .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4303: @*/
4304: PetscErrorCode TSSolve(TS ts,Vec u)
4305: {
4306:   Vec               solution;
4307:   PetscErrorCode    ierr;


4313:   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && u) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
4314:     if (!ts->vec_sol || u == ts->vec_sol) {
4315:       VecDuplicate(u,&solution);
4316:       TSSetSolution(ts,solution);
4317:       VecDestroy(&solution); /* grant ownership */
4318:     }
4319:     VecCopy(u,ts->vec_sol);
4320:     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4321:   } else if (u) {
4322:     TSSetSolution(ts,u);
4323:   }
4324:   TSSetUp(ts);
4325:   TSTrajectorySetUp(ts->trajectory,ts);

4327:   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
4328:   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()");
4329:   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");

4331:   if (ts->forward_solve) {
4332:     TSForwardSetUp(ts);
4333:   }

4335:   /* reset number of steps only when the step is not restarted. ARKIMEX
4336:      restarts the step after an event. Resetting these counters in such case causes
4337:      TSTrajectory to incorrectly save the output files
4338:   */
4339:   /* reset time step and iteration counters */
4340:   if (!ts->steps) {
4341:     ts->ksp_its           = 0;
4342:     ts->snes_its          = 0;
4343:     ts->num_snes_failures = 0;
4344:     ts->reject            = 0;
4345:     ts->steprestart       = PETSC_TRUE;
4346:     ts->steprollback      = PETSC_FALSE;
4347:   }
4348:   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime;
4349:   ts->reason = TS_CONVERGED_ITERATING;

4351:   TSViewFromOptions(ts,NULL,"-ts_view_pre");

4353:   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4354:     (*ts->ops->solve)(ts);
4355:     if (u) {VecCopy(ts->vec_sol,u);}
4356:     ts->solvetime = ts->ptime;
4357:     solution = ts->vec_sol;
4358:   } else { /* Step the requested number of timesteps. */
4359:     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4360:     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;

4362:     if (!ts->steps) {
4363:       TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
4364:       TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);
4365:     }

4367:     while (!ts->reason) {
4368:       TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);
4369:       if (!ts->steprollback) {
4370:         TSPreStep(ts);
4371:       }
4372:       TSStep(ts);
4373:       if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
4374:         TSForwardCostIntegral(ts);
4375:       }
4376:       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
4377:         TSForwardStep(ts);
4378:       }
4379:       TSPostEvaluate(ts);
4380:       TSEventHandler(ts); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
4381:       if (ts->steprollback) {
4382:         TSPostEvaluate(ts);
4383:       }
4384:       if (!ts->steprollback) {
4385:         TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
4386:         TSPostStep(ts);
4387:       }
4388:     }
4389:     TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);

4391:     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
4392:       TSInterpolate(ts,ts->max_time,u);
4393:       ts->solvetime = ts->max_time;
4394:       solution = u;
4395:       TSMonitor(ts,-1,ts->solvetime,solution);
4396:     } else {
4397:       if (u) {VecCopy(ts->vec_sol,u);}
4398:       ts->solvetime = ts->ptime;
4399:       solution = ts->vec_sol;
4400:     }
4401:   }

4403:   TSViewFromOptions(ts,NULL,"-ts_view");
4404:   VecViewFromOptions(solution,NULL,"-ts_view_solution");
4405:   PetscObjectSAWsBlock((PetscObject)ts);
4406:   if (ts->adjoint_solve) {
4407:     TSAdjointSolve(ts);
4408:   }
4409:   return(0);
4410: }

4412: /*@
4413:  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4414:  
4415:  Collective on TS
4416:  
4417:  Input Arguments:
4418:  .  ts - time stepping context
4419:  
4420:  Level: advanced
4421:  
4422:  Notes:
4423:  This function cannot be called until TSAdjointStep() has been completed.
4424:  
4425:  .seealso: TSAdjointSolve(), TSAdjointStep
4426:  @*/
4427: PetscErrorCode TSAdjointCostIntegral(TS ts)
4428: {
4431:     if (!ts->ops->adjointintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the adjoint run",((PetscObject)ts)->type_name);
4432:     (*ts->ops->adjointintegral)(ts);
4433:     return(0);
4434: }

4436: /*@
4437:    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE

4439:    Collective on TS

4441:    Input Parameter:
4442: .  ts - the TS context obtained from TSCreate()

4444:    Options Database:
4445: . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial values

4447:    Level: intermediate

4449:    Notes:
4450:    This must be called after a call to TSSolve() that solves the forward problem

4452:    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time

4454: .keywords: TS, timestep, solve

4456: .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4457: @*/
4458: PetscErrorCode TSAdjointSolve(TS ts)
4459: {
4460:   PetscErrorCode    ierr;

4464:   TSAdjointSetUp(ts);

4466:   /* reset time step and iteration counters */
4467:   ts->adjoint_steps     = 0;
4468:   ts->ksp_its           = 0;
4469:   ts->snes_its          = 0;
4470:   ts->num_snes_failures = 0;
4471:   ts->reject            = 0;
4472:   ts->reason            = TS_CONVERGED_ITERATING;

4474:   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->steps;
4475:   if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;

4477:   while (!ts->reason) {
4478:     TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);
4479:     TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);
4480:     TSAdjointEventHandler(ts);
4481:     TSAdjointStep(ts);
4482:     if (ts->vec_costintegral && !ts->costintegralfwd) {
4483:       TSAdjointCostIntegral(ts);
4484:     }
4485:   }
4486:   TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);
4487:   TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);
4488:   ts->solvetime = ts->ptime;
4489:   TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");
4490:   VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");
4491:   return(0);
4492: }

4494: /*@C
4495:    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()

4497:    Collective on TS

4499:    Input Parameters:
4500: +  ts - time stepping context obtained from TSCreate()
4501: .  step - step number that has just completed
4502: .  ptime - model time of the state
4503: -  u - state at the current model time

4505:    Notes:
4506:    TSMonitor() is typically used automatically within the time stepping implementations.
4507:    Users would almost never call this routine directly.

4509:    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions

4511:    Level: developer

4513: .keywords: TS, timestep
4514: @*/
4515: PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4516: {
4517:   DM             dm;
4518:   PetscInt       i,n = ts->numbermonitors;


4525:   TSGetDM(ts,&dm);
4526:   DMSetOutputSequenceNumber(dm,step,ptime);

4528:   VecLockPush(u);
4529:   for (i=0; i<n; i++) {
4530:     (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);
4531:   }
4532:   VecLockPop(u);
4533:   return(0);
4534: }

4536: /*@C
4537:    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()

4539:    Collective on TS

4541:    Input Parameters:
4542: +  ts - time stepping context obtained from TSCreate()
4543: .  step - step number that has just completed
4544: .  ptime - model time of the state
4545: .  u - state at the current model time
4546: .  numcost - number of cost functions (dimension of lambda  or mu)
4547: .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
4548: -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters

4550:    Notes:
4551:    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
4552:    Users would almost never call this routine directly.

4554:    Level: developer

4556: .keywords: TS, timestep
4557: @*/
4558: PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
4559: {
4561:   PetscInt       i,n = ts->numberadjointmonitors;

4566:   VecLockPush(u);
4567:   for (i=0; i<n; i++) {
4568:     (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);
4569:   }
4570:   VecLockPop(u);
4571:   return(0);
4572: }

4574: /* ------------------------------------------------------------------------*/
4575: /*@C
4576:    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4577:    TS to monitor the solution process graphically in various ways

4579:    Collective on TS

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

4588:    Output Parameter:
4589: .  ctx - the context

4591:    Options Database Key:
4592: +  -ts_monitor_lg_timestep - automatically sets line graph monitor
4593: +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
4594: .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
4595: .  -ts_monitor_lg_error -  monitor the error
4596: .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
4597: .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4598: -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true

4600:    Notes:
4601:    Use TSMonitorLGCtxDestroy() to destroy.

4603:    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()

4605:    Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the
4606:    first argument (if that TS object does not have a TSMonitorLGCtx associated with it the function call is ignored) and the second takes a TSMonitorLGCtx object
4607:    as the first argument.

4609:    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()

4611:    Level: intermediate

4613: .keywords: TS, monitor, line graph, residual

4615: .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
4616:            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
4617:            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
4618:            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
4619:            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()

4621: @*/
4622: PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4623: {
4624:   PetscDraw      draw;

4628:   PetscNew(ctx);
4629:   PetscDrawCreate(comm,host,label,x,y,m,n,&draw);
4630:   PetscDrawSetFromOptions(draw);
4631:   PetscDrawLGCreate(draw,1,&(*ctx)->lg);
4632:   PetscDrawLGSetFromOptions((*ctx)->lg);
4633:   PetscDrawDestroy(&draw);
4634:   (*ctx)->howoften = howoften;
4635:   return(0);
4636: }

4638: PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4639: {
4640:   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4641:   PetscReal      x   = ptime,y;

4645:   if (step < 0) return(0); /* -1 indicates an interpolated solution */
4646:   if (!step) {
4647:     PetscDrawAxis axis;
4648:     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4649:     PetscDrawLGGetAxis(ctx->lg,&axis);
4650:     PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);
4651:     PetscDrawLGReset(ctx->lg);
4652:   }
4653:   TSGetTimeStep(ts,&y);
4654:   if (ctx->semilogy) y = PetscLog10Real(y);
4655:   PetscDrawLGAddPoint(ctx->lg,&x,&y);
4656:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
4657:     PetscDrawLGDraw(ctx->lg);
4658:     PetscDrawLGSave(ctx->lg);
4659:   }
4660:   return(0);
4661: }

4663: /*@C
4664:    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4665:    with TSMonitorLGCtxCreate().

4667:    Collective on TSMonitorLGCtx

4669:    Input Parameter:
4670: .  ctx - the monitor context

4672:    Level: intermediate

4674: .keywords: TS, monitor, line graph, destroy

4676: .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4677: @*/
4678: PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4679: {

4683:   if ((*ctx)->transformdestroy) {
4684:     ((*ctx)->transformdestroy)((*ctx)->transformctx);
4685:   }
4686:   PetscDrawLGDestroy(&(*ctx)->lg);
4687:   PetscStrArrayDestroy(&(*ctx)->names);
4688:   PetscStrArrayDestroy(&(*ctx)->displaynames);
4689:   PetscFree((*ctx)->displayvariables);
4690:   PetscFree((*ctx)->displayvalues);
4691:   PetscFree(*ctx);
4692:   return(0);
4693: }

4695: /*@
4696:    TSGetTime - Gets the time of the most recently completed step.

4698:    Not Collective

4700:    Input Parameter:
4701: .  ts - the TS context obtained from TSCreate()

4703:    Output Parameter:
4704: .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().

4706:    Level: beginner

4708:    Note:
4709:    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
4710:    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.

4712: .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()

4714: .keywords: TS, get, time
4715: @*/
4716: PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4717: {
4721:   *t = ts->ptime;
4722:   return(0);
4723: }

4725: /*@
4726:    TSGetPrevTime - Gets the starting time of the previously completed step.

4728:    Not Collective

4730:    Input Parameter:
4731: .  ts - the TS context obtained from TSCreate()

4733:    Output Parameter:
4734: .  t  - the previous time

4736:    Level: beginner

4738: .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()

4740: .keywords: TS, get, time
4741: @*/
4742: PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4743: {
4747:   *t = ts->ptime_prev;
4748:   return(0);
4749: }

4751: /*@
4752:    TSSetTime - Allows one to reset the time.

4754:    Logically Collective on TS

4756:    Input Parameters:
4757: +  ts - the TS context obtained from TSCreate()
4758: -  time - the time

4760:    Level: intermediate

4762: .seealso: TSGetTime(), TSSetMaxSteps()

4764: .keywords: TS, set, time
4765: @*/
4766: PetscErrorCode  TSSetTime(TS ts, PetscReal t)
4767: {
4771:   ts->ptime = t;
4772:   return(0);
4773: }

4775: /*@C
4776:    TSSetOptionsPrefix - Sets the prefix used for searching for all
4777:    TS options in the database.

4779:    Logically Collective on TS

4781:    Input Parameter:
4782: +  ts     - The TS context
4783: -  prefix - The prefix to prepend to all option names

4785:    Notes:
4786:    A hyphen (-) must NOT be given at the beginning of the prefix name.
4787:    The first character of all runtime options is AUTOMATICALLY the
4788:    hyphen.

4790:    Level: advanced

4792: .keywords: TS, set, options, prefix, database

4794: .seealso: TSSetFromOptions()

4796: @*/
4797: PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4798: {
4800:   SNES           snes;

4804:   PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);
4805:   TSGetSNES(ts,&snes);
4806:   SNESSetOptionsPrefix(snes,prefix);
4807:   return(0);
4808: }

4810: /*@C
4811:    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4812:    TS options in the database.

4814:    Logically Collective on TS

4816:    Input Parameter:
4817: +  ts     - The TS context
4818: -  prefix - The prefix to prepend to all option names

4820:    Notes:
4821:    A hyphen (-) must NOT be given at the beginning of the prefix name.
4822:    The first character of all runtime options is AUTOMATICALLY the
4823:    hyphen.

4825:    Level: advanced

4827: .keywords: TS, append, options, prefix, database

4829: .seealso: TSGetOptionsPrefix()

4831: @*/
4832: PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4833: {
4835:   SNES           snes;

4839:   PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);
4840:   TSGetSNES(ts,&snes);
4841:   SNESAppendOptionsPrefix(snes,prefix);
4842:   return(0);
4843: }

4845: /*@C
4846:    TSGetOptionsPrefix - Sets the prefix used for searching for all
4847:    TS options in the database.

4849:    Not Collective

4851:    Input Parameter:
4852: .  ts - The TS context

4854:    Output Parameter:
4855: .  prefix - A pointer to the prefix string used

4857:    Notes: On the fortran side, the user should pass in a string 'prifix' of
4858:    sufficient length to hold the prefix.

4860:    Level: intermediate

4862: .keywords: TS, get, options, prefix, database

4864: .seealso: TSAppendOptionsPrefix()
4865: @*/
4866: PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4867: {

4873:   PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);
4874:   return(0);
4875: }

4877: /*@C
4878:    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.

4880:    Not Collective, but parallel objects are returned if TS is parallel

4882:    Input Parameter:
4883: .  ts  - The TS context obtained from TSCreate()

4885:    Output Parameters:
4886: +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4887: .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4888: .  func - Function to compute the Jacobian of the RHS  (or NULL)
4889: -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)

4891:    Notes: You can pass in NULL for any return argument you do not need.

4893:    Level: intermediate

4895: .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()

4897: .keywords: TS, timestep, get, matrix, Jacobian
4898: @*/
4899: PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4900: {
4902:   DM             dm;

4905:   if (Amat || Pmat) {
4906:     SNES snes;
4907:     TSGetSNES(ts,&snes);
4908:     SNESSetUpMatrices(snes);
4909:     SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);
4910:   }
4911:   TSGetDM(ts,&dm);
4912:   DMTSGetRHSJacobian(dm,func,ctx);
4913:   return(0);
4914: }

4916: /*@C
4917:    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.

4919:    Not Collective, but parallel objects are returned if TS is parallel

4921:    Input Parameter:
4922: .  ts  - The TS context obtained from TSCreate()

4924:    Output Parameters:
4925: +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4926: .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
4927: .  f   - The function to compute the matrices
4928: - ctx - User-defined context for Jacobian evaluation routine

4930:    Notes: You can pass in NULL for any return argument you do not need.

4932:    Level: advanced

4934: .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()

4936: .keywords: TS, timestep, get, matrix, Jacobian
4937: @*/
4938: PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
4939: {
4941:   DM             dm;

4944:   if (Amat || Pmat) {
4945:     SNES snes;
4946:     TSGetSNES(ts,&snes);
4947:     SNESSetUpMatrices(snes);
4948:     SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);
4949:   }
4950:   TSGetDM(ts,&dm);
4951:   DMTSGetIJacobian(dm,f,ctx);
4952:   return(0);
4953: }

4955: /*@C
4956:    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
4957:    VecView() for the solution at each timestep

4959:    Collective on TS

4961:    Input Parameters:
4962: +  ts - the TS context
4963: .  step - current time-step
4964: .  ptime - current time
4965: -  dummy - either a viewer or NULL

4967:    Options Database:
4968: .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution

4970:    Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
4971:        will look bad

4973:    Level: intermediate

4975: .keywords: TS,  vector, monitor, view

4977: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4978: @*/
4979: PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4980: {
4981:   PetscErrorCode   ierr;
4982:   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4983:   PetscDraw        draw;

4986:   if (!step && ictx->showinitial) {
4987:     if (!ictx->initialsolution) {
4988:       VecDuplicate(u,&ictx->initialsolution);
4989:     }
4990:     VecCopy(u,ictx->initialsolution);
4991:   }
4992:   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) return(0);

4994:   if (ictx->showinitial) {
4995:     PetscReal pause;
4996:     PetscViewerDrawGetPause(ictx->viewer,&pause);
4997:     PetscViewerDrawSetPause(ictx->viewer,0.0);
4998:     VecView(ictx->initialsolution,ictx->viewer);
4999:     PetscViewerDrawSetPause(ictx->viewer,pause);
5000:     PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);
5001:   }
5002:   VecView(u,ictx->viewer);
5003:   if (ictx->showtimestepandtime) {
5004:     PetscReal xl,yl,xr,yr,h;
5005:     char      time[32];

5007:     PetscViewerDrawGetDraw(ictx->viewer,0,&draw);
5008:     PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);
5009:     PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
5010:     h    = yl + .95*(yr - yl);
5011:     PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);
5012:     PetscDrawFlush(draw);
5013:   }

5015:   if (ictx->showinitial) {
5016:     PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);
5017:   }
5018:   return(0);
5019: }

5021: /*@C
5022:    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
5023:    VecView() for the sensitivities to initial states at each timestep

5025:    Collective on TS

5027:    Input Parameters:
5028: +  ts - the TS context
5029: .  step - current time-step
5030: .  ptime - current time
5031: .  u - current state
5032: .  numcost - number of cost functions
5033: .  lambda - sensitivities to initial conditions
5034: .  mu - sensitivities to parameters
5035: -  dummy - either a viewer or NULL

5037:    Level: intermediate

5039: .keywords: TS,  vector, adjoint, monitor, view

5041: .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
5042: @*/
5043: PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
5044: {
5045:   PetscErrorCode   ierr;
5046:   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
5047:   PetscDraw        draw;
5048:   PetscReal        xl,yl,xr,yr,h;
5049:   char             time[32];

5052:   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) return(0);

5054:   VecView(lambda[0],ictx->viewer);
5055:   PetscViewerDrawGetDraw(ictx->viewer,0,&draw);
5056:   PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);
5057:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
5058:   h    = yl + .95*(yr - yl);
5059:   PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);
5060:   PetscDrawFlush(draw);
5061:   return(0);
5062: }

5064: /*@C
5065:    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram

5067:    Collective on TS

5069:    Input Parameters:
5070: +  ts - the TS context
5071: .  step - current time-step
5072: .  ptime - current time
5073: -  dummy - either a viewer or NULL

5075:    Level: intermediate

5077: .keywords: TS,  vector, monitor, view

5079: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5080: @*/
5081: PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5082: {
5083:   PetscErrorCode    ierr;
5084:   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
5085:   PetscDraw         draw;
5086:   PetscDrawAxis     axis;
5087:   PetscInt          n;
5088:   PetscMPIInt       size;
5089:   PetscReal         U0,U1,xl,yl,xr,yr,h;
5090:   char              time[32];
5091:   const PetscScalar *U;

5094:   MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);
5095:   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
5096:   VecGetSize(u,&n);
5097:   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");

5099:   PetscViewerDrawGetDraw(ictx->viewer,0,&draw);
5100:   PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);
5101:   PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);
5102:   if (!step) {
5103:     PetscDrawClear(draw);
5104:     PetscDrawAxisDraw(axis);
5105:   }

5107:   VecGetArrayRead(u,&U);
5108:   U0 = PetscRealPart(U[0]);
5109:   U1 = PetscRealPart(U[1]);
5110:   VecRestoreArrayRead(u,&U);
5111:   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) return(0);

5113:   PetscDrawCollectiveBegin(draw);
5114:   PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);
5115:   if (ictx->showtimestepandtime) {
5116:     PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
5117:     PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);
5118:     h    = yl + .95*(yr - yl);
5119:     PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);
5120:   }
5121:   PetscDrawCollectiveEnd(draw);
5122:   PetscDrawFlush(draw);
5123:   PetscDrawPause(draw);
5124:   PetscDrawSave(draw);
5125:   return(0);
5126: }

5128: /*@C
5129:    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()

5131:    Collective on TS

5133:    Input Parameters:
5134: .    ctx - the monitor context

5136:    Level: intermediate

5138: .keywords: TS,  vector, monitor, view

5140: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
5141: @*/
5142: PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
5143: {

5147:   PetscViewerDestroy(&(*ictx)->viewer);
5148:   VecDestroy(&(*ictx)->initialsolution);
5149:   PetscFree(*ictx);
5150:   return(0);
5151: }

5153: /*@C
5154:    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx

5156:    Collective on TS

5158:    Input Parameter:
5159: .    ts - time-step context

5161:    Output Patameter:
5162: .    ctx - the monitor context

5164:    Options Database:
5165: .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution

5167:    Level: intermediate

5169: .keywords: TS,  vector, monitor, view

5171: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
5172: @*/
5173: PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
5174: {
5175:   PetscErrorCode   ierr;

5178:   PetscNew(ctx);
5179:   PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);
5180:   PetscViewerSetFromOptions((*ctx)->viewer);

5182:   (*ctx)->howoften    = howoften;
5183:   (*ctx)->showinitial = PETSC_FALSE;
5184:   PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);

5186:   (*ctx)->showtimestepandtime = PETSC_FALSE;
5187:   PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);
5188:   return(0);
5189: }

5191: /*@C
5192:    TSMonitorDrawError - Monitors progress of the TS solvers by calling
5193:    VecView() for the error at each timestep

5195:    Collective on TS

5197:    Input Parameters:
5198: +  ts - the TS context
5199: .  step - current time-step
5200: .  ptime - current time
5201: -  dummy - either a viewer or NULL

5203:    Level: intermediate

5205: .keywords: TS,  vector, monitor, view

5207: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5208: @*/
5209: PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5210: {
5211:   PetscErrorCode   ierr;
5212:   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
5213:   PetscViewer      viewer = ctx->viewer;
5214:   Vec              work;

5217:   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) return(0);
5218:   VecDuplicate(u,&work);
5219:   TSComputeSolutionFunction(ts,ptime,work);
5220:   VecAXPY(work,-1.0,u);
5221:   VecView(work,viewer);
5222:   VecDestroy(&work);
5223:   return(0);
5224: }

5226:  #include <petsc/private/dmimpl.h>
5227: /*@
5228:    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS

5230:    Logically Collective on TS and DM

5232:    Input Parameters:
5233: +  ts - the ODE integrator object
5234: -  dm - the dm, cannot be NULL

5236:    Level: intermediate

5238: .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
5239: @*/
5240: PetscErrorCode  TSSetDM(TS ts,DM dm)
5241: {
5243:   SNES           snes;
5244:   DMTS           tsdm;

5249:   PetscObjectReference((PetscObject)dm);
5250:   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
5251:     if (ts->dm->dmts && !dm->dmts) {
5252:       DMCopyDMTS(ts->dm,dm);
5253:       DMGetDMTS(ts->dm,&tsdm);
5254:       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
5255:         tsdm->originaldm = dm;
5256:       }
5257:     }
5258:     DMDestroy(&ts->dm);
5259:   }
5260:   ts->dm = dm;

5262:   TSGetSNES(ts,&snes);
5263:   SNESSetDM(snes,dm);
5264:   return(0);
5265: }

5267: /*@
5268:    TSGetDM - Gets the DM that may be used by some preconditioners

5270:    Not Collective

5272:    Input Parameter:
5273: . ts - the preconditioner context

5275:    Output Parameter:
5276: .  dm - the dm

5278:    Level: intermediate

5280: .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
5281: @*/
5282: PetscErrorCode  TSGetDM(TS ts,DM *dm)
5283: {

5288:   if (!ts->dm) {
5289:     DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);
5290:     if (ts->snes) {SNESSetDM(ts->snes,ts->dm);}
5291:   }
5292:   *dm = ts->dm;
5293:   return(0);
5294: }

5296: /*@
5297:    SNESTSFormFunction - Function to evaluate nonlinear residual

5299:    Logically Collective on SNES

5301:    Input Parameter:
5302: + snes - nonlinear solver
5303: . U - the current state at which to evaluate the residual
5304: - ctx - user context, must be a TS

5306:    Output Parameter:
5307: . F - the nonlinear residual

5309:    Notes:
5310:    This function is not normally called by users and is automatically registered with the SNES used by TS.
5311:    It is most frequently passed to MatFDColoringSetFunction().

5313:    Level: advanced

5315: .seealso: SNESSetFunction(), MatFDColoringSetFunction()
5316: @*/
5317: PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
5318: {
5319:   TS             ts = (TS)ctx;

5327:   (ts->ops->snesfunction)(snes,U,F,ts);
5328:   return(0);
5329: }

5331: /*@
5332:    SNESTSFormJacobian - Function to evaluate the Jacobian

5334:    Collective on SNES

5336:    Input Parameter:
5337: + snes - nonlinear solver
5338: . U - the current state at which to evaluate the residual
5339: - ctx - user context, must be a TS

5341:    Output Parameter:
5342: + A - the Jacobian
5343: . B - the preconditioning matrix (may be the same as A)
5344: - flag - indicates any structure change in the matrix

5346:    Notes:
5347:    This function is not normally called by users and is automatically registered with the SNES used by TS.

5349:    Level: developer

5351: .seealso: SNESSetJacobian()
5352: @*/
5353: PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
5354: {
5355:   TS             ts = (TS)ctx;

5366:   (ts->ops->snesjacobian)(snes,U,A,B,ts);
5367:   return(0);
5368: }

5370: /*@C
5371:    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only

5373:    Collective on TS

5375:    Input Arguments:
5376: +  ts - time stepping context
5377: .  t - time at which to evaluate
5378: .  U - state at which to evaluate
5379: -  ctx - context

5381:    Output Arguments:
5382: .  F - right hand side

5384:    Level: intermediate

5386:    Notes:
5387:    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
5388:    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().

5390: .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
5391: @*/
5392: PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
5393: {
5395:   Mat            Arhs,Brhs;

5398:   TSGetRHSMats_Private(ts,&Arhs,&Brhs);
5399:   TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);
5400:   MatMult(Arhs,U,F);
5401:   return(0);
5402: }

5404: /*@C
5405:    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.

5407:    Collective on TS

5409:    Input Arguments:
5410: +  ts - time stepping context
5411: .  t - time at which to evaluate
5412: .  U - state at which to evaluate
5413: -  ctx - context

5415:    Output Arguments:
5416: +  A - pointer to operator
5417: .  B - pointer to preconditioning matrix
5418: -  flg - matrix structure flag

5420:    Level: intermediate

5422:    Notes:
5423:    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.

5425: .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
5426: @*/
5427: PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
5428: {
5430:   return(0);
5431: }

5433: /*@C
5434:    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only

5436:    Collective on TS

5438:    Input Arguments:
5439: +  ts - time stepping context
5440: .  t - time at which to evaluate
5441: .  U - state at which to evaluate
5442: .  Udot - time derivative of state vector
5443: -  ctx - context

5445:    Output Arguments:
5446: .  F - left hand side

5448:    Level: intermediate

5450:    Notes:
5451:    The assumption here is that the left hand side is of the form A*Udot (and not A*Udot + B*U). For other cases, the
5452:    user is required to write their own TSComputeIFunction.
5453:    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
5454:    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().

5456:    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U

5458: .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
5459: @*/
5460: PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
5461: {
5463:   Mat            A,B;

5466:   TSGetIJacobian(ts,&A,&B,NULL,NULL);
5467:   TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);
5468:   MatMult(A,Udot,F);
5469:   return(0);
5470: }

5472: /*@C
5473:    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE

5475:    Collective on TS

5477:    Input Arguments:
5478: +  ts - time stepping context
5479: .  t - time at which to evaluate
5480: .  U - state at which to evaluate
5481: .  Udot - time derivative of state vector
5482: .  shift - shift to apply
5483: -  ctx - context

5485:    Output Arguments:
5486: +  A - pointer to operator
5487: .  B - pointer to preconditioning matrix
5488: -  flg - matrix structure flag

5490:    Level: advanced

5492:    Notes:
5493:    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.

5495:    It is only appropriate for problems of the form

5497: $     M Udot = F(U,t)

5499:   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5500:   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5501:   an implicit operator of the form

5503: $    shift*M + J

5505:   where J is the Jacobian of -F(U).  Support may be added in a future version of PETSc, but for now, the user must store
5506:   a copy of M or reassemble it when requested.

5508: .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
5509: @*/
5510: PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
5511: {

5515:   MatScale(A, shift / ts->ijacobian.shift);
5516:   ts->ijacobian.shift = shift;
5517:   return(0);
5518: }

5520: /*@
5521:    TSGetEquationType - Gets the type of the equation that TS is solving.

5523:    Not Collective

5525:    Input Parameter:
5526: .  ts - the TS context

5528:    Output Parameter:
5529: .  equation_type - see TSEquationType

5531:    Level: beginner

5533: .keywords: TS, equation type

5535: .seealso: TSSetEquationType(), TSEquationType
5536: @*/
5537: PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5538: {
5542:   *equation_type = ts->equation_type;
5543:   return(0);
5544: }

5546: /*@
5547:    TSSetEquationType - Sets the type of the equation that TS is solving.

5549:    Not Collective

5551:    Input Parameter:
5552: +  ts - the TS context
5553: -  equation_type - see TSEquationType

5555:    Level: advanced

5557: .keywords: TS, equation type

5559: .seealso: TSGetEquationType(), TSEquationType
5560: @*/
5561: PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5562: {
5565:   ts->equation_type = equation_type;
5566:   return(0);
5567: }

5569: /*@
5570:    TSGetConvergedReason - Gets the reason the TS iteration was stopped.

5572:    Not Collective

5574:    Input Parameter:
5575: .  ts - the TS context

5577:    Output Parameter:
5578: .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5579:             manual pages for the individual convergence tests for complete lists

5581:    Level: beginner

5583:    Notes:
5584:    Can only be called after the call to TSSolve() is complete.

5586: .keywords: TS, nonlinear, set, convergence, test

5588: .seealso: TSSetConvergenceTest(), TSConvergedReason
5589: @*/
5590: PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
5591: {
5595:   *reason = ts->reason;
5596:   return(0);
5597: }

5599: /*@
5600:    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.

5602:    Not Collective

5604:    Input Parameter:
5605: +  ts - the TS context
5606: .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5607:             manual pages for the individual convergence tests for complete lists

5609:    Level: advanced

5611:    Notes:
5612:    Can only be called during TSSolve() is active.

5614: .keywords: TS, nonlinear, set, convergence, test

5616: .seealso: TSConvergedReason
5617: @*/
5618: PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5619: {
5622:   ts->reason = reason;
5623:   return(0);
5624: }

5626: /*@
5627:    TSGetSolveTime - Gets the time after a call to TSSolve()

5629:    Not Collective

5631:    Input Parameter:
5632: .  ts - the TS context

5634:    Output Parameter:
5635: .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()

5637:    Level: beginner

5639:    Notes:
5640:    Can only be called after the call to TSSolve() is complete.

5642: .keywords: TS, nonlinear, set, convergence, test

5644: .seealso: TSSetConvergenceTest(), TSConvergedReason
5645: @*/
5646: PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5647: {
5651:   *ftime = ts->solvetime;
5652:   return(0);
5653: }

5655: /*@
5656:    TSGetSNESIterations - Gets the total number of nonlinear iterations
5657:    used by the time integrator.

5659:    Not Collective

5661:    Input Parameter:
5662: .  ts - TS context

5664:    Output Parameter:
5665: .  nits - number of nonlinear iterations

5667:    Notes:
5668:    This counter is reset to zero for each successive call to TSSolve().

5670:    Level: intermediate

5672: .keywords: TS, get, number, nonlinear, iterations

5674: .seealso:  TSGetKSPIterations()
5675: @*/
5676: PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
5677: {
5681:   *nits = ts->snes_its;
5682:   return(0);
5683: }

5685: /*@
5686:    TSGetKSPIterations - Gets the total number of linear iterations
5687:    used by the time integrator.

5689:    Not Collective

5691:    Input Parameter:
5692: .  ts - TS context

5694:    Output Parameter:
5695: .  lits - number of linear iterations

5697:    Notes:
5698:    This counter is reset to zero for each successive call to TSSolve().

5700:    Level: intermediate

5702: .keywords: TS, get, number, linear, iterations

5704: .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
5705: @*/
5706: PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
5707: {
5711:   *lits = ts->ksp_its;
5712:   return(0);
5713: }

5715: /*@
5716:    TSGetStepRejections - Gets the total number of rejected steps.

5718:    Not Collective

5720:    Input Parameter:
5721: .  ts - TS context

5723:    Output Parameter:
5724: .  rejects - number of steps rejected

5726:    Notes:
5727:    This counter is reset to zero for each successive call to TSSolve().

5729:    Level: intermediate

5731: .keywords: TS, get, number

5733: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5734: @*/
5735: PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5736: {
5740:   *rejects = ts->reject;
5741:   return(0);
5742: }

5744: /*@
5745:    TSGetSNESFailures - Gets the total number of failed SNES solves

5747:    Not Collective

5749:    Input Parameter:
5750: .  ts - TS context

5752:    Output Parameter:
5753: .  fails - number of failed nonlinear solves

5755:    Notes:
5756:    This counter is reset to zero for each successive call to TSSolve().

5758:    Level: intermediate

5760: .keywords: TS, get, number

5762: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5763: @*/
5764: PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5765: {
5769:   *fails = ts->num_snes_failures;
5770:   return(0);
5771: }

5773: /*@
5774:    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails

5776:    Not Collective

5778:    Input Parameter:
5779: +  ts - TS context
5780: -  rejects - maximum number of rejected steps, pass -1 for unlimited

5782:    Notes:
5783:    The counter is reset to zero for each step

5785:    Options Database Key:
5786:  .  -ts_max_reject - Maximum number of step rejections before a step fails

5788:    Level: intermediate

5790: .keywords: TS, set, maximum, number

5792: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5793: @*/
5794: PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5795: {
5798:   ts->max_reject = rejects;
5799:   return(0);
5800: }

5802: /*@
5803:    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves

5805:    Not Collective

5807:    Input Parameter:
5808: +  ts - TS context
5809: -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited

5811:    Notes:
5812:    The counter is reset to zero for each successive call to TSSolve().

5814:    Options Database Key:
5815:  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures

5817:    Level: intermediate

5819: .keywords: TS, set, maximum, number

5821: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5822: @*/
5823: PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5824: {
5827:   ts->max_snes_failures = fails;
5828:   return(0);
5829: }

5831: /*@
5832:    TSSetErrorIfStepFails - Error if no step succeeds

5834:    Not Collective

5836:    Input Parameter:
5837: +  ts - TS context
5838: -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure

5840:    Options Database Key:
5841:  .  -ts_error_if_step_fails - Error if no step succeeds

5843:    Level: intermediate

5845: .keywords: TS, set, error

5847: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5848: @*/
5849: PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5850: {
5853:   ts->errorifstepfailed = err;
5854:   return(0);
5855: }

5857: /*@C
5858:    TSMonitorSolution - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file or a PetscDraw object

5860:    Collective on TS

5862:    Input Parameters:
5863: +  ts - the TS context
5864: .  step - current time-step
5865: .  ptime - current time
5866: .  u - current state
5867: -  vf - viewer and its format

5869:    Level: intermediate

5871: .keywords: TS,  vector, monitor, view

5873: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5874: @*/
5875: PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5876: {

5880:   PetscViewerPushFormat(vf->viewer,vf->format);
5881:   VecView(u,vf->viewer);
5882:   PetscViewerPopFormat(vf->viewer);
5883:   return(0);
5884: }

5886: /*@C
5887:    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.

5889:    Collective on TS

5891:    Input Parameters:
5892: +  ts - the TS context
5893: .  step - current time-step
5894: .  ptime - current time
5895: .  u - current state
5896: -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)

5898:    Level: intermediate

5900:    Notes:
5901:    The VTK format does not allow writing multiple time steps in the same file, therefore a different file will be written for each time step.
5902:    These are named according to the file name template.

5904:    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().

5906: .keywords: TS,  vector, monitor, view

5908: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5909: @*/
5910: PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5911: {
5913:   char           filename[PETSC_MAX_PATH_LEN];
5914:   PetscViewer    viewer;

5917:   if (step < 0) return(0); /* -1 indicates interpolated solution */
5918:   PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);
5919:   PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);
5920:   VecView(u,viewer);
5921:   PetscViewerDestroy(&viewer);
5922:   return(0);
5923: }

5925: /*@C
5926:    TSMonitorSolutionVTKDestroy - Destroy context for monitoring

5928:    Collective on TS

5930:    Input Parameters:
5931: .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)

5933:    Level: intermediate

5935:    Note:
5936:    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().

5938: .keywords: TS,  vector, monitor, view

5940: .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5941: @*/
5942: PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5943: {

5947:   PetscFree(*(char**)filenametemplate);
5948:   return(0);
5949: }

5951: /*@
5952:    TSGetAdapt - Get the adaptive controller context for the current method

5954:    Collective on TS if controller has not been created yet

5956:    Input Arguments:
5957: .  ts - time stepping context

5959:    Output Arguments:
5960: .  adapt - adaptive controller

5962:    Level: intermediate

5964: .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
5965: @*/
5966: PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
5967: {

5973:   if (!ts->adapt) {
5974:     TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);
5975:     PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);
5976:     PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);
5977:   }
5978:   *adapt = ts->adapt;
5979:   return(0);
5980: }

5982: /*@
5983:    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller

5985:    Logically Collective

5987:    Input Arguments:
5988: +  ts - time integration context
5989: .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
5990: .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
5991: .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
5992: -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present

5994:    Options Database keys:
5995: +  -ts_rtol <rtol> - relative tolerance for local truncation error
5996: -  -ts_atol <atol> Absolute tolerance for local truncation error

5998:    Notes:
5999:    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
6000:    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
6001:    computed only for the differential or the algebraic part then this can be done using the vector of
6002:    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
6003:    differential part and infinity for the algebraic part, the LTE calculation will include only the
6004:    differential variables.

6006:    Level: beginner

6008: .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
6009: @*/
6010: PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
6011: {

6015:   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
6016:   if (vatol) {
6017:     PetscObjectReference((PetscObject)vatol);
6018:     VecDestroy(&ts->vatol);
6019:     ts->vatol = vatol;
6020:   }
6021:   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
6022:   if (vrtol) {
6023:     PetscObjectReference((PetscObject)vrtol);
6024:     VecDestroy(&ts->vrtol);
6025:     ts->vrtol = vrtol;
6026:   }
6027:   return(0);
6028: }

6030: /*@
6031:    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller

6033:    Logically Collective

6035:    Input Arguments:
6036: .  ts - time integration context

6038:    Output Arguments:
6039: +  atol - scalar absolute tolerances, NULL to ignore
6040: .  vatol - vector of absolute tolerances, NULL to ignore
6041: .  rtol - scalar relative tolerances, NULL to ignore
6042: -  vrtol - vector of relative tolerances, NULL to ignore

6044:    Level: beginner

6046: .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
6047: @*/
6048: PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
6049: {
6051:   if (atol)  *atol  = ts->atol;
6052:   if (vatol) *vatol = ts->vatol;
6053:   if (rtol)  *rtol  = ts->rtol;
6054:   if (vrtol) *vrtol = ts->vrtol;
6055:   return(0);
6056: }

6058: /*@
6059:    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors

6061:    Collective on TS

6063:    Input Arguments:
6064: +  ts - time stepping context
6065: .  U - state vector, usually ts->vec_sol
6066: -  Y - state vector to be compared to U

6068:    Output Arguments:
6069: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
6070: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6071: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

6073:    Level: developer

6075: .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
6076: @*/
6077: PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6078: {
6079:   PetscErrorCode    ierr;
6080:   PetscInt          i,n,N,rstart;
6081:   PetscInt          n_loc,na_loc,nr_loc;
6082:   PetscReal         n_glb,na_glb,nr_glb;
6083:   const PetscScalar *u,*y;
6084:   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
6085:   PetscReal         tol,tola,tolr;
6086:   PetscReal         err_loc[6],err_glb[6];

6098:   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");

6100:   VecGetSize(U,&N);
6101:   VecGetLocalSize(U,&n);
6102:   VecGetOwnershipRange(U,&rstart,NULL);
6103:   VecGetArrayRead(U,&u);
6104:   VecGetArrayRead(Y,&y);
6105:   sum  = 0.; n_loc  = 0;
6106:   suma = 0.; na_loc = 0;
6107:   sumr = 0.; nr_loc = 0;
6108:   if (ts->vatol && ts->vrtol) {
6109:     const PetscScalar *atol,*rtol;
6110:     VecGetArrayRead(ts->vatol,&atol);
6111:     VecGetArrayRead(ts->vrtol,&rtol);
6112:     for (i=0; i<n; i++) {
6113:       diff = PetscAbsScalar(y[i] - u[i]);
6114:       tola = PetscRealPart(atol[i]);
6115:       if(tola>0.){
6116:         suma  += PetscSqr(diff/tola);
6117:         na_loc++;
6118:       }
6119:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6120:       if(tolr>0.){
6121:         sumr  += PetscSqr(diff/tolr);
6122:         nr_loc++;
6123:       }
6124:       tol=tola+tolr;
6125:       if(tol>0.){
6126:         sum  += PetscSqr(diff/tol);
6127:         n_loc++;
6128:       }
6129:     }
6130:     VecRestoreArrayRead(ts->vatol,&atol);
6131:     VecRestoreArrayRead(ts->vrtol,&rtol);
6132:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
6133:     const PetscScalar *atol;
6134:     VecGetArrayRead(ts->vatol,&atol);
6135:     for (i=0; i<n; i++) {
6136:       diff = PetscAbsScalar(y[i] - u[i]);
6137:       tola = PetscRealPart(atol[i]);
6138:       if(tola>0.){
6139:         suma  += PetscSqr(diff/tola);
6140:         na_loc++;
6141:       }
6142:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6143:       if(tolr>0.){
6144:         sumr  += PetscSqr(diff/tolr);
6145:         nr_loc++;
6146:       }
6147:       tol=tola+tolr;
6148:       if(tol>0.){
6149:         sum  += PetscSqr(diff/tol);
6150:         n_loc++;
6151:       }
6152:     }
6153:     VecRestoreArrayRead(ts->vatol,&atol);
6154:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
6155:     const PetscScalar *rtol;
6156:     VecGetArrayRead(ts->vrtol,&rtol);
6157:     for (i=0; i<n; i++) {
6158:       diff = PetscAbsScalar(y[i] - u[i]);
6159:       tola = ts->atol;
6160:       if(tola>0.){
6161:         suma  += PetscSqr(diff/tola);
6162:         na_loc++;
6163:       }
6164:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6165:       if(tolr>0.){
6166:         sumr  += PetscSqr(diff/tolr);
6167:         nr_loc++;
6168:       }
6169:       tol=tola+tolr;
6170:       if(tol>0.){
6171:         sum  += PetscSqr(diff/tol);
6172:         n_loc++;
6173:       }
6174:     }
6175:     VecRestoreArrayRead(ts->vrtol,&rtol);
6176:   } else {                      /* scalar atol, scalar rtol */
6177:     for (i=0; i<n; i++) {
6178:       diff = PetscAbsScalar(y[i] - u[i]);
6179:      tola = ts->atol;
6180:       if(tola>0.){
6181:         suma  += PetscSqr(diff/tola);
6182:         na_loc++;
6183:       }
6184:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6185:       if(tolr>0.){
6186:         sumr  += PetscSqr(diff/tolr);
6187:         nr_loc++;
6188:       }
6189:       tol=tola+tolr;
6190:       if(tol>0.){
6191:         sum  += PetscSqr(diff/tol);
6192:         n_loc++;
6193:       }
6194:     }
6195:   }
6196:   VecRestoreArrayRead(U,&u);
6197:   VecRestoreArrayRead(Y,&y);

6199:   err_loc[0] = sum;
6200:   err_loc[1] = suma;
6201:   err_loc[2] = sumr;
6202:   err_loc[3] = (PetscReal)n_loc;
6203:   err_loc[4] = (PetscReal)na_loc;
6204:   err_loc[5] = (PetscReal)nr_loc;

6206:   MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));

6208:   gsum   = err_glb[0];
6209:   gsuma  = err_glb[1];
6210:   gsumr  = err_glb[2];
6211:   n_glb  = err_glb[3];
6212:   na_glb = err_glb[4];
6213:   nr_glb = err_glb[5];

6215:   *norm  = 0.;
6216:   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
6217:   *norma = 0.;
6218:   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
6219:   *normr = 0.;
6220:   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}

6222:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
6223:   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
6224:   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
6225:   return(0);
6226: }

6228: /*@
6229:    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors

6231:    Collective on TS

6233:    Input Arguments:
6234: +  ts - time stepping context
6235: .  U - state vector, usually ts->vec_sol
6236: -  Y - state vector to be compared to U

6238:    Output Arguments:
6239: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
6240: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6241: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

6243:    Level: developer

6245: .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
6246: @*/
6247: PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6248: {
6249:   PetscErrorCode    ierr;
6250:   PetscInt          i,n,N,rstart;
6251:   const PetscScalar *u,*y;
6252:   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
6253:   PetscReal         tol,tola,tolr,diff;
6254:   PetscReal         err_loc[3],err_glb[3];
6255: 
6266:   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");

6268:   VecGetSize(U,&N);
6269:   VecGetLocalSize(U,&n);
6270:   VecGetOwnershipRange(U,&rstart,NULL);
6271:   VecGetArrayRead(U,&u);
6272:   VecGetArrayRead(Y,&y);

6274:   max=0.;
6275:   maxa=0.;
6276:   maxr=0.;

6278:   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
6279:     const PetscScalar *atol,*rtol;
6280:     VecGetArrayRead(ts->vatol,&atol);
6281:     VecGetArrayRead(ts->vrtol,&rtol);

6283:     for (i=0; i<n; i++) {
6284:       diff = PetscAbsScalar(y[i] - u[i]);
6285:       tola = PetscRealPart(atol[i]);
6286:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6287:       tol  = tola+tolr;
6288:       if(tola>0.){
6289:         maxa = PetscMax(maxa,diff / tola);
6290:       }
6291:       if(tolr>0.){
6292:         maxr = PetscMax(maxr,diff / tolr);
6293:       }
6294:       if(tol>0.){
6295:         max = PetscMax(max,diff / tol);
6296:       }
6297:     }
6298:     VecRestoreArrayRead(ts->vatol,&atol);
6299:     VecRestoreArrayRead(ts->vrtol,&rtol);
6300:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
6301:     const PetscScalar *atol;
6302:     VecGetArrayRead(ts->vatol,&atol);
6303:     for (i=0; i<n; i++) {
6304:       diff = PetscAbsScalar(y[i] - u[i]);
6305:       tola = PetscRealPart(atol[i]);
6306:       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6307:       tol  = tola+tolr;
6308:       if(tola>0.){
6309:         maxa = PetscMax(maxa,diff / tola);
6310:       }
6311:       if(tolr>0.){
6312:         maxr = PetscMax(maxr,diff / tolr);
6313:       }
6314:       if(tol>0.){
6315:         max = PetscMax(max,diff / tol);
6316:       }
6317:     }
6318:     VecRestoreArrayRead(ts->vatol,&atol);
6319:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
6320:     const PetscScalar *rtol;
6321:     VecGetArrayRead(ts->vrtol,&rtol);

6323:     for (i=0; i<n; i++) {
6324:       diff = PetscAbsScalar(y[i] - u[i]);
6325:       tola = ts->atol;
6326:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6327:       tol  = tola+tolr;
6328:       if(tola>0.){
6329:         maxa = PetscMax(maxa,diff / tola);
6330:       }
6331:       if(tolr>0.){
6332:         maxr = PetscMax(maxr,diff / tolr);
6333:       }
6334:       if(tol>0.){
6335:         max = PetscMax(max,diff / tol);
6336:       }
6337:     }
6338:     VecRestoreArrayRead(ts->vrtol,&rtol);
6339:   } else {                      /* scalar atol, scalar rtol */

6341:     for (i=0; i<n; i++) {
6342:       diff = PetscAbsScalar(y[i] - u[i]);
6343:       tola = ts->atol;
6344:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6345:       tol  = tola+tolr;
6346:       if(tola>0.){
6347:         maxa = PetscMax(maxa,diff / tola);
6348:       }
6349:       if(tolr>0.){
6350:         maxr = PetscMax(maxr,diff / tolr);
6351:       }
6352:       if(tol>0.){
6353:         max = PetscMax(max,diff / tol);
6354:       }
6355:     }
6356:   }
6357:   VecRestoreArrayRead(U,&u);
6358:   VecRestoreArrayRead(Y,&y);
6359:   err_loc[0] = max;
6360:   err_loc[1] = maxa;
6361:   err_loc[2] = maxr;
6362:   MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));
6363:   gmax   = err_glb[0];
6364:   gmaxa  = err_glb[1];
6365:   gmaxr  = err_glb[2];

6367:   *norm = gmax;
6368:   *norma = gmaxa;
6369:   *normr = gmaxr;
6370:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
6371:     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
6372:     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
6373:   return(0);
6374: }

6376: /*@
6377:    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances

6379:    Collective on TS

6381:    Input Arguments:
6382: +  ts - time stepping context
6383: .  U - state vector, usually ts->vec_sol
6384: .  Y - state vector to be compared to U
6385: -  wnormtype - norm type, either NORM_2 or NORM_INFINITY

6387:    Output Arguments:
6388: .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
6389: .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6390: .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user

6392:    Options Database Keys:
6393: .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY

6395:    Level: developer

6397: .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
6398: @*/
6399: PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6400: {

6404:   if (wnormtype == NORM_2) {
6405:     TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);
6406:   } else if(wnormtype == NORM_INFINITY) {
6407:     TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);
6408:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
6409:   return(0);
6410: }


6413: /*@
6414:    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances

6416:    Collective on TS

6418:    Input Arguments:
6419: +  ts - time stepping context
6420: .  E - error vector
6421: .  U - state vector, usually ts->vec_sol
6422: -  Y - state vector, previous time step

6424:    Output Arguments:
6425: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
6426: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6427: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

6429:    Level: developer

6431: .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
6432: @*/
6433: PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6434: {
6435:   PetscErrorCode    ierr;
6436:   PetscInt          i,n,N,rstart;
6437:   PetscInt          n_loc,na_loc,nr_loc;
6438:   PetscReal         n_glb,na_glb,nr_glb;
6439:   const PetscScalar *e,*u,*y;
6440:   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
6441:   PetscReal         tol,tola,tolr;
6442:   PetscReal         err_loc[6],err_glb[6];


6458:   VecGetSize(E,&N);
6459:   VecGetLocalSize(E,&n);
6460:   VecGetOwnershipRange(E,&rstart,NULL);
6461:   VecGetArrayRead(E,&e);
6462:   VecGetArrayRead(U,&u);
6463:   VecGetArrayRead(Y,&y);
6464:   sum  = 0.; n_loc  = 0;
6465:   suma = 0.; na_loc = 0;
6466:   sumr = 0.; nr_loc = 0;
6467:   if (ts->vatol && ts->vrtol) {
6468:     const PetscScalar *atol,*rtol;
6469:     VecGetArrayRead(ts->vatol,&atol);
6470:     VecGetArrayRead(ts->vrtol,&rtol);
6471:     for (i=0; i<n; i++) {
6472:       err = PetscAbsScalar(e[i]);
6473:       tola = PetscRealPart(atol[i]);
6474:       if(tola>0.){
6475:         suma  += PetscSqr(err/tola);
6476:         na_loc++;
6477:       }
6478:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6479:       if(tolr>0.){
6480:         sumr  += PetscSqr(err/tolr);
6481:         nr_loc++;
6482:       }
6483:       tol=tola+tolr;
6484:       if(tol>0.){
6485:         sum  += PetscSqr(err/tol);
6486:         n_loc++;
6487:       }
6488:     }
6489:     VecRestoreArrayRead(ts->vatol,&atol);
6490:     VecRestoreArrayRead(ts->vrtol,&rtol);
6491:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
6492:     const PetscScalar *atol;
6493:     VecGetArrayRead(ts->vatol,&atol);
6494:     for (i=0; i<n; i++) {
6495:       err = PetscAbsScalar(e[i]);
6496:       tola = PetscRealPart(atol[i]);
6497:       if(tola>0.){
6498:         suma  += PetscSqr(err/tola);
6499:         na_loc++;
6500:       }
6501:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6502:       if(tolr>0.){
6503:         sumr  += PetscSqr(err/tolr);
6504:         nr_loc++;
6505:       }
6506:       tol=tola+tolr;
6507:       if(tol>0.){
6508:         sum  += PetscSqr(err/tol);
6509:         n_loc++;
6510:       }
6511:     }
6512:     VecRestoreArrayRead(ts->vatol,&atol);
6513:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
6514:     const PetscScalar *rtol;
6515:     VecGetArrayRead(ts->vrtol,&rtol);
6516:     for (i=0; i<n; i++) {
6517:       err = PetscAbsScalar(e[i]);
6518:       tola = ts->atol;
6519:       if(tola>0.){
6520:         suma  += PetscSqr(err/tola);
6521:         na_loc++;
6522:       }
6523:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6524:       if(tolr>0.){
6525:         sumr  += PetscSqr(err/tolr);
6526:         nr_loc++;
6527:       }
6528:       tol=tola+tolr;
6529:       if(tol>0.){
6530:         sum  += PetscSqr(err/tol);
6531:         n_loc++;
6532:       }
6533:     }
6534:     VecRestoreArrayRead(ts->vrtol,&rtol);
6535:   } else {                      /* scalar atol, scalar rtol */
6536:     for (i=0; i<n; i++) {
6537:       err = PetscAbsScalar(e[i]);
6538:      tola = ts->atol;
6539:       if(tola>0.){
6540:         suma  += PetscSqr(err/tola);
6541:         na_loc++;
6542:       }
6543:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6544:       if(tolr>0.){
6545:         sumr  += PetscSqr(err/tolr);
6546:         nr_loc++;
6547:       }
6548:       tol=tola+tolr;
6549:       if(tol>0.){
6550:         sum  += PetscSqr(err/tol);
6551:         n_loc++;
6552:       }
6553:     }
6554:   }
6555:   VecRestoreArrayRead(E,&e);
6556:   VecRestoreArrayRead(U,&u);
6557:   VecRestoreArrayRead(Y,&y);

6559:   err_loc[0] = sum;
6560:   err_loc[1] = suma;
6561:   err_loc[2] = sumr;
6562:   err_loc[3] = (PetscReal)n_loc;
6563:   err_loc[4] = (PetscReal)na_loc;
6564:   err_loc[5] = (PetscReal)nr_loc;

6566:   MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));

6568:   gsum   = err_glb[0];
6569:   gsuma  = err_glb[1];
6570:   gsumr  = err_glb[2];
6571:   n_glb  = err_glb[3];
6572:   na_glb = err_glb[4];
6573:   nr_glb = err_glb[5];

6575:   *norm  = 0.;
6576:   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
6577:   *norma = 0.;
6578:   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
6579:   *normr = 0.;
6580:   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}

6582:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
6583:   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
6584:   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
6585:   return(0);
6586: }

6588: /*@
6589:    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
6590:    Collective on TS

6592:    Input Arguments:
6593: +  ts - time stepping context
6594: .  E - error vector
6595: .  U - state vector, usually ts->vec_sol
6596: -  Y - state vector, previous time step

6598:    Output Arguments:
6599: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
6600: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6601: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

6603:    Level: developer

6605: .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
6606: @*/
6607: PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6608: {
6609:   PetscErrorCode    ierr;
6610:   PetscInt          i,n,N,rstart;
6611:   const PetscScalar *e,*u,*y;
6612:   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
6613:   PetscReal         tol,tola,tolr;
6614:   PetscReal         err_loc[3],err_glb[3];


6630:   VecGetSize(E,&N);
6631:   VecGetLocalSize(E,&n);
6632:   VecGetOwnershipRange(E,&rstart,NULL);
6633:   VecGetArrayRead(E,&e);
6634:   VecGetArrayRead(U,&u);
6635:   VecGetArrayRead(Y,&y);

6637:   max=0.;
6638:   maxa=0.;
6639:   maxr=0.;

6641:   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
6642:     const PetscScalar *atol,*rtol;
6643:     VecGetArrayRead(ts->vatol,&atol);
6644:     VecGetArrayRead(ts->vrtol,&rtol);

6646:     for (i=0; i<n; i++) {
6647:       err = PetscAbsScalar(e[i]);
6648:       tola = PetscRealPart(atol[i]);
6649:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6650:       tol  = tola+tolr;
6651:       if(tola>0.){
6652:         maxa = PetscMax(maxa,err / tola);
6653:       }
6654:       if(tolr>0.){
6655:         maxr = PetscMax(maxr,err / tolr);
6656:       }
6657:       if(tol>0.){
6658:         max = PetscMax(max,err / tol);
6659:       }
6660:     }
6661:     VecRestoreArrayRead(ts->vatol,&atol);
6662:     VecRestoreArrayRead(ts->vrtol,&rtol);
6663:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
6664:     const PetscScalar *atol;
6665:     VecGetArrayRead(ts->vatol,&atol);
6666:     for (i=0; i<n; i++) {
6667:       err = PetscAbsScalar(e[i]);
6668:       tola = PetscRealPart(atol[i]);
6669:       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6670:       tol  = tola+tolr;
6671:       if(tola>0.){
6672:         maxa = PetscMax(maxa,err / tola);
6673:       }
6674:       if(tolr>0.){
6675:         maxr = PetscMax(maxr,err / tolr);
6676:       }
6677:       if(tol>0.){
6678:         max = PetscMax(max,err / tol);
6679:       }
6680:     }
6681:     VecRestoreArrayRead(ts->vatol,&atol);
6682:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
6683:     const PetscScalar *rtol;
6684:     VecGetArrayRead(ts->vrtol,&rtol);

6686:     for (i=0; i<n; i++) {
6687:       err = PetscAbsScalar(e[i]);
6688:       tola = ts->atol;
6689:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6690:       tol  = tola+tolr;
6691:       if(tola>0.){
6692:         maxa = PetscMax(maxa,err / tola);
6693:       }
6694:       if(tolr>0.){
6695:         maxr = PetscMax(maxr,err / tolr);
6696:       }
6697:       if(tol>0.){
6698:         max = PetscMax(max,err / tol);
6699:       }
6700:     }
6701:     VecRestoreArrayRead(ts->vrtol,&rtol);
6702:   } else {                      /* scalar atol, scalar rtol */

6704:     for (i=0; i<n; i++) {
6705:       err = PetscAbsScalar(e[i]);
6706:       tola = ts->atol;
6707:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6708:       tol  = tola+tolr;
6709:       if(tola>0.){
6710:         maxa = PetscMax(maxa,err / tola);
6711:       }
6712:       if(tolr>0.){
6713:         maxr = PetscMax(maxr,err / tolr);
6714:       }
6715:       if(tol>0.){
6716:         max = PetscMax(max,err / tol);
6717:       }
6718:     }
6719:   }
6720:   VecRestoreArrayRead(E,&e);
6721:   VecRestoreArrayRead(U,&u);
6722:   VecRestoreArrayRead(Y,&y);
6723:   err_loc[0] = max;
6724:   err_loc[1] = maxa;
6725:   err_loc[2] = maxr;
6726:   MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));
6727:   gmax   = err_glb[0];
6728:   gmaxa  = err_glb[1];
6729:   gmaxr  = err_glb[2];

6731:   *norm = gmax;
6732:   *norma = gmaxa;
6733:   *normr = gmaxr;
6734:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
6735:     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
6736:     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
6737:   return(0);
6738: }

6740: /*@
6741:    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances

6743:    Collective on TS

6745:    Input Arguments:
6746: +  ts - time stepping context
6747: .  E - error vector
6748: .  U - state vector, usually ts->vec_sol
6749: .  Y - state vector, previous time step
6750: -  wnormtype - norm type, either NORM_2 or NORM_INFINITY

6752:    Output Arguments:
6753: .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
6754: .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6755: .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user

6757:    Options Database Keys:
6758: .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY

6760:    Level: developer

6762: .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
6763: @*/
6764: PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6765: {

6769:   if (wnormtype == NORM_2) {
6770:     TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);
6771:   } else if(wnormtype == NORM_INFINITY) {
6772:     TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);
6773:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
6774:   return(0);
6775: }


6778: /*@
6779:    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler

6781:    Logically Collective on TS

6783:    Input Arguments:
6784: +  ts - time stepping context
6785: -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)

6787:    Note:
6788:    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()

6790:    Level: intermediate

6792: .seealso: TSGetCFLTime(), TSADAPTCFL
6793: @*/
6794: PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
6795: {
6798:   ts->cfltime_local = cfltime;
6799:   ts->cfltime       = -1.;
6800:   return(0);
6801: }

6803: /*@
6804:    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler

6806:    Collective on TS

6808:    Input Arguments:
6809: .  ts - time stepping context

6811:    Output Arguments:
6812: .  cfltime - maximum stable time step for forward Euler

6814:    Level: advanced

6816: .seealso: TSSetCFLTimeLocal()
6817: @*/
6818: PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
6819: {

6823:   if (ts->cfltime < 0) {
6824:     MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));
6825:   }
6826:   *cfltime = ts->cfltime;
6827:   return(0);
6828: }

6830: /*@
6831:    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu

6833:    Input Parameters:
6834: .  ts   - the TS context.
6835: .  xl   - lower bound.
6836: .  xu   - upper bound.

6838:    Notes:
6839:    If this routine is not called then the lower and upper bounds are set to
6840:    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().

6842:    Level: advanced

6844: @*/
6845: PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6846: {
6848:   SNES           snes;

6851:   TSGetSNES(ts,&snes);
6852:   SNESVISetVariableBounds(snes,xl,xu);
6853:   return(0);
6854: }

6856: #if defined(PETSC_HAVE_MATLAB_ENGINE)
6857: #include <mex.h>

6859: typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;

6861: /*
6862:    TSComputeFunction_Matlab - Calls the function that has been set with
6863:                          TSSetFunctionMatlab().

6865:    Collective on TS

6867:    Input Parameters:
6868: +  snes - the TS context
6869: -  u - input vector

6871:    Output Parameter:
6872: .  y - function vector, as set by TSSetFunction()

6874:    Notes:
6875:    TSComputeFunction() is typically used within nonlinear solvers
6876:    implementations, so most users would not generally call this routine
6877:    themselves.

6879:    Level: developer

6881: .keywords: TS, nonlinear, compute, function

6883: .seealso: TSSetFunction(), TSGetFunction()
6884: */
6885: PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6886: {
6887:   PetscErrorCode  ierr;
6888:   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6889:   int             nlhs  = 1,nrhs = 7;
6890:   mxArray         *plhs[1],*prhs[7];
6891:   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;


6901:   PetscMemcpy(&ls,&snes,sizeof(snes));
6902:   PetscMemcpy(&lx,&u,sizeof(u));
6903:   PetscMemcpy(&lxdot,&udot,sizeof(udot));
6904:   PetscMemcpy(&ly,&y,sizeof(u));

6906:   prhs[0] =  mxCreateDoubleScalar((double)ls);
6907:   prhs[1] =  mxCreateDoubleScalar(time);
6908:   prhs[2] =  mxCreateDoubleScalar((double)lx);
6909:   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6910:   prhs[4] =  mxCreateDoubleScalar((double)ly);
6911:   prhs[5] =  mxCreateString(sctx->funcname);
6912:   prhs[6] =  sctx->ctx;
6913:    mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");
6914:    mxGetScalar(plhs[0]);
6915:   mxDestroyArray(prhs[0]);
6916:   mxDestroyArray(prhs[1]);
6917:   mxDestroyArray(prhs[2]);
6918:   mxDestroyArray(prhs[3]);
6919:   mxDestroyArray(prhs[4]);
6920:   mxDestroyArray(prhs[5]);
6921:   mxDestroyArray(plhs[0]);
6922:   return(0);
6923: }

6925: /*
6926:    TSSetFunctionMatlab - Sets the function evaluation routine and function
6927:    vector for use by the TS routines in solving ODEs
6928:    equations from MATLAB. Here the function is a string containing the name of a MATLAB function

6930:    Logically Collective on TS

6932:    Input Parameters:
6933: +  ts - the TS context
6934: -  func - function evaluation routine

6936:    Calling sequence of func:
6937: $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);

6939:    Level: beginner

6941: .keywords: TS, nonlinear, set, function

6943: .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6944: */
6945: PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6946: {
6947:   PetscErrorCode  ierr;
6948:   TSMatlabContext *sctx;

6951:   /* currently sctx is memory bleed */
6952:   PetscNew(&sctx);
6953:   PetscStrallocpy(func,&sctx->funcname);
6954:   /*
6955:      This should work, but it doesn't
6956:   sctx->ctx = ctx;
6957:   mexMakeArrayPersistent(sctx->ctx);
6958:   */
6959:   sctx->ctx = mxDuplicateArray(ctx);

6961:   TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);
6962:   return(0);
6963: }

6965: /*
6966:    TSComputeJacobian_Matlab - Calls the function that has been set with
6967:                          TSSetJacobianMatlab().

6969:    Collective on TS

6971:    Input Parameters:
6972: +  ts - the TS context
6973: .  u - input vector
6974: .  A, B - the matrices
6975: -  ctx - user context

6977:    Level: developer

6979: .keywords: TS, nonlinear, compute, function

6981: .seealso: TSSetFunction(), TSGetFunction()
6982: @*/
6983: PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6984: {
6985:   PetscErrorCode  ierr;
6986:   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6987:   int             nlhs  = 2,nrhs = 9;
6988:   mxArray         *plhs[2],*prhs[9];
6989:   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;


6995:   /* call Matlab function in ctx with arguments u and y */

6997:   PetscMemcpy(&ls,&ts,sizeof(ts));
6998:   PetscMemcpy(&lx,&u,sizeof(u));
6999:   PetscMemcpy(&lxdot,&udot,sizeof(u));
7000:   PetscMemcpy(&lA,A,sizeof(u));
7001:   PetscMemcpy(&lB,B,sizeof(u));

7003:   prhs[0] =  mxCreateDoubleScalar((double)ls);
7004:   prhs[1] =  mxCreateDoubleScalar((double)time);
7005:   prhs[2] =  mxCreateDoubleScalar((double)lx);
7006:   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
7007:   prhs[4] =  mxCreateDoubleScalar((double)shift);
7008:   prhs[5] =  mxCreateDoubleScalar((double)lA);
7009:   prhs[6] =  mxCreateDoubleScalar((double)lB);
7010:   prhs[7] =  mxCreateString(sctx->funcname);
7011:   prhs[8] =  sctx->ctx;
7012:    mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");
7013:    mxGetScalar(plhs[0]);
7014:   mxDestroyArray(prhs[0]);
7015:   mxDestroyArray(prhs[1]);
7016:   mxDestroyArray(prhs[2]);
7017:   mxDestroyArray(prhs[3]);
7018:   mxDestroyArray(prhs[4]);
7019:   mxDestroyArray(prhs[5]);
7020:   mxDestroyArray(prhs[6]);
7021:   mxDestroyArray(prhs[7]);
7022:   mxDestroyArray(plhs[0]);
7023:   mxDestroyArray(plhs[1]);
7024:   return(0);
7025: }

7027: /*
7028:    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
7029:    vector for use by the TS routines in solving ODEs from MATLAB. Here the function is a string containing the name of a MATLAB function

7031:    Logically Collective on TS

7033:    Input Parameters:
7034: +  ts - the TS context
7035: .  A,B - Jacobian matrices
7036: .  func - function evaluation routine
7037: -  ctx - user context

7039:    Calling sequence of func:
7040: $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);

7042:    Level: developer

7044: .keywords: TS, nonlinear, set, function

7046: .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7047: */
7048: PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
7049: {
7050:   PetscErrorCode  ierr;
7051:   TSMatlabContext *sctx;

7054:   /* currently sctx is memory bleed */
7055:   PetscNew(&sctx);
7056:   PetscStrallocpy(func,&sctx->funcname);
7057:   /*
7058:      This should work, but it doesn't
7059:   sctx->ctx = ctx;
7060:   mexMakeArrayPersistent(sctx->ctx);
7061:   */
7062:   sctx->ctx = mxDuplicateArray(ctx);

7064:   TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);
7065:   return(0);
7066: }

7068: /*
7069:    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().

7071:    Collective on TS

7073: .seealso: TSSetFunction(), TSGetFunction()
7074: @*/
7075: PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
7076: {
7077:   PetscErrorCode  ierr;
7078:   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
7079:   int             nlhs  = 1,nrhs = 6;
7080:   mxArray         *plhs[1],*prhs[6];
7081:   long long int   lx = 0,ls = 0;


7087:   PetscMemcpy(&ls,&ts,sizeof(ts));
7088:   PetscMemcpy(&lx,&u,sizeof(u));

7090:   prhs[0] =  mxCreateDoubleScalar((double)ls);
7091:   prhs[1] =  mxCreateDoubleScalar((double)it);
7092:   prhs[2] =  mxCreateDoubleScalar((double)time);
7093:   prhs[3] =  mxCreateDoubleScalar((double)lx);
7094:   prhs[4] =  mxCreateString(sctx->funcname);
7095:   prhs[5] =  sctx->ctx;
7096:    mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");
7097:    mxGetScalar(plhs[0]);
7098:   mxDestroyArray(prhs[0]);
7099:   mxDestroyArray(prhs[1]);
7100:   mxDestroyArray(prhs[2]);
7101:   mxDestroyArray(prhs[3]);
7102:   mxDestroyArray(prhs[4]);
7103:   mxDestroyArray(plhs[0]);
7104:   return(0);
7105: }

7107: /*
7108:    TSMonitorSetMatlab - Sets the monitor function from Matlab

7110:    Level: developer

7112: .keywords: TS, nonlinear, set, function

7114: .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7115: */
7116: PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
7117: {
7118:   PetscErrorCode  ierr;
7119:   TSMatlabContext *sctx;

7122:   /* currently sctx is memory bleed */
7123:   PetscNew(&sctx);
7124:   PetscStrallocpy(func,&sctx->funcname);
7125:   /*
7126:      This should work, but it doesn't
7127:   sctx->ctx = ctx;
7128:   mexMakeArrayPersistent(sctx->ctx);
7129:   */
7130:   sctx->ctx = mxDuplicateArray(ctx);

7132:   TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);
7133:   return(0);
7134: }
7135: #endif

7137: /*@C
7138:    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
7139:        in a time based line graph

7141:    Collective on TS

7143:    Input Parameters:
7144: +  ts - the TS context
7145: .  step - current time-step
7146: .  ptime - current time
7147: .  u - current solution
7148: -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()

7150:    Options Database:
7151: .   -ts_monitor_lg_solution_variables

7153:    Level: intermediate

7155:    Notes: Each process in a parallel run displays its component solutions in a separate window

7157: .keywords: TS,  vector, monitor, view

7159: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
7160:            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
7161:            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
7162:            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
7163: @*/
7164: PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7165: {
7166:   PetscErrorCode    ierr;
7167:   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
7168:   const PetscScalar *yy;
7169:   Vec               v;

7172:   if (step < 0) return(0); /* -1 indicates interpolated solution */
7173:   if (!step) {
7174:     PetscDrawAxis axis;
7175:     PetscInt      dim;
7176:     PetscDrawLGGetAxis(ctx->lg,&axis);
7177:     PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");
7178:     if (!ctx->names) {
7179:       PetscBool flg;
7180:       /* user provides names of variables to plot but no names has been set so assume names are integer values */
7181:       PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);
7182:       if (flg) {
7183:         PetscInt i,n;
7184:         char     **names;
7185:         VecGetSize(u,&n);
7186:         PetscMalloc1(n+1,&names);
7187:         for (i=0; i<n; i++) {
7188:           PetscMalloc1(5,&names[i]);
7189:           PetscSNPrintf(names[i],5,"%D",i);
7190:         }
7191:         names[n] = NULL;
7192:         ctx->names = names;
7193:       }
7194:     }
7195:     if (ctx->names && !ctx->displaynames) {
7196:       char      **displaynames;
7197:       PetscBool flg;
7198:       VecGetLocalSize(u,&dim);
7199:       PetscMalloc1(dim+1,&displaynames);
7200:       PetscMemzero(displaynames,(dim+1)*sizeof(char*));
7201:       PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);
7202:       if (flg) {
7203:         TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);
7204:       }
7205:       PetscStrArrayDestroy(&displaynames);
7206:     }
7207:     if (ctx->displaynames) {
7208:       PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);
7209:       PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);
7210:     } else if (ctx->names) {
7211:       VecGetLocalSize(u,&dim);
7212:       PetscDrawLGSetDimension(ctx->lg,dim);
7213:       PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);
7214:     } else {
7215:       VecGetLocalSize(u,&dim);
7216:       PetscDrawLGSetDimension(ctx->lg,dim);
7217:     }
7218:     PetscDrawLGReset(ctx->lg);
7219:   }

7221:   if (!ctx->transform) v = u;
7222:   else {(*ctx->transform)(ctx->transformctx,u,&v);}
7223:   VecGetArrayRead(v,&yy);
7224:   if (ctx->displaynames) {
7225:     PetscInt i;
7226:     for (i=0; i<ctx->ndisplayvariables; i++)
7227:       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
7228:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);
7229:   } else {
7230: #if defined(PETSC_USE_COMPLEX)
7231:     PetscInt  i,n;
7232:     PetscReal *yreal;
7233:     VecGetLocalSize(v,&n);
7234:     PetscMalloc1(n,&yreal);
7235:     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
7236:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);
7237:     PetscFree(yreal);
7238: #else
7239:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);
7240: #endif
7241:   }
7242:   VecRestoreArrayRead(v,&yy);
7243:   if (ctx->transform) {VecDestroy(&v);}

7245:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
7246:     PetscDrawLGDraw(ctx->lg);
7247:     PetscDrawLGSave(ctx->lg);
7248:   }
7249:   return(0);
7250: }

7252: /*@C
7253:    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot

7255:    Collective on TS

7257:    Input Parameters:
7258: +  ts - the TS context
7259: -  names - the names of the components, final string must be NULL

7261:    Level: intermediate

7263:    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

7265: .keywords: TS,  vector, monitor, view

7267: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
7268: @*/
7269: PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
7270: {
7271:   PetscErrorCode    ierr;
7272:   PetscInt          i;

7275:   for (i=0; i<ts->numbermonitors; i++) {
7276:     if (ts->monitor[i] == TSMonitorLGSolution) {
7277:       TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);
7278:       break;
7279:     }
7280:   }
7281:   return(0);
7282: }

7284: /*@C
7285:    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot

7287:    Collective on TS

7289:    Input Parameters:
7290: +  ts - the TS context
7291: -  names - the names of the components, final string must be NULL

7293:    Level: intermediate

7295: .keywords: TS,  vector, monitor, view

7297: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7298: @*/
7299: PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7300: {
7301:   PetscErrorCode    ierr;

7304:   PetscStrArrayDestroy(&ctx->names);
7305:   PetscStrArrayallocpy(names,&ctx->names);
7306:   return(0);
7307: }

7309: /*@C
7310:    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot

7312:    Collective on TS

7314:    Input Parameter:
7315: .  ts - the TS context

7317:    Output Parameter:
7318: .  names - the names of the components, final string must be NULL

7320:    Level: intermediate

7322:    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

7324: .keywords: TS,  vector, monitor, view

7326: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7327: @*/
7328: PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7329: {
7330:   PetscInt       i;

7333:   *names = NULL;
7334:   for (i=0; i<ts->numbermonitors; i++) {
7335:     if (ts->monitor[i] == TSMonitorLGSolution) {
7336:       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7337:       *names = (const char *const *)ctx->names;
7338:       break;
7339:     }
7340:   }
7341:   return(0);
7342: }

7344: /*@C
7345:    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor

7347:    Collective on TS

7349:    Input Parameters:
7350: +  ctx - the TSMonitorLG context
7351: .  displaynames - the names of the components, final string must be NULL

7353:    Level: intermediate

7355: .keywords: TS,  vector, monitor, view

7357: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7358: @*/
7359: PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7360: {
7361:   PetscInt          j = 0,k;
7362:   PetscErrorCode    ierr;

7365:   if (!ctx->names) return(0);
7366:   PetscStrArrayDestroy(&ctx->displaynames);
7367:   PetscStrArrayallocpy(displaynames,&ctx->displaynames);
7368:   while (displaynames[j]) j++;
7369:   ctx->ndisplayvariables = j;
7370:   PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);
7371:   PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);
7372:   j = 0;
7373:   while (displaynames[j]) {
7374:     k = 0;
7375:     while (ctx->names[k]) {
7376:       PetscBool flg;
7377:       PetscStrcmp(displaynames[j],ctx->names[k],&flg);
7378:       if (flg) {
7379:         ctx->displayvariables[j] = k;
7380:         break;
7381:       }
7382:       k++;
7383:     }
7384:     j++;
7385:   }
7386:   return(0);
7387: }

7389: /*@C
7390:    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor

7392:    Collective on TS

7394:    Input Parameters:
7395: +  ts - the TS context
7396: .  displaynames - the names of the components, final string must be NULL

7398:    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

7400:    Level: intermediate

7402: .keywords: TS,  vector, monitor, view

7404: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7405: @*/
7406: PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7407: {
7408:   PetscInt          i;
7409:   PetscErrorCode    ierr;

7412:   for (i=0; i<ts->numbermonitors; i++) {
7413:     if (ts->monitor[i] == TSMonitorLGSolution) {
7414:       TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);
7415:       break;
7416:     }
7417:   }
7418:   return(0);
7419: }

7421: /*@C
7422:    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed

7424:    Collective on TS

7426:    Input Parameters:
7427: +  ts - the TS context
7428: .  transform - the transform function
7429: .  destroy - function to destroy the optional context
7430: -  ctx - optional context used by transform function

7432:    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

7434:    Level: intermediate

7436: .keywords: TS,  vector, monitor, view

7438: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
7439: @*/
7440: PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7441: {
7442:   PetscInt          i;
7443:   PetscErrorCode    ierr;

7446:   for (i=0; i<ts->numbermonitors; i++) {
7447:     if (ts->monitor[i] == TSMonitorLGSolution) {
7448:       TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);
7449:     }
7450:   }
7451:   return(0);
7452: }

7454: /*@C
7455:    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed

7457:    Collective on TSLGCtx

7459:    Input Parameters:
7460: +  ts - the TS context
7461: .  transform - the transform function
7462: .  destroy - function to destroy the optional context
7463: -  ctx - optional context used by transform function

7465:    Level: intermediate

7467: .keywords: TS,  vector, monitor, view

7469: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7470: @*/
7471: PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7472: {
7474:   ctx->transform    = transform;
7475:   ctx->transformdestroy = destroy;
7476:   ctx->transformctx = tctx;
7477:   return(0);
7478: }

7480: /*@C
7481:    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
7482:        in a time based line graph

7484:    Collective on TS

7486:    Input Parameters:
7487: +  ts - the TS context
7488: .  step - current time-step
7489: .  ptime - current time
7490: .  u - current solution
7491: -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()

7493:    Level: intermediate

7495:    Notes: Each process in a parallel run displays its component errors in a separate window

7497:    The user must provide the solution using TSSetSolutionFunction() to use this monitor.

7499:    Options Database Keys:
7500: .  -ts_monitor_lg_error - create a graphical monitor of error history

7502: .keywords: TS,  vector, monitor, view

7504: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7505: @*/
7506: PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7507: {
7508:   PetscErrorCode    ierr;
7509:   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7510:   const PetscScalar *yy;
7511:   Vec               y;

7514:   if (!step) {
7515:     PetscDrawAxis axis;
7516:     PetscInt      dim;
7517:     PetscDrawLGGetAxis(ctx->lg,&axis);
7518:     PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");
7519:     VecGetLocalSize(u,&dim);
7520:     PetscDrawLGSetDimension(ctx->lg,dim);
7521:     PetscDrawLGReset(ctx->lg);
7522:   }
7523:   VecDuplicate(u,&y);
7524:   TSComputeSolutionFunction(ts,ptime,y);
7525:   VecAXPY(y,-1.0,u);
7526:   VecGetArrayRead(y,&yy);
7527: #if defined(PETSC_USE_COMPLEX)
7528:   {
7529:     PetscReal *yreal;
7530:     PetscInt  i,n;
7531:     VecGetLocalSize(y,&n);
7532:     PetscMalloc1(n,&yreal);
7533:     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
7534:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);
7535:     PetscFree(yreal);
7536:   }
7537: #else
7538:   PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);
7539: #endif
7540:   VecRestoreArrayRead(y,&yy);
7541:   VecDestroy(&y);
7542:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
7543:     PetscDrawLGDraw(ctx->lg);
7544:     PetscDrawLGSave(ctx->lg);
7545:   }
7546:   return(0);
7547: }

7549: PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7550: {
7551:   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7552:   PetscReal      x   = ptime,y;
7554:   PetscInt       its;

7557:   if (n < 0) return(0); /* -1 indicates interpolated solution */
7558:   if (!n) {
7559:     PetscDrawAxis axis;
7560:     PetscDrawLGGetAxis(ctx->lg,&axis);
7561:     PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");
7562:     PetscDrawLGReset(ctx->lg);
7563:     ctx->snes_its = 0;
7564:   }
7565:   TSGetSNESIterations(ts,&its);
7566:   y    = its - ctx->snes_its;
7567:   PetscDrawLGAddPoint(ctx->lg,&x,&y);
7568:   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7569:     PetscDrawLGDraw(ctx->lg);
7570:     PetscDrawLGSave(ctx->lg);
7571:   }
7572:   ctx->snes_its = its;
7573:   return(0);
7574: }

7576: PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7577: {
7578:   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7579:   PetscReal      x   = ptime,y;
7581:   PetscInt       its;

7584:   if (n < 0) return(0); /* -1 indicates interpolated solution */
7585:   if (!n) {
7586:     PetscDrawAxis axis;
7587:     PetscDrawLGGetAxis(ctx->lg,&axis);
7588:     PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");
7589:     PetscDrawLGReset(ctx->lg);
7590:     ctx->ksp_its = 0;
7591:   }
7592:   TSGetKSPIterations(ts,&its);
7593:   y    = its - ctx->ksp_its;
7594:   PetscDrawLGAddPoint(ctx->lg,&x,&y);
7595:   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7596:     PetscDrawLGDraw(ctx->lg);
7597:     PetscDrawLGSave(ctx->lg);
7598:   }
7599:   ctx->ksp_its = its;
7600:   return(0);
7601: }

7603: /*@
7604:    TSComputeLinearStability - computes the linear stability function at a point

7606:    Collective on TS and Vec

7608:    Input Parameters:
7609: +  ts - the TS context
7610: -  xr,xi - real and imaginary part of input arguments

7612:    Output Parameters:
7613: .  yr,yi - real and imaginary part of function value

7615:    Level: developer

7617: .keywords: TS, compute

7619: .seealso: TSSetRHSFunction(), TSComputeIFunction()
7620: @*/
7621: PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7622: {

7627:   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7628:   (*ts->ops->linearstability)(ts,xr,xi,yr,yi);
7629:   return(0);
7630: }

7632: /* ------------------------------------------------------------------------*/
7633: /*@C
7634:    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()

7636:    Collective on TS

7638:    Input Parameters:
7639: .  ts  - the ODE solver object

7641:    Output Parameter:
7642: .  ctx - the context

7644:    Level: intermediate

7646: .keywords: TS, monitor, line graph, residual, seealso

7648: .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()

7650: @*/
7651: PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7652: {

7656:   PetscNew(ctx);
7657:   return(0);
7658: }

7660: /*@C
7661:    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution

7663:    Collective on TS

7665:    Input Parameters:
7666: +  ts - the TS context
7667: .  step - current time-step
7668: .  ptime - current time
7669: .  u  - current solution
7670: -  dctx - the envelope context

7672:    Options Database:
7673: .  -ts_monitor_envelope

7675:    Level: intermediate

7677:    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope

7679: .keywords: TS,  vector, monitor, view

7681: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7682: @*/
7683: PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7684: {
7685:   PetscErrorCode       ierr;
7686:   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;

7689:   if (!ctx->max) {
7690:     VecDuplicate(u,&ctx->max);
7691:     VecDuplicate(u,&ctx->min);
7692:     VecCopy(u,ctx->max);
7693:     VecCopy(u,ctx->min);
7694:   } else {
7695:     VecPointwiseMax(ctx->max,u,ctx->max);
7696:     VecPointwiseMin(ctx->min,u,ctx->min);
7697:   }
7698:   return(0);
7699: }

7701: /*@C
7702:    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution

7704:    Collective on TS

7706:    Input Parameter:
7707: .  ts - the TS context

7709:    Output Parameter:
7710: +  max - the maximum values
7711: -  min - the minimum values

7713:    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored

7715:    Level: intermediate

7717: .keywords: TS,  vector, monitor, view

7719: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7720: @*/
7721: PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7722: {
7723:   PetscInt i;

7726:   if (max) *max = NULL;
7727:   if (min) *min = NULL;
7728:   for (i=0; i<ts->numbermonitors; i++) {
7729:     if (ts->monitor[i] == TSMonitorEnvelope) {
7730:       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7731:       if (max) *max = ctx->max;
7732:       if (min) *min = ctx->min;
7733:       break;
7734:     }
7735:   }
7736:   return(0);
7737: }

7739: /*@C
7740:    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().

7742:    Collective on TSMonitorEnvelopeCtx

7744:    Input Parameter:
7745: .  ctx - the monitor context

7747:    Level: intermediate

7749: .keywords: TS, monitor, line graph, destroy

7751: .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7752: @*/
7753: PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7754: {

7758:   VecDestroy(&(*ctx)->min);
7759:   VecDestroy(&(*ctx)->max);
7760:   PetscFree(*ctx);
7761:   return(0);
7762: }

7764: /*@
7765:    TSRestartStep - Flags the solver to restart the next step

7767:    Collective on TS

7769:    Input Parameter:
7770: .  ts - the TS context obtained from TSCreate()

7772:    Level: advanced

7774:    Notes:
7775:    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7776:    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7777:    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7778:    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7779:    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7780:    discontinuous source terms).

7782: .keywords: TS, timestep, restart

7784: .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7785: @*/
7786: PetscErrorCode TSRestartStep(TS ts)
7787: {
7790:   ts->steprestart = PETSC_TRUE;
7791:   return(0);
7792: }

7794: /*@
7795:    TSRollBack - Rolls back one time step

7797:    Collective on TS

7799:    Input Parameter:
7800: .  ts - the TS context obtained from TSCreate()

7802:    Level: advanced

7804: .keywords: TS, timestep, rollback

7806: .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
7807: @*/
7808: PetscErrorCode  TSRollBack(TS ts)
7809: {

7814:   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
7815:   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
7816:   (*ts->ops->rollback)(ts);
7817:   ts->time_step = ts->ptime - ts->ptime_prev;
7818:   ts->ptime = ts->ptime_prev;
7819:   ts->ptime_prev = ts->ptime_prev_rollback;
7820:   ts->steps--;
7821:   ts->steprollback = PETSC_TRUE;
7822:   return(0);
7823: }

7825: /*@
7826:    TSGetStages - Get the number of stages and stage values

7828:    Input Parameter:
7829: .  ts - the TS context obtained from TSCreate()

7831:    Level: advanced

7833: .keywords: TS, getstages

7835: .seealso: TSCreate()
7836: @*/
7837: PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7838: {


7845:   if (!ts->ops->getstages) *ns=0;
7846:   else {
7847:     (*ts->ops->getstages)(ts,ns,Y);
7848:   }
7849:   return(0);
7850: }

7852: /*@C
7853:   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.

7855:   Collective on SNES

7857:   Input Parameters:
7858: + ts - the TS context
7859: . t - current timestep
7860: . U - state vector
7861: . Udot - time derivative of state vector
7862: . shift - shift to apply, see note below
7863: - ctx - an optional user context

7865:   Output Parameters:
7866: + J - Jacobian matrix (not altered in this routine)
7867: - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)

7869:   Level: intermediate

7871:   Notes:
7872:   If F(t,U,Udot)=0 is the DAE, the required Jacobian is

7874:   dF/dU + shift*dF/dUdot

7876:   Most users should not need to explicitly call this routine, as it
7877:   is used internally within the nonlinear solvers.

7879:   This will first try to get the coloring from the DM.  If the DM type has no coloring
7880:   routine, then it will try to get the coloring from the matrix.  This requires that the
7881:   matrix have nonzero entries precomputed.

7883: .keywords: TS, finite differences, Jacobian, coloring, sparse
7884: .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7885: @*/
7886: PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7887: {
7888:   SNES           snes;
7889:   MatFDColoring  color;
7890:   PetscBool      hascolor, matcolor = PETSC_FALSE;

7894:   PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);
7895:   PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);
7896:   if (!color) {
7897:     DM         dm;
7898:     ISColoring iscoloring;

7900:     TSGetDM(ts, &dm);
7901:     DMHasColoring(dm, &hascolor);
7902:     if (hascolor && !matcolor) {
7903:       DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);
7904:       MatFDColoringCreate(B, iscoloring, &color);
7905:       MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);
7906:       MatFDColoringSetFromOptions(color);
7907:       MatFDColoringSetUp(B, iscoloring, color);
7908:       ISColoringDestroy(&iscoloring);
7909:     } else {
7910:       MatColoring mc;

7912:       MatColoringCreate(B, &mc);
7913:       MatColoringSetDistance(mc, 2);
7914:       MatColoringSetType(mc, MATCOLORINGSL);
7915:       MatColoringSetFromOptions(mc);
7916:       MatColoringApply(mc, &iscoloring);
7917:       MatColoringDestroy(&mc);
7918:       MatFDColoringCreate(B, iscoloring, &color);
7919:       MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);
7920:       MatFDColoringSetFromOptions(color);
7921:       MatFDColoringSetUp(B, iscoloring, color);
7922:       ISColoringDestroy(&iscoloring);
7923:     }
7924:     PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);
7925:     PetscObjectDereference((PetscObject) color);
7926:   }
7927:   TSGetSNES(ts, &snes);
7928:   MatFDColoringApply(B, color, U, snes);
7929:   if (J != B) {
7930:     MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);
7931:     MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);
7932:   }
7933:   return(0);
7934: }

7936: /*@
7937:     TSSetFunctionDomainError - Set the function testing if the current state vector is valid

7939:     Input Parameters:
7940:     ts - the TS context
7941:     func - function called within TSFunctionDomainError

7943:     Level: intermediate

7945: .keywords: TS, state, domain
7946: .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7947: @*/

7949: PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7950: {
7953:   ts->functiondomainerror = func;
7954:   return(0);
7955: }

7957: /*@
7958:     TSFunctionDomainError - Check if the current state is valid

7960:     Input Parameters:
7961:     ts - the TS context
7962:     stagetime - time of the simulation
7963:     Y - state vector to check.

7965:     Output Parameter:
7966:     accept - Set to PETSC_FALSE if the current state vector is valid.

7968:     Note:
7969:     This function should be used to ensure the state is in a valid part of the space.
7970:     For example, one can ensure here all values are positive.

7972:     Level: advanced
7973: @*/
7974: PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7975: {


7981:   *accept = PETSC_TRUE;
7982:   if (ts->functiondomainerror) {
7983:     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7984:   }
7985:   return(0);
7986: }

7988: /*@C
7989:   TSClone - This function clones a time step object.

7991:   Collective on MPI_Comm

7993:   Input Parameter:
7994: . tsin    - The input TS

7996:   Output Parameter:
7997: . tsout   - The output TS (cloned)

7999:   Notes:
8000:   This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly.

8002:   When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); TSSetSNES(ts,snes_dup);

8004:   Level: developer

8006: .keywords: TS, clone
8007: .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
8008: @*/
8009: PetscErrorCode  TSClone(TS tsin, TS *tsout)
8010: {
8011:   TS             t;
8013:   SNES           snes_start;
8014:   DM             dm;
8015:   TSType         type;

8019:   *tsout = NULL;

8021:   PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);

8023:   /* General TS description */
8024:   t->numbermonitors    = 0;
8025:   t->setupcalled       = 0;
8026:   t->ksp_its           = 0;
8027:   t->snes_its          = 0;
8028:   t->nwork             = 0;
8029:   t->rhsjacobian.time  = -1e20;
8030:   t->rhsjacobian.scale = 1.;
8031:   t->ijacobian.shift   = 1.;

8033:   TSGetSNES(tsin,&snes_start);
8034:   TSSetSNES(t,snes_start);

8036:   TSGetDM(tsin,&dm);
8037:   TSSetDM(t,dm);

8039:   t->adapt = tsin->adapt;
8040:   PetscObjectReference((PetscObject)t->adapt);

8042:   t->trajectory = tsin->trajectory;
8043:   PetscObjectReference((PetscObject)t->trajectory);

8045:   t->event = tsin->event;
8046:   if (t->event) t->event->refct++;

8048:   t->problem_type      = tsin->problem_type;
8049:   t->ptime             = tsin->ptime;
8050:   t->ptime_prev        = tsin->ptime_prev;
8051:   t->time_step         = tsin->time_step;
8052:   t->max_time          = tsin->max_time;
8053:   t->steps             = tsin->steps;
8054:   t->max_steps         = tsin->max_steps;
8055:   t->equation_type     = tsin->equation_type;
8056:   t->atol              = tsin->atol;
8057:   t->rtol              = tsin->rtol;
8058:   t->max_snes_failures = tsin->max_snes_failures;
8059:   t->max_reject        = tsin->max_reject;
8060:   t->errorifstepfailed = tsin->errorifstepfailed;

8062:   TSGetType(tsin,&type);
8063:   TSSetType(t,type);

8065:   t->vec_sol           = NULL;

8067:   t->cfltime          = tsin->cfltime;
8068:   t->cfltime_local    = tsin->cfltime_local;
8069:   t->exact_final_time = tsin->exact_final_time;

8071:   PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));

8073:   if (((PetscObject)tsin)->fortran_func_pointers) {
8074:     PetscInt i;
8075:     PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);
8076:     for (i=0; i<10; i++) {
8077:       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
8078:     }
8079:   }
8080:   *tsout = t;
8081:   return(0);
8082: }