Actual source code: ts.c

petsc-master 2019-05-18
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: #define SkipSmallValue(a,b,tol) if(PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;

  9: /* Logging support */
 10: PetscClassId  TS_CLASSID, DMTS_CLASSID;
 11: PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;

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


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

 19:    Collective on TS

 21:    Input Parameters:
 22: +  ts - TS object you wish to monitor
 23: .  name - the monitor type one is seeking
 24: .  help - message indicating what monitoring is done
 25: .  manual - manual page for the monitor
 26: .  monitor - the monitor function
 27: -  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

 29:    Level: developer

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

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

 60: static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
 61: {

 67:   if (!((PetscObject)adapt)->type_name) {
 68:     TSAdaptSetType(adapt,default_type);
 69:   }
 70:   return(0);
 71: }

 73: /*@
 74:    TSSetFromOptions - Sets various TS parameters from user options.

 76:    Collective on TS

 78:    Input Parameter:
 79: .  ts - the TS context obtained from TSCreate()

 81:    Options Database Keys:
 82: +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
 83: .  -ts_save_trajectory - checkpoint the solution at each time-step
 84: .  -ts_max_time <time> - maximum time to compute to
 85: .  -ts_max_steps <steps> - maximum number of time-steps to take
 86: .  -ts_init_time <time> - initial time to start computation
 87: .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
 88: .  -ts_dt <dt> - initial time step
 89: .  -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
 90: .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
 91: .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
 92: .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
 93: .  -ts_rtol <rtol> - relative tolerance for local truncation error
 94: .  -ts_atol <atol> Absolute tolerance for local truncation error
 95: .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
 96: .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
 97: .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
 98: .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
 99: .  -ts_monitor - print information at each timestep
100: .  -ts_monitor_lg_solution - Monitor solution graphically
101: .  -ts_monitor_lg_error - Monitor error graphically
102: .  -ts_monitor_error - Monitors norm of error
103: .  -ts_monitor_lg_timestep - Monitor timestep size graphically
104: .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
105: .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
106: .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
107: .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
108: .  -ts_monitor_draw_solution - Monitor solution graphically
109: .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
110: .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
111: .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
112: .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
113: .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time

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

117:    Level: beginner

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

121: .seealso: TSGetType()
122: @*/
123: PetscErrorCode  TSSetFromOptions(TS ts)
124: {
125:   PetscBool              opt,flg,tflg;
126:   PetscErrorCode         ierr;
127:   char                   monfilename[PETSC_MAX_PATH_LEN];
128:   PetscReal              time_step;
129:   TSExactFinalTimeOption eftopt;
130:   char                   dir[16];
131:   TSIFunction            ifun;
132:   const char             *defaultType;
133:   char                   typeName[256];


138:   TSRegisterAll();
139:   TSGetIFunction(ts,NULL,&ifun,NULL);

141:   PetscObjectOptionsBegin((PetscObject)ts);
142:   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
143:   else defaultType = ifun ? TSBEULER : TSEULER;
144:   PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);
145:   if (opt) {
146:     TSSetType(ts,typeName);
147:   } else {
148:     TSSetType(ts,defaultType);
149:   }

151:   /* Handle generic TS options */
152:   PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);
153:   PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);
154:   PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);
155:   PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);
156:   PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);
157:   if (flg) {TSSetTimeStep(ts,time_step);}
158:   PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);
159:   if (flg) {TSSetExactFinalTime(ts,eftopt);}
160:   PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);
161:   PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);
162:   PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);
163:   PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);
164:   PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);

166:   PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL);
167:   PetscOptionsBool("-ts_rhs_jacobian_test_mult_transpose","Test the RHS Jacobian transpose for consistency with RHS at each solve ","None",ts->testjacobiantranspose,&ts->testjacobiantranspose,NULL);
168:   PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);
169: #if defined(PETSC_HAVE_SAWS)
170:   {
171:   PetscBool set;
172:   flg  = PETSC_FALSE;
173:   PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);
174:   if (set) {
175:     PetscObjectSAWsSetBlock((PetscObject)ts,flg);
176:   }
177:   }
178: #endif

180:   /* Monitor options */
181:   TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);
182:   TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);
183:   TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);

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

188:   PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);
189:   if (opt) {
190:     TSMonitorLGCtx ctx;
191:     PetscInt       howoften = 1;

193:     PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);
194:     TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
195:     TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
196:   }

198:   PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);
199:   if (opt) {
200:     TSMonitorLGCtx ctx;
201:     PetscInt       howoften = 1;

203:     PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);
204:     TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
205:     TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
206:   }
207:   TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);

209:   PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);
210:   if (opt) {
211:     TSMonitorLGCtx ctx;
212:     PetscInt       howoften = 1;

214:     PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);
215:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
216:     TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
217:   }
218:   PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);
219:   if (opt) {
220:     TSMonitorLGCtx ctx;
221:     PetscInt       howoften = 1;

223:     PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);
224:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
225:     TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
226:     ctx->semilogy = PETSC_TRUE;
227:   }

229:   PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);
230:   if (opt) {
231:     TSMonitorLGCtx ctx;
232:     PetscInt       howoften = 1;

234:     PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);
235:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
236:     TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
237:   }
238:   PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);
239:   if (opt) {
240:     TSMonitorLGCtx ctx;
241:     PetscInt       howoften = 1;

243:     PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);
244:     TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);
245:     TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);
246:   }
247:   PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);
248:   if (opt) {
249:     TSMonitorSPEigCtx ctx;
250:     PetscInt          howoften = 1;

252:     PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);
253:     TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
254:     TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);
255:   }
256:   PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);
257:   if (opt) {
258:     TSMonitorSPCtx  ctx;
259:     PetscInt        howoften = 1;
260:     PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);
261:     TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);
262:     TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);
263:   }
264:   opt  = PETSC_FALSE;
265:   PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);
266:   if (opt) {
267:     TSMonitorDrawCtx ctx;
268:     PetscInt         howoften = 1;

270:     PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);
271:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
272:     TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
273:   }
274:   opt  = PETSC_FALSE;
275:   PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);
276:   if (opt) {
277:     TSMonitorDrawCtx ctx;
278:     PetscReal        bounds[4];
279:     PetscInt         n = 4;
280:     PetscDraw        draw;
281:     PetscDrawAxis    axis;

283:     PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);
284:     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
285:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);
286:     PetscViewerDrawGetDraw(ctx->viewer,0,&draw);
287:     PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);
288:     PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);
289:     PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");
290:     TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
291:   }
292:   opt  = PETSC_FALSE;
293:   PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);
294:   if (opt) {
295:     TSMonitorDrawCtx ctx;
296:     PetscInt         howoften = 1;

298:     PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);
299:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
300:     TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
301:   }
302:   opt  = PETSC_FALSE;
303:   PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);
304:   if (opt) {
305:     TSMonitorDrawCtx ctx;
306:     PetscInt         howoften = 1;

308:     PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);
309:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
310:     TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
311:   }

313:   opt  = PETSC_FALSE;
314:   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);
315:   if (flg) {
316:     const char *ptr,*ptr2;
317:     char       *filetemplate;
318:     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
319:     /* Do some cursory validation of the input. */
320:     PetscStrstr(monfilename,"%",(char**)&ptr);
321:     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
322:     for (ptr++; ptr && *ptr; ptr++) {
323:       PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);
324:       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");
325:       if (ptr2) break;
326:     }
327:     PetscStrallocpy(monfilename,&filetemplate);
328:     TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);
329:   }

331:   PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);
332:   if (flg) {
333:     TSMonitorDMDARayCtx *rayctx;
334:     int                  ray = 0;
335:     DMDADirection        ddir;
336:     DM                   da;
337:     PetscMPIInt          rank;

339:     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
340:     if (dir[0] == 'x') ddir = DMDA_X;
341:     else if (dir[0] == 'y') ddir = DMDA_Y;
342:     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
343:     sscanf(dir+2,"%d",&ray);

345:     PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);
346:     PetscNew(&rayctx);
347:     TSGetDM(ts,&da);
348:     DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);
349:     MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);
350:     if (!rank) {
351:       PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);
352:     }
353:     rayctx->lgctx = NULL;
354:     TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);
355:   }
356:   PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);
357:   if (flg) {
358:     TSMonitorDMDARayCtx *rayctx;
359:     int                 ray = 0;
360:     DMDADirection       ddir;
361:     DM                  da;
362:     PetscInt            howoften = 1;

364:     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
365:     if      (dir[0] == 'x') ddir = DMDA_X;
366:     else if (dir[0] == 'y') ddir = DMDA_Y;
367:     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
368:     sscanf(dir+2, "%d", &ray);

370:     PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);
371:     PetscNew(&rayctx);
372:     TSGetDM(ts, &da);
373:     DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);
374:     TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);
375:     TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);
376:   }

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

382:     TSMonitorEnvelopeCtxCreate(ts,&ctx);
383:     TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);
384:   }

386:   flg  = PETSC_FALSE;
387:   PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);
388:   if (flg) {
389:     DM   dm;
390:     DMTS tdm;

392:     TSGetDM(ts, &dm);
393:     DMGetDMTS(dm, &tdm);
394:     tdm->ijacobianctx = NULL;
395:     TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);
396:     PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");
397:   }

399:   /* Handle specific TS options */
400:   if (ts->ops->setfromoptions) {
401:     (*ts->ops->setfromoptions)(PetscOptionsObject,ts);
402:   }

404:   /* Handle TSAdapt options */
405:   TSGetAdapt(ts,&ts->adapt);
406:   TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);
407:   TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);

409:   /* TS trajectory must be set after TS, since it may use some TS options above */
410:   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
411:   PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);
412:   if (tflg) {
413:     TSSetSaveTrajectory(ts);
414:   }

416:   TSAdjointSetFromOptions(PetscOptionsObject,ts);

418:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
419:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);
420:   PetscOptionsEnd();

422:   if (ts->trajectory) {
423:     TSTrajectorySetFromOptions(ts->trajectory,ts);
424:   }

426:   /* why do we have to do this here and not during TSSetUp? */
427:   TSGetSNES(ts,&ts->snes);
428:   if (ts->problem_type == TS_LINEAR) {
429:     PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");
430:     if (!flg) { SNESSetType(ts->snes,SNESKSPONLY); }
431:   }
432:   SNESSetFromOptions(ts->snes);
433:   return(0);
434: }

436: /*@
437:    TSGetTrajectory - Gets the trajectory from a TS if it exists

439:    Collective on TS

441:    Input Parameters:
442: .  ts - the TS context obtained from TSCreate()

444:    Output Parameters;
445: .  tr - the TSTrajectory object, if it exists

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

449:    Level: advanced

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

453: .keywords: TS, set, checkpoint,
454: @*/
455: PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
456: {
459:   *tr = ts->trajectory;
460:   return(0);
461: }

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

466:    Collective on TS

468:    Input Parameters:
469: .  ts - the TS context obtained from TSCreate()

471:    Options Database:
472: +  -ts_save_trajectory - saves the trajectory to a file
473: -  -ts_trajectory_type type

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

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

480:    Level: intermediate

482: .seealso: TSGetTrajectory(), TSAdjointSolve()

484: .keywords: TS, set, checkpoint,
485: @*/
486: PetscErrorCode  TSSetSaveTrajectory(TS ts)
487: {

492:   if (!ts->trajectory) {
493:     TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);
494:   }
495:   return(0);
496: }

498: /*@
499:    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object

501:    Collective on TS

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

506:    Level: intermediate

508: .seealso: TSGetTrajectory(), TSAdjointSolve()

510: .keywords: TS, set, checkpoint,
511: @*/
512: PetscErrorCode  TSResetTrajectory(TS ts)
513: {

518:   if (ts->trajectory) {
519:     TSTrajectoryDestroy(&ts->trajectory);
520:     TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);
521:   }
522:   return(0);
523: }

525: /*@
526:    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
527:       set with TSSetRHSJacobian().

529:    Collective on TS and Vec

531:    Input Parameters:
532: +  ts - the TS context
533: .  t - current timestep
534: -  U - input vector

536:    Output Parameters:
537: +  A - Jacobian matrix
538: .  B - optional preconditioning matrix
539: -  flag - flag indicating matrix structure

541:    Notes:
542:    Most users should not need to explicitly call this routine, as it
543:    is used internally within the nonlinear solvers.

545:    See KSPSetOperators() for important information about setting the
546:    flag parameter.

548:    Level: developer

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

552: .seealso:  TSSetRHSJacobian(), KSPSetOperators()
553: @*/
554: PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
555: {
556:   PetscErrorCode   ierr;
557:   PetscObjectState Ustate;
558:   PetscObjectId    Uid;
559:   DM               dm;
560:   DMTS             tsdm;
561:   TSRHSJacobian    rhsjacobianfunc;
562:   void             *ctx;
563:   TSIJacobian      ijacobianfunc;
564:   TSRHSFunction    rhsfunction;

570:   TSGetDM(ts,&dm);
571:   DMGetDMTS(dm,&tsdm);
572:   DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);
573:   DMTSGetIJacobian(dm,&ijacobianfunc,NULL);
574:   DMTSGetRHSFunction(dm,&rhsfunction,&ctx);
575:   PetscObjectStateGet((PetscObject)U,&Ustate);
576:   PetscObjectGetId((PetscObject)U,&Uid);

578:   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
579:     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
580:     if (A == ts->Arhs) {
581:       if (ts->rhsjacobian.shift != 0) {
582:         MatShift(A,-ts->rhsjacobian.shift);
583:       }
584:       if (ts->rhsjacobian.scale != 1.) {
585:         MatScale(A,1./ts->rhsjacobian.scale);
586:       }
587:     }
588:     if (B && B == ts->Brhs && A != B) {
589:       if (ts->rhsjacobian.shift != 0) {
590:         MatShift(B,-ts->rhsjacobian.shift);
591:       }
592:       if (ts->rhsjacobian.scale != 1.) {
593:         MatScale(B,1./ts->rhsjacobian.scale);
594:       }
595:     }
596:     ts->rhsjacobian.shift = 0;
597:     ts->rhsjacobian.scale = 1.;
598:     return(0);
599:   }

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

603:   if (ts->rhsjacobian.reuse) {
604:     if (A == ts->Arhs) {
605:       /* MatScale has a short path for this case.
606:          However, this code path is taken the first time TSComputeRHSJacobian is called
607:          and the matrices have not assembled yet */
608:       if (ts->rhsjacobian.shift != 0) {
609:         MatShift(A,-ts->rhsjacobian.shift);
610:       }
611:       if (ts->rhsjacobian.scale != 1.) {
612:         MatScale(A,1./ts->rhsjacobian.scale);
613:       }
614:     }
615:     if (B && B == ts->Brhs && A != B) {
616:       if (ts->rhsjacobian.shift != 0) {
617:         MatShift(B,-ts->rhsjacobian.shift);
618:       }
619:       if (ts->rhsjacobian.scale != 1.) {
620:         MatScale(B,1./ts->rhsjacobian.scale);
621:       }
622:     }
623:   }

625:   if (rhsjacobianfunc) {
626:     PetscBool missing;
627:     PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);
628:     PetscStackPush("TS user Jacobian function");
629:     (*rhsjacobianfunc)(ts,t,U,A,B,ctx);
630:     PetscStackPop;
631:     PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);
632:     if (A) {
633:       MatMissingDiagonal(A,&missing,NULL);
634:       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");
635:     }
636:     if (B && B != A) {
637:       MatMissingDiagonal(B,&missing,NULL);
638:       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");
639:     }
640:   } else {
641:     MatZeroEntries(A);
642:     if (B && A != B) {MatZeroEntries(B);}
643:   }
644:   ts->rhsjacobian.time  = t;
645:   ts->rhsjacobian.shift = 0;
646:   ts->rhsjacobian.scale = 1.;
647:   PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);
648:   PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);
649:   return(0);
650: }

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

655:    Collective on TS and Vec

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

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

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

669:    Level: developer

671: .keywords: TS, compute

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

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

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

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

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

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

709:    Collective on TS and Vec

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

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

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

722:    Level: developer

724: .keywords: TS, compute

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

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

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

751:    Collective on TS and Vec

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

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

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

764:    Level: developer

766: .keywords: TS, compute

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

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

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

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

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

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

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

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

857:    Collective on TS and Vec

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

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

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

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

876:    Level: developer

878: .keywords: TS, compute

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


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

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

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

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

930:    Collective on TS and Vec

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

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

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

948:    dF/dU + shift*dF/dUdot

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

953:    Level: developer

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

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


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

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

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

1063: /*@C
1064:     TSSetRHSFunction - Sets the routine for evaluating the function,
1065:     where U_t = G(t,u).

1067:     Logically Collective on TS

1069:     Input Parameters:
1070: +   ts - the TS context obtained from TSCreate()
1071: .   r - vector to put the computed right hand side (or NULL to have it created)
1072: .   f - routine for evaluating the right-hand-side function
1073: -   ctx - [optional] user-defined context for private data for the
1074:           function evaluation routine (may be NULL)

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

1079: +   t - current timestep
1080: .   u - input vector
1081: .   F - function vector
1082: -   ctx - [optional] user-defined function context

1084:     Level: beginner

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

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

1091: .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1092: @*/
1093: PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1094: {
1096:   SNES           snes;
1097:   Vec            ralloc = NULL;
1098:   DM             dm;


1104:   TSGetDM(ts,&dm);
1105:   DMTSSetRHSFunction(dm,f,ctx);
1106:   TSGetSNES(ts,&snes);
1107:   if (!r && !ts->dm && ts->vec_sol) {
1108:     VecDuplicate(ts->vec_sol,&ralloc);
1109:     r = ralloc;
1110:   }
1111:   SNESSetFunction(snes,r,SNESTSFormFunction,ts);
1112:   VecDestroy(&ralloc);
1113:   return(0);
1114: }

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

1119:     Logically Collective on TS

1121:     Input Parameters:
1122: +   ts - the TS context obtained from TSCreate()
1123: .   f - routine for evaluating the solution
1124: -   ctx - [optional] user-defined context for private data for the
1125:           function evaluation routine (may be NULL)

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

1130: +   t - current timestep
1131: .   u - output vector
1132: -   ctx - [optional] user-defined function context

1134:     Options Database:
1135: +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
1136: -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()

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

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

1145:     Level: beginner

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

1149: .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1150: @*/
1151: PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1152: {
1154:   DM             dm;

1158:   TSGetDM(ts,&dm);
1159:   DMTSSetSolutionFunction(dm,f,ctx);
1160:   return(0);
1161: }

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

1166:     Logically Collective on TS

1168:     Input Parameters:
1169: +   ts - the TS context obtained from TSCreate()
1170: .   func - routine for evaluating the forcing function
1171: -   ctx - [optional] user-defined context for private data for the
1172:           function evaluation routine (may be NULL)

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

1177: +   t - current timestep
1178: .   f - output vector
1179: -   ctx - [optional] user-defined function context

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

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

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

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

1193:     Level: beginner

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

1197: .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
1198: @*/
1199: PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
1200: {
1202:   DM             dm;

1206:   TSGetDM(ts,&dm);
1207:   DMTSSetForcingFunction(dm,func,ctx);
1208:   return(0);
1209: }

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

1215:    Logically Collective on TS

1217:    Input Parameters:
1218: +  ts  - the TS context obtained from TSCreate()
1219: .  Amat - (approximate) Jacobian matrix
1220: .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1221: .  f   - the Jacobian evaluation routine
1222: -  ctx - [optional] user-defined context for private data for the
1223:          Jacobian evaluation routine (may be NULL)

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

1228: +  t - current timestep
1229: .  u - input vector
1230: .  Amat - (approximate) Jacobian matrix
1231: .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1232: -  ctx - [optional] user-defined context for matrix evaluation routine

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

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

1240:    Level: beginner

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

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

1246: @*/
1247: PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1248: {
1250:   SNES           snes;
1251:   DM             dm;
1252:   TSIJacobian    ijacobian;


1261:   TSGetDM(ts,&dm);
1262:   DMTSSetRHSJacobian(dm,f,ctx);
1263:   if (f == TSComputeRHSJacobianConstant) {
1264:     /* Handle this case automatically for the user; otherwise user should call themselves. */
1265:     TSRHSJacobianSetReuse(ts,PETSC_TRUE);
1266:   }
1267:   DMTSGetIJacobian(dm,&ijacobian,NULL);
1268:   TSGetSNES(ts,&snes);
1269:   if (!ijacobian) {
1270:     SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);
1271:   }
1272:   if (Amat) {
1273:     PetscObjectReference((PetscObject)Amat);
1274:     MatDestroy(&ts->Arhs);
1275:     ts->Arhs = Amat;
1276:   }
1277:   if (Pmat) {
1278:     PetscObjectReference((PetscObject)Pmat);
1279:     MatDestroy(&ts->Brhs);
1280:     ts->Brhs = Pmat;
1281:   }
1282:   return(0);
1283: }

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

1288:    Logically Collective on TS

