xref: /petsc/src/ts/interface/ts.c (revision a96d6ef6957c3dec05b3047f9fb6fbdd7aa878e2)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6900f6b5bSMatthew G. Knepley #include <petscconvest.h>
7d763cef2SBarry Smith 
81c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if(PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;
91c167fc2SEmil Constantinescu 
10d5ba7fb7SMatthew Knepley /* Logging support */
11d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
13d405a339SMatthew Knepley 
14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",NULL};
1549354f04SShri Abhyankar 
161c167fc2SEmil Constantinescu 
17fde5950dSBarry Smith /*@C
18fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
19fde5950dSBarry Smith 
20fde5950dSBarry Smith    Collective on TS
21fde5950dSBarry Smith 
22fde5950dSBarry Smith    Input Parameters:
23fde5950dSBarry Smith +  ts - TS object you wish to monitor
24fde5950dSBarry Smith .  name - the monitor type one is seeking
25fde5950dSBarry Smith .  help - message indicating what monitoring is done
26fde5950dSBarry Smith .  manual - manual page for the monitor
27fde5950dSBarry Smith .  monitor - the monitor function
28fde5950dSBarry Smith -  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
29fde5950dSBarry Smith 
30fde5950dSBarry Smith    Level: developer
31fde5950dSBarry Smith 
32fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
33fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
34fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
35fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
36fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
37fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
38fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
39fde5950dSBarry Smith @*/
40721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
41fde5950dSBarry Smith {
42fde5950dSBarry Smith   PetscErrorCode    ierr;
43fde5950dSBarry Smith   PetscViewer       viewer;
44fde5950dSBarry Smith   PetscViewerFormat format;
45fde5950dSBarry Smith   PetscBool         flg;
46fde5950dSBarry Smith 
47fde5950dSBarry Smith   PetscFunctionBegin;
4816413a6aSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
49fde5950dSBarry Smith   if (flg) {
50721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
51721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
52721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
53fde5950dSBarry Smith     if (monitorsetup) {
54721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
55fde5950dSBarry Smith     }
56721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
57fde5950dSBarry Smith   }
58fde5950dSBarry Smith   PetscFunctionReturn(0);
59fde5950dSBarry Smith }
60fde5950dSBarry Smith 
612ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
622ffb9264SLisandro Dalcin {
632ffb9264SLisandro Dalcin   PetscErrorCode ierr;
642ffb9264SLisandro Dalcin 
652ffb9264SLisandro Dalcin   PetscFunctionBegin;
66b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
67b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
682ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
692ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
702ffb9264SLisandro Dalcin   }
712ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
722ffb9264SLisandro Dalcin }
732ffb9264SLisandro Dalcin 
74bdad233fSMatthew Knepley /*@
75bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
76bdad233fSMatthew Knepley 
77bdad233fSMatthew Knepley    Collective on TS
78bdad233fSMatthew Knepley 
79bdad233fSMatthew Knepley    Input Parameter:
80bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
81bdad233fSMatthew Knepley 
82bdad233fSMatthew Knepley    Options Database Keys:
83e49d4f37SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
84ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
85ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
86ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
87ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
884dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
893e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
906c51df0eSBarry Smith .  -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
91a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
92a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
93a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
94a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
95a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
96f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
97f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
98ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
99847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
100bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
101de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
102de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1037cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1046934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1058b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
106de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
107de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
108de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
109de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1103e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1113e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
112fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
113e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
1149e336e28SPatrick Sanan -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11553ea634cSHong Zhang 
1169e336e28SPatrick Sanan    Developer Note:
1179e336e28SPatrick Sanan    We should unify all the -ts_monitor options in the way that -xxx_view has been unified
118bdad233fSMatthew Knepley 
119bdad233fSMatthew Knepley    Level: beginner
120bdad233fSMatthew Knepley 
121a313700dSBarry Smith .seealso: TSGetType()
122bdad233fSMatthew Knepley @*/
1237087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
124bdad233fSMatthew Knepley {
125bc952696SBarry Smith   PetscBool              opt,flg,tflg;
126dfbe8321SBarry Smith   PetscErrorCode         ierr;
127eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
12831748224SBarry Smith   PetscReal              time_step;
12949354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
130d1212d36SBarry Smith   char                   dir[16];
131cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1326991f827SBarry Smith   const char             *defaultType;
1336991f827SBarry Smith   char                   typeName[256];
134bdad233fSMatthew Knepley 
135bdad233fSMatthew Knepley   PetscFunctionBegin;
1360700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1376991f827SBarry Smith 
1386991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
139cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
140cd11d68dSLisandro Dalcin 
141cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1421ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1431ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1446991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1456991f827SBarry Smith   if (opt) {
1466991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1476991f827SBarry Smith   } else {
1486991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1496991f827SBarry Smith   }
150bdad233fSMatthew Knepley 
151bdad233fSMatthew Knepley   /* Handle generic TS options */
1524dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
153ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15419eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1550298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
15631748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
157cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
15849354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
15949354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1600298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1610298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1630298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1640298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
165bdad233fSMatthew Knepley 
166f3b1f45cSBarry Smith   ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL);CHKERRQ(ierr);
167f3b1f45cSBarry Smith   ierr = 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);CHKERRQ(ierr);
1680fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr);
16956f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
17056f85f32SBarry Smith   {
17156f85f32SBarry Smith     PetscBool set;
17256f85f32SBarry Smith     flg  = PETSC_FALSE;
17356f85f32SBarry Smith     ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
17456f85f32SBarry Smith     if (set) {
17556f85f32SBarry Smith       ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
17656f85f32SBarry Smith     }
17756f85f32SBarry Smith   }
17856f85f32SBarry Smith #endif
17956f85f32SBarry Smith 
180bdad233fSMatthew Knepley   /* Monitor options */
181cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
182cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
183fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
184fde5950dSBarry Smith 
185589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
1865180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1875180491cSLisandro Dalcin 
1884f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
189b3603a34SBarry Smith   if (opt) {
1900b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1913923b477SBarry Smith     PetscInt       howoften = 1;
192b3603a34SBarry Smith 
1930298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
194c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1954f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
196bdad233fSMatthew Knepley   }
1976ba87a44SLisandro Dalcin 
1984f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
199ef20d060SBarry Smith   if (opt) {
2000b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2013923b477SBarry Smith     PetscInt       howoften = 1;
202ef20d060SBarry Smith 
2030298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
204c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2054f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
206ef20d060SBarry Smith   }
207edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2087cf37e64SBarry Smith 
2096934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2106934998bSLisandro Dalcin   if (opt) {
2116934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2126934998bSLisandro Dalcin     PetscInt       howoften = 1;
2136934998bSLisandro Dalcin 
2146934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2156ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2166934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2176934998bSLisandro Dalcin   }
2188b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2198b668821SLisandro Dalcin   if (opt) {
2208b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2218b668821SLisandro Dalcin     PetscInt       howoften = 1;
2228b668821SLisandro Dalcin 
2238b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2248b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2258b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2268b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2278b668821SLisandro Dalcin   }
2288b668821SLisandro Dalcin 
229201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
230201da799SBarry Smith   if (opt) {
231201da799SBarry Smith     TSMonitorLGCtx ctx;
232201da799SBarry Smith     PetscInt       howoften = 1;
233201da799SBarry Smith 
2340298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2356ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
236201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
237201da799SBarry Smith   }
238201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
239201da799SBarry Smith   if (opt) {
240201da799SBarry Smith     TSMonitorLGCtx ctx;
241201da799SBarry Smith     PetscInt       howoften = 1;
242201da799SBarry Smith 
2430298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2446ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
245201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
246201da799SBarry Smith   }
2478189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2488189c53fSBarry Smith   if (opt) {
2498189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2508189c53fSBarry Smith     PetscInt          howoften = 1;
2518189c53fSBarry Smith 
2520298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
253c793f718SLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2548189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2558189c53fSBarry Smith   }
2560ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2571b575b74SJoseph Pusztay   if (opt) {
2581b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2591b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2600ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2611b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2620ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2631b575b74SJoseph Pusztay   }
264ef20d060SBarry Smith   opt  = PETSC_FALSE;
2650dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
266a7cc72afSBarry Smith   if (opt) {
26783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
26883a4ac43SBarry Smith     PetscInt         howoften = 1;
269a80ad3e0SBarry Smith 
2700298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
271c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
27283a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
273bdad233fSMatthew Knepley   }
274fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2752d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2762d5ee99bSBarry Smith   if (opt) {
2772d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2782d5ee99bSBarry Smith     PetscReal        bounds[4];
2792d5ee99bSBarry Smith     PetscInt         n = 4;
2802d5ee99bSBarry Smith     PetscDraw        draw;
2816934998bSLisandro Dalcin     PetscDrawAxis    axis;
2822d5ee99bSBarry Smith 
2832d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2842d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
285c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2862d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2876934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2886934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2896934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2902d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2912d5ee99bSBarry Smith   }
2922d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2930dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2943a471f94SBarry Smith   if (opt) {
29583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
29683a4ac43SBarry Smith     PetscInt         howoften = 1;
2973a471f94SBarry Smith 
2980298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
299c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
30083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3013a471f94SBarry Smith   }
3020ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3030ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3040ed3bfb6SBarry Smith   if (opt) {
3050ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3060ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3070ed3bfb6SBarry Smith 
3080ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
309c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3100ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3110ed3bfb6SBarry Smith   }
312fde5950dSBarry Smith 
313ed81e22dSJed Brown   opt  = PETSC_FALSE;
314589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
315ed81e22dSJed Brown   if (flg) {
316ed81e22dSJed Brown     const char *ptr,*ptr2;
317ed81e22dSJed Brown     char       *filetemplate;
318ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
319ed81e22dSJed Brown     /* Do some cursory validation of the input. */
320ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
321ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
322ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
323ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
324ce94432eSBarry Smith       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");
325ed81e22dSJed Brown       if (ptr2) break;
326ed81e22dSJed Brown     }
327ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
328ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
329ed81e22dSJed Brown   }
330bdad233fSMatthew Knepley 
331589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
332d1212d36SBarry Smith   if (flg) {
333d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
334d1212d36SBarry Smith     int                  ray = 0;
3353ee9839eSMatthew G. Knepley     DMDirection          ddir;
336d1212d36SBarry Smith     DM                   da;
337d1212d36SBarry Smith     PetscMPIInt          rank;
338d1212d36SBarry Smith 
339ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3403ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3413ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
342ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
343d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
344d1212d36SBarry Smith 
3455bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr);
346b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
347d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
348d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
349ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
350d1212d36SBarry Smith     if (!rank) {
351c793f718SLisandro Dalcin       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
352d1212d36SBarry Smith     }
35351b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
354d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
355d1212d36SBarry Smith   }
356589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
35751b4a12fSMatthew G. Knepley   if (flg) {
35851b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
35951b4a12fSMatthew G. Knepley     int                 ray = 0;
3603ee9839eSMatthew G. Knepley     DMDirection         ddir;
36151b4a12fSMatthew G. Knepley     DM                  da;
36251b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
363d1212d36SBarry Smith 
36451b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3653ee9839eSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DM_X;
3663ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
36751b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
36851b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3691c3436cfSJed Brown 
3705bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr);
371b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
37251b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
37351b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
374c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
37551b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
37651b4a12fSMatthew G. Knepley   }
377a7a1495cSBarry Smith 
378b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
379b3d3934dSBarry Smith   if (opt) {
380b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
381b3d3934dSBarry Smith 
382b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
383b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
384b3d3934dSBarry Smith   }
385b3d3934dSBarry Smith 
386847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
387847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
388847ff0e1SMatthew G. Knepley   if (flg) {
389847ff0e1SMatthew G. Knepley     DM   dm;
390847ff0e1SMatthew G. Knepley     DMTS tdm;
391847ff0e1SMatthew G. Knepley 
392847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
393847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
394847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
395c793f718SLisandro Dalcin     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL);CHKERRQ(ierr);
396847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
397847ff0e1SMatthew G. Knepley   }
398847ff0e1SMatthew G. Knepley 
399d763cef2SBarry Smith   /* Handle specific TS options */
400d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4016991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
402d763cef2SBarry Smith   }
403fbc52257SHong Zhang 
404a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
405a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
406a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
407a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
408a7bdc993SLisandro Dalcin 
40968bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4104f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
41168bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
41268bece0bSHong Zhang   if (tflg) {
41368bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
41468bece0bSHong Zhang   }
415a05bf03eSHong Zhang 
416a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
417d763cef2SBarry Smith 
418d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4190633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
420fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
421d763cef2SBarry Smith 
4224f122a70SLisandro Dalcin   if (ts->trajectory) {
4234f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
424d763cef2SBarry Smith   }
42568bece0bSHong Zhang 
4261ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4274f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4281ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4291ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4301ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4311ef27442SStefano Zampini   }
4324f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
433d763cef2SBarry Smith   PetscFunctionReturn(0);
434d763cef2SBarry Smith }
435d763cef2SBarry Smith 
436d2daff3dSHong Zhang /*@
43778fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
43878fbdcc8SBarry Smith 
43978fbdcc8SBarry Smith    Collective on TS
44078fbdcc8SBarry Smith 
44178fbdcc8SBarry Smith    Input Parameters:
44278fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
44378fbdcc8SBarry Smith 
4447a7aea1fSJed Brown    Output Parameters:
44578fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
44678fbdcc8SBarry Smith 
44778fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
44878fbdcc8SBarry Smith 
44978fbdcc8SBarry Smith    Level: advanced
45078fbdcc8SBarry Smith 
45178fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
45278fbdcc8SBarry Smith 
45378fbdcc8SBarry Smith @*/
45478fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
45578fbdcc8SBarry Smith {
45678fbdcc8SBarry Smith   PetscFunctionBegin;
45778fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45878fbdcc8SBarry Smith   *tr = ts->trajectory;
45978fbdcc8SBarry Smith   PetscFunctionReturn(0);
46078fbdcc8SBarry Smith }
46178fbdcc8SBarry Smith 
46278fbdcc8SBarry Smith /*@
463bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
464d2daff3dSHong Zhang 
465d2daff3dSHong Zhang    Collective on TS
466d2daff3dSHong Zhang 
467d2daff3dSHong Zhang    Input Parameters:
468bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
469bc952696SBarry Smith 
47078fbdcc8SBarry Smith    Options Database:
47178fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
47278fbdcc8SBarry Smith -  -ts_trajectory_type type
47378fbdcc8SBarry Smith 
47468bece0bSHong Zhang Note: This routine should be called after all TS options have been set
475d2daff3dSHong Zhang 
476c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
47778fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
47878fbdcc8SBarry Smith 
479d2daff3dSHong Zhang    Level: intermediate
480d2daff3dSHong Zhang 
4812d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
482d2daff3dSHong Zhang 
483d2daff3dSHong Zhang @*/
484bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
485d2daff3dSHong Zhang {
486bc952696SBarry Smith   PetscErrorCode ierr;
487bc952696SBarry Smith 
488d2daff3dSHong Zhang   PetscFunctionBegin;
489d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
490bc952696SBarry Smith   if (!ts->trajectory) {
491bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
492bc952696SBarry Smith   }
493d2daff3dSHong Zhang   PetscFunctionReturn(0);
494d2daff3dSHong Zhang }
495d2daff3dSHong Zhang 
496a7a1495cSBarry Smith /*@
4972d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
4982d29f1f2SStefano Zampini 
4992d29f1f2SStefano Zampini    Collective on TS
5002d29f1f2SStefano Zampini 
5012d29f1f2SStefano Zampini    Input Parameters:
5022d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5032d29f1f2SStefano Zampini 
5042d29f1f2SStefano Zampini    Level: intermediate
5052d29f1f2SStefano Zampini 
5062d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5072d29f1f2SStefano Zampini 
5082d29f1f2SStefano Zampini @*/
5092d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5102d29f1f2SStefano Zampini {
5112d29f1f2SStefano Zampini   PetscErrorCode ierr;
5122d29f1f2SStefano Zampini 
5132d29f1f2SStefano Zampini   PetscFunctionBegin;
5142d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5152d29f1f2SStefano Zampini   if (ts->trajectory) {
5162d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5172d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5182d29f1f2SStefano Zampini   }
5192d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5202d29f1f2SStefano Zampini }
5212d29f1f2SStefano Zampini 
5222d29f1f2SStefano Zampini /*@
523a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
524a7a1495cSBarry Smith       set with TSSetRHSJacobian().
525a7a1495cSBarry Smith 
526d083f849SBarry Smith    Collective on TS
527a7a1495cSBarry Smith 
528a7a1495cSBarry Smith    Input Parameters:
529316643e7SJed Brown +  ts - the TS context
530a7a1495cSBarry Smith .  t - current timestep
5310910c330SBarry Smith -  U - input vector
532a7a1495cSBarry Smith 
533a7a1495cSBarry Smith    Output Parameters:
534a7a1495cSBarry Smith +  A - Jacobian matrix
535a7a1495cSBarry Smith .  B - optional preconditioning matrix
536a7a1495cSBarry Smith -  flag - flag indicating matrix structure
537a7a1495cSBarry Smith 
538a7a1495cSBarry Smith    Notes:
539a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
540a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
541a7a1495cSBarry Smith 
54294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
543a7a1495cSBarry Smith    flag parameter.
544a7a1495cSBarry Smith 
545a7a1495cSBarry Smith    Level: developer
546a7a1495cSBarry Smith 
54794b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
548a7a1495cSBarry Smith @*/
549d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
550a7a1495cSBarry Smith {
551dfbe8321SBarry Smith   PetscErrorCode   ierr;
552270bf2e7SJed Brown   PetscObjectState Ustate;
5536c1e1eecSBarry Smith   PetscObjectId    Uid;
55424989b8cSPeter Brune   DM               dm;
555942e3340SBarry Smith   DMTS             tsdm;
55624989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
55724989b8cSPeter Brune   void             *ctx;
55824989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
559b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
560a7a1495cSBarry Smith 
561a7a1495cSBarry Smith   PetscFunctionBegin;
5620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5630910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5640910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
56524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
566942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
56724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5680298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
5696374d434SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
57059e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5716c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
572971015bcSStefano Zampini 
5736c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
574971015bcSStefano Zampini     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
575971015bcSStefano Zampini     if (A == ts->Arhs) {
576971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
577971015bcSStefano Zampini         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
578971015bcSStefano Zampini       }
579971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
580971015bcSStefano Zampini         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
581971015bcSStefano Zampini       }
582971015bcSStefano Zampini     }
583971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
584971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
585971015bcSStefano Zampini         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
586971015bcSStefano Zampini       }
587971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
588971015bcSStefano Zampini         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
589971015bcSStefano Zampini       }
590971015bcSStefano Zampini     }
591971015bcSStefano Zampini     ts->rhsjacobian.shift = 0;
592971015bcSStefano Zampini     ts->rhsjacobian.scale = 1.;
5930e4ef248SJed Brown     PetscFunctionReturn(0);
594f8ede8e7SJed Brown   }
595d90be118SSean Farley 
596ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
597d90be118SSean Farley 
598e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
599971015bcSStefano Zampini     if (A == ts->Arhs) {
600971015bcSStefano Zampini       /* MatScale has a short path for this case.
601971015bcSStefano Zampini          However, this code path is taken the first time TSComputeRHSJacobian is called
602971015bcSStefano Zampini          and the matrices have not assembled yet */
603971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
60494ab13aaSBarry Smith         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
605971015bcSStefano Zampini       }
606971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
60794ab13aaSBarry Smith         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
608971015bcSStefano Zampini       }
609971015bcSStefano Zampini     }
610971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
611971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
61294ab13aaSBarry Smith         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
613971015bcSStefano Zampini       }
614971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
61594ab13aaSBarry Smith         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
616e1244c69SJed Brown       }
617971015bcSStefano Zampini     }
618e1244c69SJed Brown   }
619e1244c69SJed Brown 
62024989b8cSPeter Brune   if (rhsjacobianfunc) {
62194ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
622a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
623d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
624a7a1495cSBarry Smith     PetscStackPop;
62594ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
626ef66eb69SBarry Smith   } else {
62794ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
628971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
629ef66eb69SBarry Smith   }
6300e4ef248SJed Brown   ts->rhsjacobian.time  = t;
631971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
632971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6337f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
63459e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
635a7a1495cSBarry Smith   PetscFunctionReturn(0);
636a7a1495cSBarry Smith }
637a7a1495cSBarry Smith 
638316643e7SJed Brown /*@
639d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
640d763cef2SBarry Smith 
641d083f849SBarry Smith    Collective on TS
642316643e7SJed Brown 
643316643e7SJed Brown    Input Parameters:
644316643e7SJed Brown +  ts - the TS context
645316643e7SJed Brown .  t - current time
6460910c330SBarry Smith -  U - state vector
647316643e7SJed Brown 
648316643e7SJed Brown    Output Parameter:
649316643e7SJed Brown .  y - right hand side
650316643e7SJed Brown 
651316643e7SJed Brown    Note:
652316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
653316643e7SJed Brown    is used internally within the nonlinear solvers.
654316643e7SJed Brown 
655316643e7SJed Brown    Level: developer
656316643e7SJed Brown 
657316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
658316643e7SJed Brown @*/
6590910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
660d763cef2SBarry Smith {
661dfbe8321SBarry Smith   PetscErrorCode ierr;
66224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
66324989b8cSPeter Brune   TSIFunction    ifunction;
66424989b8cSPeter Brune   void           *ctx;
66524989b8cSPeter Brune   DM             dm;
66624989b8cSPeter Brune 
667d763cef2SBarry Smith   PetscFunctionBegin;
6680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6690910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6700700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
67124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6730298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
674d763cef2SBarry Smith 
675ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
676d763cef2SBarry Smith 
6770910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
67824989b8cSPeter Brune   if (rhsfunction) {
6791be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
680d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6810910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
682d763cef2SBarry Smith     PetscStackPop;
6831be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
684214bc6a2SJed Brown   } else {
685214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
686b2cd27e8SJed Brown   }
68744a41b28SSean Farley 
6880910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
689d763cef2SBarry Smith   PetscFunctionReturn(0);
690d763cef2SBarry Smith }
691d763cef2SBarry Smith 
692ef20d060SBarry Smith /*@
693ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
694ef20d060SBarry Smith 
695d083f849SBarry Smith    Collective on TS
696ef20d060SBarry Smith 
697ef20d060SBarry Smith    Input Parameters:
698ef20d060SBarry Smith +  ts - the TS context
699ef20d060SBarry Smith -  t - current time
700ef20d060SBarry Smith 
701ef20d060SBarry Smith    Output Parameter:
7020910c330SBarry Smith .  U - the solution
703ef20d060SBarry Smith 
704ef20d060SBarry Smith    Note:
705ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
706ef20d060SBarry Smith    is used internally within the nonlinear solvers.
707ef20d060SBarry Smith 
708ef20d060SBarry Smith    Level: developer
709ef20d060SBarry Smith 
710abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
711ef20d060SBarry Smith @*/
7120910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
713ef20d060SBarry Smith {
714ef20d060SBarry Smith   PetscErrorCode     ierr;
715ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
716ef20d060SBarry Smith   void               *ctx;
717ef20d060SBarry Smith   DM                 dm;
718ef20d060SBarry Smith 
719ef20d060SBarry Smith   PetscFunctionBegin;
720ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7210910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
722ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
723ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
724ef20d060SBarry Smith 
725ef20d060SBarry Smith   if (solutionfunction) {
7269b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7270910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
728ef20d060SBarry Smith     PetscStackPop;
729ef20d060SBarry Smith   }
730ef20d060SBarry Smith   PetscFunctionReturn(0);
731ef20d060SBarry Smith }
7329b7cd975SBarry Smith /*@
7339b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7349b7cd975SBarry Smith 
735d083f849SBarry Smith    Collective on TS
7369b7cd975SBarry Smith 
7379b7cd975SBarry Smith    Input Parameters:
7389b7cd975SBarry Smith +  ts - the TS context
7399b7cd975SBarry Smith -  t - current time
7409b7cd975SBarry Smith 
7419b7cd975SBarry Smith    Output Parameter:
7429b7cd975SBarry Smith .  U - the function value
7439b7cd975SBarry Smith 
7449b7cd975SBarry Smith    Note:
7459b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7469b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7479b7cd975SBarry Smith 
7489b7cd975SBarry Smith    Level: developer
7499b7cd975SBarry Smith 
7509b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7519b7cd975SBarry Smith @*/
7529b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7539b7cd975SBarry Smith {
7549b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7559b7cd975SBarry Smith   void               *ctx;
7569b7cd975SBarry Smith   DM                 dm;
7579b7cd975SBarry Smith 
7589b7cd975SBarry Smith   PetscFunctionBegin;
7599b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7609b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7619b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7629b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7639b7cd975SBarry Smith 
7649b7cd975SBarry Smith   if (forcing) {
7659b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7669b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7679b7cd975SBarry Smith     PetscStackPop;
7689b7cd975SBarry Smith   }
7699b7cd975SBarry Smith   PetscFunctionReturn(0);
7709b7cd975SBarry Smith }
771ef20d060SBarry Smith 
772214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
773214bc6a2SJed Brown {
7742dd45cf8SJed Brown   Vec            F;
775214bc6a2SJed Brown   PetscErrorCode ierr;
776214bc6a2SJed Brown 
777214bc6a2SJed Brown   PetscFunctionBegin;
7780298fd71SBarry Smith   *Frhs = NULL;
7790298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
780214bc6a2SJed Brown   if (!ts->Frhs) {
7812dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
782214bc6a2SJed Brown   }
783214bc6a2SJed Brown   *Frhs = ts->Frhs;
784214bc6a2SJed Brown   PetscFunctionReturn(0);
785214bc6a2SJed Brown }
786214bc6a2SJed Brown 
787971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
788214bc6a2SJed Brown {
789214bc6a2SJed Brown   Mat            A,B;
7902dd45cf8SJed Brown   PetscErrorCode ierr;
79141a1d4d2SBarry Smith   TSIJacobian    ijacobian;
792214bc6a2SJed Brown 
793214bc6a2SJed Brown   PetscFunctionBegin;
794c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
795c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
79641a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
797214bc6a2SJed Brown   if (Arhs) {
798214bc6a2SJed Brown     if (!ts->Arhs) {
79941a1d4d2SBarry Smith       if (ijacobian) {
800214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
80141a1d4d2SBarry Smith       } else {
80241a1d4d2SBarry Smith         ts->Arhs = A;
80341a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
80441a1d4d2SBarry Smith       }
8053565c898SBarry Smith     } else {
8063565c898SBarry Smith       PetscBool flg;
8073565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8083565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8093565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8103565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8113565c898SBarry Smith         ts->Arhs = A;
8123565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8133565c898SBarry Smith       }
814214bc6a2SJed Brown     }
815214bc6a2SJed Brown     *Arhs = ts->Arhs;
816214bc6a2SJed Brown   }
817214bc6a2SJed Brown   if (Brhs) {
818214bc6a2SJed Brown     if (!ts->Brhs) {
819bdb70873SJed Brown       if (A != B) {
82041a1d4d2SBarry Smith         if (ijacobian) {
821214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
822bdb70873SJed Brown         } else {
82341a1d4d2SBarry Smith           ts->Brhs = B;
82441a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
82541a1d4d2SBarry Smith         }
82641a1d4d2SBarry Smith       } else {
827bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
82851699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
829bdb70873SJed Brown       }
830214bc6a2SJed Brown     }
831214bc6a2SJed Brown     *Brhs = ts->Brhs;
832214bc6a2SJed Brown   }
833214bc6a2SJed Brown   PetscFunctionReturn(0);
834214bc6a2SJed Brown }
835214bc6a2SJed Brown 
836316643e7SJed Brown /*@
8370910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
838316643e7SJed Brown 
839d083f849SBarry Smith    Collective on TS
840316643e7SJed Brown 
841316643e7SJed Brown    Input Parameters:
842316643e7SJed Brown +  ts - the TS context
843316643e7SJed Brown .  t - current time
8440910c330SBarry Smith .  U - state vector
8450910c330SBarry Smith .  Udot - time derivative of state vector
846214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
847316643e7SJed Brown 
848316643e7SJed Brown    Output Parameter:
849316643e7SJed Brown .  Y - right hand side
850316643e7SJed Brown 
851316643e7SJed Brown    Note:
852316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
853316643e7SJed Brown    is used internally within the nonlinear solvers.
854316643e7SJed Brown 
855316643e7SJed Brown    If the user did did not write their equations in implicit form, this
856316643e7SJed Brown    function recasts them in implicit form.
857316643e7SJed Brown 
858316643e7SJed Brown    Level: developer
859316643e7SJed Brown 
860316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
861316643e7SJed Brown @*/
8620910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
863316643e7SJed Brown {
864316643e7SJed Brown   PetscErrorCode ierr;
86524989b8cSPeter Brune   TSIFunction    ifunction;
86624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
86724989b8cSPeter Brune   void           *ctx;
86824989b8cSPeter Brune   DM             dm;
869316643e7SJed Brown 
870316643e7SJed Brown   PetscFunctionBegin;
8710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8720910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8730910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8740700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
875316643e7SJed Brown 
87624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
87724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8780298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
87924989b8cSPeter Brune 
880ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
881d90be118SSean Farley 
8820910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
88324989b8cSPeter Brune   if (ifunction) {
884316643e7SJed Brown     PetscStackPush("TS user implicit function");
8850910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
886316643e7SJed Brown     PetscStackPop;
887214bc6a2SJed Brown   }
888214bc6a2SJed Brown   if (imex) {
88924989b8cSPeter Brune     if (!ifunction) {
8900910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8912dd45cf8SJed Brown     }
89224989b8cSPeter Brune   } else if (rhsfunction) {
89324989b8cSPeter Brune     if (ifunction) {
894214bc6a2SJed Brown       Vec Frhs;
895214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8960910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
897214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8982dd45cf8SJed Brown     } else {
8990910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9000910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
901316643e7SJed Brown     }
9024a6899ffSJed Brown   }
9030910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
904316643e7SJed Brown   PetscFunctionReturn(0);
905316643e7SJed Brown }
906316643e7SJed Brown 
907316643e7SJed Brown /*@
908316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
909316643e7SJed Brown 
910d083f849SBarry Smith    Collective on TS
911316643e7SJed Brown 
912316643e7SJed Brown    Input
913316643e7SJed Brown       Input Parameters:
914316643e7SJed Brown +  ts - the TS context
915316643e7SJed Brown .  t - current timestep
9160910c330SBarry Smith .  U - state vector
9170910c330SBarry Smith .  Udot - time derivative of state vector
918214bc6a2SJed Brown .  shift - shift to apply, see note below
919214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
920316643e7SJed Brown 
921316643e7SJed Brown    Output Parameters:
922316643e7SJed Brown +  A - Jacobian matrix
9233565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
924316643e7SJed Brown 
925316643e7SJed Brown    Notes:
9260910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
927316643e7SJed Brown 
9280910c330SBarry Smith    dF/dU + shift*dF/dUdot
929316643e7SJed Brown 
930316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
931316643e7SJed Brown    is used internally within the nonlinear solvers.
932316643e7SJed Brown 
933316643e7SJed Brown    Level: developer
934316643e7SJed Brown 
935316643e7SJed Brown .seealso:  TSSetIJacobian()
93663495f91SJed Brown @*/
937d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
938316643e7SJed Brown {
939316643e7SJed Brown   PetscErrorCode ierr;
94024989b8cSPeter Brune   TSIJacobian    ijacobian;
94124989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
94224989b8cSPeter Brune   DM             dm;
94324989b8cSPeter Brune   void           *ctx;
944316643e7SJed Brown 
945316643e7SJed Brown   PetscFunctionBegin;
9460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9470910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9480910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
949316643e7SJed Brown   PetscValidPointer(A,6);
95094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
951316643e7SJed Brown   PetscValidPointer(B,7);
95294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
95324989b8cSPeter Brune 
95424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
95524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9560298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
95724989b8cSPeter Brune 
958ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
959316643e7SJed Brown 
96094ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
96124989b8cSPeter Brune   if (ijacobian) {
962316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
963d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
964316643e7SJed Brown     PetscStackPop;
9654a6899ffSJed Brown   }
966214bc6a2SJed Brown   if (imex) {
967b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9684c26be97Sstefano_zampini       PetscBool assembled;
969971015bcSStefano Zampini       if (rhsjacobian) {
970971015bcSStefano Zampini         Mat Arhs = NULL;
971971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
972971015bcSStefano Zampini         if (A == Arhs) {
973971015bcSStefano Zampini           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant");
974971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
975971015bcSStefano Zampini         }
976971015bcSStefano Zampini       }
97794ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9784c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9794c26be97Sstefano_zampini       if (!assembled) {
9804c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9814c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9824c26be97Sstefano_zampini       }
98394ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
98494ab13aaSBarry Smith       if (A != B) {
98594ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9864c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9874c26be97Sstefano_zampini         if (!assembled) {
9884c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9894c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9904c26be97Sstefano_zampini         }
99194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
992214bc6a2SJed Brown       }
993214bc6a2SJed Brown     }
994214bc6a2SJed Brown   } else {
995e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
996e1244c69SJed Brown     if (rhsjacobian) {
997214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
998d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
999e1244c69SJed Brown     }
100094ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10013565c898SBarry Smith       PetscBool flg;
1002e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1003e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10043565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10053565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10063565c898SBarry Smith       if (!flg) {
100794ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
100894ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10093565c898SBarry Smith       }
101094ab13aaSBarry Smith       if (A != B) {
101194ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
101294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1013316643e7SJed Brown       }
1014e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1015e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1016e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
101794ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
101894ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
101994ab13aaSBarry Smith         if (A != B) {
102094ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
102194ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1022214bc6a2SJed Brown         }
1023316643e7SJed Brown       }
102494ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
102594ab13aaSBarry Smith       if (A != B) {
102694ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1027316643e7SJed Brown       }
1028316643e7SJed Brown     }
1029316643e7SJed Brown   }
103094ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1031316643e7SJed Brown   PetscFunctionReturn(0);
1032316643e7SJed Brown }
1033316643e7SJed Brown 
1034d763cef2SBarry Smith /*@C
1035d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1036b5abc632SBarry Smith     where U_t = G(t,u).
1037d763cef2SBarry Smith 
10383f9fe445SBarry Smith     Logically Collective on TS
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith     Input Parameters:
1041d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10420298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1043d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1044d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10450298fd71SBarry Smith           function evaluation routine (may be NULL)
1046d763cef2SBarry Smith 
1047*a96d6ef6SBarry Smith     Calling sequence of f:
1048*a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1049d763cef2SBarry Smith 
1050*a96d6ef6SBarry Smith +   ts - timestep context
1051*a96d6ef6SBarry Smith .   t - current timestep
1052d763cef2SBarry Smith .   u - input vector
1053d763cef2SBarry Smith .   F - function vector
1054d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1055d763cef2SBarry Smith 
1056d763cef2SBarry Smith     Level: beginner
1057d763cef2SBarry Smith 
105895452b02SPatrick Sanan     Notes:
105995452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10602bbac0d3SBarry Smith 
1061ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1062d763cef2SBarry Smith @*/
1063089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1064d763cef2SBarry Smith {
1065089b2837SJed Brown   PetscErrorCode ierr;
1066089b2837SJed Brown   SNES           snes;
10670298fd71SBarry Smith   Vec            ralloc = NULL;
106824989b8cSPeter Brune   DM             dm;
1069d763cef2SBarry Smith 
1070089b2837SJed Brown   PetscFunctionBegin;
10710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1072ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
107324989b8cSPeter Brune 
107424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
107524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1076089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1077e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1078e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1079e856ceecSJed Brown     r = ralloc;
1080e856ceecSJed Brown   }
1081089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1082e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1083d763cef2SBarry Smith   PetscFunctionReturn(0);
1084d763cef2SBarry Smith }
1085d763cef2SBarry Smith 
1086ef20d060SBarry Smith /*@C
1087abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1088ef20d060SBarry Smith 
1089ef20d060SBarry Smith     Logically Collective on TS
1090ef20d060SBarry Smith 
1091ef20d060SBarry Smith     Input Parameters:
1092ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1093ef20d060SBarry Smith .   f - routine for evaluating the solution
1094ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10950298fd71SBarry Smith           function evaluation routine (may be NULL)
1096ef20d060SBarry Smith 
1097*a96d6ef6SBarry Smith     Calling sequence of f:
1098*a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1099ef20d060SBarry Smith 
1100ef20d060SBarry Smith +   t - current timestep
1101ef20d060SBarry Smith .   u - output vector
1102ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1103ef20d060SBarry Smith 
11040ed3bfb6SBarry Smith     Options Database:
11050ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11060ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11070ed3bfb6SBarry Smith 
1108abd5a294SJed Brown     Notes:
1109abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1110abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1111abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1112abd5a294SJed Brown 
11134f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1114abd5a294SJed Brown 
1115ef20d060SBarry Smith     Level: beginner
1116ef20d060SBarry Smith 
11170ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1118ef20d060SBarry Smith @*/
1119ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1120ef20d060SBarry Smith {
1121ef20d060SBarry Smith   PetscErrorCode ierr;
1122ef20d060SBarry Smith   DM             dm;
1123ef20d060SBarry Smith 
1124ef20d060SBarry Smith   PetscFunctionBegin;
1125ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1126ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1127ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1128ef20d060SBarry Smith   PetscFunctionReturn(0);
1129ef20d060SBarry Smith }
1130ef20d060SBarry Smith 
11319b7cd975SBarry Smith /*@C
11329b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11339b7cd975SBarry Smith 
11349b7cd975SBarry Smith     Logically Collective on TS
11359b7cd975SBarry Smith 
11369b7cd975SBarry Smith     Input Parameters:
11379b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1138e162b725SBarry Smith .   func - routine for evaluating the forcing function
11399b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11400298fd71SBarry Smith           function evaluation routine (may be NULL)
11419b7cd975SBarry Smith 
11429b7cd975SBarry Smith     Calling sequence of func:
11436bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11449b7cd975SBarry Smith 
11459b7cd975SBarry Smith +   t - current timestep
1146e162b725SBarry Smith .   f - output vector
11479b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11489b7cd975SBarry Smith 
11499b7cd975SBarry Smith     Notes:
11509b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1151e162b725SBarry Smith     create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the
1152e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1153e162b725SBarry Smith 
1154e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1155e162b725SBarry Smith 
1156e162b725SBarry Smith     This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the
1157e162b725SBarry Smith     parameters can be passed in the ctx variable.
11589b7cd975SBarry Smith 
11599b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11609b7cd975SBarry Smith 
11619b7cd975SBarry Smith     Level: beginner
11629b7cd975SBarry Smith 
11639b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11649b7cd975SBarry Smith @*/
1165e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11669b7cd975SBarry Smith {
11679b7cd975SBarry Smith   PetscErrorCode ierr;
11689b7cd975SBarry Smith   DM             dm;
11699b7cd975SBarry Smith 
11709b7cd975SBarry Smith   PetscFunctionBegin;
11719b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11729b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1173e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11749b7cd975SBarry Smith   PetscFunctionReturn(0);
11759b7cd975SBarry Smith }
11769b7cd975SBarry Smith 
1177d763cef2SBarry Smith /*@C
1178f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1179b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1180d763cef2SBarry Smith 
11813f9fe445SBarry Smith    Logically Collective on TS
1182d763cef2SBarry Smith 
1183d763cef2SBarry Smith    Input Parameters:
1184d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1185e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1186e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1187d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1188d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11890298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1190d763cef2SBarry Smith 
1191f7ab8db6SBarry Smith    Calling sequence of f:
1192*a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1193d763cef2SBarry Smith 
1194d763cef2SBarry Smith +  t - current timestep
1195d763cef2SBarry Smith .  u - input vector
1196e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1197e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1198d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1199d763cef2SBarry Smith 
12006cd88445SBarry Smith    Notes:
12016cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12026cd88445SBarry Smith 
12036cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1204ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1205d763cef2SBarry Smith 
1206d763cef2SBarry Smith    Level: beginner
1207d763cef2SBarry Smith 
1208ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1209d763cef2SBarry Smith 
1210d763cef2SBarry Smith @*/
1211e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1212d763cef2SBarry Smith {
1213277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1214089b2837SJed Brown   SNES           snes;
121524989b8cSPeter Brune   DM             dm;
121624989b8cSPeter Brune   TSIJacobian    ijacobian;
1217277b19d0SLisandro Dalcin 
1218d763cef2SBarry Smith   PetscFunctionBegin;
12190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1220e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1221e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1222e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1223e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1224d763cef2SBarry Smith 
122524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122624989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1227e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1228e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1229e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1230e1244c69SJed Brown   }
12310298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1232089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12335f659677SPeter Brune   if (!ijacobian) {
1234e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12350e4ef248SJed Brown   }
1236e5d3d808SBarry Smith   if (Amat) {
1237e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12380e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1239e5d3d808SBarry Smith     ts->Arhs = Amat;
12400e4ef248SJed Brown   }
1241e5d3d808SBarry Smith   if (Pmat) {
1242e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12430e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1244e5d3d808SBarry Smith     ts->Brhs = Pmat;
12450e4ef248SJed Brown   }
1246d763cef2SBarry Smith   PetscFunctionReturn(0);
1247d763cef2SBarry Smith }
1248d763cef2SBarry Smith 
1249316643e7SJed Brown /*@C
1250b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1251316643e7SJed Brown 
12523f9fe445SBarry Smith    Logically Collective on TS
1253316643e7SJed Brown 
1254316643e7SJed Brown    Input Parameters:
1255316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12560298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1257316643e7SJed Brown .  f   - the function evaluation routine
12580298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1259316643e7SJed Brown 
1260316643e7SJed Brown    Calling sequence of f:
12616bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1262316643e7SJed Brown 
1263316643e7SJed Brown +  t   - time at step/stage being solved
1264316643e7SJed Brown .  u   - state vector
1265316643e7SJed Brown .  u_t - time derivative of state vector
1266316643e7SJed Brown .  F   - function vector
1267316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1268316643e7SJed Brown 
1269316643e7SJed Brown    Important:
12702bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1271316643e7SJed Brown 
1272316643e7SJed Brown    Level: beginner
1273316643e7SJed Brown 
1274d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1275316643e7SJed Brown @*/
127651699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1277316643e7SJed Brown {
1278089b2837SJed Brown   PetscErrorCode ierr;
1279089b2837SJed Brown   SNES           snes;
128051699248SLisandro Dalcin   Vec            ralloc = NULL;
128124989b8cSPeter Brune   DM             dm;
1282316643e7SJed Brown 
1283316643e7SJed Brown   PetscFunctionBegin;
12840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
128551699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
128624989b8cSPeter Brune 
128724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
128824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
128924989b8cSPeter Brune 
1290089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
129151699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
129251699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
129351699248SLisandro Dalcin     r  = ralloc;
1294e856ceecSJed Brown   }
129551699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
129651699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1297089b2837SJed Brown   PetscFunctionReturn(0);
1298089b2837SJed Brown }
1299089b2837SJed Brown 
1300089b2837SJed Brown /*@C
1301089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1302089b2837SJed Brown 
1303089b2837SJed Brown    Not Collective
1304089b2837SJed Brown 
1305089b2837SJed Brown    Input Parameter:
1306089b2837SJed Brown .  ts - the TS context
1307089b2837SJed Brown 
1308089b2837SJed Brown    Output Parameter:
13090298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13100298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13110298fd71SBarry Smith -  ctx - the function context (or NULL)
1312089b2837SJed Brown 
1313089b2837SJed Brown    Level: advanced
1314089b2837SJed Brown 
1315089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1316089b2837SJed Brown @*/
1317089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1318089b2837SJed Brown {
1319089b2837SJed Brown   PetscErrorCode ierr;
1320089b2837SJed Brown   SNES           snes;
132124989b8cSPeter Brune   DM             dm;
1322089b2837SJed Brown 
1323089b2837SJed Brown   PetscFunctionBegin;
1324089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1325089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13260298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
132724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
132824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1329089b2837SJed Brown   PetscFunctionReturn(0);
1330089b2837SJed Brown }
1331089b2837SJed Brown 
1332089b2837SJed Brown /*@C
1333089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1334089b2837SJed Brown 
1335089b2837SJed Brown    Not Collective
1336089b2837SJed Brown 
1337089b2837SJed Brown    Input Parameter:
1338089b2837SJed Brown .  ts - the TS context
1339089b2837SJed Brown 
1340089b2837SJed Brown    Output Parameter:
13410298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13420298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13430298fd71SBarry Smith -  ctx - the function context (or NULL)
1344089b2837SJed Brown 
1345089b2837SJed Brown    Level: advanced
1346089b2837SJed Brown 
13472bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1348089b2837SJed Brown @*/
1349089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1350089b2837SJed Brown {
1351089b2837SJed Brown   PetscErrorCode ierr;
1352089b2837SJed Brown   SNES           snes;
135324989b8cSPeter Brune   DM             dm;
1354089b2837SJed Brown 
1355089b2837SJed Brown   PetscFunctionBegin;
1356089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1357089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13580298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
135924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
136024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1361316643e7SJed Brown   PetscFunctionReturn(0);
1362316643e7SJed Brown }
1363316643e7SJed Brown 
1364316643e7SJed Brown /*@C
1365a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1366ae8867d6SBarry Smith         provided with TSSetIFunction().
1367316643e7SJed Brown 
13683f9fe445SBarry Smith    Logically Collective on TS
1369316643e7SJed Brown 
1370316643e7SJed Brown    Input Parameters:
1371316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1372e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1373e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1374316643e7SJed Brown .  f   - the Jacobian evaluation routine
13750298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1376316643e7SJed Brown 
1377316643e7SJed Brown    Calling sequence of f:
13786bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1379316643e7SJed Brown 
1380316643e7SJed Brown +  t    - time at step/stage being solved
13811b4a444bSJed Brown .  U    - state vector
13821b4a444bSJed Brown .  U_t  - time derivative of state vector
1383316643e7SJed Brown .  a    - shift
1384e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1385e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1386316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1387316643e7SJed Brown 
1388316643e7SJed Brown    Notes:
1389e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1390316643e7SJed Brown 
1391895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1392895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1393895c21f2SBarry Smith 
1394a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1395b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1396a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1397a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1398a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1399a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1400a4f0a591SBarry Smith 
14016cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14026cd88445SBarry Smith 
14036cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1404ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1405ca5f011dSBarry Smith 
1406316643e7SJed Brown    Level: beginner
1407316643e7SJed Brown 
1408ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1409316643e7SJed Brown 
1410316643e7SJed Brown @*/
1411e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1412316643e7SJed Brown {
1413316643e7SJed Brown   PetscErrorCode ierr;
1414089b2837SJed Brown   SNES           snes;
141524989b8cSPeter Brune   DM             dm;
1416316643e7SJed Brown 
1417316643e7SJed Brown   PetscFunctionBegin;
14180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1419e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1420e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1421e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1422e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
142324989b8cSPeter Brune 
142424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
142524989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
142624989b8cSPeter Brune 
1427089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1428e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1429316643e7SJed Brown   PetscFunctionReturn(0);
1430316643e7SJed Brown }
1431316643e7SJed Brown 
1432e1244c69SJed Brown /*@
1433e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1434e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1435e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1436e1244c69SJed Brown    not been changed by the TS.
1437e1244c69SJed Brown 
1438e1244c69SJed Brown    Logically Collective
1439e1244c69SJed Brown 
1440e1244c69SJed Brown    Input Arguments:
1441e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1442e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1443e1244c69SJed Brown 
1444e1244c69SJed Brown    Level: intermediate
1445e1244c69SJed Brown 
1446e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1447e1244c69SJed Brown @*/
1448e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1449e1244c69SJed Brown {
1450e1244c69SJed Brown   PetscFunctionBegin;
1451e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1452e1244c69SJed Brown   PetscFunctionReturn(0);
1453e1244c69SJed Brown }
1454e1244c69SJed Brown 
1455efe9872eSLisandro Dalcin /*@C
1456efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1457efe9872eSLisandro Dalcin 
1458efe9872eSLisandro Dalcin    Logically Collective on TS
1459efe9872eSLisandro Dalcin 
1460efe9872eSLisandro Dalcin    Input Parameters:
1461efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1462efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1463efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1464efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1465efe9872eSLisandro Dalcin 
1466efe9872eSLisandro Dalcin    Calling sequence of fun:
14676bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1468efe9872eSLisandro Dalcin 
1469efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1470efe9872eSLisandro Dalcin .  U    - state vector
1471efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1472efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1473efe9872eSLisandro Dalcin .  F    - function vector
1474efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1475efe9872eSLisandro Dalcin 
1476efe9872eSLisandro Dalcin    Level: beginner
1477efe9872eSLisandro Dalcin 
1478*a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1479efe9872eSLisandro Dalcin @*/
1480efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1481efe9872eSLisandro Dalcin {
1482efe9872eSLisandro Dalcin   DM             dm;
1483efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1484efe9872eSLisandro Dalcin 
1485efe9872eSLisandro Dalcin   PetscFunctionBegin;
1486efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1487efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1488efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1489efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1490efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1491efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1492efe9872eSLisandro Dalcin }
1493efe9872eSLisandro Dalcin 
1494efe9872eSLisandro Dalcin /*@C
1495efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1496efe9872eSLisandro Dalcin 
1497efe9872eSLisandro Dalcin   Not Collective
1498efe9872eSLisandro Dalcin 
1499efe9872eSLisandro Dalcin   Input Parameter:
1500efe9872eSLisandro Dalcin . ts - the TS context
1501efe9872eSLisandro Dalcin 
1502efe9872eSLisandro Dalcin   Output Parameter:
1503efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1504efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1505efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1506efe9872eSLisandro Dalcin 
1507efe9872eSLisandro Dalcin   Level: advanced
1508efe9872eSLisandro Dalcin 
1509*a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1510efe9872eSLisandro Dalcin @*/
1511efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1512efe9872eSLisandro Dalcin {
1513efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1514efe9872eSLisandro Dalcin   SNES           snes;
1515efe9872eSLisandro Dalcin   DM             dm;
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin   PetscFunctionBegin;
1518efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1519efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1520efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1521efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1522efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1523efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1524efe9872eSLisandro Dalcin }
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin /*@C
1527bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1528efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin    Logically Collective on TS
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin    Input Parameters:
1533efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1534efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1535efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1536efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1537efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin    Calling sequence of jac:
15406bc98fa9SBarry Smith $    PetscErrorCode jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1541efe9872eSLisandro Dalcin 
1542efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1543efe9872eSLisandro Dalcin .  U    - state vector
1544efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1545efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1546efe9872eSLisandro Dalcin .  v    - shift for U_t
1547efe9872eSLisandro Dalcin .  a    - shift for U_tt
1548efe9872eSLisandro Dalcin .  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
1549efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1550efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1551efe9872eSLisandro Dalcin 
1552efe9872eSLisandro Dalcin    Notes:
1553efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1554efe9872eSLisandro Dalcin 
1555efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1556efe9872eSLisandro Dalcin    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.
1557efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1558bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1559efe9872eSLisandro Dalcin 
1560efe9872eSLisandro Dalcin    Level: beginner
1561efe9872eSLisandro Dalcin 
1562*a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1563efe9872eSLisandro Dalcin @*/
1564efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1565efe9872eSLisandro Dalcin {
1566efe9872eSLisandro Dalcin   DM             dm;
1567efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1568efe9872eSLisandro Dalcin 
1569efe9872eSLisandro Dalcin   PetscFunctionBegin;
1570efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1571efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1572efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1573efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1574efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1575efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1576efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1577efe9872eSLisandro Dalcin }
1578efe9872eSLisandro Dalcin 
1579efe9872eSLisandro Dalcin /*@C
1580efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1581efe9872eSLisandro Dalcin 
1582efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1583efe9872eSLisandro Dalcin 
1584efe9872eSLisandro Dalcin   Input Parameter:
1585efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin   Output Parameters:
1588efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1589efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1590efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1591efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1592efe9872eSLisandro Dalcin 
159395452b02SPatrick Sanan   Notes:
159495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   Level: advanced
1597efe9872eSLisandro Dalcin 
1598*a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1599efe9872eSLisandro Dalcin 
1600efe9872eSLisandro Dalcin @*/
1601efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1602efe9872eSLisandro Dalcin {
1603efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1604efe9872eSLisandro Dalcin   SNES           snes;
1605efe9872eSLisandro Dalcin   DM             dm;
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   PetscFunctionBegin;
1608efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1609efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1610efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1611efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1612efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1613efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1614efe9872eSLisandro Dalcin }
1615efe9872eSLisandro Dalcin 
1616efe9872eSLisandro Dalcin /*@
1617efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1618efe9872eSLisandro Dalcin 
1619d083f849SBarry Smith   Collective on TS
1620efe9872eSLisandro Dalcin 
1621efe9872eSLisandro Dalcin   Input Parameters:
1622efe9872eSLisandro Dalcin + ts - the TS context
1623efe9872eSLisandro Dalcin . t - current time
1624efe9872eSLisandro Dalcin . U - state vector
1625efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1626efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1627efe9872eSLisandro Dalcin 
1628efe9872eSLisandro Dalcin   Output Parameter:
1629efe9872eSLisandro Dalcin . F - the residual vector
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin   Note:
1632efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1633efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1634efe9872eSLisandro Dalcin 
1635efe9872eSLisandro Dalcin   Level: developer
1636efe9872eSLisandro Dalcin 
1637*a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1638efe9872eSLisandro Dalcin @*/
1639efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1640efe9872eSLisandro Dalcin {
1641efe9872eSLisandro Dalcin   DM             dm;
1642efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1643efe9872eSLisandro Dalcin   void           *ctx;
1644efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1645efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1646efe9872eSLisandro Dalcin 
1647efe9872eSLisandro Dalcin   PetscFunctionBegin;
1648efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1649efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1650efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1651efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1652efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1653efe9872eSLisandro Dalcin 
1654efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1655efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1656efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin   if (!I2Function) {
1659efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1660efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1661efe9872eSLisandro Dalcin   }
1662efe9872eSLisandro Dalcin 
1663efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1664efe9872eSLisandro Dalcin 
1665efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1666efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1667efe9872eSLisandro Dalcin   PetscStackPop;
1668efe9872eSLisandro Dalcin 
1669efe9872eSLisandro Dalcin   if (rhsfunction) {
1670efe9872eSLisandro Dalcin     Vec Frhs;
1671efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1672efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1673efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1674efe9872eSLisandro Dalcin   }
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1677efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1678efe9872eSLisandro Dalcin }
1679efe9872eSLisandro Dalcin 
1680efe9872eSLisandro Dalcin /*@
1681efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1682efe9872eSLisandro Dalcin 
1683d083f849SBarry Smith   Collective on TS
1684efe9872eSLisandro Dalcin 
1685efe9872eSLisandro Dalcin   Input Parameters:
1686efe9872eSLisandro Dalcin + ts - the TS context
1687efe9872eSLisandro Dalcin . t - current timestep
1688efe9872eSLisandro Dalcin . U - state vector
1689efe9872eSLisandro Dalcin . V - time derivative of state vector
1690efe9872eSLisandro Dalcin . A - second time derivative of state vector
1691efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1692efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin   Output Parameters:
1695efe9872eSLisandro Dalcin + J - Jacobian matrix
1696efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1697efe9872eSLisandro Dalcin 
1698efe9872eSLisandro Dalcin   Notes:
1699efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1700efe9872eSLisandro Dalcin 
1701efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1702efe9872eSLisandro Dalcin 
1703efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1704efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1705efe9872eSLisandro Dalcin 
1706efe9872eSLisandro Dalcin   Level: developer
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1709efe9872eSLisandro Dalcin @*/
1710efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1711efe9872eSLisandro Dalcin {
1712efe9872eSLisandro Dalcin   DM             dm;
1713efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1714efe9872eSLisandro Dalcin   void           *ctx;
1715efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1716efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   PetscFunctionBegin;
1719efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1720efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1721efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1722efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1723efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1724efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1727efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1728efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1729efe9872eSLisandro Dalcin 
1730efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1731efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1732efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1733efe9872eSLisandro Dalcin   }
1734efe9872eSLisandro Dalcin 
1735efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1736efe9872eSLisandro Dalcin 
1737efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1738efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1739efe9872eSLisandro Dalcin   PetscStackPop;
1740efe9872eSLisandro Dalcin 
1741efe9872eSLisandro Dalcin   if (rhsjacobian) {
1742efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1743efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1744efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1745efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1746efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1747efe9872eSLisandro Dalcin   }
1748efe9872eSLisandro Dalcin 
1749efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1750efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1751efe9872eSLisandro Dalcin }
1752efe9872eSLisandro Dalcin 
1753438f35afSJed Brown /*@C
1754438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1755438f35afSJed Brown 
1756438f35afSJed Brown    Logically Collective
1757438f35afSJed Brown 
1758438f35afSJed Brown    Input Arguments:
1759438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1760*a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1761438f35afSJed Brown -  ctx - a context for tvar
1762438f35afSJed Brown 
1763*a96d6ef6SBarry Smith     Calling sequence of tvar:
1764*a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1765*a96d6ef6SBarry Smith 
1766*a96d6ef6SBarry Smith +   ts - timestep context
1767*a96d6ef6SBarry Smith .   p - input vector (primative form)
1768*a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1769*a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1770*a96d6ef6SBarry Smith 
1771438f35afSJed Brown    Level: advanced
1772438f35afSJed Brown 
1773438f35afSJed Brown    Notes:
1774438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1775438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1776438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1777438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1778438f35afSJed Brown    evaluated via the chain rule, as in
1779438f35afSJed Brown 
1780438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1781438f35afSJed Brown 
1782438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1783438f35afSJed Brown @*/
1784438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1785438f35afSJed Brown {
1786438f35afSJed Brown   PetscErrorCode ierr;
1787438f35afSJed Brown   DM             dm;
1788438f35afSJed Brown 
1789438f35afSJed Brown   PetscFunctionBegin;
1790438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1791438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1792438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1793438f35afSJed Brown   PetscFunctionReturn(0);
1794438f35afSJed Brown }
1795438f35afSJed Brown 
1796efe9872eSLisandro Dalcin /*@
1797e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1798e3c11fc1SJed Brown 
1799e3c11fc1SJed Brown    Logically Collective
1800e3c11fc1SJed Brown 
1801e3c11fc1SJed Brown    Input Parameters:
1802e3c11fc1SJed Brown +  ts - TS on which to compute
1803e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1804e3c11fc1SJed Brown 
1805e3c11fc1SJed Brown    Output Parameters:
1806e3c11fc1SJed Brown .  C - transient (conservative) variable
1807e3c11fc1SJed Brown 
1808e3c11fc1SJed Brown    Developer Notes:
1809e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1810e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1811e3c11fc1SJed Brown    being used.
1812e3c11fc1SJed Brown 
1813e3c11fc1SJed Brown    Level: developer
1814e3c11fc1SJed Brown 
1815e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1816e3c11fc1SJed Brown @*/
1817e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1818e3c11fc1SJed Brown {
1819e3c11fc1SJed Brown   PetscErrorCode ierr;
1820e3c11fc1SJed Brown   DM             dm;
1821e3c11fc1SJed Brown   DMTS           dmts;
1822e3c11fc1SJed Brown 
1823e3c11fc1SJed Brown   PetscFunctionBegin;
1824e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1825e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1826e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1827e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1828e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1829e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1830e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1831e3c11fc1SJed Brown   }
1832e3c11fc1SJed Brown   PetscFunctionReturn(0);
1833e3c11fc1SJed Brown }
1834e3c11fc1SJed Brown 
1835e3c11fc1SJed Brown /*@
1836e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1837e3c11fc1SJed Brown 
1838e3c11fc1SJed Brown    Logically Collective
1839e3c11fc1SJed Brown 
1840e3c11fc1SJed Brown    Input Parameters:
1841e3c11fc1SJed Brown .  ts - TS on which to compute
1842e3c11fc1SJed Brown 
1843e3c11fc1SJed Brown    Output Parameters:
1844e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1845e3c11fc1SJed Brown 
1846e3c11fc1SJed Brown    Level: developer
1847e3c11fc1SJed Brown 
1848e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1849e3c11fc1SJed Brown @*/
1850e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1851e3c11fc1SJed Brown {
1852e3c11fc1SJed Brown   PetscErrorCode ierr;
1853e3c11fc1SJed Brown   DM             dm;
1854e3c11fc1SJed Brown   DMTS           dmts;
1855e3c11fc1SJed Brown 
1856e3c11fc1SJed Brown   PetscFunctionBegin;
1857e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1858e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1859e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1860e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1861e3c11fc1SJed Brown   PetscFunctionReturn(0);
1862e3c11fc1SJed Brown }
1863e3c11fc1SJed Brown 
1864e3c11fc1SJed Brown /*@
1865efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1866efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1867efe9872eSLisandro Dalcin 
1868d083f849SBarry Smith    Logically Collective on TS
1869efe9872eSLisandro Dalcin 
1870efe9872eSLisandro Dalcin    Input Parameters:
1871efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1872efe9872eSLisandro Dalcin .  u - the solution vector
1873efe9872eSLisandro Dalcin -  v - the time derivative vector
1874efe9872eSLisandro Dalcin 
1875efe9872eSLisandro Dalcin    Level: beginner
1876efe9872eSLisandro Dalcin 
1877efe9872eSLisandro Dalcin @*/
1878efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1879efe9872eSLisandro Dalcin {
1880efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1881efe9872eSLisandro Dalcin 
1882efe9872eSLisandro Dalcin   PetscFunctionBegin;
1883efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1884efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1885efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1886efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1887efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1888efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1889efe9872eSLisandro Dalcin   ts->vec_dot = v;
1890efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1891efe9872eSLisandro Dalcin }
1892efe9872eSLisandro Dalcin 
1893efe9872eSLisandro Dalcin /*@
1894efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1895efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1896efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1897efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1898efe9872eSLisandro Dalcin 
1899efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1900efe9872eSLisandro Dalcin 
1901efe9872eSLisandro Dalcin    Input Parameter:
1902efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1903efe9872eSLisandro Dalcin 
1904efe9872eSLisandro Dalcin    Output Parameter:
1905efe9872eSLisandro Dalcin +  u - the vector containing the solution
1906efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1907efe9872eSLisandro Dalcin 
1908efe9872eSLisandro Dalcin    Level: intermediate
1909efe9872eSLisandro Dalcin 
1910efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1911efe9872eSLisandro Dalcin 
1912efe9872eSLisandro Dalcin @*/
1913efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1914efe9872eSLisandro Dalcin {
1915efe9872eSLisandro Dalcin   PetscFunctionBegin;
1916efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1917efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1918efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1919efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1920efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1921efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1922efe9872eSLisandro Dalcin }
1923efe9872eSLisandro Dalcin 
192455849f57SBarry Smith /*@C
192555849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
192655849f57SBarry Smith 
192755849f57SBarry Smith   Collective on PetscViewer
192855849f57SBarry Smith 
192955849f57SBarry Smith   Input Parameters:
193055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
193155849f57SBarry Smith            some related function before a call to TSLoad().
193255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
193355849f57SBarry Smith 
193455849f57SBarry Smith    Level: intermediate
193555849f57SBarry Smith 
193655849f57SBarry Smith   Notes:
193755849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
193855849f57SBarry Smith 
193955849f57SBarry Smith   Notes for advanced users:
194055849f57SBarry Smith   Most users should not need to know the details of the binary storage
194155849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
194255849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
194355849f57SBarry Smith   format is
194455849f57SBarry Smith .vb
194555849f57SBarry Smith      has not yet been determined
194655849f57SBarry Smith .ve
194755849f57SBarry Smith 
194855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
194955849f57SBarry Smith @*/
1950f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
195155849f57SBarry Smith {
195255849f57SBarry Smith   PetscErrorCode ierr;
195355849f57SBarry Smith   PetscBool      isbinary;
195455849f57SBarry Smith   PetscInt       classid;
195555849f57SBarry Smith   char           type[256];
19562d53ad75SBarry Smith   DMTS           sdm;
1957ad6bc421SBarry Smith   DM             dm;
195855849f57SBarry Smith 
195955849f57SBarry Smith   PetscFunctionBegin;
1960f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
196155849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
196255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
196355849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
196455849f57SBarry Smith 
1965060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1966ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1967060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1968f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1969f2c2a1b9SBarry Smith   if (ts->ops->load) {
1970f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1971f2c2a1b9SBarry Smith   }
1972ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1973ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1974ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1975f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1976f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19772d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19782d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
197955849f57SBarry Smith   PetscFunctionReturn(0);
198055849f57SBarry Smith }
198155849f57SBarry Smith 
19829804daf3SBarry Smith #include <petscdraw.h>
1983e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1984e04113cfSBarry Smith #include <petscviewersaws.h>
1985f05ece33SBarry Smith #endif
1986fe2efc57SMark 
1987fe2efc57SMark /*@C
1988fe2efc57SMark    TSViewFromOptions - View from Options
1989fe2efc57SMark 
1990fe2efc57SMark    Collective on TS
1991fe2efc57SMark 
1992fe2efc57SMark    Input Parameters:
1993fe2efc57SMark +  A - the application ordering context
1994736c3998SJose E. Roman .  obj - Optional object
1995736c3998SJose E. Roman -  name - command line option
1996fe2efc57SMark 
1997fe2efc57SMark    Level: intermediate
1998fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
1999fe2efc57SMark @*/
2000fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
2001fe2efc57SMark {
2002fe2efc57SMark   PetscErrorCode ierr;
2003fe2efc57SMark 
2004fe2efc57SMark   PetscFunctionBegin;
2005fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
2006fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
2007fe2efc57SMark   PetscFunctionReturn(0);
2008fe2efc57SMark }
2009fe2efc57SMark 
20107e2c5f70SBarry Smith /*@C
2011d763cef2SBarry Smith     TSView - Prints the TS data structure.
2012d763cef2SBarry Smith 
20134c49b128SBarry Smith     Collective on TS
2014d763cef2SBarry Smith 
2015d763cef2SBarry Smith     Input Parameters:
2016d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2017d763cef2SBarry Smith -   viewer - visualization context
2018d763cef2SBarry Smith 
2019d763cef2SBarry Smith     Options Database Key:
2020d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2021d763cef2SBarry Smith 
2022d763cef2SBarry Smith     Notes:
2023d763cef2SBarry Smith     The available visualization contexts include
2024b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2025b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2026d763cef2SBarry Smith          output where only the first processor opens
2027d763cef2SBarry Smith          the file.  All other processors send their
2028d763cef2SBarry Smith          data to the first processor to print.
2029d763cef2SBarry Smith 
2030d763cef2SBarry Smith     The user can open an alternative visualization context with
2031b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith     Level: beginner
2034d763cef2SBarry Smith 
2035b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2036d763cef2SBarry Smith @*/
20377087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2038d763cef2SBarry Smith {
2039dfbe8321SBarry Smith   PetscErrorCode ierr;
204019fd82e9SBarry Smith   TSType         type;
20412b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20422d53ad75SBarry Smith   DMTS           sdm;
2043e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2044536b137fSBarry Smith   PetscBool      issaws;
2045f05ece33SBarry Smith #endif
2046d763cef2SBarry Smith 
2047d763cef2SBarry Smith   PetscFunctionBegin;
20480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20493050cee2SBarry Smith   if (!viewer) {
2050ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20513050cee2SBarry Smith   }
20520700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2053c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2054fd16b177SBarry Smith 
2055251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2056251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
205755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20582b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2059e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2060536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2061f05ece33SBarry Smith #endif
206232077d6dSBarry Smith   if (iascii) {
2063dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2064efd4aadfSBarry Smith     if (ts->ops->view) {
2065efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2066efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2067efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2068efd4aadfSBarry Smith     }
2069ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
207077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2071ef85077eSLisandro Dalcin     }
2072ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20737c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2074ef85077eSLisandro Dalcin     }
2075efd4aadfSBarry Smith     if (ts->usessnes) {
2076efd4aadfSBarry Smith       PetscBool lin;
2077d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20785ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2079d763cef2SBarry Smith       }
20805ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
20811ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2082efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2083efd4aadfSBarry Smith     }
2084193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2085a0af407cSBarry Smith     if (ts->vrtol) {
2086a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2087a0af407cSBarry Smith     } else {
2088a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2089a0af407cSBarry Smith     }
2090a0af407cSBarry Smith     if (ts->vatol) {
2091a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2092a0af407cSBarry Smith     } else {
2093a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2094a0af407cSBarry Smith     }
2095825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2096efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2097825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
20980f5bd95cSBarry Smith   } else if (isstring) {
2099a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
210036a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
210136a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
210255849f57SBarry Smith   } else if (isbinary) {
210355849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
210455849f57SBarry Smith     MPI_Comm    comm;
210555849f57SBarry Smith     PetscMPIInt rank;
210655849f57SBarry Smith     char        type[256];
210755849f57SBarry Smith 
210855849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
210955849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
211055849f57SBarry Smith     if (!rank) {
2111f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
211255849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2113f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
211455849f57SBarry Smith     }
211555849f57SBarry Smith     if (ts->ops->view) {
211655849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
211755849f57SBarry Smith     }
2118efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2119f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2120f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21212d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21222d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21232b0a91c0SBarry Smith   } else if (isdraw) {
21242b0a91c0SBarry Smith     PetscDraw draw;
21252b0a91c0SBarry Smith     char      str[36];
212689fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21272b0a91c0SBarry Smith 
21282b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21292b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21302b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21312b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
213251fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
213389fd9fafSBarry Smith     bottom = y - h;
21342b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21352b0a91c0SBarry Smith     if (ts->ops->view) {
21362b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21372b0a91c0SBarry Smith     }
2138efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2139efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21402b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2141e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2142536b137fSBarry Smith   } else if (issaws) {
2143d45a07a7SBarry Smith     PetscMPIInt rank;
21442657e9d9SBarry Smith     const char  *name;
21452657e9d9SBarry Smith 
21462657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2147d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2148d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2149d45a07a7SBarry Smith       char       dir[1024];
2150d45a07a7SBarry Smith 
2151e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2152a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21532657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21542657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21552657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2156d763cef2SBarry Smith     }
21570acecf5bSBarry Smith     if (ts->ops->view) {
21580acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21590acecf5bSBarry Smith     }
2160f05ece33SBarry Smith #endif
2161f05ece33SBarry Smith   }
216236a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
216336a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
216436a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
216536a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
216636a9e3b9SBarry Smith   }
216736a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
216836a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2169f05ece33SBarry Smith 
2170b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2171251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2172b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2173d763cef2SBarry Smith   PetscFunctionReturn(0);
2174d763cef2SBarry Smith }
2175d763cef2SBarry Smith 
2176b07ff414SBarry Smith /*@
2177d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2178d763cef2SBarry Smith    the timesteppers.
2179d763cef2SBarry Smith 
21803f9fe445SBarry Smith    Logically Collective on TS
2181d763cef2SBarry Smith 
2182d763cef2SBarry Smith    Input Parameters:
2183d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2184d763cef2SBarry Smith -  usrP - optional user context
2185d763cef2SBarry Smith 
218695452b02SPatrick Sanan    Fortran Notes:
218795452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2188daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2189daf670e6SBarry Smith 
2190d763cef2SBarry Smith    Level: intermediate
2191d763cef2SBarry Smith 
2192d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2193d763cef2SBarry Smith @*/
21947087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2195d763cef2SBarry Smith {
2196d763cef2SBarry Smith   PetscFunctionBegin;
21970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2198d763cef2SBarry Smith   ts->user = usrP;
2199d763cef2SBarry Smith   PetscFunctionReturn(0);
2200d763cef2SBarry Smith }
2201d763cef2SBarry Smith 
2202b07ff414SBarry Smith /*@
2203d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2204d763cef2SBarry Smith     timestepper.
2205d763cef2SBarry Smith 
2206d763cef2SBarry Smith     Not Collective
2207d763cef2SBarry Smith 
2208d763cef2SBarry Smith     Input Parameter:
2209d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2210d763cef2SBarry Smith 
2211d763cef2SBarry Smith     Output Parameter:
2212d763cef2SBarry Smith .   usrP - user context
2213d763cef2SBarry Smith 
221495452b02SPatrick Sanan    Fortran Notes:
221595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2216daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2217daf670e6SBarry Smith 
2218d763cef2SBarry Smith     Level: intermediate
2219d763cef2SBarry Smith 
2220d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2221d763cef2SBarry Smith @*/
2222e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2223d763cef2SBarry Smith {
2224d763cef2SBarry Smith   PetscFunctionBegin;
22250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2226e71120c6SJed Brown   *(void**)usrP = ts->user;
2227d763cef2SBarry Smith   PetscFunctionReturn(0);
2228d763cef2SBarry Smith }
2229d763cef2SBarry Smith 
2230d763cef2SBarry Smith /*@
223180275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2232d763cef2SBarry Smith 
2233d763cef2SBarry Smith    Not Collective
2234d763cef2SBarry Smith 
2235d763cef2SBarry Smith    Input Parameter:
2236d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2237d763cef2SBarry Smith 
2238d763cef2SBarry Smith    Output Parameter:
223980275a0aSLisandro Dalcin .  steps - number of steps completed so far
2240d763cef2SBarry Smith 
2241d763cef2SBarry Smith    Level: intermediate
2242d763cef2SBarry Smith 
22439be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2244d763cef2SBarry Smith @*/
224580275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2246d763cef2SBarry Smith {
2247d763cef2SBarry Smith   PetscFunctionBegin;
22480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224980275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
225080275a0aSLisandro Dalcin   *steps = ts->steps;
225180275a0aSLisandro Dalcin   PetscFunctionReturn(0);
225280275a0aSLisandro Dalcin }
225380275a0aSLisandro Dalcin 
225480275a0aSLisandro Dalcin /*@
225580275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
225680275a0aSLisandro Dalcin 
225780275a0aSLisandro Dalcin    Logically Collective on TS
225880275a0aSLisandro Dalcin 
225980275a0aSLisandro Dalcin    Input Parameters:
226080275a0aSLisandro Dalcin +  ts - the TS context
226180275a0aSLisandro Dalcin -  steps - number of steps completed so far
226280275a0aSLisandro Dalcin 
226380275a0aSLisandro Dalcin    Notes:
226480275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
226580275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
226680275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
226780275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
226880275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
226980275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
227080275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
227180275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
227280275a0aSLisandro Dalcin 
227380275a0aSLisandro Dalcin    Level: advanced
227480275a0aSLisandro Dalcin 
227580275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
227680275a0aSLisandro Dalcin @*/
227780275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
227880275a0aSLisandro Dalcin {
227980275a0aSLisandro Dalcin   PetscFunctionBegin;
228080275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
228180275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
228280275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
228380275a0aSLisandro Dalcin   ts->steps = steps;
2284d763cef2SBarry Smith   PetscFunctionReturn(0);
2285d763cef2SBarry Smith }
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith /*@
2288d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2289d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2290d763cef2SBarry Smith 
22913f9fe445SBarry Smith    Logically Collective on TS
2292d763cef2SBarry Smith 
2293d763cef2SBarry Smith    Input Parameters:
2294d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2295d763cef2SBarry Smith -  time_step - the size of the timestep
2296d763cef2SBarry Smith 
2297d763cef2SBarry Smith    Level: intermediate
2298d763cef2SBarry Smith 
2299aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2300d763cef2SBarry Smith 
2301d763cef2SBarry Smith @*/
23027087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2303d763cef2SBarry Smith {
2304d763cef2SBarry Smith   PetscFunctionBegin;
23050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2306c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2307d763cef2SBarry Smith   ts->time_step = time_step;
2308d763cef2SBarry Smith   PetscFunctionReturn(0);
2309d763cef2SBarry Smith }
2310d763cef2SBarry Smith 
2311a43b19c4SJed Brown /*@
231249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
231349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
231449354f04SShri Abhyankar      or just return at the final time TS computed.
2315a43b19c4SJed Brown 
2316a43b19c4SJed Brown   Logically Collective on TS
2317a43b19c4SJed Brown 
2318a43b19c4SJed Brown    Input Parameter:
2319a43b19c4SJed Brown +   ts - the time-step context
232049354f04SShri Abhyankar -   eftopt - exact final time option
2321a43b19c4SJed Brown 
2322feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2323feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2324feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2325feed9e9dSBarry Smith 
2326feed9e9dSBarry Smith    Options Database:
2327feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2328feed9e9dSBarry Smith 
2329ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2330ee346746SBarry Smith     then the final time you selected.
2331ee346746SBarry Smith 
2332a43b19c4SJed Brown    Level: beginner
2333a43b19c4SJed Brown 
2334f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2335a43b19c4SJed Brown @*/
233649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2337a43b19c4SJed Brown {
2338a43b19c4SJed Brown   PetscFunctionBegin;
2339a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
234049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
234149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2342a43b19c4SJed Brown   PetscFunctionReturn(0);
2343a43b19c4SJed Brown }
2344a43b19c4SJed Brown 
2345d763cef2SBarry Smith /*@
2346f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2347f6953c82SLisandro Dalcin 
2348f6953c82SLisandro Dalcin    Not Collective
2349f6953c82SLisandro Dalcin 
2350f6953c82SLisandro Dalcin    Input Parameter:
2351f6953c82SLisandro Dalcin .  ts - the TS context
2352f6953c82SLisandro Dalcin 
2353f6953c82SLisandro Dalcin    Output Parameter:
2354f6953c82SLisandro Dalcin .  eftopt - exact final time option
2355f6953c82SLisandro Dalcin 
2356f6953c82SLisandro Dalcin    Level: beginner
2357f6953c82SLisandro Dalcin 
2358f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2359f6953c82SLisandro Dalcin @*/
2360f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2361f6953c82SLisandro Dalcin {
2362f6953c82SLisandro Dalcin   PetscFunctionBegin;
2363f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2364f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2365f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2366f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2367f6953c82SLisandro Dalcin }
2368f6953c82SLisandro Dalcin 
2369f6953c82SLisandro Dalcin /*@
2370d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2371d763cef2SBarry Smith 
2372d763cef2SBarry Smith    Not Collective
2373d763cef2SBarry Smith 
2374d763cef2SBarry Smith    Input Parameter:
2375d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2376d763cef2SBarry Smith 
2377d763cef2SBarry Smith    Output Parameter:
2378d763cef2SBarry Smith .  dt - the current timestep size
2379d763cef2SBarry Smith 
2380d763cef2SBarry Smith    Level: intermediate
2381d763cef2SBarry Smith 
2382aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2383d763cef2SBarry Smith 
2384d763cef2SBarry Smith @*/
23857087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2386d763cef2SBarry Smith {
2387d763cef2SBarry Smith   PetscFunctionBegin;
23880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2389f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2390d763cef2SBarry Smith   *dt = ts->time_step;
2391d763cef2SBarry Smith   PetscFunctionReturn(0);
2392d763cef2SBarry Smith }
2393d763cef2SBarry Smith 
2394d8e5e3e6SSatish Balay /*@
2395d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2396d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2397d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2398d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2399d763cef2SBarry Smith 
2400d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2401d763cef2SBarry Smith 
2402d763cef2SBarry Smith    Input Parameter:
2403d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2404d763cef2SBarry Smith 
2405d763cef2SBarry Smith    Output Parameter:
2406d763cef2SBarry Smith .  v - the vector containing the solution
2407d763cef2SBarry Smith 
240863e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
240963e21af5SBarry Smith    final time. It returns the solution at the next timestep.
241063e21af5SBarry Smith 
2411d763cef2SBarry Smith    Level: intermediate
2412d763cef2SBarry Smith 
24130ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith @*/
24167087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2417d763cef2SBarry Smith {
2418d763cef2SBarry Smith   PetscFunctionBegin;
24190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24204482741eSBarry Smith   PetscValidPointer(v,2);
24218737fe31SLisandro Dalcin   *v = ts->vec_sol;
2422d763cef2SBarry Smith   PetscFunctionReturn(0);
2423d763cef2SBarry Smith }
2424d763cef2SBarry Smith 
242503fe5f5eSDebojyoti Ghosh /*@
2426b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
242703fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2428b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
242903fe5f5eSDebojyoti Ghosh    structure as the solution vector.
243003fe5f5eSDebojyoti Ghosh 
243103fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
243203fe5f5eSDebojyoti Ghosh 
243303fe5f5eSDebojyoti Ghosh    Parameters :
2434a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2435b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2436b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
243703fe5f5eSDebojyoti Ghosh        returned in v.
2438a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
243903fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2440b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
244103fe5f5eSDebojyoti Ghosh 
24424cdd57e5SDebojyoti Ghosh    Level: advanced
244303fe5f5eSDebojyoti Ghosh 
244403fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
244503fe5f5eSDebojyoti Ghosh 
244603fe5f5eSDebojyoti Ghosh @*/
2447b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
244803fe5f5eSDebojyoti Ghosh {
244903fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
245003fe5f5eSDebojyoti Ghosh 
245103fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
245203fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2453b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
245403fe5f5eSDebojyoti Ghosh   else {
2455b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
245603fe5f5eSDebojyoti Ghosh   }
245703fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
245803fe5f5eSDebojyoti Ghosh }
245903fe5f5eSDebojyoti Ghosh 
24604cdd57e5SDebojyoti Ghosh /*@
24614cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24624cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24634cdd57e5SDebojyoti Ghosh 
24644cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24654cdd57e5SDebojyoti Ghosh 
24664cdd57e5SDebojyoti Ghosh    Parameters :
2467a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2468a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24694cdd57e5SDebojyoti Ghosh 
24704cdd57e5SDebojyoti Ghosh    Level: intermediate
24714cdd57e5SDebojyoti Ghosh 
24724cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24734cdd57e5SDebojyoti Ghosh 
24744cdd57e5SDebojyoti Ghosh @*/
24754cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24764cdd57e5SDebojyoti Ghosh {
24774cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24784cdd57e5SDebojyoti Ghosh 
24794cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24804cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2481f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24824cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2483f6356ec7SDebojyoti Ghosh   } else {
2484f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2485f6356ec7SDebojyoti Ghosh   }
24864cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24874cdd57e5SDebojyoti Ghosh }
24884cdd57e5SDebojyoti Ghosh 
24894cdd57e5SDebojyoti Ghosh /*@
24904cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24914cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24924cdd57e5SDebojyoti Ghosh 
24934cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24944cdd57e5SDebojyoti Ghosh 
24959657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24969657682dSDebojyoti Ghosh 
24974cdd57e5SDebojyoti Ghosh    Parameters :
2498a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2499657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2500a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
25014cdd57e5SDebojyoti Ghosh 
25024cdd57e5SDebojyoti Ghosh    Level: intermediate
25034cdd57e5SDebojyoti Ghosh 
250457df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25054cdd57e5SDebojyoti Ghosh 
25064cdd57e5SDebojyoti Ghosh @*/
25070a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25084cdd57e5SDebojyoti Ghosh {
25094cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25104cdd57e5SDebojyoti Ghosh 
25114cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25124cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2513f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25140a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2515f6356ec7SDebojyoti Ghosh   } else {
2516f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2517f6356ec7SDebojyoti Ghosh   }
25184cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25194cdd57e5SDebojyoti Ghosh }
25204cdd57e5SDebojyoti Ghosh 
252157df6a1bSDebojyoti Ghosh /*@
252257df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
252357df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
252457df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
252557df6a1bSDebojyoti Ghosh 
252657df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
252757df6a1bSDebojyoti Ghosh 
252857df6a1bSDebojyoti Ghosh    Parameters :
2529a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2530a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
253157df6a1bSDebojyoti Ghosh 
253257df6a1bSDebojyoti Ghosh    Level: intermediate
253357df6a1bSDebojyoti Ghosh 
253457df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
253557df6a1bSDebojyoti Ghosh 
253657df6a1bSDebojyoti Ghosh @*/
253757df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
253857df6a1bSDebojyoti Ghosh {
253957df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
254057df6a1bSDebojyoti Ghosh 
254157df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
254257df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25439657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
254457df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
254557df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
254657df6a1bSDebojyoti Ghosh   }
254757df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
254857df6a1bSDebojyoti Ghosh }
254957df6a1bSDebojyoti Ghosh 
2550bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2551d8e5e3e6SSatish Balay /*@
2552bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2553d763cef2SBarry Smith 
2554bdad233fSMatthew Knepley   Not collective
2555d763cef2SBarry Smith 
2556bdad233fSMatthew Knepley   Input Parameters:
2557bdad233fSMatthew Knepley + ts   - The TS
2558bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2559d763cef2SBarry Smith .vb
25600910c330SBarry Smith          U_t - A U = 0      (linear)
25610910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25620910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2563d763cef2SBarry Smith .ve
2564d763cef2SBarry Smith 
2565d763cef2SBarry Smith    Level: beginner
2566d763cef2SBarry Smith 
2567bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2568d763cef2SBarry Smith @*/
25697087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2570a7cc72afSBarry Smith {
25719e2a6581SJed Brown   PetscErrorCode ierr;
25729e2a6581SJed Brown 
2573d763cef2SBarry Smith   PetscFunctionBegin;
25740700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2575bdad233fSMatthew Knepley   ts->problem_type = type;
25769e2a6581SJed Brown   if (type == TS_LINEAR) {
25779e2a6581SJed Brown     SNES snes;
25789e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25799e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25809e2a6581SJed Brown   }
2581d763cef2SBarry Smith   PetscFunctionReturn(0);
2582d763cef2SBarry Smith }
2583d763cef2SBarry Smith 
2584bdad233fSMatthew Knepley /*@C
2585bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2586bdad233fSMatthew Knepley 
2587bdad233fSMatthew Knepley   Not collective
2588bdad233fSMatthew Knepley 
2589bdad233fSMatthew Knepley   Input Parameter:
2590bdad233fSMatthew Knepley . ts   - The TS
2591bdad233fSMatthew Knepley 
2592bdad233fSMatthew Knepley   Output Parameter:
2593bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2594bdad233fSMatthew Knepley .vb
2595089b2837SJed Brown          M U_t = A U
2596089b2837SJed Brown          M(t) U_t = A(t) U
2597b5abc632SBarry Smith          F(t,U,U_t)
2598bdad233fSMatthew Knepley .ve
2599bdad233fSMatthew Knepley 
2600bdad233fSMatthew Knepley    Level: beginner
2601bdad233fSMatthew Knepley 
2602bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2603bdad233fSMatthew Knepley @*/
26047087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2605a7cc72afSBarry Smith {
2606bdad233fSMatthew Knepley   PetscFunctionBegin;
26070700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26084482741eSBarry Smith   PetscValidIntPointer(type,2);
2609bdad233fSMatthew Knepley   *type = ts->problem_type;
2610bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2611bdad233fSMatthew Knepley }
2612d763cef2SBarry Smith 
2613d763cef2SBarry Smith /*@
2614d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2615d763cef2SBarry Smith    of a timestepper.
2616d763cef2SBarry Smith 
2617d763cef2SBarry Smith    Collective on TS
2618d763cef2SBarry Smith 
2619d763cef2SBarry Smith    Input Parameter:
2620d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2621d763cef2SBarry Smith 
2622d763cef2SBarry Smith    Notes:
2623d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2624d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2625141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2626d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2627141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2628d763cef2SBarry Smith 
2629d763cef2SBarry Smith    Level: advanced
2630d763cef2SBarry Smith 
2631141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2632d763cef2SBarry Smith @*/
26337087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2634d763cef2SBarry Smith {
2635dfbe8321SBarry Smith   PetscErrorCode ierr;
26366c6b9e74SPeter Brune   DM             dm;
26376c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2638d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2639cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26406c6b9e74SPeter Brune   TSIJacobian    ijac;
2641efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26426c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
26432ffb9264SLisandro Dalcin   PetscBool      isnone;
2644d763cef2SBarry Smith 
2645d763cef2SBarry Smith   PetscFunctionBegin;
26460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2647277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2648277b19d0SLisandro Dalcin 
26497adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2650cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2651cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2652d763cef2SBarry Smith   }
2653277b19d0SLisandro Dalcin 
26541a638600SStefano Zampini   if (!ts->vec_sol) {
26551a638600SStefano Zampini     if (ts->dm) {
26561a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
26571a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
26581a638600SStefano Zampini   }
2659277b19d0SLisandro Dalcin 
2660298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2661298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2662298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2663298bade4SHong Zhang   }
2664298bade4SHong Zhang 
2665cd4cee2dSHong Zhang   if (ts->quadraturets) {
2666cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2667ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2668cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2669cd4cee2dSHong Zhang   }
2670cd4cee2dSHong Zhang 
2671971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2672971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2673e1244c69SJed Brown     Mat Amat,Pmat;
2674e1244c69SJed Brown     SNES snes;
2675e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2676e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2677e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2678e1244c69SJed Brown      * have displaced the RHS matrix */
2679971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2680abc0d4abSBarry Smith       /* 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 */
2681abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2682e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2683e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2684e1244c69SJed Brown     }
2685971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2686abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2687e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2688e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2689e1244c69SJed Brown     }
2690e1244c69SJed Brown   }
26912ffb9264SLisandro Dalcin 
26922ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
26932ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
26942ffb9264SLisandro Dalcin 
2695277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2696000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2697277b19d0SLisandro Dalcin   }
2698277b19d0SLisandro Dalcin 
26992ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
27002ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
27012ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
27022ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
27032ffb9264SLisandro Dalcin 
2704a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27056c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27066c6b9e74SPeter Brune    */
27076c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27080298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27096c6b9e74SPeter Brune   if (!func) {
27106c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27116c6b9e74SPeter Brune   }
2712a6772fa2SLisandro Dalcin   /* If the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
27136c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27146c6b9e74SPeter Brune    */
27150298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27160298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2717efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27180298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2719efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27206c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27216c6b9e74SPeter Brune   }
2722c0517034SDebojyoti Ghosh 
2723c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2724c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2725c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2726c0517034SDebojyoti Ghosh   }
2727c0517034SDebojyoti Ghosh 
2728277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2729277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2730277b19d0SLisandro Dalcin }
2731277b19d0SLisandro Dalcin 
2732f6a906c0SBarry Smith /*@
2733277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2734277b19d0SLisandro Dalcin 
2735277b19d0SLisandro Dalcin    Collective on TS
2736277b19d0SLisandro Dalcin 
2737277b19d0SLisandro Dalcin    Input Parameter:
2738277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2739277b19d0SLisandro Dalcin 
2740277b19d0SLisandro Dalcin    Level: beginner
2741277b19d0SLisandro Dalcin 
2742277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2743277b19d0SLisandro Dalcin @*/
2744277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2745277b19d0SLisandro Dalcin {
27461d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2747277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2748277b19d0SLisandro Dalcin 
2749277b19d0SLisandro Dalcin   PetscFunctionBegin;
2750277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2751b18ea86cSHong Zhang 
2752277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2753277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2754277b19d0SLisandro Dalcin   }
2755277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2756e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2757bbd56ea5SKarl Rupp 
27584e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27594e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2760214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27616bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2762efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2763e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2764e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
276538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2766bbd56ea5SKarl Rupp 
2767cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2768ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2769ecf68647SHong Zhang   if (ts->forward_solve) {
2770ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2771ecf68647SHong Zhang   }
2772cd4cee2dSHong Zhang   if (ts->quadraturets) {
2773cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
277436eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2775cd4cee2dSHong Zhang   }
27761d06f6b3SHong Zhang   while (ilink) {
27771d06f6b3SHong Zhang     next = ilink->next;
27781d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
27791d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
27801d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
27811d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
27821d06f6b3SHong Zhang     ilink = next;
278387f4e208SHong Zhang   }
2784545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2785277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2786d763cef2SBarry Smith   PetscFunctionReturn(0);
2787d763cef2SBarry Smith }
2788d763cef2SBarry Smith 
2789d8e5e3e6SSatish Balay /*@
2790d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2791d763cef2SBarry Smith    with TSCreate().
2792d763cef2SBarry Smith 
2793d763cef2SBarry Smith    Collective on TS
2794d763cef2SBarry Smith 
2795d763cef2SBarry Smith    Input Parameter:
2796d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2797d763cef2SBarry Smith 
2798d763cef2SBarry Smith    Level: beginner
2799d763cef2SBarry Smith 
2800d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2801d763cef2SBarry Smith @*/
28026bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2803d763cef2SBarry Smith {
28046849ba73SBarry Smith   PetscErrorCode ierr;
2805d763cef2SBarry Smith 
2806d763cef2SBarry Smith   PetscFunctionBegin;
28076bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2808ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2809c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2810d763cef2SBarry Smith 
2811ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2812ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2813ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2814ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2815ecf68647SHong Zhang   }
2816e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2817e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28186bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28196d4c513bSLisandro Dalcin 
2820bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2821bc952696SBarry Smith 
282284df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28236427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28246427ac75SLisandro Dalcin 
28256bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28266bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28276bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28280dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28296d4c513bSLisandro Dalcin 
2830cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2831a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2832d763cef2SBarry Smith   PetscFunctionReturn(0);
2833d763cef2SBarry Smith }
2834d763cef2SBarry Smith 
2835d8e5e3e6SSatish Balay /*@
2836d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2837d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2838d763cef2SBarry Smith 
2839d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2840d763cef2SBarry Smith 
2841d763cef2SBarry Smith    Input Parameter:
2842d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2843d763cef2SBarry Smith 
2844d763cef2SBarry Smith    Output Parameter:
2845d763cef2SBarry Smith .  snes - the nonlinear solver context
2846d763cef2SBarry Smith 
2847d763cef2SBarry Smith    Notes:
2848d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2849d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
285094b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2851d763cef2SBarry Smith 
2852d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28530298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2854d763cef2SBarry Smith 
2855d763cef2SBarry Smith    Level: beginner
2856d763cef2SBarry Smith 
2857d763cef2SBarry Smith @*/
28587087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2859d763cef2SBarry Smith {
2860d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2861d372ba47SLisandro Dalcin 
2862d763cef2SBarry Smith   PetscFunctionBegin;
28630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28644482741eSBarry Smith   PetscValidPointer(snes,2);
2865d372ba47SLisandro Dalcin   if (!ts->snes) {
2866ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
286716413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
28680298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28693bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2870d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2871496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28729e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28739e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
28749e2a6581SJed Brown     }
2875d372ba47SLisandro Dalcin   }
2876d763cef2SBarry Smith   *snes = ts->snes;
2877d763cef2SBarry Smith   PetscFunctionReturn(0);
2878d763cef2SBarry Smith }
2879d763cef2SBarry Smith 
2880deb2cd25SJed Brown /*@
2881deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2882deb2cd25SJed Brown 
2883deb2cd25SJed Brown    Collective
2884deb2cd25SJed Brown 
2885deb2cd25SJed Brown    Input Parameter:
2886deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2887deb2cd25SJed Brown -  snes - the nonlinear solver context
2888deb2cd25SJed Brown 
2889deb2cd25SJed Brown    Notes:
2890deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2891deb2cd25SJed Brown 
2892deb2cd25SJed Brown    Level: developer
2893deb2cd25SJed Brown 
2894deb2cd25SJed Brown @*/
2895deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2896deb2cd25SJed Brown {
2897deb2cd25SJed Brown   PetscErrorCode ierr;
2898d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2899deb2cd25SJed Brown 
2900deb2cd25SJed Brown   PetscFunctionBegin;
2901deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2902deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2903deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2904deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2905bbd56ea5SKarl Rupp 
2906deb2cd25SJed Brown   ts->snes = snes;
2907bbd56ea5SKarl Rupp 
29080298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29090298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2910740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29110298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2912740132f1SEmil Constantinescu   }
2913deb2cd25SJed Brown   PetscFunctionReturn(0);
2914deb2cd25SJed Brown }
2915deb2cd25SJed Brown 
2916d8e5e3e6SSatish Balay /*@
291794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2918d763cef2SBarry Smith    a TS (timestepper) context.
2919d763cef2SBarry Smith 
292094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2921d763cef2SBarry Smith 
2922d763cef2SBarry Smith    Input Parameter:
2923d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2924d763cef2SBarry Smith 
2925d763cef2SBarry Smith    Output Parameter:
292694b7f48cSBarry Smith .  ksp - the nonlinear solver context
2927d763cef2SBarry Smith 
2928d763cef2SBarry Smith    Notes:
292994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2930d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2931d763cef2SBarry Smith    KSP and PC contexts as well.
2932d763cef2SBarry Smith 
293394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29340298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2935d763cef2SBarry Smith 
2936d763cef2SBarry Smith    Level: beginner
2937d763cef2SBarry Smith 
2938d763cef2SBarry Smith @*/
29397087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2940d763cef2SBarry Smith {
2941d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2942089b2837SJed Brown   SNES           snes;
2943d372ba47SLisandro Dalcin 
2944d763cef2SBarry Smith   PetscFunctionBegin;
29450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29464482741eSBarry Smith   PetscValidPointer(ksp,2);
294717186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2948e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2949089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2950089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2951d763cef2SBarry Smith   PetscFunctionReturn(0);
2952d763cef2SBarry Smith }
2953d763cef2SBarry Smith 
2954d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2955d763cef2SBarry Smith 
2956adb62b0dSMatthew Knepley /*@
2957618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2958618ce8baSLisandro Dalcin 
2959618ce8baSLisandro Dalcin    Logically Collective on TS
2960618ce8baSLisandro Dalcin 
2961618ce8baSLisandro Dalcin    Input Parameters:
2962618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2963618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2964618ce8baSLisandro Dalcin 
2965618ce8baSLisandro Dalcin    Options Database Keys:
2966618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2967618ce8baSLisandro Dalcin 
2968618ce8baSLisandro Dalcin    Notes:
2969618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2970618ce8baSLisandro Dalcin 
2971618ce8baSLisandro Dalcin    Level: intermediate
2972618ce8baSLisandro Dalcin 
2973618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2974618ce8baSLisandro Dalcin @*/
2975618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2976618ce8baSLisandro Dalcin {
2977618ce8baSLisandro Dalcin   PetscFunctionBegin;
2978618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2979618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2980618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2981618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2982618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2983618ce8baSLisandro Dalcin }
2984618ce8baSLisandro Dalcin 
2985618ce8baSLisandro Dalcin /*@
2986618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2987618ce8baSLisandro Dalcin 
2988618ce8baSLisandro Dalcin    Not Collective
2989618ce8baSLisandro Dalcin 
2990618ce8baSLisandro Dalcin    Input Parameters:
2991618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2992618ce8baSLisandro Dalcin 
2993618ce8baSLisandro Dalcin    Output Parameter:
2994618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2995618ce8baSLisandro Dalcin 
2996618ce8baSLisandro Dalcin    Level: advanced
2997618ce8baSLisandro Dalcin 
2998618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2999618ce8baSLisandro Dalcin @*/
3000618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3001618ce8baSLisandro Dalcin {
3002618ce8baSLisandro Dalcin   PetscFunctionBegin;
3003618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3004618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3005618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3006618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3007618ce8baSLisandro Dalcin }
3008618ce8baSLisandro Dalcin 
3009618ce8baSLisandro Dalcin /*@
3010618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3011618ce8baSLisandro Dalcin 
3012618ce8baSLisandro Dalcin    Logically Collective on TS
3013618ce8baSLisandro Dalcin 
3014618ce8baSLisandro Dalcin    Input Parameters:
3015618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3016618ce8baSLisandro Dalcin -  maxtime - final time to step to
3017618ce8baSLisandro Dalcin 
3018618ce8baSLisandro Dalcin    Options Database Keys:
3019ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3020618ce8baSLisandro Dalcin 
3021618ce8baSLisandro Dalcin    Notes:
3022618ce8baSLisandro Dalcin    The default maximum time is 5.0
3023618ce8baSLisandro Dalcin 
3024618ce8baSLisandro Dalcin    Level: intermediate
3025618ce8baSLisandro Dalcin 
3026618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3027618ce8baSLisandro Dalcin @*/
3028618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3029618ce8baSLisandro Dalcin {
3030618ce8baSLisandro Dalcin   PetscFunctionBegin;
3031618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3032618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3033618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3034618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3035618ce8baSLisandro Dalcin }
3036618ce8baSLisandro Dalcin 
3037618ce8baSLisandro Dalcin /*@
3038618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3039618ce8baSLisandro Dalcin 
3040618ce8baSLisandro Dalcin    Not Collective
3041618ce8baSLisandro Dalcin 
3042618ce8baSLisandro Dalcin    Input Parameters:
3043618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3044618ce8baSLisandro Dalcin 
3045618ce8baSLisandro Dalcin    Output Parameter:
3046618ce8baSLisandro Dalcin .  maxtime - final time to step to
3047618ce8baSLisandro Dalcin 
3048618ce8baSLisandro Dalcin    Level: advanced
3049618ce8baSLisandro Dalcin 
3050618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3051618ce8baSLisandro Dalcin @*/
3052618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3053618ce8baSLisandro Dalcin {
3054618ce8baSLisandro Dalcin   PetscFunctionBegin;
3055618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3056618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3057618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3058618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3059618ce8baSLisandro Dalcin }
3060618ce8baSLisandro Dalcin 
3061618ce8baSLisandro Dalcin /*@
3062aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3063edc382c3SSatish Balay 
3064edc382c3SSatish Balay    Level: deprecated
3065edc382c3SSatish Balay 
3066aaa6c58dSLisandro Dalcin @*/
3067aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3068aaa6c58dSLisandro Dalcin {
3069aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3070aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3071aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3072aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3073aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3074aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3075aaa6c58dSLisandro Dalcin }
3076aaa6c58dSLisandro Dalcin 
3077aaa6c58dSLisandro Dalcin /*@
307819eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3079edc382c3SSatish Balay 
3080edc382c3SSatish Balay    Level: deprecated
3081edc382c3SSatish Balay 
3082adb62b0dSMatthew Knepley @*/
30837087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3084adb62b0dSMatthew Knepley {
3085adb62b0dSMatthew Knepley   PetscFunctionBegin;
30860700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3087abc0a331SBarry Smith   if (maxsteps) {
30884482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3089adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3090adb62b0dSMatthew Knepley   }
3091abc0a331SBarry Smith   if (maxtime) {
30924482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3093adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3094adb62b0dSMatthew Knepley   }
3095adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3096adb62b0dSMatthew Knepley }
3097adb62b0dSMatthew Knepley 
3098d763cef2SBarry Smith /*@
309919eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3100edc382c3SSatish Balay 
3101edc382c3SSatish Balay    Level: deprecated
3102edc382c3SSatish Balay 
3103d763cef2SBarry Smith @*/
31047087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3105d763cef2SBarry Smith {
3106d763cef2SBarry Smith   PetscFunctionBegin;
31070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3108c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3109c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
311039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
311139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3112d763cef2SBarry Smith   PetscFunctionReturn(0);
3113d763cef2SBarry Smith }
3114d763cef2SBarry Smith 
3115d763cef2SBarry Smith /*@
31165c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3117edc382c3SSatish Balay 
3118edc382c3SSatish Balay    Level: deprecated
3119edc382c3SSatish Balay 
31205c5f5948SLisandro Dalcin @*/
3121e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31225c5f5948SLisandro Dalcin 
312319eac22cSLisandro Dalcin /*@
31244f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3125edc382c3SSatish Balay 
3126edc382c3SSatish Balay    Level: deprecated
3127edc382c3SSatish Balay 
31284f4e0956SLisandro Dalcin @*/
31294f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31304f4e0956SLisandro Dalcin 
31314f4e0956SLisandro Dalcin /*@
3132d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3133d763cef2SBarry Smith    for use by the TS routines.
3134d763cef2SBarry Smith 
3135d083f849SBarry Smith    Logically Collective on TS
3136d763cef2SBarry Smith 
3137d763cef2SBarry Smith    Input Parameters:
3138d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31390910c330SBarry Smith -  u - the solution vector
3140d763cef2SBarry Smith 
3141d763cef2SBarry Smith    Level: beginner
3142d763cef2SBarry Smith 
31430ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3144d763cef2SBarry Smith @*/
31450910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3146d763cef2SBarry Smith {
31478737fe31SLisandro Dalcin   PetscErrorCode ierr;
3148496e6a7aSJed Brown   DM             dm;
31498737fe31SLisandro Dalcin 
3150d763cef2SBarry Smith   PetscFunctionBegin;
31510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31520910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31530910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31546bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31550910c330SBarry Smith   ts->vec_sol = u;
3156bbd56ea5SKarl Rupp 
3157496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
31580910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3159d763cef2SBarry Smith   PetscFunctionReturn(0);
3160d763cef2SBarry Smith }
3161d763cef2SBarry Smith 
3162ac226902SBarry Smith /*@C
3163000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31643f2090d5SJed Brown   called once at the beginning of each time step.
3165000e7ae3SMatthew Knepley 
31663f9fe445SBarry Smith   Logically Collective on TS
3167000e7ae3SMatthew Knepley 
3168000e7ae3SMatthew Knepley   Input Parameters:
3169000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3170000e7ae3SMatthew Knepley - func - The function
3171000e7ae3SMatthew Knepley 
3172000e7ae3SMatthew Knepley   Calling sequence of func:
31736bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3174000e7ae3SMatthew Knepley 
3175000e7ae3SMatthew Knepley   Level: intermediate
3176000e7ae3SMatthew Knepley 
3177dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3178000e7ae3SMatthew Knepley @*/
31797087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3180000e7ae3SMatthew Knepley {
3181000e7ae3SMatthew Knepley   PetscFunctionBegin;
31820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3183ae60f76fSBarry Smith   ts->prestep = func;
3184000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3185000e7ae3SMatthew Knepley }
3186000e7ae3SMatthew Knepley 
318709ee8438SJed Brown /*@
31883f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31893f2090d5SJed Brown 
31903f2090d5SJed Brown   Collective on TS
31913f2090d5SJed Brown 
31923f2090d5SJed Brown   Input Parameters:
31933f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
31943f2090d5SJed Brown 
31953f2090d5SJed Brown   Notes:
31963f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
31973f2090d5SJed Brown   so most users would not generally call this routine themselves.
31983f2090d5SJed Brown 
31993f2090d5SJed Brown   Level: developer
32003f2090d5SJed Brown 
32019be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32023f2090d5SJed Brown @*/
32037087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32043f2090d5SJed Brown {
32053f2090d5SJed Brown   PetscErrorCode ierr;
32063f2090d5SJed Brown 
32073f2090d5SJed Brown   PetscFunctionBegin;
32080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3209ae60f76fSBarry Smith   if (ts->prestep) {
32105efd42a4SStefano Zampini     Vec              U;
32115efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32125efd42a4SStefano Zampini 
32135efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32145efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3215ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32165efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3217dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3218312ce896SJed Brown   }
32193f2090d5SJed Brown   PetscFunctionReturn(0);
32203f2090d5SJed Brown }
32213f2090d5SJed Brown 
3222b8123daeSJed Brown /*@C
3223b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3224b8123daeSJed Brown   called once at the beginning of each stage.
3225b8123daeSJed Brown 
3226b8123daeSJed Brown   Logically Collective on TS
3227b8123daeSJed Brown 
3228b8123daeSJed Brown   Input Parameters:
3229b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3230b8123daeSJed Brown - func - The function
3231b8123daeSJed Brown 
3232b8123daeSJed Brown   Calling sequence of func:
3233b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3234b8123daeSJed Brown 
3235b8123daeSJed Brown   Level: intermediate
3236b8123daeSJed Brown 
3237b8123daeSJed Brown   Note:
3238b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
323980275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3240b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3241b8123daeSJed Brown 
32429be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3243b8123daeSJed Brown @*/
3244b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3245b8123daeSJed Brown {
3246b8123daeSJed Brown   PetscFunctionBegin;
3247b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3248ae60f76fSBarry Smith   ts->prestage = func;
3249b8123daeSJed Brown   PetscFunctionReturn(0);
3250b8123daeSJed Brown }
3251b8123daeSJed Brown 
32529be3e283SDebojyoti Ghosh /*@C
32539be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32549be3e283SDebojyoti Ghosh   called once at the end of each stage.
32559be3e283SDebojyoti Ghosh 
32569be3e283SDebojyoti Ghosh   Logically Collective on TS
32579be3e283SDebojyoti Ghosh 
32589be3e283SDebojyoti Ghosh   Input Parameters:
32599be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32609be3e283SDebojyoti Ghosh - func - The function
32619be3e283SDebojyoti Ghosh 
32629be3e283SDebojyoti Ghosh   Calling sequence of func:
32639be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32649be3e283SDebojyoti Ghosh 
32659be3e283SDebojyoti Ghosh   Level: intermediate
32669be3e283SDebojyoti Ghosh 
32679be3e283SDebojyoti Ghosh   Note:
32689be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
326980275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32709be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32719be3e283SDebojyoti Ghosh 
32729be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32739be3e283SDebojyoti Ghosh @*/
32749be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32759be3e283SDebojyoti Ghosh {
32769be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32779be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32789be3e283SDebojyoti Ghosh   ts->poststage = func;
32799be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32809be3e283SDebojyoti Ghosh }
32819be3e283SDebojyoti Ghosh 
3282c688d042SShri Abhyankar /*@C
3283c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3284c688d042SShri Abhyankar   called once at the end of each step evaluation.
3285c688d042SShri Abhyankar 
3286c688d042SShri Abhyankar   Logically Collective on TS
3287c688d042SShri Abhyankar 
3288c688d042SShri Abhyankar   Input Parameters:
3289c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3290c688d042SShri Abhyankar - func - The function
3291c688d042SShri Abhyankar 
3292c688d042SShri Abhyankar   Calling sequence of func:
3293c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3294c688d042SShri Abhyankar 
3295c688d042SShri Abhyankar   Level: intermediate
3296c688d042SShri Abhyankar 
3297c688d042SShri Abhyankar   Note:
32981785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
32991785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3300e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3301e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3302e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3303c688d042SShri Abhyankar 
3304c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3305c688d042SShri Abhyankar @*/
3306c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3307c688d042SShri Abhyankar {
3308c688d042SShri Abhyankar   PetscFunctionBegin;
3309c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3310c688d042SShri Abhyankar   ts->postevaluate = func;
3311c688d042SShri Abhyankar   PetscFunctionReturn(0);
3312c688d042SShri Abhyankar }
3313c688d042SShri Abhyankar 
3314b8123daeSJed Brown /*@
3315b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3316b8123daeSJed Brown 
3317b8123daeSJed Brown   Collective on TS
3318b8123daeSJed Brown 
3319b8123daeSJed Brown   Input Parameters:
3320b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33219be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3322b8123daeSJed Brown 
3323b8123daeSJed Brown   Notes:
3324b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3325b8123daeSJed Brown   most users would not generally call this routine themselves.
3326b8123daeSJed Brown 
3327b8123daeSJed Brown   Level: developer
3328b8123daeSJed Brown 
33299be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3330b8123daeSJed Brown @*/
3331b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3332b8123daeSJed Brown {
3333b8123daeSJed Brown   PetscFunctionBegin;
3334b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3335ae60f76fSBarry Smith   if (ts->prestage) {
3336ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3337b8123daeSJed Brown   }
3338b8123daeSJed Brown   PetscFunctionReturn(0);
3339b8123daeSJed Brown }
3340b8123daeSJed Brown 
33419be3e283SDebojyoti Ghosh /*@
33429be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33439be3e283SDebojyoti Ghosh 
33449be3e283SDebojyoti Ghosh   Collective on TS
33459be3e283SDebojyoti Ghosh 
33469be3e283SDebojyoti Ghosh   Input Parameters:
33479be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33489be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33499be3e283SDebojyoti Ghosh   stageindex  - Stage number
33509be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33519be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33529be3e283SDebojyoti Ghosh 
33539be3e283SDebojyoti Ghosh   Notes:
33549be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33559be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33569be3e283SDebojyoti Ghosh 
33579be3e283SDebojyoti Ghosh   Level: developer
33589be3e283SDebojyoti Ghosh 
33599be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33609be3e283SDebojyoti Ghosh @*/
33619be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33629be3e283SDebojyoti Ghosh {
33639be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33649be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33654beae5d8SLisandro Dalcin   if (ts->poststage) {
33669be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33679be3e283SDebojyoti Ghosh   }
33689be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33699be3e283SDebojyoti Ghosh }
33709be3e283SDebojyoti Ghosh 
3371c688d042SShri Abhyankar /*@
3372c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3373c688d042SShri Abhyankar 
3374c688d042SShri Abhyankar   Collective on TS
3375c688d042SShri Abhyankar 
3376c688d042SShri Abhyankar   Input Parameters:
3377c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3378c688d042SShri Abhyankar 
3379c688d042SShri Abhyankar   Notes:
3380c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3381c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3382c688d042SShri Abhyankar 
3383c688d042SShri Abhyankar   Level: developer
3384c688d042SShri Abhyankar 
3385c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3386c688d042SShri Abhyankar @*/
3387c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3388c688d042SShri Abhyankar {
3389c688d042SShri Abhyankar   PetscErrorCode ierr;
3390c688d042SShri Abhyankar 
3391c688d042SShri Abhyankar   PetscFunctionBegin;
3392c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3393c688d042SShri Abhyankar   if (ts->postevaluate) {
3394dcb233daSLisandro Dalcin     Vec              U;
3395dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3396dcb233daSLisandro Dalcin 
3397dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3398dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3399c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3400dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3401dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3402c688d042SShri Abhyankar   }
3403c688d042SShri Abhyankar   PetscFunctionReturn(0);
3404c688d042SShri Abhyankar }
3405c688d042SShri Abhyankar 
3406ac226902SBarry Smith /*@C
3407000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34083f2090d5SJed Brown   called once at the end of each time step.
3409000e7ae3SMatthew Knepley 
34103f9fe445SBarry Smith   Logically Collective on TS
3411000e7ae3SMatthew Knepley 
3412000e7ae3SMatthew Knepley   Input Parameters:
3413000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3414000e7ae3SMatthew Knepley - func - The function
3415000e7ae3SMatthew Knepley 
3416000e7ae3SMatthew Knepley   Calling sequence of func:
3417b8123daeSJed Brown $ func (TS ts);
3418000e7ae3SMatthew Knepley 
34191785ff2aSShri Abhyankar   Notes:
34201785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34211785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34221785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34231785ff2aSShri Abhyankar 
3424000e7ae3SMatthew Knepley   Level: intermediate
3425000e7ae3SMatthew Knepley 
3426dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3427000e7ae3SMatthew Knepley @*/
34287087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3429000e7ae3SMatthew Knepley {
3430000e7ae3SMatthew Knepley   PetscFunctionBegin;
34310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3432ae60f76fSBarry Smith   ts->poststep = func;
3433000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3434000e7ae3SMatthew Knepley }
3435000e7ae3SMatthew Knepley 
343609ee8438SJed Brown /*@
34373f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34383f2090d5SJed Brown 
34393f2090d5SJed Brown   Collective on TS
34403f2090d5SJed Brown 
34413f2090d5SJed Brown   Input Parameters:
34423f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34433f2090d5SJed Brown 
34443f2090d5SJed Brown   Notes:
34453f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34463f2090d5SJed Brown   so most users would not generally call this routine themselves.
34473f2090d5SJed Brown 
34483f2090d5SJed Brown   Level: developer
34493f2090d5SJed Brown 
34503f2090d5SJed Brown @*/
34517087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34523f2090d5SJed Brown {
34533f2090d5SJed Brown   PetscErrorCode ierr;
34543f2090d5SJed Brown 
34553f2090d5SJed Brown   PetscFunctionBegin;
34560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3457ae60f76fSBarry Smith   if (ts->poststep) {
34585efd42a4SStefano Zampini     Vec              U;
34595efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34605efd42a4SStefano Zampini 
34615efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
34625efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3463ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
34645efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3465dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
346672ac3e02SJed Brown   }
34673f2090d5SJed Brown   PetscFunctionReturn(0);
34683f2090d5SJed Brown }
34693f2090d5SJed Brown 
3470d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3471d763cef2SBarry Smith 
3472d763cef2SBarry Smith /*@C
3473a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3474d763cef2SBarry Smith    timestep to display the iteration's  progress.
3475d763cef2SBarry Smith 
34763f9fe445SBarry Smith    Logically Collective on TS
3477d763cef2SBarry Smith 
3478d763cef2SBarry Smith    Input Parameters:
3479d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3480e213d8f1SJed Brown .  monitor - monitoring routine
3481329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34820298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3483b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34840298fd71SBarry Smith           (may be NULL)
3485d763cef2SBarry Smith 
3486e213d8f1SJed Brown    Calling sequence of monitor:
3487dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3488d763cef2SBarry Smith 
3489d763cef2SBarry Smith +    ts - the TS context
349063e21af5SBarry Smith .    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)
34911f06c33eSBarry Smith .    time - current time
34920910c330SBarry Smith .    u - current iterate
3493d763cef2SBarry Smith -    mctx - [optional] monitoring context
3494d763cef2SBarry Smith 
3495d763cef2SBarry Smith    Notes:
3496d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3497d763cef2SBarry Smith    already has been loaded.
3498d763cef2SBarry Smith 
349995452b02SPatrick Sanan    Fortran Notes:
350095452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3501025f1a04SBarry Smith 
3502d763cef2SBarry Smith    Level: intermediate
3503d763cef2SBarry Smith 
3504a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3505d763cef2SBarry Smith @*/
3506c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3507d763cef2SBarry Smith {
350878064530SBarry Smith   PetscErrorCode ierr;
350978064530SBarry Smith   PetscInt       i;
351078064530SBarry Smith   PetscBool      identical;
351178064530SBarry Smith 
3512d763cef2SBarry Smith   PetscFunctionBegin;
35130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
351478064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
351578064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
351678064530SBarry Smith     if (identical) PetscFunctionReturn(0);
351778064530SBarry Smith   }
351817186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3519d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35208704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3521d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3522d763cef2SBarry Smith   PetscFunctionReturn(0);
3523d763cef2SBarry Smith }
3524d763cef2SBarry Smith 
3525d763cef2SBarry Smith /*@C
3526a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3527d763cef2SBarry Smith 
35283f9fe445SBarry Smith    Logically Collective on TS
3529d763cef2SBarry Smith 
3530d763cef2SBarry Smith    Input Parameters:
3531d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3532d763cef2SBarry Smith 
3533d763cef2SBarry Smith    Notes:
3534d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3535d763cef2SBarry Smith 
3536d763cef2SBarry Smith    Level: intermediate
3537d763cef2SBarry Smith 
3538a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3539d763cef2SBarry Smith @*/
35407087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3541d763cef2SBarry Smith {
3542d952e501SBarry Smith   PetscErrorCode ierr;
3543d952e501SBarry Smith   PetscInt       i;
3544d952e501SBarry Smith 
3545d763cef2SBarry Smith   PetscFunctionBegin;
35460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3547d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35488704b422SBarry Smith     if (ts->monitordestroy[i]) {
35498704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3550d952e501SBarry Smith     }
3551d952e501SBarry Smith   }
3552d763cef2SBarry Smith   ts->numbermonitors = 0;
3553d763cef2SBarry Smith   PetscFunctionReturn(0);
3554d763cef2SBarry Smith }
3555d763cef2SBarry Smith 
3556721cd6eeSBarry Smith /*@C
3557721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35585516499fSSatish Balay 
35595516499fSSatish Balay    Level: intermediate
356041251cbbSSatish Balay 
356163e21af5SBarry Smith .seealso:  TSMonitorSet()
356241251cbbSSatish Balay @*/
3563721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3564d763cef2SBarry Smith {
3565dfbe8321SBarry Smith   PetscErrorCode ierr;
3566721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
356741aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3568d132466eSBarry Smith 
3569d763cef2SBarry Smith   PetscFunctionBegin;
35704d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
357141aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
357241aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3573721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
357441aca3d6SBarry Smith   if (iascii) {
3575649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
357663e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
357763e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
357863e21af5SBarry Smith     } else {
35798392e04aSShri Abhyankar       ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr);
358063e21af5SBarry Smith     }
3581649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
358241aca3d6SBarry Smith   } else if (ibinary) {
358341aca3d6SBarry Smith     PetscMPIInt rank;
358441aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
358541aca3d6SBarry Smith     if (!rank) {
3586450a797fSBarry Smith       PetscBool skipHeader;
3587450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3588450a797fSBarry Smith 
3589450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3590450a797fSBarry Smith       if (!skipHeader) {
3591f253e43cSLisandro Dalcin          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
3592450a797fSBarry Smith        }
359341aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
359441aca3d6SBarry Smith     } else {
359541aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
359641aca3d6SBarry Smith     }
359741aca3d6SBarry Smith   }
3598721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3599d763cef2SBarry Smith   PetscFunctionReturn(0);
3600d763cef2SBarry Smith }
3601d763cef2SBarry Smith 
3602cc9c3a59SBarry Smith /*@C
3603cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3604cc9c3a59SBarry Smith 
3605cc9c3a59SBarry Smith    Level: intermediate
3606cc9c3a59SBarry Smith 
3607cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3608cc9c3a59SBarry Smith @*/
3609cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3610cc9c3a59SBarry Smith {
3611cc9c3a59SBarry Smith   PetscErrorCode ierr;
3612cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3613cc9c3a59SBarry Smith   PetscBool      iascii;
3614cc9c3a59SBarry Smith   PetscReal      max,min;
3615cc9c3a59SBarry Smith 
3616cc9c3a59SBarry Smith 
3617cc9c3a59SBarry Smith   PetscFunctionBegin;
3618cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3619cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3620cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3621cc9c3a59SBarry Smith   if (iascii) {
3622cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3623cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3624cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
36255132926bSBarry Smith     ierr = 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);CHKERRQ(ierr);
3626cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3627cc9c3a59SBarry Smith   }
3628cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3629cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3630cc9c3a59SBarry Smith }
3631cc9c3a59SBarry Smith 
3632cd652676SJed Brown /*@
3633cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3634cd652676SJed Brown 
3635cd652676SJed Brown    Collective on TS
3636cd652676SJed Brown 
3637cd652676SJed Brown    Input Argument:
3638cd652676SJed Brown +  ts - time stepping context
3639cd652676SJed Brown -  t - time to interpolate to
3640cd652676SJed Brown 
3641cd652676SJed Brown    Output Argument:
36420910c330SBarry Smith .  U - state at given time
3643cd652676SJed Brown 
3644cd652676SJed Brown    Level: intermediate
3645cd652676SJed Brown 
3646cd652676SJed Brown    Developer Notes:
3647cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3648cd652676SJed Brown 
3649874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3650cd652676SJed Brown @*/
36510910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3652cd652676SJed Brown {
3653cd652676SJed Brown   PetscErrorCode ierr;
3654cd652676SJed Brown 
3655cd652676SJed Brown   PetscFunctionBegin;
3656cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3657b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3658be5899b3SLisandro Dalcin   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);
3659ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36600910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3661cd652676SJed Brown   PetscFunctionReturn(0);
3662cd652676SJed Brown }
3663cd652676SJed Brown 
3664d763cef2SBarry Smith /*@
36656d9e5789SSean Farley    TSStep - Steps one time step
3666d763cef2SBarry Smith 
3667d763cef2SBarry Smith    Collective on TS
3668d763cef2SBarry Smith 
3669d763cef2SBarry Smith    Input Parameter:
3670d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3671d763cef2SBarry Smith 
367227829d71SBarry Smith    Level: developer
3673d763cef2SBarry Smith 
3674b8123daeSJed Brown    Notes:
367527829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
367627829d71SBarry Smith 
3677b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3678b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3679b8123daeSJed Brown 
368019eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
368119eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
368225cb2221SBarry Smith 
36839be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3684d763cef2SBarry Smith @*/
3685193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3686d763cef2SBarry Smith {
3687dfbe8321SBarry Smith   PetscErrorCode   ierr;
3688fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3689be5899b3SLisandro Dalcin   PetscReal        ptime;
3690d763cef2SBarry Smith 
3691d763cef2SBarry Smith   PetscFunctionBegin;
36920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3693f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3694fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3695f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3696f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3697f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3698f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3699f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3700fffbeea8SBarry Smith 
3701d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
370268bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3703d405a339SMatthew Knepley 
3704fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3705ef85077eSLisandro Dalcin   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>");
3706a6772fa2SLisandro Dalcin   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()");
3707be5899b3SLisandro Dalcin   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");
3708a6772fa2SLisandro Dalcin 
3709be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3710be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37112808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3712fc8dbba5SLisandro Dalcin 
3713d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3714193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3715d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3716fc8dbba5SLisandro Dalcin 
3717fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3718be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
37192808aa04SLisandro Dalcin     ts->steps++;
3720be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
372128d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3722d2daff3dSHong Zhang   }
3723fc8dbba5SLisandro Dalcin 
3724fc8dbba5SLisandro Dalcin   if (!ts->reason) {
372508c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
372608c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
372708c7845fSBarry Smith   }
3728fc8dbba5SLisandro Dalcin 
3729fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed && 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]);
3730fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
373108c7845fSBarry Smith   PetscFunctionReturn(0);
373208c7845fSBarry Smith }
373308c7845fSBarry Smith 
373408c7845fSBarry Smith /*@
37357cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37367cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37377cbde773SLisandro Dalcin 
37387cbde773SLisandro Dalcin    Collective on TS
37397cbde773SLisandro Dalcin 
37407cbde773SLisandro Dalcin    Input Arguments:
37417cbde773SLisandro Dalcin +  ts - time stepping context
37427cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37437cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37447cbde773SLisandro Dalcin 
37457cbde773SLisandro Dalcin    Output Arguments:
37467cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37477cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37487cbde773SLisandro Dalcin 
37497cbde773SLisandro Dalcin    Level: advanced
37507cbde773SLisandro Dalcin 
37517cbde773SLisandro Dalcin    Notes:
37527cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37537cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37547cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37557cbde773SLisandro Dalcin 
37567cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37577cbde773SLisandro Dalcin @*/
37587cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37597cbde773SLisandro Dalcin {
37607cbde773SLisandro Dalcin   PetscErrorCode ierr;
37617cbde773SLisandro Dalcin 
37627cbde773SLisandro Dalcin   PetscFunctionBegin;
37637cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37647cbde773SLisandro Dalcin   PetscValidType(ts,1);
37657cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37667cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37677cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37687cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37697cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37707cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37717cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37727cbde773SLisandro Dalcin   PetscFunctionReturn(0);
37737cbde773SLisandro Dalcin }
37747cbde773SLisandro Dalcin 
377505175c85SJed Brown /*@
377605175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
377705175c85SJed Brown 
37781c3436cfSJed Brown    Collective on TS
377905175c85SJed Brown 
378005175c85SJed Brown    Input Arguments:
37811c3436cfSJed Brown +  ts - time stepping context
37821c3436cfSJed Brown .  order - desired order of accuracy
37830298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
378405175c85SJed Brown 
378505175c85SJed Brown    Output Arguments:
37860910c330SBarry Smith .  U - state at the end of the current step
378705175c85SJed Brown 
378805175c85SJed Brown    Level: advanced
378905175c85SJed Brown 
3790108c343cSJed Brown    Notes:
3791108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3792108c343cSJed Brown    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.
3793108c343cSJed Brown 
37941c3436cfSJed Brown .seealso: TSStep(), TSAdapt
379505175c85SJed Brown @*/
37960910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
379705175c85SJed Brown {
379805175c85SJed Brown   PetscErrorCode ierr;
379905175c85SJed Brown 
380005175c85SJed Brown   PetscFunctionBegin;
380105175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
380205175c85SJed Brown   PetscValidType(ts,1);
38030910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3804ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38050910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
380605175c85SJed Brown   PetscFunctionReturn(0);
380705175c85SJed Brown }
380805175c85SJed Brown 
3809aad739acSMatthew G. Knepley /*@C
38102e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3811aad739acSMatthew G. Knepley 
3812aad739acSMatthew G. Knepley   Not collective
3813aad739acSMatthew G. Knepley 
3814aad739acSMatthew G. Knepley   Input Argument:
3815aad739acSMatthew G. Knepley . ts        - time stepping context
3816aad739acSMatthew G. Knepley 
3817aad739acSMatthew G. Knepley   Output Argument:
38182e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3819aad739acSMatthew G. Knepley 
3820aad739acSMatthew G. Knepley    Level: advanced
3821aad739acSMatthew G. Knepley 
3822aad739acSMatthew G. Knepley    Notes:
3823aad739acSMatthew G. Knepley    The calling sequence for the function is
38242e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3825aad739acSMatthew G. Knepley $ ts - The timestepping context
38262e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3827aad739acSMatthew G. Knepley 
38282e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3829aad739acSMatthew G. Knepley @*/
38302e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3831aad739acSMatthew G. Knepley {
3832aad739acSMatthew G. Knepley   PetscFunctionBegin;
3833aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38342e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38352e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3836aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3837aad739acSMatthew G. Knepley }
3838aad739acSMatthew G. Knepley 
3839aad739acSMatthew G. Knepley /*@C
38402e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3841aad739acSMatthew G. Knepley 
3842aad739acSMatthew G. Knepley   Logically collective on ts
3843aad739acSMatthew G. Knepley 
3844aad739acSMatthew G. Knepley   Input Arguments:
3845aad739acSMatthew G. Knepley + ts        - time stepping context
38462e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3847aad739acSMatthew G. Knepley 
3848aad739acSMatthew G. Knepley   Level: advanced
3849aad739acSMatthew G. Knepley 
3850*a96d6ef6SBarry Smith   Calling sequence for initCondition:
3851*a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3852*a96d6ef6SBarry Smith 
3853*a96d6ef6SBarry Smith + ts - The timestepping context
3854*a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3855aad739acSMatthew G. Knepley 
38562e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3857aad739acSMatthew G. Knepley @*/
38582e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3859aad739acSMatthew G. Knepley {
3860aad739acSMatthew G. Knepley   PetscFunctionBegin;
3861aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38622e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
38632e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3864aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3865aad739acSMatthew G. Knepley }
3866aad739acSMatthew G. Knepley 
3867aad739acSMatthew G. Knepley /*@
38682e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3869aad739acSMatthew G. Knepley 
3870aad739acSMatthew G. Knepley   Collective on ts
3871aad739acSMatthew G. Knepley 
3872aad739acSMatthew G. Knepley   Input Arguments:
3873aad739acSMatthew G. Knepley + ts - time stepping context
38742e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3875aad739acSMatthew G. Knepley 
3876aad739acSMatthew G. Knepley   Level: advanced
3877aad739acSMatthew G. Knepley 
38782e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3879aad739acSMatthew G. Knepley @*/
38802e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3881aad739acSMatthew G. Knepley {
3882aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3883aad739acSMatthew G. Knepley 
3884aad739acSMatthew G. Knepley   PetscFunctionBegin;
3885aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3886aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
38872e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3888aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3889aad739acSMatthew G. Knepley }
3890aad739acSMatthew G. Knepley 
3891aad739acSMatthew G. Knepley /*@C
3892aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3893aad739acSMatthew G. Knepley 
3894aad739acSMatthew G. Knepley   Not collective
3895aad739acSMatthew G. Knepley 
3896aad739acSMatthew G. Knepley   Input Argument:
3897aad739acSMatthew G. Knepley . ts         - time stepping context
3898aad739acSMatthew G. Knepley 
3899aad739acSMatthew G. Knepley   Output Argument:
3900aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3901aad739acSMatthew G. Knepley 
3902aad739acSMatthew G. Knepley   Level: advanced
3903aad739acSMatthew G. Knepley 
3904*a96d6ef6SBarry Smith   Calling sequence for exactError:
3905*a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3906*a96d6ef6SBarry Smith 
3907*a96d6ef6SBarry Smith + ts - The timestepping context
3908*a96d6ef6SBarry Smith . u  - The approximate solution vector
3909*a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3910aad739acSMatthew G. Knepley 
3911aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3912aad739acSMatthew G. Knepley @*/
3913aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3914aad739acSMatthew G. Knepley {
3915aad739acSMatthew G. Knepley   PetscFunctionBegin;
3916aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3917aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3918aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3919aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3920aad739acSMatthew G. Knepley }
3921aad739acSMatthew G. Knepley 
3922aad739acSMatthew G. Knepley /*@C
3923aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3924aad739acSMatthew G. Knepley 
3925aad739acSMatthew G. Knepley   Logically collective on ts
3926aad739acSMatthew G. Knepley 
3927aad739acSMatthew G. Knepley   Input Arguments:
3928aad739acSMatthew G. Knepley + ts         - time stepping context
3929aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3930aad739acSMatthew G. Knepley 
3931aad739acSMatthew G. Knepley   Level: advanced
3932aad739acSMatthew G. Knepley 
3933*a96d6ef6SBarry Smith   Calling sequence for exactError:
3934*a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3935*a96d6ef6SBarry Smith 
3936*a96d6ef6SBarry Smith + ts - The timestepping context
3937*a96d6ef6SBarry Smith . u  - The approximate solution vector
3938*a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3939aad739acSMatthew G. Knepley 
3940aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3941aad739acSMatthew G. Knepley @*/
3942aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3943aad739acSMatthew G. Knepley {
3944aad739acSMatthew G. Knepley   PetscFunctionBegin;
3945aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3946f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3947aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3948aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3949aad739acSMatthew G. Knepley }
3950aad739acSMatthew G. Knepley 
3951aad739acSMatthew G. Knepley /*@
3952aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3953aad739acSMatthew G. Knepley 
3954aad739acSMatthew G. Knepley   Collective on ts
3955aad739acSMatthew G. Knepley 
3956aad739acSMatthew G. Knepley   Input Arguments:
3957aad739acSMatthew G. Knepley + ts - time stepping context
3958aad739acSMatthew G. Knepley . u  - The approximate solution
3959aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3960aad739acSMatthew G. Knepley 
3961aad739acSMatthew G. Knepley   Level: advanced
3962aad739acSMatthew G. Knepley 
39632e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3964aad739acSMatthew G. Knepley @*/
3965aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3966aad739acSMatthew G. Knepley {
3967aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3968aad739acSMatthew G. Knepley 
3969aad739acSMatthew G. Knepley   PetscFunctionBegin;
3970aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3971aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3972aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
3973aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
3974aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3975aad739acSMatthew G. Knepley }
3976aad739acSMatthew G. Knepley 
3977b1cb36f3SHong Zhang /*@
39786a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
39796a4d4014SLisandro Dalcin 
39806a4d4014SLisandro Dalcin    Collective on TS
39816a4d4014SLisandro Dalcin 
39826a4d4014SLisandro Dalcin    Input Parameter:
39836a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
398463e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
398563e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
39865a3a76d0SJed Brown 
39876a4d4014SLisandro Dalcin    Level: beginner
39886a4d4014SLisandro Dalcin 
39895a3a76d0SJed Brown    Notes:
39905a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
39915a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
39925a3a76d0SJed Brown    stepped over the final time.
39935a3a76d0SJed Brown 
399463e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
39956a4d4014SLisandro Dalcin @*/
3996cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
39976a4d4014SLisandro Dalcin {
3998b06615a5SLisandro Dalcin   Vec               solution;
39996a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
4000f22f69f0SBarry Smith 
40016a4d4014SLisandro Dalcin   PetscFunctionBegin;
40020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4003f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4004ee41a567SStefano Zampini   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 */
40050910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4006b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4007b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4008b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40095a3a76d0SJed Brown     }
40100910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4011715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4012bbd56ea5SKarl Rupp   } else if (u) {
40130910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40145a3a76d0SJed Brown   }
4015b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
401668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4017a6772fa2SLisandro Dalcin 
4018ef85077eSLisandro Dalcin   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>");
4019a6772fa2SLisandro Dalcin   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()");
4020a6772fa2SLisandro Dalcin   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");
4021a6772fa2SLisandro Dalcin 
4022715f1b00SHong Zhang   if (ts->forward_solve) {
4023715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4024715f1b00SHong Zhang   }
4025715f1b00SHong Zhang 
4026e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4027715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4028e7069c78SShri      TSTrajectory to incorrectly save the output files
4029e7069c78SShri   */
4030715f1b00SHong Zhang   /* reset time step and iteration counters */
40312808aa04SLisandro Dalcin   if (!ts->steps) {
40325ef26d82SJed Brown     ts->ksp_its           = 0;
40335ef26d82SJed Brown     ts->snes_its          = 0;
4034c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4035c610991cSLisandro Dalcin     ts->reject            = 0;
40362808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40372808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40387d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
40392808aa04SLisandro Dalcin   }
4040e97c63d7SStefano Zampini 
4041e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4042e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4043e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4044e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4045e97c63d7SStefano Zampini 
4046e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4047e97c63d7SStefano Zampini   }
4048193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4049193ac0bcSJed Brown 
4050900f6b5bSMatthew G. Knepley   {
4051900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4052900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4053900f6b5bSMatthew G. Knepley     PetscBool         flg;
4054900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4055900f6b5bSMatthew G. Knepley 
4056900f6b5bSMatthew G. Knepley     if (!incall) {
4057900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4058900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4059900f6b5bSMatthew G. Knepley       if (flg) {
4060900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4061900f6b5bSMatthew G. Knepley         DM           dm;
4062900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4063900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4064f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4065900f6b5bSMatthew G. Knepley 
4066900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4067f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
4068900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4069900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4070900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4071900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4072f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
4073900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4074900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4075900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4076900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4077900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4078900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4079900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4080900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4081900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4082900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4083900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4084900f6b5bSMatthew G. Knepley       }
4085900f6b5bSMatthew G. Knepley     }
4086900f6b5bSMatthew G. Knepley   }
4087900f6b5bSMatthew G. Knepley 
4088ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4089f05ece33SBarry Smith 
4090193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4091193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4092a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4093cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4094a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4095be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4096db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4097db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4098e7069c78SShri 
40992808aa04SLisandro Dalcin     if (!ts->steps) {
41002808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41016427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41022808aa04SLisandro Dalcin     }
41036427ac75SLisandro Dalcin 
4104e1a7a14fSJed Brown     while (!ts->reason) {
41052808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41069687d888SLisandro Dalcin       if (!ts->steprollback) {
41079687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41089687d888SLisandro Dalcin       }
4109193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4110f3b1f45cSBarry Smith       if (ts->testjacobian) {
4111f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4112f3b1f45cSBarry Smith       }
4113f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4114f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4115f3b1f45cSBarry Smith       }
4116cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
41177b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4118b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
41197b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4120b1cb36f3SHong Zhang       }
412158818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41227b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4123715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
41247b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4125715f1b00SHong Zhang       }
412610b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4127e783b05fSHong Zhang       ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
412858818c2dSLisandro Dalcin       if (ts->steprollback) {
412958818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
413058818c2dSLisandro Dalcin       }
4131e783b05fSHong Zhang       if (!ts->steprollback) {
41322808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41331eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4134aeb4809dSShri Abhyankar       }
4135193ac0bcSJed Brown     }
41362808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41376427ac75SLisandro Dalcin 
413849354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41390910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4140cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4141b06615a5SLisandro Dalcin       solution = u;
414263e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41430574a7fbSJed Brown     } else {
4144ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4145cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4146b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41470574a7fbSJed Brown     }
4148193ac0bcSJed Brown   }
4149d2daff3dSHong Zhang 
4150ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
415163e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
415256f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4153ef222394SBarry Smith   if (ts->adjoint_solve) {
4154ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41552b0a91c0SBarry Smith   }
41566a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41576a4d4014SLisandro Dalcin }
41586a4d4014SLisandro Dalcin 
41597db568b7SBarry Smith /*@C
4160228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4161228d79bcSJed Brown 
4162228d79bcSJed Brown    Collective on TS
4163228d79bcSJed Brown 
4164228d79bcSJed Brown    Input Parameters:
4165228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4166228d79bcSJed Brown .  step - step number that has just completed
4167228d79bcSJed Brown .  ptime - model time of the state
41680910c330SBarry Smith -  u - state at the current model time
4169228d79bcSJed Brown 
4170228d79bcSJed Brown    Notes:
41717db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
41727db568b7SBarry Smith    Users would almost never call this routine directly.
4173228d79bcSJed Brown 
417463e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
417563e21af5SBarry Smith 
41767db568b7SBarry Smith    Level: developer
4177228d79bcSJed Brown 
4178228d79bcSJed Brown @*/
41790910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4180d763cef2SBarry Smith {
4181d6152f81SLisandro Dalcin   DM             dm;
4182a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4183d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4184d763cef2SBarry Smith 
4185d763cef2SBarry Smith   PetscFunctionBegin;
4186b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4187b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4188d6152f81SLisandro Dalcin 
4189d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4190d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4191d6152f81SLisandro Dalcin 
41928860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4193d763cef2SBarry Smith   for (i=0; i<n; i++) {
41940910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4195d763cef2SBarry Smith   }
41968860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4197d763cef2SBarry Smith   PetscFunctionReturn(0);
4198d763cef2SBarry Smith }
4199d763cef2SBarry Smith 
4200d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4201d763cef2SBarry Smith /*@C
42027db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4203a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4204d763cef2SBarry Smith 
4205d763cef2SBarry Smith    Collective on TS
4206d763cef2SBarry Smith 
4207d763cef2SBarry Smith    Input Parameters:
4208d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4209d763cef2SBarry Smith .  label - the title to put in the title bar
42107c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4211a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4212a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4213d763cef2SBarry Smith 
4214d763cef2SBarry Smith    Output Parameter:
42150b039ecaSBarry Smith .  ctx - the context
4216d763cef2SBarry Smith 
4217d763cef2SBarry Smith    Options Database Key:
42184f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42198b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
42207db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42217db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42227db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42237db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4224b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4225d763cef2SBarry Smith 
4226d763cef2SBarry Smith    Notes:
4227a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4228d763cef2SBarry Smith 
42297db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42307db568b7SBarry Smith 
42317db568b7SBarry Smith    Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the
42327db568b7SBarry Smith    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
42337db568b7SBarry Smith    as the first argument.
42347db568b7SBarry Smith 
42357db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42367db568b7SBarry Smith 
4237d763cef2SBarry Smith    Level: intermediate
4238d763cef2SBarry Smith 
42397db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
42407db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
42417db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
42427db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
42437db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
42447c922b88SBarry Smith 
4245d763cef2SBarry Smith @*/
4246a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4247d763cef2SBarry Smith {
42487f52daa2SLisandro Dalcin   PetscDraw      draw;
4249dfbe8321SBarry Smith   PetscErrorCode ierr;
4250d763cef2SBarry Smith 
4251d763cef2SBarry Smith   PetscFunctionBegin;
4252b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
42537f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
42547f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
42557f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4256287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
42577f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4258a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4259d763cef2SBarry Smith   PetscFunctionReturn(0);
4260d763cef2SBarry Smith }
4261d763cef2SBarry Smith 
4262b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4263d763cef2SBarry Smith {
42640b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4265c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4266dfbe8321SBarry Smith   PetscErrorCode ierr;
4267d763cef2SBarry Smith 
4268d763cef2SBarry Smith   PetscFunctionBegin;
426963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4270b06615a5SLisandro Dalcin   if (!step) {
4271a9f9c1f6SBarry Smith     PetscDrawAxis axis;
42728b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4273a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
42748b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4275a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4276a9f9c1f6SBarry Smith   }
4277c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
42788b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
42790b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4280b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
42810b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
42826934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
42833923b477SBarry Smith   }
4284d763cef2SBarry Smith   PetscFunctionReturn(0);
4285d763cef2SBarry Smith }
4286d763cef2SBarry Smith 
4287d763cef2SBarry Smith /*@C
4288a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4289a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4290d763cef2SBarry Smith 
42910b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4292d763cef2SBarry Smith 
4293d763cef2SBarry Smith    Input Parameter:
42940b039ecaSBarry Smith .  ctx - the monitor context
4295d763cef2SBarry Smith 
4296d763cef2SBarry Smith    Level: intermediate
4297d763cef2SBarry Smith 
42984f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4299d763cef2SBarry Smith @*/
4300a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4301d763cef2SBarry Smith {
4302dfbe8321SBarry Smith   PetscErrorCode ierr;
4303d763cef2SBarry Smith 
4304d763cef2SBarry Smith   PetscFunctionBegin;
43057684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43067684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43077684fa3eSBarry Smith   }
43080b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
430931152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4310387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4311387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4312387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43130b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4314d763cef2SBarry Smith   PetscFunctionReturn(0);
4315d763cef2SBarry Smith }
4316d763cef2SBarry Smith 
43171b575b74SJoseph Pusztay /*
43181b575b74SJoseph Pusztay 
43191b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
43201b575b74SJoseph Pusztay 
43211b575b74SJoseph Pusztay */
43221b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43231b575b74SJoseph Pusztay {
43241b575b74SJoseph Pusztay   PetscDraw      draw;
43251b575b74SJoseph Pusztay   PetscErrorCode ierr;
43261b575b74SJoseph Pusztay 
43271b575b74SJoseph Pusztay   PetscFunctionBegin;
43281b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43291b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43301b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43311b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43321b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43331b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43341b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43351b575b74SJoseph Pusztay 
43361b575b74SJoseph Pusztay }
43371b575b74SJoseph Pusztay 
43381b575b74SJoseph Pusztay /*
43391b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
43401b575b74SJoseph Pusztay */
43411b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
43421b575b74SJoseph Pusztay {
43431b575b74SJoseph Pusztay   PetscErrorCode ierr;
43441b575b74SJoseph Pusztay 
43451b575b74SJoseph Pusztay   PetscFunctionBegin;
43461b575b74SJoseph Pusztay 
43471b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
43481b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
43491b575b74SJoseph Pusztay 
43501b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43511b575b74SJoseph Pusztay 
43521b575b74SJoseph Pusztay }
43531b575b74SJoseph Pusztay 
4354d763cef2SBarry Smith /*@
4355b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4356d763cef2SBarry Smith 
4357d763cef2SBarry Smith    Not Collective
4358d763cef2SBarry Smith 
4359d763cef2SBarry Smith    Input Parameter:
4360d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4361d763cef2SBarry Smith 
4362d763cef2SBarry Smith    Output Parameter:
436319eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4364d763cef2SBarry Smith 
4365d763cef2SBarry Smith    Level: beginner
4366d763cef2SBarry Smith 
4367b8123daeSJed Brown    Note:
4368b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
43699be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4370b8123daeSJed Brown 
43718f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4372d763cef2SBarry Smith 
4373d763cef2SBarry Smith @*/
43747087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4375d763cef2SBarry Smith {
4376d763cef2SBarry Smith   PetscFunctionBegin;
43770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4378f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4379d763cef2SBarry Smith   *t = ts->ptime;
4380d763cef2SBarry Smith   PetscFunctionReturn(0);
4381d763cef2SBarry Smith }
4382d763cef2SBarry Smith 
4383e5e524a1SHong Zhang /*@
4384e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4385e5e524a1SHong Zhang 
4386e5e524a1SHong Zhang    Not Collective
4387e5e524a1SHong Zhang 
4388e5e524a1SHong Zhang    Input Parameter:
4389e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4390e5e524a1SHong Zhang 
4391e5e524a1SHong Zhang    Output Parameter:
4392e5e524a1SHong Zhang .  t  - the previous time
4393e5e524a1SHong Zhang 
4394e5e524a1SHong Zhang    Level: beginner
4395e5e524a1SHong Zhang 
4396aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4397e5e524a1SHong Zhang 
4398e5e524a1SHong Zhang @*/
4399e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4400e5e524a1SHong Zhang {
4401e5e524a1SHong Zhang   PetscFunctionBegin;
4402e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4403e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4404e5e524a1SHong Zhang   *t = ts->ptime_prev;
4405e5e524a1SHong Zhang   PetscFunctionReturn(0);
4406e5e524a1SHong Zhang }
4407e5e524a1SHong Zhang 
44086a4d4014SLisandro Dalcin /*@
44096a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44106a4d4014SLisandro Dalcin 
44113f9fe445SBarry Smith    Logically Collective on TS
44126a4d4014SLisandro Dalcin 
44136a4d4014SLisandro Dalcin    Input Parameters:
44146a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44156a4d4014SLisandro Dalcin -  time - the time
44166a4d4014SLisandro Dalcin 
44176a4d4014SLisandro Dalcin    Level: intermediate
44186a4d4014SLisandro Dalcin 
441919eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
44206a4d4014SLisandro Dalcin 
44216a4d4014SLisandro Dalcin @*/
44227087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44236a4d4014SLisandro Dalcin {
44246a4d4014SLisandro Dalcin   PetscFunctionBegin;
44250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4426c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44276a4d4014SLisandro Dalcin   ts->ptime = t;
44286a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44296a4d4014SLisandro Dalcin }
44306a4d4014SLisandro Dalcin 
4431d763cef2SBarry Smith /*@C
4432d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4433d763cef2SBarry Smith    TS options in the database.
4434d763cef2SBarry Smith 
44353f9fe445SBarry Smith    Logically Collective on TS
4436d763cef2SBarry Smith 
4437d763cef2SBarry Smith    Input Parameter:
4438d763cef2SBarry Smith +  ts     - The TS context
4439d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4440d763cef2SBarry Smith 
4441d763cef2SBarry Smith    Notes:
4442d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4443d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4444d763cef2SBarry Smith    hyphen.
4445d763cef2SBarry Smith 
4446d763cef2SBarry Smith    Level: advanced
4447d763cef2SBarry Smith 
4448d763cef2SBarry Smith .seealso: TSSetFromOptions()
4449d763cef2SBarry Smith 
4450d763cef2SBarry Smith @*/
44517087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4452d763cef2SBarry Smith {
4453dfbe8321SBarry Smith   PetscErrorCode ierr;
4454089b2837SJed Brown   SNES           snes;
4455d763cef2SBarry Smith 
4456d763cef2SBarry Smith   PetscFunctionBegin;
44570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4458d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4459089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4460089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4461d763cef2SBarry Smith   PetscFunctionReturn(0);
4462d763cef2SBarry Smith }
4463d763cef2SBarry Smith 
4464d763cef2SBarry Smith /*@C
4465d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4466d763cef2SBarry Smith    TS options in the database.
4467d763cef2SBarry Smith 
44683f9fe445SBarry Smith    Logically Collective on TS
4469d763cef2SBarry Smith 
4470d763cef2SBarry Smith    Input Parameter:
4471d763cef2SBarry Smith +  ts     - The TS context
4472d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4473d763cef2SBarry Smith 
4474d763cef2SBarry Smith    Notes:
4475d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4476d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4477d763cef2SBarry Smith    hyphen.
4478d763cef2SBarry Smith 
4479d763cef2SBarry Smith    Level: advanced
4480d763cef2SBarry Smith 
4481d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4482d763cef2SBarry Smith 
4483d763cef2SBarry Smith @*/
44847087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4485d763cef2SBarry Smith {
4486dfbe8321SBarry Smith   PetscErrorCode ierr;
4487089b2837SJed Brown   SNES           snes;
4488d763cef2SBarry Smith 
4489d763cef2SBarry Smith   PetscFunctionBegin;
44900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4491d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4492089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4493089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4494d763cef2SBarry Smith   PetscFunctionReturn(0);
4495d763cef2SBarry Smith }
4496d763cef2SBarry Smith 
4497d763cef2SBarry Smith /*@C
4498d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4499d763cef2SBarry Smith    TS options in the database.
4500d763cef2SBarry Smith 
4501d763cef2SBarry Smith    Not Collective
4502d763cef2SBarry Smith 
4503d763cef2SBarry Smith    Input Parameter:
4504d763cef2SBarry Smith .  ts - The TS context
4505d763cef2SBarry Smith 
4506d763cef2SBarry Smith    Output Parameter:
4507d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4508d763cef2SBarry Smith 
450995452b02SPatrick Sanan    Notes:
451095452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4511d763cef2SBarry Smith    sufficient length to hold the prefix.
4512d763cef2SBarry Smith 
4513d763cef2SBarry Smith    Level: intermediate
4514d763cef2SBarry Smith 
4515d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4516d763cef2SBarry Smith @*/
45177087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4518d763cef2SBarry Smith {
4519dfbe8321SBarry Smith   PetscErrorCode ierr;
4520d763cef2SBarry Smith 
4521d763cef2SBarry Smith   PetscFunctionBegin;
45220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45234482741eSBarry Smith   PetscValidPointer(prefix,2);
4524d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4525d763cef2SBarry Smith   PetscFunctionReturn(0);
4526d763cef2SBarry Smith }
4527d763cef2SBarry Smith 
4528d763cef2SBarry Smith /*@C
4529d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4530d763cef2SBarry Smith 
4531d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4532d763cef2SBarry Smith 
4533d763cef2SBarry Smith    Input Parameter:
4534d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4535d763cef2SBarry Smith 
4536d763cef2SBarry Smith    Output Parameters:
4537e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4538e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4539e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4540e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4541d763cef2SBarry Smith 
454295452b02SPatrick Sanan    Notes:
454395452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4544d763cef2SBarry Smith 
4545d763cef2SBarry Smith    Level: intermediate
4546d763cef2SBarry Smith 
454780275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4548d763cef2SBarry Smith 
4549d763cef2SBarry Smith @*/
4550e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4551d763cef2SBarry Smith {
4552089b2837SJed Brown   PetscErrorCode ierr;
455324989b8cSPeter Brune   DM             dm;
4554089b2837SJed Brown 
4555d763cef2SBarry Smith   PetscFunctionBegin;
455623a57915SBarry Smith   if (Amat || Pmat) {
455723a57915SBarry Smith     SNES snes;
4558089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
455923a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4560e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
456123a57915SBarry Smith   }
456224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
456324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4564d763cef2SBarry Smith   PetscFunctionReturn(0);
4565d763cef2SBarry Smith }
4566d763cef2SBarry Smith 
45672eca1d9cSJed Brown /*@C
45682eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
45692eca1d9cSJed Brown 
45702eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
45712eca1d9cSJed Brown 
45722eca1d9cSJed Brown    Input Parameter:
45732eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
45742eca1d9cSJed Brown 
45752eca1d9cSJed Brown    Output Parameters:
4576e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4577e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
45782eca1d9cSJed Brown .  f   - The function to compute the matrices
45792eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
45802eca1d9cSJed Brown 
458195452b02SPatrick Sanan    Notes:
458295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
45832eca1d9cSJed Brown 
45842eca1d9cSJed Brown    Level: advanced
45852eca1d9cSJed Brown 
458680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
45872eca1d9cSJed Brown 
45882eca1d9cSJed Brown @*/
4589e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
45902eca1d9cSJed Brown {
4591089b2837SJed Brown   PetscErrorCode ierr;
459224989b8cSPeter Brune   DM             dm;
45930910c330SBarry Smith 
45942eca1d9cSJed Brown   PetscFunctionBegin;
4595c0aab802Sstefano_zampini   if (Amat || Pmat) {
4596c0aab802Sstefano_zampini     SNES snes;
4597089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4598f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4599e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4600c0aab802Sstefano_zampini   }
460124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
460224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46032eca1d9cSJed Brown   PetscFunctionReturn(0);
46042eca1d9cSJed Brown }
46052eca1d9cSJed Brown 
46061713a123SBarry Smith /*@C
460783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46081713a123SBarry Smith    VecView() for the solution at each timestep
46091713a123SBarry Smith 
46101713a123SBarry Smith    Collective on TS
46111713a123SBarry Smith 
46121713a123SBarry Smith    Input Parameters:
46131713a123SBarry Smith +  ts - the TS context
46141713a123SBarry Smith .  step - current time-step
4615142b95e3SSatish Balay .  ptime - current time
46160298fd71SBarry Smith -  dummy - either a viewer or NULL
46171713a123SBarry Smith 
461899fdda47SBarry Smith    Options Database:
461999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
462099fdda47SBarry Smith 
462195452b02SPatrick Sanan    Notes:
462295452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
462399fdda47SBarry Smith        will look bad
462499fdda47SBarry Smith 
46251713a123SBarry Smith    Level: intermediate
46261713a123SBarry Smith 
4627a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46281713a123SBarry Smith @*/
46290910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46301713a123SBarry Smith {
4631dfbe8321SBarry Smith   PetscErrorCode   ierr;
463283a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4633473a3ab2SBarry Smith   PetscDraw        draw;
46341713a123SBarry Smith 
46351713a123SBarry Smith   PetscFunctionBegin;
46366083293cSBarry Smith   if (!step && ictx->showinitial) {
46376083293cSBarry Smith     if (!ictx->initialsolution) {
46380910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46391713a123SBarry Smith     }
46400910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
46416083293cSBarry Smith   }
4642b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46430dcf80beSBarry Smith 
46446083293cSBarry Smith   if (ictx->showinitial) {
46456083293cSBarry Smith     PetscReal pause;
46466083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
46476083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
46486083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
46496083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
46506083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
46516083293cSBarry Smith   }
46520910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4653473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
465451fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4655473a3ab2SBarry Smith     char      time[32];
4656473a3ab2SBarry Smith 
4657473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
465885b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4659473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4660473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
466151fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4662473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4663473a3ab2SBarry Smith   }
4664473a3ab2SBarry Smith 
46656083293cSBarry Smith   if (ictx->showinitial) {
46666083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
46676083293cSBarry Smith   }
46681713a123SBarry Smith   PetscFunctionReturn(0);
46691713a123SBarry Smith }
46701713a123SBarry Smith 
46719110b2e7SHong Zhang /*@C
46722d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
46732d5ee99bSBarry Smith 
46742d5ee99bSBarry Smith    Collective on TS
46752d5ee99bSBarry Smith 
46762d5ee99bSBarry Smith    Input Parameters:
46772d5ee99bSBarry Smith +  ts - the TS context
46782d5ee99bSBarry Smith .  step - current time-step
46792d5ee99bSBarry Smith .  ptime - current time
46802d5ee99bSBarry Smith -  dummy - either a viewer or NULL
46812d5ee99bSBarry Smith 
46822d5ee99bSBarry Smith    Level: intermediate
46832d5ee99bSBarry Smith 
46842d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46852d5ee99bSBarry Smith @*/
46862d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46872d5ee99bSBarry Smith {
46882d5ee99bSBarry Smith   PetscErrorCode    ierr;
46892d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
46902d5ee99bSBarry Smith   PetscDraw         draw;
46916934998bSLisandro Dalcin   PetscDrawAxis     axis;
46922d5ee99bSBarry Smith   PetscInt          n;
46932d5ee99bSBarry Smith   PetscMPIInt       size;
46946934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
46952d5ee99bSBarry Smith   char              time[32];
46962d5ee99bSBarry Smith   const PetscScalar *U;
46972d5ee99bSBarry Smith 
46982d5ee99bSBarry Smith   PetscFunctionBegin;
46996934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
47006934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47012d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47026934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47032d5ee99bSBarry Smith 
47042d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47056934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47066934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47076934998bSLisandro Dalcin   if (!step) {
47086934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47096934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47106934998bSLisandro Dalcin   }
47112d5ee99bSBarry Smith 
47122d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47136934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47146934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4715a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47166934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47172d5ee99bSBarry Smith 
47186934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47196934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47202d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47214b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
472285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47232d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
472451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47252d5ee99bSBarry Smith   }
47266934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47272d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
472856d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47296934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47302d5ee99bSBarry Smith   PetscFunctionReturn(0);
47312d5ee99bSBarry Smith }
47322d5ee99bSBarry Smith 
47336083293cSBarry Smith /*@C
473483a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
47356083293cSBarry Smith 
47366083293cSBarry Smith    Collective on TS
47376083293cSBarry Smith 
47386083293cSBarry Smith    Input Parameters:
47396083293cSBarry Smith .    ctx - the monitor context
47406083293cSBarry Smith 
47416083293cSBarry Smith    Level: intermediate
47426083293cSBarry Smith 
474383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
47446083293cSBarry Smith @*/
474583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
47466083293cSBarry Smith {
47476083293cSBarry Smith   PetscErrorCode ierr;
47486083293cSBarry Smith 
47496083293cSBarry Smith   PetscFunctionBegin;
475083a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
475183a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
475283a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
47536083293cSBarry Smith   PetscFunctionReturn(0);
47546083293cSBarry Smith }
47556083293cSBarry Smith 
47566083293cSBarry Smith /*@C
475783a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
47586083293cSBarry Smith 
47596083293cSBarry Smith    Collective on TS
47606083293cSBarry Smith 
47616083293cSBarry Smith    Input Parameter:
47626083293cSBarry Smith .    ts - time-step context
47636083293cSBarry Smith 
47646083293cSBarry Smith    Output Patameter:
47656083293cSBarry Smith .    ctx - the monitor context
47666083293cSBarry Smith 
476799fdda47SBarry Smith    Options Database:
476899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
476999fdda47SBarry Smith 
47706083293cSBarry Smith    Level: intermediate
47716083293cSBarry Smith 
477283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
47736083293cSBarry Smith @*/
477483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
47756083293cSBarry Smith {
47766083293cSBarry Smith   PetscErrorCode   ierr;
47776083293cSBarry Smith 
47786083293cSBarry Smith   PetscFunctionBegin;
4779b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
478083a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4781e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4782bbd56ea5SKarl Rupp 
478383a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4784473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4785c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4786473a3ab2SBarry Smith 
4787473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4788c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
47896083293cSBarry Smith   PetscFunctionReturn(0);
47906083293cSBarry Smith }
47916083293cSBarry Smith 
47923a471f94SBarry Smith /*@C
47930ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
47940ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
47950ed3bfb6SBarry Smith 
47960ed3bfb6SBarry Smith    Collective on TS
47970ed3bfb6SBarry Smith 
47980ed3bfb6SBarry Smith    Input Parameters:
47990ed3bfb6SBarry Smith +  ts - the TS context
48000ed3bfb6SBarry Smith .  step - current time-step
48010ed3bfb6SBarry Smith .  ptime - current time
48020ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
48030ed3bfb6SBarry Smith 
48040ed3bfb6SBarry Smith    Options Database:
48050ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48060ed3bfb6SBarry Smith 
48070ed3bfb6SBarry Smith    Level: intermediate
48080ed3bfb6SBarry Smith 
48090ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48100ed3bfb6SBarry Smith @*/
48110ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48120ed3bfb6SBarry Smith {
48130ed3bfb6SBarry Smith   PetscErrorCode   ierr;
48140ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
48150ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
48160ed3bfb6SBarry Smith   Vec              work;
48170ed3bfb6SBarry Smith 
48180ed3bfb6SBarry Smith   PetscFunctionBegin;
48190ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48200ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48210ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48220ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48230ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48240ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48250ed3bfb6SBarry Smith }
48260ed3bfb6SBarry Smith 
48270ed3bfb6SBarry Smith /*@C
482883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48293a471f94SBarry Smith    VecView() for the error at each timestep
48303a471f94SBarry Smith 
48313a471f94SBarry Smith    Collective on TS
48323a471f94SBarry Smith 
48333a471f94SBarry Smith    Input Parameters:
48343a471f94SBarry Smith +  ts - the TS context
48353a471f94SBarry Smith .  step - current time-step
48363a471f94SBarry Smith .  ptime - current time
48370298fd71SBarry Smith -  dummy - either a viewer or NULL
48383a471f94SBarry Smith 
48390ed3bfb6SBarry Smith    Options Database:
48400ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48410ed3bfb6SBarry Smith 
48423a471f94SBarry Smith    Level: intermediate
48433a471f94SBarry Smith 
48440ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48453a471f94SBarry Smith @*/
48460910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48473a471f94SBarry Smith {
48483a471f94SBarry Smith   PetscErrorCode   ierr;
484983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
485083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
48513a471f94SBarry Smith   Vec              work;
48523a471f94SBarry Smith 
48533a471f94SBarry Smith   PetscFunctionBegin;
4854b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48550910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48563a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48570910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
48583a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48593a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48603a471f94SBarry Smith   PetscFunctionReturn(0);
48613a471f94SBarry Smith }
48623a471f94SBarry Smith 
4863af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
48646c699258SBarry Smith /*@
48652a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
48666c699258SBarry Smith 
4867d083f849SBarry Smith    Logically Collective on ts
48686c699258SBarry Smith 
48696c699258SBarry Smith    Input Parameters:
48702a808120SBarry Smith +  ts - the ODE integrator object
48712a808120SBarry Smith -  dm - the dm, cannot be NULL
48726c699258SBarry Smith 
4873e03a659cSJed Brown    Notes:
4874e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4875e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4876e03a659cSJed Brown    different problems using the same function space.
4877e03a659cSJed Brown 
48786c699258SBarry Smith    Level: intermediate
48796c699258SBarry Smith 
48806c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
48816c699258SBarry Smith @*/
48827087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
48836c699258SBarry Smith {
48846c699258SBarry Smith   PetscErrorCode ierr;
4885089b2837SJed Brown   SNES           snes;
4886942e3340SBarry Smith   DMTS           tsdm;
48876c699258SBarry Smith 
48886c699258SBarry Smith   PetscFunctionBegin;
48890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48902a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
489170663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4892942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
48932a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4894942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4895942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
489624989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
489724989b8cSPeter Brune         tsdm->originaldm = dm;
489824989b8cSPeter Brune       }
489924989b8cSPeter Brune     }
49006bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
490124989b8cSPeter Brune   }
49026c699258SBarry Smith   ts->dm = dm;
4903bbd56ea5SKarl Rupp 
4904089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4905089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49066c699258SBarry Smith   PetscFunctionReturn(0);
49076c699258SBarry Smith }
49086c699258SBarry Smith 
49096c699258SBarry Smith /*@
49106c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49116c699258SBarry Smith 
49123f9fe445SBarry Smith    Not Collective
49136c699258SBarry Smith 
49146c699258SBarry Smith    Input Parameter:
49156c699258SBarry Smith . ts - the preconditioner context
49166c699258SBarry Smith 
49176c699258SBarry Smith    Output Parameter:
49186c699258SBarry Smith .  dm - the dm
49196c699258SBarry Smith 
49206c699258SBarry Smith    Level: intermediate
49216c699258SBarry Smith 
49226c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49236c699258SBarry Smith @*/
49247087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49256c699258SBarry Smith {
4926496e6a7aSJed Brown   PetscErrorCode ierr;
4927496e6a7aSJed Brown 
49286c699258SBarry Smith   PetscFunctionBegin;
49290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4930496e6a7aSJed Brown   if (!ts->dm) {
4931ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4932496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4933496e6a7aSJed Brown   }
49346c699258SBarry Smith   *dm = ts->dm;
49356c699258SBarry Smith   PetscFunctionReturn(0);
49366c699258SBarry Smith }
49371713a123SBarry Smith 
49380f5c6efeSJed Brown /*@
49390f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49400f5c6efeSJed Brown 
49413f9fe445SBarry Smith    Logically Collective on SNES
49420f5c6efeSJed Brown 
49430f5c6efeSJed Brown    Input Parameter:
4944d42a1c89SJed Brown + snes - nonlinear solver
49450910c330SBarry Smith . U - the current state at which to evaluate the residual
4946d42a1c89SJed Brown - ctx - user context, must be a TS
49470f5c6efeSJed Brown 
49480f5c6efeSJed Brown    Output Parameter:
49490f5c6efeSJed Brown . F - the nonlinear residual
49500f5c6efeSJed Brown 
49510f5c6efeSJed Brown    Notes:
49520f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49530f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
49540f5c6efeSJed Brown 
49550f5c6efeSJed Brown    Level: advanced
49560f5c6efeSJed Brown 
49570f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
49580f5c6efeSJed Brown @*/
49590910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
49600f5c6efeSJed Brown {
49610f5c6efeSJed Brown   TS             ts = (TS)ctx;
49620f5c6efeSJed Brown   PetscErrorCode ierr;
49630f5c6efeSJed Brown 
49640f5c6efeSJed Brown   PetscFunctionBegin;
49650f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
49660910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
49670f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
49680f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
49690910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
49700f5c6efeSJed Brown   PetscFunctionReturn(0);
49710f5c6efeSJed Brown }
49720f5c6efeSJed Brown 
49730f5c6efeSJed Brown /*@
49740f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
49750f5c6efeSJed Brown 
49760f5c6efeSJed Brown    Collective on SNES
49770f5c6efeSJed Brown 
49780f5c6efeSJed Brown    Input Parameter:
49790f5c6efeSJed Brown + snes - nonlinear solver
49800910c330SBarry Smith . U - the current state at which to evaluate the residual
49810f5c6efeSJed Brown - ctx - user context, must be a TS
49820f5c6efeSJed Brown 
49830f5c6efeSJed Brown    Output Parameter:
49840f5c6efeSJed Brown + A - the Jacobian
49850f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
49860f5c6efeSJed Brown - flag - indicates any structure change in the matrix
49870f5c6efeSJed Brown 
49880f5c6efeSJed Brown    Notes:
49890f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49900f5c6efeSJed Brown 
49910f5c6efeSJed Brown    Level: developer
49920f5c6efeSJed Brown 
49930f5c6efeSJed Brown .seealso: SNESSetJacobian()
49940f5c6efeSJed Brown @*/
4995d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
49960f5c6efeSJed Brown {
49970f5c6efeSJed Brown   TS             ts = (TS)ctx;
49980f5c6efeSJed Brown   PetscErrorCode ierr;
49990f5c6efeSJed Brown 
50000f5c6efeSJed Brown   PetscFunctionBegin;
50010f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50020910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50030f5c6efeSJed Brown   PetscValidPointer(A,3);
500494ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50050f5c6efeSJed Brown   PetscValidPointer(B,4);
500694ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50070f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5008d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50090f5c6efeSJed Brown   PetscFunctionReturn(0);
50100f5c6efeSJed Brown }
5011325fc9f4SBarry Smith 
50120e4ef248SJed Brown /*@C
50139ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50140e4ef248SJed Brown 
50150e4ef248SJed Brown    Collective on TS
50160e4ef248SJed Brown 
50170e4ef248SJed Brown    Input Arguments:
50180e4ef248SJed Brown +  ts - time stepping context
50190e4ef248SJed Brown .  t - time at which to evaluate
50200910c330SBarry Smith .  U - state at which to evaluate
50210e4ef248SJed Brown -  ctx - context
50220e4ef248SJed Brown 
50230e4ef248SJed Brown    Output Arguments:
50240e4ef248SJed Brown .  F - right hand side
50250e4ef248SJed Brown 
50260e4ef248SJed Brown    Level: intermediate
50270e4ef248SJed Brown 
50280e4ef248SJed Brown    Notes:
50290e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50300e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50310e4ef248SJed Brown 
50320e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50330e4ef248SJed Brown @*/
50340910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50350e4ef248SJed Brown {
50360e4ef248SJed Brown   PetscErrorCode ierr;
50370e4ef248SJed Brown   Mat            Arhs,Brhs;
50380e4ef248SJed Brown 
50390e4ef248SJed Brown   PetscFunctionBegin;
50400e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5041d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
50420910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
50430e4ef248SJed Brown   PetscFunctionReturn(0);
50440e4ef248SJed Brown }
50450e4ef248SJed Brown 
50460e4ef248SJed Brown /*@C
50470e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
50480e4ef248SJed Brown 
50490e4ef248SJed Brown    Collective on TS
50500e4ef248SJed Brown 
50510e4ef248SJed Brown    Input Arguments:
50520e4ef248SJed Brown +  ts - time stepping context
50530e4ef248SJed Brown .  t - time at which to evaluate
50540910c330SBarry Smith .  U - state at which to evaluate
50550e4ef248SJed Brown -  ctx - context
50560e4ef248SJed Brown 
50570e4ef248SJed Brown    Output Arguments:
50580e4ef248SJed Brown +  A - pointer to operator
50590e4ef248SJed Brown .  B - pointer to preconditioning matrix
50600e4ef248SJed Brown -  flg - matrix structure flag
50610e4ef248SJed Brown 
50620e4ef248SJed Brown    Level: intermediate
50630e4ef248SJed Brown 
50640e4ef248SJed Brown    Notes:
50650e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
50660e4ef248SJed Brown 
50670e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
50680e4ef248SJed Brown @*/
5069d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
50700e4ef248SJed Brown {
50710e4ef248SJed Brown   PetscFunctionBegin;
50720e4ef248SJed Brown   PetscFunctionReturn(0);
50730e4ef248SJed Brown }
50740e4ef248SJed Brown 
50750026cea9SSean Farley /*@C
50760026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
50770026cea9SSean Farley 
50780026cea9SSean Farley    Collective on TS
50790026cea9SSean Farley 
50800026cea9SSean Farley    Input Arguments:
50810026cea9SSean Farley +  ts - time stepping context
50820026cea9SSean Farley .  t - time at which to evaluate
50830910c330SBarry Smith .  U - state at which to evaluate
50840910c330SBarry Smith .  Udot - time derivative of state vector
50850026cea9SSean Farley -  ctx - context
50860026cea9SSean Farley 
50870026cea9SSean Farley    Output Arguments:
50880026cea9SSean Farley .  F - left hand side
50890026cea9SSean Farley 
50900026cea9SSean Farley    Level: intermediate
50910026cea9SSean Farley 
50920026cea9SSean Farley    Notes:
50930910c330SBarry Smith    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
50940026cea9SSean Farley    user is required to write their own TSComputeIFunction.
50950026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
50960026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
50970026cea9SSean Farley 
50989ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
50999ae8fd06SBarry Smith 
51009ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51010026cea9SSean Farley @*/
51020910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51030026cea9SSean Farley {
51040026cea9SSean Farley   PetscErrorCode ierr;
51050026cea9SSean Farley   Mat            A,B;
51060026cea9SSean Farley 
51070026cea9SSean Farley   PetscFunctionBegin;
51080298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5109d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51100910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51110026cea9SSean Farley   PetscFunctionReturn(0);
51120026cea9SSean Farley }
51130026cea9SSean Farley 
51140026cea9SSean Farley /*@C
5115b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51160026cea9SSean Farley 
51170026cea9SSean Farley    Collective on TS
51180026cea9SSean Farley 
51190026cea9SSean Farley    Input Arguments:
51200026cea9SSean Farley +  ts - time stepping context
51210026cea9SSean Farley .  t - time at which to evaluate
51220910c330SBarry Smith .  U - state at which to evaluate
51230910c330SBarry Smith .  Udot - time derivative of state vector
51240026cea9SSean Farley .  shift - shift to apply
51250026cea9SSean Farley -  ctx - context
51260026cea9SSean Farley 
51270026cea9SSean Farley    Output Arguments:
51280026cea9SSean Farley +  A - pointer to operator
51290026cea9SSean Farley .  B - pointer to preconditioning matrix
51300026cea9SSean Farley -  flg - matrix structure flag
51310026cea9SSean Farley 
5132b41af12eSJed Brown    Level: advanced
51330026cea9SSean Farley 
51340026cea9SSean Farley    Notes:
51350026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51360026cea9SSean Farley 
5137b41af12eSJed Brown    It is only appropriate for problems of the form
5138b41af12eSJed Brown 
5139b41af12eSJed Brown $     M Udot = F(U,t)
5140b41af12eSJed Brown 
5141b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5142b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5143b41af12eSJed Brown   an implicit operator of the form
5144b41af12eSJed Brown 
5145b41af12eSJed Brown $    shift*M + J
5146b41af12eSJed Brown 
5147b41af12eSJed Brown   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
5148b41af12eSJed Brown   a copy of M or reassemble it when requested.
5149b41af12eSJed Brown 
51500026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
51510026cea9SSean Farley @*/
5152d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
51530026cea9SSean Farley {
5154b41af12eSJed Brown   PetscErrorCode ierr;
5155b41af12eSJed Brown 
51560026cea9SSean Farley   PetscFunctionBegin;
515794ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5158b41af12eSJed Brown   ts->ijacobian.shift = shift;
51590026cea9SSean Farley   PetscFunctionReturn(0);
51600026cea9SSean Farley }
5161b41af12eSJed Brown 
5162e817cc15SEmil Constantinescu /*@
5163e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5164e817cc15SEmil Constantinescu 
5165e817cc15SEmil Constantinescu    Not Collective
5166e817cc15SEmil Constantinescu 
5167e817cc15SEmil Constantinescu    Input Parameter:
5168e817cc15SEmil Constantinescu .  ts - the TS context
5169e817cc15SEmil Constantinescu 
5170e817cc15SEmil Constantinescu    Output Parameter:
51714e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5172e817cc15SEmil Constantinescu 
5173e817cc15SEmil Constantinescu    Level: beginner
5174e817cc15SEmil Constantinescu 
5175e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5176e817cc15SEmil Constantinescu @*/
5177e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5178e817cc15SEmil Constantinescu {
5179e817cc15SEmil Constantinescu   PetscFunctionBegin;
5180e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5181e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5182e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5183e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5184e817cc15SEmil Constantinescu }
5185e817cc15SEmil Constantinescu 
5186e817cc15SEmil Constantinescu /*@
5187e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5188e817cc15SEmil Constantinescu 
5189e817cc15SEmil Constantinescu    Not Collective
5190e817cc15SEmil Constantinescu 
5191e817cc15SEmil Constantinescu    Input Parameter:
5192e817cc15SEmil Constantinescu +  ts - the TS context
51931297b224SEmil Constantinescu -  equation_type - see TSEquationType
5194e817cc15SEmil Constantinescu 
5195e817cc15SEmil Constantinescu    Level: advanced
5196e817cc15SEmil Constantinescu 
5197e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5198e817cc15SEmil Constantinescu @*/
5199e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5200e817cc15SEmil Constantinescu {
5201e817cc15SEmil Constantinescu   PetscFunctionBegin;
5202e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5203e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5204e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5205e817cc15SEmil Constantinescu }
52060026cea9SSean Farley 
52074af1b03aSJed Brown /*@
52084af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52094af1b03aSJed Brown 
52104af1b03aSJed Brown    Not Collective
52114af1b03aSJed Brown 
52124af1b03aSJed Brown    Input Parameter:
52134af1b03aSJed Brown .  ts - the TS context
52144af1b03aSJed Brown 
52154af1b03aSJed Brown    Output Parameter:
52164af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52174af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52184af1b03aSJed Brown 
5219487e0bb9SJed Brown    Level: beginner
52204af1b03aSJed Brown 
5221cd652676SJed Brown    Notes:
5222cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52234af1b03aSJed Brown 
52244af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52254af1b03aSJed Brown @*/
52264af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52274af1b03aSJed Brown {
52284af1b03aSJed Brown   PetscFunctionBegin;
52294af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52304af1b03aSJed Brown   PetscValidPointer(reason,2);
52314af1b03aSJed Brown   *reason = ts->reason;
52324af1b03aSJed Brown   PetscFunctionReturn(0);
52334af1b03aSJed Brown }
52344af1b03aSJed Brown 
5235d6ad946cSShri Abhyankar /*@
5236d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5237d6ad946cSShri Abhyankar 
52386b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5239d6ad946cSShri Abhyankar 
52406b221cbeSPatrick Sanan    Input Parameters:
5241d6ad946cSShri Abhyankar +  ts - the TS context
52426b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5243d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5244d6ad946cSShri Abhyankar 
5245f5abba47SShri Abhyankar    Level: advanced
5246d6ad946cSShri Abhyankar 
5247d6ad946cSShri Abhyankar    Notes:
52486b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5249d6ad946cSShri Abhyankar 
5250d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5251d6ad946cSShri Abhyankar @*/
5252d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5253d6ad946cSShri Abhyankar {
5254d6ad946cSShri Abhyankar   PetscFunctionBegin;
5255d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5256d6ad946cSShri Abhyankar   ts->reason = reason;
5257d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5258d6ad946cSShri Abhyankar }
5259d6ad946cSShri Abhyankar 
5260cc708dedSBarry Smith /*@
5261cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5262cc708dedSBarry Smith 
5263cc708dedSBarry Smith    Not Collective
5264cc708dedSBarry Smith 
5265cc708dedSBarry Smith    Input Parameter:
5266cc708dedSBarry Smith .  ts - the TS context
5267cc708dedSBarry Smith 
5268cc708dedSBarry Smith    Output Parameter:
526919eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5270cc708dedSBarry Smith 
5271487e0bb9SJed Brown    Level: beginner
5272cc708dedSBarry Smith 
5273cc708dedSBarry Smith    Notes:
5274cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5275cc708dedSBarry Smith 
5276cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5277cc708dedSBarry Smith @*/
5278cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5279cc708dedSBarry Smith {
5280cc708dedSBarry Smith   PetscFunctionBegin;
5281cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5282cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5283cc708dedSBarry Smith   *ftime = ts->solvetime;
5284cc708dedSBarry Smith   PetscFunctionReturn(0);
5285cc708dedSBarry Smith }
5286cc708dedSBarry Smith 
52872c18e0fdSBarry Smith /*@
52885ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
52899f67acb7SJed Brown    used by the time integrator.
52909f67acb7SJed Brown 
52919f67acb7SJed Brown    Not Collective
52929f67acb7SJed Brown 
52939f67acb7SJed Brown    Input Parameter:
52949f67acb7SJed Brown .  ts - TS context
52959f67acb7SJed Brown 
52969f67acb7SJed Brown    Output Parameter:
52979f67acb7SJed Brown .  nits - number of nonlinear iterations
52989f67acb7SJed Brown 
52999f67acb7SJed Brown    Notes:
53009f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53019f67acb7SJed Brown 
53029f67acb7SJed Brown    Level: intermediate
53039f67acb7SJed Brown 
53045ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53059f67acb7SJed Brown @*/
53065ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53079f67acb7SJed Brown {
53089f67acb7SJed Brown   PetscFunctionBegin;
53099f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53109f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53115ef26d82SJed Brown   *nits = ts->snes_its;
53129f67acb7SJed Brown   PetscFunctionReturn(0);
53139f67acb7SJed Brown }
53149f67acb7SJed Brown 
53159f67acb7SJed Brown /*@
53165ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53179f67acb7SJed Brown    used by the time integrator.
53189f67acb7SJed Brown 
53199f67acb7SJed Brown    Not Collective
53209f67acb7SJed Brown 
53219f67acb7SJed Brown    Input Parameter:
53229f67acb7SJed Brown .  ts - TS context
53239f67acb7SJed Brown 
53249f67acb7SJed Brown    Output Parameter:
53259f67acb7SJed Brown .  lits - number of linear iterations
53269f67acb7SJed Brown 
53279f67acb7SJed Brown    Notes:
53289f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53299f67acb7SJed Brown 
53309f67acb7SJed Brown    Level: intermediate
53319f67acb7SJed Brown 
53325ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
53339f67acb7SJed Brown @*/
53345ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
53359f67acb7SJed Brown {
53369f67acb7SJed Brown   PetscFunctionBegin;
53379f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53389f67acb7SJed Brown   PetscValidIntPointer(lits,2);
53395ef26d82SJed Brown   *lits = ts->ksp_its;
53409f67acb7SJed Brown   PetscFunctionReturn(0);
53419f67acb7SJed Brown }
53429f67acb7SJed Brown 
5343cef5090cSJed Brown /*@
5344cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5345cef5090cSJed Brown 
5346cef5090cSJed Brown    Not Collective
5347cef5090cSJed Brown 
5348cef5090cSJed Brown    Input Parameter:
5349cef5090cSJed Brown .  ts - TS context
5350cef5090cSJed Brown 
5351cef5090cSJed Brown    Output Parameter:
5352cef5090cSJed Brown .  rejects - number of steps rejected
5353cef5090cSJed Brown 
5354cef5090cSJed Brown    Notes:
5355cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5356cef5090cSJed Brown 
5357cef5090cSJed Brown    Level: intermediate
5358cef5090cSJed Brown 
53595ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5360cef5090cSJed Brown @*/
5361cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5362cef5090cSJed Brown {
5363cef5090cSJed Brown   PetscFunctionBegin;
5364cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5365cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5366cef5090cSJed Brown   *rejects = ts->reject;
5367cef5090cSJed Brown   PetscFunctionReturn(0);
5368cef5090cSJed Brown }
5369cef5090cSJed Brown 
5370cef5090cSJed Brown /*@
5371cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5372cef5090cSJed Brown 
5373cef5090cSJed Brown    Not Collective
5374cef5090cSJed Brown 
5375cef5090cSJed Brown    Input Parameter:
5376cef5090cSJed Brown .  ts - TS context
5377cef5090cSJed Brown 
5378cef5090cSJed Brown    Output Parameter:
5379cef5090cSJed Brown .  fails - number of failed nonlinear solves
5380cef5090cSJed Brown 
5381cef5090cSJed Brown    Notes:
5382cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5383cef5090cSJed Brown 
5384cef5090cSJed Brown    Level: intermediate
5385cef5090cSJed Brown 
53865ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5387cef5090cSJed Brown @*/
5388cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5389cef5090cSJed Brown {
5390cef5090cSJed Brown   PetscFunctionBegin;
5391cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5392cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5393cef5090cSJed Brown   *fails = ts->num_snes_failures;
5394cef5090cSJed Brown   PetscFunctionReturn(0);
5395cef5090cSJed Brown }
5396cef5090cSJed Brown 
5397cef5090cSJed Brown /*@
5398cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5399cef5090cSJed Brown 
5400cef5090cSJed Brown    Not Collective
5401cef5090cSJed Brown 
5402cef5090cSJed Brown    Input Parameter:
5403cef5090cSJed Brown +  ts - TS context
5404cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5405cef5090cSJed Brown 
5406cef5090cSJed Brown    Notes:
5407cef5090cSJed Brown    The counter is reset to zero for each step
5408cef5090cSJed Brown 
5409cef5090cSJed Brown    Options Database Key:
5410cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5411cef5090cSJed Brown 
5412cef5090cSJed Brown    Level: intermediate
5413cef5090cSJed Brown 
54145ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5415cef5090cSJed Brown @*/
5416cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5417cef5090cSJed Brown {
5418cef5090cSJed Brown   PetscFunctionBegin;
5419cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5420cef5090cSJed Brown   ts->max_reject = rejects;
5421cef5090cSJed Brown   PetscFunctionReturn(0);
5422cef5090cSJed Brown }
5423cef5090cSJed Brown 
5424cef5090cSJed Brown /*@
5425cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5426cef5090cSJed Brown 
5427cef5090cSJed Brown    Not Collective
5428cef5090cSJed Brown 
5429cef5090cSJed Brown    Input Parameter:
5430cef5090cSJed Brown +  ts - TS context
5431cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5432cef5090cSJed Brown 
5433cef5090cSJed Brown    Notes:
5434cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5435cef5090cSJed Brown 
5436cef5090cSJed Brown    Options Database Key:
5437cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5438cef5090cSJed Brown 
5439cef5090cSJed Brown    Level: intermediate
5440cef5090cSJed Brown 
54415ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5442cef5090cSJed Brown @*/
5443cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5444cef5090cSJed Brown {
5445cef5090cSJed Brown   PetscFunctionBegin;
5446cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5447cef5090cSJed Brown   ts->max_snes_failures = fails;
5448cef5090cSJed Brown   PetscFunctionReturn(0);
5449cef5090cSJed Brown }
5450cef5090cSJed Brown 
5451cef5090cSJed Brown /*@
5452cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5453cef5090cSJed Brown 
5454cef5090cSJed Brown    Not Collective
5455cef5090cSJed Brown 
5456cef5090cSJed Brown    Input Parameter:
5457cef5090cSJed Brown +  ts - TS context
5458cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5459cef5090cSJed Brown 
5460cef5090cSJed Brown    Options Database Key:
5461cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5462cef5090cSJed Brown 
5463cef5090cSJed Brown    Level: intermediate
5464cef5090cSJed Brown 
54655ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5466cef5090cSJed Brown @*/
5467cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5468cef5090cSJed Brown {
5469cef5090cSJed Brown   PetscFunctionBegin;
5470cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5471cef5090cSJed Brown   ts->errorifstepfailed = err;
5472cef5090cSJed Brown   PetscFunctionReturn(0);
5473cef5090cSJed Brown }
5474cef5090cSJed Brown 
5475fb1732b5SBarry Smith /*@C
5476fde5950dSBarry Smith    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
5477fb1732b5SBarry Smith 
5478fb1732b5SBarry Smith    Collective on TS
5479fb1732b5SBarry Smith 
5480fb1732b5SBarry Smith    Input Parameters:
5481fb1732b5SBarry Smith +  ts - the TS context
5482fb1732b5SBarry Smith .  step - current time-step
5483fb1732b5SBarry Smith .  ptime - current time
54840910c330SBarry Smith .  u - current state
5485721cd6eeSBarry Smith -  vf - viewer and its format
5486fb1732b5SBarry Smith 
5487fb1732b5SBarry Smith    Level: intermediate
5488fb1732b5SBarry Smith 
5489fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5490fb1732b5SBarry Smith @*/
5491721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5492fb1732b5SBarry Smith {
5493fb1732b5SBarry Smith   PetscErrorCode ierr;
5494fb1732b5SBarry Smith 
5495fb1732b5SBarry Smith   PetscFunctionBegin;
5496721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5497721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5498721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5499ed81e22dSJed Brown   PetscFunctionReturn(0);
5500ed81e22dSJed Brown }
5501ed81e22dSJed Brown 
5502ed81e22dSJed Brown /*@C
5503ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5504ed81e22dSJed Brown 
5505ed81e22dSJed Brown    Collective on TS
5506ed81e22dSJed Brown 
5507ed81e22dSJed Brown    Input Parameters:
5508ed81e22dSJed Brown +  ts - the TS context
5509ed81e22dSJed Brown .  step - current time-step
5510ed81e22dSJed Brown .  ptime - current time
55110910c330SBarry Smith .  u - current state
5512ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5513ed81e22dSJed Brown 
5514ed81e22dSJed Brown    Level: intermediate
5515ed81e22dSJed Brown 
5516ed81e22dSJed Brown    Notes:
5517ed81e22dSJed Brown    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.
5518ed81e22dSJed Brown    These are named according to the file name template.
5519ed81e22dSJed Brown 
5520ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5521ed81e22dSJed Brown 
5522ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5523ed81e22dSJed Brown @*/
55240910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5525ed81e22dSJed Brown {
5526ed81e22dSJed Brown   PetscErrorCode ierr;
5527ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5528ed81e22dSJed Brown   PetscViewer    viewer;
5529ed81e22dSJed Brown 
5530ed81e22dSJed Brown   PetscFunctionBegin;
553163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55328caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5533ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
55340910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5535ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5536ed81e22dSJed Brown   PetscFunctionReturn(0);
5537ed81e22dSJed Brown }
5538ed81e22dSJed Brown 
5539ed81e22dSJed Brown /*@C
5540ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5541ed81e22dSJed Brown 
5542ed81e22dSJed Brown    Collective on TS
5543ed81e22dSJed Brown 
5544ed81e22dSJed Brown    Input Parameters:
5545ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5546ed81e22dSJed Brown 
5547ed81e22dSJed Brown    Level: intermediate
5548ed81e22dSJed Brown 
5549ed81e22dSJed Brown    Note:
5550ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5551ed81e22dSJed Brown 
5552ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5553ed81e22dSJed Brown @*/
5554ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5555ed81e22dSJed Brown {
5556ed81e22dSJed Brown   PetscErrorCode ierr;
5557ed81e22dSJed Brown 
5558ed81e22dSJed Brown   PetscFunctionBegin;
5559ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5560fb1732b5SBarry Smith   PetscFunctionReturn(0);
5561fb1732b5SBarry Smith }
5562fb1732b5SBarry Smith 
556384df9cb4SJed Brown /*@
5564552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
556584df9cb4SJed Brown 
5566ed81e22dSJed Brown    Collective on TS if controller has not been created yet
556784df9cb4SJed Brown 
556884df9cb4SJed Brown    Input Arguments:
5569ed81e22dSJed Brown .  ts - time stepping context
557084df9cb4SJed Brown 
557184df9cb4SJed Brown    Output Arguments:
5572ed81e22dSJed Brown .  adapt - adaptive controller
557384df9cb4SJed Brown 
557484df9cb4SJed Brown    Level: intermediate
557584df9cb4SJed Brown 
5576ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
557784df9cb4SJed Brown @*/
5578552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
557984df9cb4SJed Brown {
558084df9cb4SJed Brown   PetscErrorCode ierr;
558184df9cb4SJed Brown 
558284df9cb4SJed Brown   PetscFunctionBegin;
558384df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5584bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
558584df9cb4SJed Brown   if (!ts->adapt) {
5586ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
55873bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
55881c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
558984df9cb4SJed Brown   }
5590bec58848SLisandro Dalcin   *adapt = ts->adapt;
559184df9cb4SJed Brown   PetscFunctionReturn(0);
559284df9cb4SJed Brown }
5593d6ebe24aSShri Abhyankar 
55941c3436cfSJed Brown /*@
55951c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
55961c3436cfSJed Brown 
55971c3436cfSJed Brown    Logically Collective
55981c3436cfSJed Brown 
55991c3436cfSJed Brown    Input Arguments:
56001c3436cfSJed Brown +  ts - time integration context
56011c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56020298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56031c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56040298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
56051c3436cfSJed Brown 
5606a3cdaa26SBarry Smith    Options Database keys:
5607a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5608a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5609a3cdaa26SBarry Smith 
56103ff766beSShri Abhyankar    Notes:
56113ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
56123ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
56133ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
56143ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
56153ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
56163ff766beSShri Abhyankar    differential variables.
56173ff766beSShri Abhyankar 
56181c3436cfSJed Brown    Level: beginner
56191c3436cfSJed Brown 
56205c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
56211c3436cfSJed Brown @*/
56221c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56231c3436cfSJed Brown {
56241c3436cfSJed Brown   PetscErrorCode ierr;
56251c3436cfSJed Brown 
56261c3436cfSJed Brown   PetscFunctionBegin;
5627c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56281c3436cfSJed Brown   if (vatol) {
56291c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56301c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56311c3436cfSJed Brown     ts->vatol = vatol;
56321c3436cfSJed Brown   }
5633c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
56341c3436cfSJed Brown   if (vrtol) {
56351c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
56361c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
56371c3436cfSJed Brown     ts->vrtol = vrtol;
56381c3436cfSJed Brown   }
56391c3436cfSJed Brown   PetscFunctionReturn(0);
56401c3436cfSJed Brown }
56411c3436cfSJed Brown 
5642c5033834SJed Brown /*@
5643c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5644c5033834SJed Brown 
5645c5033834SJed Brown    Logically Collective
5646c5033834SJed Brown 
5647c5033834SJed Brown    Input Arguments:
5648c5033834SJed Brown .  ts - time integration context
5649c5033834SJed Brown 
5650c5033834SJed Brown    Output Arguments:
56510298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
56520298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
56530298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
56540298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5655c5033834SJed Brown 
5656c5033834SJed Brown    Level: beginner
5657c5033834SJed Brown 
56585c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5659c5033834SJed Brown @*/
5660c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5661c5033834SJed Brown {
5662c5033834SJed Brown   PetscFunctionBegin;
5663c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5664c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5665c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5666c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5667c5033834SJed Brown   PetscFunctionReturn(0);
5668c5033834SJed Brown }
5669c5033834SJed Brown 
56709c6b16b5SShri Abhyankar /*@
5671a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
56729c6b16b5SShri Abhyankar 
56739c6b16b5SShri Abhyankar    Collective on TS
56749c6b16b5SShri Abhyankar 
56759c6b16b5SShri Abhyankar    Input Arguments:
56769c6b16b5SShri Abhyankar +  ts - time stepping context
5677a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5678a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
56799c6b16b5SShri Abhyankar 
56809c6b16b5SShri Abhyankar    Output Arguments:
5681a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
56827453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5683a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
56849c6b16b5SShri Abhyankar 
56859c6b16b5SShri Abhyankar    Level: developer
56869c6b16b5SShri Abhyankar 
5687deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
56889c6b16b5SShri Abhyankar @*/
56897453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56909c6b16b5SShri Abhyankar {
56919c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
56929c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
56937453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
56947453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
56959c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
56967453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
56977453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
56987453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
56999c6b16b5SShri Abhyankar 
57009c6b16b5SShri Abhyankar   PetscFunctionBegin;
57019c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5702a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5703a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5704a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5705a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5706a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5707a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57088a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57098a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5710a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57119c6b16b5SShri Abhyankar 
57129c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57139c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57149c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57159c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57169c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57177453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
57187453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
57197453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
57209c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57219c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57229c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57239c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57249c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
572576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57267453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57277453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57287453f775SEmil Constantinescu       if(tola>0.){
57297453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57307453f775SEmil Constantinescu         na_loc++;
57317453f775SEmil Constantinescu       }
57327453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57337453f775SEmil Constantinescu       if(tolr>0.){
57347453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57357453f775SEmil Constantinescu         nr_loc++;
57367453f775SEmil Constantinescu       }
57377453f775SEmil Constantinescu       tol=tola+tolr;
57387453f775SEmil Constantinescu       if(tol>0.){
57397453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57407453f775SEmil Constantinescu         n_loc++;
57417453f775SEmil Constantinescu       }
57429c6b16b5SShri Abhyankar     }
57439c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57449c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57459c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57469c6b16b5SShri Abhyankar     const PetscScalar *atol;
57479c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57489c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
574976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57507453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57517453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57527453f775SEmil Constantinescu       if(tola>0.){
57537453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57547453f775SEmil Constantinescu         na_loc++;
57557453f775SEmil Constantinescu       }
57567453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57577453f775SEmil Constantinescu       if(tolr>0.){
57587453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57597453f775SEmil Constantinescu         nr_loc++;
57607453f775SEmil Constantinescu       }
57617453f775SEmil Constantinescu       tol=tola+tolr;
57627453f775SEmil Constantinescu       if(tol>0.){
57637453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57647453f775SEmil Constantinescu         n_loc++;
57657453f775SEmil Constantinescu       }
57669c6b16b5SShri Abhyankar     }
57679c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57689c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57699c6b16b5SShri Abhyankar     const PetscScalar *rtol;
57709c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57719c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
577276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57737453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57747453f775SEmil Constantinescu       tola = ts->atol;
57757453f775SEmil Constantinescu       if(tola>0.){
57767453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57777453f775SEmil Constantinescu         na_loc++;
57787453f775SEmil Constantinescu       }
57797453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57807453f775SEmil Constantinescu       if(tolr>0.){
57817453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57827453f775SEmil Constantinescu         nr_loc++;
57837453f775SEmil Constantinescu       }
57847453f775SEmil Constantinescu       tol=tola+tolr;
57857453f775SEmil Constantinescu       if(tol>0.){
57867453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57877453f775SEmil Constantinescu         n_loc++;
57887453f775SEmil Constantinescu       }
57899c6b16b5SShri Abhyankar     }
57909c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57919c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
57929c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
579376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57947453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57957453f775SEmil Constantinescu       tola = ts->atol;
57967453f775SEmil Constantinescu       if(tola>0.){
57977453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57987453f775SEmil Constantinescu         na_loc++;
57997453f775SEmil Constantinescu       }
58007453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58017453f775SEmil Constantinescu       if(tolr>0.){
58027453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58037453f775SEmil Constantinescu         nr_loc++;
58047453f775SEmil Constantinescu       }
58057453f775SEmil Constantinescu       tol=tola+tolr;
58067453f775SEmil Constantinescu       if(tol>0.){
58077453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58087453f775SEmil Constantinescu         n_loc++;
58097453f775SEmil Constantinescu       }
58109c6b16b5SShri Abhyankar     }
58119c6b16b5SShri Abhyankar   }
58129c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58139c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58149c6b16b5SShri Abhyankar 
58157453f775SEmil Constantinescu   err_loc[0] = sum;
58167453f775SEmil Constantinescu   err_loc[1] = suma;
58177453f775SEmil Constantinescu   err_loc[2] = sumr;
58187453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58197453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58207453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58217453f775SEmil Constantinescu 
5822a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58237453f775SEmil Constantinescu 
58247453f775SEmil Constantinescu   gsum   = err_glb[0];
58257453f775SEmil Constantinescu   gsuma  = err_glb[1];
58267453f775SEmil Constantinescu   gsumr  = err_glb[2];
58277453f775SEmil Constantinescu   n_glb  = err_glb[3];
58287453f775SEmil Constantinescu   na_glb = err_glb[4];
58297453f775SEmil Constantinescu   nr_glb = err_glb[5];
58307453f775SEmil Constantinescu 
5831b1316ef9SEmil Constantinescu   *norm  = 0.;
5832b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5833b1316ef9SEmil Constantinescu   *norma = 0.;
5834b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5835b1316ef9SEmil Constantinescu   *normr = 0.;
5836b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58379c6b16b5SShri Abhyankar 
58389c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58397453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58407453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58419c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
58429c6b16b5SShri Abhyankar }
58439c6b16b5SShri Abhyankar 
58449c6b16b5SShri Abhyankar /*@
5845a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
58469c6b16b5SShri Abhyankar 
58479c6b16b5SShri Abhyankar    Collective on TS
58489c6b16b5SShri Abhyankar 
58499c6b16b5SShri Abhyankar    Input Arguments:
58509c6b16b5SShri Abhyankar +  ts - time stepping context
5851a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5852a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
58539c6b16b5SShri Abhyankar 
58549c6b16b5SShri Abhyankar    Output Arguments:
5855a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58567453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5857a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58589c6b16b5SShri Abhyankar 
58599c6b16b5SShri Abhyankar    Level: developer
58609c6b16b5SShri Abhyankar 
5861deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
58629c6b16b5SShri Abhyankar @*/
58637453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58649c6b16b5SShri Abhyankar {
58659c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
58667453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
58679c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
58687453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
58697453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
58707453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
58719c6b16b5SShri Abhyankar 
58729c6b16b5SShri Abhyankar   PetscFunctionBegin;
58739c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5874a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5875a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5876a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5877a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5878a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5879a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
58808a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
58818a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5882a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
58839c6b16b5SShri Abhyankar 
58849c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
58859c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
58869c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
58879c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58889c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58897453f775SEmil Constantinescu 
58907453f775SEmil Constantinescu   max=0.;
58917453f775SEmil Constantinescu   maxa=0.;
58927453f775SEmil Constantinescu   maxr=0.;
58937453f775SEmil Constantinescu 
58947453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
58959c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
58969c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58979c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58987453f775SEmil Constantinescu 
58997453f775SEmil Constantinescu     for (i=0; i<n; i++) {
590076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59017453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59027453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59037453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59047453f775SEmil Constantinescu       tol  = tola+tolr;
59057453f775SEmil Constantinescu       if(tola>0.){
59067453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59077453f775SEmil Constantinescu       }
59087453f775SEmil Constantinescu       if(tolr>0.){
59097453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59107453f775SEmil Constantinescu       }
59117453f775SEmil Constantinescu       if(tol>0.){
59127453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59137453f775SEmil Constantinescu       }
59149c6b16b5SShri Abhyankar     }
59159c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59169c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59179c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59189c6b16b5SShri Abhyankar     const PetscScalar *atol;
59199c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59207453f775SEmil Constantinescu     for (i=0; i<n; i++) {
592176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59237453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59247453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59257453f775SEmil Constantinescu       tol  = tola+tolr;
59267453f775SEmil Constantinescu       if(tola>0.){
59277453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59287453f775SEmil Constantinescu       }
59297453f775SEmil Constantinescu       if(tolr>0.){
59307453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59317453f775SEmil Constantinescu       }
59327453f775SEmil Constantinescu       if(tol>0.){
59337453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59347453f775SEmil Constantinescu       }
59359c6b16b5SShri Abhyankar     }
59369c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59379c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59389c6b16b5SShri Abhyankar     const PetscScalar *rtol;
59399c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59407453f775SEmil Constantinescu 
59417453f775SEmil Constantinescu     for (i=0; i<n; i++) {
594276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59437453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59447453f775SEmil Constantinescu       tola = ts->atol;
59457453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59467453f775SEmil Constantinescu       tol  = tola+tolr;
59477453f775SEmil Constantinescu       if(tola>0.){
59487453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59497453f775SEmil Constantinescu       }
59507453f775SEmil Constantinescu       if(tolr>0.){
59517453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59527453f775SEmil Constantinescu       }
59537453f775SEmil Constantinescu       if(tol>0.){
59547453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59557453f775SEmil Constantinescu       }
59569c6b16b5SShri Abhyankar     }
59579c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59589c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
59597453f775SEmil Constantinescu 
59607453f775SEmil Constantinescu     for (i=0; i<n; i++) {
596176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59627453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59637453f775SEmil Constantinescu       tola = ts->atol;
59647453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59657453f775SEmil Constantinescu       tol  = tola+tolr;
59667453f775SEmil Constantinescu       if(tola>0.){
59677453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59687453f775SEmil Constantinescu       }
59697453f775SEmil Constantinescu       if(tolr>0.){
59707453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59717453f775SEmil Constantinescu       }
59727453f775SEmil Constantinescu       if(tol>0.){
59737453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59747453f775SEmil Constantinescu       }
59759c6b16b5SShri Abhyankar     }
59769c6b16b5SShri Abhyankar   }
59779c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59789c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59797453f775SEmil Constantinescu   err_loc[0] = max;
59807453f775SEmil Constantinescu   err_loc[1] = maxa;
59817453f775SEmil Constantinescu   err_loc[2] = maxr;
5982a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59837453f775SEmil Constantinescu   gmax   = err_glb[0];
59847453f775SEmil Constantinescu   gmaxa  = err_glb[1];
59857453f775SEmil Constantinescu   gmaxr  = err_glb[2];
59869c6b16b5SShri Abhyankar 
59879c6b16b5SShri Abhyankar   *norm = gmax;
59887453f775SEmil Constantinescu   *norma = gmaxa;
59897453f775SEmil Constantinescu   *normr = gmaxr;
59909c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59917453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59927453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59939c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59949c6b16b5SShri Abhyankar }
59959c6b16b5SShri Abhyankar 
59961c3436cfSJed Brown /*@
59978a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
59981c3436cfSJed Brown 
59991c3436cfSJed Brown    Collective on TS
60001c3436cfSJed Brown 
60011c3436cfSJed Brown    Input Arguments:
60021c3436cfSJed Brown +  ts - time stepping context
6003a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6004a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6005a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60067619abb3SShri 
60071c3436cfSJed Brown    Output Arguments:
6008a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60098a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6010a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6011a4868fbcSLisandro Dalcin 
6012a4868fbcSLisandro Dalcin    Options Database Keys:
6013a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6014a4868fbcSLisandro Dalcin 
60151c3436cfSJed Brown    Level: developer
60161c3436cfSJed Brown 
60178a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
60181c3436cfSJed Brown @*/
60197453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60201c3436cfSJed Brown {
60218beabaa1SBarry Smith   PetscErrorCode ierr;
60221c3436cfSJed Brown 
60231c3436cfSJed Brown   PetscFunctionBegin;
6024a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60257453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6026a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
60277453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6028a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60291c3436cfSJed Brown   PetscFunctionReturn(0);
60301c3436cfSJed Brown }
60311c3436cfSJed Brown 
60328a175baeSEmil Constantinescu 
60338a175baeSEmil Constantinescu /*@
60348a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
60358a175baeSEmil Constantinescu 
60368a175baeSEmil Constantinescu    Collective on TS
60378a175baeSEmil Constantinescu 
60388a175baeSEmil Constantinescu    Input Arguments:
60398a175baeSEmil Constantinescu +  ts - time stepping context
60408a175baeSEmil Constantinescu .  E - error vector
60418a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60428a175baeSEmil Constantinescu -  Y - state vector, previous time step
60438a175baeSEmil Constantinescu 
60448a175baeSEmil Constantinescu    Output Arguments:
6045a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60468a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6047a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60488a175baeSEmil Constantinescu 
60498a175baeSEmil Constantinescu    Level: developer
60508a175baeSEmil Constantinescu 
60518a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
60528a175baeSEmil Constantinescu @*/
60538a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60548a175baeSEmil Constantinescu {
60558a175baeSEmil Constantinescu   PetscErrorCode    ierr;
60568a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
60578a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
60588a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
60598a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
60608a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
60618a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
60628a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
60638a175baeSEmil Constantinescu 
60648a175baeSEmil Constantinescu   PetscFunctionBegin;
60658a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60668a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
60678a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
60688a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
60698a175baeSEmil Constantinescu   PetscValidType(E,2);
60708a175baeSEmil Constantinescu   PetscValidType(U,3);
60718a175baeSEmil Constantinescu   PetscValidType(Y,4);
60728a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
60738a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
60748a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
60758a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
60768a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
60778a175baeSEmil Constantinescu 
60788a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
60798a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
60808a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
60818a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
60828a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60838a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60848a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
60858a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
60868a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
60878a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
60888a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
60898a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60908a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60918a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
609276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60948a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60958a175baeSEmil Constantinescu       if(tola>0.){
60968a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
60978a175baeSEmil Constantinescu         na_loc++;
60988a175baeSEmil Constantinescu       }
60998a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61008a175baeSEmil Constantinescu       if(tolr>0.){
61018a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61028a175baeSEmil Constantinescu         nr_loc++;
61038a175baeSEmil Constantinescu       }
61048a175baeSEmil Constantinescu       tol=tola+tolr;
61058a175baeSEmil Constantinescu       if(tol>0.){
61068a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61078a175baeSEmil Constantinescu         n_loc++;
61088a175baeSEmil Constantinescu       }
61098a175baeSEmil Constantinescu     }
61108a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61118a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61128a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61138a175baeSEmil Constantinescu     const PetscScalar *atol;
61148a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61158a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
611676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61178a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61188a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61198a175baeSEmil Constantinescu       if(tola>0.){
61208a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61218a175baeSEmil Constantinescu         na_loc++;
61228a175baeSEmil Constantinescu       }
61238a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61248a175baeSEmil Constantinescu       if(tolr>0.){
61258a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61268a175baeSEmil Constantinescu         nr_loc++;
61278a175baeSEmil Constantinescu       }
61288a175baeSEmil Constantinescu       tol=tola+tolr;
61298a175baeSEmil Constantinescu       if(tol>0.){
61308a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61318a175baeSEmil Constantinescu         n_loc++;
61328a175baeSEmil Constantinescu       }
61338a175baeSEmil Constantinescu     }
61348a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61358a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61368a175baeSEmil Constantinescu     const PetscScalar *rtol;
61378a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61388a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
613976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61408a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61418a175baeSEmil Constantinescu       tola = ts->atol;
61428a175baeSEmil Constantinescu       if(tola>0.){
61438a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61448a175baeSEmil Constantinescu         na_loc++;
61458a175baeSEmil Constantinescu       }
61468a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61478a175baeSEmil Constantinescu       if(tolr>0.){
61488a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61498a175baeSEmil Constantinescu         nr_loc++;
61508a175baeSEmil Constantinescu       }
61518a175baeSEmil Constantinescu       tol=tola+tolr;
61528a175baeSEmil Constantinescu       if(tol>0.){
61538a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61548a175baeSEmil Constantinescu         n_loc++;
61558a175baeSEmil Constantinescu       }
61568a175baeSEmil Constantinescu     }
61578a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61588a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
61598a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
616076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61618a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61628a175baeSEmil Constantinescu       tola = ts->atol;
61638a175baeSEmil Constantinescu       if(tola>0.){
61648a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61658a175baeSEmil Constantinescu         na_loc++;
61668a175baeSEmil Constantinescu       }
61678a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61688a175baeSEmil Constantinescu       if(tolr>0.){
61698a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61708a175baeSEmil Constantinescu         nr_loc++;
61718a175baeSEmil Constantinescu       }
61728a175baeSEmil Constantinescu       tol=tola+tolr;
61738a175baeSEmil Constantinescu       if(tol>0.){
61748a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61758a175baeSEmil Constantinescu         n_loc++;
61768a175baeSEmil Constantinescu       }
61778a175baeSEmil Constantinescu     }
61788a175baeSEmil Constantinescu   }
61798a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
61808a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61818a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61828a175baeSEmil Constantinescu 
61838a175baeSEmil Constantinescu   err_loc[0] = sum;
61848a175baeSEmil Constantinescu   err_loc[1] = suma;
61858a175baeSEmil Constantinescu   err_loc[2] = sumr;
61868a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
61878a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
61888a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
61898a175baeSEmil Constantinescu 
6190a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61918a175baeSEmil Constantinescu 
61928a175baeSEmil Constantinescu   gsum   = err_glb[0];
61938a175baeSEmil Constantinescu   gsuma  = err_glb[1];
61948a175baeSEmil Constantinescu   gsumr  = err_glb[2];
61958a175baeSEmil Constantinescu   n_glb  = err_glb[3];
61968a175baeSEmil Constantinescu   na_glb = err_glb[4];
61978a175baeSEmil Constantinescu   nr_glb = err_glb[5];
61988a175baeSEmil Constantinescu 
61998a175baeSEmil Constantinescu   *norm  = 0.;
62008a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
62018a175baeSEmil Constantinescu   *norma = 0.;
62028a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62038a175baeSEmil Constantinescu   *normr = 0.;
62048a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62058a175baeSEmil Constantinescu 
62068a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62078a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62088a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62098a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62108a175baeSEmil Constantinescu }
62118a175baeSEmil Constantinescu 
62128a175baeSEmil Constantinescu /*@
62138a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62148a175baeSEmil Constantinescu    Collective on TS
62158a175baeSEmil Constantinescu 
62168a175baeSEmil Constantinescu    Input Arguments:
62178a175baeSEmil Constantinescu +  ts - time stepping context
62188a175baeSEmil Constantinescu .  E - error vector
62198a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62208a175baeSEmil Constantinescu -  Y - state vector, previous time step
62218a175baeSEmil Constantinescu 
62228a175baeSEmil Constantinescu    Output Arguments:
6223a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62248a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6225a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62268a175baeSEmil Constantinescu 
62278a175baeSEmil Constantinescu    Level: developer
62288a175baeSEmil Constantinescu 
62298a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62308a175baeSEmil Constantinescu @*/
62318a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62328a175baeSEmil Constantinescu {
62338a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62348a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62358a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62368a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
62378a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62388a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
62398a175baeSEmil Constantinescu 
62408a175baeSEmil Constantinescu   PetscFunctionBegin;
62418a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62428a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
62438a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
62448a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
62458a175baeSEmil Constantinescu   PetscValidType(E,2);
62468a175baeSEmil Constantinescu   PetscValidType(U,3);
62478a175baeSEmil Constantinescu   PetscValidType(Y,4);
62488a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
62498a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
62508a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
62518a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
62528a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
62538a175baeSEmil Constantinescu 
62548a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
62558a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
62568a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
62578a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
62588a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62598a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62608a175baeSEmil Constantinescu 
62618a175baeSEmil Constantinescu   max=0.;
62628a175baeSEmil Constantinescu   maxa=0.;
62638a175baeSEmil Constantinescu   maxr=0.;
62648a175baeSEmil Constantinescu 
62658a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
62668a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
62678a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62688a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62698a175baeSEmil Constantinescu 
62708a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
627176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62728a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62738a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62748a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62758a175baeSEmil Constantinescu       tol  = tola+tolr;
62768a175baeSEmil Constantinescu       if(tola>0.){
62778a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62788a175baeSEmil Constantinescu       }
62798a175baeSEmil Constantinescu       if(tolr>0.){
62808a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
62818a175baeSEmil Constantinescu       }
62828a175baeSEmil Constantinescu       if(tol>0.){
62838a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
62848a175baeSEmil Constantinescu       }
62858a175baeSEmil Constantinescu     }
62868a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62878a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62888a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62898a175baeSEmil Constantinescu     const PetscScalar *atol;
62908a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62918a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
629276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62948a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62958a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62968a175baeSEmil Constantinescu       tol  = tola+tolr;
62978a175baeSEmil Constantinescu       if(tola>0.){
62988a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62998a175baeSEmil Constantinescu       }
63008a175baeSEmil Constantinescu       if(tolr>0.){
63018a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63028a175baeSEmil Constantinescu       }
63038a175baeSEmil Constantinescu       if(tol>0.){
63048a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63058a175baeSEmil Constantinescu       }
63068a175baeSEmil Constantinescu     }
63078a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63088a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63098a175baeSEmil Constantinescu     const PetscScalar *rtol;
63108a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63118a175baeSEmil Constantinescu 
63128a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
631376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63148a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63158a175baeSEmil Constantinescu       tola = ts->atol;
63168a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63178a175baeSEmil Constantinescu       tol  = tola+tolr;
63188a175baeSEmil Constantinescu       if(tola>0.){
63198a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63208a175baeSEmil Constantinescu       }
63218a175baeSEmil Constantinescu       if(tolr>0.){
63228a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63238a175baeSEmil Constantinescu       }
63248a175baeSEmil Constantinescu       if(tol>0.){
63258a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63268a175baeSEmil Constantinescu       }
63278a175baeSEmil Constantinescu     }
63288a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63298a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63308a175baeSEmil Constantinescu 
63318a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
633276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63338a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63348a175baeSEmil Constantinescu       tola = ts->atol;
63358a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63368a175baeSEmil Constantinescu       tol  = tola+tolr;
63378a175baeSEmil Constantinescu       if(tola>0.){
63388a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63398a175baeSEmil Constantinescu       }
63408a175baeSEmil Constantinescu       if(tolr>0.){
63418a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63428a175baeSEmil Constantinescu       }
63438a175baeSEmil Constantinescu       if(tol>0.){
63448a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63458a175baeSEmil Constantinescu       }
63468a175baeSEmil Constantinescu     }
63478a175baeSEmil Constantinescu   }
63488a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
63498a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63508a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63518a175baeSEmil Constantinescu   err_loc[0] = max;
63528a175baeSEmil Constantinescu   err_loc[1] = maxa;
63538a175baeSEmil Constantinescu   err_loc[2] = maxr;
6354a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63558a175baeSEmil Constantinescu   gmax   = err_glb[0];
63568a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
63578a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
63588a175baeSEmil Constantinescu 
63598a175baeSEmil Constantinescu   *norm = gmax;
63608a175baeSEmil Constantinescu   *norma = gmaxa;
63618a175baeSEmil Constantinescu   *normr = gmaxr;
63628a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63638a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63648a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63658a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63668a175baeSEmil Constantinescu }
63678a175baeSEmil Constantinescu 
63688a175baeSEmil Constantinescu /*@
63698a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
63708a175baeSEmil Constantinescu 
63718a175baeSEmil Constantinescu    Collective on TS
63728a175baeSEmil Constantinescu 
63738a175baeSEmil Constantinescu    Input Arguments:
63748a175baeSEmil Constantinescu +  ts - time stepping context
63758a175baeSEmil Constantinescu .  E - error vector
63768a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63778a175baeSEmil Constantinescu .  Y - state vector, previous time step
63788a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
63798a175baeSEmil Constantinescu 
63808a175baeSEmil Constantinescu    Output Arguments:
6381a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
63828a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6383a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
63848a175baeSEmil Constantinescu 
63858a175baeSEmil Constantinescu    Options Database Keys:
63868a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
63878a175baeSEmil Constantinescu 
63888a175baeSEmil Constantinescu    Level: developer
63898a175baeSEmil Constantinescu 
63908a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
63918a175baeSEmil Constantinescu @*/
63928a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63938a175baeSEmil Constantinescu {
63948a175baeSEmil Constantinescu   PetscErrorCode ierr;
63958a175baeSEmil Constantinescu 
63968a175baeSEmil Constantinescu   PetscFunctionBegin;
63978a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
63988a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
63998a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
64008a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64018a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64028a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64038a175baeSEmil Constantinescu }
64048a175baeSEmil Constantinescu 
64058a175baeSEmil Constantinescu 
64068d59e960SJed Brown /*@
64078d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64088d59e960SJed Brown 
64098d59e960SJed Brown    Logically Collective on TS
64108d59e960SJed Brown 
64118d59e960SJed Brown    Input Arguments:
64128d59e960SJed Brown +  ts - time stepping context
64138d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64148d59e960SJed Brown 
64158d59e960SJed Brown    Note:
64168d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64178d59e960SJed Brown 
64188d59e960SJed Brown    Level: intermediate
64198d59e960SJed Brown 
64208d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64218d59e960SJed Brown @*/
64228d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64238d59e960SJed Brown {
64248d59e960SJed Brown   PetscFunctionBegin;
64258d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64268d59e960SJed Brown   ts->cfltime_local = cfltime;
64278d59e960SJed Brown   ts->cfltime       = -1.;
64288d59e960SJed Brown   PetscFunctionReturn(0);
64298d59e960SJed Brown }
64308d59e960SJed Brown 
64318d59e960SJed Brown /*@
64328d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
64338d59e960SJed Brown 
64348d59e960SJed Brown    Collective on TS
64358d59e960SJed Brown 
64368d59e960SJed Brown    Input Arguments:
64378d59e960SJed Brown .  ts - time stepping context
64388d59e960SJed Brown 
64398d59e960SJed Brown    Output Arguments:
64408d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
64418d59e960SJed Brown 
64428d59e960SJed Brown    Level: advanced
64438d59e960SJed Brown 
64448d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
64458d59e960SJed Brown @*/
64468d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
64478d59e960SJed Brown {
64488d59e960SJed Brown   PetscErrorCode ierr;
64498d59e960SJed Brown 
64508d59e960SJed Brown   PetscFunctionBegin;
64518d59e960SJed Brown   if (ts->cfltime < 0) {
6452b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64538d59e960SJed Brown   }
64548d59e960SJed Brown   *cfltime = ts->cfltime;
64558d59e960SJed Brown   PetscFunctionReturn(0);
64568d59e960SJed Brown }
64578d59e960SJed Brown 
6458d6ebe24aSShri Abhyankar /*@
6459d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6460d6ebe24aSShri Abhyankar 
6461d6ebe24aSShri Abhyankar    Input Parameters:
6462a2b725a8SWilliam Gropp +  ts   - the TS context.
6463d6ebe24aSShri Abhyankar .  xl   - lower bound.
6464a2b725a8SWilliam Gropp -  xu   - upper bound.
6465d6ebe24aSShri Abhyankar 
6466d6ebe24aSShri Abhyankar    Notes:
6467d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6468e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6469d6ebe24aSShri Abhyankar 
64702bd2b0e6SSatish Balay    Level: advanced
64712bd2b0e6SSatish Balay 
6472d6ebe24aSShri Abhyankar @*/
6473d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6474d6ebe24aSShri Abhyankar {
6475d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6476d6ebe24aSShri Abhyankar   SNES           snes;
6477d6ebe24aSShri Abhyankar 
6478d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6479d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6480d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6481d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6482d6ebe24aSShri Abhyankar }
6483d6ebe24aSShri Abhyankar 
6484b3603a34SBarry Smith /*@C
64854f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6486b3603a34SBarry Smith        in a time based line graph
6487b3603a34SBarry Smith 
6488b3603a34SBarry Smith    Collective on TS
6489b3603a34SBarry Smith 
6490b3603a34SBarry Smith    Input Parameters:
6491b3603a34SBarry Smith +  ts - the TS context
6492b3603a34SBarry Smith .  step - current time-step
6493b3603a34SBarry Smith .  ptime - current time
64947db568b7SBarry Smith .  u - current solution
64957db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6496b3603a34SBarry Smith 
6497b3d3934dSBarry Smith    Options Database:
64989ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6499b3d3934dSBarry Smith 
6500b3603a34SBarry Smith    Level: intermediate
6501b3603a34SBarry Smith 
650295452b02SPatrick Sanan    Notes:
650395452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6504b3603a34SBarry Smith 
65057db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65067db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65077db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65087db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6509b3603a34SBarry Smith @*/
65107db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6511b3603a34SBarry Smith {
6512b3603a34SBarry Smith   PetscErrorCode    ierr;
65137db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6514b3603a34SBarry Smith   const PetscScalar *yy;
651580666b62SBarry Smith   Vec               v;
6516b3603a34SBarry Smith 
6517b3603a34SBarry Smith   PetscFunctionBegin;
651863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
651958ff32f7SBarry Smith   if (!step) {
6520a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65216934998bSLisandro Dalcin     PetscInt      dim;
6522a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65230ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6524bab0a581SBarry Smith     if (!ctx->names) {
6525bab0a581SBarry Smith       PetscBool flg;
6526bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6527bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6528bab0a581SBarry Smith       if (flg) {
6529bab0a581SBarry Smith         PetscInt i,n;
6530bab0a581SBarry Smith         char     **names;
6531bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6532bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6533bab0a581SBarry Smith         for (i=0; i<n; i++) {
6534bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6535bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6536bab0a581SBarry Smith         }
6537bab0a581SBarry Smith         names[n] = NULL;
6538bab0a581SBarry Smith         ctx->names = names;
6539bab0a581SBarry Smith       }
6540bab0a581SBarry Smith     }
6541387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6542387f4636SBarry Smith       char      **displaynames;
6543387f4636SBarry Smith       PetscBool flg;
6544387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6545580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6546c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6547387f4636SBarry Smith       if (flg) {
6548a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6549387f4636SBarry Smith       }
6550387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6551387f4636SBarry Smith     }
6552387f4636SBarry Smith     if (ctx->displaynames) {
6553387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6554387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6555387f4636SBarry Smith     } else if (ctx->names) {
65560910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65570b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6558387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6559b0bc92c6SBarry Smith     } else {
6560b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6561b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6562387f4636SBarry Smith     }
65630b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
656458ff32f7SBarry Smith   }
65656934998bSLisandro Dalcin 
65666934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65676934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
656880666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
65696934998bSLisandro Dalcin   if (ctx->displaynames) {
65706934998bSLisandro Dalcin     PetscInt i;
65716934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
65726934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
65736934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
65746934998bSLisandro Dalcin   } else {
6575e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6576e3efe391SJed Brown     PetscInt  i,n;
65776934998bSLisandro Dalcin     PetscReal *yreal;
657880666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6579785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6580e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
65810b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6582e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6583e3efe391SJed Brown #else
65840b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6585e3efe391SJed Brown #endif
658680666b62SBarry Smith   }
65876934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
65886934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
65896934998bSLisandro Dalcin 
6590b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65910b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65926934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65933923b477SBarry Smith   }
6594b3603a34SBarry Smith   PetscFunctionReturn(0);
6595b3603a34SBarry Smith }
6596b3603a34SBarry Smith 
6597b037adc7SBarry Smith /*@C
659831152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6599b037adc7SBarry Smith 
6600b037adc7SBarry Smith    Collective on TS
6601b037adc7SBarry Smith 
6602b037adc7SBarry Smith    Input Parameters:
6603b037adc7SBarry Smith +  ts - the TS context
6604b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6605b037adc7SBarry Smith 
6606b037adc7SBarry Smith    Level: intermediate
6607b037adc7SBarry Smith 
660895452b02SPatrick Sanan    Notes:
660995452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66107db568b7SBarry Smith 
6611a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6612b037adc7SBarry Smith @*/
661331152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6614b037adc7SBarry Smith {
6615b037adc7SBarry Smith   PetscErrorCode    ierr;
6616b037adc7SBarry Smith   PetscInt          i;
6617b037adc7SBarry Smith 
6618b037adc7SBarry Smith   PetscFunctionBegin;
6619b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6620b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66215537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6622b3d3934dSBarry Smith       break;
6623b3d3934dSBarry Smith     }
6624b3d3934dSBarry Smith   }
6625b3d3934dSBarry Smith   PetscFunctionReturn(0);
6626b3d3934dSBarry Smith }
6627b3d3934dSBarry Smith 
6628e673d494SBarry Smith /*@C
6629e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6630e673d494SBarry Smith 
6631e673d494SBarry Smith    Collective on TS
6632e673d494SBarry Smith 
6633e673d494SBarry Smith    Input Parameters:
6634e673d494SBarry Smith +  ts - the TS context
6635e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6636e673d494SBarry Smith 
6637e673d494SBarry Smith    Level: intermediate
6638e673d494SBarry Smith 
6639a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6640e673d494SBarry Smith @*/
6641e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6642e673d494SBarry Smith {
6643e673d494SBarry Smith   PetscErrorCode    ierr;
6644e673d494SBarry Smith 
6645e673d494SBarry Smith   PetscFunctionBegin;
6646e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6647e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6648e673d494SBarry Smith   PetscFunctionReturn(0);
6649e673d494SBarry Smith }
6650e673d494SBarry Smith 
6651b3d3934dSBarry Smith /*@C
6652b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6653b3d3934dSBarry Smith 
6654b3d3934dSBarry Smith    Collective on TS
6655b3d3934dSBarry Smith 
6656b3d3934dSBarry Smith    Input Parameter:
6657b3d3934dSBarry Smith .  ts - the TS context
6658b3d3934dSBarry Smith 
6659b3d3934dSBarry Smith    Output Parameter:
6660b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6661b3d3934dSBarry Smith 
6662b3d3934dSBarry Smith    Level: intermediate
6663b3d3934dSBarry Smith 
666495452b02SPatrick Sanan    Notes:
666595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66667db568b7SBarry Smith 
6667b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6668b3d3934dSBarry Smith @*/
6669b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6670b3d3934dSBarry Smith {
6671b3d3934dSBarry Smith   PetscInt       i;
6672b3d3934dSBarry Smith 
6673b3d3934dSBarry Smith   PetscFunctionBegin;
6674b3d3934dSBarry Smith   *names = NULL;
6675b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6676b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66775537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6678b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6679b3d3934dSBarry Smith       break;
6680387f4636SBarry Smith     }
6681387f4636SBarry Smith   }
6682387f4636SBarry Smith   PetscFunctionReturn(0);
6683387f4636SBarry Smith }
6684387f4636SBarry Smith 
6685a66092f1SBarry Smith /*@C
6686a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6687a66092f1SBarry Smith 
6688a66092f1SBarry Smith    Collective on TS
6689a66092f1SBarry Smith 
6690a66092f1SBarry Smith    Input Parameters:
6691a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6692a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6693a66092f1SBarry Smith 
6694a66092f1SBarry Smith    Level: intermediate
6695a66092f1SBarry Smith 
6696a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6697a66092f1SBarry Smith @*/
6698a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6699a66092f1SBarry Smith {
6700a66092f1SBarry Smith   PetscInt          j = 0,k;
6701a66092f1SBarry Smith   PetscErrorCode    ierr;
6702a66092f1SBarry Smith 
6703a66092f1SBarry Smith   PetscFunctionBegin;
6704a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6705a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6706a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6707a66092f1SBarry Smith   while (displaynames[j]) j++;
6708a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6709a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6710a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6711a66092f1SBarry Smith   j = 0;
6712a66092f1SBarry Smith   while (displaynames[j]) {
6713a66092f1SBarry Smith     k = 0;
6714a66092f1SBarry Smith     while (ctx->names[k]) {
6715a66092f1SBarry Smith       PetscBool flg;
6716a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6717a66092f1SBarry Smith       if (flg) {
6718a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6719a66092f1SBarry Smith         break;
6720a66092f1SBarry Smith       }
6721a66092f1SBarry Smith       k++;
6722a66092f1SBarry Smith     }
6723a66092f1SBarry Smith     j++;
6724a66092f1SBarry Smith   }
6725a66092f1SBarry Smith   PetscFunctionReturn(0);
6726a66092f1SBarry Smith }
6727a66092f1SBarry Smith 
6728387f4636SBarry Smith /*@C
6729387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6730387f4636SBarry Smith 
6731387f4636SBarry Smith    Collective on TS
6732387f4636SBarry Smith 
6733387f4636SBarry Smith    Input Parameters:
6734387f4636SBarry Smith +  ts - the TS context
6735a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6736387f4636SBarry Smith 
673795452b02SPatrick Sanan    Notes:
673895452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67397db568b7SBarry Smith 
6740387f4636SBarry Smith    Level: intermediate
6741387f4636SBarry Smith 
6742387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6743387f4636SBarry Smith @*/
6744387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6745387f4636SBarry Smith {
6746a66092f1SBarry Smith   PetscInt          i;
6747387f4636SBarry Smith   PetscErrorCode    ierr;
6748387f4636SBarry Smith 
6749387f4636SBarry Smith   PetscFunctionBegin;
6750387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6751387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67525537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6753b3d3934dSBarry Smith       break;
6754b037adc7SBarry Smith     }
6755b037adc7SBarry Smith   }
6756b037adc7SBarry Smith   PetscFunctionReturn(0);
6757b037adc7SBarry Smith }
6758b037adc7SBarry Smith 
675980666b62SBarry Smith /*@C
676080666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
676180666b62SBarry Smith 
676280666b62SBarry Smith    Collective on TS
676380666b62SBarry Smith 
676480666b62SBarry Smith    Input Parameters:
676580666b62SBarry Smith +  ts - the TS context
676680666b62SBarry Smith .  transform - the transform function
67677684fa3eSBarry Smith .  destroy - function to destroy the optional context
676880666b62SBarry Smith -  ctx - optional context used by transform function
676980666b62SBarry Smith 
677095452b02SPatrick Sanan    Notes:
677195452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67727db568b7SBarry Smith 
677380666b62SBarry Smith    Level: intermediate
677480666b62SBarry Smith 
6775a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
677680666b62SBarry Smith @*/
67777684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
677880666b62SBarry Smith {
677980666b62SBarry Smith   PetscInt          i;
6780a66092f1SBarry Smith   PetscErrorCode    ierr;
678180666b62SBarry Smith 
678280666b62SBarry Smith   PetscFunctionBegin;
678380666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
678480666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67855537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
678680666b62SBarry Smith     }
678780666b62SBarry Smith   }
678880666b62SBarry Smith   PetscFunctionReturn(0);
678980666b62SBarry Smith }
679080666b62SBarry Smith 
6791e673d494SBarry Smith /*@C
6792e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6793e673d494SBarry Smith 
6794e673d494SBarry Smith    Collective on TSLGCtx
6795e673d494SBarry Smith 
6796e673d494SBarry Smith    Input Parameters:
6797e673d494SBarry Smith +  ts - the TS context
6798e673d494SBarry Smith .  transform - the transform function
67997684fa3eSBarry Smith .  destroy - function to destroy the optional context
6800e673d494SBarry Smith -  ctx - optional context used by transform function
6801e673d494SBarry Smith 
6802e673d494SBarry Smith    Level: intermediate
6803e673d494SBarry Smith 
6804a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6805e673d494SBarry Smith @*/
68067684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6807e673d494SBarry Smith {
6808e673d494SBarry Smith   PetscFunctionBegin;
6809e673d494SBarry Smith   ctx->transform    = transform;
68107684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6811e673d494SBarry Smith   ctx->transformctx = tctx;
6812e673d494SBarry Smith   PetscFunctionReturn(0);
6813e673d494SBarry Smith }
6814e673d494SBarry Smith 
6815ef20d060SBarry Smith /*@C
68168b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6817ef20d060SBarry Smith        in a time based line graph
6818ef20d060SBarry Smith 
6819ef20d060SBarry Smith    Collective on TS
6820ef20d060SBarry Smith 
6821ef20d060SBarry Smith    Input Parameters:
6822ef20d060SBarry Smith +  ts - the TS context
6823ef20d060SBarry Smith .  step - current time-step
6824ef20d060SBarry Smith .  ptime - current time
68257db568b7SBarry Smith .  u - current solution
68267db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6827ef20d060SBarry Smith 
6828ef20d060SBarry Smith    Level: intermediate
6829ef20d060SBarry Smith 
683095452b02SPatrick Sanan    Notes:
683195452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6832abd5a294SJed Brown 
6833abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6834abd5a294SJed Brown 
6835abd5a294SJed Brown    Options Database Keys:
68364f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6837ef20d060SBarry Smith 
6838abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6839ef20d060SBarry Smith @*/
68400910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6841ef20d060SBarry Smith {
6842ef20d060SBarry Smith   PetscErrorCode    ierr;
68430b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6844ef20d060SBarry Smith   const PetscScalar *yy;
6845ef20d060SBarry Smith   Vec               y;
6846ef20d060SBarry Smith 
6847ef20d060SBarry Smith   PetscFunctionBegin;
6848a9f9c1f6SBarry Smith   if (!step) {
6849a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68506934998bSLisandro Dalcin     PetscInt      dim;
6851a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68528b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68530910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6854a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6855a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6856a9f9c1f6SBarry Smith   }
68570910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6858ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68590910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6860ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6861e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6862e3efe391SJed Brown   {
6863e3efe391SJed Brown     PetscReal *yreal;
6864e3efe391SJed Brown     PetscInt  i,n;
6865e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6866785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6867e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
68680b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6869e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6870e3efe391SJed Brown   }
6871e3efe391SJed Brown #else
68720b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6873e3efe391SJed Brown #endif
6874ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6875ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6876b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68770b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
68786934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
68793923b477SBarry Smith   }
6880ef20d060SBarry Smith   PetscFunctionReturn(0);
6881ef20d060SBarry Smith }
6882ef20d060SBarry Smith 
68835e3b7effSJoseph Pusztay /*@C
68845e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
68855e3b7effSJoseph Pusztay 
68865e3b7effSJoseph Pusztay    Input Parameters:
68875e3b7effSJoseph Pusztay +  ts - the TS context
68885e3b7effSJoseph Pusztay .  step - current time-step
68895e3b7effSJoseph Pusztay .  ptime - current time
68905e3b7effSJoseph Pusztay .  u - current solution
68915e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
68925e3b7effSJoseph Pusztay 
68935e3b7effSJoseph Pusztay    Options Database:
6894918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
68955e3b7effSJoseph Pusztay 
68965e3b7effSJoseph Pusztay    Level: intermediate
68975e3b7effSJoseph Pusztay 
68985e3b7effSJoseph Pusztay @*/
68990ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
69001b575b74SJoseph Pusztay {
69011b575b74SJoseph Pusztay   PetscErrorCode    ierr;
69021b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
69031b575b74SJoseph Pusztay   const PetscScalar *yy;
6904b1670a61SJoseph Pusztay   PetscReal       *y,*x;
69051b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
69061b575b74SJoseph Pusztay   DM                dm;
69071b575b74SJoseph Pusztay 
69081b575b74SJoseph Pusztay   PetscFunctionBegin;
69091b575b74SJoseph Pusztay 
69101b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
69111b575b74SJoseph Pusztay   if (!step) {
69121b575b74SJoseph Pusztay     PetscDrawAxis axis;
69131b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
69141b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6915895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6916895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
69171b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
69181b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
69191b575b74SJoseph Pusztay     if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
69201b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69211b575b74SJoseph Pusztay     Np /= 2*dim;
69221b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69231b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69241b575b74SJoseph Pusztay   }
69251b575b74SJoseph Pusztay 
69261b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69271b575b74SJoseph Pusztay   Np /= 2*dim;
69281b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6929895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69301b575b74SJoseph Pusztay   /* get points from solution vector */
69311b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6932b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6933b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6934895f37d5SJoseph Pusztay   }
69351b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
69361b575b74SJoseph Pusztay 
69371b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69381b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
69391b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
69401b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
69411b575b74SJoseph Pusztay   }
69421b575b74SJoseph Pusztay 
6943918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6944918b1d10SJoseph Pusztay 
69451b575b74SJoseph Pusztay   PetscFunctionReturn(0);
69461b575b74SJoseph Pusztay }
69471b575b74SJoseph Pusztay 
69481b575b74SJoseph Pusztay 
69491b575b74SJoseph Pusztay 
69507cf37e64SBarry Smith /*@C
69517cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69527cf37e64SBarry Smith 
69537cf37e64SBarry Smith    Collective on TS
69547cf37e64SBarry Smith 
69557cf37e64SBarry Smith    Input Parameters:
69567cf37e64SBarry Smith +  ts - the TS context
69577cf37e64SBarry Smith .  step - current time-step
69587cf37e64SBarry Smith .  ptime - current time
69597cf37e64SBarry Smith .  u - current solution
69607cf37e64SBarry Smith -  dctx - unused context
69617cf37e64SBarry Smith 
69627cf37e64SBarry Smith    Level: intermediate
69637cf37e64SBarry Smith 
69647cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
69657cf37e64SBarry Smith 
69667cf37e64SBarry Smith    Options Database Keys:
69677cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
69687cf37e64SBarry Smith 
69697cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
69707cf37e64SBarry Smith @*/
6971edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
69727cf37e64SBarry Smith {
69737cf37e64SBarry Smith   PetscErrorCode    ierr;
69747cf37e64SBarry Smith   Vec               y;
69757cf37e64SBarry Smith   PetscReal         nrm;
6976edbaebb3SBarry Smith   PetscBool         flg;
69777cf37e64SBarry Smith 
69787cf37e64SBarry Smith   PetscFunctionBegin;
69797cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
69807cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69817cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6982edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6983edbaebb3SBarry Smith   if (flg) {
69847cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6985edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6986edbaebb3SBarry Smith   }
6987edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6988edbaebb3SBarry Smith   if (flg) {
6989edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6990edbaebb3SBarry Smith   }
6991edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
69927cf37e64SBarry Smith   PetscFunctionReturn(0);
69937cf37e64SBarry Smith }
69947cf37e64SBarry Smith 
6995201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6996201da799SBarry Smith {
6997201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6998201da799SBarry Smith   PetscReal      x   = ptime,y;
6999201da799SBarry Smith   PetscErrorCode ierr;
7000201da799SBarry Smith   PetscInt       its;
7001201da799SBarry Smith 
7002201da799SBarry Smith   PetscFunctionBegin;
700363e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7004201da799SBarry Smith   if (!n) {
7005201da799SBarry Smith     PetscDrawAxis axis;
7006201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7007201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7008201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7009201da799SBarry Smith     ctx->snes_its = 0;
7010201da799SBarry Smith   }
7011201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7012201da799SBarry Smith   y    = its - ctx->snes_its;
7013201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70143fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7015201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70166934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7017201da799SBarry Smith   }
7018201da799SBarry Smith   ctx->snes_its = its;
7019201da799SBarry Smith   PetscFunctionReturn(0);
7020201da799SBarry Smith }
7021201da799SBarry Smith 
7022201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7023201da799SBarry Smith {
7024201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7025201da799SBarry Smith   PetscReal      x   = ptime,y;
7026201da799SBarry Smith   PetscErrorCode ierr;
7027201da799SBarry Smith   PetscInt       its;
7028201da799SBarry Smith 
7029201da799SBarry Smith   PetscFunctionBegin;
703063e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7031201da799SBarry Smith   if (!n) {
7032201da799SBarry Smith     PetscDrawAxis axis;
7033201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7034201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7035201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7036201da799SBarry Smith     ctx->ksp_its = 0;
7037201da799SBarry Smith   }
7038201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7039201da799SBarry Smith   y    = its - ctx->ksp_its;
7040201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
704199fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7042201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70436934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7044201da799SBarry Smith   }
7045201da799SBarry Smith   ctx->ksp_its = its;
7046201da799SBarry Smith   PetscFunctionReturn(0);
7047201da799SBarry Smith }
7048f9c1d6abSBarry Smith 
7049f9c1d6abSBarry Smith /*@
7050f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7051f9c1d6abSBarry Smith 
7052d083f849SBarry Smith    Collective on TS
7053f9c1d6abSBarry Smith 
7054f9c1d6abSBarry Smith    Input Parameters:
7055f9c1d6abSBarry Smith +  ts - the TS context
7056f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7057f9c1d6abSBarry Smith 
7058f9c1d6abSBarry Smith    Output Parameters:
7059f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7060f9c1d6abSBarry Smith 
7061f9c1d6abSBarry Smith    Level: developer
7062f9c1d6abSBarry Smith 
7063f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7064f9c1d6abSBarry Smith @*/
7065f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7066f9c1d6abSBarry Smith {
7067f9c1d6abSBarry Smith   PetscErrorCode ierr;
7068f9c1d6abSBarry Smith 
7069f9c1d6abSBarry Smith   PetscFunctionBegin;
7070f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7071ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7072f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7073f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7074f9c1d6abSBarry Smith }
707524655328SShri 
7076b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7077b3d3934dSBarry Smith /*@C
7078b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7079b3d3934dSBarry Smith 
7080b3d3934dSBarry Smith    Collective on TS
7081b3d3934dSBarry Smith 
7082b3d3934dSBarry Smith    Input Parameters:
7083b3d3934dSBarry Smith .  ts  - the ODE solver object
7084b3d3934dSBarry Smith 
7085b3d3934dSBarry Smith    Output Parameter:
7086b3d3934dSBarry Smith .  ctx - the context
7087b3d3934dSBarry Smith 
7088b3d3934dSBarry Smith    Level: intermediate
7089b3d3934dSBarry Smith 
7090b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7091b3d3934dSBarry Smith 
7092b3d3934dSBarry Smith @*/
7093b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7094b3d3934dSBarry Smith {
7095b3d3934dSBarry Smith   PetscErrorCode ierr;
7096b3d3934dSBarry Smith 
7097b3d3934dSBarry Smith   PetscFunctionBegin;
7098a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7099b3d3934dSBarry Smith   PetscFunctionReturn(0);
7100b3d3934dSBarry Smith }
7101b3d3934dSBarry Smith 
7102b3d3934dSBarry Smith /*@C
7103b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7104b3d3934dSBarry Smith 
7105b3d3934dSBarry Smith    Collective on TS
7106b3d3934dSBarry Smith 
7107b3d3934dSBarry Smith    Input Parameters:
7108b3d3934dSBarry Smith +  ts - the TS context
7109b3d3934dSBarry Smith .  step - current time-step
7110b3d3934dSBarry Smith .  ptime - current time
71117db568b7SBarry Smith .  u  - current solution
71127db568b7SBarry Smith -  dctx - the envelope context
7113b3d3934dSBarry Smith 
7114b3d3934dSBarry Smith    Options Database:
7115b3d3934dSBarry Smith .  -ts_monitor_envelope
7116b3d3934dSBarry Smith 
7117b3d3934dSBarry Smith    Level: intermediate
7118b3d3934dSBarry Smith 
711995452b02SPatrick Sanan    Notes:
712095452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7121b3d3934dSBarry Smith 
71227db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7123b3d3934dSBarry Smith @*/
71247db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7125b3d3934dSBarry Smith {
7126b3d3934dSBarry Smith   PetscErrorCode       ierr;
71277db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7128b3d3934dSBarry Smith 
7129b3d3934dSBarry Smith   PetscFunctionBegin;
7130b3d3934dSBarry Smith   if (!ctx->max) {
7131b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7132b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7133b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7134b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7135b3d3934dSBarry Smith   } else {
7136b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7137b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7138b3d3934dSBarry Smith   }
7139b3d3934dSBarry Smith   PetscFunctionReturn(0);
7140b3d3934dSBarry Smith }
7141b3d3934dSBarry Smith 
7142b3d3934dSBarry Smith /*@C
7143b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7144b3d3934dSBarry Smith 
7145b3d3934dSBarry Smith    Collective on TS
7146b3d3934dSBarry Smith 
7147b3d3934dSBarry Smith    Input Parameter:
7148b3d3934dSBarry Smith .  ts - the TS context
7149b3d3934dSBarry Smith 
7150b3d3934dSBarry Smith    Output Parameter:
7151b3d3934dSBarry Smith +  max - the maximum values
7152b3d3934dSBarry Smith -  min - the minimum values
7153b3d3934dSBarry Smith 
715495452b02SPatrick Sanan    Notes:
715595452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71567db568b7SBarry Smith 
7157b3d3934dSBarry Smith    Level: intermediate
7158b3d3934dSBarry Smith 
7159b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7160b3d3934dSBarry Smith @*/
7161b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7162b3d3934dSBarry Smith {
7163b3d3934dSBarry Smith   PetscInt i;
7164b3d3934dSBarry Smith 
7165b3d3934dSBarry Smith   PetscFunctionBegin;
7166b3d3934dSBarry Smith   if (max) *max = NULL;
7167b3d3934dSBarry Smith   if (min) *min = NULL;
7168b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7169b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
71705537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7171b3d3934dSBarry Smith       if (max) *max = ctx->max;
7172b3d3934dSBarry Smith       if (min) *min = ctx->min;
7173b3d3934dSBarry Smith       break;
7174b3d3934dSBarry Smith     }
7175b3d3934dSBarry Smith   }
7176b3d3934dSBarry Smith   PetscFunctionReturn(0);
7177b3d3934dSBarry Smith }
7178b3d3934dSBarry Smith 
7179b3d3934dSBarry Smith /*@C
7180b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7181b3d3934dSBarry Smith 
7182b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7183b3d3934dSBarry Smith 
7184b3d3934dSBarry Smith    Input Parameter:
7185b3d3934dSBarry Smith .  ctx - the monitor context
7186b3d3934dSBarry Smith 
7187b3d3934dSBarry Smith    Level: intermediate
7188b3d3934dSBarry Smith 
71897db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7190b3d3934dSBarry Smith @*/
7191b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7192b3d3934dSBarry Smith {
7193b3d3934dSBarry Smith   PetscErrorCode ierr;
7194b3d3934dSBarry Smith 
7195b3d3934dSBarry Smith   PetscFunctionBegin;
7196b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7197b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7198b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7199b3d3934dSBarry Smith   PetscFunctionReturn(0);
7200b3d3934dSBarry Smith }
7201f2dee214SBarry Smith 
720224655328SShri /*@
7203dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7204dcb233daSLisandro Dalcin 
7205dcb233daSLisandro Dalcin    Collective on TS
7206dcb233daSLisandro Dalcin 
7207dcb233daSLisandro Dalcin    Input Parameter:
7208dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7209dcb233daSLisandro Dalcin 
7210dcb233daSLisandro Dalcin    Level: advanced
7211dcb233daSLisandro Dalcin 
7212dcb233daSLisandro Dalcin    Notes:
7213dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7214dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7215dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7216dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7217dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7218dcb233daSLisandro Dalcin    discontinuous source terms).
7219dcb233daSLisandro Dalcin 
7220dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7221dcb233daSLisandro Dalcin @*/
7222dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7223dcb233daSLisandro Dalcin {
7224dcb233daSLisandro Dalcin   PetscFunctionBegin;
7225dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7226dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7227dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7228dcb233daSLisandro Dalcin }
7229dcb233daSLisandro Dalcin 
7230dcb233daSLisandro Dalcin /*@
723124655328SShri    TSRollBack - Rolls back one time step
723224655328SShri 
723324655328SShri    Collective on TS
723424655328SShri 
723524655328SShri    Input Parameter:
723624655328SShri .  ts - the TS context obtained from TSCreate()
723724655328SShri 
723824655328SShri    Level: advanced
723924655328SShri 
724024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
724124655328SShri @*/
724224655328SShri PetscErrorCode  TSRollBack(TS ts)
724324655328SShri {
724424655328SShri   PetscErrorCode ierr;
724524655328SShri 
724624655328SShri   PetscFunctionBegin;
724724655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7248b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
724924655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
725024655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
725124655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
725224655328SShri   ts->ptime = ts->ptime_prev;
7253be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
72542808aa04SLisandro Dalcin   ts->steps--;
7255b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
725624655328SShri   PetscFunctionReturn(0);
725724655328SShri }
7258aeb4809dSShri Abhyankar 
7259ff22ae23SHong Zhang /*@
7260ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7261ff22ae23SHong Zhang 
7262ff22ae23SHong Zhang    Input Parameter:
7263ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7264ff22ae23SHong Zhang 
72650429704eSStefano Zampini    Output Parameters:
72660429704eSStefano Zampini +  ns - the number of stages
72670429704eSStefano Zampini -  Y - the current stage vectors
72680429704eSStefano Zampini 
7269ff22ae23SHong Zhang    Level: advanced
7270ff22ae23SHong Zhang 
72710429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
72720429704eSStefano Zampini 
7273ff22ae23SHong Zhang .seealso: TSCreate()
7274ff22ae23SHong Zhang @*/
7275ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7276ff22ae23SHong Zhang {
7277ff22ae23SHong Zhang   PetscErrorCode ierr;
7278ff22ae23SHong Zhang 
7279ff22ae23SHong Zhang   PetscFunctionBegin;
7280ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
72810429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
72820429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
72830429704eSStefano Zampini   if (!ts->ops->getstages) {
72840429704eSStefano Zampini     if (ns) *ns = 0;
72850429704eSStefano Zampini     if (Y) *Y = NULL;
72860429704eSStefano Zampini   } else {
7287ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7288ff22ae23SHong Zhang   }
7289ff22ae23SHong Zhang   PetscFunctionReturn(0);
7290ff22ae23SHong Zhang }
7291ff22ae23SHong Zhang 
7292847ff0e1SMatthew G. Knepley /*@C
7293847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7294847ff0e1SMatthew G. Knepley 
7295847ff0e1SMatthew G. Knepley   Collective on SNES
7296847ff0e1SMatthew G. Knepley 
7297847ff0e1SMatthew G. Knepley   Input Parameters:
7298847ff0e1SMatthew G. Knepley + ts - the TS context
7299847ff0e1SMatthew G. Knepley . t - current timestep
7300847ff0e1SMatthew G. Knepley . U - state vector
7301847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7302847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7303847ff0e1SMatthew G. Knepley - ctx - an optional user context
7304847ff0e1SMatthew G. Knepley 
7305847ff0e1SMatthew G. Knepley   Output Parameters:
7306847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7307847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7308847ff0e1SMatthew G. Knepley 
7309847ff0e1SMatthew G. Knepley   Level: intermediate
7310847ff0e1SMatthew G. Knepley 
7311847ff0e1SMatthew G. Knepley   Notes:
7312847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7313847ff0e1SMatthew G. Knepley 
7314847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7315847ff0e1SMatthew G. Knepley 
7316847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7317847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7318847ff0e1SMatthew G. Knepley 
7319847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7320847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7321847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7322847ff0e1SMatthew G. Knepley 
7323847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7324847ff0e1SMatthew G. Knepley @*/
7325847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7326847ff0e1SMatthew G. Knepley {
7327847ff0e1SMatthew G. Knepley   SNES           snes;
7328847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7329847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7330847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7331847ff0e1SMatthew G. Knepley 
7332847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7333c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7334847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7335847ff0e1SMatthew G. Knepley   if (!color) {
7336847ff0e1SMatthew G. Knepley     DM         dm;
7337847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7338847ff0e1SMatthew G. Knepley 
7339847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7340847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7341847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7342847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7343847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7344847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7345847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7346847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7347847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7348847ff0e1SMatthew G. Knepley     } else {
7349847ff0e1SMatthew G. Knepley       MatColoring mc;
7350847ff0e1SMatthew G. Knepley 
7351847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7352847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7353847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7354847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7355847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7356847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7357847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7358847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7359847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7360847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7361847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7362847ff0e1SMatthew G. Knepley     }
7363847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7364847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7365847ff0e1SMatthew G. Knepley   }
7366847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7367847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7368847ff0e1SMatthew G. Knepley   if (J != B) {
7369847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7370847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7371847ff0e1SMatthew G. Knepley   }
7372847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7373847ff0e1SMatthew G. Knepley }
737493b34091SDebojyoti Ghosh 
7375cb9d8021SPierre Barbier de Reuille /*@
73766bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7377cb9d8021SPierre Barbier de Reuille 
7378cb9d8021SPierre Barbier de Reuille     Input Parameters:
73796bc98fa9SBarry Smith +    ts - the TS context
73806bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
73816bc98fa9SBarry Smith 
73826bc98fa9SBarry Smith     Calling sequence of func:
73836bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
73846bc98fa9SBarry Smith 
73856bc98fa9SBarry Smith +   ts - the TS context
73866bc98fa9SBarry Smith .   time - the current time (of the stage)
73876bc98fa9SBarry Smith .   state - the state to check if it is valid
73886bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7389cb9d8021SPierre Barbier de Reuille 
7390cb9d8021SPierre Barbier de Reuille     Level: intermediate
7391cb9d8021SPierre Barbier de Reuille 
73926bc98fa9SBarry Smith     Notes:
73936bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
73946bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
73956bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
73966bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
73976bc98fa9SBarry Smith 
73986bc98fa9SBarry Smith     Developer Notes:
73996bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
74006bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
74016bc98fa9SBarry Smith 
74026bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7403cb9d8021SPierre Barbier de Reuille @*/
7404cb9d8021SPierre Barbier de Reuille 
7405d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7406cb9d8021SPierre Barbier de Reuille {
7407cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7408cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7409cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7410cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7411cb9d8021SPierre Barbier de Reuille }
7412cb9d8021SPierre Barbier de Reuille 
7413cb9d8021SPierre Barbier de Reuille /*@
74146bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7415cb9d8021SPierre Barbier de Reuille 
7416cb9d8021SPierre Barbier de Reuille     Input Parameters:
74176bc98fa9SBarry Smith +    ts - the TS context
74186bc98fa9SBarry Smith .    stagetime - time of the simulation
74196bc98fa9SBarry Smith -    Y - state vector to check.
7420cb9d8021SPierre Barbier de Reuille 
7421cb9d8021SPierre Barbier de Reuille     Output Parameter:
74226bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7423cb9d8021SPierre Barbier de Reuille 
7424cb9d8021SPierre Barbier de Reuille     Note:
74256bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74266bc98fa9SBarry Smith     to check if the current state is valid.
742796a0c994SBarry Smith 
74286bc98fa9SBarry Smith     Level: developer
74296bc98fa9SBarry Smith 
74306bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7431cb9d8021SPierre Barbier de Reuille @*/
7432d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7433cb9d8021SPierre Barbier de Reuille {
7434cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7435cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7436cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7437cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7438d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7439cb9d8021SPierre Barbier de Reuille   }
7440cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7441cb9d8021SPierre Barbier de Reuille }
74421ceb14c0SBarry Smith 
744393b34091SDebojyoti Ghosh /*@C
7444e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
744593b34091SDebojyoti Ghosh 
7446d083f849SBarry Smith   Collective
744793b34091SDebojyoti Ghosh 
744893b34091SDebojyoti Ghosh   Input Parameter:
744993b34091SDebojyoti Ghosh . tsin    - The input TS
745093b34091SDebojyoti Ghosh 
745193b34091SDebojyoti Ghosh   Output Parameter:
7452e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
745393b34091SDebojyoti Ghosh 
74545eca1a21SEmil Constantinescu   Notes:
74555eca1a21SEmil Constantinescu   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.
74565eca1a21SEmil Constantinescu 
7457928bb9adSStefano Zampini   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);
74585eca1a21SEmil Constantinescu 
74595eca1a21SEmil Constantinescu   Level: developer
746093b34091SDebojyoti Ghosh 
7461e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
746293b34091SDebojyoti Ghosh @*/
7463baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
746493b34091SDebojyoti Ghosh {
746593b34091SDebojyoti Ghosh   TS             t;
746693b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7467dc846ba4SSatish Balay   SNES           snes_start;
7468dc846ba4SSatish Balay   DM             dm;
7469dc846ba4SSatish Balay   TSType         type;
747093b34091SDebojyoti Ghosh 
747193b34091SDebojyoti Ghosh   PetscFunctionBegin;
747293b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
747393b34091SDebojyoti Ghosh   *tsout = NULL;
747493b34091SDebojyoti Ghosh 
74757a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
747693b34091SDebojyoti Ghosh 
747793b34091SDebojyoti Ghosh   /* General TS description */
747893b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
747993b34091SDebojyoti Ghosh   t->setupcalled       = 0;
748093b34091SDebojyoti Ghosh   t->ksp_its           = 0;
748193b34091SDebojyoti Ghosh   t->snes_its          = 0;
748293b34091SDebojyoti Ghosh   t->nwork             = 0;
74837d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
748493b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
748593b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
748693b34091SDebojyoti Ghosh 
748734561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
748834561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7489d15a3a53SEmil Constantinescu 
749093b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
749193b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
749293b34091SDebojyoti Ghosh 
749393b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
749451699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
749593b34091SDebojyoti Ghosh 
7496e7069c78SShri   t->trajectory = tsin->trajectory;
7497e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7498e7069c78SShri 
7499e7069c78SShri   t->event = tsin->event;
75006b10a48eSSatish Balay   if (t->event) t->event->refct++;
7501e7069c78SShri 
750293b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
750393b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7504e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
750593b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
750693b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
750793b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
750893b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
750993b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
751093b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
751193b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
751293b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
751393b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
751493b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
751593b34091SDebojyoti Ghosh 
751693b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
751793b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
751893b34091SDebojyoti Ghosh 
751993b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
752093b34091SDebojyoti Ghosh 
752193b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
752293b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
752393b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
752493b34091SDebojyoti Ghosh 
752593b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
752693b34091SDebojyoti Ghosh 
75270d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75280d4fed19SBarry Smith     PetscInt i;
75290d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75300d4fed19SBarry Smith     for (i=0; i<10; i++) {
75310d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75320d4fed19SBarry Smith     }
75330d4fed19SBarry Smith   }
753493b34091SDebojyoti Ghosh   *tsout = t;
753593b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
753693b34091SDebojyoti Ghosh }
7537f3b1f45cSBarry Smith 
7538f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7539f3b1f45cSBarry Smith {
7540f3b1f45cSBarry Smith   PetscErrorCode ierr;
7541f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7542f3b1f45cSBarry Smith 
7543f3b1f45cSBarry Smith   PetscFunctionBegin;
7544f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7545f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7546f3b1f45cSBarry Smith }
7547f3b1f45cSBarry Smith 
7548f3b1f45cSBarry Smith /*@
7549f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7550f3b1f45cSBarry Smith 
7551d083f849SBarry Smith    Logically Collective on TS
7552f3b1f45cSBarry Smith 
7553f3b1f45cSBarry Smith     Input Parameters:
7554f3b1f45cSBarry Smith     TS - the time stepping routine
7555f3b1f45cSBarry Smith 
7556f3b1f45cSBarry Smith    Output Parameter:
7557f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7558f3b1f45cSBarry Smith 
7559f3b1f45cSBarry Smith    Options Database:
7560f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7561f3b1f45cSBarry Smith 
7562f3b1f45cSBarry Smith    Level: advanced
7563f3b1f45cSBarry Smith 
756495452b02SPatrick Sanan    Notes:
756595452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7566f3b1f45cSBarry Smith 
7567f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7568f3b1f45cSBarry Smith @*/
7569f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7570f3b1f45cSBarry Smith {
7571f3b1f45cSBarry Smith   Mat            J,B;
7572f3b1f45cSBarry Smith   PetscErrorCode ierr;
7573f3b1f45cSBarry Smith   TSRHSJacobian  func;
7574f3b1f45cSBarry Smith   void*          ctx;
7575f3b1f45cSBarry Smith 
7576f3b1f45cSBarry Smith   PetscFunctionBegin;
7577f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7578f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7579f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7580f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7581f3b1f45cSBarry Smith }
7582f3b1f45cSBarry Smith 
7583f3b1f45cSBarry Smith /*@C
7584f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7585f3b1f45cSBarry Smith 
7586d083f849SBarry Smith    Logically Collective on TS
7587f3b1f45cSBarry Smith 
7588f3b1f45cSBarry Smith     Input Parameters:
7589f3b1f45cSBarry Smith     TS - the time stepping routine
7590f3b1f45cSBarry Smith 
7591f3b1f45cSBarry Smith    Output Parameter:
7592f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7593f3b1f45cSBarry Smith 
7594f3b1f45cSBarry Smith    Options Database:
7595f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7596f3b1f45cSBarry Smith 
759795452b02SPatrick Sanan    Notes:
759895452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7599f3b1f45cSBarry Smith 
7600f3b1f45cSBarry Smith    Level: advanced
7601f3b1f45cSBarry Smith 
7602f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7603f3b1f45cSBarry Smith @*/
7604f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7605f3b1f45cSBarry Smith {
7606f3b1f45cSBarry Smith   Mat            J,B;
7607f3b1f45cSBarry Smith   PetscErrorCode ierr;
7608f3b1f45cSBarry Smith   void           *ctx;
7609f3b1f45cSBarry Smith   TSRHSJacobian  func;
7610f3b1f45cSBarry Smith 
7611f3b1f45cSBarry Smith   PetscFunctionBegin;
7612f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7613f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7614f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7615f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7616f3b1f45cSBarry Smith }
76170fe4d17eSHong Zhang 
76180fe4d17eSHong Zhang /*@
76190fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
76200fe4d17eSHong Zhang 
76210fe4d17eSHong Zhang   Logically collective
76220fe4d17eSHong Zhang 
76230fe4d17eSHong Zhang   Input Parameter:
76240fe4d17eSHong Zhang +  ts - timestepping context
76250fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76260fe4d17eSHong Zhang 
76270fe4d17eSHong Zhang   Options Database:
76280fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76290fe4d17eSHong Zhang 
76300fe4d17eSHong Zhang   Notes:
76310fe4d17eSHong Zhang     This is only useful for multirate methods
76320fe4d17eSHong Zhang 
76330fe4d17eSHong Zhang   Level: intermediate
76340fe4d17eSHong Zhang 
76350fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
76360fe4d17eSHong Zhang @*/
76370fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
76380fe4d17eSHong Zhang {
76390fe4d17eSHong Zhang   PetscFunctionBegin;
76400fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76410fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
76420fe4d17eSHong Zhang   PetscFunctionReturn(0);
76430fe4d17eSHong Zhang }
76440fe4d17eSHong Zhang 
76450fe4d17eSHong Zhang /*@
76460fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
76470fe4d17eSHong Zhang 
76480fe4d17eSHong Zhang   Not collective
76490fe4d17eSHong Zhang 
76500fe4d17eSHong Zhang   Input Parameter:
76510fe4d17eSHong Zhang .  ts - timestepping context
76520fe4d17eSHong Zhang 
76530fe4d17eSHong Zhang   Output Parameter:
76540fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76550fe4d17eSHong Zhang 
76560fe4d17eSHong Zhang   Level: intermediate
76570fe4d17eSHong Zhang 
76580fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
76590fe4d17eSHong Zhang @*/
76600fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
76610fe4d17eSHong Zhang {
76620fe4d17eSHong Zhang   PetscFunctionBegin;
76630fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76640fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
76650fe4d17eSHong Zhang   PetscFunctionReturn(0);
76660fe4d17eSHong Zhang }
7667