1290:    Input Parameters:
1291: +  ts  - the TS context obtained from TSCreate()
1292: .  r   - vector to hold the residual (or NULL to have it created internally)
1293: .  f   - the function evaluation routine
1294: -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)

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

1299: +  t   - time at step/stage being solved
1300: .  u   - state vector
1301: .  u_t - time derivative of state vector
1302: .  F   - function vector
1303: -  ctx - [optional] user-defined context for matrix evaluation routine

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

1308:    Level: beginner

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

1312: .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1313: @*/
1314: PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1315: {
1317:   SNES           snes;
1318:   Vec            ralloc = NULL;
1319:   DM             dm;


1325:   TSGetDM(ts,&dm);
1326:   DMTSSetIFunction(dm,f,ctx);

1328:   TSGetSNES(ts,&snes);
1329:   if (!r && !ts->dm && ts->vec_sol) {
1330:     VecDuplicate(ts->vec_sol,&ralloc);
1331:     r  = ralloc;
1332:   }
1333:   SNESSetFunction(snes,r,SNESTSFormFunction,ts);
1334:   VecDestroy(&ralloc);
1335:   return(0);
1336: }

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

1341:    Not Collective

1343:    Input Parameter:
1344: .  ts - the TS context

1346:    Output Parameter:
1347: +  r - vector to hold residual (or NULL)
1348: .  func - the function to compute residual (or NULL)
1349: -  ctx - the function context (or NULL)

1351:    Level: advanced

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

1355: .seealso: TSSetIFunction(), SNESGetFunction()
1356: @*/
1357: PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1358: {
1360:   SNES           snes;
1361:   DM             dm;

1365:   TSGetSNES(ts,&snes);
1366:   SNESGetFunction(snes,r,NULL,NULL);
1367:   TSGetDM(ts,&dm);
1368:   DMTSGetIFunction(dm,func,ctx);
1369:   return(0);
1370: }

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

1375:    Not Collective

1377:    Input Parameter:
1378: .  ts - the TS context

1380:    Output Parameter:
1381: +  r - vector to hold computed right hand side (or NULL)
1382: .  func - the function to compute right hand side (or NULL)
1383: -  ctx - the function context (or NULL)

1385:    Level: advanced

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

1389: .seealso: TSSetRHSFunction(), SNESGetFunction()
1390: @*/
1391: PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1392: {
1394:   SNES           snes;
1395:   DM             dm;

1399:   TSGetSNES(ts,&snes);
1400:   SNESGetFunction(snes,r,NULL,NULL);
1401:   TSGetDM(ts,&dm);
1402:   DMTSGetRHSFunction(dm,func,ctx);
1403:   return(0);
1404: }

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

1410:    Logically Collective on TS

1412:    Input Parameters:
1413: +  ts  - the TS context obtained from TSCreate()
1414: .  Amat - (approximate) Jacobian matrix
1415: .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1416: .  f   - the Jacobian evaluation routine
1417: -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)

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

1422: +  t    - time at step/stage being solved
1423: .  U    - state vector
1424: .  U_t  - time derivative of state vector
1425: .  a    - shift
1426: .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1427: .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1428: -  ctx  - [optional] user-defined context for matrix evaluation routine

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

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

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

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

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

1448:    Level: beginner

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

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

1454: @*/
1455: PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1456: {
1458:   SNES           snes;
1459:   DM             dm;


1468:   TSGetDM(ts,&dm);
1469:   DMTSSetIJacobian(dm,f,ctx);

1471:   TSGetSNES(ts,&snes);
1472:   SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);
1473:   return(0);
1474: }

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

1482:    Logically Collective

1484:    Input Arguments:
1485: +  ts - TS context obtained from TSCreate()
1486: -  reuse - PETSC_TRUE if the RHS Jacobian

1488:    Level: intermediate

1490: .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1491: @*/
1492: PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1493: {
1495:   ts->rhsjacobian.reuse = reuse;
1496:   return(0);
1497: }

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

1502:    Logically Collective on TS

1504:    Input Parameters:
1505: +  ts  - the TS context obtained from TSCreate()
1506: .  F   - vector to hold the residual (or NULL to have it created internally)
1507: .  fun - the function evaluation routine
1508: -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)

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

1513: +  t    - time at step/stage being solved
1514: .  U    - state vector
1515: .  U_t  - time derivative of state vector
1516: .  U_tt - second time derivative of state vector
1517: .  F    - function vector
1518: -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)

1520:    Level: beginner

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

1524: .seealso: TSSetI2Jacobian()
1525: @*/
1526: PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1527: {
1528:   DM             dm;

1534:   TSSetIFunction(ts,F,NULL,NULL);
1535:   TSGetDM(ts,&dm);
1536:   DMTSSetI2Function(dm,fun,ctx);
1537:   return(0);
1538: }

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

1543:   Not Collective

1545:   Input Parameter:
1546: . ts - the TS context

1548:   Output Parameter:
1549: + r - vector to hold residual (or NULL)
1550: . fun - the function to compute residual (or NULL)
1551: - ctx - the function context (or NULL)

1553:   Level: advanced

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

1557: .seealso: TSSetI2Function(), SNESGetFunction()
1558: @*/
1559: PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1560: {
1562:   SNES           snes;
1563:   DM             dm;

1567:   TSGetSNES(ts,&snes);
1568:   SNESGetFunction(snes,r,NULL,NULL);
1569:   TSGetDM(ts,&dm);
1570:   DMTSGetI2Function(dm,fun,ctx);
1571:   return(0);
1572: }

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

1578:    Logically Collective on TS

1580:    Input Parameters:
1581: +  ts  - the TS context obtained from TSCreate()
1582: .  J   - Jacobian matrix
1583: .  P   - preconditioning matrix for J (may be same as J)
1584: .  jac - the Jacobian evaluation routine
1585: -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)

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

1590: +  t    - time at step/stage being solved
1591: .  U    - state vector
1592: .  U_t  - time derivative of state vector
1593: .  U_tt - second time derivative of state vector
1594: .  v    - shift for U_t
1595: .  a    - shift for U_tt
1596: .  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
1597: .  P    - preconditioning matrix for J, may be same as J
1598: -  ctx  - [optional] user-defined context for matrix evaluation routine

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

1603:    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1604:    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.
1605:    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1606:    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.

1608:    Level: beginner

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

1612: .seealso: TSSetI2Function()
1613: @*/
1614: PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1615: {
1616:   DM             dm;

1623:   TSSetIJacobian(ts,J,P,NULL,NULL);
1624:   TSGetDM(ts,&dm);
1625:   DMTSSetI2Jacobian(dm,jac,ctx);
1626:   return(0);
1627: }

1629: /*@C
1630:   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.

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

1634:   Input Parameter:
1635: . ts  - The TS context obtained from TSCreate()

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

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

1646:   Level: advanced

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

1650: .keywords: TS, timestep, get, matrix, Jacobian
1651: @*/
1652: PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1653: {
1655:   SNES           snes;
1656:   DM             dm;

1659:   TSGetSNES(ts,&snes);
1660:   SNESSetUpMatrices(snes);
1661:   SNESGetJacobian(snes,J,P,NULL,NULL);
1662:   TSGetDM(ts,&dm);
1663:   DMTSGetI2Jacobian(dm,jac,ctx);
1664:   return(0);
1665: }

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

1670:   Collective on TS and Vec

1672:   Input Parameters:
1673: + ts - the TS context
1674: . t - current time
1675: . U - state vector
1676: . V - time derivative of state vector (U_t)
1677: - A - second time derivative of state vector (U_tt)

1679:   Output Parameter:
1680: . F - the residual vector

1682:   Note:
1683:   Most users should not need to explicitly call this routine, as it
1684:   is used internally within the nonlinear solvers.

1686:   Level: developer

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

1690: .seealso: TSSetI2Function()
1691: @*/
1692: PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1693: {
1694:   DM             dm;
1695:   TSI2Function   I2Function;
1696:   void           *ctx;
1697:   TSRHSFunction  rhsfunction;


1707:   TSGetDM(ts,&dm);
1708:   DMTSGetI2Function(dm,&I2Function,&ctx);
1709:   DMTSGetRHSFunction(dm,&rhsfunction,NULL);

1711:   if (!I2Function) {
1712:     TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);
1713:     return(0);
1714:   }

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

1718:   PetscStackPush("TS user implicit function");
1719:   I2Function(ts,t,U,V,A,F,ctx);
1720:   PetscStackPop;

1722:   if (rhsfunction) {
1723:     Vec Frhs;
1724:     TSGetRHSVec_Private(ts,&Frhs);
1725:     TSComputeRHSFunction(ts,t,U,Frhs);
1726:     VecAXPY(F,-1,Frhs);
1727:   }

1729:   PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);
1730:   return(0);
1731: }

1733: /*@
1734:   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE

1736:   Collective on TS and Vec

1738:   Input Parameters:
1739: + ts - the TS context
1740: . t - current timestep
1741: . U - state vector
1742: . V - time derivative of state vector
1743: . A - second time derivative of state vector
1744: . shiftV - shift to apply, see note below
1745: - shiftA - shift to apply, see note below

1747:   Output Parameters:
1748: + J - Jacobian matrix
1749: - P - optional preconditioning matrix

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

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

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

1759:   Level: developer

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

1763: .seealso:  TSSetI2Jacobian()
1764: @*/
1765: PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1766: {
1767:   DM             dm;
1768:   TSI2Jacobian   I2Jacobian;
1769:   void           *ctx;
1770:   TSRHSJacobian  rhsjacobian;


1781:   TSGetDM(ts,&dm);
1782:   DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);
1783:   DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);

1785:   if (!I2Jacobian) {
1786:     TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);
1787:     return(0);
1788:   }

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

1792:   PetscStackPush("TS user implicit Jacobian");
1793:   I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);
1794:   PetscStackPop;

1796:   if (rhsjacobian) {
1797:     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1798:     TSGetRHSMats_Private(ts,&Jrhs,&Prhs);
1799:     TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);
1800:     MatAXPY(J,-1,Jrhs,axpy);
1801:     if (P != J) {MatAXPY(P,-1,Prhs,axpy);}
1802:   }

1804:   PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);
1805:   return(0);
1806: }

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

1812:    Logically Collective on TS and Vec

1814:    Input Parameters:
1815: +  ts - the TS context obtained from TSCreate()
1816: .  u - the solution vector
1817: -  v - the time derivative vector

1819:    Level: beginner

1821: .keywords: TS, timestep, set, solution, initial conditions
1822: @*/
1823: PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1824: {

1831:   TSSetSolution(ts,u);
1832:   PetscObjectReference((PetscObject)v);
1833:   VecDestroy(&ts->vec_dot);
1834:   ts->vec_dot = v;
1835:   return(0);
1836: }

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

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

1846:    Input Parameter:
1847: .  ts - the TS context obtained from TSCreate()

1849:    Output Parameter:
1850: +  u - the vector containing the solution
1851: -  v - the vector containing the time derivative

1853:    Level: intermediate

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

1857: .keywords: TS, timestep, get, solution
1858: @*/
1859: PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1860: {
1865:   if (u) *u = ts->vec_sol;
1866:   if (v) *v = ts->vec_dot;
1867:   return(0);
1868: }

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

1873:   Collective on PetscViewer

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

1880:    Level: intermediate

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

1885:   Notes for advanced users:
1886:   Most users should not need to know the details of the binary storage
1887:   format, since TSLoad() and TSView() completely hide these details.
1888:   But for anyone who's interested, the standard binary matrix storage
1889:   format is
1890: .vb
1891:      has not yet been determined
1892: .ve

1894: .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
1895: @*/
1896: PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
1897: {
1899:   PetscBool      isbinary;
1900:   PetscInt       classid;
1901:   char           type[256];
1902:   DMTS           sdm;
1903:   DM             dm;

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

1911:   PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);
1912:   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1913:   PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);
1914:   TSSetType(ts, type);
1915:   if (ts->ops->load) {
1916:     (*ts->ops->load)(ts,viewer);
1917:   }
1918:   DMCreate(PetscObjectComm((PetscObject)ts),&dm);
1919:   DMLoad(dm,viewer);
1920:   TSSetDM(ts,dm);
1921:   DMCreateGlobalVector(ts->dm,&ts->vec_sol);
1922:   VecLoad(ts->vec_sol,viewer);
1923:   DMGetDMTS(ts->dm,&sdm);
1924:   DMTSLoad(sdm,viewer);
1925:   return(0);
1926: }

1928:  #include <petscdraw.h>
1929: #if defined(PETSC_HAVE_SAWS)
1930:  #include <petscviewersaws.h>
1931: #endif
1932: /*@C
1933:     TSView - Prints the TS data structure.

1935:     Collective on TS

1937:     Input Parameters:
1938: +   ts - the TS context obtained from TSCreate()
1939: -   viewer - visualization context

1941:     Options Database Key:
1942: .   -ts_view - calls TSView() at end of TSStep()

1944:     Notes:
1945:     The available visualization contexts include
1946: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1947: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1948:          output where only the first processor opens
1949:          the file.  All other processors send their
1950:          data to the first processor to print.

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

1955:     Level: beginner

1957: .keywords: TS, timestep, view

1959: .seealso: PetscViewerASCIIOpen()
1960: @*/
1961: PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1962: {
1964:   TSType         type;
1965:   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
1966:   DMTS           sdm;
1967: #if defined(PETSC_HAVE_SAWS)
1968:   PetscBool      issaws;
1969: #endif

1973:   if (!viewer) {
1974:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);
1975:   }

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

2032:     PetscObjectGetComm((PetscObject)ts,&comm);
2033:     MPI_Comm_rank(comm,&rank);
2034:     if (!rank) {
2035:       PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);
2036:       PetscStrncpy(type,((PetscObject)ts)->type_name,256);
2037:       PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);
2038:     }
2039:     if (ts->ops->view) {
2040:       (*ts->ops->view)(ts,viewer);
2041:     }
2042:     if (ts->adapt) {TSAdaptView(ts->adapt,viewer);}
2043:     DMView(ts->dm,viewer);
2044:     VecView(ts->vec_sol,viewer);
2045:     DMGetDMTS(ts->dm,&sdm);
2046:     DMTSView(sdm,viewer);
2047:   } else if (isdraw) {
2048:     PetscDraw draw;
2049:     char      str[36];
2050:     PetscReal x,y,bottom,h;

2052:     PetscViewerDrawGetDraw(viewer,0,&draw);
2053:     PetscDrawGetCurrentPoint(draw,&x,&y);
2054:     PetscStrcpy(str,"TS: ");
2055:     PetscStrcat(str,((PetscObject)ts)->type_name);
2056:     PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);
2057:     bottom = y - h;
2058:     PetscDrawPushCurrentPoint(draw,x,bottom);
2059:     if (ts->ops->view) {
2060:       (*ts->ops->view)(ts,viewer);
2061:     }
2062:     if (ts->adapt) {TSAdaptView(ts->adapt,viewer);}
2063:     if (ts->snes)  {SNESView(ts->snes,viewer);}
2064:     PetscDrawPopCurrentPoint(draw);
2065: #if defined(PETSC_HAVE_SAWS)
2066:   } else if (issaws) {
2067:     PetscMPIInt rank;
2068:     const char  *name;

2070:     PetscObjectGetName((PetscObject)ts,&name);
2071:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
2072:     if (!((PetscObject)ts)->amsmem && !rank) {
2073:       char       dir[1024];

2075:       PetscObjectViewSAWs((PetscObject)ts,viewer);
2076:       PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);
2077:       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
2078:       PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);
2079:       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2080:     }
2081:     if (ts->ops->view) {
2082:       (*ts->ops->view)(ts,viewer);
2083:     }
2084: #endif
2085:   }
2086:   if (ts->snes && ts->usessnes)  {
2087:     PetscViewerASCIIPushTab(viewer);
2088:     SNESView(ts->snes,viewer);
2089:     PetscViewerASCIIPopTab(viewer);
2090:   }
2091:   DMGetDMTS(ts->dm,&sdm);
2092:   DMTSView(sdm,viewer);

2094:   PetscViewerASCIIPushTab(viewer);
2095:   PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);
2096:   PetscViewerASCIIPopTab(viewer);
2097:   return(0);
2098: }

2100: /*@
2101:    TSSetApplicationContext - Sets an optional user-defined context for
2102:    the timesteppers.

2104:    Logically Collective on TS

2106:    Input Parameters:
2107: +  ts - the TS context obtained from TSCreate()
2108: -  usrP - optional user context

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

2114:    Level: intermediate

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

2118: .seealso: TSGetApplicationContext()
2119: @*/
2120: PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2121: {
2124:   ts->user = usrP;
2125:   return(0);
2126: }

2128: /*@
2129:     TSGetApplicationContext - Gets the user-defined context for the
2130:     timestepper.

2132:     Not Collective

2134:     Input Parameter:
2135: .   ts - the TS context obtained from TSCreate()

2137:     Output Parameter:
2138: .   usrP - user context

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

2144:     Level: intermediate

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

2148: .seealso: TSSetApplicationContext()
2149: @*/
2150: PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2151: {
2154:   *(void**)usrP = ts->user;
2155:   return(0);
2156: }

2158: /*@
2159:    TSGetStepNumber - Gets the number of steps completed.

2161:    Not Collective

2163:    Input Parameter:
2164: .  ts - the TS context obtained from TSCreate()

2166:    Output Parameter:
2167: .  steps - number of steps completed so far

2169:    Level: intermediate

2171: .keywords: TS, timestep, get, iteration, number
2172: .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2173: @*/
2174: PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2175: {
2179:   *steps = ts->steps;
2180:   return(0);
2181: }

2183: /*@
2184:    TSSetStepNumber - Sets the number of steps completed.

2186:    Logically Collective on TS

2188:    Input Parameters:
2189: +  ts - the TS context
2190: -  steps - number of steps completed so far

2192:    Notes:
2193:    For most uses of the TS solvers the user need not explicitly call
2194:    TSSetStepNumber(), as the step counter is appropriately updated in
2195:    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
2196:    reinitialize timestepping by setting the step counter to zero (and time
2197:    to the initial time) to solve a similar problem with different initial
2198:    conditions or parameters. Other possible use case is to continue
2199:    timestepping from a previously interrupted run in such a way that TS
2200:    monitors will be called with a initial nonzero step counter.

2202:    Level: advanced

2204: .keywords: TS, timestep, set, iteration, number
2205: .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
2206: @*/
2207: PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
2208: {
2212:   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
2213:   ts->steps = steps;
2214:   return(0);
2215: }

2217: /*@
2218:    TSSetTimeStep - Allows one to reset the timestep at any time,
2219:    useful for simple pseudo-timestepping codes.

2221:    Logically Collective on TS

2223:    Input Parameters:
2224: +  ts - the TS context obtained from TSCreate()
2225: -  time_step - the size of the timestep

2227:    Level: intermediate

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

2231: .keywords: TS, set, timestep
2232: @*/
2233: PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2234: {
2238:   ts->time_step = time_step;
2239:   return(0);
2240: }

2242: /*@
2243:    TSSetExactFinalTime - Determines whether to adapt the final time step to
2244:      match the exact final time, interpolate solution to the exact final time,
2245:      or just return at the final time TS computed.

2247:   Logically Collective on TS

2249:    Input Parameter:
2250: +   ts - the time-step context
2251: -   eftopt - exact final time option

2253: $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2254: $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2255: $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time

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

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

2263:    Level: beginner

2265: .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2266: @*/
2267: PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2268: {
2272:   ts->exact_final_time = eftopt;
2273:   return(0);
2274: }

2276: /*@
2277:    TSGetExactFinalTime - Gets the exact final time option.

2279:    Not Collective

2281:    Input Parameter:
2282: .  ts - the TS context

2284:    Output Parameter:
2285: .  eftopt - exact final time option

2287:    Level: beginner

2289: .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2290: @*/
2291: PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2292: {
2296:   *eftopt = ts->exact_final_time;
2297:   return(0);
2298: }

2300: /*@
2301:    TSGetTimeStep - Gets the current timestep size.

2303:    Not Collective

2305:    Input Parameter:
2306: .  ts - the TS context obtained from TSCreate()

2308:    Output Parameter:
2309: .  dt - the current timestep size

2311:    Level: intermediate

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

2315: .keywords: TS, get, timestep
2316: @*/
2317: PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2318: {
2322:   *dt = ts->time_step;
2323:   return(0);
2324: }

2326: /*@
2327:    TSGetSolution - Returns the solution at the present timestep. It
2328:    is valid to call this routine inside the function that you are evaluating
2329:    in order to move to the new timestep. This vector not changed until
2330:    the solution at the next timestep has been calculated.

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

2334:    Input Parameter:
2335: .  ts - the TS context obtained from TSCreate()

2337:    Output Parameter:
2338: .  v - the vector containing the solution

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

2343:    Level: intermediate

2345: .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()

2347: .keywords: TS, timestep, get, solution
2348: @*/
2349: PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2350: {
2354:   *v = ts->vec_sol;
2355:   return(0);
2356: }

2358: /*@
2359:    TSGetSolutionComponents - Returns any solution components at the present
2360:    timestep, if available for the time integration method being used.
2361:    Solution components are quantities that share the same size and
2362:    structure as the solution vector.

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

2366:    Parameters :
2367: .  ts - the TS context obtained from TSCreate() (input parameter).
2368: .  n - If v is PETSC_NULL, then the number of solution components is
2369:        returned through n, else the n-th solution component is
2370:        returned in v.
2371: .  v - the vector containing the n-th solution component
2372:        (may be PETSC_NULL to use this function to find out
2373:         the number of solutions components).

2375:    Level: advanced

2377: .seealso: TSGetSolution()

2379: .keywords: TS, timestep, get, solution
2380: @*/
2381: PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
2382: {

2387:   if (!ts->ops->getsolutioncomponents) *n = 0;
2388:   else {
2389:     (*ts->ops->getsolutioncomponents)(ts,n,v);
2390:   }
2391:   return(0);
2392: }

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

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

2400:    Parameters :
2401: .  ts - the TS context obtained from TSCreate() (input parameter).
2402: .  v - the vector containing the auxiliary solution

2404:    Level: intermediate

2406: .seealso: TSGetSolution()

2408: .keywords: TS, timestep, get, solution
2409: @*/
2410: PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
2411: {

2416:   if (ts->ops->getauxsolution) {
2417:     (*ts->ops->getauxsolution)(ts,v);
2418:   } else {
2419:     VecZeroEntries(*v);
2420:   }
2421:   return(0);
2422: }

2424: /*@
2425:    TSGetTimeError - Returns the estimated error vector, if the chosen
2426:    TSType has an error estimation functionality.

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

2430:    Note: MUST call after TSSetUp()

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

2437:    Level: intermediate

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

2441: .keywords: TS, timestep, get, error
2442: @*/
2443: PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
2444: {

2449:   if (ts->ops->gettimeerror) {
2450:     (*ts->ops->gettimeerror)(ts,n,v);
2451:   } else {
2452:     VecZeroEntries(*v);
2453:   }
2454:   return(0);
2455: }

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

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

2464:    Parameters :
2465: .  ts - the TS context obtained from TSCreate() (input parameter).
2466: .  v - the vector containing the error (same size as the solution).

2468:    Level: intermediate

2470: .seealso: TSSetSolution(), TSGetTimeError)

2472: .keywords: TS, timestep, get, error
2473: @*/
2474: PetscErrorCode  TSSetTimeError(TS ts,Vec v)
2475: {

2480:   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
2481:   if (ts->ops->settimeerror) {
2482:     (*ts->ops->settimeerror)(ts,v);
2483:   }
2484:   return(0);
2485: }

2487: /* ----- Routines to initialize and destroy a timestepper ---- */
2488: /*@
2489:   TSSetProblemType - Sets the type of problem to be solved.

2491:   Not collective

2493:   Input Parameters:
2494: + ts   - The TS
2495: - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2496: .vb
2497:          U_t - A U = 0      (linear)
2498:          U_t - A(t) U = 0   (linear)
2499:          F(t,U,U_t) = 0     (nonlinear)
2500: .ve

2502:    Level: beginner

2504: .keywords: TS, problem type
2505: .seealso: TSSetUp(), TSProblemType, TS
2506: @*/
2507: PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2508: {

2513:   ts->problem_type = type;
2514:   if (type == TS_LINEAR) {
2515:     SNES snes;
2516:     TSGetSNES(ts,&snes);
2517:     SNESSetType(snes,SNESKSPONLY);
2518:   }
2519:   return(0);
2520: }

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

2525:   Not collective

2527:   Input Parameter:
2528: . ts   - The TS

2530:   Output Parameter:
2531: . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2532: .vb
2533:          M U_t = A U
2534:          M(t) U_t = A(t) U
2535:          F(t,U,U_t)
2536: .ve

2538:    Level: beginner

2540: .keywords: TS, problem type
2541: .seealso: TSSetUp(), TSProblemType, TS
2542: @*/
2543: PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2544: {
2548:   *type = ts->problem_type;
2549:   return(0);
2550: }

2552: /*@
2553:    TSSetUp - Sets up the internal data structures for the later use
2554:    of a timestepper.

2556:    Collective on TS

2558:    Input Parameter:
2559: .  ts - the TS context obtained from TSCreate()

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

2568:    Level: advanced

2570: .keywords: TS, timestep, setup

2572: .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2573: @*/
2574: PetscErrorCode  TSSetUp(TS ts)
2575: {
2577:   DM             dm;
2578:   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2579:   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2580:   TSIFunction    ifun;
2581:   TSIJacobian    ijac;
2582:   TSI2Jacobian   i2jac;
2583:   TSRHSJacobian  rhsjac;
2584:   PetscBool      isnone;

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

2590:   if (!((PetscObject)ts)->type_name) {
2591:     TSGetIFunction(ts,NULL,&ifun,NULL);
2592:     TSSetType(ts,ifun ? TSBEULER : TSEULER);
2593:   }

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

2597:   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2598:     PetscObjectReference((PetscObject)ts->Jacprhs);
2599:     ts->Jacp = ts->Jacprhs;
2600:   }

2602:   if (ts->quadraturets) {
2603:     TSSetUp(ts->quadraturets);
2604:     VecDestroy(&ts->vec_costintegrand);
2605:     VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);
2606:   }

2608:   TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);
2609:   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2610:     Mat Amat,Pmat;
2611:     SNES snes;
2612:     TSGetSNES(ts,&snes);
2613:     SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);
2614:     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2615:      * have displaced the RHS matrix */
2616:     if (Amat && Amat == ts->Arhs) {
2617:       /* 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 */
2618:       MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);
2619:       SNESSetJacobian(snes,Amat,NULL,NULL,NULL);
2620:       MatDestroy(&Amat);
2621:     }
2622:     if (Pmat && Pmat == ts->Brhs) {
2623:       MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);
2624:       SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);
2625:       MatDestroy(&Pmat);
2626:     }
2627:   }

2629:   TSGetAdapt(ts,&ts->adapt);
2630:   TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);

2632:   if (ts->ops->setup) {
2633:     (*ts->ops->setup)(ts);
2634:   }

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

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

2660:   /* if time integration scheme has a starting method, call it */
2661:   if (ts->ops->startingmethod) {
2662:     (*ts->ops->startingmethod)(ts);
2663:   }

2665:   ts->setupcalled = PETSC_TRUE;
2666:   return(0);
2667: }

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

2672:    Collective on TS

2674:    Input Parameter:
2675: .  ts - the TS context obtained from TSCreate()

2677:    Level: beginner

2679: .keywords: TS, timestep, reset

2681: .seealso: TSCreate(), TSSetup(), TSDestroy()
2682: @*/
2683: PetscErrorCode  TSReset(TS ts)
2684: {
2685:   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2686:   PetscErrorCode  ierr;


2691:   if (ts->ops->reset) {
2692:     (*ts->ops->reset)(ts);
2693:   }
2694:   if (ts->snes) {SNESReset(ts->snes);}
2695:   if (ts->adapt) {TSAdaptReset(ts->adapt);}

2697:   MatDestroy(&ts->Arhs);
2698:   MatDestroy(&ts->Brhs);
2699:   VecDestroy(&ts->Frhs);
2700:   VecDestroy(&ts->vec_sol);
2701:   VecDestroy(&ts->vec_dot);
2702:   VecDestroy(&ts->vatol);
2703:   VecDestroy(&ts->vrtol);
2704:   VecDestroyVecs(ts->nwork,&ts->work);

2706:   MatDestroy(&ts->Jacprhs);
2707:   MatDestroy(&ts->Jacp);
2708:   if (ts->forward_solve) {
2709:     TSForwardReset(ts);
2710:   }
2711:   if (ts->quadraturets) {
2712:     TSReset(ts->quadraturets);
2713:     VecDestroy(&ts->vec_costintegrand);
2714:   }
2715:   while (ilink) {
2716:     next = ilink->next;
2717:     TSDestroy(&ilink->ts);
2718:     PetscFree(ilink->splitname);
2719:     ISDestroy(&ilink->is);
2720:     PetscFree(ilink);
2721:     ilink = next;
2722:   }
2723:   ts->num_rhs_splits = 0;
2724:   ts->setupcalled = PETSC_FALSE;
2725:   return(0);
2726: }

2728: /*@
2729:    TSDestroy - Destroys the timestepper context that was created
2730:    with TSCreate().

2732:    Collective on TS

2734:    Input Parameter:
2735: .  ts - the TS context obtained from TSCreate()

2737:    Level: beginner

2739: .keywords: TS, timestepper, destroy

2741: .seealso: TSCreate(), TSSetUp(), TSSolve()
2742: @*/
2743: PetscErrorCode  TSDestroy(TS *ts)
2744: {

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

2752:   TSReset(*ts);
2753:   TSAdjointReset(*ts);
2754:   if ((*ts)->forward_solve) {
2755:     TSForwardReset(*ts);
2756:   }
2757:   /* if memory was published with SAWs then destroy it */
2758:   PetscObjectSAWsViewOff((PetscObject)*ts);
2759:   if ((*ts)->ops->destroy) {(*(*ts)->ops->destroy)((*ts));}

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

2763:   TSAdaptDestroy(&(*ts)->adapt);
2764:   TSEventDestroy(&(*ts)->event);

2766:   SNESDestroy(&(*ts)->snes);
2767:   DMDestroy(&(*ts)->dm);
2768:   TSMonitorCancel((*ts));
2769:   TSAdjointMonitorCancel((*ts));

2771:   TSDestroy(&(*ts)->quadraturets);
2772:   PetscHeaderDestroy(ts);
2773:   return(0);
2774: }

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

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

2782:    Input Parameter:
2783: .  ts - the TS context obtained from TSCreate()

2785:    Output Parameter:
2786: .  snes - the nonlinear solver context

2788:    Notes:
2789:    The user can then directly manipulate the SNES context to set various
2790:    options, etc.  Likewise, the user can then extract and manipulate the
2791:    KSP, KSP, and PC contexts as well.

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

2796:    Level: beginner

2798: .keywords: timestep, get, SNES
2799: @*/
2800: PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2801: {

2807:   if (!ts->snes) {
2808:     SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);
2809:     PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);
2810:     SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);
2811:     PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);
2812:     PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);
2813:     if (ts->dm) {SNESSetDM(ts->snes,ts->dm);}
2814:     if (ts->problem_type == TS_LINEAR) {
2815:       SNESSetType(ts->snes,SNESKSPONLY);
2816:     }
2817:   }
2818:   *snes = ts->snes;
2819:   return(0);
2820: }

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

2825:    Collective

2827:    Input Parameter:
2828: +  ts - the TS context obtained from TSCreate()
2829: -  snes - the nonlinear solver context

2831:    Notes:
2832:    Most users should have the TS created by calling TSGetSNES()

2834:    Level: developer

2836: .keywords: timestep, set, SNES
2837: @*/
2838: PetscErrorCode TSSetSNES(TS ts,SNES snes)
2839: {
2841:   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);

2846:   PetscObjectReference((PetscObject)snes);
2847:   SNESDestroy(&ts->snes);

2849:   ts->snes = snes;

2851:   SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);
2852:   SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);
2853:   if (func == SNESTSFormJacobian) {
2854:     SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);
2855:   }
2856:   return(0);
2857: }

2859: /*@
2860:    TSGetKSP - Returns the KSP (linear solver) associated with
2861:    a TS (timestepper) context.

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

2865:    Input Parameter:
2866: .  ts - the TS context obtained from TSCreate()

2868:    Output Parameter:
2869: .  ksp - the nonlinear solver context

2871:    Notes:
2872:    The user can then directly manipulate the KSP context to set various
2873:    options, etc.  Likewise, the user can then extract and manipulate the
2874:    KSP and PC contexts as well.

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

2879:    Level: beginner

2881: .keywords: timestep, get, KSP
2882: @*/
2883: PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2884: {
2886:   SNES           snes;

2891:   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2892:   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2893:   TSGetSNES(ts,&snes);
2894:   SNESGetKSP(snes,ksp);
2895:   return(0);
2896: }

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

2900: /*@
2901:    TSSetMaxSteps - Sets the maximum number of steps to use.

2903:    Logically Collective on TS

2905:    Input Parameters:
2906: +  ts - the TS context obtained from TSCreate()
2907: -  maxsteps - maximum number of steps to use

2909:    Options Database Keys:
2910: .  -ts_max_steps <maxsteps> - Sets maxsteps

2912:    Notes:
2913:    The default maximum number of steps is 5000

2915:    Level: intermediate

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

2919: .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2920: @*/
2921: PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2922: {
2926:   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2927:   ts->max_steps = maxsteps;
2928:   return(0);
2929: }

2931: /*@
2932:    TSGetMaxSteps - Gets the maximum number of steps to use.

2934:    Not Collective

2936:    Input Parameters:
2937: .  ts - the TS context obtained from TSCreate()

2939:    Output Parameter:
2940: .  maxsteps - maximum number of steps to use

2942:    Level: advanced

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

2946: .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2947: @*/
2948: PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2949: {
2953:   *maxsteps = ts->max_steps;
2954:   return(0);
2955: }

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

2960:    Logically Collective on TS

2962:    Input Parameters:
2963: +  ts - the TS context obtained from TSCreate()
2964: -  maxtime - final time to step to

2966:    Options Database Keys:
2967: .  -ts_max_time <maxtime> - Sets maxtime

2969:    Notes:
2970:    The default maximum time is 5.0

2972:    Level: intermediate

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

2976: .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2977: @*/
2978: PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2979: {
2983:   ts->max_time = maxtime;
2984:   return(0);
2985: }

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

2990:    Not Collective

2992:    Input Parameters:
2993: .  ts - the TS context obtained from TSCreate()

2995:    Output Parameter:
2996: .  maxtime - final time to step to

2998:    Level: advanced

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

3002: .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3003: @*/
3004: PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3005: {
3009:   *maxtime = ts->max_time;
3010:   return(0);
3011: }

3013: /*@
3014:    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().

3016:    Level: deprecated

3018: @*/
3019: PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3020: {
3024:   TSSetTime(ts,initial_time);
3025:   TSSetTimeStep(ts,time_step);
3026:   return(0);
3027: }

3029: /*@
3030:    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().

3032:    Level: deprecated

3034: @*/
3035: PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3036: {
3039:   if (maxsteps) {
3041:     *maxsteps = ts->max_steps;
3042:   }
3043:   if (maxtime) {
3045:     *maxtime = ts->max_time;
3046:   }
3047:   return(0);
3048: }

3050: /*@
3051:    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().

3053:    Level: deprecated

3055: @*/
3056: PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3057: {
3062:   if (maxsteps >= 0) ts->max_steps = maxsteps;
3063:   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3064:   return(0);
3065: }

3067: /*@
3068:    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().

3070:    Level: deprecated

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

3075: /*@
3076:    TSGetTotalSteps - Deprecated, use TSGetStepNumber().

3078:    Level: deprecated

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

3083: /*@
3084:    TSSetSolution - Sets the initial solution vector
3085:    for use by the TS routines.

3087:    Logically Collective on TS and Vec

3089:    Input Parameters:
3090: +  ts - the TS context obtained from TSCreate()
3091: -  u - the solution vector

3093:    Level: beginner

3095: .keywords: TS, timestep, set, solution, initial values

3097: .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3098: @*/
3099: PetscErrorCode  TSSetSolution(TS ts,Vec u)
3100: {
3102:   DM             dm;

3107:   PetscObjectReference((PetscObject)u);
3108:   VecDestroy(&ts->vec_sol);
3109:   ts->vec_sol = u;

3111:   TSGetDM(ts,&dm);
3112:   DMShellSetGlobalVector(dm,u);
3113:   return(0);
3114: }

3116: /*@C
3117:   TSSetPreStep - Sets the general-purpose function
3118:   called once at the beginning of each time step.

3120:   Logically Collective on TS

3122:   Input Parameters:
3123: + ts   - The TS context obtained from TSCreate()
3124: - func - The function

3126:   Calling sequence of func:
3127: . func (TS ts);

3129:   Level: intermediate

3131: .keywords: TS, timestep
3132: .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3133: @*/
3134: PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3135: {
3138:   ts->prestep = func;
3139:   return(0);
3140: }

3142: /*@
3143:   TSPreStep - Runs the user-defined pre-step function.

3145:   Collective on TS

3147:   Input Parameters:
3148: . ts   - The TS context obtained from TSCreate()

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

3154:   Level: developer

3156: .keywords: TS, timestep
3157: .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
3158: @*/
3159: PetscErrorCode  TSPreStep(TS ts)
3160: {

3165:   if (ts->prestep) {
3166:     Vec              U;
3167:     PetscObjectState sprev,spost;

3169:     TSGetSolution(ts,&U);
3170:     PetscObjectStateGet((PetscObject)U,&sprev);
3171:     PetscStackCallStandard((*ts->prestep),(ts));
3172:     PetscObjectStateGet((PetscObject)U,&spost);
3173:     if (sprev != spost) {TSRestartStep(ts);}
3174:   }
3175:   return(0);
3176: }

3178: /*@C
3179:   TSSetPreStage - Sets the general-purpose function
3180:   called once at the beginning of each stage.

3182:   Logically Collective on TS

3184:   Input Parameters:
3185: + ts   - The TS context obtained from TSCreate()
3186: - func - The function

3188:   Calling sequence of func:
3189: . PetscErrorCode func(TS ts, PetscReal stagetime);

3191:   Level: intermediate

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

3198: .keywords: TS, timestep
3199: .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3200: @*/
3201: PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3202: {
3205:   ts->prestage = func;
3206:   return(0);
3207: }

3209: /*@C
3210:   TSSetPostStage - Sets the general-purpose function
3211:   called once at the end of each stage.

3213:   Logically Collective on TS

3215:   Input Parameters:
3216: + ts   - The TS context obtained from TSCreate()
3217: - func - The function

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

3222:   Level: intermediate

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

3229: .keywords: TS, timestep
3230: .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3231: @*/
3232: PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
3233: {
3236:   ts->poststage = func;
3237:   return(0);
3238: }

3240: /*@C
3241:   TSSetPostEvaluate - Sets the general-purpose function
3242:   called once at the end of each step evaluation.

3244:   Logically Collective on TS

3246:   Input Parameters:
3247: + ts   - The TS context obtained from TSCreate()
3248: - func - The function

3250:   Calling sequence of func:
3251: . PetscErrorCode func(TS ts);

3253:   Level: intermediate

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

3262: .keywords: TS, timestep
3263: .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3264: @*/
3265: PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3266: {
3269:   ts->postevaluate = func;
3270:   return(0);
3271: }

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

3276:   Collective on TS

3278:   Input Parameters:
3279: . ts          - The TS context obtained from TSCreate()
3280:   stagetime   - The absolute time of the current stage

3282:   Notes:
3283:   TSPreStage() is typically used within time stepping implementations,
3284:   most users would not generally call this routine themselves.

3286:   Level: developer

3288: .keywords: TS, timestep
3289: .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3290: @*/
3291: PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3292: {
3295:   if (ts->prestage) {
3296:     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3297:   }
3298:   return(0);
3299: }

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

3304:   Collective on TS

3306:   Input Parameters:
3307: . ts          - The TS context obtained from TSCreate()
3308:   stagetime   - The absolute time of the current stage
3309:   stageindex  - Stage number
3310:   Y           - Array of vectors (of size = total number
3311:                 of stages) with the stage solutions

3313:   Notes:
3314:   TSPostStage() is typically used within time stepping implementations,
3315:   most users would not generally call this routine themselves.

3317:   Level: developer

3319: .keywords: TS, timestep
3320: .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3321: @*/
3322: PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
3323: {
3326:   if (ts->poststage) {
3327:     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
3328:   }
3329:   return(0);
3330: }

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

3335:   Collective on TS

3337:   Input Parameters:
3338: . ts          - The TS context obtained from TSCreate()

3340:   Notes:
3341:   TSPostEvaluate() is typically used within time stepping implementations,
3342:   most users would not generally call this routine themselves.

3344:   Level: developer

3346: .keywords: TS, timestep
3347: .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3348: @*/
3349: PetscErrorCode  TSPostEvaluate(TS ts)
3350: {

3355:   if (ts->postevaluate) {
3356:     Vec              U;
3357:     PetscObjectState sprev,spost;

3359:     TSGetSolution(ts,&U);
3360:     PetscObjectStateGet((PetscObject)U,&sprev);
3361:     PetscStackCallStandard((*ts->postevaluate),(ts));
3362:     PetscObjectStateGet((PetscObject)U,&spost);
3363:     if (sprev != spost) {TSRestartStep(ts);}
3364:   }
3365:   return(0);
3366: }

3368: /*@C
3369:   TSSetPostStep - Sets the general-purpose function
3370:   called once at the end of each time step.

3372:   Logically Collective on TS

3374:   Input Parameters:
3375: + ts   - The TS context obtained from TSCreate()
3376: - func - The function

3378:   Calling sequence of func:
3379: $ func (TS ts);

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

3386:   Level: intermediate

3388: .keywords: TS, timestep
3389: .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3390: @*/
3391: PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3392: {
3395:   ts->poststep = func;
3396:   return(0);
3397: }

3399: /*@
3400:   TSPostStep - Runs the user-defined post-step function.

3402:   Collective on TS

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

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

3411:   Level: developer

3413: .keywords: TS, timestep
3414: @*/
3415: PetscErrorCode  TSPostStep(TS ts)
3416: {

3421:   if (ts->poststep) {
3422:     Vec              U;
3423:     PetscObjectState sprev,spost;

3425:     TSGetSolution(ts,&U);
3426:     PetscObjectStateGet((PetscObject)U,&sprev);
3427:     PetscStackCallStandard((*ts->poststep),(ts));
3428:     PetscObjectStateGet((PetscObject)U,&spost);
3429:     if (sprev != spost) {TSRestartStep(ts);}
3430:   }
3431:   return(0);
3432: }

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

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

3440:    Logically Collective on TS

3442:    Input Parameters:
3443: +  ts - the TS context obtained from TSCreate()
3444: .  monitor - monitoring routine
3445: .  mctx - [optional] user-defined context for private data for the
3446:              monitor routine (use NULL if no context is desired)
3447: -  monitordestroy - [optional] routine that frees monitor context
3448:           (may be NULL)

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

3453: +    ts - the TS context
3454: .    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)
3455: .    time - current time
3456: .    u - current iterate
3457: -    mctx - [optional] monitoring context

3459:    Notes:
3460:    This routine adds an additional monitor to the list of monitors that
3461:    already has been loaded.

3463:    Fortran Notes:
3464:     Only a single monitor function can be set for each TS object

3466:    Level: intermediate

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

3470: .seealso: TSMonitorDefault(), TSMonitorCancel()
3471: @*/
3472: PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3473: {
3475:   PetscInt       i;
3476:   PetscBool      identical;

3480:   for (i=0; i<ts->numbermonitors;i++) {
3481:     PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);
3482:     if (identical) return(0);
3483:   }
3484:   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3485:   ts->monitor[ts->numbermonitors]          = monitor;
3486:   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3487:   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3488:   return(0);
3489: }

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

3494:    Logically Collective on TS

3496:    Input Parameters:
3497: .  ts - the TS context obtained from TSCreate()

3499:    Notes:
3500:    There is no way to remove a single, specific monitor.

3502:    Level: intermediate

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

3506: .seealso: TSMonitorDefault(), TSMonitorSet()
3507: @*/
3508: PetscErrorCode  TSMonitorCancel(TS ts)
3509: {
3511:   PetscInt       i;

3515:   for (i=0; i<ts->numbermonitors; i++) {
3516:     if (ts->monitordestroy[i]) {
3517:       (*ts->monitordestroy[i])(&ts->monitorcontext[i]);
3518:     }
3519:   }
3520:   ts->numbermonitors = 0;
3521:   return(0);
3522: }

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

3527:    Level: intermediate

3529: .keywords: TS, set, monitor

3531: .seealso:  TSMonitorSet()
3532: @*/
3533: PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3534: {
3536:   PetscViewer    viewer =  vf->viewer;
3537:   PetscBool      iascii,ibinary;

3541:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
3542:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);
3543:   PetscViewerPushFormat(viewer,vf->format);
3544:   if (iascii) {
3545:     PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);
3546:     if (step == -1){ /* this indicates it is an interpolated solution */
3547:       PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);
3548:     } else {
3549:       PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");
3550:     }
3551:     PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);
3552:   } else if (ibinary) {
3553:     PetscMPIInt rank;
3554:     MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
3555:     if (!rank) {
3556:       PetscBool skipHeader;
3557:       PetscInt  classid = REAL_FILE_CLASSID;

3559:       PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);
3560:       if (!skipHeader) {
3561:          PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);
3562:        }
3563:       PetscRealView(1,&ptime,viewer);
3564:     } else {
3565:       PetscRealView(0,&ptime,viewer);
3566:     }
3567:   }
3568:   PetscViewerPopFormat(viewer);
3569:   return(0);
3570: }

3572: /*@C
3573:    TSMonitorExtreme - Prints the extreme values of the solution at each timestep

3575:    Level: intermediate

3577: .keywords: TS, set, monitor

3579: .seealso:  TSMonitorSet()
3580: @*/
3581: PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3582: {
3584:   PetscViewer    viewer =  vf->viewer;
3585:   PetscBool      iascii;
3586:   PetscReal      max,min;

3588: 
3591:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
3592:   PetscViewerPushFormat(viewer,vf->format);
3593:   if (iascii) {
3594:     VecMax(v,NULL,&max);
3595:     VecMin(v,NULL,&min);
3596:     PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);
3597:     PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s max %g min %g\n",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)" : "",(double)max,(double)min);
3598:     PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);
3599:   }
3600:   PetscViewerPopFormat(viewer);
3601:   return(0);
3602: }

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

3607:    Collective on TS

3609:    Input Argument:
3610: +  ts - time stepping context
3611: -  t - time to interpolate to

3613:    Output Argument:
3614: .  U - state at given time

3616:    Level: intermediate

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

3621: .keywords: TS, set

3623: .seealso: TSSetExactFinalTime(), TSSolve()
3624: @*/
3625: PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3626: {

3632:   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);
3633:   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
3634:   (*ts->ops->interpolate)(ts,t,U);
3635:   return(0);
3636: }

3638: /*@
3639:    TSStep - Steps one time step

3641:    Collective on TS

3643:    Input Parameter:
3644: .  ts - the TS context obtained from TSCreate()

3646:    Level: developer

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

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

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

3657: .keywords: TS, timestep, solve

3659: .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3660: @*/
3661: PetscErrorCode  TSStep(TS ts)
3662: {
3663:   PetscErrorCode   ierr;
3664:   static PetscBool cite = PETSC_FALSE;
3665:   PetscReal        ptime;

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

3677:   TSSetUp(ts);
3678:   TSTrajectorySetUp(ts->trajectory,ts);

3680:   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>");
3681:   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()");
3682:   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");

3684:   if (!ts->steps) ts->ptime_prev = ts->ptime;
3685:   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
3686:   ts->reason = TS_CONVERGED_ITERATING;
3687:   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3688:   PetscLogEventBegin(TS_Step,ts,0,0,0);
3689:   (*ts->ops->step)(ts);
3690:   PetscLogEventEnd(TS_Step,ts,0,0,0);
3691:   ts->ptime_prev = ptime;
3692:   ts->steps++;
3693:   ts->steprollback = PETSC_FALSE;
3694:   ts->steprestart  = PETSC_FALSE;

3696:   if (ts->reason < 0) {
3697:     if (ts->errorifstepfailed) {
3698:       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]);
3699:       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3700:     }
3701:   } else if (!ts->reason) {
3702:     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3703:     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3704:   }
3705:   return(0);
3706: }

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

3712:    Collective on TS

3714:    Input Arguments:
3715: +  ts - time stepping context
3716: .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
3717: -  order - optional, desired order for the error evaluation or PETSC_DECIDE

3719:    Output Arguments:
3720: +  order - optional, the actual order of the error evaluation
3721: -  wlte - the weighted local truncation error norm

3723:    Level: advanced

3725:    Notes:
3726:    If the timestepper cannot evaluate the error in a particular step
3727:    (eg. in the first step or restart steps after event handling),
3728:    this routine returns wlte=-1.0 .

3730: .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
3731: @*/
3732: PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
3733: {

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

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

3752:    Collective on TS

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

3759:    Output Arguments:
3760: .  U - state at the end of the current step

3762:    Level: advanced

3764:    Notes:
3765:    This function cannot be called until all stages have been evaluated.
3766:    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.

3768: .seealso: TSStep(), TSAdapt
3769: @*/
3770: PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
3771: {

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

3783: /*@
3784:    TSSolve - Steps the requested number of timesteps.

3786:    Collective on TS

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

3793:    Level: beginner

3795:    Notes:
3796:    The final time returned by this function may be different from the time of the internally
3797:    held state accessible by TSGetSolution() and TSGetTime() because the method may have
3798:    stepped over the final time.

3800: .keywords: TS, timestep, solve

3802: .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
3803: @*/
3804: PetscErrorCode TSSolve(TS ts,Vec u)
3805: {
3806:   Vec               solution;
3807:   PetscErrorCode    ierr;


3813:   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 */
3814:     if (!ts->vec_sol || u == ts->vec_sol) {
3815:       VecDuplicate(u,&solution);
3816:       TSSetSolution(ts,solution);
3817:       VecDestroy(&solution); /* grant ownership */
3818:     }
3819:     VecCopy(u,ts->vec_sol);
3820:     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3821:   } else if (u) {
3822:     TSSetSolution(ts,u);
3823:   }
3824:   TSSetUp(ts);
3825:   TSTrajectorySetUp(ts->trajectory,ts);

3827:   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>");
3828:   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()");
3829:   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");

3831:   if (ts->forward_solve) {
3832:     TSForwardSetUp(ts);
3833:   }

3835:   /* reset number of steps only when the step is not restarted. ARKIMEX
3836:      restarts the step after an event. Resetting these counters in such case causes
3837:      TSTrajectory to incorrectly save the output files
3838:   */
3839:   /* reset time step and iteration counters */
3840:   if (!ts->steps) {
3841:     ts->ksp_its           = 0;
3842:     ts->snes_its          = 0;
3843:     ts->num_snes_failures = 0;
3844:     ts->reject            = 0;
3845:     ts->steprestart       = PETSC_TRUE;
3846:     ts->steprollback      = PETSC_FALSE;
3847:   }
3848:   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime;
3849:   ts->reason = TS_CONVERGED_ITERATING;

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

3853:   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3854:     (*ts->ops->solve)(ts);
3855:     if (u) {VecCopy(ts->vec_sol,u);}
3856:     ts->solvetime = ts->ptime;
3857:     solution = ts->vec_sol;
3858:   } else { /* Step the requested number of timesteps. */
3859:     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3860:     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;

3862:     if (!ts->steps) {
3863:       TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
3864:       TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);
3865:     }

3867:     while (!ts->reason) {
3868:       TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);
3869:       if (!ts->steprollback) {
3870:         TSPreStep(ts);
3871:       }
3872:       TSStep(ts);
3873:       if (ts->testjacobian) {
3874:         TSRHSJacobianTest(ts,NULL);
3875:       }
3876:       if (ts->testjacobiantranspose) {
3877:         TSRHSJacobianTestTranspose(ts,NULL);
3878:       }
3879:       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
3880:         TSForwardCostIntegral(ts);
3881:       }
3882:       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3883:         TSForwardStep(ts);
3884:       }
3885:       TSPostEvaluate(ts);
3886:       TSEventHandler(ts); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
3887:       if (ts->steprollback) {
3888:         TSPostEvaluate(ts);
3889:       }
3890:       if (!ts->steprollback) {
3891:         TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
3892:         TSPostStep(ts);
3893:       }
3894:     }
3895:     TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);

3897:     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
3898:       TSInterpolate(ts,ts->max_time,u);
3899:       ts->solvetime = ts->max_time;
3900:       solution = u;
3901:       TSMonitor(ts,-1,ts->solvetime,solution);
3902:     } else {
3903:       if (u) {VecCopy(ts->vec_sol,u);}
3904:       ts->solvetime = ts->ptime;
3905:       solution = ts->vec_sol;
3906:     }
3907:   }

3909:   TSViewFromOptions(ts,NULL,"-ts_view");
3910:   VecViewFromOptions(solution,NULL,"-ts_view_solution");
3911:   PetscObjectSAWsBlock((PetscObject)ts);
3912:   if (ts->adjoint_solve) {
3913:     TSAdjointSolve(ts);
3914:   }
3915:   return(0);
3916: }

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

3921:    Collective on TS

3923:    Input Parameters:
3924: +  ts - time stepping context obtained from TSCreate()
3925: .  step - step number that has just completed
3926: .  ptime - model time of the state
3927: -  u - state at the current model time

3929:    Notes:
3930:    TSMonitor() is typically used automatically within the time stepping implementations.
3931:    Users would almost never call this routine directly.

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

3935:    Level: developer

3937: .keywords: TS, timestep
3938: @*/
3939: PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3940: {
3941:   DM             dm;
3942:   PetscInt       i,n = ts->numbermonitors;


3949:   TSGetDM(ts,&dm);
3950:   DMSetOutputSequenceNumber(dm,step,ptime);

3952:   VecLockReadPush(u);
3953:   for (i=0; i<n; i++) {
3954:     (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);
3955:   }
3956:   VecLockReadPop(u);
3957:   return(0);
3958: }

3960: /* ------------------------------------------------------------------------*/
3961: /*@C
3962:    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3963:    TS to monitor the solution process graphically in various ways

3965:    Collective on TS

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

3974:    Output Parameter:
3975: .  ctx - the context

3977:    Options Database Key:
3978: +  -ts_monitor_lg_timestep - automatically sets line graph monitor
3979: +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
3980: .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
3981: .  -ts_monitor_lg_error -  monitor the error
3982: .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
3983: .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3984: -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true

3986:    Notes:
3987:    Use TSMonitorLGCtxDestroy() to destroy.

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

3991:    Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the
3992:    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
3993:    as the first argument.

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

3997:    Level: intermediate

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

4001: .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
4002:            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
4003:            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
4004:            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
4005:            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()

4007: @*/
4008: PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4009: {
4010:   PetscDraw      draw;

4014:   PetscNew(ctx);
4015:   PetscDrawCreate(comm,host,label,x,y,m,n,&draw);
4016:   PetscDrawSetFromOptions(draw);
4017:   PetscDrawLGCreate(draw,1,&(*ctx)->lg);
4018:   PetscDrawLGSetFromOptions((*ctx)->lg);
4019:   PetscDrawDestroy(&draw);
4020:   (*ctx)->howoften = howoften;
4021:   return(0);
4022: }

4024: PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4025: {
4026:   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4027:   PetscReal      x   = ptime,y;

4031:   if (step < 0) return(0); /* -1 indicates an interpolated solution */
4032:   if (!step) {
4033:     PetscDrawAxis axis;
4034:     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4035:     PetscDrawLGGetAxis(ctx->lg,&axis);
4036:     PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);
4037:     PetscDrawLGReset(ctx->lg);
4038:   }
4039:   TSGetTimeStep(ts,&y);
4040:   if (ctx->semilogy) y = PetscLog10Real(y);
4041:   PetscDrawLGAddPoint(ctx->lg,&x,&y);
4042:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
4043:     PetscDrawLGDraw(ctx->lg);
4044:     PetscDrawLGSave(ctx->lg);
4045:   }
4046:   return(0);
4047: }

4049: /*@C
4050:    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4051:    with TSMonitorLGCtxCreate().

4053:    Collective on TSMonitorLGCtx

4055:    Input Parameter:
4056: .  ctx - the monitor context

4058:    Level: intermediate

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

4062: .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4063: @*/
4064: PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4065: {

4069:   if ((*ctx)->transformdestroy) {
4070:     ((*ctx)->transformdestroy)((*ctx)->transformctx);
4071:   }
4072:   PetscDrawLGDestroy(&(*ctx)->lg);
4073:   PetscStrArrayDestroy(&(*ctx)->names);
4074:   PetscStrArrayDestroy(&(*ctx)->displaynames);
4075:   PetscFree((*ctx)->displayvariables);
4076:   PetscFree((*ctx)->displayvalues);
4077:   PetscFree(*ctx);
4078:   return(0);
4079: }

4081: /*
4082:   
4083:   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations

4085: */
4086: PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
4087: {
4088:   PetscDraw      draw;

4092:   PetscNew(ctx);
4093:   PetscDrawCreate(comm,host,label,x,y,m,n,&draw);
4094:   PetscDrawSetFromOptions(draw);
4095:   PetscDrawSPCreate(draw,1,&(*ctx)->sp);
4096:   PetscDrawDestroy(&draw);
4097:   (*ctx)->howoften = howoften;
4098:   return(0);

4100: }

4102: /* 
4103:   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate 
4104: */
4105: PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
4106: {

4110: 
4111:   PetscDrawSPDestroy(&(*ctx)->sp);
4112:   PetscFree(*ctx);
4113: 
4114:   return(0);

4116: }

4118: /*@
4119:    TSGetTime - Gets the time of the most recently completed step.

4121:    Not Collective

4123:    Input Parameter:
4124: .  ts - the TS context obtained from TSCreate()

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

4129:    Level: beginner

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

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

4137: .keywords: TS, get, time
4138: @*/
4139: PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4140: {
4144:   *t = ts->ptime;
4145:   return(0);
4146: }

4148: /*@
4149:    TSGetPrevTime - Gets the starting time of the previously completed step.

4151:    Not Collective

4153:    Input Parameter:
4154: .  ts - the TS context obtained from TSCreate()

4156:    Output Parameter:
4157: .  t  - the previous time

4159:    Level: beginner

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

4163: .keywords: TS, get, time
4164: @*/
4165: PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4166: {
4170:   *t = ts->ptime_prev;
4171:   return(0);
4172: }

4174: /*@
4175:    TSSetTime - Allows one to reset the time.

4177:    Logically Collective on TS

4179:    Input Parameters:
4180: +  ts - the TS context obtained from TSCreate()
4181: -  time - the time

4183:    Level: intermediate

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

4187: .keywords: TS, set, time
4188: @*/
4189: PetscErrorCode  TSSetTime(TS ts, PetscReal t)
4190: {
4194:   ts->ptime = t;
4195:   return(0);
4196: }

4198: /*@C
4199:    TSSetOptionsPrefix - Sets the prefix used for searching for all
4200:    TS options in the database.

4202:    Logically Collective on TS

4204:    Input Parameter:
4205: +  ts     - The TS context
4206: -  prefix - The prefix to prepend to all option names

4208:    Notes:
4209:    A hyphen (-) must NOT be given at the beginning of the prefix name.
4210:    The first character of all runtime options is AUTOMATICALLY the
4211:    hyphen.

4213:    Level: advanced

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

4217: .seealso: TSSetFromOptions()

4219: @*/
4220: PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4221: {
4223:   SNES           snes;

4227:   PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);
4228:   TSGetSNES(ts,&snes);
4229:   SNESSetOptionsPrefix(snes,prefix);
4230:   return(0);
4231: }

4233: /*@C
4234:    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4235:    TS options in the database.

4237:    Logically Collective on TS

4239:    Input Parameter:
4240: +  ts     - The TS context
4241: -  prefix - The prefix to prepend to all option names

4243:    Notes:
4244:    A hyphen (-) must NOT be given at the beginning of the prefix name.
4245:    The first character of all runtime options is AUTOMATICALLY the
4246:    hyphen.

4248:    Level: advanced

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

4252: .seealso: TSGetOptionsPrefix()

4254: @*/
4255: PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4256: {
4258:   SNES           snes;

4262:   PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);
4263:   TSGetSNES(ts,&snes);
4264:   SNESAppendOptionsPrefix(snes,prefix);
4265:   return(0);
4266: }

4268: /*@C
4269:    TSGetOptionsPrefix - Sets the prefix used for searching for all
4270:    TS options in the database.

4272:    Not Collective

4274:    Input Parameter:
4275: .  ts - The TS context

4277:    Output Parameter:
4278: .  prefix - A pointer to the prefix string used

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

4284:    Level: intermediate

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

4288: .seealso: TSAppendOptionsPrefix()
4289: @*/
4290: PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4291: {

4297:   PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);
4298:   return(0);
4299: }

4301: /*@C
4302:    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.

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

4306:    Input Parameter:
4307: .  ts  - The TS context obtained from TSCreate()

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

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

4318:    Level: intermediate

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

4322: .keywords: TS, timestep, get, matrix, Jacobian
4323: @*/
4324: PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4325: {
4327:   DM             dm;

4330:   if (Amat || Pmat) {
4331:     SNES snes;
4332:     TSGetSNES(ts,&snes);
4333:     SNESSetUpMatrices(snes);
4334:     SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);
4335:   }
4336:   TSGetDM(ts,&dm);
4337:   DMTSGetRHSJacobian(dm,func,ctx);
4338:   return(0);
4339: }

4341: /*@C
4342:    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.

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

4346:    Input Parameter:
4347: .  ts  - The TS context obtained from TSCreate()

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

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

4358:    Level: advanced

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

4362: .keywords: TS, timestep, get, matrix, Jacobian
4363: @*/
4364: PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
4365: {
4367:   DM             dm;

4370:   if (Amat || Pmat) {
4371:     SNES snes;
4372:     TSGetSNES(ts,&snes);
4373:     SNESSetUpMatrices(snes);
4374:     SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);
4375:   }
4376:   TSGetDM(ts,&dm);
4377:   DMTSGetIJacobian(dm,f,ctx);
4378:   return(0);
4379: }

4381: /*@C
4382:    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
4383:    VecView() for the solution at each timestep

4385:    Collective on TS

4387:    Input Parameters:
4388: +  ts - the TS context
4389: .  step - current time-step
4390: .  ptime - current time
4391: -  dummy - either a viewer or NULL

4393:    Options Database:
4394: .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution

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

4400:    Level: intermediate

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

4404: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4405: @*/
4406: PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4407: {
4408:   PetscErrorCode   ierr;
4409:   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4410:   PetscDraw        draw;

4413:   if (!step && ictx->showinitial) {
4414:     if (!ictx->initialsolution) {
4415:       VecDuplicate(u,&ictx->initialsolution);
4416:     }
4417:     VecCopy(u,ictx->initialsolution);
4418:   }
4419:   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) return(0);

4421:   if (ictx->showinitial) {
4422:     PetscReal pause;
4423:     PetscViewerDrawGetPause(ictx->viewer,&pause);
4424:     PetscViewerDrawSetPause(ictx->viewer,0.0);
4425:     VecView(ictx->initialsolution,ictx->viewer);
4426:     PetscViewerDrawSetPause(ictx->viewer,pause);
4427:     PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);
4428:   }
4429:   VecView(u,ictx->viewer);
4430:   if (ictx->showtimestepandtime) {
4431:     PetscReal xl,yl,xr,yr,h;
4432:     char      time[32];

4434:     PetscViewerDrawGetDraw(ictx->viewer,0,&draw);
4435:     PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);
4436:     PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
4437:     h    = yl + .95*(yr - yl);
4438:     PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);
4439:     PetscDrawFlush(draw);
4440:   }

4442:   if (ictx->showinitial) {
4443:     PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);
4444:   }
4445:   return(0);
4446: }

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

4451:    Collective on TS

4453:    Input Parameters:
4454: +  ts - the TS context
4455: .  step - current time-step
4456: .  ptime - current time
4457: -  dummy - either a viewer or NULL

4459:    Level: intermediate

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

4463: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4464: @*/
4465: PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4466: {
4467:   PetscErrorCode    ierr;
4468:   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
4469:   PetscDraw         draw;
4470:   PetscDrawAxis     axis;
4471:   PetscInt          n;
4472:   PetscMPIInt       size;
4473:   PetscReal         U0,U1,xl,yl,xr,yr,h;
4474:   char              time[32];
4475:   const PetscScalar *U;

4478:   MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);
4479:   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
4480:   VecGetSize(u,&n);
4481:   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");

4483:   PetscViewerDrawGetDraw(ictx->viewer,0,&draw);
4484:   PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);
4485:   PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);
4486:   if (!step) {
4487:     PetscDrawClear(draw);
4488:     PetscDrawAxisDraw(axis);
4489:   }

4491:   VecGetArrayRead(u,&U);
4492:   U0 = PetscRealPart(U[0]);
4493:   U1 = PetscRealPart(U[1]);
4494:   VecRestoreArrayRead(u,&U);
4495:   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) return(0);

4497:   PetscDrawCollectiveBegin(draw);
4498:   PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);
4499:   if (ictx->showtimestepandtime) {
4500:     PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
4501:     PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);
4502:     h    = yl + .95*(yr - yl);
4503:     PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);
4504:   }
4505:   PetscDrawCollectiveEnd(draw);
4506:   PetscDrawFlush(draw);
4507:   PetscDrawPause(draw);
4508:   PetscDrawSave(draw);
4509:   return(0);
4510: }

4512: /*@C
4513:    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()

4515:    Collective on TS

4517:    Input Parameters:
4518: .    ctx - the monitor context

4520:    Level: intermediate

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

4524: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
4525: @*/
4526: PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
4527: {

4531:   PetscViewerDestroy(&(*ictx)->viewer);
4532:   VecDestroy(&(*ictx)->initialsolution);
4533:   PetscFree(*ictx);
4534:   return(0);
4535: }

4537: /*@C
4538:    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx

4540:    Collective on TS

4542:    Input Parameter:
4543: .    ts - time-step context

4545:    Output Patameter:
4546: .    ctx - the monitor context

4548:    Options Database:
4549: .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution

4551:    Level: intermediate

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

4555: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
4556: @*/
4557: PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
4558: {
4559:   PetscErrorCode   ierr;

4562:   PetscNew(ctx);
4563:   PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);
4564:   PetscViewerSetFromOptions((*ctx)->viewer);

4566:   (*ctx)->howoften    = howoften;
4567:   (*ctx)->showinitial = PETSC_FALSE;
4568:   PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);

4570:   (*ctx)->showtimestepandtime = PETSC_FALSE;
4571:   PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);
4572:   return(0);
4573: }

4575: /*@C
4576:    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
4577:    VecView() for the solution provided by TSSetSolutionFunction() at each timestep

4579:    Collective on TS

4581:    Input Parameters:
4582: +  ts - the TS context
4583: .  step - current time-step
4584: .  ptime - current time
4585: -  dummy - either a viewer or NULL

4587:    Options Database:
4588: .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()

4590:    Level: intermediate

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

4594: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4595: @*/
4596: PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4597: {
4598:   PetscErrorCode   ierr;
4599:   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
4600:   PetscViewer      viewer = ctx->viewer;
4601:   Vec              work;

4604:   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) return(0);
4605:   VecDuplicate(u,&work);
4606:   TSComputeSolutionFunction(ts,ptime,work);
4607:   VecView(work,viewer);
4608:   VecDestroy(&work);
4609:   return(0);
4610: }

4612: /*@C
4613:    TSMonitorDrawError - Monitors progress of the TS solvers by calling
4614:    VecView() for the error at each timestep

4616:    Collective on TS

4618:    Input Parameters:
4619: +  ts - the TS context
4620: .  step - current time-step
4621: .  ptime - current time
4622: -  dummy - either a viewer or NULL

4624:    Options Database:
4625: .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()

4627:    Level: intermediate

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

4631: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4632: @*/
4633: PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4634: {
4635:   PetscErrorCode   ierr;
4636:   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
4637:   PetscViewer      viewer = ctx->viewer;
4638:   Vec              work;

4641:   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) return(0);
4642:   VecDuplicate(u,&work);
4643:   TSComputeSolutionFunction(ts,ptime,work);
4644:   VecAXPY(work,-1.0,u);
4645:   VecView(work,viewer);
4646:   VecDestroy(&work);
4647:   return(0);
4648: }

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

4654:    Logically Collective on TS and DM

4656:    Input Parameters:
4657: +  ts - the ODE integrator object
4658: -  dm - the dm, cannot be NULL

4660:    Notes:
4661:    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4662:    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4663:    different problems using the same function space.

4665:    Level: intermediate

4667: .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
4668: @*/
4669: PetscErrorCode  TSSetDM(TS ts,DM dm)
4670: {
4672:   SNES           snes;
4673:   DMTS           tsdm;

4678:   PetscObjectReference((PetscObject)dm);
4679:   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
4680:     if (ts->dm->dmts && !dm->dmts) {
4681:       DMCopyDMTS(ts->dm,dm);
4682:       DMGetDMTS(ts->dm,&tsdm);
4683:       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
4684:         tsdm->originaldm = dm;
4685:       }
4686:     }
4687:     DMDestroy(&ts->dm);
4688:   }
4689:   ts->dm = dm;

4691:   TSGetSNES(ts,&snes);
4692:   SNESSetDM(snes,dm);
4693:   return(0);
4694: }

4696: /*@
4697:    TSGetDM - Gets the DM that may be used by some preconditioners

4699:    Not Collective

4701:    Input Parameter:
4702: . ts - the preconditioner context

4704:    Output Parameter:
4705: .  dm - the dm

4707:    Level: intermediate

4709: .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
4710: @*/
4711: PetscErrorCode  TSGetDM(TS ts,DM *dm)
4712: {

4717:   if (!ts->dm) {
4718:     DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);
4719:     if (ts->snes) {SNESSetDM(ts->snes,ts->dm);}
4720:   }
4721:   *dm = ts->dm;
4722:   return(0);
4723: }

4725: /*@
4726:    SNESTSFormFunction - Function to evaluate nonlinear residual

4728:    Logically Collective on SNES

4730:    Input Parameter:
4731: + snes - nonlinear solver
4732: . U - the current state at which to evaluate the residual
4733: - ctx - user context, must be a TS

4735:    Output Parameter:
4736: . F - the nonlinear residual

4738:    Notes:
4739:    This function is not normally called by users and is automatically registered with the SNES used by TS.
4740:    It is most frequently passed to MatFDColoringSetFunction().

4742:    Level: advanced

4744: .seealso: SNESSetFunction(), MatFDColoringSetFunction()
4745: @*/
4746: PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
4747: {
4748:   TS             ts = (TS)ctx;

4756:   (ts->ops->snesfunction)(snes,U,F,ts);
4757:   return(0);
4758: }

4760: /*@
4761:    SNESTSFormJacobian - Function to evaluate the Jacobian

4763:    Collective on SNES

4765:    Input Parameter:
4766: + snes - nonlinear solver
4767: . U - the current state at which to evaluate the residual
4768: - ctx - user context, must be a TS

4770:    Output Parameter:
4771: + A - the Jacobian
4772: . B - the preconditioning matrix (may be the same as A)
4773: - flag - indicates any structure change in the matrix

4775:    Notes:
4776:    This function is not normally called by users and is automatically registered with the SNES used by TS.

4778:    Level: developer

4780: .seealso: SNESSetJacobian()
4781: @*/
4782: PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
4783: {
4784:   TS             ts = (TS)ctx;

4795:   (ts->ops->snesjacobian)(snes,U,A,B,ts);
4796:   return(0);
4797: }

4799: /*@C
4800:    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only

4802:    Collective on TS

4804:    Input Arguments:
4805: +  ts - time stepping context
4806: .  t - time at which to evaluate
4807: .  U - state at which to evaluate
4808: -  ctx - context

4810:    Output Arguments:
4811: .  F - right hand side

4813:    Level: intermediate

4815:    Notes:
4816:    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
4817:    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().

4819: .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
4820: @*/
4821: PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
4822: {
4824:   Mat            Arhs,Brhs;

4827:   TSGetRHSMats_Private(ts,&Arhs,&Brhs);
4828:   TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);
4829:   MatMult(Arhs,U,F);
4830:   return(0);
4831: }

4833: /*@C
4834:    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.

4836:    Collective on TS

4838:    Input Arguments:
4839: +  ts - time stepping context
4840: .  t - time at which to evaluate
4841: .  U - state at which to evaluate
4842: -  ctx - context

4844:    Output Arguments:
4845: +  A - pointer to operator
4846: .  B - pointer to preconditioning matrix
4847: -  flg - matrix structure flag

4849:    Level: intermediate

4851:    Notes:
4852:    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.

4854: .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
4855: @*/
4856: PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
4857: {
4859:   return(0);
4860: }

4862: /*@C
4863:    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only

4865:    Collective on TS

4867:    Input Arguments:
4868: +  ts - time stepping context
4869: .  t - time at which to evaluate
4870: .  U - state at which to evaluate
4871: .  Udot - time derivative of state vector
4872: -  ctx - context

4874:    Output Arguments:
4875: .  F - left hand side

4877:    Level: intermediate

4879:    Notes:
4880:    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
4881:    user is required to write their own TSComputeIFunction.
4882:    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
4883:    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().

4885:    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U

4887: .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
4888: @*/
4889: PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
4890: {
4892:   Mat            A,B;

4895:   TSGetIJacobian(ts,&A,&B,NULL,NULL);
4896:   TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);
4897:   MatMult(A,Udot,F);
4898:   return(0);
4899: }

4901: /*@C
4902:    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE

4904:    Collective on TS

4906:    Input Arguments:
4907: +  ts - time stepping context
4908: .  t - time at which to evaluate
4909: .  U - state at which to evaluate
4910: .  Udot - time derivative of state vector
4911: .  shift - shift to apply
4912: -  ctx - context

4914:    Output Arguments:
4915: +  A - pointer to operator
4916: .  B - pointer to preconditioning matrix
4917: -  flg - matrix structure flag

4919:    Level: advanced

4921:    Notes:
4922:    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.

4924:    It is only appropriate for problems of the form

4926: $     M Udot = F(U,t)

4928:   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4929:   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4930:   an implicit operator of the form

4932: $    shift*M + J

4934:   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
4935:   a copy of M or reassemble it when requested.

4937: .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
4938: @*/
4939: PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
4940: {

4944:   MatScale(A, shift / ts->ijacobian.shift);
4945:   ts->ijacobian.shift = shift;
4946:   return(0);
4947: }

4949: /*@
4950:    TSGetEquationType - Gets the type of the equation that TS is solving.

4952:    Not Collective

4954:    Input Parameter:
4955: .  ts - the TS context

4957:    Output Parameter:
4958: .  equation_type - see TSEquationType

4960:    Level: beginner

4962: .keywords: TS, equation type

4964: .seealso: TSSetEquationType(), TSEquationType
4965: @*/
4966: PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4967: {
4971:   *equation_type = ts->equation_type;
4972:   return(0);
4973: }

4975: /*@
4976:    TSSetEquationType - Sets the type of the equation that TS is solving.

4978:    Not Collective

4980:    Input Parameter:
4981: +  ts - the TS context
4982: -  equation_type - see TSEquationType

4984:    Level: advanced

4986: .keywords: TS, equation type

4988: .seealso: TSGetEquationType(), TSEquationType
4989: @*/
4990: PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4991: {
4994:   ts->equation_type = equation_type;
4995:   return(0);
4996: }

4998: /*@
4999:    TSGetConvergedReason - Gets the reason the TS iteration was stopped.

5001:    Not Collective

5003:    Input Parameter:
5004: .  ts - the TS context

5006:    Output Parameter:
5007: .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5008:             manual pages for the individual convergence tests for complete lists

5010:    Level: beginner

5012:    Notes:
5013:    Can only be called after the call to TSSolve() is complete.

5015: .keywords: TS, nonlinear, set, convergence, test

5017: .seealso: TSSetConvergenceTest(), TSConvergedReason
5018: @*/
5019: PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
5020: {
5024:   *reason = ts->reason;
5025:   return(0);
5026: }

5028: /*@
5029:    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.

5031:    Not Collective

5033:    Input Parameter:
5034: +  ts - the TS context
5035: .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5036:             manual pages for the individual convergence tests for complete lists

5038:    Level: advanced

5040:    Notes:
5041:    Can only be called during TSSolve() is active.

5043: .keywords: TS, nonlinear, set, convergence, test

5045: .seealso: TSConvergedReason
5046: @*/
5047: PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5048: {
5051:   ts->reason = reason;
5052:   return(0);
5053: }

5055: /*@
5056:    TSGetSolveTime - Gets the time after a call to TSSolve()

5058:    Not Collective

5060:    Input Parameter:
5061: .  ts - the TS context

5063:    Output Parameter:
5064: .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()

5066:    Level: beginner

5068:    Notes:
5069:    Can only be called after the call to TSSolve() is complete.

5071: .keywords: TS, nonlinear, set, convergence, test

5073: .seealso: TSSetConvergenceTest(), TSConvergedReason
5074: @*/
5075: PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5076: {
5080:   *ftime = ts->solvetime;
5081:   return(0);
5082: }

5084: /*@
5085:    TSGetSNESIterations - Gets the total number of nonlinear iterations
5086:    used by the time integrator.

5088:    Not Collective

5090:    Input Parameter:
5091: .  ts - TS context

5093:    Output Parameter:
5094: .  nits - number of nonlinear iterations

5096:    Notes:
5097:    This counter is reset to zero for each successive call to TSSolve().

5099:    Level: intermediate

5101: .keywords: TS, get, number, nonlinear, iterations

5103: .seealso:  TSGetKSPIterations()
5104: @*/
5105: PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
5106: {
5110:   *nits = ts->snes_its;
5111:   return(0);
5112: }

5114: /*@
5115:    TSGetKSPIterations - Gets the total number of linear iterations
5116:    used by the time integrator.

5118:    Not Collective

5120:    Input Parameter:
5121: .  ts - TS context

5123:    Output Parameter:
5124: .  lits - number of linear iterations

5126:    Notes:
5127:    This counter is reset to zero for each successive call to TSSolve().

5129:    Level: intermediate

5131: .keywords: TS, get, number, linear, iterations

5133: .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
5134: @*/
5135: PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
5136: {
5140:   *lits = ts->ksp_its;
5141:   return(0);
5142: }

5144: /*@
5145:    TSGetStepRejections - Gets the total number of rejected steps.

5147:    Not Collective

5149:    Input Parameter:
5150: .  ts - TS context

5152:    Output Parameter:
5153: .  rejects - number of steps rejected

5155:    Notes:
5156:    This counter is reset to zero for each successive call to TSSolve().

5158:    Level: intermediate

5160: .keywords: TS, get, number

5162: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5163: @*/
5164: PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5165: {
5169:   *rejects = ts->reject;
5170:   return(0);
5171: }

5173: /*@
5174:    TSGetSNESFailures - Gets the total number of failed SNES solves

5176:    Not Collective

5178:    Input Parameter:
5179: .  ts - TS context

5181:    Output Parameter:
5182: .  fails - number of failed nonlinear solves

5184:    Notes:
5185:    This counter is reset to zero for each successive call to TSSolve().

5187:    Level: intermediate

5189: .keywords: TS, get, number

5191: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5192: @*/
5193: PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5194: {
5198:   *fails = ts->num_snes_failures;
5199:   return(0);
5200: }

5202: /*@
5203:    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails

5205:    Not Collective

5207:    Input Parameter:
5208: +  ts - TS context
5209: -  rejects - maximum number of rejected steps, pass -1 for unlimited

5211:    Notes:
5212:    The counter is reset to zero for each step

5214:    Options Database Key:
5215:  .  -ts_max_reject - Maximum number of step rejections before a step fails

5217:    Level: intermediate

5219: .keywords: TS, set, maximum, number

5221: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5222: @*/
5223: PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5224: {
5227:   ts->max_reject = rejects;
5228:   return(0);
5229: }

5231: /*@
5232:    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves

5234:    Not Collective

5236:    Input Parameter:
5237: +  ts - TS context
5238: -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited

5240:    Notes:
5241:    The counter is reset to zero for each successive call to TSSolve().

5243:    Options Database Key:
5244:  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures

5246:    Level: intermediate

5248: .keywords: TS, set, maximum, number

5250: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5251: @*/
5252: PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5253: {
5256:   ts->max_snes_failures = fails;
5257:   return(0);
5258: }

5260: /*@
5261:    TSSetErrorIfStepFails - Error if no step succeeds

5263:    Not Collective

5265:    Input Parameter:
5266: +  ts - TS context
5267: -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure

5269:    Options Database Key:
5270:  .  -ts_error_if_step_fails - Error if no step succeeds

5272:    Level: intermediate

5274: .keywords: TS, set, error

5276: .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5277: @*/
5278: PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5279: {
5282:   ts->errorifstepfailed = err;
5283:   return(0);
5284: }

5286: /*@C
5287:    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

5289:    Collective on TS

5291:    Input Parameters:
5292: +  ts - the TS context
5293: .  step - current time-step
5294: .  ptime - current time
5295: .  u - current state
5296: -  vf - viewer and its format

5298:    Level: intermediate

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

5302: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5303: @*/
5304: PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5305: {

5309:   PetscViewerPushFormat(vf->viewer,vf->format);
5310:   VecView(u,vf->viewer);
5311:   PetscViewerPopFormat(vf->viewer);
5312:   return(0);
5313: }

5315: /*@C
5316:    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.

5318:    Collective on TS

5320:    Input Parameters:
5321: +  ts - the TS context
5322: .  step - current time-step
5323: .  ptime - current time
5324: .  u - current state
5325: -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)

5327:    Level: intermediate

5329:    Notes:
5330:    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.
5331:    These are named according to the file name template.

5333:    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().

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

5337: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5338: @*/
5339: PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5340: {
5342:   char           filename[PETSC_MAX_PATH_LEN];
5343:   PetscViewer    viewer;

5346:   if (step < 0) return(0); /* -1 indicates interpolated solution */
5347:   PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);
5348:   PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);
5349:   VecView(u,viewer);
5350:   PetscViewerDestroy(&viewer);
5351:   return(0);
5352: }

5354: /*@C
5355:    TSMonitorSolutionVTKDestroy - Destroy context for monitoring

5357:    Collective on TS

5359:    Input Parameters:
5360: .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)

5362:    Level: intermediate

5364:    Note:
5365:    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().

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

5369: .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5370: @*/
5371: PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5372: {

5376:   PetscFree(*(char**)filenametemplate);
5377:   return(0);
5378: }

5380: /*@
5381:    TSGetAdapt - Get the adaptive controller context for the current method

5383:    Collective on TS if controller has not been created yet

5385:    Input Arguments:
5386: .  ts - time stepping context

5388:    Output Arguments:
5389: .  adapt - adaptive controller

5391:    Level: intermediate

5393: .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
5394: @*/
5395: PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
5396: {

5402:   if (!ts->adapt) {
5403:     TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);
5404:     PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);
5405:     PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);
5406:   }
5407:   *adapt = ts->adapt;
5408:   return(0);
5409: }

5411: /*@
5412:    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller

5414:    Logically Collective

5416:    Input Arguments:
5417: +  ts - time integration context
5418: .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
5419: .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
5420: .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
5421: -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present

5423:    Options Database keys:
5424: +  -ts_rtol <rtol> - relative tolerance for local truncation error
5425: -  -ts_atol <atol> Absolute tolerance for local truncation error

5427:    Notes:
5428:    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
5429:    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
5430:    computed only for the differential or the algebraic part then this can be done using the vector of
5431:    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
5432:    differential part and infinity for the algebraic part, the LTE calculation will include only the
5433:    differential variables.

5435:    Level: beginner

5437: .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
5438: @*/
5439: PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
5440: {

5444:   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
5445:   if (vatol) {
5446:     PetscObjectReference((PetscObject)vatol);
5447:     VecDestroy(&ts->vatol);
5448:     ts->vatol = vatol;
5449:   }
5450:   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
5451:   if (vrtol) {
5452:     PetscObjectReference((PetscObject)vrtol);
5453:     VecDestroy(&ts->vrtol);
5454:     ts->vrtol = vrtol;
5455:   }
5456:   return(0);
5457: }

5459: /*@
5460:    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller

5462:    Logically Collective

5464:    Input Arguments:
5465: .  ts - time integration context

5467:    Output Arguments:
5468: +  atol - scalar absolute tolerances, NULL to ignore
5469: .  vatol - vector of absolute tolerances, NULL to ignore
5470: .  rtol - scalar relative tolerances, NULL to ignore
5471: -  vrtol - vector of relative tolerances, NULL to ignore

5473:    Level: beginner

5475: .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5476: @*/
5477: PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5478: {
5480:   if (atol)  *atol  = ts->atol;
5481:   if (vatol) *vatol = ts->vatol;
5482:   if (rtol)  *rtol  = ts->rtol;
5483:   if (vrtol) *vrtol = ts->vrtol;
5484:   return(0);
5485: }

5487: /*@
5488:    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors

5490:    Collective on TS

5492:    Input Arguments:
5493: +  ts - time stepping context
5494: .  U - state vector, usually ts->vec_sol
5495: -  Y - state vector to be compared to U

5497:    Output Arguments:
5498: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
5499: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5500: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

5502:    Level: developer

5504: .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
5505: @*/
5506: PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
5507: {
5508:   PetscErrorCode    ierr;
5509:   PetscInt          i,n,N,rstart;
5510:   PetscInt          n_loc,na_loc,nr_loc;
5511:   PetscReal         n_glb,na_glb,nr_glb;
5512:   const PetscScalar *u,*y;
5513:   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
5514:   PetscReal         tol,tola,tolr;
5515:   PetscReal         err_loc[6],err_glb[6];

5527:   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");

5529:   VecGetSize(U,&N);
5530:   VecGetLocalSize(U,&n);
5531:   VecGetOwnershipRange(U,&rstart,NULL);
5532:   VecGetArrayRead(U,&u);
5533:   VecGetArrayRead(Y,&y);
5534:   sum  = 0.; n_loc  = 0;
5535:   suma = 0.; na_loc = 0;
5536:   sumr = 0.; nr_loc = 0;
5537:   if (ts->vatol && ts->vrtol) {
5538:     const PetscScalar *atol,*rtol;
5539:     VecGetArrayRead(ts->vatol,&atol);
5540:     VecGetArrayRead(ts->vrtol,&rtol);
5541:     for (i=0; i<n; i++) {
5542:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5543:       diff = PetscAbsScalar(y[i] - u[i]);
5544:       tola = PetscRealPart(atol[i]);
5545:       if(tola>0.){
5546:         suma  += PetscSqr(diff/tola);
5547:         na_loc++;
5548:       }
5549:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5550:       if(tolr>0.){
5551:         sumr  += PetscSqr(diff/tolr);
5552:         nr_loc++;
5553:       }
5554:       tol=tola+tolr;
5555:       if(tol>0.){
5556:         sum  += PetscSqr(diff/tol);
5557:         n_loc++;
5558:       }
5559:     }
5560:     VecRestoreArrayRead(ts->vatol,&atol);
5561:     VecRestoreArrayRead(ts->vrtol,&rtol);
5562:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
5563:     const PetscScalar *atol;
5564:     VecGetArrayRead(ts->vatol,&atol);
5565:     for (i=0; i<n; i++) {
5566:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5567:       diff = PetscAbsScalar(y[i] - u[i]);
5568:       tola = PetscRealPart(atol[i]);
5569:       if(tola>0.){
5570:         suma  += PetscSqr(diff/tola);
5571:         na_loc++;
5572:       }
5573:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5574:       if(tolr>0.){
5575:         sumr  += PetscSqr(diff/tolr);
5576:         nr_loc++;
5577:       }
5578:       tol=tola+tolr;
5579:       if(tol>0.){
5580:         sum  += PetscSqr(diff/tol);
5581:         n_loc++;
5582:       }
5583:     }
5584:     VecRestoreArrayRead(ts->vatol,&atol);
5585:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
5586:     const PetscScalar *rtol;
5587:     VecGetArrayRead(ts->vrtol,&rtol);
5588:     for (i=0; i<n; i++) {
5589:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5590:       diff = PetscAbsScalar(y[i] - u[i]);
5591:       tola = ts->atol;
5592:       if(tola>0.){
5593:         suma  += PetscSqr(diff/tola);
5594:         na_loc++;
5595:       }
5596:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5597:       if(tolr>0.){
5598:         sumr  += PetscSqr(diff/tolr);
5599:         nr_loc++;
5600:       }
5601:       tol=tola+tolr;
5602:       if(tol>0.){
5603:         sum  += PetscSqr(diff/tol);
5604:         n_loc++;
5605:       }
5606:     }
5607:     VecRestoreArrayRead(ts->vrtol,&rtol);
5608:   } else {                      /* scalar atol, scalar rtol */
5609:     for (i=0; i<n; i++) {
5610:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5611:       diff = PetscAbsScalar(y[i] - u[i]);
5612:       tola = ts->atol;
5613:       if(tola>0.){
5614:         suma  += PetscSqr(diff/tola);
5615:         na_loc++;
5616:       }
5617:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5618:       if(tolr>0.){
5619:         sumr  += PetscSqr(diff/tolr);
5620:         nr_loc++;
5621:       }
5622:       tol=tola+tolr;
5623:       if(tol>0.){
5624:         sum  += PetscSqr(diff/tol);
5625:         n_loc++;
5626:       }
5627:     }
5628:   }
5629:   VecRestoreArrayRead(U,&u);
5630:   VecRestoreArrayRead(Y,&y);

5632:   err_loc[0] = sum;
5633:   err_loc[1] = suma;
5634:   err_loc[2] = sumr;
5635:   err_loc[3] = (PetscReal)n_loc;
5636:   err_loc[4] = (PetscReal)na_loc;
5637:   err_loc[5] = (PetscReal)nr_loc;

5639:   MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));

5641:   gsum   = err_glb[0];
5642:   gsuma  = err_glb[1];
5643:   gsumr  = err_glb[2];
5644:   n_glb  = err_glb[3];
5645:   na_glb = err_glb[4];
5646:   nr_glb = err_glb[5];

5648:   *norm  = 0.;
5649:   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5650:   *norma = 0.;
5651:   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5652:   *normr = 0.;
5653:   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}

5655:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
5656:   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
5657:   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
5658:   return(0);
5659: }

5661: /*@
5662:    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors

5664:    Collective on TS

5666:    Input Arguments:
5667: +  ts - time stepping context
5668: .  U - state vector, usually ts->vec_sol
5669: -  Y - state vector to be compared to U

5671:    Output Arguments:
5672: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
5673: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5674: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

5676:    Level: developer

5678: .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
5679: @*/
5680: PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
5681: {
5682:   PetscErrorCode    ierr;
5683:   PetscInt          i,n,N,rstart;
5684:   const PetscScalar *u,*y;
5685:   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
5686:   PetscReal         tol,tola,tolr,diff;
5687:   PetscReal         err_loc[3],err_glb[3];

5699:   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");

5701:   VecGetSize(U,&N);
5702:   VecGetLocalSize(U,&n);
5703:   VecGetOwnershipRange(U,&rstart,NULL);
5704:   VecGetArrayRead(U,&u);
5705:   VecGetArrayRead(Y,&y);

5707:   max=0.;
5708:   maxa=0.;
5709:   maxr=0.;

5711:   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
5712:     const PetscScalar *atol,*rtol;
5713:     VecGetArrayRead(ts->vatol,&atol);
5714:     VecGetArrayRead(ts->vrtol,&rtol);

5716:     for (i=0; i<n; i++) {
5717:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5718:       diff = PetscAbsScalar(y[i] - u[i]);
5719:       tola = PetscRealPart(atol[i]);
5720:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5721:       tol  = tola+tolr;
5722:       if(tola>0.){
5723:         maxa = PetscMax(maxa,diff / tola);
5724:       }
5725:       if(tolr>0.){
5726:         maxr = PetscMax(maxr,diff / tolr);
5727:       }
5728:       if(tol>0.){
5729:         max = PetscMax(max,diff / tol);
5730:       }
5731:     }
5732:     VecRestoreArrayRead(ts->vatol,&atol);
5733:     VecRestoreArrayRead(ts->vrtol,&rtol);
5734:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
5735:     const PetscScalar *atol;
5736:     VecGetArrayRead(ts->vatol,&atol);
5737:     for (i=0; i<n; i++) {
5738:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5739:       diff = PetscAbsScalar(y[i] - u[i]);
5740:       tola = PetscRealPart(atol[i]);
5741:       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5742:       tol  = tola+tolr;
5743:       if(tola>0.){
5744:         maxa = PetscMax(maxa,diff / tola);
5745:       }
5746:       if(tolr>0.){
5747:         maxr = PetscMax(maxr,diff / tolr);
5748:       }
5749:       if(tol>0.){
5750:         max = PetscMax(max,diff / tol);
5751:       }
5752:     }
5753:     VecRestoreArrayRead(ts->vatol,&atol);
5754:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
5755:     const PetscScalar *rtol;
5756:     VecGetArrayRead(ts->vrtol,&rtol);

5758:     for (i=0; i<n; i++) {
5759:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5760:       diff = PetscAbsScalar(y[i] - u[i]);
5761:       tola = ts->atol;
5762:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5763:       tol  = tola+tolr;
5764:       if(tola>0.){
5765:         maxa = PetscMax(maxa,diff / tola);
5766:       }
5767:       if(tolr>0.){
5768:         maxr = PetscMax(maxr,diff / tolr);
5769:       }
5770:       if(tol>0.){
5771:         max = PetscMax(max,diff / tol);
5772:       }
5773:     }
5774:     VecRestoreArrayRead(ts->vrtol,&rtol);
5775:   } else {                      /* scalar atol, scalar rtol */

5777:     for (i=0; i<n; i++) {
5778:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5779:       diff = PetscAbsScalar(y[i] - u[i]);
5780:       tola = ts->atol;
5781:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5782:       tol  = tola+tolr;
5783:       if(tola>0.){
5784:         maxa = PetscMax(maxa,diff / tola);
5785:       }
5786:       if(tolr>0.){
5787:         maxr = PetscMax(maxr,diff / tolr);
5788:       }
5789:       if(tol>0.){
5790:         max = PetscMax(max,diff / tol);
5791:       }
5792:     }
5793:   }
5794:   VecRestoreArrayRead(U,&u);
5795:   VecRestoreArrayRead(Y,&y);
5796:   err_loc[0] = max;
5797:   err_loc[1] = maxa;
5798:   err_loc[2] = maxr;
5799:   MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));
5800:   gmax   = err_glb[0];
5801:   gmaxa  = err_glb[1];
5802:   gmaxr  = err_glb[2];

5804:   *norm = gmax;
5805:   *norma = gmaxa;
5806:   *normr = gmaxr;
5807:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
5808:     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
5809:     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
5810:   return(0);
5811: }

5813: /*@
5814:    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances

5816:    Collective on TS

5818:    Input Arguments:
5819: +  ts - time stepping context
5820: .  U - state vector, usually ts->vec_sol
5821: .  Y - state vector to be compared to U
5822: -  wnormtype - norm type, either NORM_2 or NORM_INFINITY

5824:    Output Arguments:
5825: .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
5826: .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5827: .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user

5829:    Options Database Keys:
5830: .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY

5832:    Level: developer

5834: .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
5835: @*/
5836: PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
5837: {

5841:   if (wnormtype == NORM_2) {
5842:     TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);
5843:   } else if(wnormtype == NORM_INFINITY) {
5844:     TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);
5845:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
5846:   return(0);
5847: }


5850: /*@
5851:    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances

5853:    Collective on TS

5855:    Input Arguments:
5856: +  ts - time stepping context
5857: .  E - error vector
5858: .  U - state vector, usually ts->vec_sol
5859: -  Y - state vector, previous time step

5861:    Output Arguments:
5862: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
5863: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5864: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

5866:    Level: developer

5868: .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
5869: @*/
5870: PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
5871: {
5872:   PetscErrorCode    ierr;
5873:   PetscInt          i,n,N,rstart;
5874:   PetscInt          n_loc,na_loc,nr_loc;
5875:   PetscReal         n_glb,na_glb,nr_glb;
5876:   const PetscScalar *e,*u,*y;
5877:   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
5878:   PetscReal         tol,tola,tolr;
5879:   PetscReal         err_loc[6],err_glb[6];


5895:   VecGetSize(E,&N);
5896:   VecGetLocalSize(E,&n);
5897:   VecGetOwnershipRange(E,&rstart,NULL);
5898:   VecGetArrayRead(E,&e);
5899:   VecGetArrayRead(U,&u);
5900:   VecGetArrayRead(Y,&y);
5901:   sum  = 0.; n_loc  = 0;
5902:   suma = 0.; na_loc = 0;
5903:   sumr = 0.; nr_loc = 0;
5904:   if (ts->vatol && ts->vrtol) {
5905:     const PetscScalar *atol,*rtol;
5906:     VecGetArrayRead(ts->vatol,&atol);
5907:     VecGetArrayRead(ts->vrtol,&rtol);
5908:     for (i=0; i<n; i++) {
5909:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5910:       err = PetscAbsScalar(e[i]);
5911:       tola = PetscRealPart(atol[i]);
5912:       if(tola>0.){
5913:         suma  += PetscSqr(err/tola);
5914:         na_loc++;
5915:       }
5916:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5917:       if(tolr>0.){
5918:         sumr  += PetscSqr(err/tolr);
5919:         nr_loc++;
5920:       }
5921:       tol=tola+tolr;
5922:       if(tol>0.){
5923:         sum  += PetscSqr(err/tol);
5924:         n_loc++;
5925:       }
5926:     }
5927:     VecRestoreArrayRead(ts->vatol,&atol);
5928:     VecRestoreArrayRead(ts->vrtol,&rtol);
5929:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
5930:     const PetscScalar *atol;
5931:     VecGetArrayRead(ts->vatol,&atol);
5932:     for (i=0; i<n; i++) {
5933:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5934:       err = PetscAbsScalar(e[i]);
5935:       tola = PetscRealPart(atol[i]);
5936:       if(tola>0.){
5937:         suma  += PetscSqr(err/tola);
5938:         na_loc++;
5939:       }
5940:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5941:       if(tolr>0.){
5942:         sumr  += PetscSqr(err/tolr);
5943:         nr_loc++;
5944:       }
5945:       tol=tola+tolr;
5946:       if(tol>0.){
5947:         sum  += PetscSqr(err/tol);
5948:         n_loc++;
5949:       }
5950:     }
5951:     VecRestoreArrayRead(ts->vatol,&atol);
5952:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
5953:     const PetscScalar *rtol;
5954:     VecGetArrayRead(ts->vrtol,&rtol);
5955:     for (i=0; i<n; i++) {
5956:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5957:       err = PetscAbsScalar(e[i]);
5958:       tola = ts->atol;
5959:       if(tola>0.){
5960:         suma  += PetscSqr(err/tola);
5961:         na_loc++;
5962:       }
5963:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5964:       if(tolr>0.){
5965:         sumr  += PetscSqr(err/tolr);
5966:         nr_loc++;
5967:       }
5968:       tol=tola+tolr;
5969:       if(tol>0.){
5970:         sum  += PetscSqr(err/tol);
5971:         n_loc++;
5972:       }
5973:     }
5974:     VecRestoreArrayRead(ts->vrtol,&rtol);
5975:   } else {                      /* scalar atol, scalar rtol */
5976:     for (i=0; i<n; i++) {
5977:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
5978:       err = PetscAbsScalar(e[i]);
5979:       tola = ts->atol;
5980:       if(tola>0.){
5981:         suma  += PetscSqr(err/tola);
5982:         na_loc++;
5983:       }
5984:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
5985:       if(tolr>0.){
5986:         sumr  += PetscSqr(err/tolr);
5987:         nr_loc++;
5988:       }
5989:       tol=tola+tolr;
5990:       if(tol>0.){
5991:         sum  += PetscSqr(err/tol);
5992:         n_loc++;
5993:       }
5994:     }
5995:   }
5996:   VecRestoreArrayRead(E,&e);
5997:   VecRestoreArrayRead(U,&u);
5998:   VecRestoreArrayRead(Y,&y);

6000:   err_loc[0] = sum;
6001:   err_loc[1] = suma;
6002:   err_loc[2] = sumr;
6003:   err_loc[3] = (PetscReal)n_loc;
6004:   err_loc[4] = (PetscReal)na_loc;
6005:   err_loc[5] = (PetscReal)nr_loc;

6007:   MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));

6009:   gsum   = err_glb[0];
6010:   gsuma  = err_glb[1];
6011:   gsumr  = err_glb[2];
6012:   n_glb  = err_glb[3];
6013:   na_glb = err_glb[4];
6014:   nr_glb = err_glb[5];

6016:   *norm  = 0.;
6017:   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
6018:   *norma = 0.;
6019:   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
6020:   *normr = 0.;
6021:   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}

6023:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
6024:   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
6025:   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
6026:   return(0);
6027: }

6029: /*@
6030:    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
6031:    Collective on TS

6033:    Input Arguments:
6034: +  ts - time stepping context
6035: .  E - error vector
6036: .  U - state vector, usually ts->vec_sol
6037: -  Y - state vector, previous time step

6039:    Output Arguments:
6040: .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
6041: .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6042: .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances

6044:    Level: developer

6046: .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
6047: @*/
6048: PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6049: {
6050:   PetscErrorCode    ierr;
6051:   PetscInt          i,n,N,rstart;
6052:   const PetscScalar *e,*u,*y;
6053:   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
6054:   PetscReal         tol,tola,tolr;
6055:   PetscReal         err_loc[3],err_glb[3];


6071:   VecGetSize(E,&N);
6072:   VecGetLocalSize(E,&n);
6073:   VecGetOwnershipRange(E,&rstart,NULL);
6074:   VecGetArrayRead(E,&e);
6075:   VecGetArrayRead(U,&u);
6076:   VecGetArrayRead(Y,&y);

6078:   max=0.;
6079:   maxa=0.;
6080:   maxr=0.;

6082:   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
6083:     const PetscScalar *atol,*rtol;
6084:     VecGetArrayRead(ts->vatol,&atol);
6085:     VecGetArrayRead(ts->vrtol,&rtol);

6087:     for (i=0; i<n; i++) {
6088:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
6089:       err = PetscAbsScalar(e[i]);
6090:       tola = PetscRealPart(atol[i]);
6091:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6092:       tol  = tola+tolr;
6093:       if(tola>0.){
6094:         maxa = PetscMax(maxa,err / tola);
6095:       }
6096:       if(tolr>0.){
6097:         maxr = PetscMax(maxr,err / tolr);
6098:       }
6099:       if(tol>0.){
6100:         max = PetscMax(max,err / tol);
6101:       }
6102:     }
6103:     VecRestoreArrayRead(ts->vatol,&atol);
6104:     VecRestoreArrayRead(ts->vrtol,&rtol);
6105:   } else if (ts->vatol) {       /* vector atol, scalar rtol */
6106:     const PetscScalar *atol;
6107:     VecGetArrayRead(ts->vatol,&atol);
6108:     for (i=0; i<n; i++) {
6109:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
6110:       err = PetscAbsScalar(e[i]);
6111:       tola = PetscRealPart(atol[i]);
6112:       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6113:       tol  = tola+tolr;
6114:       if(tola>0.){
6115:         maxa = PetscMax(maxa,err / tola);
6116:       }
6117:       if(tolr>0.){
6118:         maxr = PetscMax(maxr,err / tolr);
6119:       }
6120:       if(tol>0.){
6121:         max = PetscMax(max,err / tol);
6122:       }
6123:     }
6124:     VecRestoreArrayRead(ts->vatol,&atol);
6125:   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
6126:     const PetscScalar *rtol;
6127:     VecGetArrayRead(ts->vrtol,&rtol);

6129:     for (i=0; i<n; i++) {
6130:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
6131:       err = PetscAbsScalar(e[i]);
6132:       tola = ts->atol;
6133:       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6134:       tol  = tola+tolr;
6135:       if(tola>0.){
6136:         maxa = PetscMax(maxa,err / tola);
6137:       }
6138:       if(tolr>0.){
6139:         maxr = PetscMax(maxr,err / tolr);
6140:       }
6141:       if(tol>0.){
6142:         max = PetscMax(max,err / tol);
6143:       }
6144:     }
6145:     VecRestoreArrayRead(ts->vrtol,&rtol);
6146:   } else {                      /* scalar atol, scalar rtol */

6148:     for (i=0; i<n; i++) {
6149:       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
6150:       err = PetscAbsScalar(e[i]);
6151:       tola = ts->atol;
6152:       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
6153:       tol  = tola+tolr;
6154:       if(tola>0.){
6155:         maxa = PetscMax(maxa,err / tola);
6156:       }
6157:       if(tolr>0.){
6158:         maxr = PetscMax(maxr,err / tolr);
6159:       }
6160:       if(tol>0.){
6161:         max = PetscMax(max,err / tol);
6162:       }
6163:     }
6164:   }
6165:   VecRestoreArrayRead(E,&e);
6166:   VecRestoreArrayRead(U,&u);
6167:   VecRestoreArrayRead(Y,&y);
6168:   err_loc[0] = max;
6169:   err_loc[1] = maxa;
6170:   err_loc[2] = maxr;
6171:   MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));
6172:   gmax   = err_glb[0];
6173:   gmaxa  = err_glb[1];
6174:   gmaxr  = err_glb[2];

6176:   *norm = gmax;
6177:   *norma = gmaxa;
6178:   *normr = gmaxr;
6179:   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
6180:     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
6181:     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
6182:   return(0);
6183: }

6185: /*@
6186:    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances

6188:    Collective on TS

6190:    Input Arguments:
6191: +  ts - time stepping context
6192: .  E - error vector
6193: .  U - state vector, usually ts->vec_sol
6194: .  Y - state vector, previous time step
6195: -  wnormtype - norm type, either NORM_2 or NORM_INFINITY

6197:    Output Arguments:
6198: .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
6199: .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6200: .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user

6202:    Options Database Keys:
6203: .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY

6205:    Level: developer

6207: .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
6208: @*/
6209: PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
6210: {

6214:   if (wnormtype == NORM_2) {
6215:     TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);
6216:   } else if(wnormtype == NORM_INFINITY) {
6217:     TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);
6218:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
6219:   return(0);
6220: }


6223: /*@
6224:    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler

6226:    Logically Collective on TS

6228:    Input Arguments:
6229: +  ts - time stepping context
6230: -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)

6232:    Note:
6233:    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()

6235:    Level: intermediate

6237: .seealso: TSGetCFLTime(), TSADAPTCFL
6238: @*/
6239: PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
6240: {
6243:   ts->cfltime_local = cfltime;
6244:   ts->cfltime       = -1.;
6245:   return(0);
6246: }

6248: /*@
6249:    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler

6251:    Collective on TS

6253:    Input Arguments:
6254: .  ts - time stepping context

6256:    Output Arguments:
6257: .  cfltime - maximum stable time step for forward Euler

6259:    Level: advanced

6261: .seealso: TSSetCFLTimeLocal()
6262: @*/
6263: PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
6264: {

6268:   if (ts->cfltime < 0) {
6269:     MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));
6270:   }
6271:   *cfltime = ts->cfltime;
6272:   return(0);
6273: }

6275: /*@
6276:    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu

6278:    Input Parameters:
6279: .  ts   - the TS context.
6280: .  xl   - lower bound.
6281: .  xu   - upper bound.

6283:    Notes:
6284:    If this routine is not called then the lower and upper bounds are set to
6285:    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().

6287:    Level: advanced

6289: @*/
6290: PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6291: {
6293:   SNES           snes;

6296:   TSGetSNES(ts,&snes);
6297:   SNESVISetVariableBounds(snes,xl,xu);
6298:   return(0);
6299: }

6301: #if defined(PETSC_HAVE_MATLAB_ENGINE)
6302: #include <mex.h>

6304: typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;

6306: /*
6307:    TSComputeFunction_Matlab - Calls the function that has been set with
6308:                          TSSetFunctionMatlab().

6310:    Collective on TS

6312:    Input Parameters:
6313: +  snes - the TS context
6314: -  u - input vector

6316:    Output Parameter:
6317: .  y - function vector, as set by TSSetFunction()

6319:    Notes:
6320:    TSComputeFunction() is typically used within nonlinear solvers
6321:    implementations, so most users would not generally call this routine
6322:    themselves.

6324:    Level: developer

6326: .keywords: TS, nonlinear, compute, function

6328: .seealso: TSSetFunction(), TSGetFunction()
6329: */
6330: PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6331: {
6332:   PetscErrorCode  ierr;
6333:   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6334:   int             nlhs  = 1,nrhs = 7;
6335:   mxArray         *plhs[1],*prhs[7];
6336:   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;


6346:   PetscMemcpy(&ls,&snes,sizeof(snes));
6347:   PetscMemcpy(&lx,&u,sizeof(u));
6348:   PetscMemcpy(&lxdot,&udot,sizeof(udot));
6349:   PetscMemcpy(&ly,&y,sizeof(u));

6351:   prhs[0] =  mxCreateDoubleScalar((double)ls);
6352:   prhs[1] =  mxCreateDoubleScalar(time);
6353:   prhs[2] =  mxCreateDoubleScalar((double)lx);
6354:   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6355:   prhs[4] =  mxCreateDoubleScalar((double)ly);
6356:   prhs[5] =  mxCreateString(sctx->funcname);
6357:   prhs[6] =  sctx->ctx;
6358:    mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");
6359:    mxGetScalar(plhs[0]);
6360:   mxDestroyArray(prhs[0]);
6361:   mxDestroyArray(prhs[1]);
6362:   mxDestroyArray(prhs[2]);
6363:   mxDestroyArray(prhs[3]);
6364:   mxDestroyArray(prhs[4]);
6365:   mxDestroyArray(prhs[5]);
6366:   mxDestroyArray(plhs[0]);
6367:   return(0);
6368: }

6370: /*
6371:    TSSetFunctionMatlab - Sets the function evaluation routine and function
6372:    vector for use by the TS routines in solving ODEs
6373:    equations from MATLAB. Here the function is a string containing the name of a MATLAB function

6375:    Logically Collective on TS

6377:    Input Parameters:
6378: +  ts - the TS context
6379: -  func - function evaluation routine

6381:    Calling sequence of func:
6382: $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);

6384:    Level: beginner

6386: .keywords: TS, nonlinear, set, function

6388: .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6389: */
6390: PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6391: {
6392:   PetscErrorCode  ierr;
6393:   TSMatlabContext *sctx;

6396:   /* currently sctx is memory bleed */
6397:   PetscNew(&sctx);
6398:   PetscStrallocpy(func,&sctx->funcname);
6399:   /*
6400:      This should work, but it doesn't
6401:   sctx->ctx = ctx;
6402:   mexMakeArrayPersistent(sctx->ctx);
6403:   */
6404:   sctx->ctx = mxDuplicateArray(ctx);

6406:   TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);
6407:   return(0);
6408: }

6410: /*
6411:    TSComputeJacobian_Matlab - Calls the function that has been set with
6412:                          TSSetJacobianMatlab().

6414:    Collective on TS

6416:    Input Parameters:
6417: +  ts - the TS context
6418: .  u - input vector
6419: .  A, B - the matrices
6420: -  ctx - user context

6422:    Level: developer

6424: .keywords: TS, nonlinear, compute, function

6426: .seealso: TSSetFunction(), TSGetFunction()
6427: @*/
6428: PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6429: {
6430:   PetscErrorCode  ierr;
6431:   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6432:   int             nlhs  = 2,nrhs = 9;
6433:   mxArray         *plhs[2],*prhs[9];
6434:   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;


6440:   /* call Matlab function in ctx with arguments u and y */

6442:   PetscMemcpy(&ls,&ts,sizeof(ts));
6443:   PetscMemcpy(&lx,&u,sizeof(u));
6444:   PetscMemcpy(&lxdot,&udot,sizeof(u));
6445:   PetscMemcpy(&lA,A,sizeof(u));
6446:   PetscMemcpy(&lB,B,sizeof(u));

6448:   prhs[0] =  mxCreateDoubleScalar((double)ls);
6449:   prhs[1] =  mxCreateDoubleScalar((double)time);
6450:   prhs[2] =  mxCreateDoubleScalar((double)lx);
6451:   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6452:   prhs[4] =  mxCreateDoubleScalar((double)shift);
6453:   prhs[5] =  mxCreateDoubleScalar((double)lA);
6454:   prhs[6] =  mxCreateDoubleScalar((double)lB);
6455:   prhs[7] =  mxCreateString(sctx->funcname);
6456:   prhs[8] =  sctx->ctx;
6457:    mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");
6458:    mxGetScalar(plhs[0]);
6459:   mxDestroyArray(prhs[0]);
6460:   mxDestroyArray(prhs[1]);
6461:   mxDestroyArray(prhs[2]);
6462:   mxDestroyArray(prhs[3]);
6463:   mxDestroyArray(prhs[4]);
6464:   mxDestroyArray(prhs[5]);
6465:   mxDestroyArray(prhs[6]);
6466:   mxDestroyArray(prhs[7]);
6467:   mxDestroyArray(plhs[0]);
6468:   mxDestroyArray(plhs[1]);
6469:   return(0);
6470: }

6472: /*
6473:    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6474:    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

6476:    Logically Collective on TS

6478:    Input Parameters:
6479: +  ts - the TS context
6480: .  A,B - Jacobian matrices
6481: .  func - function evaluation routine
6482: -  ctx - user context

6484:    Calling sequence of func:
6485: $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);

6487:    Level: developer

6489: .keywords: TS, nonlinear, set, function

6491: .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6492: */
6493: PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6494: {
6495:   PetscErrorCode  ierr;
6496:   TSMatlabContext *sctx;

6499:   /* currently sctx is memory bleed */
6500:   PetscNew(&sctx);
6501:   PetscStrallocpy(func,&sctx->funcname);
6502:   /*
6503:      This should work, but it doesn't
6504:   sctx->ctx = ctx;
6505:   mexMakeArrayPersistent(sctx->ctx);
6506:   */
6507:   sctx->ctx = mxDuplicateArray(ctx);

6509:   TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);
6510:   return(0);
6511: }

6513: /*
6514:    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().

6516:    Collective on TS

6518: .seealso: TSSetFunction(), TSGetFunction()
6519: @*/
6520: PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6521: {
6522:   PetscErrorCode  ierr;
6523:   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6524:   int             nlhs  = 1,nrhs = 6;
6525:   mxArray         *plhs[1],*prhs[6];
6526:   long long int   lx = 0,ls = 0;


6532:   PetscMemcpy(&ls,&ts,sizeof(ts));
6533:   PetscMemcpy(&lx,&u,sizeof(u));

6535:   prhs[0] =  mxCreateDoubleScalar((double)ls);
6536:   prhs[1] =  mxCreateDoubleScalar((double)it);
6537:   prhs[2] =  mxCreateDoubleScalar((double)time);
6538:   prhs[3] =  mxCreateDoubleScalar((double)lx);
6539:   prhs[4] =  mxCreateString(sctx->funcname);
6540:   prhs[5] =  sctx->ctx;
6541:    mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");
6542:    mxGetScalar(plhs[0]);
6543:   mxDestroyArray(prhs[0]);
6544:   mxDestroyArray(prhs[1]);
6545:   mxDestroyArray(prhs[2]);
6546:   mxDestroyArray(prhs[3]);
6547:   mxDestroyArray(prhs[4]);
6548:   mxDestroyArray(plhs[0]);
6549:   return(0);
6550: }

6552: /*
6553:    TSMonitorSetMatlab - Sets the monitor function from Matlab

6555:    Level: developer

6557: .keywords: TS, nonlinear, set, function

6559: .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6560: */
6561: PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6562: {
6563:   PetscErrorCode  ierr;
6564:   TSMatlabContext *sctx;

6567:   /* currently sctx is memory bleed */
6568:   PetscNew(&sctx);
6569:   PetscStrallocpy(func,&sctx->funcname);
6570:   /*
6571:      This should work, but it doesn't
6572:   sctx->ctx = ctx;
6573:   mexMakeArrayPersistent(sctx->ctx);
6574:   */
6575:   sctx->ctx = mxDuplicateArray(ctx);

6577:   TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);
6578:   return(0);
6579: }
6580: #endif

6582: /*@C
6583:    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6584:        in a time based line graph

6586:    Collective on TS

6588:    Input Parameters:
6589: +  ts - the TS context
6590: .  step - current time-step
6591: .  ptime - current time
6592: .  u - current solution
6593: -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()

6595:    Options Database:
6596: .   -ts_monitor_lg_solution_variables

6598:    Level: intermediate

6600:    Notes:
6601:     Each process in a parallel run displays its component solutions in a separate window

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

6605: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
6606:            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
6607:            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
6608:            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6609: @*/
6610: PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6611: {
6612:   PetscErrorCode    ierr;
6613:   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6614:   const PetscScalar *yy;
6615:   Vec               v;

6618:   if (step < 0) return(0); /* -1 indicates interpolated solution */
6619:   if (!step) {
6620:     PetscDrawAxis axis;
6621:     PetscInt      dim;
6622:     PetscDrawLGGetAxis(ctx->lg,&axis);
6623:     PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");
6624:     if (!ctx->names) {
6625:       PetscBool flg;
6626:       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6627:       PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);
6628:       if (flg) {
6629:         PetscInt i,n;
6630:         char     **names;
6631:         VecGetSize(u,&n);
6632:         PetscMalloc1(n+1,&names);
6633:         for (i=0; i<n; i++) {
6634:           PetscMalloc1(5,&names[i]);
6635:           PetscSNPrintf(names[i],5,"%D",i);
6636:         }
6637:         names[n] = NULL;
6638:         ctx->names = names;
6639:       }
6640:     }
6641:     if (ctx->names && !ctx->displaynames) {
6642:       char      **displaynames;
6643:       PetscBool flg;
6644:       VecGetLocalSize(u,&dim);
6645:       PetscMalloc1(dim+1,&displaynames);
6646:       PetscMemzero(displaynames,(dim+1)*sizeof(char*));
6647:       PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);
6648:       if (flg) {
6649:         TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);
6650:       }
6651:       PetscStrArrayDestroy(&displaynames);
6652:     }
6653:     if (ctx->displaynames) {
6654:       PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);
6655:       PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);
6656:     } else if (ctx->names) {
6657:       VecGetLocalSize(u,&dim);
6658:       PetscDrawLGSetDimension(ctx->lg,dim);
6659:       PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);
6660:     } else {
6661:       VecGetLocalSize(u,&dim);
6662:       PetscDrawLGSetDimension(ctx->lg,dim);
6663:     }
6664:     PetscDrawLGReset(ctx->lg);
6665:   }

6667:   if (!ctx->transform) v = u;
6668:   else {(*ctx->transform)(ctx->transformctx,u,&v);}
6669:   VecGetArrayRead(v,&yy);
6670:   if (ctx->displaynames) {
6671:     PetscInt i;
6672:     for (i=0; i<ctx->ndisplayvariables; i++)
6673:       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
6674:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);
6675:   } else {
6676: #if defined(PETSC_USE_COMPLEX)
6677:     PetscInt  i,n;
6678:     PetscReal *yreal;
6679:     VecGetLocalSize(v,&n);
6680:     PetscMalloc1(n,&yreal);
6681:     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
6682:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);
6683:     PetscFree(yreal);
6684: #else
6685:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);
6686: #endif
6687:   }
6688:   VecRestoreArrayRead(v,&yy);
6689:   if (ctx->transform) {VecDestroy(&v);}

6691:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
6692:     PetscDrawLGDraw(ctx->lg);
6693:     PetscDrawLGSave(ctx->lg);
6694:   }
6695:   return(0);
6696: }

6698: /*@C
6699:    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot

6701:    Collective on TS

6703:    Input Parameters:
6704: +  ts - the TS context
6705: -  names - the names of the components, final string must be NULL

6707:    Level: intermediate

6709:    Notes:
6710:     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

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

6714: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6715: @*/
6716: PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6717: {
6718:   PetscErrorCode    ierr;
6719:   PetscInt          i;

6722:   for (i=0; i<ts->numbermonitors; i++) {
6723:     if (ts->monitor[i] == TSMonitorLGSolution) {
6724:       TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);
6725:       break;
6726:     }
6727:   }
6728:   return(0);
6729: }

6731: /*@C
6732:    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot

6734:    Collective on TS

6736:    Input Parameters:
6737: +  ts - the TS context
6738: -  names - the names of the components, final string must be NULL

6740:    Level: intermediate

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

6744: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6745: @*/
6746: PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6747: {
6748:   PetscErrorCode    ierr;

6751:   PetscStrArrayDestroy(&ctx->names);
6752:   PetscStrArrayallocpy(names,&ctx->names);
6753:   return(0);
6754: }

6756: /*@C
6757:    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot

6759:    Collective on TS

6761:    Input Parameter:
6762: .  ts - the TS context

6764:    Output Parameter:
6765: .  names - the names of the components, final string must be NULL

6767:    Level: intermediate

6769:    Notes:
6770:     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

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

6774: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6775: @*/
6776: PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6777: {
6778:   PetscInt       i;

6781:   *names = NULL;
6782:   for (i=0; i<ts->numbermonitors; i++) {
6783:     if (ts->monitor[i] == TSMonitorLGSolution) {
6784:       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6785:       *names = (const char *const *)ctx->names;
6786:       break;
6787:     }
6788:   }
6789:   return(0);
6790: }

6792: /*@C
6793:    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor

6795:    Collective on TS

6797:    Input Parameters:
6798: +  ctx - the TSMonitorLG context
6799: .  displaynames - the names of the components, final string must be NULL

6801:    Level: intermediate

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

6805: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6806: @*/
6807: PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6808: {
6809:   PetscInt          j = 0,k;
6810:   PetscErrorCode    ierr;

6813:   if (!ctx->names) return(0);
6814:   PetscStrArrayDestroy(&ctx->displaynames);
6815:   PetscStrArrayallocpy(displaynames,&ctx->displaynames);
6816:   while (displaynames[j]) j++;
6817:   ctx->ndisplayvariables = j;
6818:   PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);
6819:   PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);
6820:   j = 0;
6821:   while (displaynames[j]) {
6822:     k = 0;
6823:     while (ctx->names[k]) {
6824:       PetscBool flg;
6825:       PetscStrcmp(displaynames[j],ctx->names[k],&flg);
6826:       if (flg) {
6827:         ctx->displayvariables[j] = k;
6828:         break;
6829:       }
6830:       k++;
6831:     }
6832:     j++;
6833:   }
6834:   return(0);
6835: }

6837: /*@C
6838:    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor

6840:    Collective on TS

6842:    Input Parameters:
6843: +  ts - the TS context
6844: .  displaynames - the names of the components, final string must be NULL

6846:    Notes:
6847:     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

6849:    Level: intermediate

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

6853: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6854: @*/
6855: PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6856: {
6857:   PetscInt          i;
6858:   PetscErrorCode    ierr;

6861:   for (i=0; i<ts->numbermonitors; i++) {
6862:     if (ts->monitor[i] == TSMonitorLGSolution) {
6863:       TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);
6864:       break;
6865:     }
6866:   }
6867:   return(0);
6868: }

6870: /*@C
6871:    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed

6873:    Collective on TS

6875:    Input Parameters:
6876: +  ts - the TS context
6877: .  transform - the transform function
6878: .  destroy - function to destroy the optional context
6879: -  ctx - optional context used by transform function

6881:    Notes:
6882:     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored

6884:    Level: intermediate

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

6888: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
6889: @*/
6890: PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6891: {
6892:   PetscInt          i;
6893:   PetscErrorCode    ierr;

6896:   for (i=0; i<ts->numbermonitors; i++) {
6897:     if (ts->monitor[i] == TSMonitorLGSolution) {
6898:       TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);
6899:     }
6900:   }
6901:   return(0);
6902: }

6904: /*@C
6905:    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed

6907:    Collective on TSLGCtx

6909:    Input Parameters:
6910: +  ts - the TS context
6911: .  transform - the transform function
6912: .  destroy - function to destroy the optional context
6913: -  ctx - optional context used by transform function

6915:    Level: intermediate

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

6919: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6920: @*/
6921: PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6922: {
6924:   ctx->transform    = transform;
6925:   ctx->transformdestroy = destroy;
6926:   ctx->transformctx = tctx;
6927:   return(0);
6928: }

6930: /*@C
6931:    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6932:        in a time based line graph

6934:    Collective on TS

6936:    Input Parameters:
6937: +  ts - the TS context
6938: .  step - current time-step
6939: .  ptime - current time
6940: .  u - current solution
6941: -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()

6943:    Level: intermediate

6945:    Notes:
6946:     Each process in a parallel run displays its component errors in a separate window

6948:    The user must provide the solution using TSSetSolutionFunction() to use this monitor.

6950:    Options Database Keys:
6951: .  -ts_monitor_lg_error - create a graphical monitor of error history

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

6955: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6956: @*/
6957: PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6958: {
6959:   PetscErrorCode    ierr;
6960:   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6961:   const PetscScalar *yy;
6962:   Vec               y;

6965:   if (!step) {
6966:     PetscDrawAxis axis;
6967:     PetscInt      dim;
6968:     PetscDrawLGGetAxis(ctx->lg,&axis);
6969:     PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");
6970:     VecGetLocalSize(u,&dim);
6971:     PetscDrawLGSetDimension(ctx->lg,dim);
6972:     PetscDrawLGReset(ctx->lg);
6973:   }
6974:   VecDuplicate(u,&y);
6975:   TSComputeSolutionFunction(ts,ptime,y);
6976:   VecAXPY(y,-1.0,u);
6977:   VecGetArrayRead(y,&yy);
6978: #if defined(PETSC_USE_COMPLEX)
6979:   {
6980:     PetscReal *yreal;
6981:     PetscInt  i,n;
6982:     VecGetLocalSize(y,&n);
6983:     PetscMalloc1(n,&yreal);
6984:     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
6985:     PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);
6986:     PetscFree(yreal);
6987:   }
6988: #else
6989:   PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);
6990: #endif
6991:   VecRestoreArrayRead(y,&yy);
6992:   VecDestroy(&y);
6993:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
6994:     PetscDrawLGDraw(ctx->lg);
6995:     PetscDrawLGSave(ctx->lg);
6996:   }
6997:   return(0);
6998: }

7000: /*@C
7001:    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot

7003:    Input Parameters:
7004: +  ts - the TS context
7005: .  step - current time-step
7006: .  ptime - current time
7007: .  u - current solution
7008: -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()

7010:    Options Database:
7011: .   -ts_monitor_sp_swarm

7013:    Level: intermediate

7015: .keywords: TS,  vector, monitor, view, swarm
7016: @*/
7017: PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7018: {
7019:   PetscErrorCode    ierr;
7020:   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
7021:   const PetscScalar *yy;
7022:   PetscReal       *y,*x;
7023:   PetscInt          Np, p, dim=2;
7024:   DM                dm;

7027: 
7028:   if (step < 0) return(0); /* -1 indicates interpolated solution */
7029:   if (!step) {
7030:     PetscDrawAxis axis;
7031:     PetscDrawSPGetAxis(ctx->sp,&axis);
7032:     PetscDrawAxisSetLabels(axis,"Particles","X","Y");
7033:     PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);
7034:     PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);
7035:     TSGetDM(ts, &dm);
7036:     DMGetDimension(dm, &dim);
7037:     if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");
7038:     VecGetLocalSize(u, &Np);
7039:     Np /= 2*dim;
7040:     PetscDrawSPSetDimension(ctx->sp, Np);
7041:     PetscDrawSPReset(ctx->sp);
7042:   }
7043: 
7044:   VecGetLocalSize(u, &Np);
7045:   Np /= 2*dim;
7046:   VecGetArrayRead(u,&yy);
7047:   PetscMalloc2(Np, &x, Np, &y);
7048:   /* get points from solution vector */
7049:   for (p=0; p<Np; ++p){
7050:     x[p] = PetscRealPart(yy[2*dim*p]);
7051:     y[p] = PetscRealPart(yy[2*dim*p+1]);
7052:   }
7053:   VecRestoreArrayRead(u,&yy);
7054: 
7055:   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
7056:     PetscDrawSPAddPoint(ctx->sp,x,y);
7057:     PetscDrawSPDraw(ctx->sp,PETSC_FALSE);
7058:     PetscDrawSPSave(ctx->sp);
7059:   }

7061:   PetscFree2(x, y);

7063:   return(0);
7064: }



7068: /*@C
7069:    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep

7071:    Collective on TS

7073:    Input Parameters:
7074: +  ts - the TS context
7075: .  step - current time-step
7076: .  ptime - current time
7077: .  u - current solution
7078: -  dctx - unused context

7080:    Level: intermediate

7082:    The user must provide the solution using TSSetSolutionFunction() to use this monitor.

7084:    Options Database Keys:
7085: .  -ts_monitor_error - create a graphical monitor of error history

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

7089: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7090: @*/
7091: PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
7092: {
7093:   PetscErrorCode    ierr;
7094:   Vec               y;
7095:   PetscReal         nrm;
7096:   PetscBool         flg;

7099:   VecDuplicate(u,&y);
7100:   TSComputeSolutionFunction(ts,ptime,y);
7101:   VecAXPY(y,-1.0,u);
7102:   PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);
7103:   if (flg) {
7104:     VecNorm(y,NORM_2,&nrm);
7105:     PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);
7106:   }
7107:   PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);
7108:   if (flg) {
7109:     VecView(y,vf->viewer);
7110:   }
7111:   VecDestroy(&y);
7112:   return(0);
7113: }

7115: PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7116: {
7117:   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7118:   PetscReal      x   = ptime,y;
7120:   PetscInt       its;

7123:   if (n < 0) return(0); /* -1 indicates interpolated solution */
7124:   if (!n) {
7125:     PetscDrawAxis axis;
7126:     PetscDrawLGGetAxis(ctx->lg,&axis);
7127:     PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");
7128:     PetscDrawLGReset(ctx->lg);
7129:     ctx->snes_its = 0;
7130:   }
7131:   TSGetSNESIterations(ts,&its);
7132:   y    = its - ctx->snes_its;
7133:   PetscDrawLGAddPoint(ctx->lg,&x,&y);
7134:   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7135:     PetscDrawLGDraw(ctx->lg);
7136:     PetscDrawLGSave(ctx->lg);
7137:   }
7138:   ctx->snes_its = its;
7139:   return(0);
7140: }

7142: PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7143: {
7144:   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7145:   PetscReal      x   = ptime,y;
7147:   PetscInt       its;

7150:   if (n < 0) return(0); /* -1 indicates interpolated solution */
7151:   if (!n) {
7152:     PetscDrawAxis axis;
7153:     PetscDrawLGGetAxis(ctx->lg,&axis);
7154:     PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");
7155:     PetscDrawLGReset(ctx->lg);
7156:     ctx->ksp_its = 0;
7157:   }
7158:   TSGetKSPIterations(ts,&its);
7159:   y    = its - ctx->ksp_its;
7160:   PetscDrawLGAddPoint(ctx->lg,&x,&y);
7161:   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7162:     PetscDrawLGDraw(ctx->lg);
7163:     PetscDrawLGSave(ctx->lg);
7164:   }
7165:   ctx->ksp_its = its;
7166:   return(0);
7167: }

7169: /*@
7170:    TSComputeLinearStability - computes the linear stability function at a point

7172:    Collective on TS and Vec

7174:    Input Parameters:
7175: +  ts - the TS context
7176: -  xr,xi - real and imaginary part of input arguments

7178:    Output Parameters:
7179: .  yr,yi - real and imaginary part of function value

7181:    Level: developer

7183: .keywords: TS, compute

7185: .seealso: TSSetRHSFunction(), TSComputeIFunction()
7186: @*/
7187: PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7188: {

7193:   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7194:   (*ts->ops->linearstability)(ts,xr,xi,yr,yi);
7195:   return(0);
7196: }

7198: /* ------------------------------------------------------------------------*/
7199: /*@C
7200:    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()

7202:    Collective on TS

7204:    Input Parameters:
7205: .  ts  - the ODE solver object

7207:    Output Parameter:
7208: .  ctx - the context

7210:    Level: intermediate

7212: .keywords: TS, monitor, line graph, residual, seealso

7214: .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()

7216: @*/
7217: PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7218: {

7222:   PetscNew(ctx);
7223:   return(0);
7224: }

7226: /*@C
7227:    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution

7229:    Collective on TS

7231:    Input Parameters:
7232: +  ts - the TS context
7233: .  step - current time-step
7234: .  ptime - current time
7235: .  u  - current solution
7236: -  dctx - the envelope context

7238:    Options Database:
7239: .  -ts_monitor_envelope

7241:    Level: intermediate

7243:    Notes:
7244:     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope

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

7248: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7249: @*/
7250: PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7251: {
7252:   PetscErrorCode       ierr;
7253:   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;

7256:   if (!ctx->max) {
7257:     VecDuplicate(u,&ctx->max);
7258:     VecDuplicate(u,&ctx->min);
7259:     VecCopy(u,ctx->max);
7260:     VecCopy(u,ctx->min);
7261:   } else {
7262:     VecPointwiseMax(ctx->max,u,ctx->max);
7263:     VecPointwiseMin(ctx->min,u,ctx->min);
7264:   }
7265:   return(0);
7266: }

7268: /*@C
7269:    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution

7271:    Collective on TS

7273:    Input Parameter:
7274: .  ts - the TS context

7276:    Output Parameter:
7277: +  max - the maximum values
7278: -  min - the minimum values

7280:    Notes:
7281:     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored

7283:    Level: intermediate

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

7287: .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7288: @*/
7289: PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7290: {
7291:   PetscInt i;

7294:   if (max) *max = NULL;
7295:   if (min) *min = NULL;
7296:   for (i=0; i<ts->numbermonitors; i++) {
7297:     if (ts->monitor[i] == TSMonitorEnvelope) {
7298:       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7299:       if (max) *max = ctx->max;
7300:       if (min) *min = ctx->min;
7301:       break;
7302:     }
7303:   }
7304:   return(0);
7305: }

7307: /*@C
7308:    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().

7310:    Collective on TSMonitorEnvelopeCtx

7312:    Input Parameter:
7313: .  ctx - the monitor context

7315:    Level: intermediate

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

7319: .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7320: @*/
7321: PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7322: {

7326:   VecDestroy(&(*ctx)->min);
7327:   VecDestroy(&(*ctx)->max);
7328:   PetscFree(*ctx);
7329:   return(0);
7330: }

7332: /*@
7333:    TSRestartStep - Flags the solver to restart the next step

7335:    Collective on TS

7337:    Input Parameter:
7338: .  ts - the TS context obtained from TSCreate()

7340:    Level: advanced

7342:    Notes:
7343:    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7344:    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7345:    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7346:    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7347:    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7348:    discontinuous source terms).

7350: .keywords: TS, timestep, restart

7352: .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7353: @*/
7354: PetscErrorCode TSRestartStep(TS ts)
7355: {
7358:   ts->steprestart = PETSC_TRUE;
7359:   return(0);
7360: }

7362: /*@
7363:    TSRollBack - Rolls back one time step

7365:    Collective on TS

7367:    Input Parameter:
7368: .  ts - the TS context obtained from TSCreate()

7370:    Level: advanced

7372: .keywords: TS, timestep, rollback

7374: .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
7375: @*/
7376: PetscErrorCode  TSRollBack(TS ts)
7377: {

7382:   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
7383:   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
7384:   (*ts->ops->rollback)(ts);
7385:   ts->time_step = ts->ptime - ts->ptime_prev;
7386:   ts->ptime = ts->ptime_prev;
7387:   ts->ptime_prev = ts->ptime_prev_rollback;
7388:   ts->steps--;
7389:   ts->steprollback = PETSC_TRUE;
7390:   return(0);
7391: }

7393: /*@
7394:    TSGetStages - Get the number of stages and stage values

7396:    Input Parameter:
7397: .  ts - the TS context obtained from TSCreate()

7399:    Output Parameters:
7400: +  ns - the number of stages
7401: -  Y - the current stage vectors

7403:    Level: advanced

7405:    Notes: Both ns and Y can be NULL.

7407: .keywords: TS, getstages

7409: .seealso: TSCreate()
7410: @*/
7411: PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7412: {

7419:   if (!ts->ops->getstages) {
7420:     if (ns) *ns = 0;
7421:     if (Y) *Y = NULL;
7422:   } else {
7423:     (*ts->ops->getstages)(ts,ns,Y);
7424:   }
7425:   return(0);
7426: }

7428: /*@C
7429:   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.

7431:   Collective on SNES

7433:   Input Parameters:
7434: + ts - the TS context
7435: . t - current timestep
7436: . U - state vector
7437: . Udot - time derivative of state vector
7438: . shift - shift to apply, see note below
7439: - ctx - an optional user context

7441:   Output Parameters:
7442: + J - Jacobian matrix (not altered in this routine)
7443: - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)

7445:   Level: intermediate

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

7450:   dF/dU + shift*dF/dUdot

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

7455:   This will first try to get the coloring from the DM.  If the DM type has no coloring
7456:   routine, then it will try to get the coloring from the matrix.  This requires that the
7457:   matrix have nonzero entries precomputed.

7459: .keywords: TS, finite differences, Jacobian, coloring, sparse
7460: .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7461: @*/
7462: PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7463: {
7464:   SNES           snes;
7465:   MatFDColoring  color;
7466:   PetscBool      hascolor, matcolor = PETSC_FALSE;

7470:   PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);
7471:   PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);
7472:   if (!color) {
7473:     DM         dm;
7474:     ISColoring iscoloring;

7476:     TSGetDM(ts, &dm);
7477:     DMHasColoring(dm, &hascolor);
7478:     if (hascolor && !matcolor) {
7479:       DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);
7480:       MatFDColoringCreate(B, iscoloring, &color);
7481:       MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);
7482:       MatFDColoringSetFromOptions(color);
7483:       MatFDColoringSetUp(B, iscoloring, color);
7484:       ISColoringDestroy(&iscoloring);
7485:     } else {
7486:       MatColoring mc;

7488:       MatColoringCreate(B, &mc);
7489:       MatColoringSetDistance(mc, 2);
7490:       MatColoringSetType(mc, MATCOLORINGSL);
7491:       MatColoringSetFromOptions(mc);
7492:       MatColoringApply(mc, &iscoloring);
7493:       MatColoringDestroy(&mc);
7494:       MatFDColoringCreate(B, iscoloring, &color);
7495:       MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);
7496:       MatFDColoringSetFromOptions(color);
7497:       MatFDColoringSetUp(B, iscoloring, color);
7498:       ISColoringDestroy(&iscoloring);
7499:     }
7500:     PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);
7501:     PetscObjectDereference((PetscObject) color);
7502:   }
7503:   TSGetSNES(ts, &snes);
7504:   MatFDColoringApply(B, color, U, snes);
7505:   if (J != B) {
7506:     MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);
7507:     MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);
7508:   }
7509:   return(0);
7510: }

7512: /*@
7513:     TSSetFunctionDomainError - Set the function testing if the current state vector is valid

7515:     Input Parameters:
7516:     ts - the TS context
7517:     func - function called within TSFunctionDomainError

7519:     Level: intermediate

7521: .keywords: TS, state, domain
7522: .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7523: @*/

7525: PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7526: {
7529:   ts->functiondomainerror = func;
7530:   return(0);
7531: }

7533: /*@
7534:     TSFunctionDomainError - Check if the current state is valid

7536:     Input Parameters:
7537:     ts - the TS context
7538:     stagetime - time of the simulation
7539:     Y - state vector to check.

7541:     Output Parameter:
7542:     accept - Set to PETSC_FALSE if the current state vector is valid.

7544:     Note:
7545:     This function should be used to ensure the state is in a valid part of the space.
7546:     For example, one can ensure here all values are positive.

7548:     Level: advanced
7549: @*/
7550: PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7551: {
7554:   *accept = PETSC_TRUE;
7555:   if (ts->functiondomainerror) {
7556:     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7557:   }
7558:   return(0);
7559: }

7561: /*@C
7562:   TSClone - This function clones a time step object.

7564:   Collective on MPI_Comm

7566:   Input Parameter:
7567: . tsin    - The input TS

7569:   Output Parameter:
7570: . tsout   - The output TS (cloned)

7572:   Notes:
7573:   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.

7575:   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);

7577:   Level: developer

7579: .keywords: TS, clone
7580: .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
7581: @*/
7582: PetscErrorCode  TSClone(TS tsin, TS *tsout)
7583: {
7584:   TS             t;
7586:   SNES           snes_start;
7587:   DM             dm;
7588:   TSType         type;

7592:   *tsout = NULL;

7594:   PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);

7596:   /* General TS description */
7597:   t->numbermonitors    = 0;
7598:   t->setupcalled       = 0;
7599:   t->ksp_its           = 0;
7600:   t->snes_its          = 0;
7601:   t->nwork             = 0;
7602:   t->rhsjacobian.time  = -1e20;
7603:   t->rhsjacobian.scale = 1.;
7604:   t->ijacobian.shift   = 1.;

7606:   TSGetSNES(tsin,&snes_start);
7607:   TSSetSNES(t,snes_start);

7609:   TSGetDM(tsin,&dm);
7610:   TSSetDM(t,dm);

7612:   t->adapt = tsin->adapt;
7613:   PetscObjectReference((PetscObject)t->adapt);

7615:   t->trajectory = tsin->trajectory;
7616:   PetscObjectReference((PetscObject)t->trajectory);

7618:   t->event = tsin->event;
7619:   if (t->event) t->event->refct++;

7621:   t->problem_type      = tsin->problem_type;
7622:   t->ptime             = tsin->ptime;
7623:   t->ptime_prev        = tsin->ptime_prev;
7624:   t->time_step         = tsin->time_step;
7625:   t->max_time          = tsin->max_time;
7626:   t->steps             = tsin->steps;
7627:   t->max_steps         = tsin->max_steps;
7628:   t->equation_type     = tsin->equation_type;
7629:   t->atol              = tsin->atol;
7630:   t->rtol              = tsin->rtol;
7631:   t->max_snes_failures = tsin->max_snes_failures;
7632:   t->max_reject        = tsin->max_reject;
7633:   t->errorifstepfailed = tsin->errorifstepfailed;

7635:   TSGetType(tsin,&type);
7636:   TSSetType(t,type);

7638:   t->vec_sol           = NULL;

7640:   t->cfltime          = tsin->cfltime;
7641:   t->cfltime_local    = tsin->cfltime_local;
7642:   t->exact_final_time = tsin->exact_final_time;

7644:   PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));

7646:   if (((PetscObject)tsin)->fortran_func_pointers) {
7647:     PetscInt i;
7648:     PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);
7649:     for (i=0; i<10; i++) {
7650:       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
7651:     }
7652:   }
7653:   *tsout = t;
7654:   return(0);
7655: }

7657: static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7658: {
7660:   TS             ts = (TS) ctx;

7663:   TSComputeRHSFunction(ts,0,x,y);
7664:   return(0);
7665: }

7667: /*@
7668:     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.

7670:    Logically Collective on TS and Mat

7672:     Input Parameters:
7673:     TS - the time stepping routine

7675:    Output Parameter:
7676: .   flg - PETSC_TRUE if the multiply is likely correct

7678:    Options Database:
7679:  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator

7681:    Level: advanced

7683:    Notes:
7684:     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian

7686: .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7687: @*/
7688: PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7689: {
7690:   Mat            J,B;
7692:   TSRHSJacobian  func;
7693:   void*          ctx;

7696:   TSGetRHSJacobian(ts,&J,&B,&func,&ctx);
7697:   (*func)(ts,0.0,ts->vec_sol,J,B,ctx);
7698:   MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);
7699:   return(0);
7700: }

7702: /*@C
7703:     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.

7705:    Logically Collective on TS and Mat

7707:     Input Parameters:
7708:     TS - the time stepping routine

7710:    Output Parameter:
7711: .   flg - PETSC_TRUE if the multiply is likely correct

7713:    Options Database:
7714: .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator

7716:    Notes:
7717:     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian

7719:    Level: advanced

7721: .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7722: @*/
7723: PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7724: {
7725:   Mat            J,B;
7727:   void           *ctx;
7728:   TSRHSJacobian  func;

7731:   TSGetRHSJacobian(ts,&J,&B,&func,&ctx);
7732:   (*func)(ts,0.0,ts->vec_sol,J,B,ctx);
7733:   MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);
7734:   return(0);
7735: }

7737: /*@
7738:   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.

7740:   Logically collective

7742:   Input Parameter:
7743: +  ts - timestepping context
7744: -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used

7746:   Options Database:
7747: .   -ts_use_splitrhsfunction - <true,false>

7749:   Notes:
7750:     This is only useful for multirate methods

7752:   Level: intermediate

7754: .seealso: TSGetUseSplitRHSFunction()
7755: @*/
7756: PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
7757: {
7760:   ts->use_splitrhsfunction = use_splitrhsfunction;
7761:   return(0);
7762: }

7764: /*@
7765:   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.

7767:   Not collective

7769:   Input Parameter:
7770: .  ts - timestepping context

7772:   Output Parameter:
7773: .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used

7775:   Level: intermediate

7777: .seealso: TSSetUseSplitRHSFunction()
7778: @*/
7779: PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
7780: {
7783:   *use_splitrhsfunction = ts->use_splitrhsfunction;
7784:   return(0);
7785: }