xref: /petsc/src/ts/interface/ts.c (revision 1a638600ef47b7915145763b823e18b264643d8c)
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 
14feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
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 
1855180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&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);
1946ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,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);
2046ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,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);
2536934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,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);
2710ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"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");
2856934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,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);
2990ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"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);
3090ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"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;
31491b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);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 
331d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&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) {
351d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,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   }
35651b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&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);
37451b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,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;
395847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);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 
44478fbdcc8SBarry Smith    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);
569b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);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) {
6216cd88445SBarry Smith     PetscBool missing;
62294ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
623a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
624d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
625a7a1495cSBarry Smith     PetscStackPop;
62694ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6276cd88445SBarry Smith     if (A) {
6286cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6296cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
6306cd88445SBarry Smith     }
6316cd88445SBarry Smith     if (B && B != A) {
6326cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6336cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
6346cd88445SBarry Smith     }
635ef66eb69SBarry Smith   } else {
63694ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
637971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
638ef66eb69SBarry Smith   }
6390e4ef248SJed Brown   ts->rhsjacobian.time  = t;
640971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
641971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6427f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
64359e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
644a7a1495cSBarry Smith   PetscFunctionReturn(0);
645a7a1495cSBarry Smith }
646a7a1495cSBarry Smith 
647316643e7SJed Brown /*@
648d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
649d763cef2SBarry Smith 
650d083f849SBarry Smith    Collective on TS
651316643e7SJed Brown 
652316643e7SJed Brown    Input Parameters:
653316643e7SJed Brown +  ts - the TS context
654316643e7SJed Brown .  t - current time
6550910c330SBarry Smith -  U - state vector
656316643e7SJed Brown 
657316643e7SJed Brown    Output Parameter:
658316643e7SJed Brown .  y - right hand side
659316643e7SJed Brown 
660316643e7SJed Brown    Note:
661316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
662316643e7SJed Brown    is used internally within the nonlinear solvers.
663316643e7SJed Brown 
664316643e7SJed Brown    Level: developer
665316643e7SJed Brown 
666316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
667316643e7SJed Brown @*/
6680910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
669d763cef2SBarry Smith {
670dfbe8321SBarry Smith   PetscErrorCode ierr;
67124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
67224989b8cSPeter Brune   TSIFunction    ifunction;
67324989b8cSPeter Brune   void           *ctx;
67424989b8cSPeter Brune   DM             dm;
67524989b8cSPeter Brune 
676d763cef2SBarry Smith   PetscFunctionBegin;
6770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6780910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6790700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
68024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
68124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6820298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
683d763cef2SBarry Smith 
684ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
685d763cef2SBarry Smith 
6860910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
68724989b8cSPeter Brune   if (rhsfunction) {
6881be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
689d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6900910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
691d763cef2SBarry Smith     PetscStackPop;
6921be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
693214bc6a2SJed Brown   } else {
694214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
695b2cd27e8SJed Brown   }
69644a41b28SSean Farley 
6970910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
698d763cef2SBarry Smith   PetscFunctionReturn(0);
699d763cef2SBarry Smith }
700d763cef2SBarry Smith 
701ef20d060SBarry Smith /*@
702ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
703ef20d060SBarry Smith 
704d083f849SBarry Smith    Collective on TS
705ef20d060SBarry Smith 
706ef20d060SBarry Smith    Input Parameters:
707ef20d060SBarry Smith +  ts - the TS context
708ef20d060SBarry Smith -  t - current time
709ef20d060SBarry Smith 
710ef20d060SBarry Smith    Output Parameter:
7110910c330SBarry Smith .  U - the solution
712ef20d060SBarry Smith 
713ef20d060SBarry Smith    Note:
714ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
715ef20d060SBarry Smith    is used internally within the nonlinear solvers.
716ef20d060SBarry Smith 
717ef20d060SBarry Smith    Level: developer
718ef20d060SBarry Smith 
719abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
720ef20d060SBarry Smith @*/
7210910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
722ef20d060SBarry Smith {
723ef20d060SBarry Smith   PetscErrorCode     ierr;
724ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
725ef20d060SBarry Smith   void               *ctx;
726ef20d060SBarry Smith   DM                 dm;
727ef20d060SBarry Smith 
728ef20d060SBarry Smith   PetscFunctionBegin;
729ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7300910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
731ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
732ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
733ef20d060SBarry Smith 
734ef20d060SBarry Smith   if (solutionfunction) {
7359b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7360910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
737ef20d060SBarry Smith     PetscStackPop;
738ef20d060SBarry Smith   }
739ef20d060SBarry Smith   PetscFunctionReturn(0);
740ef20d060SBarry Smith }
7419b7cd975SBarry Smith /*@
7429b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7439b7cd975SBarry Smith 
744d083f849SBarry Smith    Collective on TS
7459b7cd975SBarry Smith 
7469b7cd975SBarry Smith    Input Parameters:
7479b7cd975SBarry Smith +  ts - the TS context
7489b7cd975SBarry Smith -  t - current time
7499b7cd975SBarry Smith 
7509b7cd975SBarry Smith    Output Parameter:
7519b7cd975SBarry Smith .  U - the function value
7529b7cd975SBarry Smith 
7539b7cd975SBarry Smith    Note:
7549b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7559b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7569b7cd975SBarry Smith 
7579b7cd975SBarry Smith    Level: developer
7589b7cd975SBarry Smith 
7599b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7609b7cd975SBarry Smith @*/
7619b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7629b7cd975SBarry Smith {
7639b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7649b7cd975SBarry Smith   void               *ctx;
7659b7cd975SBarry Smith   DM                 dm;
7669b7cd975SBarry Smith 
7679b7cd975SBarry Smith   PetscFunctionBegin;
7689b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7699b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7709b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7719b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7729b7cd975SBarry Smith 
7739b7cd975SBarry Smith   if (forcing) {
7749b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7759b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7769b7cd975SBarry Smith     PetscStackPop;
7779b7cd975SBarry Smith   }
7789b7cd975SBarry Smith   PetscFunctionReturn(0);
7799b7cd975SBarry Smith }
780ef20d060SBarry Smith 
781214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
782214bc6a2SJed Brown {
7832dd45cf8SJed Brown   Vec            F;
784214bc6a2SJed Brown   PetscErrorCode ierr;
785214bc6a2SJed Brown 
786214bc6a2SJed Brown   PetscFunctionBegin;
7870298fd71SBarry Smith   *Frhs = NULL;
7880298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
789214bc6a2SJed Brown   if (!ts->Frhs) {
7902dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
791214bc6a2SJed Brown   }
792214bc6a2SJed Brown   *Frhs = ts->Frhs;
793214bc6a2SJed Brown   PetscFunctionReturn(0);
794214bc6a2SJed Brown }
795214bc6a2SJed Brown 
796971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
797214bc6a2SJed Brown {
798214bc6a2SJed Brown   Mat            A,B;
7992dd45cf8SJed Brown   PetscErrorCode ierr;
80041a1d4d2SBarry Smith   TSIJacobian    ijacobian;
801214bc6a2SJed Brown 
802214bc6a2SJed Brown   PetscFunctionBegin;
803c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
804c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
80541a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
806214bc6a2SJed Brown   if (Arhs) {
807214bc6a2SJed Brown     if (!ts->Arhs) {
80841a1d4d2SBarry Smith       if (ijacobian) {
809214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
81041a1d4d2SBarry Smith       } else {
81141a1d4d2SBarry Smith         ts->Arhs = A;
81241a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
81341a1d4d2SBarry Smith       }
8143565c898SBarry Smith     } else {
8153565c898SBarry Smith       PetscBool flg;
8163565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8173565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8183565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8193565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8203565c898SBarry Smith         ts->Arhs = A;
8213565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8223565c898SBarry Smith       }
823214bc6a2SJed Brown     }
824214bc6a2SJed Brown     *Arhs = ts->Arhs;
825214bc6a2SJed Brown   }
826214bc6a2SJed Brown   if (Brhs) {
827214bc6a2SJed Brown     if (!ts->Brhs) {
828bdb70873SJed Brown       if (A != B) {
82941a1d4d2SBarry Smith         if (ijacobian) {
830214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
831bdb70873SJed Brown         } else {
83241a1d4d2SBarry Smith           ts->Brhs = B;
83341a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
83441a1d4d2SBarry Smith         }
83541a1d4d2SBarry Smith       } else {
836bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
83751699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
838bdb70873SJed Brown       }
839214bc6a2SJed Brown     }
840214bc6a2SJed Brown     *Brhs = ts->Brhs;
841214bc6a2SJed Brown   }
842214bc6a2SJed Brown   PetscFunctionReturn(0);
843214bc6a2SJed Brown }
844214bc6a2SJed Brown 
845316643e7SJed Brown /*@
8460910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
847316643e7SJed Brown 
848d083f849SBarry Smith    Collective on TS
849316643e7SJed Brown 
850316643e7SJed Brown    Input Parameters:
851316643e7SJed Brown +  ts - the TS context
852316643e7SJed Brown .  t - current time
8530910c330SBarry Smith .  U - state vector
8540910c330SBarry Smith .  Udot - time derivative of state vector
855214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
856316643e7SJed Brown 
857316643e7SJed Brown    Output Parameter:
858316643e7SJed Brown .  Y - right hand side
859316643e7SJed Brown 
860316643e7SJed Brown    Note:
861316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
862316643e7SJed Brown    is used internally within the nonlinear solvers.
863316643e7SJed Brown 
864316643e7SJed Brown    If the user did did not write their equations in implicit form, this
865316643e7SJed Brown    function recasts them in implicit form.
866316643e7SJed Brown 
867316643e7SJed Brown    Level: developer
868316643e7SJed Brown 
869316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
870316643e7SJed Brown @*/
8710910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
872316643e7SJed Brown {
873316643e7SJed Brown   PetscErrorCode ierr;
87424989b8cSPeter Brune   TSIFunction    ifunction;
87524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
87624989b8cSPeter Brune   void           *ctx;
87724989b8cSPeter Brune   DM             dm;
878316643e7SJed Brown 
879316643e7SJed Brown   PetscFunctionBegin;
8800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8810910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8820910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8830700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
884316643e7SJed Brown 
88524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8870298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
88824989b8cSPeter Brune 
889ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
890d90be118SSean Farley 
8910910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
89224989b8cSPeter Brune   if (ifunction) {
893316643e7SJed Brown     PetscStackPush("TS user implicit function");
8940910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
895316643e7SJed Brown     PetscStackPop;
896214bc6a2SJed Brown   }
897214bc6a2SJed Brown   if (imex) {
89824989b8cSPeter Brune     if (!ifunction) {
8990910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
9002dd45cf8SJed Brown     }
90124989b8cSPeter Brune   } else if (rhsfunction) {
90224989b8cSPeter Brune     if (ifunction) {
903214bc6a2SJed Brown       Vec Frhs;
904214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9050910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
906214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9072dd45cf8SJed Brown     } else {
9080910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9090910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
910316643e7SJed Brown     }
9114a6899ffSJed Brown   }
9120910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
913316643e7SJed Brown   PetscFunctionReturn(0);
914316643e7SJed Brown }
915316643e7SJed Brown 
916316643e7SJed Brown /*@
917316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
918316643e7SJed Brown 
919d083f849SBarry Smith    Collective on TS
920316643e7SJed Brown 
921316643e7SJed Brown    Input
922316643e7SJed Brown       Input Parameters:
923316643e7SJed Brown +  ts - the TS context
924316643e7SJed Brown .  t - current timestep
9250910c330SBarry Smith .  U - state vector
9260910c330SBarry Smith .  Udot - time derivative of state vector
927214bc6a2SJed Brown .  shift - shift to apply, see note below
928214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
929316643e7SJed Brown 
930316643e7SJed Brown    Output Parameters:
931316643e7SJed Brown +  A - Jacobian matrix
9323565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
933316643e7SJed Brown 
934316643e7SJed Brown    Notes:
9350910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
936316643e7SJed Brown 
9370910c330SBarry Smith    dF/dU + shift*dF/dUdot
938316643e7SJed Brown 
939316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
940316643e7SJed Brown    is used internally within the nonlinear solvers.
941316643e7SJed Brown 
942316643e7SJed Brown    Level: developer
943316643e7SJed Brown 
944316643e7SJed Brown .seealso:  TSSetIJacobian()
94563495f91SJed Brown @*/
946d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
947316643e7SJed Brown {
948316643e7SJed Brown   PetscErrorCode ierr;
94924989b8cSPeter Brune   TSIJacobian    ijacobian;
95024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
95124989b8cSPeter Brune   DM             dm;
95224989b8cSPeter Brune   void           *ctx;
953316643e7SJed Brown 
954316643e7SJed Brown   PetscFunctionBegin;
9550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9560910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9570910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
958316643e7SJed Brown   PetscValidPointer(A,6);
95994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
960316643e7SJed Brown   PetscValidPointer(B,7);
96194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
96224989b8cSPeter Brune 
96324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9650298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96624989b8cSPeter Brune 
967ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
968316643e7SJed Brown 
96994ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
97024989b8cSPeter Brune   if (ijacobian) {
9716cd88445SBarry Smith     PetscBool missing;
972316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
973d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
974316643e7SJed Brown     PetscStackPop;
9756cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9766cd88445SBarry Smith     if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
9773565c898SBarry Smith     if (B != A) {
9786cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9796cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
9806cd88445SBarry Smith     }
9814a6899ffSJed Brown   }
982214bc6a2SJed Brown   if (imex) {
983b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9844c26be97Sstefano_zampini       PetscBool assembled;
985971015bcSStefano Zampini       if (rhsjacobian) {
986971015bcSStefano Zampini         Mat Arhs = NULL;
987971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
988971015bcSStefano Zampini         if (A == Arhs) {
989971015bcSStefano Zampini           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant");
990971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
991971015bcSStefano Zampini         }
992971015bcSStefano Zampini       }
99394ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9944c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9954c26be97Sstefano_zampini       if (!assembled) {
9964c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9974c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9984c26be97Sstefano_zampini       }
99994ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
100094ab13aaSBarry Smith       if (A != B) {
100194ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
10024c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
10034c26be97Sstefano_zampini         if (!assembled) {
10044c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10054c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10064c26be97Sstefano_zampini         }
100794ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1008214bc6a2SJed Brown       }
1009214bc6a2SJed Brown     }
1010214bc6a2SJed Brown   } else {
1011e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1012e1244c69SJed Brown     if (rhsjacobian) {
1013214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1014d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1015e1244c69SJed Brown     }
101694ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10173565c898SBarry Smith       PetscBool flg;
1018e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1019e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10203565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10213565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10223565c898SBarry Smith       if (!flg) {
102394ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
102494ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10253565c898SBarry Smith       }
102694ab13aaSBarry Smith       if (A != B) {
102794ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
102894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1029316643e7SJed Brown       }
1030e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1031e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1032e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
103394ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
103494ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
103594ab13aaSBarry Smith         if (A != B) {
103694ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
103794ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1038214bc6a2SJed Brown         }
1039316643e7SJed Brown       }
104094ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
104194ab13aaSBarry Smith       if (A != B) {
104294ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1043316643e7SJed Brown       }
1044316643e7SJed Brown     }
1045316643e7SJed Brown   }
104694ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1047316643e7SJed Brown   PetscFunctionReturn(0);
1048316643e7SJed Brown }
1049316643e7SJed Brown 
1050d763cef2SBarry Smith /*@C
1051d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1052b5abc632SBarry Smith     where U_t = G(t,u).
1053d763cef2SBarry Smith 
10543f9fe445SBarry Smith     Logically Collective on TS
1055d763cef2SBarry Smith 
1056d763cef2SBarry Smith     Input Parameters:
1057d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10580298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1059d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1060d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10610298fd71SBarry Smith           function evaluation routine (may be NULL)
1062d763cef2SBarry Smith 
1063d763cef2SBarry Smith     Calling sequence of func:
10646bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1065d763cef2SBarry Smith 
1066d763cef2SBarry Smith +   t - current timestep
1067d763cef2SBarry Smith .   u - input vector
1068d763cef2SBarry Smith .   F - function vector
1069d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1070d763cef2SBarry Smith 
1071d763cef2SBarry Smith     Level: beginner
1072d763cef2SBarry Smith 
107395452b02SPatrick Sanan     Notes:
107495452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10752bbac0d3SBarry Smith 
1076ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1077d763cef2SBarry Smith @*/
1078089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1079d763cef2SBarry Smith {
1080089b2837SJed Brown   PetscErrorCode ierr;
1081089b2837SJed Brown   SNES           snes;
10820298fd71SBarry Smith   Vec            ralloc = NULL;
108324989b8cSPeter Brune   DM             dm;
1084d763cef2SBarry Smith 
1085089b2837SJed Brown   PetscFunctionBegin;
10860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1087ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
108824989b8cSPeter Brune 
108924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
109024989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1091089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1092e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1093e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1094e856ceecSJed Brown     r = ralloc;
1095e856ceecSJed Brown   }
1096089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1097e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1098d763cef2SBarry Smith   PetscFunctionReturn(0);
1099d763cef2SBarry Smith }
1100d763cef2SBarry Smith 
1101ef20d060SBarry Smith /*@C
1102abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1103ef20d060SBarry Smith 
1104ef20d060SBarry Smith     Logically Collective on TS
1105ef20d060SBarry Smith 
1106ef20d060SBarry Smith     Input Parameters:
1107ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1108ef20d060SBarry Smith .   f - routine for evaluating the solution
1109ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11100298fd71SBarry Smith           function evaluation routine (may be NULL)
1111ef20d060SBarry Smith 
1112ef20d060SBarry Smith     Calling sequence of func:
11136bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec u,void *ctx);
1114ef20d060SBarry Smith 
1115ef20d060SBarry Smith +   t - current timestep
1116ef20d060SBarry Smith .   u - output vector
1117ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1118ef20d060SBarry Smith 
11190ed3bfb6SBarry Smith     Options Database:
11200ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11210ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11220ed3bfb6SBarry Smith 
1123abd5a294SJed Brown     Notes:
1124abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1125abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1126abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1127abd5a294SJed Brown 
11284f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1129abd5a294SJed Brown 
1130ef20d060SBarry Smith     Level: beginner
1131ef20d060SBarry Smith 
11320ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1133ef20d060SBarry Smith @*/
1134ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1135ef20d060SBarry Smith {
1136ef20d060SBarry Smith   PetscErrorCode ierr;
1137ef20d060SBarry Smith   DM             dm;
1138ef20d060SBarry Smith 
1139ef20d060SBarry Smith   PetscFunctionBegin;
1140ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1141ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1142ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1143ef20d060SBarry Smith   PetscFunctionReturn(0);
1144ef20d060SBarry Smith }
1145ef20d060SBarry Smith 
11469b7cd975SBarry Smith /*@C
11479b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11489b7cd975SBarry Smith 
11499b7cd975SBarry Smith     Logically Collective on TS
11509b7cd975SBarry Smith 
11519b7cd975SBarry Smith     Input Parameters:
11529b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1153e162b725SBarry Smith .   func - routine for evaluating the forcing function
11549b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11550298fd71SBarry Smith           function evaluation routine (may be NULL)
11569b7cd975SBarry Smith 
11579b7cd975SBarry Smith     Calling sequence of func:
11586bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11599b7cd975SBarry Smith 
11609b7cd975SBarry Smith +   t - current timestep
1161e162b725SBarry Smith .   f - output vector
11629b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11639b7cd975SBarry Smith 
11649b7cd975SBarry Smith     Notes:
11659b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1166e162b725SBarry 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
1167e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1168e162b725SBarry Smith 
1169e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1170e162b725SBarry Smith 
1171e162b725SBarry 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
1172e162b725SBarry Smith     parameters can be passed in the ctx variable.
11739b7cd975SBarry Smith 
11749b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11759b7cd975SBarry Smith 
11769b7cd975SBarry Smith     Level: beginner
11779b7cd975SBarry Smith 
11789b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11799b7cd975SBarry Smith @*/
1180e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11819b7cd975SBarry Smith {
11829b7cd975SBarry Smith   PetscErrorCode ierr;
11839b7cd975SBarry Smith   DM             dm;
11849b7cd975SBarry Smith 
11859b7cd975SBarry Smith   PetscFunctionBegin;
11869b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11879b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1188e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11899b7cd975SBarry Smith   PetscFunctionReturn(0);
11909b7cd975SBarry Smith }
11919b7cd975SBarry Smith 
1192d763cef2SBarry Smith /*@C
1193f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1194b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1195d763cef2SBarry Smith 
11963f9fe445SBarry Smith    Logically Collective on TS
1197d763cef2SBarry Smith 
1198d763cef2SBarry Smith    Input Parameters:
1199d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1200e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1201e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1202d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1203d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12040298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1205d763cef2SBarry Smith 
1206f7ab8db6SBarry Smith    Calling sequence of f:
12076bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1208d763cef2SBarry Smith 
1209d763cef2SBarry Smith +  t - current timestep
1210d763cef2SBarry Smith .  u - input vector
1211e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1212e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1213d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1214d763cef2SBarry Smith 
12156cd88445SBarry Smith    Notes:
12166cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12176cd88445SBarry Smith 
12186cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1219ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1220d763cef2SBarry Smith 
1221d763cef2SBarry Smith    Level: beginner
1222d763cef2SBarry Smith 
1223ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1224d763cef2SBarry Smith 
1225d763cef2SBarry Smith @*/
1226e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1227d763cef2SBarry Smith {
1228277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1229089b2837SJed Brown   SNES           snes;
123024989b8cSPeter Brune   DM             dm;
123124989b8cSPeter Brune   TSIJacobian    ijacobian;
1232277b19d0SLisandro Dalcin 
1233d763cef2SBarry Smith   PetscFunctionBegin;
12340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1235e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1236e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1237e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1238e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1239d763cef2SBarry Smith 
124024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124124989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1242e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1243e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1244e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1245e1244c69SJed Brown   }
12460298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1247089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12485f659677SPeter Brune   if (!ijacobian) {
1249e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12500e4ef248SJed Brown   }
1251e5d3d808SBarry Smith   if (Amat) {
1252e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12530e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1254e5d3d808SBarry Smith     ts->Arhs = Amat;
12550e4ef248SJed Brown   }
1256e5d3d808SBarry Smith   if (Pmat) {
1257e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12580e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1259e5d3d808SBarry Smith     ts->Brhs = Pmat;
12600e4ef248SJed Brown   }
1261d763cef2SBarry Smith   PetscFunctionReturn(0);
1262d763cef2SBarry Smith }
1263d763cef2SBarry Smith 
1264316643e7SJed Brown /*@C
1265b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1266316643e7SJed Brown 
12673f9fe445SBarry Smith    Logically Collective on TS
1268316643e7SJed Brown 
1269316643e7SJed Brown    Input Parameters:
1270316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12710298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1272316643e7SJed Brown .  f   - the function evaluation routine
12730298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1274316643e7SJed Brown 
1275316643e7SJed Brown    Calling sequence of f:
12766bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1277316643e7SJed Brown 
1278316643e7SJed Brown +  t   - time at step/stage being solved
1279316643e7SJed Brown .  u   - state vector
1280316643e7SJed Brown .  u_t - time derivative of state vector
1281316643e7SJed Brown .  F   - function vector
1282316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1283316643e7SJed Brown 
1284316643e7SJed Brown    Important:
12852bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1286316643e7SJed Brown 
1287316643e7SJed Brown    Level: beginner
1288316643e7SJed Brown 
1289d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1290316643e7SJed Brown @*/
129151699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1292316643e7SJed Brown {
1293089b2837SJed Brown   PetscErrorCode ierr;
1294089b2837SJed Brown   SNES           snes;
129551699248SLisandro Dalcin   Vec            ralloc = NULL;
129624989b8cSPeter Brune   DM             dm;
1297316643e7SJed Brown 
1298316643e7SJed Brown   PetscFunctionBegin;
12990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
130051699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
130124989b8cSPeter Brune 
130224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130324989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
130424989b8cSPeter Brune 
1305089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
130651699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
130751699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
130851699248SLisandro Dalcin     r  = ralloc;
1309e856ceecSJed Brown   }
131051699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
131151699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1312089b2837SJed Brown   PetscFunctionReturn(0);
1313089b2837SJed Brown }
1314089b2837SJed Brown 
1315089b2837SJed Brown /*@C
1316089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1317089b2837SJed Brown 
1318089b2837SJed Brown    Not Collective
1319089b2837SJed Brown 
1320089b2837SJed Brown    Input Parameter:
1321089b2837SJed Brown .  ts - the TS context
1322089b2837SJed Brown 
1323089b2837SJed Brown    Output Parameter:
13240298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13250298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13260298fd71SBarry Smith -  ctx - the function context (or NULL)
1327089b2837SJed Brown 
1328089b2837SJed Brown    Level: advanced
1329089b2837SJed Brown 
1330089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1331089b2837SJed Brown @*/
1332089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1333089b2837SJed Brown {
1334089b2837SJed Brown   PetscErrorCode ierr;
1335089b2837SJed Brown   SNES           snes;
133624989b8cSPeter Brune   DM             dm;
1337089b2837SJed Brown 
1338089b2837SJed Brown   PetscFunctionBegin;
1339089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1340089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13410298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
134224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
134324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1344089b2837SJed Brown   PetscFunctionReturn(0);
1345089b2837SJed Brown }
1346089b2837SJed Brown 
1347089b2837SJed Brown /*@C
1348089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1349089b2837SJed Brown 
1350089b2837SJed Brown    Not Collective
1351089b2837SJed Brown 
1352089b2837SJed Brown    Input Parameter:
1353089b2837SJed Brown .  ts - the TS context
1354089b2837SJed Brown 
1355089b2837SJed Brown    Output Parameter:
13560298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13570298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13580298fd71SBarry Smith -  ctx - the function context (or NULL)
1359089b2837SJed Brown 
1360089b2837SJed Brown    Level: advanced
1361089b2837SJed Brown 
13622bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1363089b2837SJed Brown @*/
1364089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1365089b2837SJed Brown {
1366089b2837SJed Brown   PetscErrorCode ierr;
1367089b2837SJed Brown   SNES           snes;
136824989b8cSPeter Brune   DM             dm;
1369089b2837SJed Brown 
1370089b2837SJed Brown   PetscFunctionBegin;
1371089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1372089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13730298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
137424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1376316643e7SJed Brown   PetscFunctionReturn(0);
1377316643e7SJed Brown }
1378316643e7SJed Brown 
1379316643e7SJed Brown /*@C
1380a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1381ae8867d6SBarry Smith         provided with TSSetIFunction().
1382316643e7SJed Brown 
13833f9fe445SBarry Smith    Logically Collective on TS
1384316643e7SJed Brown 
1385316643e7SJed Brown    Input Parameters:
1386316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1387e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1388e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1389316643e7SJed Brown .  f   - the Jacobian evaluation routine
13900298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1391316643e7SJed Brown 
1392316643e7SJed Brown    Calling sequence of f:
13936bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1394316643e7SJed Brown 
1395316643e7SJed Brown +  t    - time at step/stage being solved
13961b4a444bSJed Brown .  U    - state vector
13971b4a444bSJed Brown .  U_t  - time derivative of state vector
1398316643e7SJed Brown .  a    - shift
1399e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1400e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1401316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1402316643e7SJed Brown 
1403316643e7SJed Brown    Notes:
1404e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1405316643e7SJed Brown 
1406895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1407895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1408895c21f2SBarry Smith 
1409a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1410b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1411a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1412a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1413a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1414a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1415a4f0a591SBarry Smith 
14166cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14176cd88445SBarry Smith 
14186cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1419ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1420ca5f011dSBarry Smith 
1421316643e7SJed Brown    Level: beginner
1422316643e7SJed Brown 
1423ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1424316643e7SJed Brown 
1425316643e7SJed Brown @*/
1426e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1427316643e7SJed Brown {
1428316643e7SJed Brown   PetscErrorCode ierr;
1429089b2837SJed Brown   SNES           snes;
143024989b8cSPeter Brune   DM             dm;
1431316643e7SJed Brown 
1432316643e7SJed Brown   PetscFunctionBegin;
14330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1434e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1435e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1436e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1437e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
143824989b8cSPeter Brune 
143924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
144024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
144124989b8cSPeter Brune 
1442089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1443e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1444316643e7SJed Brown   PetscFunctionReturn(0);
1445316643e7SJed Brown }
1446316643e7SJed Brown 
1447e1244c69SJed Brown /*@
1448e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1449e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1450e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1451e1244c69SJed Brown    not been changed by the TS.
1452e1244c69SJed Brown 
1453e1244c69SJed Brown    Logically Collective
1454e1244c69SJed Brown 
1455e1244c69SJed Brown    Input Arguments:
1456e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1457e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1458e1244c69SJed Brown 
1459e1244c69SJed Brown    Level: intermediate
1460e1244c69SJed Brown 
1461e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1462e1244c69SJed Brown @*/
1463e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1464e1244c69SJed Brown {
1465e1244c69SJed Brown   PetscFunctionBegin;
1466e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1467e1244c69SJed Brown   PetscFunctionReturn(0);
1468e1244c69SJed Brown }
1469e1244c69SJed Brown 
1470efe9872eSLisandro Dalcin /*@C
1471efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1472efe9872eSLisandro Dalcin 
1473efe9872eSLisandro Dalcin    Logically Collective on TS
1474efe9872eSLisandro Dalcin 
1475efe9872eSLisandro Dalcin    Input Parameters:
1476efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1477efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1478efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1479efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1480efe9872eSLisandro Dalcin 
1481efe9872eSLisandro Dalcin    Calling sequence of fun:
14826bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1483efe9872eSLisandro Dalcin 
1484efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1485efe9872eSLisandro Dalcin .  U    - state vector
1486efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1487efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1488efe9872eSLisandro Dalcin .  F    - function vector
1489efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1490efe9872eSLisandro Dalcin 
1491efe9872eSLisandro Dalcin    Level: beginner
1492efe9872eSLisandro Dalcin 
1493efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1494efe9872eSLisandro Dalcin @*/
1495efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1496efe9872eSLisandro Dalcin {
1497efe9872eSLisandro Dalcin   DM             dm;
1498efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1499efe9872eSLisandro Dalcin 
1500efe9872eSLisandro Dalcin   PetscFunctionBegin;
1501efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1502efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1503efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1504efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1505efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1506efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1507efe9872eSLisandro Dalcin }
1508efe9872eSLisandro Dalcin 
1509efe9872eSLisandro Dalcin /*@C
1510efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1511efe9872eSLisandro Dalcin 
1512efe9872eSLisandro Dalcin   Not Collective
1513efe9872eSLisandro Dalcin 
1514efe9872eSLisandro Dalcin   Input Parameter:
1515efe9872eSLisandro Dalcin . ts - the TS context
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin   Output Parameter:
1518efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1519efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1520efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1521efe9872eSLisandro Dalcin 
1522efe9872eSLisandro Dalcin   Level: advanced
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1525efe9872eSLisandro Dalcin @*/
1526efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1527efe9872eSLisandro Dalcin {
1528efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1529efe9872eSLisandro Dalcin   SNES           snes;
1530efe9872eSLisandro Dalcin   DM             dm;
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin   PetscFunctionBegin;
1533efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1534efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1535efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1536efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1537efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1538efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1539efe9872eSLisandro Dalcin }
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin /*@C
1542bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1543efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1544efe9872eSLisandro Dalcin 
1545efe9872eSLisandro Dalcin    Logically Collective on TS
1546efe9872eSLisandro Dalcin 
1547efe9872eSLisandro Dalcin    Input Parameters:
1548efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1549efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1550efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1551efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1552efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1553efe9872eSLisandro Dalcin 
1554efe9872eSLisandro Dalcin    Calling sequence of jac:
15556bc98fa9SBarry 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);
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1558efe9872eSLisandro Dalcin .  U    - state vector
1559efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1560efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1561efe9872eSLisandro Dalcin .  v    - shift for U_t
1562efe9872eSLisandro Dalcin .  a    - shift for U_tt
1563efe9872eSLisandro 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
1564efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1565efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1566efe9872eSLisandro Dalcin 
1567efe9872eSLisandro Dalcin    Notes:
1568efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1569efe9872eSLisandro Dalcin 
1570efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1571efe9872eSLisandro 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.
1572efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1573bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1574efe9872eSLisandro Dalcin 
1575efe9872eSLisandro Dalcin    Level: beginner
1576efe9872eSLisandro Dalcin 
1577efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1578efe9872eSLisandro Dalcin @*/
1579efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1580efe9872eSLisandro Dalcin {
1581efe9872eSLisandro Dalcin   DM             dm;
1582efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1583efe9872eSLisandro Dalcin 
1584efe9872eSLisandro Dalcin   PetscFunctionBegin;
1585efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1586efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1587efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1588efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1589efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1590efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1591efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1592efe9872eSLisandro Dalcin }
1593efe9872eSLisandro Dalcin 
1594efe9872eSLisandro Dalcin /*@C
1595efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1598efe9872eSLisandro Dalcin 
1599efe9872eSLisandro Dalcin   Input Parameter:
1600efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1601efe9872eSLisandro Dalcin 
1602efe9872eSLisandro Dalcin   Output Parameters:
1603efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1604efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1605efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1606efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1607efe9872eSLisandro Dalcin 
160895452b02SPatrick Sanan   Notes:
160995452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1610efe9872eSLisandro Dalcin 
1611efe9872eSLisandro Dalcin   Level: advanced
1612efe9872eSLisandro Dalcin 
161380275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1614efe9872eSLisandro Dalcin 
1615efe9872eSLisandro Dalcin @*/
1616efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1617efe9872eSLisandro Dalcin {
1618efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1619efe9872eSLisandro Dalcin   SNES           snes;
1620efe9872eSLisandro Dalcin   DM             dm;
1621efe9872eSLisandro Dalcin 
1622efe9872eSLisandro Dalcin   PetscFunctionBegin;
1623efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1626efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1627efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1628efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1629efe9872eSLisandro Dalcin }
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin /*@
1632efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1633efe9872eSLisandro Dalcin 
1634d083f849SBarry Smith   Collective on TS
1635efe9872eSLisandro Dalcin 
1636efe9872eSLisandro Dalcin   Input Parameters:
1637efe9872eSLisandro Dalcin + ts - the TS context
1638efe9872eSLisandro Dalcin . t - current time
1639efe9872eSLisandro Dalcin . U - state vector
1640efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1641efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1642efe9872eSLisandro Dalcin 
1643efe9872eSLisandro Dalcin   Output Parameter:
1644efe9872eSLisandro Dalcin . F - the residual vector
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   Note:
1647efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1648efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1649efe9872eSLisandro Dalcin 
1650efe9872eSLisandro Dalcin   Level: developer
1651efe9872eSLisandro Dalcin 
1652efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1653efe9872eSLisandro Dalcin @*/
1654efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1655efe9872eSLisandro Dalcin {
1656efe9872eSLisandro Dalcin   DM             dm;
1657efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1658efe9872eSLisandro Dalcin   void           *ctx;
1659efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1660efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1661efe9872eSLisandro Dalcin 
1662efe9872eSLisandro Dalcin   PetscFunctionBegin;
1663efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1664efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1665efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1666efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1667efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1668efe9872eSLisandro Dalcin 
1669efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1670efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1671efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   if (!I2Function) {
1674efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1675efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1676efe9872eSLisandro Dalcin   }
1677efe9872eSLisandro Dalcin 
1678efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1679efe9872eSLisandro Dalcin 
1680efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1681efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1682efe9872eSLisandro Dalcin   PetscStackPop;
1683efe9872eSLisandro Dalcin 
1684efe9872eSLisandro Dalcin   if (rhsfunction) {
1685efe9872eSLisandro Dalcin     Vec Frhs;
1686efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1687efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1688efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1689efe9872eSLisandro Dalcin   }
1690efe9872eSLisandro Dalcin 
1691efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1692efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1693efe9872eSLisandro Dalcin }
1694efe9872eSLisandro Dalcin 
1695efe9872eSLisandro Dalcin /*@
1696efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1697efe9872eSLisandro Dalcin 
1698d083f849SBarry Smith   Collective on TS
1699efe9872eSLisandro Dalcin 
1700efe9872eSLisandro Dalcin   Input Parameters:
1701efe9872eSLisandro Dalcin + ts - the TS context
1702efe9872eSLisandro Dalcin . t - current timestep
1703efe9872eSLisandro Dalcin . U - state vector
1704efe9872eSLisandro Dalcin . V - time derivative of state vector
1705efe9872eSLisandro Dalcin . A - second time derivative of state vector
1706efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1707efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin   Output Parameters:
1710efe9872eSLisandro Dalcin + J - Jacobian matrix
1711efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   Notes:
1714efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1715efe9872eSLisandro Dalcin 
1716efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1719efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1720efe9872eSLisandro Dalcin 
1721efe9872eSLisandro Dalcin   Level: developer
1722efe9872eSLisandro Dalcin 
1723efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1724efe9872eSLisandro Dalcin @*/
1725efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1726efe9872eSLisandro Dalcin {
1727efe9872eSLisandro Dalcin   DM             dm;
1728efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1729efe9872eSLisandro Dalcin   void           *ctx;
1730efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1731efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1732efe9872eSLisandro Dalcin 
1733efe9872eSLisandro Dalcin   PetscFunctionBegin;
1734efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1735efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1736efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1737efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1738efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1739efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1740efe9872eSLisandro Dalcin 
1741efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1742efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1743efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1744efe9872eSLisandro Dalcin 
1745efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1746efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1747efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1748efe9872eSLisandro Dalcin   }
1749efe9872eSLisandro Dalcin 
1750efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1751efe9872eSLisandro Dalcin 
1752efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1753efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1754efe9872eSLisandro Dalcin   PetscStackPop;
1755efe9872eSLisandro Dalcin 
1756efe9872eSLisandro Dalcin   if (rhsjacobian) {
1757efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1758efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1759efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1760efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1761efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1762efe9872eSLisandro Dalcin   }
1763efe9872eSLisandro Dalcin 
1764efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1765efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1766efe9872eSLisandro Dalcin }
1767efe9872eSLisandro Dalcin 
1768efe9872eSLisandro Dalcin /*@
1769efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1770efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1771efe9872eSLisandro Dalcin 
1772d083f849SBarry Smith    Logically Collective on TS
1773efe9872eSLisandro Dalcin 
1774efe9872eSLisandro Dalcin    Input Parameters:
1775efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1776efe9872eSLisandro Dalcin .  u - the solution vector
1777efe9872eSLisandro Dalcin -  v - the time derivative vector
1778efe9872eSLisandro Dalcin 
1779efe9872eSLisandro Dalcin    Level: beginner
1780efe9872eSLisandro Dalcin 
1781efe9872eSLisandro Dalcin @*/
1782efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1783efe9872eSLisandro Dalcin {
1784efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1785efe9872eSLisandro Dalcin 
1786efe9872eSLisandro Dalcin   PetscFunctionBegin;
1787efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1788efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1789efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1790efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1791efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1792efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1793efe9872eSLisandro Dalcin   ts->vec_dot = v;
1794efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1795efe9872eSLisandro Dalcin }
1796efe9872eSLisandro Dalcin 
1797efe9872eSLisandro Dalcin /*@
1798efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1799efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1800efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1801efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1802efe9872eSLisandro Dalcin 
1803efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1804efe9872eSLisandro Dalcin 
1805efe9872eSLisandro Dalcin    Input Parameter:
1806efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1807efe9872eSLisandro Dalcin 
1808efe9872eSLisandro Dalcin    Output Parameter:
1809efe9872eSLisandro Dalcin +  u - the vector containing the solution
1810efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1811efe9872eSLisandro Dalcin 
1812efe9872eSLisandro Dalcin    Level: intermediate
1813efe9872eSLisandro Dalcin 
1814efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1815efe9872eSLisandro Dalcin 
1816efe9872eSLisandro Dalcin @*/
1817efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1818efe9872eSLisandro Dalcin {
1819efe9872eSLisandro Dalcin   PetscFunctionBegin;
1820efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1821efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1822efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1823efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1824efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1825efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1826efe9872eSLisandro Dalcin }
1827efe9872eSLisandro Dalcin 
182855849f57SBarry Smith /*@C
182955849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
183055849f57SBarry Smith 
183155849f57SBarry Smith   Collective on PetscViewer
183255849f57SBarry Smith 
183355849f57SBarry Smith   Input Parameters:
183455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
183555849f57SBarry Smith            some related function before a call to TSLoad().
183655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
183755849f57SBarry Smith 
183855849f57SBarry Smith    Level: intermediate
183955849f57SBarry Smith 
184055849f57SBarry Smith   Notes:
184155849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
184255849f57SBarry Smith 
184355849f57SBarry Smith   Notes for advanced users:
184455849f57SBarry Smith   Most users should not need to know the details of the binary storage
184555849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
184655849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
184755849f57SBarry Smith   format is
184855849f57SBarry Smith .vb
184955849f57SBarry Smith      has not yet been determined
185055849f57SBarry Smith .ve
185155849f57SBarry Smith 
185255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
185355849f57SBarry Smith @*/
1854f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
185555849f57SBarry Smith {
185655849f57SBarry Smith   PetscErrorCode ierr;
185755849f57SBarry Smith   PetscBool      isbinary;
185855849f57SBarry Smith   PetscInt       classid;
185955849f57SBarry Smith   char           type[256];
18602d53ad75SBarry Smith   DMTS           sdm;
1861ad6bc421SBarry Smith   DM             dm;
186255849f57SBarry Smith 
186355849f57SBarry Smith   PetscFunctionBegin;
1864f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
186555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
186655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
186755849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
186855849f57SBarry Smith 
1869060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1870ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1871060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1872f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1873f2c2a1b9SBarry Smith   if (ts->ops->load) {
1874f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1875f2c2a1b9SBarry Smith   }
1876ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1877ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1878ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1879f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1880f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18812d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18822d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
188355849f57SBarry Smith   PetscFunctionReturn(0);
188455849f57SBarry Smith }
188555849f57SBarry Smith 
18869804daf3SBarry Smith #include <petscdraw.h>
1887e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1888e04113cfSBarry Smith #include <petscviewersaws.h>
1889f05ece33SBarry Smith #endif
1890fe2efc57SMark 
1891fe2efc57SMark /*@C
1892fe2efc57SMark    TSViewFromOptions - View from Options
1893fe2efc57SMark 
1894fe2efc57SMark    Collective on TS
1895fe2efc57SMark 
1896fe2efc57SMark    Input Parameters:
1897fe2efc57SMark +  A - the application ordering context
1898736c3998SJose E. Roman .  obj - Optional object
1899736c3998SJose E. Roman -  name - command line option
1900fe2efc57SMark 
1901fe2efc57SMark    Level: intermediate
1902fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
1903fe2efc57SMark @*/
1904fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
1905fe2efc57SMark {
1906fe2efc57SMark   PetscErrorCode ierr;
1907fe2efc57SMark 
1908fe2efc57SMark   PetscFunctionBegin;
1909fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
1910fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
1911fe2efc57SMark   PetscFunctionReturn(0);
1912fe2efc57SMark }
1913fe2efc57SMark 
19147e2c5f70SBarry Smith /*@C
1915d763cef2SBarry Smith     TSView - Prints the TS data structure.
1916d763cef2SBarry Smith 
19174c49b128SBarry Smith     Collective on TS
1918d763cef2SBarry Smith 
1919d763cef2SBarry Smith     Input Parameters:
1920d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1921d763cef2SBarry Smith -   viewer - visualization context
1922d763cef2SBarry Smith 
1923d763cef2SBarry Smith     Options Database Key:
1924d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1925d763cef2SBarry Smith 
1926d763cef2SBarry Smith     Notes:
1927d763cef2SBarry Smith     The available visualization contexts include
1928b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1929b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1930d763cef2SBarry Smith          output where only the first processor opens
1931d763cef2SBarry Smith          the file.  All other processors send their
1932d763cef2SBarry Smith          data to the first processor to print.
1933d763cef2SBarry Smith 
1934d763cef2SBarry Smith     The user can open an alternative visualization context with
1935b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1936d763cef2SBarry Smith 
1937d763cef2SBarry Smith     Level: beginner
1938d763cef2SBarry Smith 
1939b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1940d763cef2SBarry Smith @*/
19417087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1942d763cef2SBarry Smith {
1943dfbe8321SBarry Smith   PetscErrorCode ierr;
194419fd82e9SBarry Smith   TSType         type;
19452b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19462d53ad75SBarry Smith   DMTS           sdm;
1947e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1948536b137fSBarry Smith   PetscBool      issaws;
1949f05ece33SBarry Smith #endif
1950d763cef2SBarry Smith 
1951d763cef2SBarry Smith   PetscFunctionBegin;
19520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19533050cee2SBarry Smith   if (!viewer) {
1954ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19553050cee2SBarry Smith   }
19560700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1957c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1958fd16b177SBarry Smith 
1959251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1960251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
196155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19622b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1963e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1964536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1965f05ece33SBarry Smith #endif
196632077d6dSBarry Smith   if (iascii) {
1967dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1968efd4aadfSBarry Smith     if (ts->ops->view) {
1969efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1970efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1971efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1972efd4aadfSBarry Smith     }
1973ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
197477431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1975ef85077eSLisandro Dalcin     }
1976ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19777c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1978ef85077eSLisandro Dalcin     }
1979efd4aadfSBarry Smith     if (ts->usessnes) {
1980efd4aadfSBarry Smith       PetscBool lin;
1981d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19825ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1983d763cef2SBarry Smith       }
19845ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
19851ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
1986efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1987efd4aadfSBarry Smith     }
1988193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1989a0af407cSBarry Smith     if (ts->vrtol) {
1990a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1991a0af407cSBarry Smith     } else {
1992a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1993a0af407cSBarry Smith     }
1994a0af407cSBarry Smith     if (ts->vatol) {
1995a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1996a0af407cSBarry Smith     } else {
1997a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1998a0af407cSBarry Smith     }
1999825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2000efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2001825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
20020f5bd95cSBarry Smith   } else if (isstring) {
2003a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
200436a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
200536a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
200655849f57SBarry Smith   } else if (isbinary) {
200755849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
200855849f57SBarry Smith     MPI_Comm    comm;
200955849f57SBarry Smith     PetscMPIInt rank;
201055849f57SBarry Smith     char        type[256];
201155849f57SBarry Smith 
201255849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
201355849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
201455849f57SBarry Smith     if (!rank) {
201555849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
201655849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
201755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
201855849f57SBarry Smith     }
201955849f57SBarry Smith     if (ts->ops->view) {
202055849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
202155849f57SBarry Smith     }
2022efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2023f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2024f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
20252d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20262d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20272b0a91c0SBarry Smith   } else if (isdraw) {
20282b0a91c0SBarry Smith     PetscDraw draw;
20292b0a91c0SBarry Smith     char      str[36];
203089fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20312b0a91c0SBarry Smith 
20322b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20332b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20342b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20352b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
203651fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
203789fd9fafSBarry Smith     bottom = y - h;
20382b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20392b0a91c0SBarry Smith     if (ts->ops->view) {
20402b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20412b0a91c0SBarry Smith     }
2042efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2043efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20442b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2045e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2046536b137fSBarry Smith   } else if (issaws) {
2047d45a07a7SBarry Smith     PetscMPIInt rank;
20482657e9d9SBarry Smith     const char  *name;
20492657e9d9SBarry Smith 
20502657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2051d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2052d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2053d45a07a7SBarry Smith       char       dir[1024];
2054d45a07a7SBarry Smith 
2055e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2056a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20572657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20582657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20592657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2060d763cef2SBarry Smith     }
20610acecf5bSBarry Smith     if (ts->ops->view) {
20620acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20630acecf5bSBarry Smith     }
2064f05ece33SBarry Smith #endif
2065f05ece33SBarry Smith   }
206636a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
206736a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
206836a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
206936a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
207036a9e3b9SBarry Smith   }
207136a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
207236a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2073f05ece33SBarry Smith 
2074b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2075251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2076b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2077d763cef2SBarry Smith   PetscFunctionReturn(0);
2078d763cef2SBarry Smith }
2079d763cef2SBarry Smith 
2080b07ff414SBarry Smith /*@
2081d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2082d763cef2SBarry Smith    the timesteppers.
2083d763cef2SBarry Smith 
20843f9fe445SBarry Smith    Logically Collective on TS
2085d763cef2SBarry Smith 
2086d763cef2SBarry Smith    Input Parameters:
2087d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2088d763cef2SBarry Smith -  usrP - optional user context
2089d763cef2SBarry Smith 
209095452b02SPatrick Sanan    Fortran Notes:
209195452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2092daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2093daf670e6SBarry Smith 
2094d763cef2SBarry Smith    Level: intermediate
2095d763cef2SBarry Smith 
2096d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2097d763cef2SBarry Smith @*/
20987087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2099d763cef2SBarry Smith {
2100d763cef2SBarry Smith   PetscFunctionBegin;
21010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2102d763cef2SBarry Smith   ts->user = usrP;
2103d763cef2SBarry Smith   PetscFunctionReturn(0);
2104d763cef2SBarry Smith }
2105d763cef2SBarry Smith 
2106b07ff414SBarry Smith /*@
2107d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2108d763cef2SBarry Smith     timestepper.
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith     Not Collective
2111d763cef2SBarry Smith 
2112d763cef2SBarry Smith     Input Parameter:
2113d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith     Output Parameter:
2116d763cef2SBarry Smith .   usrP - user context
2117d763cef2SBarry Smith 
211895452b02SPatrick Sanan    Fortran Notes:
211995452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2120daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2121daf670e6SBarry Smith 
2122d763cef2SBarry Smith     Level: intermediate
2123d763cef2SBarry Smith 
2124d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2125d763cef2SBarry Smith @*/
2126e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2127d763cef2SBarry Smith {
2128d763cef2SBarry Smith   PetscFunctionBegin;
21290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2130e71120c6SJed Brown   *(void**)usrP = ts->user;
2131d763cef2SBarry Smith   PetscFunctionReturn(0);
2132d763cef2SBarry Smith }
2133d763cef2SBarry Smith 
2134d763cef2SBarry Smith /*@
213580275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith    Not Collective
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith    Input Parameter:
2140d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith    Output Parameter:
214380275a0aSLisandro Dalcin .  steps - number of steps completed so far
2144d763cef2SBarry Smith 
2145d763cef2SBarry Smith    Level: intermediate
2146d763cef2SBarry Smith 
21479be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2148d763cef2SBarry Smith @*/
214980275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2150d763cef2SBarry Smith {
2151d763cef2SBarry Smith   PetscFunctionBegin;
21520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
215380275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
215480275a0aSLisandro Dalcin   *steps = ts->steps;
215580275a0aSLisandro Dalcin   PetscFunctionReturn(0);
215680275a0aSLisandro Dalcin }
215780275a0aSLisandro Dalcin 
215880275a0aSLisandro Dalcin /*@
215980275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
216080275a0aSLisandro Dalcin 
216180275a0aSLisandro Dalcin    Logically Collective on TS
216280275a0aSLisandro Dalcin 
216380275a0aSLisandro Dalcin    Input Parameters:
216480275a0aSLisandro Dalcin +  ts - the TS context
216580275a0aSLisandro Dalcin -  steps - number of steps completed so far
216680275a0aSLisandro Dalcin 
216780275a0aSLisandro Dalcin    Notes:
216880275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
216980275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
217080275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
217180275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
217280275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
217380275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
217480275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
217580275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
217680275a0aSLisandro Dalcin 
217780275a0aSLisandro Dalcin    Level: advanced
217880275a0aSLisandro Dalcin 
217980275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
218080275a0aSLisandro Dalcin @*/
218180275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
218280275a0aSLisandro Dalcin {
218380275a0aSLisandro Dalcin   PetscFunctionBegin;
218480275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218580275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
218680275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
218780275a0aSLisandro Dalcin   ts->steps = steps;
2188d763cef2SBarry Smith   PetscFunctionReturn(0);
2189d763cef2SBarry Smith }
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith /*@
2192d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2193d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2194d763cef2SBarry Smith 
21953f9fe445SBarry Smith    Logically Collective on TS
2196d763cef2SBarry Smith 
2197d763cef2SBarry Smith    Input Parameters:
2198d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2199d763cef2SBarry Smith -  time_step - the size of the timestep
2200d763cef2SBarry Smith 
2201d763cef2SBarry Smith    Level: intermediate
2202d763cef2SBarry Smith 
2203aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2204d763cef2SBarry Smith 
2205d763cef2SBarry Smith @*/
22067087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2207d763cef2SBarry Smith {
2208d763cef2SBarry Smith   PetscFunctionBegin;
22090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2210c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2211d763cef2SBarry Smith   ts->time_step = time_step;
2212d763cef2SBarry Smith   PetscFunctionReturn(0);
2213d763cef2SBarry Smith }
2214d763cef2SBarry Smith 
2215a43b19c4SJed Brown /*@
221649354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
221749354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
221849354f04SShri Abhyankar      or just return at the final time TS computed.
2219a43b19c4SJed Brown 
2220a43b19c4SJed Brown   Logically Collective on TS
2221a43b19c4SJed Brown 
2222a43b19c4SJed Brown    Input Parameter:
2223a43b19c4SJed Brown +   ts - the time-step context
222449354f04SShri Abhyankar -   eftopt - exact final time option
2225a43b19c4SJed Brown 
2226feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2227feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2228feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2229feed9e9dSBarry Smith 
2230feed9e9dSBarry Smith    Options Database:
2231feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2232feed9e9dSBarry Smith 
2233ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2234ee346746SBarry Smith     then the final time you selected.
2235ee346746SBarry Smith 
2236a43b19c4SJed Brown    Level: beginner
2237a43b19c4SJed Brown 
2238f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2239a43b19c4SJed Brown @*/
224049354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2241a43b19c4SJed Brown {
2242a43b19c4SJed Brown   PetscFunctionBegin;
2243a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224449354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
224549354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2246a43b19c4SJed Brown   PetscFunctionReturn(0);
2247a43b19c4SJed Brown }
2248a43b19c4SJed Brown 
2249d763cef2SBarry Smith /*@
2250f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2251f6953c82SLisandro Dalcin 
2252f6953c82SLisandro Dalcin    Not Collective
2253f6953c82SLisandro Dalcin 
2254f6953c82SLisandro Dalcin    Input Parameter:
2255f6953c82SLisandro Dalcin .  ts - the TS context
2256f6953c82SLisandro Dalcin 
2257f6953c82SLisandro Dalcin    Output Parameter:
2258f6953c82SLisandro Dalcin .  eftopt - exact final time option
2259f6953c82SLisandro Dalcin 
2260f6953c82SLisandro Dalcin    Level: beginner
2261f6953c82SLisandro Dalcin 
2262f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2263f6953c82SLisandro Dalcin @*/
2264f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2265f6953c82SLisandro Dalcin {
2266f6953c82SLisandro Dalcin   PetscFunctionBegin;
2267f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2268f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2269f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2270f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2271f6953c82SLisandro Dalcin }
2272f6953c82SLisandro Dalcin 
2273f6953c82SLisandro Dalcin /*@
2274d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2275d763cef2SBarry Smith 
2276d763cef2SBarry Smith    Not Collective
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Input Parameter:
2279d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2280d763cef2SBarry Smith 
2281d763cef2SBarry Smith    Output Parameter:
2282d763cef2SBarry Smith .  dt - the current timestep size
2283d763cef2SBarry Smith 
2284d763cef2SBarry Smith    Level: intermediate
2285d763cef2SBarry Smith 
2286aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2287d763cef2SBarry Smith 
2288d763cef2SBarry Smith @*/
22897087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2290d763cef2SBarry Smith {
2291d763cef2SBarry Smith   PetscFunctionBegin;
22920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2293f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2294d763cef2SBarry Smith   *dt = ts->time_step;
2295d763cef2SBarry Smith   PetscFunctionReturn(0);
2296d763cef2SBarry Smith }
2297d763cef2SBarry Smith 
2298d8e5e3e6SSatish Balay /*@
2299d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2300d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2301d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2302d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2303d763cef2SBarry Smith 
2304d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2305d763cef2SBarry Smith 
2306d763cef2SBarry Smith    Input Parameter:
2307d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2308d763cef2SBarry Smith 
2309d763cef2SBarry Smith    Output Parameter:
2310d763cef2SBarry Smith .  v - the vector containing the solution
2311d763cef2SBarry Smith 
231263e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
231363e21af5SBarry Smith    final time. It returns the solution at the next timestep.
231463e21af5SBarry Smith 
2315d763cef2SBarry Smith    Level: intermediate
2316d763cef2SBarry Smith 
23170ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2318d763cef2SBarry Smith 
2319d763cef2SBarry Smith @*/
23207087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2321d763cef2SBarry Smith {
2322d763cef2SBarry Smith   PetscFunctionBegin;
23230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23244482741eSBarry Smith   PetscValidPointer(v,2);
23258737fe31SLisandro Dalcin   *v = ts->vec_sol;
2326d763cef2SBarry Smith   PetscFunctionReturn(0);
2327d763cef2SBarry Smith }
2328d763cef2SBarry Smith 
232903fe5f5eSDebojyoti Ghosh /*@
2330b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
233103fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2332b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
233303fe5f5eSDebojyoti Ghosh    structure as the solution vector.
233403fe5f5eSDebojyoti Ghosh 
233503fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
233603fe5f5eSDebojyoti Ghosh 
233703fe5f5eSDebojyoti Ghosh    Parameters :
2338a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2339b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2340b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
234103fe5f5eSDebojyoti Ghosh        returned in v.
2342a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
234303fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2344b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
234503fe5f5eSDebojyoti Ghosh 
23464cdd57e5SDebojyoti Ghosh    Level: advanced
234703fe5f5eSDebojyoti Ghosh 
234803fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
234903fe5f5eSDebojyoti Ghosh 
235003fe5f5eSDebojyoti Ghosh @*/
2351b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
235203fe5f5eSDebojyoti Ghosh {
235303fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
235403fe5f5eSDebojyoti Ghosh 
235503fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
235603fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2357b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
235803fe5f5eSDebojyoti Ghosh   else {
2359b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
236003fe5f5eSDebojyoti Ghosh   }
236103fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
236203fe5f5eSDebojyoti Ghosh }
236303fe5f5eSDebojyoti Ghosh 
23644cdd57e5SDebojyoti Ghosh /*@
23654cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23664cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23674cdd57e5SDebojyoti Ghosh 
23684cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23694cdd57e5SDebojyoti Ghosh 
23704cdd57e5SDebojyoti Ghosh    Parameters :
2371a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2372a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
23734cdd57e5SDebojyoti Ghosh 
23744cdd57e5SDebojyoti Ghosh    Level: intermediate
23754cdd57e5SDebojyoti Ghosh 
23764cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23774cdd57e5SDebojyoti Ghosh 
23784cdd57e5SDebojyoti Ghosh @*/
23794cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23804cdd57e5SDebojyoti Ghosh {
23814cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23824cdd57e5SDebojyoti Ghosh 
23834cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23844cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2385f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23864cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2387f6356ec7SDebojyoti Ghosh   } else {
2388f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2389f6356ec7SDebojyoti Ghosh   }
23904cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23914cdd57e5SDebojyoti Ghosh }
23924cdd57e5SDebojyoti Ghosh 
23934cdd57e5SDebojyoti Ghosh /*@
23944cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23954cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23964cdd57e5SDebojyoti Ghosh 
23974cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23984cdd57e5SDebojyoti Ghosh 
23999657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24009657682dSDebojyoti Ghosh 
24014cdd57e5SDebojyoti Ghosh    Parameters :
2402a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2403657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2404a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
24054cdd57e5SDebojyoti Ghosh 
24064cdd57e5SDebojyoti Ghosh    Level: intermediate
24074cdd57e5SDebojyoti Ghosh 
240857df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24094cdd57e5SDebojyoti Ghosh 
24104cdd57e5SDebojyoti Ghosh @*/
24110a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24124cdd57e5SDebojyoti Ghosh {
24134cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24144cdd57e5SDebojyoti Ghosh 
24154cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24164cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2417f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
24180a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2419f6356ec7SDebojyoti Ghosh   } else {
2420f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2421f6356ec7SDebojyoti Ghosh   }
24224cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24234cdd57e5SDebojyoti Ghosh }
24244cdd57e5SDebojyoti Ghosh 
242557df6a1bSDebojyoti Ghosh /*@
242657df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
242757df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
242857df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
242957df6a1bSDebojyoti Ghosh 
243057df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
243157df6a1bSDebojyoti Ghosh 
243257df6a1bSDebojyoti Ghosh    Parameters :
2433a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2434a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
243557df6a1bSDebojyoti Ghosh 
243657df6a1bSDebojyoti Ghosh    Level: intermediate
243757df6a1bSDebojyoti Ghosh 
243857df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
243957df6a1bSDebojyoti Ghosh 
244057df6a1bSDebojyoti Ghosh @*/
244157df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
244257df6a1bSDebojyoti Ghosh {
244357df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
244457df6a1bSDebojyoti Ghosh 
244557df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
244657df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24479657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
244857df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
244957df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
245057df6a1bSDebojyoti Ghosh   }
245157df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
245257df6a1bSDebojyoti Ghosh }
245357df6a1bSDebojyoti Ghosh 
2454bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2455d8e5e3e6SSatish Balay /*@
2456bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2457d763cef2SBarry Smith 
2458bdad233fSMatthew Knepley   Not collective
2459d763cef2SBarry Smith 
2460bdad233fSMatthew Knepley   Input Parameters:
2461bdad233fSMatthew Knepley + ts   - The TS
2462bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2463d763cef2SBarry Smith .vb
24640910c330SBarry Smith          U_t - A U = 0      (linear)
24650910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24660910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2467d763cef2SBarry Smith .ve
2468d763cef2SBarry Smith 
2469d763cef2SBarry Smith    Level: beginner
2470d763cef2SBarry Smith 
2471bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2472d763cef2SBarry Smith @*/
24737087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2474a7cc72afSBarry Smith {
24759e2a6581SJed Brown   PetscErrorCode ierr;
24769e2a6581SJed Brown 
2477d763cef2SBarry Smith   PetscFunctionBegin;
24780700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2479bdad233fSMatthew Knepley   ts->problem_type = type;
24809e2a6581SJed Brown   if (type == TS_LINEAR) {
24819e2a6581SJed Brown     SNES snes;
24829e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24839e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24849e2a6581SJed Brown   }
2485d763cef2SBarry Smith   PetscFunctionReturn(0);
2486d763cef2SBarry Smith }
2487d763cef2SBarry Smith 
2488bdad233fSMatthew Knepley /*@C
2489bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2490bdad233fSMatthew Knepley 
2491bdad233fSMatthew Knepley   Not collective
2492bdad233fSMatthew Knepley 
2493bdad233fSMatthew Knepley   Input Parameter:
2494bdad233fSMatthew Knepley . ts   - The TS
2495bdad233fSMatthew Knepley 
2496bdad233fSMatthew Knepley   Output Parameter:
2497bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2498bdad233fSMatthew Knepley .vb
2499089b2837SJed Brown          M U_t = A U
2500089b2837SJed Brown          M(t) U_t = A(t) U
2501b5abc632SBarry Smith          F(t,U,U_t)
2502bdad233fSMatthew Knepley .ve
2503bdad233fSMatthew Knepley 
2504bdad233fSMatthew Knepley    Level: beginner
2505bdad233fSMatthew Knepley 
2506bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2507bdad233fSMatthew Knepley @*/
25087087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2509a7cc72afSBarry Smith {
2510bdad233fSMatthew Knepley   PetscFunctionBegin;
25110700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25124482741eSBarry Smith   PetscValidIntPointer(type,2);
2513bdad233fSMatthew Knepley   *type = ts->problem_type;
2514bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2515bdad233fSMatthew Knepley }
2516d763cef2SBarry Smith 
2517d763cef2SBarry Smith /*@
2518d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2519d763cef2SBarry Smith    of a timestepper.
2520d763cef2SBarry Smith 
2521d763cef2SBarry Smith    Collective on TS
2522d763cef2SBarry Smith 
2523d763cef2SBarry Smith    Input Parameter:
2524d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2525d763cef2SBarry Smith 
2526d763cef2SBarry Smith    Notes:
2527d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2528d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2529141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2530d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2531141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2532d763cef2SBarry Smith 
2533d763cef2SBarry Smith    Level: advanced
2534d763cef2SBarry Smith 
2535141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2536d763cef2SBarry Smith @*/
25377087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2538d763cef2SBarry Smith {
2539dfbe8321SBarry Smith   PetscErrorCode ierr;
25406c6b9e74SPeter Brune   DM             dm;
25416c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2542d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2543cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25446c6b9e74SPeter Brune   TSIJacobian    ijac;
2545efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25466c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25472ffb9264SLisandro Dalcin   PetscBool      isnone;
2548d763cef2SBarry Smith 
2549d763cef2SBarry Smith   PetscFunctionBegin;
25500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2551277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2552277b19d0SLisandro Dalcin 
25537adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2554cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2555cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2556d763cef2SBarry Smith   }
2557277b19d0SLisandro Dalcin 
2558*1a638600SStefano Zampini   if (!ts->vec_sol) {
2559*1a638600SStefano Zampini     if (ts->dm) {
2560*1a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
2561*1a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2562*1a638600SStefano Zampini   }
2563277b19d0SLisandro Dalcin 
2564298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2565298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2566298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2567298bade4SHong Zhang   }
2568298bade4SHong Zhang 
2569cd4cee2dSHong Zhang   if (ts->quadraturets) {
2570cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2571ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2572cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2573cd4cee2dSHong Zhang   }
2574cd4cee2dSHong Zhang 
2575971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2576971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2577e1244c69SJed Brown     Mat Amat,Pmat;
2578e1244c69SJed Brown     SNES snes;
2579e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2580e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2581e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2582e1244c69SJed Brown      * have displaced the RHS matrix */
2583971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2584abc0d4abSBarry 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 */
2585abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2586e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2587e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2588e1244c69SJed Brown     }
2589971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2590abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2591e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2592e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2593e1244c69SJed Brown     }
2594e1244c69SJed Brown   }
25952ffb9264SLisandro Dalcin 
25962ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25972ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25982ffb9264SLisandro Dalcin 
2599277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2600000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2601277b19d0SLisandro Dalcin   }
2602277b19d0SLisandro Dalcin 
26032ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
26042ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
26052ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
26062ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
26072ffb9264SLisandro Dalcin 
2608a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26096c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26106c6b9e74SPeter Brune    */
26116c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
26120298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
26136c6b9e74SPeter Brune   if (!func) {
26146c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
26156c6b9e74SPeter Brune   }
2616a6772fa2SLisandro 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.
26176c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26186c6b9e74SPeter Brune    */
26190298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26200298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2621efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26220298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2623efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
26246c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
26256c6b9e74SPeter Brune   }
2626c0517034SDebojyoti Ghosh 
2627c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2628c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2629c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2630c0517034SDebojyoti Ghosh   }
2631c0517034SDebojyoti Ghosh 
2632277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2633277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2634277b19d0SLisandro Dalcin }
2635277b19d0SLisandro Dalcin 
2636f6a906c0SBarry Smith /*@
2637277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2638277b19d0SLisandro Dalcin 
2639277b19d0SLisandro Dalcin    Collective on TS
2640277b19d0SLisandro Dalcin 
2641277b19d0SLisandro Dalcin    Input Parameter:
2642277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2643277b19d0SLisandro Dalcin 
2644277b19d0SLisandro Dalcin    Level: beginner
2645277b19d0SLisandro Dalcin 
2646277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2647277b19d0SLisandro Dalcin @*/
2648277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2649277b19d0SLisandro Dalcin {
26501d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2651277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2652277b19d0SLisandro Dalcin 
2653277b19d0SLisandro Dalcin   PetscFunctionBegin;
2654277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2655b18ea86cSHong Zhang 
2656277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2657277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2658277b19d0SLisandro Dalcin   }
2659277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2660e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2661bbd56ea5SKarl Rupp 
26624e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26634e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2664214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26656bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2666efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2667e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2668e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
266938637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2670bbd56ea5SKarl Rupp 
2671cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2672ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2673ecf68647SHong Zhang   if (ts->forward_solve) {
2674ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2675ecf68647SHong Zhang   }
2676cd4cee2dSHong Zhang   if (ts->quadraturets) {
2677cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
267836eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2679cd4cee2dSHong Zhang   }
26801d06f6b3SHong Zhang   while (ilink) {
26811d06f6b3SHong Zhang     next = ilink->next;
26821d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
26831d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
26841d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
26851d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
26861d06f6b3SHong Zhang     ilink = next;
268787f4e208SHong Zhang   }
2688545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2689277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2690d763cef2SBarry Smith   PetscFunctionReturn(0);
2691d763cef2SBarry Smith }
2692d763cef2SBarry Smith 
2693d8e5e3e6SSatish Balay /*@
2694d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2695d763cef2SBarry Smith    with TSCreate().
2696d763cef2SBarry Smith 
2697d763cef2SBarry Smith    Collective on TS
2698d763cef2SBarry Smith 
2699d763cef2SBarry Smith    Input Parameter:
2700d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2701d763cef2SBarry Smith 
2702d763cef2SBarry Smith    Level: beginner
2703d763cef2SBarry Smith 
2704d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2705d763cef2SBarry Smith @*/
27066bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2707d763cef2SBarry Smith {
27086849ba73SBarry Smith   PetscErrorCode ierr;
2709d763cef2SBarry Smith 
2710d763cef2SBarry Smith   PetscFunctionBegin;
27116bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2712ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
27136bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2714d763cef2SBarry Smith 
2715ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2716ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2717ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2718ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2719ecf68647SHong Zhang   }
2720e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2721e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27226bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27236d4c513bSLisandro Dalcin 
2724bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2725bc952696SBarry Smith 
272684df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27276427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27286427ac75SLisandro Dalcin 
27296bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27306bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27316bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27320dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27336d4c513bSLisandro Dalcin 
2734cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2735a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2736d763cef2SBarry Smith   PetscFunctionReturn(0);
2737d763cef2SBarry Smith }
2738d763cef2SBarry Smith 
2739d8e5e3e6SSatish Balay /*@
2740d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2741d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2742d763cef2SBarry Smith 
2743d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2744d763cef2SBarry Smith 
2745d763cef2SBarry Smith    Input Parameter:
2746d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2747d763cef2SBarry Smith 
2748d763cef2SBarry Smith    Output Parameter:
2749d763cef2SBarry Smith .  snes - the nonlinear solver context
2750d763cef2SBarry Smith 
2751d763cef2SBarry Smith    Notes:
2752d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2753d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
275494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2755d763cef2SBarry Smith 
2756d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27570298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2758d763cef2SBarry Smith 
2759d763cef2SBarry Smith    Level: beginner
2760d763cef2SBarry Smith 
2761d763cef2SBarry Smith @*/
27627087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2763d763cef2SBarry Smith {
2764d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2765d372ba47SLisandro Dalcin 
2766d763cef2SBarry Smith   PetscFunctionBegin;
27670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27684482741eSBarry Smith   PetscValidPointer(snes,2);
2769d372ba47SLisandro Dalcin   if (!ts->snes) {
2770ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
277116413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
27720298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27733bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2774d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2775496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27769e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27779e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27789e2a6581SJed Brown     }
2779d372ba47SLisandro Dalcin   }
2780d763cef2SBarry Smith   *snes = ts->snes;
2781d763cef2SBarry Smith   PetscFunctionReturn(0);
2782d763cef2SBarry Smith }
2783d763cef2SBarry Smith 
2784deb2cd25SJed Brown /*@
2785deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2786deb2cd25SJed Brown 
2787deb2cd25SJed Brown    Collective
2788deb2cd25SJed Brown 
2789deb2cd25SJed Brown    Input Parameter:
2790deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2791deb2cd25SJed Brown -  snes - the nonlinear solver context
2792deb2cd25SJed Brown 
2793deb2cd25SJed Brown    Notes:
2794deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2795deb2cd25SJed Brown 
2796deb2cd25SJed Brown    Level: developer
2797deb2cd25SJed Brown 
2798deb2cd25SJed Brown @*/
2799deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2800deb2cd25SJed Brown {
2801deb2cd25SJed Brown   PetscErrorCode ierr;
2802d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2803deb2cd25SJed Brown 
2804deb2cd25SJed Brown   PetscFunctionBegin;
2805deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2806deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2807deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2808deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2809bbd56ea5SKarl Rupp 
2810deb2cd25SJed Brown   ts->snes = snes;
2811bbd56ea5SKarl Rupp 
28120298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28130298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2814740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28150298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2816740132f1SEmil Constantinescu   }
2817deb2cd25SJed Brown   PetscFunctionReturn(0);
2818deb2cd25SJed Brown }
2819deb2cd25SJed Brown 
2820d8e5e3e6SSatish Balay /*@
282194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2822d763cef2SBarry Smith    a TS (timestepper) context.
2823d763cef2SBarry Smith 
282494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2825d763cef2SBarry Smith 
2826d763cef2SBarry Smith    Input Parameter:
2827d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2828d763cef2SBarry Smith 
2829d763cef2SBarry Smith    Output Parameter:
283094b7f48cSBarry Smith .  ksp - the nonlinear solver context
2831d763cef2SBarry Smith 
2832d763cef2SBarry Smith    Notes:
283394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2834d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2835d763cef2SBarry Smith    KSP and PC contexts as well.
2836d763cef2SBarry Smith 
283794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28380298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2839d763cef2SBarry Smith 
2840d763cef2SBarry Smith    Level: beginner
2841d763cef2SBarry Smith 
2842d763cef2SBarry Smith @*/
28437087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2844d763cef2SBarry Smith {
2845d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2846089b2837SJed Brown   SNES           snes;
2847d372ba47SLisandro Dalcin 
2848d763cef2SBarry Smith   PetscFunctionBegin;
28490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28504482741eSBarry Smith   PetscValidPointer(ksp,2);
285117186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2852e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2853089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2854089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2855d763cef2SBarry Smith   PetscFunctionReturn(0);
2856d763cef2SBarry Smith }
2857d763cef2SBarry Smith 
2858d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2859d763cef2SBarry Smith 
2860adb62b0dSMatthew Knepley /*@
2861618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2862618ce8baSLisandro Dalcin 
2863618ce8baSLisandro Dalcin    Logically Collective on TS
2864618ce8baSLisandro Dalcin 
2865618ce8baSLisandro Dalcin    Input Parameters:
2866618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2867618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2868618ce8baSLisandro Dalcin 
2869618ce8baSLisandro Dalcin    Options Database Keys:
2870618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2871618ce8baSLisandro Dalcin 
2872618ce8baSLisandro Dalcin    Notes:
2873618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2874618ce8baSLisandro Dalcin 
2875618ce8baSLisandro Dalcin    Level: intermediate
2876618ce8baSLisandro Dalcin 
2877618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2878618ce8baSLisandro Dalcin @*/
2879618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2880618ce8baSLisandro Dalcin {
2881618ce8baSLisandro Dalcin   PetscFunctionBegin;
2882618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2883618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2884618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2885618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2886618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2887618ce8baSLisandro Dalcin }
2888618ce8baSLisandro Dalcin 
2889618ce8baSLisandro Dalcin /*@
2890618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2891618ce8baSLisandro Dalcin 
2892618ce8baSLisandro Dalcin    Not Collective
2893618ce8baSLisandro Dalcin 
2894618ce8baSLisandro Dalcin    Input Parameters:
2895618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2896618ce8baSLisandro Dalcin 
2897618ce8baSLisandro Dalcin    Output Parameter:
2898618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2899618ce8baSLisandro Dalcin 
2900618ce8baSLisandro Dalcin    Level: advanced
2901618ce8baSLisandro Dalcin 
2902618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2903618ce8baSLisandro Dalcin @*/
2904618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2905618ce8baSLisandro Dalcin {
2906618ce8baSLisandro Dalcin   PetscFunctionBegin;
2907618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2908618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2909618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2910618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2911618ce8baSLisandro Dalcin }
2912618ce8baSLisandro Dalcin 
2913618ce8baSLisandro Dalcin /*@
2914618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2915618ce8baSLisandro Dalcin 
2916618ce8baSLisandro Dalcin    Logically Collective on TS
2917618ce8baSLisandro Dalcin 
2918618ce8baSLisandro Dalcin    Input Parameters:
2919618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2920618ce8baSLisandro Dalcin -  maxtime - final time to step to
2921618ce8baSLisandro Dalcin 
2922618ce8baSLisandro Dalcin    Options Database Keys:
2923ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2924618ce8baSLisandro Dalcin 
2925618ce8baSLisandro Dalcin    Notes:
2926618ce8baSLisandro Dalcin    The default maximum time is 5.0
2927618ce8baSLisandro Dalcin 
2928618ce8baSLisandro Dalcin    Level: intermediate
2929618ce8baSLisandro Dalcin 
2930618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2931618ce8baSLisandro Dalcin @*/
2932618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2933618ce8baSLisandro Dalcin {
2934618ce8baSLisandro Dalcin   PetscFunctionBegin;
2935618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2936618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2937618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2938618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2939618ce8baSLisandro Dalcin }
2940618ce8baSLisandro Dalcin 
2941618ce8baSLisandro Dalcin /*@
2942618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2943618ce8baSLisandro Dalcin 
2944618ce8baSLisandro Dalcin    Not Collective
2945618ce8baSLisandro Dalcin 
2946618ce8baSLisandro Dalcin    Input Parameters:
2947618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2948618ce8baSLisandro Dalcin 
2949618ce8baSLisandro Dalcin    Output Parameter:
2950618ce8baSLisandro Dalcin .  maxtime - final time to step to
2951618ce8baSLisandro Dalcin 
2952618ce8baSLisandro Dalcin    Level: advanced
2953618ce8baSLisandro Dalcin 
2954618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2955618ce8baSLisandro Dalcin @*/
2956618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2957618ce8baSLisandro Dalcin {
2958618ce8baSLisandro Dalcin   PetscFunctionBegin;
2959618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2960618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2961618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2962618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2963618ce8baSLisandro Dalcin }
2964618ce8baSLisandro Dalcin 
2965618ce8baSLisandro Dalcin /*@
2966aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2967edc382c3SSatish Balay 
2968edc382c3SSatish Balay    Level: deprecated
2969edc382c3SSatish Balay 
2970aaa6c58dSLisandro Dalcin @*/
2971aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2972aaa6c58dSLisandro Dalcin {
2973aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
2974aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2975aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2976aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2977aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2978aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
2979aaa6c58dSLisandro Dalcin }
2980aaa6c58dSLisandro Dalcin 
2981aaa6c58dSLisandro Dalcin /*@
298219eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
2983edc382c3SSatish Balay 
2984edc382c3SSatish Balay    Level: deprecated
2985edc382c3SSatish Balay 
2986adb62b0dSMatthew Knepley @*/
29877087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2988adb62b0dSMatthew Knepley {
2989adb62b0dSMatthew Knepley   PetscFunctionBegin;
29900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2991abc0a331SBarry Smith   if (maxsteps) {
29924482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2993adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2994adb62b0dSMatthew Knepley   }
2995abc0a331SBarry Smith   if (maxtime) {
29964482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2997adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2998adb62b0dSMatthew Knepley   }
2999adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3000adb62b0dSMatthew Knepley }
3001adb62b0dSMatthew Knepley 
3002d763cef2SBarry Smith /*@
300319eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3004edc382c3SSatish Balay 
3005edc382c3SSatish Balay    Level: deprecated
3006edc382c3SSatish Balay 
3007d763cef2SBarry Smith @*/
30087087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3009d763cef2SBarry Smith {
3010d763cef2SBarry Smith   PetscFunctionBegin;
30110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3012c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3013c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
301439b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
301539b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3016d763cef2SBarry Smith   PetscFunctionReturn(0);
3017d763cef2SBarry Smith }
3018d763cef2SBarry Smith 
3019d763cef2SBarry Smith /*@
30205c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3021edc382c3SSatish Balay 
3022edc382c3SSatish Balay    Level: deprecated
3023edc382c3SSatish Balay 
30245c5f5948SLisandro Dalcin @*/
3025e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30265c5f5948SLisandro Dalcin 
302719eac22cSLisandro Dalcin /*@
30284f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3029edc382c3SSatish Balay 
3030edc382c3SSatish Balay    Level: deprecated
3031edc382c3SSatish Balay 
30324f4e0956SLisandro Dalcin @*/
30334f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30344f4e0956SLisandro Dalcin 
30354f4e0956SLisandro Dalcin /*@
3036d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3037d763cef2SBarry Smith    for use by the TS routines.
3038d763cef2SBarry Smith 
3039d083f849SBarry Smith    Logically Collective on TS
3040d763cef2SBarry Smith 
3041d763cef2SBarry Smith    Input Parameters:
3042d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
30430910c330SBarry Smith -  u - the solution vector
3044d763cef2SBarry Smith 
3045d763cef2SBarry Smith    Level: beginner
3046d763cef2SBarry Smith 
30470ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3048d763cef2SBarry Smith @*/
30490910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3050d763cef2SBarry Smith {
30518737fe31SLisandro Dalcin   PetscErrorCode ierr;
3052496e6a7aSJed Brown   DM             dm;
30538737fe31SLisandro Dalcin 
3054d763cef2SBarry Smith   PetscFunctionBegin;
30550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30560910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30570910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30586bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30590910c330SBarry Smith   ts->vec_sol = u;
3060bbd56ea5SKarl Rupp 
3061496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30620910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3063d763cef2SBarry Smith   PetscFunctionReturn(0);
3064d763cef2SBarry Smith }
3065d763cef2SBarry Smith 
3066ac226902SBarry Smith /*@C
3067000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30683f2090d5SJed Brown   called once at the beginning of each time step.
3069000e7ae3SMatthew Knepley 
30703f9fe445SBarry Smith   Logically Collective on TS
3071000e7ae3SMatthew Knepley 
3072000e7ae3SMatthew Knepley   Input Parameters:
3073000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3074000e7ae3SMatthew Knepley - func - The function
3075000e7ae3SMatthew Knepley 
3076000e7ae3SMatthew Knepley   Calling sequence of func:
30776bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3078000e7ae3SMatthew Knepley 
3079000e7ae3SMatthew Knepley   Level: intermediate
3080000e7ae3SMatthew Knepley 
3081dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3082000e7ae3SMatthew Knepley @*/
30837087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3084000e7ae3SMatthew Knepley {
3085000e7ae3SMatthew Knepley   PetscFunctionBegin;
30860700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3087ae60f76fSBarry Smith   ts->prestep = func;
3088000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3089000e7ae3SMatthew Knepley }
3090000e7ae3SMatthew Knepley 
309109ee8438SJed Brown /*@
30923f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
30933f2090d5SJed Brown 
30943f2090d5SJed Brown   Collective on TS
30953f2090d5SJed Brown 
30963f2090d5SJed Brown   Input Parameters:
30973f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
30983f2090d5SJed Brown 
30993f2090d5SJed Brown   Notes:
31003f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
31013f2090d5SJed Brown   so most users would not generally call this routine themselves.
31023f2090d5SJed Brown 
31033f2090d5SJed Brown   Level: developer
31043f2090d5SJed Brown 
31059be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
31063f2090d5SJed Brown @*/
31077087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
31083f2090d5SJed Brown {
31093f2090d5SJed Brown   PetscErrorCode ierr;
31103f2090d5SJed Brown 
31113f2090d5SJed Brown   PetscFunctionBegin;
31120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3113ae60f76fSBarry Smith   if (ts->prestep) {
31145efd42a4SStefano Zampini     Vec              U;
31155efd42a4SStefano Zampini     PetscObjectState sprev,spost;
31165efd42a4SStefano Zampini 
31175efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
31185efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3119ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
31205efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3121dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3122312ce896SJed Brown   }
31233f2090d5SJed Brown   PetscFunctionReturn(0);
31243f2090d5SJed Brown }
31253f2090d5SJed Brown 
3126b8123daeSJed Brown /*@C
3127b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3128b8123daeSJed Brown   called once at the beginning of each stage.
3129b8123daeSJed Brown 
3130b8123daeSJed Brown   Logically Collective on TS
3131b8123daeSJed Brown 
3132b8123daeSJed Brown   Input Parameters:
3133b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3134b8123daeSJed Brown - func - The function
3135b8123daeSJed Brown 
3136b8123daeSJed Brown   Calling sequence of func:
3137b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3138b8123daeSJed Brown 
3139b8123daeSJed Brown   Level: intermediate
3140b8123daeSJed Brown 
3141b8123daeSJed Brown   Note:
3142b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
314380275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3144b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3145b8123daeSJed Brown 
31469be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3147b8123daeSJed Brown @*/
3148b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3149b8123daeSJed Brown {
3150b8123daeSJed Brown   PetscFunctionBegin;
3151b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3152ae60f76fSBarry Smith   ts->prestage = func;
3153b8123daeSJed Brown   PetscFunctionReturn(0);
3154b8123daeSJed Brown }
3155b8123daeSJed Brown 
31569be3e283SDebojyoti Ghosh /*@C
31579be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31589be3e283SDebojyoti Ghosh   called once at the end of each stage.
31599be3e283SDebojyoti Ghosh 
31609be3e283SDebojyoti Ghosh   Logically Collective on TS
31619be3e283SDebojyoti Ghosh 
31629be3e283SDebojyoti Ghosh   Input Parameters:
31639be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
31649be3e283SDebojyoti Ghosh - func - The function
31659be3e283SDebojyoti Ghosh 
31669be3e283SDebojyoti Ghosh   Calling sequence of func:
31679be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
31689be3e283SDebojyoti Ghosh 
31699be3e283SDebojyoti Ghosh   Level: intermediate
31709be3e283SDebojyoti Ghosh 
31719be3e283SDebojyoti Ghosh   Note:
31729be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
317380275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
31749be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
31759be3e283SDebojyoti Ghosh 
31769be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
31779be3e283SDebojyoti Ghosh @*/
31789be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
31799be3e283SDebojyoti Ghosh {
31809be3e283SDebojyoti Ghosh   PetscFunctionBegin;
31819be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
31829be3e283SDebojyoti Ghosh   ts->poststage = func;
31839be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
31849be3e283SDebojyoti Ghosh }
31859be3e283SDebojyoti Ghosh 
3186c688d042SShri Abhyankar /*@C
3187c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3188c688d042SShri Abhyankar   called once at the end of each step evaluation.
3189c688d042SShri Abhyankar 
3190c688d042SShri Abhyankar   Logically Collective on TS
3191c688d042SShri Abhyankar 
3192c688d042SShri Abhyankar   Input Parameters:
3193c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3194c688d042SShri Abhyankar - func - The function
3195c688d042SShri Abhyankar 
3196c688d042SShri Abhyankar   Calling sequence of func:
3197c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3198c688d042SShri Abhyankar 
3199c688d042SShri Abhyankar   Level: intermediate
3200c688d042SShri Abhyankar 
3201c688d042SShri Abhyankar   Note:
32021785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
32031785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3204e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3205e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3206e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3207c688d042SShri Abhyankar 
3208c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3209c688d042SShri Abhyankar @*/
3210c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3211c688d042SShri Abhyankar {
3212c688d042SShri Abhyankar   PetscFunctionBegin;
3213c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3214c688d042SShri Abhyankar   ts->postevaluate = func;
3215c688d042SShri Abhyankar   PetscFunctionReturn(0);
3216c688d042SShri Abhyankar }
3217c688d042SShri Abhyankar 
3218b8123daeSJed Brown /*@
3219b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3220b8123daeSJed Brown 
3221b8123daeSJed Brown   Collective on TS
3222b8123daeSJed Brown 
3223b8123daeSJed Brown   Input Parameters:
3224b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
32259be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3226b8123daeSJed Brown 
3227b8123daeSJed Brown   Notes:
3228b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3229b8123daeSJed Brown   most users would not generally call this routine themselves.
3230b8123daeSJed Brown 
3231b8123daeSJed Brown   Level: developer
3232b8123daeSJed Brown 
32339be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3234b8123daeSJed Brown @*/
3235b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3236b8123daeSJed Brown {
3237b8123daeSJed Brown   PetscFunctionBegin;
3238b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3239ae60f76fSBarry Smith   if (ts->prestage) {
3240ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3241b8123daeSJed Brown   }
3242b8123daeSJed Brown   PetscFunctionReturn(0);
3243b8123daeSJed Brown }
3244b8123daeSJed Brown 
32459be3e283SDebojyoti Ghosh /*@
32469be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
32479be3e283SDebojyoti Ghosh 
32489be3e283SDebojyoti Ghosh   Collective on TS
32499be3e283SDebojyoti Ghosh 
32509be3e283SDebojyoti Ghosh   Input Parameters:
32519be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
32529be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32539be3e283SDebojyoti Ghosh   stageindex  - Stage number
32549be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32559be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32569be3e283SDebojyoti Ghosh 
32579be3e283SDebojyoti Ghosh   Notes:
32589be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32599be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32609be3e283SDebojyoti Ghosh 
32619be3e283SDebojyoti Ghosh   Level: developer
32629be3e283SDebojyoti Ghosh 
32639be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
32649be3e283SDebojyoti Ghosh @*/
32659be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
32669be3e283SDebojyoti Ghosh {
32679be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32689be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32694beae5d8SLisandro Dalcin   if (ts->poststage) {
32709be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
32719be3e283SDebojyoti Ghosh   }
32729be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32739be3e283SDebojyoti Ghosh }
32749be3e283SDebojyoti Ghosh 
3275c688d042SShri Abhyankar /*@
3276c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3277c688d042SShri Abhyankar 
3278c688d042SShri Abhyankar   Collective on TS
3279c688d042SShri Abhyankar 
3280c688d042SShri Abhyankar   Input Parameters:
3281c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3282c688d042SShri Abhyankar 
3283c688d042SShri Abhyankar   Notes:
3284c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3285c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3286c688d042SShri Abhyankar 
3287c688d042SShri Abhyankar   Level: developer
3288c688d042SShri Abhyankar 
3289c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3290c688d042SShri Abhyankar @*/
3291c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3292c688d042SShri Abhyankar {
3293c688d042SShri Abhyankar   PetscErrorCode ierr;
3294c688d042SShri Abhyankar 
3295c688d042SShri Abhyankar   PetscFunctionBegin;
3296c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3297c688d042SShri Abhyankar   if (ts->postevaluate) {
3298dcb233daSLisandro Dalcin     Vec              U;
3299dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3300dcb233daSLisandro Dalcin 
3301dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3302dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3303c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3304dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3305dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3306c688d042SShri Abhyankar   }
3307c688d042SShri Abhyankar   PetscFunctionReturn(0);
3308c688d042SShri Abhyankar }
3309c688d042SShri Abhyankar 
3310ac226902SBarry Smith /*@C
3311000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
33123f2090d5SJed Brown   called once at the end of each time step.
3313000e7ae3SMatthew Knepley 
33143f9fe445SBarry Smith   Logically Collective on TS
3315000e7ae3SMatthew Knepley 
3316000e7ae3SMatthew Knepley   Input Parameters:
3317000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3318000e7ae3SMatthew Knepley - func - The function
3319000e7ae3SMatthew Knepley 
3320000e7ae3SMatthew Knepley   Calling sequence of func:
3321b8123daeSJed Brown $ func (TS ts);
3322000e7ae3SMatthew Knepley 
33231785ff2aSShri Abhyankar   Notes:
33241785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
33251785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
33261785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
33271785ff2aSShri Abhyankar 
3328000e7ae3SMatthew Knepley   Level: intermediate
3329000e7ae3SMatthew Knepley 
3330dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3331000e7ae3SMatthew Knepley @*/
33327087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3333000e7ae3SMatthew Knepley {
3334000e7ae3SMatthew Knepley   PetscFunctionBegin;
33350700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3336ae60f76fSBarry Smith   ts->poststep = func;
3337000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3338000e7ae3SMatthew Knepley }
3339000e7ae3SMatthew Knepley 
334009ee8438SJed Brown /*@
33413f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
33423f2090d5SJed Brown 
33433f2090d5SJed Brown   Collective on TS
33443f2090d5SJed Brown 
33453f2090d5SJed Brown   Input Parameters:
33463f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33473f2090d5SJed Brown 
33483f2090d5SJed Brown   Notes:
33493f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33503f2090d5SJed Brown   so most users would not generally call this routine themselves.
33513f2090d5SJed Brown 
33523f2090d5SJed Brown   Level: developer
33533f2090d5SJed Brown 
33543f2090d5SJed Brown @*/
33557087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33563f2090d5SJed Brown {
33573f2090d5SJed Brown   PetscErrorCode ierr;
33583f2090d5SJed Brown 
33593f2090d5SJed Brown   PetscFunctionBegin;
33600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3361ae60f76fSBarry Smith   if (ts->poststep) {
33625efd42a4SStefano Zampini     Vec              U;
33635efd42a4SStefano Zampini     PetscObjectState sprev,spost;
33645efd42a4SStefano Zampini 
33655efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
33665efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3367ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
33685efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3369dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
337072ac3e02SJed Brown   }
33713f2090d5SJed Brown   PetscFunctionReturn(0);
33723f2090d5SJed Brown }
33733f2090d5SJed Brown 
3374d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3375d763cef2SBarry Smith 
3376d763cef2SBarry Smith /*@C
3377a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3378d763cef2SBarry Smith    timestep to display the iteration's  progress.
3379d763cef2SBarry Smith 
33803f9fe445SBarry Smith    Logically Collective on TS
3381d763cef2SBarry Smith 
3382d763cef2SBarry Smith    Input Parameters:
3383d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3384e213d8f1SJed Brown .  monitor - monitoring routine
3385329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
33860298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3387b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
33880298fd71SBarry Smith           (may be NULL)
3389d763cef2SBarry Smith 
3390e213d8f1SJed Brown    Calling sequence of monitor:
3391dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3392d763cef2SBarry Smith 
3393d763cef2SBarry Smith +    ts - the TS context
339463e21af5SBarry 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)
33951f06c33eSBarry Smith .    time - current time
33960910c330SBarry Smith .    u - current iterate
3397d763cef2SBarry Smith -    mctx - [optional] monitoring context
3398d763cef2SBarry Smith 
3399d763cef2SBarry Smith    Notes:
3400d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3401d763cef2SBarry Smith    already has been loaded.
3402d763cef2SBarry Smith 
340395452b02SPatrick Sanan    Fortran Notes:
340495452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3405025f1a04SBarry Smith 
3406d763cef2SBarry Smith    Level: intermediate
3407d763cef2SBarry Smith 
3408a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3409d763cef2SBarry Smith @*/
3410c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3411d763cef2SBarry Smith {
341278064530SBarry Smith   PetscErrorCode ierr;
341378064530SBarry Smith   PetscInt       i;
341478064530SBarry Smith   PetscBool      identical;
341578064530SBarry Smith 
3416d763cef2SBarry Smith   PetscFunctionBegin;
34170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
341878064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
341978064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
342078064530SBarry Smith     if (identical) PetscFunctionReturn(0);
342178064530SBarry Smith   }
342217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3423d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
34248704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3425d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3426d763cef2SBarry Smith   PetscFunctionReturn(0);
3427d763cef2SBarry Smith }
3428d763cef2SBarry Smith 
3429d763cef2SBarry Smith /*@C
3430a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3431d763cef2SBarry Smith 
34323f9fe445SBarry Smith    Logically Collective on TS
3433d763cef2SBarry Smith 
3434d763cef2SBarry Smith    Input Parameters:
3435d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3436d763cef2SBarry Smith 
3437d763cef2SBarry Smith    Notes:
3438d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3439d763cef2SBarry Smith 
3440d763cef2SBarry Smith    Level: intermediate
3441d763cef2SBarry Smith 
3442a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3443d763cef2SBarry Smith @*/
34447087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3445d763cef2SBarry Smith {
3446d952e501SBarry Smith   PetscErrorCode ierr;
3447d952e501SBarry Smith   PetscInt       i;
3448d952e501SBarry Smith 
3449d763cef2SBarry Smith   PetscFunctionBegin;
34500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3451d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34528704b422SBarry Smith     if (ts->monitordestroy[i]) {
34538704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3454d952e501SBarry Smith     }
3455d952e501SBarry Smith   }
3456d763cef2SBarry Smith   ts->numbermonitors = 0;
3457d763cef2SBarry Smith   PetscFunctionReturn(0);
3458d763cef2SBarry Smith }
3459d763cef2SBarry Smith 
3460721cd6eeSBarry Smith /*@C
3461721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
34625516499fSSatish Balay 
34635516499fSSatish Balay    Level: intermediate
346441251cbbSSatish Balay 
346563e21af5SBarry Smith .seealso:  TSMonitorSet()
346641251cbbSSatish Balay @*/
3467721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3468d763cef2SBarry Smith {
3469dfbe8321SBarry Smith   PetscErrorCode ierr;
3470721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
347141aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3472d132466eSBarry Smith 
3473d763cef2SBarry Smith   PetscFunctionBegin;
34744d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
347541aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
347641aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3477721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
347841aca3d6SBarry Smith   if (iascii) {
3479649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
348063e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
348163e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
348263e21af5SBarry Smith     } else {
34838392e04aSShri 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);
348463e21af5SBarry Smith     }
3485649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
348641aca3d6SBarry Smith   } else if (ibinary) {
348741aca3d6SBarry Smith     PetscMPIInt rank;
348841aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
348941aca3d6SBarry Smith     if (!rank) {
3490450a797fSBarry Smith       PetscBool skipHeader;
3491450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3492450a797fSBarry Smith 
3493450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3494450a797fSBarry Smith       if (!skipHeader) {
3495450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3496450a797fSBarry Smith        }
349741aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
349841aca3d6SBarry Smith     } else {
349941aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
350041aca3d6SBarry Smith     }
350141aca3d6SBarry Smith   }
3502721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3503d763cef2SBarry Smith   PetscFunctionReturn(0);
3504d763cef2SBarry Smith }
3505d763cef2SBarry Smith 
3506cc9c3a59SBarry Smith /*@C
3507cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3508cc9c3a59SBarry Smith 
3509cc9c3a59SBarry Smith    Level: intermediate
3510cc9c3a59SBarry Smith 
3511cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3512cc9c3a59SBarry Smith @*/
3513cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3514cc9c3a59SBarry Smith {
3515cc9c3a59SBarry Smith   PetscErrorCode ierr;
3516cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3517cc9c3a59SBarry Smith   PetscBool      iascii;
3518cc9c3a59SBarry Smith   PetscReal      max,min;
3519cc9c3a59SBarry Smith 
3520cc9c3a59SBarry Smith 
3521cc9c3a59SBarry Smith   PetscFunctionBegin;
3522cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3523cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3524cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3525cc9c3a59SBarry Smith   if (iascii) {
3526cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3527cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3528cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
35295132926bSBarry 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);
3530cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3531cc9c3a59SBarry Smith   }
3532cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3533cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3534cc9c3a59SBarry Smith }
3535cc9c3a59SBarry Smith 
3536cd652676SJed Brown /*@
3537cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3538cd652676SJed Brown 
3539cd652676SJed Brown    Collective on TS
3540cd652676SJed Brown 
3541cd652676SJed Brown    Input Argument:
3542cd652676SJed Brown +  ts - time stepping context
3543cd652676SJed Brown -  t - time to interpolate to
3544cd652676SJed Brown 
3545cd652676SJed Brown    Output Argument:
35460910c330SBarry Smith .  U - state at given time
3547cd652676SJed Brown 
3548cd652676SJed Brown    Level: intermediate
3549cd652676SJed Brown 
3550cd652676SJed Brown    Developer Notes:
3551cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3552cd652676SJed Brown 
3553874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3554cd652676SJed Brown @*/
35550910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3556cd652676SJed Brown {
3557cd652676SJed Brown   PetscErrorCode ierr;
3558cd652676SJed Brown 
3559cd652676SJed Brown   PetscFunctionBegin;
3560cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3561b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3562be5899b3SLisandro 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);
3563ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35640910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3565cd652676SJed Brown   PetscFunctionReturn(0);
3566cd652676SJed Brown }
3567cd652676SJed Brown 
3568d763cef2SBarry Smith /*@
35696d9e5789SSean Farley    TSStep - Steps one time step
3570d763cef2SBarry Smith 
3571d763cef2SBarry Smith    Collective on TS
3572d763cef2SBarry Smith 
3573d763cef2SBarry Smith    Input Parameter:
3574d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3575d763cef2SBarry Smith 
357627829d71SBarry Smith    Level: developer
3577d763cef2SBarry Smith 
3578b8123daeSJed Brown    Notes:
357927829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
358027829d71SBarry Smith 
3581b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3582b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3583b8123daeSJed Brown 
358419eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
358519eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
358625cb2221SBarry Smith 
35879be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3588d763cef2SBarry Smith @*/
3589193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3590d763cef2SBarry Smith {
3591dfbe8321SBarry Smith   PetscErrorCode   ierr;
3592fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3593be5899b3SLisandro Dalcin   PetscReal        ptime;
3594d763cef2SBarry Smith 
3595d763cef2SBarry Smith   PetscFunctionBegin;
35960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3597fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3598fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3599fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3600fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3601fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3602fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3603302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3604fffbeea8SBarry Smith 
3605d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
360668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3607d405a339SMatthew Knepley 
3608fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3609ef85077eSLisandro 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>");
3610a6772fa2SLisandro 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()");
3611be5899b3SLisandro 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");
3612a6772fa2SLisandro Dalcin 
3613be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3614be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
36152808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3616fc8dbba5SLisandro Dalcin 
3617d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3618193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3619d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3620fc8dbba5SLisandro Dalcin 
3621fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3622be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
36232808aa04SLisandro Dalcin     ts->steps++;
3624be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
362528d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3626d2daff3dSHong Zhang   }
3627fc8dbba5SLisandro Dalcin 
3628fc8dbba5SLisandro Dalcin   if (!ts->reason) {
362908c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
363008c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
363108c7845fSBarry Smith   }
3632fc8dbba5SLisandro Dalcin 
3633fc8dbba5SLisandro 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]);
3634fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
363508c7845fSBarry Smith   PetscFunctionReturn(0);
363608c7845fSBarry Smith }
363708c7845fSBarry Smith 
363808c7845fSBarry Smith /*@
36397cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36407cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36417cbde773SLisandro Dalcin 
36427cbde773SLisandro Dalcin    Collective on TS
36437cbde773SLisandro Dalcin 
36447cbde773SLisandro Dalcin    Input Arguments:
36457cbde773SLisandro Dalcin +  ts - time stepping context
36467cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36477cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
36487cbde773SLisandro Dalcin 
36497cbde773SLisandro Dalcin    Output Arguments:
36507cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
36517cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
36527cbde773SLisandro Dalcin 
36537cbde773SLisandro Dalcin    Level: advanced
36547cbde773SLisandro Dalcin 
36557cbde773SLisandro Dalcin    Notes:
36567cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36577cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36587cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36597cbde773SLisandro Dalcin 
36607cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36617cbde773SLisandro Dalcin @*/
36627cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36637cbde773SLisandro Dalcin {
36647cbde773SLisandro Dalcin   PetscErrorCode ierr;
36657cbde773SLisandro Dalcin 
36667cbde773SLisandro Dalcin   PetscFunctionBegin;
36677cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36687cbde773SLisandro Dalcin   PetscValidType(ts,1);
36697cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
36707cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36717cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36727cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36737cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36747cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36757cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
36767cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36777cbde773SLisandro Dalcin }
36787cbde773SLisandro Dalcin 
367905175c85SJed Brown /*@
368005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
368105175c85SJed Brown 
36821c3436cfSJed Brown    Collective on TS
368305175c85SJed Brown 
368405175c85SJed Brown    Input Arguments:
36851c3436cfSJed Brown +  ts - time stepping context
36861c3436cfSJed Brown .  order - desired order of accuracy
36870298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
368805175c85SJed Brown 
368905175c85SJed Brown    Output Arguments:
36900910c330SBarry Smith .  U - state at the end of the current step
369105175c85SJed Brown 
369205175c85SJed Brown    Level: advanced
369305175c85SJed Brown 
3694108c343cSJed Brown    Notes:
3695108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3696108c343cSJed 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.
3697108c343cSJed Brown 
36981c3436cfSJed Brown .seealso: TSStep(), TSAdapt
369905175c85SJed Brown @*/
37000910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
370105175c85SJed Brown {
370205175c85SJed Brown   PetscErrorCode ierr;
370305175c85SJed Brown 
370405175c85SJed Brown   PetscFunctionBegin;
370505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
370605175c85SJed Brown   PetscValidType(ts,1);
37070910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3708ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
37090910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
371005175c85SJed Brown   PetscFunctionReturn(0);
371105175c85SJed Brown }
371205175c85SJed Brown 
3713aad739acSMatthew G. Knepley /*@C
37142e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3715aad739acSMatthew G. Knepley 
3716aad739acSMatthew G. Knepley   Not collective
3717aad739acSMatthew G. Knepley 
3718aad739acSMatthew G. Knepley   Input Argument:
3719aad739acSMatthew G. Knepley . ts        - time stepping context
3720aad739acSMatthew G. Knepley 
3721aad739acSMatthew G. Knepley   Output Argument:
37222e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3723aad739acSMatthew G. Knepley 
3724aad739acSMatthew G. Knepley    Level: advanced
3725aad739acSMatthew G. Knepley 
3726aad739acSMatthew G. Knepley    Notes:
3727aad739acSMatthew G. Knepley    The calling sequence for the function is
37282e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3729aad739acSMatthew G. Knepley $ ts - The timestepping context
37302e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3731aad739acSMatthew G. Knepley 
37322e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3733aad739acSMatthew G. Knepley @*/
37342e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3735aad739acSMatthew G. Knepley {
3736aad739acSMatthew G. Knepley   PetscFunctionBegin;
3737aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
37382e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
37392e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3740aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3741aad739acSMatthew G. Knepley }
3742aad739acSMatthew G. Knepley 
3743aad739acSMatthew G. Knepley /*@C
37442e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3745aad739acSMatthew G. Knepley 
3746aad739acSMatthew G. Knepley   Logically collective on ts
3747aad739acSMatthew G. Knepley 
3748aad739acSMatthew G. Knepley   Input Arguments:
3749aad739acSMatthew G. Knepley + ts        - time stepping context
37502e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3751aad739acSMatthew G. Knepley 
3752aad739acSMatthew G. Knepley   Level: advanced
3753aad739acSMatthew G. Knepley 
3754aad739acSMatthew G. Knepley   Notes:
3755aad739acSMatthew G. Knepley   The calling sequence for the function is
37562e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3757aad739acSMatthew G. Knepley $ ts - The timestepping context
37582e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3759aad739acSMatthew G. Knepley 
37602e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3761aad739acSMatthew G. Knepley @*/
37622e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3763aad739acSMatthew G. Knepley {
3764aad739acSMatthew G. Knepley   PetscFunctionBegin;
3765aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
37662e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
37672e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3768aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3769aad739acSMatthew G. Knepley }
3770aad739acSMatthew G. Knepley 
3771aad739acSMatthew G. Knepley /*@
37722e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3773aad739acSMatthew G. Knepley 
3774aad739acSMatthew G. Knepley   Collective on ts
3775aad739acSMatthew G. Knepley 
3776aad739acSMatthew G. Knepley   Input Arguments:
3777aad739acSMatthew G. Knepley + ts - time stepping context
37782e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3779aad739acSMatthew G. Knepley 
3780aad739acSMatthew G. Knepley   Level: advanced
3781aad739acSMatthew G. Knepley 
3782aad739acSMatthew G. Knepley   Notes:
3783aad739acSMatthew G. Knepley   The calling sequence for the function is
37842e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3785aad739acSMatthew G. Knepley $ ts - The timestepping context
37862e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3787aad739acSMatthew G. Knepley 
37882e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3789aad739acSMatthew G. Knepley @*/
37902e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3791aad739acSMatthew G. Knepley {
3792aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3793aad739acSMatthew G. Knepley 
3794aad739acSMatthew G. Knepley   PetscFunctionBegin;
3795aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3796aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
37972e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3798aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3799aad739acSMatthew G. Knepley }
3800aad739acSMatthew G. Knepley 
3801aad739acSMatthew G. Knepley /*@C
3802aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3803aad739acSMatthew G. Knepley 
3804aad739acSMatthew G. Knepley   Not collective
3805aad739acSMatthew G. Knepley 
3806aad739acSMatthew G. Knepley   Input Argument:
3807aad739acSMatthew G. Knepley . ts         - time stepping context
3808aad739acSMatthew G. Knepley 
3809aad739acSMatthew G. Knepley   Output Argument:
3810aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3811aad739acSMatthew G. Knepley 
3812aad739acSMatthew G. Knepley   Level: advanced
3813aad739acSMatthew G. Knepley 
3814aad739acSMatthew G. Knepley   Notes:
3815aad739acSMatthew G. Knepley   The calling sequence for the function is
3816aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u)
3817aad739acSMatthew G. Knepley $ ts - The timestepping context
3818aad739acSMatthew G. Knepley $ u  - The approximate solution vector
3819aad739acSMatthew G. Knepley $ e  - The input vector in which the error is stored
3820aad739acSMatthew G. Knepley 
3821aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3822aad739acSMatthew G. Knepley @*/
3823aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3824aad739acSMatthew G. Knepley {
3825aad739acSMatthew G. Knepley   PetscFunctionBegin;
3826aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3827aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3828aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3829aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3830aad739acSMatthew G. Knepley }
3831aad739acSMatthew G. Knepley 
3832aad739acSMatthew G. Knepley /*@C
3833aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3834aad739acSMatthew G. Knepley 
3835aad739acSMatthew G. Knepley   Logically collective on ts
3836aad739acSMatthew G. Knepley 
3837aad739acSMatthew G. Knepley   Input Arguments:
3838aad739acSMatthew G. Knepley + ts         - time stepping context
3839aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3840aad739acSMatthew G. Knepley 
3841aad739acSMatthew G. Knepley   Level: advanced
3842aad739acSMatthew G. Knepley 
3843aad739acSMatthew G. Knepley   Notes:
3844aad739acSMatthew G. Knepley   The calling sequence for the function is
3845aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u)
3846aad739acSMatthew G. Knepley $ ts - The timestepping context
3847aad739acSMatthew G. Knepley $ u  - The approximate solution vector
3848aad739acSMatthew G. Knepley $ e  - The input vector in which the error is stored
3849aad739acSMatthew G. Knepley 
3850aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3851aad739acSMatthew G. Knepley @*/
3852aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3853aad739acSMatthew G. Knepley {
3854aad739acSMatthew G. Knepley   PetscFunctionBegin;
3855aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3856f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3857aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3858aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3859aad739acSMatthew G. Knepley }
3860aad739acSMatthew G. Knepley 
3861aad739acSMatthew G. Knepley /*@
3862aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3863aad739acSMatthew G. Knepley 
3864aad739acSMatthew G. Knepley   Collective on ts
3865aad739acSMatthew G. Knepley 
3866aad739acSMatthew G. Knepley   Input Arguments:
3867aad739acSMatthew G. Knepley + ts - time stepping context
3868aad739acSMatthew G. Knepley . u  - The approximate solution
3869aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3870aad739acSMatthew G. Knepley 
3871aad739acSMatthew G. Knepley   Level: advanced
3872aad739acSMatthew G. Knepley 
3873aad739acSMatthew G. Knepley   Notes:
3874aad739acSMatthew G. Knepley   The calling sequence for the function is
3875aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u)
3876aad739acSMatthew G. Knepley $ ts - The timestepping context
3877aad739acSMatthew G. Knepley $ u  - The approximate solution vector
3878aad739acSMatthew G. Knepley $ e  - The input vector in which the error is stored
3879aad739acSMatthew G. Knepley 
38802e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3881aad739acSMatthew G. Knepley @*/
3882aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3883aad739acSMatthew G. Knepley {
3884aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3885aad739acSMatthew G. Knepley 
3886aad739acSMatthew G. Knepley   PetscFunctionBegin;
3887aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3888aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3889aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
3890aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
3891aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3892aad739acSMatthew G. Knepley }
3893aad739acSMatthew G. Knepley 
3894b1cb36f3SHong Zhang /*@
38956a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
38966a4d4014SLisandro Dalcin 
38976a4d4014SLisandro Dalcin    Collective on TS
38986a4d4014SLisandro Dalcin 
38996a4d4014SLisandro Dalcin    Input Parameter:
39006a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
390163e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
390263e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
39035a3a76d0SJed Brown 
39046a4d4014SLisandro Dalcin    Level: beginner
39056a4d4014SLisandro Dalcin 
39065a3a76d0SJed Brown    Notes:
39075a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
39085a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
39095a3a76d0SJed Brown    stepped over the final time.
39105a3a76d0SJed Brown 
391163e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
39126a4d4014SLisandro Dalcin @*/
3913cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
39146a4d4014SLisandro Dalcin {
3915b06615a5SLisandro Dalcin   Vec               solution;
39166a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3917f22f69f0SBarry Smith 
39186a4d4014SLisandro Dalcin   PetscFunctionBegin;
39190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3920f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3921ee41a567SStefano 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 */
39220910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3923b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3924b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3925b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
39265a3a76d0SJed Brown     }
39270910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3928715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3929bbd56ea5SKarl Rupp   } else if (u) {
39300910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
39315a3a76d0SJed Brown   }
3932b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
393368bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3934a6772fa2SLisandro Dalcin 
3935ef85077eSLisandro 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>");
3936a6772fa2SLisandro 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()");
3937a6772fa2SLisandro 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");
3938a6772fa2SLisandro Dalcin 
3939715f1b00SHong Zhang   if (ts->forward_solve) {
3940715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3941715f1b00SHong Zhang   }
3942715f1b00SHong Zhang 
3943e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3944715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3945e7069c78SShri      TSTrajectory to incorrectly save the output files
3946e7069c78SShri   */
3947715f1b00SHong Zhang   /* reset time step and iteration counters */
39482808aa04SLisandro Dalcin   if (!ts->steps) {
39495ef26d82SJed Brown     ts->ksp_its           = 0;
39505ef26d82SJed Brown     ts->snes_its          = 0;
3951c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3952c610991cSLisandro Dalcin     ts->reject            = 0;
39532808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
39542808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
39557d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
39562808aa04SLisandro Dalcin   }
3957e97c63d7SStefano Zampini 
3958e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
3959e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
3960e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
3961e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
3962e97c63d7SStefano Zampini 
3963e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
3964e97c63d7SStefano Zampini   }
3965193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3966193ac0bcSJed Brown 
3967900f6b5bSMatthew G. Knepley   {
3968900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
3969900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
3970900f6b5bSMatthew G. Knepley     PetscBool         flg;
3971900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
3972900f6b5bSMatthew G. Knepley 
3973900f6b5bSMatthew G. Knepley     if (!incall) {
3974900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
3975900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
3976900f6b5bSMatthew G. Knepley       if (flg) {
3977900f6b5bSMatthew G. Knepley         PetscConvEst conv;
3978900f6b5bSMatthew G. Knepley         DM           dm;
3979900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
3980900f6b5bSMatthew G. Knepley         PetscInt     Nf;
3981900f6b5bSMatthew G. Knepley 
3982900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
3983900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3984900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3985900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
3986900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
3987900f6b5bSMatthew G. Knepley         ierr = PetscConvEstUseTS(conv);CHKERRQ(ierr);
3988900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
3989900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
3990900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
3991900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
3992900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
3993900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
3994900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3995900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3996900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
3997900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
3998900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
3999900f6b5bSMatthew G. Knepley       }
4000900f6b5bSMatthew G. Knepley     }
4001900f6b5bSMatthew G. Knepley   }
4002900f6b5bSMatthew G. Knepley 
4003ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4004f05ece33SBarry Smith 
4005193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4006193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4007a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4008cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4009a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4010be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4011db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4012db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4013e7069c78SShri 
40142808aa04SLisandro Dalcin     if (!ts->steps) {
40152808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40166427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40172808aa04SLisandro Dalcin     }
40186427ac75SLisandro Dalcin 
4019e1a7a14fSJed Brown     while (!ts->reason) {
40202808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40219687d888SLisandro Dalcin       if (!ts->steprollback) {
40229687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
40239687d888SLisandro Dalcin       }
4024193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4025f3b1f45cSBarry Smith       if (ts->testjacobian) {
4026f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4027f3b1f45cSBarry Smith       }
4028f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4029f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4030f3b1f45cSBarry Smith       }
4031cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
4032b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4033b1cb36f3SHong Zhang       }
403458818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
4035715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
4036715f1b00SHong Zhang       }
403710b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4038e783b05fSHong 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. */
403958818c2dSLisandro Dalcin       if (ts->steprollback) {
404058818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
404158818c2dSLisandro Dalcin       }
4042e783b05fSHong Zhang       if (!ts->steprollback) {
40432808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40441eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4045aeb4809dSShri Abhyankar       }
4046193ac0bcSJed Brown     }
40472808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40486427ac75SLisandro Dalcin 
404949354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
40500910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4051cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4052b06615a5SLisandro Dalcin       solution = u;
405363e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
40540574a7fbSJed Brown     } else {
4055ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4056cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4057b06615a5SLisandro Dalcin       solution = ts->vec_sol;
40580574a7fbSJed Brown     }
4059193ac0bcSJed Brown   }
4060d2daff3dSHong Zhang 
4061ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
406263e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
406356f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4064ef222394SBarry Smith   if (ts->adjoint_solve) {
4065ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
40662b0a91c0SBarry Smith   }
40676a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40686a4d4014SLisandro Dalcin }
40696a4d4014SLisandro Dalcin 
40707db568b7SBarry Smith /*@C
4071228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4072228d79bcSJed Brown 
4073228d79bcSJed Brown    Collective on TS
4074228d79bcSJed Brown 
4075228d79bcSJed Brown    Input Parameters:
4076228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4077228d79bcSJed Brown .  step - step number that has just completed
4078228d79bcSJed Brown .  ptime - model time of the state
40790910c330SBarry Smith -  u - state at the current model time
4080228d79bcSJed Brown 
4081228d79bcSJed Brown    Notes:
40827db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
40837db568b7SBarry Smith    Users would almost never call this routine directly.
4084228d79bcSJed Brown 
408563e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
408663e21af5SBarry Smith 
40877db568b7SBarry Smith    Level: developer
4088228d79bcSJed Brown 
4089228d79bcSJed Brown @*/
40900910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4091d763cef2SBarry Smith {
4092d6152f81SLisandro Dalcin   DM             dm;
4093a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4094d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4095d763cef2SBarry Smith 
4096d763cef2SBarry Smith   PetscFunctionBegin;
4097b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4098b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4099d6152f81SLisandro Dalcin 
4100d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4101d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4102d6152f81SLisandro Dalcin 
41038860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4104d763cef2SBarry Smith   for (i=0; i<n; i++) {
41050910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4106d763cef2SBarry Smith   }
41078860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4108d763cef2SBarry Smith   PetscFunctionReturn(0);
4109d763cef2SBarry Smith }
4110d763cef2SBarry Smith 
4111d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4112d763cef2SBarry Smith /*@C
41137db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4114a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4115d763cef2SBarry Smith 
4116d763cef2SBarry Smith    Collective on TS
4117d763cef2SBarry Smith 
4118d763cef2SBarry Smith    Input Parameters:
4119d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4120d763cef2SBarry Smith .  label - the title to put in the title bar
41217c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4122a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4123a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4124d763cef2SBarry Smith 
4125d763cef2SBarry Smith    Output Parameter:
41260b039ecaSBarry Smith .  ctx - the context
4127d763cef2SBarry Smith 
4128d763cef2SBarry Smith    Options Database Key:
41294f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
41308b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
41317db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
41327db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
41337db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
41347db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4135b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4136d763cef2SBarry Smith 
4137d763cef2SBarry Smith    Notes:
4138a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4139d763cef2SBarry Smith 
41407db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
41417db568b7SBarry Smith 
41427db568b7SBarry 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
41437db568b7SBarry 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
41447db568b7SBarry Smith    as the first argument.
41457db568b7SBarry Smith 
41467db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
41477db568b7SBarry Smith 
4148d763cef2SBarry Smith    Level: intermediate
4149d763cef2SBarry Smith 
41507db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
41517db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
41527db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
41537db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
41547db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
41557c922b88SBarry Smith 
4156d763cef2SBarry Smith @*/
4157a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4158d763cef2SBarry Smith {
41597f52daa2SLisandro Dalcin   PetscDraw      draw;
4160dfbe8321SBarry Smith   PetscErrorCode ierr;
4161d763cef2SBarry Smith 
4162d763cef2SBarry Smith   PetscFunctionBegin;
4163b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
41647f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
41657f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
41667f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4167287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
41687f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4169a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4170d763cef2SBarry Smith   PetscFunctionReturn(0);
4171d763cef2SBarry Smith }
4172d763cef2SBarry Smith 
4173b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4174d763cef2SBarry Smith {
41750b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4176c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4177dfbe8321SBarry Smith   PetscErrorCode ierr;
4178d763cef2SBarry Smith 
4179d763cef2SBarry Smith   PetscFunctionBegin;
418063e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4181b06615a5SLisandro Dalcin   if (!step) {
4182a9f9c1f6SBarry Smith     PetscDrawAxis axis;
41838b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4184a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
41858b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4186a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4187a9f9c1f6SBarry Smith   }
4188c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
41898b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
41900b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4191b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
41920b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
41936934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
41943923b477SBarry Smith   }
4195d763cef2SBarry Smith   PetscFunctionReturn(0);
4196d763cef2SBarry Smith }
4197d763cef2SBarry Smith 
4198d763cef2SBarry Smith /*@C
4199a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4200a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4201d763cef2SBarry Smith 
42020b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4203d763cef2SBarry Smith 
4204d763cef2SBarry Smith    Input Parameter:
42050b039ecaSBarry Smith .  ctx - the monitor context
4206d763cef2SBarry Smith 
4207d763cef2SBarry Smith    Level: intermediate
4208d763cef2SBarry Smith 
42094f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4210d763cef2SBarry Smith @*/
4211a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4212d763cef2SBarry Smith {
4213dfbe8321SBarry Smith   PetscErrorCode ierr;
4214d763cef2SBarry Smith 
4215d763cef2SBarry Smith   PetscFunctionBegin;
42167684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
42177684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
42187684fa3eSBarry Smith   }
42190b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
422031152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4221387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4222387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4223387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
42240b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4225d763cef2SBarry Smith   PetscFunctionReturn(0);
4226d763cef2SBarry Smith }
4227d763cef2SBarry Smith 
42281b575b74SJoseph Pusztay /*
42291b575b74SJoseph Pusztay 
42301b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
42311b575b74SJoseph Pusztay 
42321b575b74SJoseph Pusztay */
42331b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
42341b575b74SJoseph Pusztay {
42351b575b74SJoseph Pusztay   PetscDraw      draw;
42361b575b74SJoseph Pusztay   PetscErrorCode ierr;
42371b575b74SJoseph Pusztay 
42381b575b74SJoseph Pusztay   PetscFunctionBegin;
42391b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
42401b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
42411b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
42421b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
42431b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
42441b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
42451b575b74SJoseph Pusztay   PetscFunctionReturn(0);
42461b575b74SJoseph Pusztay 
42471b575b74SJoseph Pusztay }
42481b575b74SJoseph Pusztay 
42491b575b74SJoseph Pusztay /*
42501b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
42511b575b74SJoseph Pusztay */
42521b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
42531b575b74SJoseph Pusztay {
42541b575b74SJoseph Pusztay   PetscErrorCode ierr;
42551b575b74SJoseph Pusztay 
42561b575b74SJoseph Pusztay   PetscFunctionBegin;
42571b575b74SJoseph Pusztay 
42581b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
42591b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
42601b575b74SJoseph Pusztay 
42611b575b74SJoseph Pusztay   PetscFunctionReturn(0);
42621b575b74SJoseph Pusztay 
42631b575b74SJoseph Pusztay }
42641b575b74SJoseph Pusztay 
4265d763cef2SBarry Smith /*@
4266b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4267d763cef2SBarry Smith 
4268d763cef2SBarry Smith    Not Collective
4269d763cef2SBarry Smith 
4270d763cef2SBarry Smith    Input Parameter:
4271d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4272d763cef2SBarry Smith 
4273d763cef2SBarry Smith    Output Parameter:
427419eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4275d763cef2SBarry Smith 
4276d763cef2SBarry Smith    Level: beginner
4277d763cef2SBarry Smith 
4278b8123daeSJed Brown    Note:
4279b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
42809be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4281b8123daeSJed Brown 
42828f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4283d763cef2SBarry Smith 
4284d763cef2SBarry Smith @*/
42857087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4286d763cef2SBarry Smith {
4287d763cef2SBarry Smith   PetscFunctionBegin;
42880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4289f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4290d763cef2SBarry Smith   *t = ts->ptime;
4291d763cef2SBarry Smith   PetscFunctionReturn(0);
4292d763cef2SBarry Smith }
4293d763cef2SBarry Smith 
4294e5e524a1SHong Zhang /*@
4295e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4296e5e524a1SHong Zhang 
4297e5e524a1SHong Zhang    Not Collective
4298e5e524a1SHong Zhang 
4299e5e524a1SHong Zhang    Input Parameter:
4300e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4301e5e524a1SHong Zhang 
4302e5e524a1SHong Zhang    Output Parameter:
4303e5e524a1SHong Zhang .  t  - the previous time
4304e5e524a1SHong Zhang 
4305e5e524a1SHong Zhang    Level: beginner
4306e5e524a1SHong Zhang 
4307aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4308e5e524a1SHong Zhang 
4309e5e524a1SHong Zhang @*/
4310e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4311e5e524a1SHong Zhang {
4312e5e524a1SHong Zhang   PetscFunctionBegin;
4313e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4314e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4315e5e524a1SHong Zhang   *t = ts->ptime_prev;
4316e5e524a1SHong Zhang   PetscFunctionReturn(0);
4317e5e524a1SHong Zhang }
4318e5e524a1SHong Zhang 
43196a4d4014SLisandro Dalcin /*@
43206a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
43216a4d4014SLisandro Dalcin 
43223f9fe445SBarry Smith    Logically Collective on TS
43236a4d4014SLisandro Dalcin 
43246a4d4014SLisandro Dalcin    Input Parameters:
43256a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
43266a4d4014SLisandro Dalcin -  time - the time
43276a4d4014SLisandro Dalcin 
43286a4d4014SLisandro Dalcin    Level: intermediate
43296a4d4014SLisandro Dalcin 
433019eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
43316a4d4014SLisandro Dalcin 
43326a4d4014SLisandro Dalcin @*/
43337087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
43346a4d4014SLisandro Dalcin {
43356a4d4014SLisandro Dalcin   PetscFunctionBegin;
43360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4337c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
43386a4d4014SLisandro Dalcin   ts->ptime = t;
43396a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
43406a4d4014SLisandro Dalcin }
43416a4d4014SLisandro Dalcin 
4342d763cef2SBarry Smith /*@C
4343d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4344d763cef2SBarry Smith    TS options in the database.
4345d763cef2SBarry Smith 
43463f9fe445SBarry Smith    Logically Collective on TS
4347d763cef2SBarry Smith 
4348d763cef2SBarry Smith    Input Parameter:
4349d763cef2SBarry Smith +  ts     - The TS context
4350d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4351d763cef2SBarry Smith 
4352d763cef2SBarry Smith    Notes:
4353d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4354d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4355d763cef2SBarry Smith    hyphen.
4356d763cef2SBarry Smith 
4357d763cef2SBarry Smith    Level: advanced
4358d763cef2SBarry Smith 
4359d763cef2SBarry Smith .seealso: TSSetFromOptions()
4360d763cef2SBarry Smith 
4361d763cef2SBarry Smith @*/
43627087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4363d763cef2SBarry Smith {
4364dfbe8321SBarry Smith   PetscErrorCode ierr;
4365089b2837SJed Brown   SNES           snes;
4366d763cef2SBarry Smith 
4367d763cef2SBarry Smith   PetscFunctionBegin;
43680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4369d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4370089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4371089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4372d763cef2SBarry Smith   PetscFunctionReturn(0);
4373d763cef2SBarry Smith }
4374d763cef2SBarry Smith 
4375d763cef2SBarry Smith /*@C
4376d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4377d763cef2SBarry Smith    TS options in the database.
4378d763cef2SBarry Smith 
43793f9fe445SBarry Smith    Logically Collective on TS
4380d763cef2SBarry Smith 
4381d763cef2SBarry Smith    Input Parameter:
4382d763cef2SBarry Smith +  ts     - The TS context
4383d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4384d763cef2SBarry Smith 
4385d763cef2SBarry Smith    Notes:
4386d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4387d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4388d763cef2SBarry Smith    hyphen.
4389d763cef2SBarry Smith 
4390d763cef2SBarry Smith    Level: advanced
4391d763cef2SBarry Smith 
4392d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4393d763cef2SBarry Smith 
4394d763cef2SBarry Smith @*/
43957087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4396d763cef2SBarry Smith {
4397dfbe8321SBarry Smith   PetscErrorCode ierr;
4398089b2837SJed Brown   SNES           snes;
4399d763cef2SBarry Smith 
4400d763cef2SBarry Smith   PetscFunctionBegin;
44010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4402d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4403089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4404089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4405d763cef2SBarry Smith   PetscFunctionReturn(0);
4406d763cef2SBarry Smith }
4407d763cef2SBarry Smith 
4408d763cef2SBarry Smith /*@C
4409d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4410d763cef2SBarry Smith    TS options in the database.
4411d763cef2SBarry Smith 
4412d763cef2SBarry Smith    Not Collective
4413d763cef2SBarry Smith 
4414d763cef2SBarry Smith    Input Parameter:
4415d763cef2SBarry Smith .  ts - The TS context
4416d763cef2SBarry Smith 
4417d763cef2SBarry Smith    Output Parameter:
4418d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4419d763cef2SBarry Smith 
442095452b02SPatrick Sanan    Notes:
442195452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4422d763cef2SBarry Smith    sufficient length to hold the prefix.
4423d763cef2SBarry Smith 
4424d763cef2SBarry Smith    Level: intermediate
4425d763cef2SBarry Smith 
4426d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4427d763cef2SBarry Smith @*/
44287087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4429d763cef2SBarry Smith {
4430dfbe8321SBarry Smith   PetscErrorCode ierr;
4431d763cef2SBarry Smith 
4432d763cef2SBarry Smith   PetscFunctionBegin;
44330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44344482741eSBarry Smith   PetscValidPointer(prefix,2);
4435d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4436d763cef2SBarry Smith   PetscFunctionReturn(0);
4437d763cef2SBarry Smith }
4438d763cef2SBarry Smith 
4439d763cef2SBarry Smith /*@C
4440d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4441d763cef2SBarry Smith 
4442d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4443d763cef2SBarry Smith 
4444d763cef2SBarry Smith    Input Parameter:
4445d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4446d763cef2SBarry Smith 
4447d763cef2SBarry Smith    Output Parameters:
4448e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4449e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4450e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4451e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4452d763cef2SBarry Smith 
445395452b02SPatrick Sanan    Notes:
445495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4455d763cef2SBarry Smith 
4456d763cef2SBarry Smith    Level: intermediate
4457d763cef2SBarry Smith 
445880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4459d763cef2SBarry Smith 
4460d763cef2SBarry Smith @*/
4461e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4462d763cef2SBarry Smith {
4463089b2837SJed Brown   PetscErrorCode ierr;
446424989b8cSPeter Brune   DM             dm;
4465089b2837SJed Brown 
4466d763cef2SBarry Smith   PetscFunctionBegin;
446723a57915SBarry Smith   if (Amat || Pmat) {
446823a57915SBarry Smith     SNES snes;
4469089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
447023a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4471e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
447223a57915SBarry Smith   }
447324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
447424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4475d763cef2SBarry Smith   PetscFunctionReturn(0);
4476d763cef2SBarry Smith }
4477d763cef2SBarry Smith 
44782eca1d9cSJed Brown /*@C
44792eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
44802eca1d9cSJed Brown 
44812eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
44822eca1d9cSJed Brown 
44832eca1d9cSJed Brown    Input Parameter:
44842eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
44852eca1d9cSJed Brown 
44862eca1d9cSJed Brown    Output Parameters:
4487e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4488e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
44892eca1d9cSJed Brown .  f   - The function to compute the matrices
44902eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
44912eca1d9cSJed Brown 
449295452b02SPatrick Sanan    Notes:
449395452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
44942eca1d9cSJed Brown 
44952eca1d9cSJed Brown    Level: advanced
44962eca1d9cSJed Brown 
449780275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
44982eca1d9cSJed Brown 
44992eca1d9cSJed Brown @*/
4500e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
45012eca1d9cSJed Brown {
4502089b2837SJed Brown   PetscErrorCode ierr;
450324989b8cSPeter Brune   DM             dm;
45040910c330SBarry Smith 
45052eca1d9cSJed Brown   PetscFunctionBegin;
4506c0aab802Sstefano_zampini   if (Amat || Pmat) {
4507c0aab802Sstefano_zampini     SNES snes;
4508089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4509f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4510e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4511c0aab802Sstefano_zampini   }
451224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
451324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
45142eca1d9cSJed Brown   PetscFunctionReturn(0);
45152eca1d9cSJed Brown }
45162eca1d9cSJed Brown 
45171713a123SBarry Smith /*@C
451883a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
45191713a123SBarry Smith    VecView() for the solution at each timestep
45201713a123SBarry Smith 
45211713a123SBarry Smith    Collective on TS
45221713a123SBarry Smith 
45231713a123SBarry Smith    Input Parameters:
45241713a123SBarry Smith +  ts - the TS context
45251713a123SBarry Smith .  step - current time-step
4526142b95e3SSatish Balay .  ptime - current time
45270298fd71SBarry Smith -  dummy - either a viewer or NULL
45281713a123SBarry Smith 
452999fdda47SBarry Smith    Options Database:
453099fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
453199fdda47SBarry Smith 
453295452b02SPatrick Sanan    Notes:
453395452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
453499fdda47SBarry Smith        will look bad
453599fdda47SBarry Smith 
45361713a123SBarry Smith    Level: intermediate
45371713a123SBarry Smith 
4538a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
45391713a123SBarry Smith @*/
45400910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45411713a123SBarry Smith {
4542dfbe8321SBarry Smith   PetscErrorCode   ierr;
454383a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4544473a3ab2SBarry Smith   PetscDraw        draw;
45451713a123SBarry Smith 
45461713a123SBarry Smith   PetscFunctionBegin;
45476083293cSBarry Smith   if (!step && ictx->showinitial) {
45486083293cSBarry Smith     if (!ictx->initialsolution) {
45490910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
45501713a123SBarry Smith     }
45510910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
45526083293cSBarry Smith   }
4553b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45540dcf80beSBarry Smith 
45556083293cSBarry Smith   if (ictx->showinitial) {
45566083293cSBarry Smith     PetscReal pause;
45576083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
45586083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
45596083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
45606083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
45616083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
45626083293cSBarry Smith   }
45630910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4564473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
456551fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4566473a3ab2SBarry Smith     char      time[32];
4567473a3ab2SBarry Smith 
4568473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
456985b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4570473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4571473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
457251fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4573473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4574473a3ab2SBarry Smith   }
4575473a3ab2SBarry Smith 
45766083293cSBarry Smith   if (ictx->showinitial) {
45776083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
45786083293cSBarry Smith   }
45791713a123SBarry Smith   PetscFunctionReturn(0);
45801713a123SBarry Smith }
45811713a123SBarry Smith 
45829110b2e7SHong Zhang /*@C
45832d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
45842d5ee99bSBarry Smith 
45852d5ee99bSBarry Smith    Collective on TS
45862d5ee99bSBarry Smith 
45872d5ee99bSBarry Smith    Input Parameters:
45882d5ee99bSBarry Smith +  ts - the TS context
45892d5ee99bSBarry Smith .  step - current time-step
45902d5ee99bSBarry Smith .  ptime - current time
45912d5ee99bSBarry Smith -  dummy - either a viewer or NULL
45922d5ee99bSBarry Smith 
45932d5ee99bSBarry Smith    Level: intermediate
45942d5ee99bSBarry Smith 
45952d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
45962d5ee99bSBarry Smith @*/
45972d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45982d5ee99bSBarry Smith {
45992d5ee99bSBarry Smith   PetscErrorCode    ierr;
46002d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
46012d5ee99bSBarry Smith   PetscDraw         draw;
46026934998bSLisandro Dalcin   PetscDrawAxis     axis;
46032d5ee99bSBarry Smith   PetscInt          n;
46042d5ee99bSBarry Smith   PetscMPIInt       size;
46056934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
46062d5ee99bSBarry Smith   char              time[32];
46072d5ee99bSBarry Smith   const PetscScalar *U;
46082d5ee99bSBarry Smith 
46092d5ee99bSBarry Smith   PetscFunctionBegin;
46106934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
46116934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
46122d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
46136934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
46142d5ee99bSBarry Smith 
46152d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
46166934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
46176934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
46186934998bSLisandro Dalcin   if (!step) {
46196934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
46206934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
46216934998bSLisandro Dalcin   }
46222d5ee99bSBarry Smith 
46232d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
46246934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
46256934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4626a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
46276934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
46282d5ee99bSBarry Smith 
46296934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
46306934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
46312d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
46324b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
463385b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
46342d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
463551fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
46362d5ee99bSBarry Smith   }
46376934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
46382d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
463956d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
46406934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
46412d5ee99bSBarry Smith   PetscFunctionReturn(0);
46422d5ee99bSBarry Smith }
46432d5ee99bSBarry Smith 
46446083293cSBarry Smith /*@C
464583a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
46466083293cSBarry Smith 
46476083293cSBarry Smith    Collective on TS
46486083293cSBarry Smith 
46496083293cSBarry Smith    Input Parameters:
46506083293cSBarry Smith .    ctx - the monitor context
46516083293cSBarry Smith 
46526083293cSBarry Smith    Level: intermediate
46536083293cSBarry Smith 
465483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
46556083293cSBarry Smith @*/
465683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
46576083293cSBarry Smith {
46586083293cSBarry Smith   PetscErrorCode ierr;
46596083293cSBarry Smith 
46606083293cSBarry Smith   PetscFunctionBegin;
466183a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
466283a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
466383a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
46646083293cSBarry Smith   PetscFunctionReturn(0);
46656083293cSBarry Smith }
46666083293cSBarry Smith 
46676083293cSBarry Smith /*@C
466883a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
46696083293cSBarry Smith 
46706083293cSBarry Smith    Collective on TS
46716083293cSBarry Smith 
46726083293cSBarry Smith    Input Parameter:
46736083293cSBarry Smith .    ts - time-step context
46746083293cSBarry Smith 
46756083293cSBarry Smith    Output Patameter:
46766083293cSBarry Smith .    ctx - the monitor context
46776083293cSBarry Smith 
467899fdda47SBarry Smith    Options Database:
467999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
468099fdda47SBarry Smith 
46816083293cSBarry Smith    Level: intermediate
46826083293cSBarry Smith 
468383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
46846083293cSBarry Smith @*/
468583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
46866083293cSBarry Smith {
46876083293cSBarry Smith   PetscErrorCode   ierr;
46886083293cSBarry Smith 
46896083293cSBarry Smith   PetscFunctionBegin;
4690b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
469183a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4692e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4693bbd56ea5SKarl Rupp 
469483a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4695473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4696c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4697473a3ab2SBarry Smith 
4698473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4699c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
47006083293cSBarry Smith   PetscFunctionReturn(0);
47016083293cSBarry Smith }
47026083293cSBarry Smith 
47033a471f94SBarry Smith /*@C
47040ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
47050ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
47060ed3bfb6SBarry Smith 
47070ed3bfb6SBarry Smith    Collective on TS
47080ed3bfb6SBarry Smith 
47090ed3bfb6SBarry Smith    Input Parameters:
47100ed3bfb6SBarry Smith +  ts - the TS context
47110ed3bfb6SBarry Smith .  step - current time-step
47120ed3bfb6SBarry Smith .  ptime - current time
47130ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
47140ed3bfb6SBarry Smith 
47150ed3bfb6SBarry Smith    Options Database:
47160ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
47170ed3bfb6SBarry Smith 
47180ed3bfb6SBarry Smith    Level: intermediate
47190ed3bfb6SBarry Smith 
47200ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
47210ed3bfb6SBarry Smith @*/
47220ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47230ed3bfb6SBarry Smith {
47240ed3bfb6SBarry Smith   PetscErrorCode   ierr;
47250ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
47260ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
47270ed3bfb6SBarry Smith   Vec              work;
47280ed3bfb6SBarry Smith 
47290ed3bfb6SBarry Smith   PetscFunctionBegin;
47300ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47310ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
47320ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
47330ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
47340ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
47350ed3bfb6SBarry Smith   PetscFunctionReturn(0);
47360ed3bfb6SBarry Smith }
47370ed3bfb6SBarry Smith 
47380ed3bfb6SBarry Smith /*@C
473983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
47403a471f94SBarry Smith    VecView() for the error at each timestep
47413a471f94SBarry Smith 
47423a471f94SBarry Smith    Collective on TS
47433a471f94SBarry Smith 
47443a471f94SBarry Smith    Input Parameters:
47453a471f94SBarry Smith +  ts - the TS context
47463a471f94SBarry Smith .  step - current time-step
47473a471f94SBarry Smith .  ptime - current time
47480298fd71SBarry Smith -  dummy - either a viewer or NULL
47493a471f94SBarry Smith 
47500ed3bfb6SBarry Smith    Options Database:
47510ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
47520ed3bfb6SBarry Smith 
47533a471f94SBarry Smith    Level: intermediate
47543a471f94SBarry Smith 
47550ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
47563a471f94SBarry Smith @*/
47570910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47583a471f94SBarry Smith {
47593a471f94SBarry Smith   PetscErrorCode   ierr;
476083a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
476183a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
47623a471f94SBarry Smith   Vec              work;
47633a471f94SBarry Smith 
47643a471f94SBarry Smith   PetscFunctionBegin;
4765b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47660910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
47673a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
47680910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
47693a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
47703a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
47713a471f94SBarry Smith   PetscFunctionReturn(0);
47723a471f94SBarry Smith }
47733a471f94SBarry Smith 
4774af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
47756c699258SBarry Smith /*@
47762a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
47776c699258SBarry Smith 
4778d083f849SBarry Smith    Logically Collective on ts
47796c699258SBarry Smith 
47806c699258SBarry Smith    Input Parameters:
47812a808120SBarry Smith +  ts - the ODE integrator object
47822a808120SBarry Smith -  dm - the dm, cannot be NULL
47836c699258SBarry Smith 
4784e03a659cSJed Brown    Notes:
4785e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4786e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4787e03a659cSJed Brown    different problems using the same function space.
4788e03a659cSJed Brown 
47896c699258SBarry Smith    Level: intermediate
47906c699258SBarry Smith 
47916c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
47926c699258SBarry Smith @*/
47937087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
47946c699258SBarry Smith {
47956c699258SBarry Smith   PetscErrorCode ierr;
4796089b2837SJed Brown   SNES           snes;
4797942e3340SBarry Smith   DMTS           tsdm;
47986c699258SBarry Smith 
47996c699258SBarry Smith   PetscFunctionBegin;
48000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48012a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
480270663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4803942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
48042a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4805942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4806942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
480724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
480824989b8cSPeter Brune         tsdm->originaldm = dm;
480924989b8cSPeter Brune       }
481024989b8cSPeter Brune     }
48116bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
481224989b8cSPeter Brune   }
48136c699258SBarry Smith   ts->dm = dm;
4814bbd56ea5SKarl Rupp 
4815089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4816089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
48176c699258SBarry Smith   PetscFunctionReturn(0);
48186c699258SBarry Smith }
48196c699258SBarry Smith 
48206c699258SBarry Smith /*@
48216c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
48226c699258SBarry Smith 
48233f9fe445SBarry Smith    Not Collective
48246c699258SBarry Smith 
48256c699258SBarry Smith    Input Parameter:
48266c699258SBarry Smith . ts - the preconditioner context
48276c699258SBarry Smith 
48286c699258SBarry Smith    Output Parameter:
48296c699258SBarry Smith .  dm - the dm
48306c699258SBarry Smith 
48316c699258SBarry Smith    Level: intermediate
48326c699258SBarry Smith 
48336c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
48346c699258SBarry Smith @*/
48357087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
48366c699258SBarry Smith {
4837496e6a7aSJed Brown   PetscErrorCode ierr;
4838496e6a7aSJed Brown 
48396c699258SBarry Smith   PetscFunctionBegin;
48400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4841496e6a7aSJed Brown   if (!ts->dm) {
4842ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4843496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4844496e6a7aSJed Brown   }
48456c699258SBarry Smith   *dm = ts->dm;
48466c699258SBarry Smith   PetscFunctionReturn(0);
48476c699258SBarry Smith }
48481713a123SBarry Smith 
48490f5c6efeSJed Brown /*@
48500f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
48510f5c6efeSJed Brown 
48523f9fe445SBarry Smith    Logically Collective on SNES
48530f5c6efeSJed Brown 
48540f5c6efeSJed Brown    Input Parameter:
4855d42a1c89SJed Brown + snes - nonlinear solver
48560910c330SBarry Smith . U - the current state at which to evaluate the residual
4857d42a1c89SJed Brown - ctx - user context, must be a TS
48580f5c6efeSJed Brown 
48590f5c6efeSJed Brown    Output Parameter:
48600f5c6efeSJed Brown . F - the nonlinear residual
48610f5c6efeSJed Brown 
48620f5c6efeSJed Brown    Notes:
48630f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
48640f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
48650f5c6efeSJed Brown 
48660f5c6efeSJed Brown    Level: advanced
48670f5c6efeSJed Brown 
48680f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
48690f5c6efeSJed Brown @*/
48700910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
48710f5c6efeSJed Brown {
48720f5c6efeSJed Brown   TS             ts = (TS)ctx;
48730f5c6efeSJed Brown   PetscErrorCode ierr;
48740f5c6efeSJed Brown 
48750f5c6efeSJed Brown   PetscFunctionBegin;
48760f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
48770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
48780f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
48790f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
48800910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
48810f5c6efeSJed Brown   PetscFunctionReturn(0);
48820f5c6efeSJed Brown }
48830f5c6efeSJed Brown 
48840f5c6efeSJed Brown /*@
48850f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
48860f5c6efeSJed Brown 
48870f5c6efeSJed Brown    Collective on SNES
48880f5c6efeSJed Brown 
48890f5c6efeSJed Brown    Input Parameter:
48900f5c6efeSJed Brown + snes - nonlinear solver
48910910c330SBarry Smith . U - the current state at which to evaluate the residual
48920f5c6efeSJed Brown - ctx - user context, must be a TS
48930f5c6efeSJed Brown 
48940f5c6efeSJed Brown    Output Parameter:
48950f5c6efeSJed Brown + A - the Jacobian
48960f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
48970f5c6efeSJed Brown - flag - indicates any structure change in the matrix
48980f5c6efeSJed Brown 
48990f5c6efeSJed Brown    Notes:
49000f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49010f5c6efeSJed Brown 
49020f5c6efeSJed Brown    Level: developer
49030f5c6efeSJed Brown 
49040f5c6efeSJed Brown .seealso: SNESSetJacobian()
49050f5c6efeSJed Brown @*/
4906d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
49070f5c6efeSJed Brown {
49080f5c6efeSJed Brown   TS             ts = (TS)ctx;
49090f5c6efeSJed Brown   PetscErrorCode ierr;
49100f5c6efeSJed Brown 
49110f5c6efeSJed Brown   PetscFunctionBegin;
49120f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
49130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
49140f5c6efeSJed Brown   PetscValidPointer(A,3);
491594ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
49160f5c6efeSJed Brown   PetscValidPointer(B,4);
491794ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
49180f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4919d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
49200f5c6efeSJed Brown   PetscFunctionReturn(0);
49210f5c6efeSJed Brown }
4922325fc9f4SBarry Smith 
49230e4ef248SJed Brown /*@C
49249ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
49250e4ef248SJed Brown 
49260e4ef248SJed Brown    Collective on TS
49270e4ef248SJed Brown 
49280e4ef248SJed Brown    Input Arguments:
49290e4ef248SJed Brown +  ts - time stepping context
49300e4ef248SJed Brown .  t - time at which to evaluate
49310910c330SBarry Smith .  U - state at which to evaluate
49320e4ef248SJed Brown -  ctx - context
49330e4ef248SJed Brown 
49340e4ef248SJed Brown    Output Arguments:
49350e4ef248SJed Brown .  F - right hand side
49360e4ef248SJed Brown 
49370e4ef248SJed Brown    Level: intermediate
49380e4ef248SJed Brown 
49390e4ef248SJed Brown    Notes:
49400e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
49410e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
49420e4ef248SJed Brown 
49430e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
49440e4ef248SJed Brown @*/
49450910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
49460e4ef248SJed Brown {
49470e4ef248SJed Brown   PetscErrorCode ierr;
49480e4ef248SJed Brown   Mat            Arhs,Brhs;
49490e4ef248SJed Brown 
49500e4ef248SJed Brown   PetscFunctionBegin;
49510e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4952d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
49530910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
49540e4ef248SJed Brown   PetscFunctionReturn(0);
49550e4ef248SJed Brown }
49560e4ef248SJed Brown 
49570e4ef248SJed Brown /*@C
49580e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
49590e4ef248SJed Brown 
49600e4ef248SJed Brown    Collective on TS
49610e4ef248SJed Brown 
49620e4ef248SJed Brown    Input Arguments:
49630e4ef248SJed Brown +  ts - time stepping context
49640e4ef248SJed Brown .  t - time at which to evaluate
49650910c330SBarry Smith .  U - state at which to evaluate
49660e4ef248SJed Brown -  ctx - context
49670e4ef248SJed Brown 
49680e4ef248SJed Brown    Output Arguments:
49690e4ef248SJed Brown +  A - pointer to operator
49700e4ef248SJed Brown .  B - pointer to preconditioning matrix
49710e4ef248SJed Brown -  flg - matrix structure flag
49720e4ef248SJed Brown 
49730e4ef248SJed Brown    Level: intermediate
49740e4ef248SJed Brown 
49750e4ef248SJed Brown    Notes:
49760e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
49770e4ef248SJed Brown 
49780e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
49790e4ef248SJed Brown @*/
4980d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
49810e4ef248SJed Brown {
49820e4ef248SJed Brown   PetscFunctionBegin;
49830e4ef248SJed Brown   PetscFunctionReturn(0);
49840e4ef248SJed Brown }
49850e4ef248SJed Brown 
49860026cea9SSean Farley /*@C
49870026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
49880026cea9SSean Farley 
49890026cea9SSean Farley    Collective on TS
49900026cea9SSean Farley 
49910026cea9SSean Farley    Input Arguments:
49920026cea9SSean Farley +  ts - time stepping context
49930026cea9SSean Farley .  t - time at which to evaluate
49940910c330SBarry Smith .  U - state at which to evaluate
49950910c330SBarry Smith .  Udot - time derivative of state vector
49960026cea9SSean Farley -  ctx - context
49970026cea9SSean Farley 
49980026cea9SSean Farley    Output Arguments:
49990026cea9SSean Farley .  F - left hand side
50000026cea9SSean Farley 
50010026cea9SSean Farley    Level: intermediate
50020026cea9SSean Farley 
50030026cea9SSean Farley    Notes:
50040910c330SBarry 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
50050026cea9SSean Farley    user is required to write their own TSComputeIFunction.
50060026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
50070026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
50080026cea9SSean Farley 
50099ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
50109ae8fd06SBarry Smith 
50119ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
50120026cea9SSean Farley @*/
50130910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
50140026cea9SSean Farley {
50150026cea9SSean Farley   PetscErrorCode ierr;
50160026cea9SSean Farley   Mat            A,B;
50170026cea9SSean Farley 
50180026cea9SSean Farley   PetscFunctionBegin;
50190298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5020d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
50210910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
50220026cea9SSean Farley   PetscFunctionReturn(0);
50230026cea9SSean Farley }
50240026cea9SSean Farley 
50250026cea9SSean Farley /*@C
5026b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
50270026cea9SSean Farley 
50280026cea9SSean Farley    Collective on TS
50290026cea9SSean Farley 
50300026cea9SSean Farley    Input Arguments:
50310026cea9SSean Farley +  ts - time stepping context
50320026cea9SSean Farley .  t - time at which to evaluate
50330910c330SBarry Smith .  U - state at which to evaluate
50340910c330SBarry Smith .  Udot - time derivative of state vector
50350026cea9SSean Farley .  shift - shift to apply
50360026cea9SSean Farley -  ctx - context
50370026cea9SSean Farley 
50380026cea9SSean Farley    Output Arguments:
50390026cea9SSean Farley +  A - pointer to operator
50400026cea9SSean Farley .  B - pointer to preconditioning matrix
50410026cea9SSean Farley -  flg - matrix structure flag
50420026cea9SSean Farley 
5043b41af12eSJed Brown    Level: advanced
50440026cea9SSean Farley 
50450026cea9SSean Farley    Notes:
50460026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
50470026cea9SSean Farley 
5048b41af12eSJed Brown    It is only appropriate for problems of the form
5049b41af12eSJed Brown 
5050b41af12eSJed Brown $     M Udot = F(U,t)
5051b41af12eSJed Brown 
5052b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5053b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5054b41af12eSJed Brown   an implicit operator of the form
5055b41af12eSJed Brown 
5056b41af12eSJed Brown $    shift*M + J
5057b41af12eSJed Brown 
5058b41af12eSJed 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
5059b41af12eSJed Brown   a copy of M or reassemble it when requested.
5060b41af12eSJed Brown 
50610026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
50620026cea9SSean Farley @*/
5063d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
50640026cea9SSean Farley {
5065b41af12eSJed Brown   PetscErrorCode ierr;
5066b41af12eSJed Brown 
50670026cea9SSean Farley   PetscFunctionBegin;
506894ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5069b41af12eSJed Brown   ts->ijacobian.shift = shift;
50700026cea9SSean Farley   PetscFunctionReturn(0);
50710026cea9SSean Farley }
5072b41af12eSJed Brown 
5073e817cc15SEmil Constantinescu /*@
5074e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5075e817cc15SEmil Constantinescu 
5076e817cc15SEmil Constantinescu    Not Collective
5077e817cc15SEmil Constantinescu 
5078e817cc15SEmil Constantinescu    Input Parameter:
5079e817cc15SEmil Constantinescu .  ts - the TS context
5080e817cc15SEmil Constantinescu 
5081e817cc15SEmil Constantinescu    Output Parameter:
50824e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5083e817cc15SEmil Constantinescu 
5084e817cc15SEmil Constantinescu    Level: beginner
5085e817cc15SEmil Constantinescu 
5086e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5087e817cc15SEmil Constantinescu @*/
5088e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5089e817cc15SEmil Constantinescu {
5090e817cc15SEmil Constantinescu   PetscFunctionBegin;
5091e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5092e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5093e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5094e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5095e817cc15SEmil Constantinescu }
5096e817cc15SEmil Constantinescu 
5097e817cc15SEmil Constantinescu /*@
5098e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5099e817cc15SEmil Constantinescu 
5100e817cc15SEmil Constantinescu    Not Collective
5101e817cc15SEmil Constantinescu 
5102e817cc15SEmil Constantinescu    Input Parameter:
5103e817cc15SEmil Constantinescu +  ts - the TS context
51041297b224SEmil Constantinescu -  equation_type - see TSEquationType
5105e817cc15SEmil Constantinescu 
5106e817cc15SEmil Constantinescu    Level: advanced
5107e817cc15SEmil Constantinescu 
5108e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5109e817cc15SEmil Constantinescu @*/
5110e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5111e817cc15SEmil Constantinescu {
5112e817cc15SEmil Constantinescu   PetscFunctionBegin;
5113e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5114e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5115e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5116e817cc15SEmil Constantinescu }
51170026cea9SSean Farley 
51184af1b03aSJed Brown /*@
51194af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
51204af1b03aSJed Brown 
51214af1b03aSJed Brown    Not Collective
51224af1b03aSJed Brown 
51234af1b03aSJed Brown    Input Parameter:
51244af1b03aSJed Brown .  ts - the TS context
51254af1b03aSJed Brown 
51264af1b03aSJed Brown    Output Parameter:
51274af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
51284af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
51294af1b03aSJed Brown 
5130487e0bb9SJed Brown    Level: beginner
51314af1b03aSJed Brown 
5132cd652676SJed Brown    Notes:
5133cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
51344af1b03aSJed Brown 
51354af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
51364af1b03aSJed Brown @*/
51374af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
51384af1b03aSJed Brown {
51394af1b03aSJed Brown   PetscFunctionBegin;
51404af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51414af1b03aSJed Brown   PetscValidPointer(reason,2);
51424af1b03aSJed Brown   *reason = ts->reason;
51434af1b03aSJed Brown   PetscFunctionReturn(0);
51444af1b03aSJed Brown }
51454af1b03aSJed Brown 
5146d6ad946cSShri Abhyankar /*@
5147d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5148d6ad946cSShri Abhyankar 
51496b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5150d6ad946cSShri Abhyankar 
51516b221cbeSPatrick Sanan    Input Parameters:
5152d6ad946cSShri Abhyankar +  ts - the TS context
51536b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5154d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5155d6ad946cSShri Abhyankar 
5156f5abba47SShri Abhyankar    Level: advanced
5157d6ad946cSShri Abhyankar 
5158d6ad946cSShri Abhyankar    Notes:
51596b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5160d6ad946cSShri Abhyankar 
5161d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5162d6ad946cSShri Abhyankar @*/
5163d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5164d6ad946cSShri Abhyankar {
5165d6ad946cSShri Abhyankar   PetscFunctionBegin;
5166d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5167d6ad946cSShri Abhyankar   ts->reason = reason;
5168d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5169d6ad946cSShri Abhyankar }
5170d6ad946cSShri Abhyankar 
5171cc708dedSBarry Smith /*@
5172cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5173cc708dedSBarry Smith 
5174cc708dedSBarry Smith    Not Collective
5175cc708dedSBarry Smith 
5176cc708dedSBarry Smith    Input Parameter:
5177cc708dedSBarry Smith .  ts - the TS context
5178cc708dedSBarry Smith 
5179cc708dedSBarry Smith    Output Parameter:
518019eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5181cc708dedSBarry Smith 
5182487e0bb9SJed Brown    Level: beginner
5183cc708dedSBarry Smith 
5184cc708dedSBarry Smith    Notes:
5185cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5186cc708dedSBarry Smith 
5187cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5188cc708dedSBarry Smith @*/
5189cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5190cc708dedSBarry Smith {
5191cc708dedSBarry Smith   PetscFunctionBegin;
5192cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5193cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5194cc708dedSBarry Smith   *ftime = ts->solvetime;
5195cc708dedSBarry Smith   PetscFunctionReturn(0);
5196cc708dedSBarry Smith }
5197cc708dedSBarry Smith 
51982c18e0fdSBarry Smith /*@
51995ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
52009f67acb7SJed Brown    used by the time integrator.
52019f67acb7SJed Brown 
52029f67acb7SJed Brown    Not Collective
52039f67acb7SJed Brown 
52049f67acb7SJed Brown    Input Parameter:
52059f67acb7SJed Brown .  ts - TS context
52069f67acb7SJed Brown 
52079f67acb7SJed Brown    Output Parameter:
52089f67acb7SJed Brown .  nits - number of nonlinear iterations
52099f67acb7SJed Brown 
52109f67acb7SJed Brown    Notes:
52119f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
52129f67acb7SJed Brown 
52139f67acb7SJed Brown    Level: intermediate
52149f67acb7SJed Brown 
52155ef26d82SJed Brown .seealso:  TSGetKSPIterations()
52169f67acb7SJed Brown @*/
52175ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
52189f67acb7SJed Brown {
52199f67acb7SJed Brown   PetscFunctionBegin;
52209f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52219f67acb7SJed Brown   PetscValidIntPointer(nits,2);
52225ef26d82SJed Brown   *nits = ts->snes_its;
52239f67acb7SJed Brown   PetscFunctionReturn(0);
52249f67acb7SJed Brown }
52259f67acb7SJed Brown 
52269f67acb7SJed Brown /*@
52275ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
52289f67acb7SJed Brown    used by the time integrator.
52299f67acb7SJed Brown 
52309f67acb7SJed Brown    Not Collective
52319f67acb7SJed Brown 
52329f67acb7SJed Brown    Input Parameter:
52339f67acb7SJed Brown .  ts - TS context
52349f67acb7SJed Brown 
52359f67acb7SJed Brown    Output Parameter:
52369f67acb7SJed Brown .  lits - number of linear iterations
52379f67acb7SJed Brown 
52389f67acb7SJed Brown    Notes:
52399f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
52409f67acb7SJed Brown 
52419f67acb7SJed Brown    Level: intermediate
52429f67acb7SJed Brown 
52435ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
52449f67acb7SJed Brown @*/
52455ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
52469f67acb7SJed Brown {
52479f67acb7SJed Brown   PetscFunctionBegin;
52489f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52499f67acb7SJed Brown   PetscValidIntPointer(lits,2);
52505ef26d82SJed Brown   *lits = ts->ksp_its;
52519f67acb7SJed Brown   PetscFunctionReturn(0);
52529f67acb7SJed Brown }
52539f67acb7SJed Brown 
5254cef5090cSJed Brown /*@
5255cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5256cef5090cSJed Brown 
5257cef5090cSJed Brown    Not Collective
5258cef5090cSJed Brown 
5259cef5090cSJed Brown    Input Parameter:
5260cef5090cSJed Brown .  ts - TS context
5261cef5090cSJed Brown 
5262cef5090cSJed Brown    Output Parameter:
5263cef5090cSJed Brown .  rejects - number of steps rejected
5264cef5090cSJed Brown 
5265cef5090cSJed Brown    Notes:
5266cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5267cef5090cSJed Brown 
5268cef5090cSJed Brown    Level: intermediate
5269cef5090cSJed Brown 
52705ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5271cef5090cSJed Brown @*/
5272cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5273cef5090cSJed Brown {
5274cef5090cSJed Brown   PetscFunctionBegin;
5275cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5276cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5277cef5090cSJed Brown   *rejects = ts->reject;
5278cef5090cSJed Brown   PetscFunctionReturn(0);
5279cef5090cSJed Brown }
5280cef5090cSJed Brown 
5281cef5090cSJed Brown /*@
5282cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5283cef5090cSJed Brown 
5284cef5090cSJed Brown    Not Collective
5285cef5090cSJed Brown 
5286cef5090cSJed Brown    Input Parameter:
5287cef5090cSJed Brown .  ts - TS context
5288cef5090cSJed Brown 
5289cef5090cSJed Brown    Output Parameter:
5290cef5090cSJed Brown .  fails - number of failed nonlinear solves
5291cef5090cSJed Brown 
5292cef5090cSJed Brown    Notes:
5293cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5294cef5090cSJed Brown 
5295cef5090cSJed Brown    Level: intermediate
5296cef5090cSJed Brown 
52975ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5298cef5090cSJed Brown @*/
5299cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5300cef5090cSJed Brown {
5301cef5090cSJed Brown   PetscFunctionBegin;
5302cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5303cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5304cef5090cSJed Brown   *fails = ts->num_snes_failures;
5305cef5090cSJed Brown   PetscFunctionReturn(0);
5306cef5090cSJed Brown }
5307cef5090cSJed Brown 
5308cef5090cSJed Brown /*@
5309cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5310cef5090cSJed Brown 
5311cef5090cSJed Brown    Not Collective
5312cef5090cSJed Brown 
5313cef5090cSJed Brown    Input Parameter:
5314cef5090cSJed Brown +  ts - TS context
5315cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5316cef5090cSJed Brown 
5317cef5090cSJed Brown    Notes:
5318cef5090cSJed Brown    The counter is reset to zero for each step
5319cef5090cSJed Brown 
5320cef5090cSJed Brown    Options Database Key:
5321cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5322cef5090cSJed Brown 
5323cef5090cSJed Brown    Level: intermediate
5324cef5090cSJed Brown 
53255ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5326cef5090cSJed Brown @*/
5327cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5328cef5090cSJed Brown {
5329cef5090cSJed Brown   PetscFunctionBegin;
5330cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5331cef5090cSJed Brown   ts->max_reject = rejects;
5332cef5090cSJed Brown   PetscFunctionReturn(0);
5333cef5090cSJed Brown }
5334cef5090cSJed Brown 
5335cef5090cSJed Brown /*@
5336cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5337cef5090cSJed Brown 
5338cef5090cSJed Brown    Not Collective
5339cef5090cSJed Brown 
5340cef5090cSJed Brown    Input Parameter:
5341cef5090cSJed Brown +  ts - TS context
5342cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5343cef5090cSJed Brown 
5344cef5090cSJed Brown    Notes:
5345cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5346cef5090cSJed Brown 
5347cef5090cSJed Brown    Options Database Key:
5348cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5349cef5090cSJed Brown 
5350cef5090cSJed Brown    Level: intermediate
5351cef5090cSJed Brown 
53525ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5353cef5090cSJed Brown @*/
5354cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5355cef5090cSJed Brown {
5356cef5090cSJed Brown   PetscFunctionBegin;
5357cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5358cef5090cSJed Brown   ts->max_snes_failures = fails;
5359cef5090cSJed Brown   PetscFunctionReturn(0);
5360cef5090cSJed Brown }
5361cef5090cSJed Brown 
5362cef5090cSJed Brown /*@
5363cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5364cef5090cSJed Brown 
5365cef5090cSJed Brown    Not Collective
5366cef5090cSJed Brown 
5367cef5090cSJed Brown    Input Parameter:
5368cef5090cSJed Brown +  ts - TS context
5369cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5370cef5090cSJed Brown 
5371cef5090cSJed Brown    Options Database Key:
5372cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5373cef5090cSJed Brown 
5374cef5090cSJed Brown    Level: intermediate
5375cef5090cSJed Brown 
53765ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5377cef5090cSJed Brown @*/
5378cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5379cef5090cSJed Brown {
5380cef5090cSJed Brown   PetscFunctionBegin;
5381cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5382cef5090cSJed Brown   ts->errorifstepfailed = err;
5383cef5090cSJed Brown   PetscFunctionReturn(0);
5384cef5090cSJed Brown }
5385cef5090cSJed Brown 
5386fb1732b5SBarry Smith /*@C
5387fde5950dSBarry 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
5388fb1732b5SBarry Smith 
5389fb1732b5SBarry Smith    Collective on TS
5390fb1732b5SBarry Smith 
5391fb1732b5SBarry Smith    Input Parameters:
5392fb1732b5SBarry Smith +  ts - the TS context
5393fb1732b5SBarry Smith .  step - current time-step
5394fb1732b5SBarry Smith .  ptime - current time
53950910c330SBarry Smith .  u - current state
5396721cd6eeSBarry Smith -  vf - viewer and its format
5397fb1732b5SBarry Smith 
5398fb1732b5SBarry Smith    Level: intermediate
5399fb1732b5SBarry Smith 
5400fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5401fb1732b5SBarry Smith @*/
5402721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5403fb1732b5SBarry Smith {
5404fb1732b5SBarry Smith   PetscErrorCode ierr;
5405fb1732b5SBarry Smith 
5406fb1732b5SBarry Smith   PetscFunctionBegin;
5407721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5408721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5409721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5410ed81e22dSJed Brown   PetscFunctionReturn(0);
5411ed81e22dSJed Brown }
5412ed81e22dSJed Brown 
5413ed81e22dSJed Brown /*@C
5414ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5415ed81e22dSJed Brown 
5416ed81e22dSJed Brown    Collective on TS
5417ed81e22dSJed Brown 
5418ed81e22dSJed Brown    Input Parameters:
5419ed81e22dSJed Brown +  ts - the TS context
5420ed81e22dSJed Brown .  step - current time-step
5421ed81e22dSJed Brown .  ptime - current time
54220910c330SBarry Smith .  u - current state
5423ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5424ed81e22dSJed Brown 
5425ed81e22dSJed Brown    Level: intermediate
5426ed81e22dSJed Brown 
5427ed81e22dSJed Brown    Notes:
5428ed81e22dSJed 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.
5429ed81e22dSJed Brown    These are named according to the file name template.
5430ed81e22dSJed Brown 
5431ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5432ed81e22dSJed Brown 
5433ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5434ed81e22dSJed Brown @*/
54350910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5436ed81e22dSJed Brown {
5437ed81e22dSJed Brown   PetscErrorCode ierr;
5438ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5439ed81e22dSJed Brown   PetscViewer    viewer;
5440ed81e22dSJed Brown 
5441ed81e22dSJed Brown   PetscFunctionBegin;
544263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
54438caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5444ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
54450910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5446ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5447ed81e22dSJed Brown   PetscFunctionReturn(0);
5448ed81e22dSJed Brown }
5449ed81e22dSJed Brown 
5450ed81e22dSJed Brown /*@C
5451ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5452ed81e22dSJed Brown 
5453ed81e22dSJed Brown    Collective on TS
5454ed81e22dSJed Brown 
5455ed81e22dSJed Brown    Input Parameters:
5456ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5457ed81e22dSJed Brown 
5458ed81e22dSJed Brown    Level: intermediate
5459ed81e22dSJed Brown 
5460ed81e22dSJed Brown    Note:
5461ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5462ed81e22dSJed Brown 
5463ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5464ed81e22dSJed Brown @*/
5465ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5466ed81e22dSJed Brown {
5467ed81e22dSJed Brown   PetscErrorCode ierr;
5468ed81e22dSJed Brown 
5469ed81e22dSJed Brown   PetscFunctionBegin;
5470ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5471fb1732b5SBarry Smith   PetscFunctionReturn(0);
5472fb1732b5SBarry Smith }
5473fb1732b5SBarry Smith 
547484df9cb4SJed Brown /*@
5475552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
547684df9cb4SJed Brown 
5477ed81e22dSJed Brown    Collective on TS if controller has not been created yet
547884df9cb4SJed Brown 
547984df9cb4SJed Brown    Input Arguments:
5480ed81e22dSJed Brown .  ts - time stepping context
548184df9cb4SJed Brown 
548284df9cb4SJed Brown    Output Arguments:
5483ed81e22dSJed Brown .  adapt - adaptive controller
548484df9cb4SJed Brown 
548584df9cb4SJed Brown    Level: intermediate
548684df9cb4SJed Brown 
5487ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
548884df9cb4SJed Brown @*/
5489552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
549084df9cb4SJed Brown {
549184df9cb4SJed Brown   PetscErrorCode ierr;
549284df9cb4SJed Brown 
549384df9cb4SJed Brown   PetscFunctionBegin;
549484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5495bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
549684df9cb4SJed Brown   if (!ts->adapt) {
5497ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
54983bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
54991c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
550084df9cb4SJed Brown   }
5501bec58848SLisandro Dalcin   *adapt = ts->adapt;
550284df9cb4SJed Brown   PetscFunctionReturn(0);
550384df9cb4SJed Brown }
5504d6ebe24aSShri Abhyankar 
55051c3436cfSJed Brown /*@
55061c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
55071c3436cfSJed Brown 
55081c3436cfSJed Brown    Logically Collective
55091c3436cfSJed Brown 
55101c3436cfSJed Brown    Input Arguments:
55111c3436cfSJed Brown +  ts - time integration context
55121c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
55130298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
55141c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
55150298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
55161c3436cfSJed Brown 
5517a3cdaa26SBarry Smith    Options Database keys:
5518a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5519a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5520a3cdaa26SBarry Smith 
55213ff766beSShri Abhyankar    Notes:
55223ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
55233ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
55243ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
55253ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
55263ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
55273ff766beSShri Abhyankar    differential variables.
55283ff766beSShri Abhyankar 
55291c3436cfSJed Brown    Level: beginner
55301c3436cfSJed Brown 
5531c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
55321c3436cfSJed Brown @*/
55331c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
55341c3436cfSJed Brown {
55351c3436cfSJed Brown   PetscErrorCode ierr;
55361c3436cfSJed Brown 
55371c3436cfSJed Brown   PetscFunctionBegin;
5538c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
55391c3436cfSJed Brown   if (vatol) {
55401c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
55411c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
55421c3436cfSJed Brown     ts->vatol = vatol;
55431c3436cfSJed Brown   }
5544c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
55451c3436cfSJed Brown   if (vrtol) {
55461c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
55471c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
55481c3436cfSJed Brown     ts->vrtol = vrtol;
55491c3436cfSJed Brown   }
55501c3436cfSJed Brown   PetscFunctionReturn(0);
55511c3436cfSJed Brown }
55521c3436cfSJed Brown 
5553c5033834SJed Brown /*@
5554c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5555c5033834SJed Brown 
5556c5033834SJed Brown    Logically Collective
5557c5033834SJed Brown 
5558c5033834SJed Brown    Input Arguments:
5559c5033834SJed Brown .  ts - time integration context
5560c5033834SJed Brown 
5561c5033834SJed Brown    Output Arguments:
55620298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
55630298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
55640298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
55650298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5566c5033834SJed Brown 
5567c5033834SJed Brown    Level: beginner
5568c5033834SJed Brown 
5569c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5570c5033834SJed Brown @*/
5571c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5572c5033834SJed Brown {
5573c5033834SJed Brown   PetscFunctionBegin;
5574c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5575c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5576c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5577c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5578c5033834SJed Brown   PetscFunctionReturn(0);
5579c5033834SJed Brown }
5580c5033834SJed Brown 
55819c6b16b5SShri Abhyankar /*@
5582a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
55839c6b16b5SShri Abhyankar 
55849c6b16b5SShri Abhyankar    Collective on TS
55859c6b16b5SShri Abhyankar 
55869c6b16b5SShri Abhyankar    Input Arguments:
55879c6b16b5SShri Abhyankar +  ts - time stepping context
5588a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5589a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55909c6b16b5SShri Abhyankar 
55919c6b16b5SShri Abhyankar    Output Arguments:
5592a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55937453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5594a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55959c6b16b5SShri Abhyankar 
55969c6b16b5SShri Abhyankar    Level: developer
55979c6b16b5SShri Abhyankar 
5598deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
55999c6b16b5SShri Abhyankar @*/
56007453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56019c6b16b5SShri Abhyankar {
56029c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
56039c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
56047453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
56057453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
56069c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
56077453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
56087453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
56097453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
56109c6b16b5SShri Abhyankar 
56119c6b16b5SShri Abhyankar   PetscFunctionBegin;
56129c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5613a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5614a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5615a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5616a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5617a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5618a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
56198a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
56208a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5621a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
56229c6b16b5SShri Abhyankar 
56239c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
56249c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
56259c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
56269c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
56279c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
56287453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
56297453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
56307453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
56319c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
56329c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
56339c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56349c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56359c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
563676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56377453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56387453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56397453f775SEmil Constantinescu       if(tola>0.){
56407453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
56417453f775SEmil Constantinescu         na_loc++;
56427453f775SEmil Constantinescu       }
56437453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56447453f775SEmil Constantinescu       if(tolr>0.){
56457453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
56467453f775SEmil Constantinescu         nr_loc++;
56477453f775SEmil Constantinescu       }
56487453f775SEmil Constantinescu       tol=tola+tolr;
56497453f775SEmil Constantinescu       if(tol>0.){
56507453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
56517453f775SEmil Constantinescu         n_loc++;
56527453f775SEmil Constantinescu       }
56539c6b16b5SShri Abhyankar     }
56549c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56559c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56569c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
56579c6b16b5SShri Abhyankar     const PetscScalar *atol;
56589c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56599c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
566076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56617453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56627453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56637453f775SEmil Constantinescu       if(tola>0.){
56647453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
56657453f775SEmil Constantinescu         na_loc++;
56667453f775SEmil Constantinescu       }
56677453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56687453f775SEmil Constantinescu       if(tolr>0.){
56697453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
56707453f775SEmil Constantinescu         nr_loc++;
56717453f775SEmil Constantinescu       }
56727453f775SEmil Constantinescu       tol=tola+tolr;
56737453f775SEmil Constantinescu       if(tol>0.){
56747453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
56757453f775SEmil Constantinescu         n_loc++;
56767453f775SEmil Constantinescu       }
56779c6b16b5SShri Abhyankar     }
56789c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56799c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
56809c6b16b5SShri Abhyankar     const PetscScalar *rtol;
56819c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56829c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
568376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56847453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56857453f775SEmil Constantinescu       tola = ts->atol;
56867453f775SEmil Constantinescu       if(tola>0.){
56877453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
56887453f775SEmil Constantinescu         na_loc++;
56897453f775SEmil Constantinescu       }
56907453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56917453f775SEmil Constantinescu       if(tolr>0.){
56927453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
56937453f775SEmil Constantinescu         nr_loc++;
56947453f775SEmil Constantinescu       }
56957453f775SEmil Constantinescu       tol=tola+tolr;
56967453f775SEmil Constantinescu       if(tol>0.){
56977453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
56987453f775SEmil Constantinescu         n_loc++;
56997453f775SEmil Constantinescu       }
57009c6b16b5SShri Abhyankar     }
57019c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57029c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
57039c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
570476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57057453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57067453f775SEmil Constantinescu       tola = ts->atol;
57077453f775SEmil Constantinescu       if(tola>0.){
57087453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57097453f775SEmil Constantinescu         na_loc++;
57107453f775SEmil Constantinescu       }
57117453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57127453f775SEmil Constantinescu       if(tolr>0.){
57137453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57147453f775SEmil Constantinescu         nr_loc++;
57157453f775SEmil Constantinescu       }
57167453f775SEmil Constantinescu       tol=tola+tolr;
57177453f775SEmil Constantinescu       if(tol>0.){
57187453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57197453f775SEmil Constantinescu         n_loc++;
57207453f775SEmil Constantinescu       }
57219c6b16b5SShri Abhyankar     }
57229c6b16b5SShri Abhyankar   }
57239c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
57249c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
57259c6b16b5SShri Abhyankar 
57267453f775SEmil Constantinescu   err_loc[0] = sum;
57277453f775SEmil Constantinescu   err_loc[1] = suma;
57287453f775SEmil Constantinescu   err_loc[2] = sumr;
57297453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
57307453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
57317453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
57327453f775SEmil Constantinescu 
5733a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
57347453f775SEmil Constantinescu 
57357453f775SEmil Constantinescu   gsum   = err_glb[0];
57367453f775SEmil Constantinescu   gsuma  = err_glb[1];
57377453f775SEmil Constantinescu   gsumr  = err_glb[2];
57387453f775SEmil Constantinescu   n_glb  = err_glb[3];
57397453f775SEmil Constantinescu   na_glb = err_glb[4];
57407453f775SEmil Constantinescu   nr_glb = err_glb[5];
57417453f775SEmil Constantinescu 
5742b1316ef9SEmil Constantinescu   *norm  = 0.;
5743b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5744b1316ef9SEmil Constantinescu   *norma = 0.;
5745b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5746b1316ef9SEmil Constantinescu   *normr = 0.;
5747b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
57489c6b16b5SShri Abhyankar 
57499c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
57507453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
57517453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
57529c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
57539c6b16b5SShri Abhyankar }
57549c6b16b5SShri Abhyankar 
57559c6b16b5SShri Abhyankar /*@
5756a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
57579c6b16b5SShri Abhyankar 
57589c6b16b5SShri Abhyankar    Collective on TS
57599c6b16b5SShri Abhyankar 
57609c6b16b5SShri Abhyankar    Input Arguments:
57619c6b16b5SShri Abhyankar +  ts - time stepping context
5762a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5763a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57649c6b16b5SShri Abhyankar 
57659c6b16b5SShri Abhyankar    Output Arguments:
5766a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57677453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5768a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57699c6b16b5SShri Abhyankar 
57709c6b16b5SShri Abhyankar    Level: developer
57719c6b16b5SShri Abhyankar 
5772deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
57739c6b16b5SShri Abhyankar @*/
57747453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57759c6b16b5SShri Abhyankar {
57769c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57777453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
57789c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57797453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
57807453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
57817453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
57829c6b16b5SShri Abhyankar 
57839c6b16b5SShri Abhyankar   PetscFunctionBegin;
57849c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5785a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5786a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5787a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5788a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5789a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5790a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57918a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57928a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5793a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57949c6b16b5SShri Abhyankar 
57959c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57969c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57979c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57989c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57999c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58007453f775SEmil Constantinescu 
58017453f775SEmil Constantinescu   max=0.;
58027453f775SEmil Constantinescu   maxa=0.;
58037453f775SEmil Constantinescu   maxr=0.;
58047453f775SEmil Constantinescu 
58057453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
58069c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
58079c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58089c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58097453f775SEmil Constantinescu 
58107453f775SEmil Constantinescu     for (i=0; i<n; i++) {
581176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58127453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58137453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58147453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58157453f775SEmil Constantinescu       tol  = tola+tolr;
58167453f775SEmil Constantinescu       if(tola>0.){
58177453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
58187453f775SEmil Constantinescu       }
58197453f775SEmil Constantinescu       if(tolr>0.){
58207453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
58217453f775SEmil Constantinescu       }
58227453f775SEmil Constantinescu       if(tol>0.){
58237453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
58247453f775SEmil Constantinescu       }
58259c6b16b5SShri Abhyankar     }
58269c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58279c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58289c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58299c6b16b5SShri Abhyankar     const PetscScalar *atol;
58309c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58317453f775SEmil Constantinescu     for (i=0; i<n; i++) {
583276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58337453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58347453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58357453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58367453f775SEmil Constantinescu       tol  = tola+tolr;
58377453f775SEmil Constantinescu       if(tola>0.){
58387453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
58397453f775SEmil Constantinescu       }
58407453f775SEmil Constantinescu       if(tolr>0.){
58417453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
58427453f775SEmil Constantinescu       }
58437453f775SEmil Constantinescu       if(tol>0.){
58447453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
58457453f775SEmil Constantinescu       }
58469c6b16b5SShri Abhyankar     }
58479c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58489c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58499c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58517453f775SEmil Constantinescu 
58527453f775SEmil Constantinescu     for (i=0; i<n; i++) {
585376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58547453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58557453f775SEmil Constantinescu       tola = ts->atol;
58567453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58577453f775SEmil Constantinescu       tol  = tola+tolr;
58587453f775SEmil Constantinescu       if(tola>0.){
58597453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
58607453f775SEmil Constantinescu       }
58617453f775SEmil Constantinescu       if(tolr>0.){
58627453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
58637453f775SEmil Constantinescu       }
58647453f775SEmil Constantinescu       if(tol>0.){
58657453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
58667453f775SEmil Constantinescu       }
58679c6b16b5SShri Abhyankar     }
58689c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58699c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58707453f775SEmil Constantinescu 
58717453f775SEmil Constantinescu     for (i=0; i<n; i++) {
587276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58737453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58747453f775SEmil Constantinescu       tola = ts->atol;
58757453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58767453f775SEmil Constantinescu       tol  = tola+tolr;
58777453f775SEmil Constantinescu       if(tola>0.){
58787453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
58797453f775SEmil Constantinescu       }
58807453f775SEmil Constantinescu       if(tolr>0.){
58817453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
58827453f775SEmil Constantinescu       }
58837453f775SEmil Constantinescu       if(tol>0.){
58847453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
58857453f775SEmil Constantinescu       }
58869c6b16b5SShri Abhyankar     }
58879c6b16b5SShri Abhyankar   }
58889c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58899c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58907453f775SEmil Constantinescu   err_loc[0] = max;
58917453f775SEmil Constantinescu   err_loc[1] = maxa;
58927453f775SEmil Constantinescu   err_loc[2] = maxr;
5893a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58947453f775SEmil Constantinescu   gmax   = err_glb[0];
58957453f775SEmil Constantinescu   gmaxa  = err_glb[1];
58967453f775SEmil Constantinescu   gmaxr  = err_glb[2];
58979c6b16b5SShri Abhyankar 
58989c6b16b5SShri Abhyankar   *norm = gmax;
58997453f775SEmil Constantinescu   *norma = gmaxa;
59007453f775SEmil Constantinescu   *normr = gmaxr;
59019c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59027453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59037453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59049c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59059c6b16b5SShri Abhyankar }
59069c6b16b5SShri Abhyankar 
59071c3436cfSJed Brown /*@
59088a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
59091c3436cfSJed Brown 
59101c3436cfSJed Brown    Collective on TS
59111c3436cfSJed Brown 
59121c3436cfSJed Brown    Input Arguments:
59131c3436cfSJed Brown +  ts - time stepping context
5914a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5915a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5916a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
59177619abb3SShri 
59181c3436cfSJed Brown    Output Arguments:
5919a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
59208a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5921a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5922a4868fbcSLisandro Dalcin 
5923a4868fbcSLisandro Dalcin    Options Database Keys:
5924a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5925a4868fbcSLisandro Dalcin 
59261c3436cfSJed Brown    Level: developer
59271c3436cfSJed Brown 
59288a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
59291c3436cfSJed Brown @*/
59307453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59311c3436cfSJed Brown {
59328beabaa1SBarry Smith   PetscErrorCode ierr;
59331c3436cfSJed Brown 
59341c3436cfSJed Brown   PetscFunctionBegin;
5935a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
59367453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5937a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
59387453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5939a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
59401c3436cfSJed Brown   PetscFunctionReturn(0);
59411c3436cfSJed Brown }
59421c3436cfSJed Brown 
59438a175baeSEmil Constantinescu 
59448a175baeSEmil Constantinescu /*@
59458a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
59468a175baeSEmil Constantinescu 
59478a175baeSEmil Constantinescu    Collective on TS
59488a175baeSEmil Constantinescu 
59498a175baeSEmil Constantinescu    Input Arguments:
59508a175baeSEmil Constantinescu +  ts - time stepping context
59518a175baeSEmil Constantinescu .  E - error vector
59528a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
59538a175baeSEmil Constantinescu -  Y - state vector, previous time step
59548a175baeSEmil Constantinescu 
59558a175baeSEmil Constantinescu    Output Arguments:
5956a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59578a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5958a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59598a175baeSEmil Constantinescu 
59608a175baeSEmil Constantinescu    Level: developer
59618a175baeSEmil Constantinescu 
59628a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
59638a175baeSEmil Constantinescu @*/
59648a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59658a175baeSEmil Constantinescu {
59668a175baeSEmil Constantinescu   PetscErrorCode    ierr;
59678a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
59688a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
59698a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
59708a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
59718a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
59728a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
59738a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
59748a175baeSEmil Constantinescu 
59758a175baeSEmil Constantinescu   PetscFunctionBegin;
59768a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
59778a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
59788a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
59798a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
59808a175baeSEmil Constantinescu   PetscValidType(E,2);
59818a175baeSEmil Constantinescu   PetscValidType(U,3);
59828a175baeSEmil Constantinescu   PetscValidType(Y,4);
59838a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
59848a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
59858a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
59868a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
59878a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
59888a175baeSEmil Constantinescu 
59898a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
59908a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
59918a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
59928a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
59938a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59948a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59958a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
59968a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
59978a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
59988a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
59998a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
60008a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60018a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60028a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
600376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60048a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60058a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60068a175baeSEmil Constantinescu       if(tola>0.){
60078a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
60088a175baeSEmil Constantinescu         na_loc++;
60098a175baeSEmil Constantinescu       }
60108a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60118a175baeSEmil Constantinescu       if(tolr>0.){
60128a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
60138a175baeSEmil Constantinescu         nr_loc++;
60148a175baeSEmil Constantinescu       }
60158a175baeSEmil Constantinescu       tol=tola+tolr;
60168a175baeSEmil Constantinescu       if(tol>0.){
60178a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
60188a175baeSEmil Constantinescu         n_loc++;
60198a175baeSEmil Constantinescu       }
60208a175baeSEmil Constantinescu     }
60218a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60228a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60238a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60248a175baeSEmil Constantinescu     const PetscScalar *atol;
60258a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60268a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
602776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60288a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60298a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60308a175baeSEmil Constantinescu       if(tola>0.){
60318a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
60328a175baeSEmil Constantinescu         na_loc++;
60338a175baeSEmil Constantinescu       }
60348a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60358a175baeSEmil Constantinescu       if(tolr>0.){
60368a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
60378a175baeSEmil Constantinescu         nr_loc++;
60388a175baeSEmil Constantinescu       }
60398a175baeSEmil Constantinescu       tol=tola+tolr;
60408a175baeSEmil Constantinescu       if(tol>0.){
60418a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
60428a175baeSEmil Constantinescu         n_loc++;
60438a175baeSEmil Constantinescu       }
60448a175baeSEmil Constantinescu     }
60458a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60468a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60478a175baeSEmil Constantinescu     const PetscScalar *rtol;
60488a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60498a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
605076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60518a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60528a175baeSEmil Constantinescu       tola = ts->atol;
60538a175baeSEmil Constantinescu       if(tola>0.){
60548a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
60558a175baeSEmil Constantinescu         na_loc++;
60568a175baeSEmil Constantinescu       }
60578a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60588a175baeSEmil Constantinescu       if(tolr>0.){
60598a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
60608a175baeSEmil Constantinescu         nr_loc++;
60618a175baeSEmil Constantinescu       }
60628a175baeSEmil Constantinescu       tol=tola+tolr;
60638a175baeSEmil Constantinescu       if(tol>0.){
60648a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
60658a175baeSEmil Constantinescu         n_loc++;
60668a175baeSEmil Constantinescu       }
60678a175baeSEmil Constantinescu     }
60688a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60698a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
60708a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
607176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60728a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60738a175baeSEmil Constantinescu       tola = ts->atol;
60748a175baeSEmil Constantinescu       if(tola>0.){
60758a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
60768a175baeSEmil Constantinescu         na_loc++;
60778a175baeSEmil Constantinescu       }
60788a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60798a175baeSEmil Constantinescu       if(tolr>0.){
60808a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
60818a175baeSEmil Constantinescu         nr_loc++;
60828a175baeSEmil Constantinescu       }
60838a175baeSEmil Constantinescu       tol=tola+tolr;
60848a175baeSEmil Constantinescu       if(tol>0.){
60858a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
60868a175baeSEmil Constantinescu         n_loc++;
60878a175baeSEmil Constantinescu       }
60888a175baeSEmil Constantinescu     }
60898a175baeSEmil Constantinescu   }
60908a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
60918a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60928a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60938a175baeSEmil Constantinescu 
60948a175baeSEmil Constantinescu   err_loc[0] = sum;
60958a175baeSEmil Constantinescu   err_loc[1] = suma;
60968a175baeSEmil Constantinescu   err_loc[2] = sumr;
60978a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
60988a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
60998a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
61008a175baeSEmil Constantinescu 
6101a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61028a175baeSEmil Constantinescu 
61038a175baeSEmil Constantinescu   gsum   = err_glb[0];
61048a175baeSEmil Constantinescu   gsuma  = err_glb[1];
61058a175baeSEmil Constantinescu   gsumr  = err_glb[2];
61068a175baeSEmil Constantinescu   n_glb  = err_glb[3];
61078a175baeSEmil Constantinescu   na_glb = err_glb[4];
61088a175baeSEmil Constantinescu   nr_glb = err_glb[5];
61098a175baeSEmil Constantinescu 
61108a175baeSEmil Constantinescu   *norm  = 0.;
61118a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
61128a175baeSEmil Constantinescu   *norma = 0.;
61138a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
61148a175baeSEmil Constantinescu   *normr = 0.;
61158a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
61168a175baeSEmil Constantinescu 
61178a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61188a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61198a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61208a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61218a175baeSEmil Constantinescu }
61228a175baeSEmil Constantinescu 
61238a175baeSEmil Constantinescu /*@
61248a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
61258a175baeSEmil Constantinescu    Collective on TS
61268a175baeSEmil Constantinescu 
61278a175baeSEmil Constantinescu    Input Arguments:
61288a175baeSEmil Constantinescu +  ts - time stepping context
61298a175baeSEmil Constantinescu .  E - error vector
61308a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61318a175baeSEmil Constantinescu -  Y - state vector, previous time step
61328a175baeSEmil Constantinescu 
61338a175baeSEmil Constantinescu    Output Arguments:
6134a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61358a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6136a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61378a175baeSEmil Constantinescu 
61388a175baeSEmil Constantinescu    Level: developer
61398a175baeSEmil Constantinescu 
61408a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
61418a175baeSEmil Constantinescu @*/
61428a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61438a175baeSEmil Constantinescu {
61448a175baeSEmil Constantinescu   PetscErrorCode    ierr;
61458a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
61468a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
61478a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
61488a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
61498a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
61508a175baeSEmil Constantinescu 
61518a175baeSEmil Constantinescu   PetscFunctionBegin;
61528a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61538a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
61548a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
61558a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61568a175baeSEmil Constantinescu   PetscValidType(E,2);
61578a175baeSEmil Constantinescu   PetscValidType(U,3);
61588a175baeSEmil Constantinescu   PetscValidType(Y,4);
61598a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61608a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61618a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61628a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61638a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61648a175baeSEmil Constantinescu 
61658a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61668a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61678a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61688a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61698a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61708a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61718a175baeSEmil Constantinescu 
61728a175baeSEmil Constantinescu   max=0.;
61738a175baeSEmil Constantinescu   maxa=0.;
61748a175baeSEmil Constantinescu   maxr=0.;
61758a175baeSEmil Constantinescu 
61768a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
61778a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61788a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61798a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61808a175baeSEmil Constantinescu 
61818a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
618276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61838a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61848a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61858a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61868a175baeSEmil Constantinescu       tol  = tola+tolr;
61878a175baeSEmil Constantinescu       if(tola>0.){
61888a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
61898a175baeSEmil Constantinescu       }
61908a175baeSEmil Constantinescu       if(tolr>0.){
61918a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61928a175baeSEmil Constantinescu       }
61938a175baeSEmil Constantinescu       if(tol>0.){
61948a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61958a175baeSEmil Constantinescu       }
61968a175baeSEmil Constantinescu     }
61978a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61988a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61998a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62008a175baeSEmil Constantinescu     const PetscScalar *atol;
62018a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62028a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
620376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62048a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62058a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62068a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62078a175baeSEmil Constantinescu       tol  = tola+tolr;
62088a175baeSEmil Constantinescu       if(tola>0.){
62098a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62108a175baeSEmil Constantinescu       }
62118a175baeSEmil Constantinescu       if(tolr>0.){
62128a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
62138a175baeSEmil Constantinescu       }
62148a175baeSEmil Constantinescu       if(tol>0.){
62158a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
62168a175baeSEmil Constantinescu       }
62178a175baeSEmil Constantinescu     }
62188a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62198a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62208a175baeSEmil Constantinescu     const PetscScalar *rtol;
62218a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62228a175baeSEmil Constantinescu 
62238a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
622476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62258a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62268a175baeSEmil Constantinescu       tola = ts->atol;
62278a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62288a175baeSEmil Constantinescu       tol  = tola+tolr;
62298a175baeSEmil Constantinescu       if(tola>0.){
62308a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62318a175baeSEmil Constantinescu       }
62328a175baeSEmil Constantinescu       if(tolr>0.){
62338a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
62348a175baeSEmil Constantinescu       }
62358a175baeSEmil Constantinescu       if(tol>0.){
62368a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
62378a175baeSEmil Constantinescu       }
62388a175baeSEmil Constantinescu     }
62398a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62408a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
62418a175baeSEmil Constantinescu 
62428a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
624376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62448a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62458a175baeSEmil Constantinescu       tola = ts->atol;
62468a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62478a175baeSEmil Constantinescu       tol  = tola+tolr;
62488a175baeSEmil Constantinescu       if(tola>0.){
62498a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62508a175baeSEmil Constantinescu       }
62518a175baeSEmil Constantinescu       if(tolr>0.){
62528a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
62538a175baeSEmil Constantinescu       }
62548a175baeSEmil Constantinescu       if(tol>0.){
62558a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
62568a175baeSEmil Constantinescu       }
62578a175baeSEmil Constantinescu     }
62588a175baeSEmil Constantinescu   }
62598a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62608a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62618a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62628a175baeSEmil Constantinescu   err_loc[0] = max;
62638a175baeSEmil Constantinescu   err_loc[1] = maxa;
62648a175baeSEmil Constantinescu   err_loc[2] = maxr;
6265a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62668a175baeSEmil Constantinescu   gmax   = err_glb[0];
62678a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
62688a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
62698a175baeSEmil Constantinescu 
62708a175baeSEmil Constantinescu   *norm = gmax;
62718a175baeSEmil Constantinescu   *norma = gmaxa;
62728a175baeSEmil Constantinescu   *normr = gmaxr;
62738a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62748a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62758a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62768a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62778a175baeSEmil Constantinescu }
62788a175baeSEmil Constantinescu 
62798a175baeSEmil Constantinescu /*@
62808a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
62818a175baeSEmil Constantinescu 
62828a175baeSEmil Constantinescu    Collective on TS
62838a175baeSEmil Constantinescu 
62848a175baeSEmil Constantinescu    Input Arguments:
62858a175baeSEmil Constantinescu +  ts - time stepping context
62868a175baeSEmil Constantinescu .  E - error vector
62878a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62888a175baeSEmil Constantinescu .  Y - state vector, previous time step
62898a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
62908a175baeSEmil Constantinescu 
62918a175baeSEmil Constantinescu    Output Arguments:
6292a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
62938a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6294a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
62958a175baeSEmil Constantinescu 
62968a175baeSEmil Constantinescu    Options Database Keys:
62978a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
62988a175baeSEmil Constantinescu 
62998a175baeSEmil Constantinescu    Level: developer
63008a175baeSEmil Constantinescu 
63018a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
63028a175baeSEmil Constantinescu @*/
63038a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63048a175baeSEmil Constantinescu {
63058a175baeSEmil Constantinescu   PetscErrorCode ierr;
63068a175baeSEmil Constantinescu 
63078a175baeSEmil Constantinescu   PetscFunctionBegin;
63088a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
63098a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
63108a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
63118a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
63128a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
63138a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63148a175baeSEmil Constantinescu }
63158a175baeSEmil Constantinescu 
63168a175baeSEmil Constantinescu 
63178d59e960SJed Brown /*@
63188d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
63198d59e960SJed Brown 
63208d59e960SJed Brown    Logically Collective on TS
63218d59e960SJed Brown 
63228d59e960SJed Brown    Input Arguments:
63238d59e960SJed Brown +  ts - time stepping context
63248d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
63258d59e960SJed Brown 
63268d59e960SJed Brown    Note:
63278d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
63288d59e960SJed Brown 
63298d59e960SJed Brown    Level: intermediate
63308d59e960SJed Brown 
63318d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
63328d59e960SJed Brown @*/
63338d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
63348d59e960SJed Brown {
63358d59e960SJed Brown   PetscFunctionBegin;
63368d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63378d59e960SJed Brown   ts->cfltime_local = cfltime;
63388d59e960SJed Brown   ts->cfltime       = -1.;
63398d59e960SJed Brown   PetscFunctionReturn(0);
63408d59e960SJed Brown }
63418d59e960SJed Brown 
63428d59e960SJed Brown /*@
63438d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
63448d59e960SJed Brown 
63458d59e960SJed Brown    Collective on TS
63468d59e960SJed Brown 
63478d59e960SJed Brown    Input Arguments:
63488d59e960SJed Brown .  ts - time stepping context
63498d59e960SJed Brown 
63508d59e960SJed Brown    Output Arguments:
63518d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
63528d59e960SJed Brown 
63538d59e960SJed Brown    Level: advanced
63548d59e960SJed Brown 
63558d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
63568d59e960SJed Brown @*/
63578d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
63588d59e960SJed Brown {
63598d59e960SJed Brown   PetscErrorCode ierr;
63608d59e960SJed Brown 
63618d59e960SJed Brown   PetscFunctionBegin;
63628d59e960SJed Brown   if (ts->cfltime < 0) {
6363b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63648d59e960SJed Brown   }
63658d59e960SJed Brown   *cfltime = ts->cfltime;
63668d59e960SJed Brown   PetscFunctionReturn(0);
63678d59e960SJed Brown }
63688d59e960SJed Brown 
6369d6ebe24aSShri Abhyankar /*@
6370d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6371d6ebe24aSShri Abhyankar 
6372d6ebe24aSShri Abhyankar    Input Parameters:
6373a2b725a8SWilliam Gropp +  ts   - the TS context.
6374d6ebe24aSShri Abhyankar .  xl   - lower bound.
6375a2b725a8SWilliam Gropp -  xu   - upper bound.
6376d6ebe24aSShri Abhyankar 
6377d6ebe24aSShri Abhyankar    Notes:
6378d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6379e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6380d6ebe24aSShri Abhyankar 
63812bd2b0e6SSatish Balay    Level: advanced
63822bd2b0e6SSatish Balay 
6383d6ebe24aSShri Abhyankar @*/
6384d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6385d6ebe24aSShri Abhyankar {
6386d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6387d6ebe24aSShri Abhyankar   SNES           snes;
6388d6ebe24aSShri Abhyankar 
6389d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6390d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6391d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6392d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6393d6ebe24aSShri Abhyankar }
6394d6ebe24aSShri Abhyankar 
6395b3603a34SBarry Smith /*@C
63964f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6397b3603a34SBarry Smith        in a time based line graph
6398b3603a34SBarry Smith 
6399b3603a34SBarry Smith    Collective on TS
6400b3603a34SBarry Smith 
6401b3603a34SBarry Smith    Input Parameters:
6402b3603a34SBarry Smith +  ts - the TS context
6403b3603a34SBarry Smith .  step - current time-step
6404b3603a34SBarry Smith .  ptime - current time
64057db568b7SBarry Smith .  u - current solution
64067db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6407b3603a34SBarry Smith 
6408b3d3934dSBarry Smith    Options Database:
64099ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6410b3d3934dSBarry Smith 
6411b3603a34SBarry Smith    Level: intermediate
6412b3603a34SBarry Smith 
641395452b02SPatrick Sanan    Notes:
641495452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6415b3603a34SBarry Smith 
64167db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
64177db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
64187db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
64197db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6420b3603a34SBarry Smith @*/
64217db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6422b3603a34SBarry Smith {
6423b3603a34SBarry Smith   PetscErrorCode    ierr;
64247db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6425b3603a34SBarry Smith   const PetscScalar *yy;
642680666b62SBarry Smith   Vec               v;
6427b3603a34SBarry Smith 
6428b3603a34SBarry Smith   PetscFunctionBegin;
642963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
643058ff32f7SBarry Smith   if (!step) {
6431a9f9c1f6SBarry Smith     PetscDrawAxis axis;
64326934998bSLisandro Dalcin     PetscInt      dim;
6433a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
64340ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6435bab0a581SBarry Smith     if (!ctx->names) {
6436bab0a581SBarry Smith       PetscBool flg;
6437bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6438bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6439bab0a581SBarry Smith       if (flg) {
6440bab0a581SBarry Smith         PetscInt i,n;
6441bab0a581SBarry Smith         char     **names;
6442bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6443bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6444bab0a581SBarry Smith         for (i=0; i<n; i++) {
6445bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6446bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6447bab0a581SBarry Smith         }
6448bab0a581SBarry Smith         names[n] = NULL;
6449bab0a581SBarry Smith         ctx->names = names;
6450bab0a581SBarry Smith       }
6451bab0a581SBarry Smith     }
6452387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6453387f4636SBarry Smith       char      **displaynames;
6454387f4636SBarry Smith       PetscBool flg;
6455387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6456580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6457c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6458387f4636SBarry Smith       if (flg) {
6459a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6460387f4636SBarry Smith       }
6461387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6462387f4636SBarry Smith     }
6463387f4636SBarry Smith     if (ctx->displaynames) {
6464387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6465387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6466387f4636SBarry Smith     } else if (ctx->names) {
64670910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
64680b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6469387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6470b0bc92c6SBarry Smith     } else {
6471b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6472b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6473387f4636SBarry Smith     }
64740b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
647558ff32f7SBarry Smith   }
64766934998bSLisandro Dalcin 
64776934998bSLisandro Dalcin   if (!ctx->transform) v = u;
64786934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
647980666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
64806934998bSLisandro Dalcin   if (ctx->displaynames) {
64816934998bSLisandro Dalcin     PetscInt i;
64826934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
64836934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
64846934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
64856934998bSLisandro Dalcin   } else {
6486e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6487e3efe391SJed Brown     PetscInt  i,n;
64886934998bSLisandro Dalcin     PetscReal *yreal;
648980666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6490785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6491e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
64920b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6493e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6494e3efe391SJed Brown #else
64950b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6496e3efe391SJed Brown #endif
649780666b62SBarry Smith   }
64986934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
64996934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
65006934998bSLisandro Dalcin 
6501b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65020b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65036934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65043923b477SBarry Smith   }
6505b3603a34SBarry Smith   PetscFunctionReturn(0);
6506b3603a34SBarry Smith }
6507b3603a34SBarry Smith 
6508b037adc7SBarry Smith /*@C
650931152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6510b037adc7SBarry Smith 
6511b037adc7SBarry Smith    Collective on TS
6512b037adc7SBarry Smith 
6513b037adc7SBarry Smith    Input Parameters:
6514b037adc7SBarry Smith +  ts - the TS context
6515b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6516b037adc7SBarry Smith 
6517b037adc7SBarry Smith    Level: intermediate
6518b037adc7SBarry Smith 
651995452b02SPatrick Sanan    Notes:
652095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
65217db568b7SBarry Smith 
6522a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6523b037adc7SBarry Smith @*/
652431152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6525b037adc7SBarry Smith {
6526b037adc7SBarry Smith   PetscErrorCode    ierr;
6527b037adc7SBarry Smith   PetscInt          i;
6528b037adc7SBarry Smith 
6529b037adc7SBarry Smith   PetscFunctionBegin;
6530b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6531b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
65325537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6533b3d3934dSBarry Smith       break;
6534b3d3934dSBarry Smith     }
6535b3d3934dSBarry Smith   }
6536b3d3934dSBarry Smith   PetscFunctionReturn(0);
6537b3d3934dSBarry Smith }
6538b3d3934dSBarry Smith 
6539e673d494SBarry Smith /*@C
6540e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6541e673d494SBarry Smith 
6542e673d494SBarry Smith    Collective on TS
6543e673d494SBarry Smith 
6544e673d494SBarry Smith    Input Parameters:
6545e673d494SBarry Smith +  ts - the TS context
6546e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6547e673d494SBarry Smith 
6548e673d494SBarry Smith    Level: intermediate
6549e673d494SBarry Smith 
6550a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6551e673d494SBarry Smith @*/
6552e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6553e673d494SBarry Smith {
6554e673d494SBarry Smith   PetscErrorCode    ierr;
6555e673d494SBarry Smith 
6556e673d494SBarry Smith   PetscFunctionBegin;
6557e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6558e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6559e673d494SBarry Smith   PetscFunctionReturn(0);
6560e673d494SBarry Smith }
6561e673d494SBarry Smith 
6562b3d3934dSBarry Smith /*@C
6563b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6564b3d3934dSBarry Smith 
6565b3d3934dSBarry Smith    Collective on TS
6566b3d3934dSBarry Smith 
6567b3d3934dSBarry Smith    Input Parameter:
6568b3d3934dSBarry Smith .  ts - the TS context
6569b3d3934dSBarry Smith 
6570b3d3934dSBarry Smith    Output Parameter:
6571b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6572b3d3934dSBarry Smith 
6573b3d3934dSBarry Smith    Level: intermediate
6574b3d3934dSBarry Smith 
657595452b02SPatrick Sanan    Notes:
657695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
65777db568b7SBarry Smith 
6578b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6579b3d3934dSBarry Smith @*/
6580b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6581b3d3934dSBarry Smith {
6582b3d3934dSBarry Smith   PetscInt       i;
6583b3d3934dSBarry Smith 
6584b3d3934dSBarry Smith   PetscFunctionBegin;
6585b3d3934dSBarry Smith   *names = NULL;
6586b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6587b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
65885537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6589b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6590b3d3934dSBarry Smith       break;
6591387f4636SBarry Smith     }
6592387f4636SBarry Smith   }
6593387f4636SBarry Smith   PetscFunctionReturn(0);
6594387f4636SBarry Smith }
6595387f4636SBarry Smith 
6596a66092f1SBarry Smith /*@C
6597a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6598a66092f1SBarry Smith 
6599a66092f1SBarry Smith    Collective on TS
6600a66092f1SBarry Smith 
6601a66092f1SBarry Smith    Input Parameters:
6602a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6603a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6604a66092f1SBarry Smith 
6605a66092f1SBarry Smith    Level: intermediate
6606a66092f1SBarry Smith 
6607a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6608a66092f1SBarry Smith @*/
6609a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6610a66092f1SBarry Smith {
6611a66092f1SBarry Smith   PetscInt          j = 0,k;
6612a66092f1SBarry Smith   PetscErrorCode    ierr;
6613a66092f1SBarry Smith 
6614a66092f1SBarry Smith   PetscFunctionBegin;
6615a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6616a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6617a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6618a66092f1SBarry Smith   while (displaynames[j]) j++;
6619a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6620a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6621a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6622a66092f1SBarry Smith   j = 0;
6623a66092f1SBarry Smith   while (displaynames[j]) {
6624a66092f1SBarry Smith     k = 0;
6625a66092f1SBarry Smith     while (ctx->names[k]) {
6626a66092f1SBarry Smith       PetscBool flg;
6627a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6628a66092f1SBarry Smith       if (flg) {
6629a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6630a66092f1SBarry Smith         break;
6631a66092f1SBarry Smith       }
6632a66092f1SBarry Smith       k++;
6633a66092f1SBarry Smith     }
6634a66092f1SBarry Smith     j++;
6635a66092f1SBarry Smith   }
6636a66092f1SBarry Smith   PetscFunctionReturn(0);
6637a66092f1SBarry Smith }
6638a66092f1SBarry Smith 
6639387f4636SBarry Smith /*@C
6640387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6641387f4636SBarry Smith 
6642387f4636SBarry Smith    Collective on TS
6643387f4636SBarry Smith 
6644387f4636SBarry Smith    Input Parameters:
6645387f4636SBarry Smith +  ts - the TS context
6646a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6647387f4636SBarry Smith 
664895452b02SPatrick Sanan    Notes:
664995452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66507db568b7SBarry Smith 
6651387f4636SBarry Smith    Level: intermediate
6652387f4636SBarry Smith 
6653387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6654387f4636SBarry Smith @*/
6655387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6656387f4636SBarry Smith {
6657a66092f1SBarry Smith   PetscInt          i;
6658387f4636SBarry Smith   PetscErrorCode    ierr;
6659387f4636SBarry Smith 
6660387f4636SBarry Smith   PetscFunctionBegin;
6661387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6662387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66635537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6664b3d3934dSBarry Smith       break;
6665b037adc7SBarry Smith     }
6666b037adc7SBarry Smith   }
6667b037adc7SBarry Smith   PetscFunctionReturn(0);
6668b037adc7SBarry Smith }
6669b037adc7SBarry Smith 
667080666b62SBarry Smith /*@C
667180666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
667280666b62SBarry Smith 
667380666b62SBarry Smith    Collective on TS
667480666b62SBarry Smith 
667580666b62SBarry Smith    Input Parameters:
667680666b62SBarry Smith +  ts - the TS context
667780666b62SBarry Smith .  transform - the transform function
66787684fa3eSBarry Smith .  destroy - function to destroy the optional context
667980666b62SBarry Smith -  ctx - optional context used by transform function
668080666b62SBarry Smith 
668195452b02SPatrick Sanan    Notes:
668295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66837db568b7SBarry Smith 
668480666b62SBarry Smith    Level: intermediate
668580666b62SBarry Smith 
6686a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
668780666b62SBarry Smith @*/
66887684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
668980666b62SBarry Smith {
669080666b62SBarry Smith   PetscInt          i;
6691a66092f1SBarry Smith   PetscErrorCode    ierr;
669280666b62SBarry Smith 
669380666b62SBarry Smith   PetscFunctionBegin;
669480666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
669580666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66965537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
669780666b62SBarry Smith     }
669880666b62SBarry Smith   }
669980666b62SBarry Smith   PetscFunctionReturn(0);
670080666b62SBarry Smith }
670180666b62SBarry Smith 
6702e673d494SBarry Smith /*@C
6703e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6704e673d494SBarry Smith 
6705e673d494SBarry Smith    Collective on TSLGCtx
6706e673d494SBarry Smith 
6707e673d494SBarry Smith    Input Parameters:
6708e673d494SBarry Smith +  ts - the TS context
6709e673d494SBarry Smith .  transform - the transform function
67107684fa3eSBarry Smith .  destroy - function to destroy the optional context
6711e673d494SBarry Smith -  ctx - optional context used by transform function
6712e673d494SBarry Smith 
6713e673d494SBarry Smith    Level: intermediate
6714e673d494SBarry Smith 
6715a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6716e673d494SBarry Smith @*/
67177684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6718e673d494SBarry Smith {
6719e673d494SBarry Smith   PetscFunctionBegin;
6720e673d494SBarry Smith   ctx->transform    = transform;
67217684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6722e673d494SBarry Smith   ctx->transformctx = tctx;
6723e673d494SBarry Smith   PetscFunctionReturn(0);
6724e673d494SBarry Smith }
6725e673d494SBarry Smith 
6726ef20d060SBarry Smith /*@C
67278b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6728ef20d060SBarry Smith        in a time based line graph
6729ef20d060SBarry Smith 
6730ef20d060SBarry Smith    Collective on TS
6731ef20d060SBarry Smith 
6732ef20d060SBarry Smith    Input Parameters:
6733ef20d060SBarry Smith +  ts - the TS context
6734ef20d060SBarry Smith .  step - current time-step
6735ef20d060SBarry Smith .  ptime - current time
67367db568b7SBarry Smith .  u - current solution
67377db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6738ef20d060SBarry Smith 
6739ef20d060SBarry Smith    Level: intermediate
6740ef20d060SBarry Smith 
674195452b02SPatrick Sanan    Notes:
674295452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6743abd5a294SJed Brown 
6744abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6745abd5a294SJed Brown 
6746abd5a294SJed Brown    Options Database Keys:
67474f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6748ef20d060SBarry Smith 
6749abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6750ef20d060SBarry Smith @*/
67510910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6752ef20d060SBarry Smith {
6753ef20d060SBarry Smith   PetscErrorCode    ierr;
67540b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6755ef20d060SBarry Smith   const PetscScalar *yy;
6756ef20d060SBarry Smith   Vec               y;
6757ef20d060SBarry Smith 
6758ef20d060SBarry Smith   PetscFunctionBegin;
6759a9f9c1f6SBarry Smith   if (!step) {
6760a9f9c1f6SBarry Smith     PetscDrawAxis axis;
67616934998bSLisandro Dalcin     PetscInt      dim;
6762a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
67638b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
67640910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6765a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6766a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6767a9f9c1f6SBarry Smith   }
67680910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6769ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
67700910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6771ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6772e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6773e3efe391SJed Brown   {
6774e3efe391SJed Brown     PetscReal *yreal;
6775e3efe391SJed Brown     PetscInt  i,n;
6776e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6777785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6778e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
67790b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6780e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6781e3efe391SJed Brown   }
6782e3efe391SJed Brown #else
67830b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6784e3efe391SJed Brown #endif
6785ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6786ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6787b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
67880b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
67896934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
67903923b477SBarry Smith   }
6791ef20d060SBarry Smith   PetscFunctionReturn(0);
6792ef20d060SBarry Smith }
6793ef20d060SBarry Smith 
67945e3b7effSJoseph Pusztay /*@C
67955e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
67965e3b7effSJoseph Pusztay 
67975e3b7effSJoseph Pusztay    Input Parameters:
67985e3b7effSJoseph Pusztay +  ts - the TS context
67995e3b7effSJoseph Pusztay .  step - current time-step
68005e3b7effSJoseph Pusztay .  ptime - current time
68015e3b7effSJoseph Pusztay .  u - current solution
68025e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
68035e3b7effSJoseph Pusztay 
68045e3b7effSJoseph Pusztay    Options Database:
6805918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
68065e3b7effSJoseph Pusztay 
68075e3b7effSJoseph Pusztay    Level: intermediate
68085e3b7effSJoseph Pusztay 
68095e3b7effSJoseph Pusztay @*/
68100ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
68111b575b74SJoseph Pusztay {
68121b575b74SJoseph Pusztay   PetscErrorCode    ierr;
68131b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
68141b575b74SJoseph Pusztay   const PetscScalar *yy;
6815b1670a61SJoseph Pusztay   PetscReal       *y,*x;
68161b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
68171b575b74SJoseph Pusztay   DM                dm;
68181b575b74SJoseph Pusztay 
68191b575b74SJoseph Pusztay   PetscFunctionBegin;
68201b575b74SJoseph Pusztay 
68211b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
68221b575b74SJoseph Pusztay   if (!step) {
68231b575b74SJoseph Pusztay     PetscDrawAxis axis;
68241b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
68251b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6826895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6827895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
68281b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
68291b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
68301b575b74SJoseph Pusztay     if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
68311b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
68321b575b74SJoseph Pusztay     Np /= 2*dim;
68331b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
68341b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
68351b575b74SJoseph Pusztay   }
68361b575b74SJoseph Pusztay 
68371b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
68381b575b74SJoseph Pusztay   Np /= 2*dim;
68391b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6840895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
68411b575b74SJoseph Pusztay   /* get points from solution vector */
68421b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6843b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6844b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6845895f37d5SJoseph Pusztay   }
68461b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
68471b575b74SJoseph Pusztay 
68481b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68491b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
68501b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
68511b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
68521b575b74SJoseph Pusztay   }
68531b575b74SJoseph Pusztay 
6854918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6855918b1d10SJoseph Pusztay 
68561b575b74SJoseph Pusztay   PetscFunctionReturn(0);
68571b575b74SJoseph Pusztay }
68581b575b74SJoseph Pusztay 
68591b575b74SJoseph Pusztay 
68601b575b74SJoseph Pusztay 
68617cf37e64SBarry Smith /*@C
68627cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
68637cf37e64SBarry Smith 
68647cf37e64SBarry Smith    Collective on TS
68657cf37e64SBarry Smith 
68667cf37e64SBarry Smith    Input Parameters:
68677cf37e64SBarry Smith +  ts - the TS context
68687cf37e64SBarry Smith .  step - current time-step
68697cf37e64SBarry Smith .  ptime - current time
68707cf37e64SBarry Smith .  u - current solution
68717cf37e64SBarry Smith -  dctx - unused context
68727cf37e64SBarry Smith 
68737cf37e64SBarry Smith    Level: intermediate
68747cf37e64SBarry Smith 
68757cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
68767cf37e64SBarry Smith 
68777cf37e64SBarry Smith    Options Database Keys:
68787cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
68797cf37e64SBarry Smith 
68807cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
68817cf37e64SBarry Smith @*/
6882edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
68837cf37e64SBarry Smith {
68847cf37e64SBarry Smith   PetscErrorCode    ierr;
68857cf37e64SBarry Smith   Vec               y;
68867cf37e64SBarry Smith   PetscReal         nrm;
6887edbaebb3SBarry Smith   PetscBool         flg;
68887cf37e64SBarry Smith 
68897cf37e64SBarry Smith   PetscFunctionBegin;
68907cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
68917cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68927cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6893edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6894edbaebb3SBarry Smith   if (flg) {
68957cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6896edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6897edbaebb3SBarry Smith   }
6898edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6899edbaebb3SBarry Smith   if (flg) {
6900edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6901edbaebb3SBarry Smith   }
6902edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
69037cf37e64SBarry Smith   PetscFunctionReturn(0);
69047cf37e64SBarry Smith }
69057cf37e64SBarry Smith 
6906201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6907201da799SBarry Smith {
6908201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6909201da799SBarry Smith   PetscReal      x   = ptime,y;
6910201da799SBarry Smith   PetscErrorCode ierr;
6911201da799SBarry Smith   PetscInt       its;
6912201da799SBarry Smith 
6913201da799SBarry Smith   PetscFunctionBegin;
691463e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6915201da799SBarry Smith   if (!n) {
6916201da799SBarry Smith     PetscDrawAxis axis;
6917201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6918201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6919201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6920201da799SBarry Smith     ctx->snes_its = 0;
6921201da799SBarry Smith   }
6922201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6923201da799SBarry Smith   y    = its - ctx->snes_its;
6924201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
69253fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6926201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69276934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6928201da799SBarry Smith   }
6929201da799SBarry Smith   ctx->snes_its = its;
6930201da799SBarry Smith   PetscFunctionReturn(0);
6931201da799SBarry Smith }
6932201da799SBarry Smith 
6933201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6934201da799SBarry Smith {
6935201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6936201da799SBarry Smith   PetscReal      x   = ptime,y;
6937201da799SBarry Smith   PetscErrorCode ierr;
6938201da799SBarry Smith   PetscInt       its;
6939201da799SBarry Smith 
6940201da799SBarry Smith   PetscFunctionBegin;
694163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6942201da799SBarry Smith   if (!n) {
6943201da799SBarry Smith     PetscDrawAxis axis;
6944201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6945201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6946201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6947201da799SBarry Smith     ctx->ksp_its = 0;
6948201da799SBarry Smith   }
6949201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6950201da799SBarry Smith   y    = its - ctx->ksp_its;
6951201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
695299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6953201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69546934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6955201da799SBarry Smith   }
6956201da799SBarry Smith   ctx->ksp_its = its;
6957201da799SBarry Smith   PetscFunctionReturn(0);
6958201da799SBarry Smith }
6959f9c1d6abSBarry Smith 
6960f9c1d6abSBarry Smith /*@
6961f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6962f9c1d6abSBarry Smith 
6963d083f849SBarry Smith    Collective on TS
6964f9c1d6abSBarry Smith 
6965f9c1d6abSBarry Smith    Input Parameters:
6966f9c1d6abSBarry Smith +  ts - the TS context
6967f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6968f9c1d6abSBarry Smith 
6969f9c1d6abSBarry Smith    Output Parameters:
6970f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6971f9c1d6abSBarry Smith 
6972f9c1d6abSBarry Smith    Level: developer
6973f9c1d6abSBarry Smith 
6974f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6975f9c1d6abSBarry Smith @*/
6976f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6977f9c1d6abSBarry Smith {
6978f9c1d6abSBarry Smith   PetscErrorCode ierr;
6979f9c1d6abSBarry Smith 
6980f9c1d6abSBarry Smith   PetscFunctionBegin;
6981f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6982ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6983f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6984f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6985f9c1d6abSBarry Smith }
698624655328SShri 
6987b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6988b3d3934dSBarry Smith /*@C
6989b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6990b3d3934dSBarry Smith 
6991b3d3934dSBarry Smith    Collective on TS
6992b3d3934dSBarry Smith 
6993b3d3934dSBarry Smith    Input Parameters:
6994b3d3934dSBarry Smith .  ts  - the ODE solver object
6995b3d3934dSBarry Smith 
6996b3d3934dSBarry Smith    Output Parameter:
6997b3d3934dSBarry Smith .  ctx - the context
6998b3d3934dSBarry Smith 
6999b3d3934dSBarry Smith    Level: intermediate
7000b3d3934dSBarry Smith 
7001b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7002b3d3934dSBarry Smith 
7003b3d3934dSBarry Smith @*/
7004b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7005b3d3934dSBarry Smith {
7006b3d3934dSBarry Smith   PetscErrorCode ierr;
7007b3d3934dSBarry Smith 
7008b3d3934dSBarry Smith   PetscFunctionBegin;
7009a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7010b3d3934dSBarry Smith   PetscFunctionReturn(0);
7011b3d3934dSBarry Smith }
7012b3d3934dSBarry Smith 
7013b3d3934dSBarry Smith /*@C
7014b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7015b3d3934dSBarry Smith 
7016b3d3934dSBarry Smith    Collective on TS
7017b3d3934dSBarry Smith 
7018b3d3934dSBarry Smith    Input Parameters:
7019b3d3934dSBarry Smith +  ts - the TS context
7020b3d3934dSBarry Smith .  step - current time-step
7021b3d3934dSBarry Smith .  ptime - current time
70227db568b7SBarry Smith .  u  - current solution
70237db568b7SBarry Smith -  dctx - the envelope context
7024b3d3934dSBarry Smith 
7025b3d3934dSBarry Smith    Options Database:
7026b3d3934dSBarry Smith .  -ts_monitor_envelope
7027b3d3934dSBarry Smith 
7028b3d3934dSBarry Smith    Level: intermediate
7029b3d3934dSBarry Smith 
703095452b02SPatrick Sanan    Notes:
703195452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7032b3d3934dSBarry Smith 
70337db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7034b3d3934dSBarry Smith @*/
70357db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7036b3d3934dSBarry Smith {
7037b3d3934dSBarry Smith   PetscErrorCode       ierr;
70387db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7039b3d3934dSBarry Smith 
7040b3d3934dSBarry Smith   PetscFunctionBegin;
7041b3d3934dSBarry Smith   if (!ctx->max) {
7042b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7043b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7044b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7045b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7046b3d3934dSBarry Smith   } else {
7047b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7048b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7049b3d3934dSBarry Smith   }
7050b3d3934dSBarry Smith   PetscFunctionReturn(0);
7051b3d3934dSBarry Smith }
7052b3d3934dSBarry Smith 
7053b3d3934dSBarry Smith /*@C
7054b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7055b3d3934dSBarry Smith 
7056b3d3934dSBarry Smith    Collective on TS
7057b3d3934dSBarry Smith 
7058b3d3934dSBarry Smith    Input Parameter:
7059b3d3934dSBarry Smith .  ts - the TS context
7060b3d3934dSBarry Smith 
7061b3d3934dSBarry Smith    Output Parameter:
7062b3d3934dSBarry Smith +  max - the maximum values
7063b3d3934dSBarry Smith -  min - the minimum values
7064b3d3934dSBarry Smith 
706595452b02SPatrick Sanan    Notes:
706695452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
70677db568b7SBarry Smith 
7068b3d3934dSBarry Smith    Level: intermediate
7069b3d3934dSBarry Smith 
7070b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7071b3d3934dSBarry Smith @*/
7072b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7073b3d3934dSBarry Smith {
7074b3d3934dSBarry Smith   PetscInt i;
7075b3d3934dSBarry Smith 
7076b3d3934dSBarry Smith   PetscFunctionBegin;
7077b3d3934dSBarry Smith   if (max) *max = NULL;
7078b3d3934dSBarry Smith   if (min) *min = NULL;
7079b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7080b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
70815537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7082b3d3934dSBarry Smith       if (max) *max = ctx->max;
7083b3d3934dSBarry Smith       if (min) *min = ctx->min;
7084b3d3934dSBarry Smith       break;
7085b3d3934dSBarry Smith     }
7086b3d3934dSBarry Smith   }
7087b3d3934dSBarry Smith   PetscFunctionReturn(0);
7088b3d3934dSBarry Smith }
7089b3d3934dSBarry Smith 
7090b3d3934dSBarry Smith /*@C
7091b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7092b3d3934dSBarry Smith 
7093b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7094b3d3934dSBarry Smith 
7095b3d3934dSBarry Smith    Input Parameter:
7096b3d3934dSBarry Smith .  ctx - the monitor context
7097b3d3934dSBarry Smith 
7098b3d3934dSBarry Smith    Level: intermediate
7099b3d3934dSBarry Smith 
71007db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7101b3d3934dSBarry Smith @*/
7102b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7103b3d3934dSBarry Smith {
7104b3d3934dSBarry Smith   PetscErrorCode ierr;
7105b3d3934dSBarry Smith 
7106b3d3934dSBarry Smith   PetscFunctionBegin;
7107b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7108b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7109b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7110b3d3934dSBarry Smith   PetscFunctionReturn(0);
7111b3d3934dSBarry Smith }
7112f2dee214SBarry Smith 
711324655328SShri /*@
7114dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7115dcb233daSLisandro Dalcin 
7116dcb233daSLisandro Dalcin    Collective on TS
7117dcb233daSLisandro Dalcin 
7118dcb233daSLisandro Dalcin    Input Parameter:
7119dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7120dcb233daSLisandro Dalcin 
7121dcb233daSLisandro Dalcin    Level: advanced
7122dcb233daSLisandro Dalcin 
7123dcb233daSLisandro Dalcin    Notes:
7124dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7125dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7126dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7127dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7128dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7129dcb233daSLisandro Dalcin    discontinuous source terms).
7130dcb233daSLisandro Dalcin 
7131dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7132dcb233daSLisandro Dalcin @*/
7133dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7134dcb233daSLisandro Dalcin {
7135dcb233daSLisandro Dalcin   PetscFunctionBegin;
7136dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7137dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7138dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7139dcb233daSLisandro Dalcin }
7140dcb233daSLisandro Dalcin 
7141dcb233daSLisandro Dalcin /*@
714224655328SShri    TSRollBack - Rolls back one time step
714324655328SShri 
714424655328SShri    Collective on TS
714524655328SShri 
714624655328SShri    Input Parameter:
714724655328SShri .  ts - the TS context obtained from TSCreate()
714824655328SShri 
714924655328SShri    Level: advanced
715024655328SShri 
715124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
715224655328SShri @*/
715324655328SShri PetscErrorCode  TSRollBack(TS ts)
715424655328SShri {
715524655328SShri   PetscErrorCode ierr;
715624655328SShri 
715724655328SShri   PetscFunctionBegin;
715824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7159b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
716024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
716124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
716224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
716324655328SShri   ts->ptime = ts->ptime_prev;
7164be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
71652808aa04SLisandro Dalcin   ts->steps--;
7166b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
716724655328SShri   PetscFunctionReturn(0);
716824655328SShri }
7169aeb4809dSShri Abhyankar 
7170ff22ae23SHong Zhang /*@
7171ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7172ff22ae23SHong Zhang 
7173ff22ae23SHong Zhang    Input Parameter:
7174ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7175ff22ae23SHong Zhang 
71760429704eSStefano Zampini    Output Parameters:
71770429704eSStefano Zampini +  ns - the number of stages
71780429704eSStefano Zampini -  Y - the current stage vectors
71790429704eSStefano Zampini 
7180ff22ae23SHong Zhang    Level: advanced
7181ff22ae23SHong Zhang 
71820429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
71830429704eSStefano Zampini 
7184ff22ae23SHong Zhang .seealso: TSCreate()
7185ff22ae23SHong Zhang @*/
7186ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7187ff22ae23SHong Zhang {
7188ff22ae23SHong Zhang   PetscErrorCode ierr;
7189ff22ae23SHong Zhang 
7190ff22ae23SHong Zhang   PetscFunctionBegin;
7191ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
71920429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
71930429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
71940429704eSStefano Zampini   if (!ts->ops->getstages) {
71950429704eSStefano Zampini     if (ns) *ns = 0;
71960429704eSStefano Zampini     if (Y) *Y = NULL;
71970429704eSStefano Zampini   } else {
7198ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7199ff22ae23SHong Zhang   }
7200ff22ae23SHong Zhang   PetscFunctionReturn(0);
7201ff22ae23SHong Zhang }
7202ff22ae23SHong Zhang 
7203847ff0e1SMatthew G. Knepley /*@C
7204847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7205847ff0e1SMatthew G. Knepley 
7206847ff0e1SMatthew G. Knepley   Collective on SNES
7207847ff0e1SMatthew G. Knepley 
7208847ff0e1SMatthew G. Knepley   Input Parameters:
7209847ff0e1SMatthew G. Knepley + ts - the TS context
7210847ff0e1SMatthew G. Knepley . t - current timestep
7211847ff0e1SMatthew G. Knepley . U - state vector
7212847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7213847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7214847ff0e1SMatthew G. Knepley - ctx - an optional user context
7215847ff0e1SMatthew G. Knepley 
7216847ff0e1SMatthew G. Knepley   Output Parameters:
7217847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7218847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7219847ff0e1SMatthew G. Knepley 
7220847ff0e1SMatthew G. Knepley   Level: intermediate
7221847ff0e1SMatthew G. Knepley 
7222847ff0e1SMatthew G. Knepley   Notes:
7223847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7224847ff0e1SMatthew G. Knepley 
7225847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7226847ff0e1SMatthew G. Knepley 
7227847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7228847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7229847ff0e1SMatthew G. Knepley 
7230847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7231847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7232847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7233847ff0e1SMatthew G. Knepley 
7234847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7235847ff0e1SMatthew G. Knepley @*/
7236847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7237847ff0e1SMatthew G. Knepley {
7238847ff0e1SMatthew G. Knepley   SNES           snes;
7239847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7240847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7241847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7242847ff0e1SMatthew G. Knepley 
7243847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7244c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7245847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7246847ff0e1SMatthew G. Knepley   if (!color) {
7247847ff0e1SMatthew G. Knepley     DM         dm;
7248847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7249847ff0e1SMatthew G. Knepley 
7250847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7251847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7252847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7253847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7254847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7255847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7256847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7257847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7258847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7259847ff0e1SMatthew G. Knepley     } else {
7260847ff0e1SMatthew G. Knepley       MatColoring mc;
7261847ff0e1SMatthew G. Knepley 
7262847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7263847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7264847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7265847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7266847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7267847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7268847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7269847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7270847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7271847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7272847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7273847ff0e1SMatthew G. Knepley     }
7274847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7275847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7276847ff0e1SMatthew G. Knepley   }
7277847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7278847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7279847ff0e1SMatthew G. Knepley   if (J != B) {
7280847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7281847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7282847ff0e1SMatthew G. Knepley   }
7283847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7284847ff0e1SMatthew G. Knepley }
728593b34091SDebojyoti Ghosh 
7286cb9d8021SPierre Barbier de Reuille /*@
72876bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7288cb9d8021SPierre Barbier de Reuille 
7289cb9d8021SPierre Barbier de Reuille     Input Parameters:
72906bc98fa9SBarry Smith +    ts - the TS context
72916bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
72926bc98fa9SBarry Smith 
72936bc98fa9SBarry Smith     Calling sequence of func:
72946bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
72956bc98fa9SBarry Smith 
72966bc98fa9SBarry Smith +   ts - the TS context
72976bc98fa9SBarry Smith .   time - the current time (of the stage)
72986bc98fa9SBarry Smith .   state - the state to check if it is valid
72996bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7300cb9d8021SPierre Barbier de Reuille 
7301cb9d8021SPierre Barbier de Reuille     Level: intermediate
7302cb9d8021SPierre Barbier de Reuille 
73036bc98fa9SBarry Smith     Notes:
73046bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
73056bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
73066bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
73076bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
73086bc98fa9SBarry Smith 
73096bc98fa9SBarry Smith     Developer Notes:
73106bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
73116bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
73126bc98fa9SBarry Smith 
73136bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7314cb9d8021SPierre Barbier de Reuille @*/
7315cb9d8021SPierre Barbier de Reuille 
7316d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7317cb9d8021SPierre Barbier de Reuille {
7318cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7319cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7320cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7321cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7322cb9d8021SPierre Barbier de Reuille }
7323cb9d8021SPierre Barbier de Reuille 
7324cb9d8021SPierre Barbier de Reuille /*@
73256bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7326cb9d8021SPierre Barbier de Reuille 
7327cb9d8021SPierre Barbier de Reuille     Input Parameters:
73286bc98fa9SBarry Smith +    ts - the TS context
73296bc98fa9SBarry Smith .    stagetime - time of the simulation
73306bc98fa9SBarry Smith -    Y - state vector to check.
7331cb9d8021SPierre Barbier de Reuille 
7332cb9d8021SPierre Barbier de Reuille     Output Parameter:
73336bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7334cb9d8021SPierre Barbier de Reuille 
7335cb9d8021SPierre Barbier de Reuille     Note:
73366bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
73376bc98fa9SBarry Smith     to check if the current state is valid.
733896a0c994SBarry Smith 
73396bc98fa9SBarry Smith     Level: developer
73406bc98fa9SBarry Smith 
73416bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7342cb9d8021SPierre Barbier de Reuille @*/
7343d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7344cb9d8021SPierre Barbier de Reuille {
7345cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7346cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7347cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7348cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7349d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7350cb9d8021SPierre Barbier de Reuille   }
7351cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7352cb9d8021SPierre Barbier de Reuille }
73531ceb14c0SBarry Smith 
735493b34091SDebojyoti Ghosh /*@C
7355e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
735693b34091SDebojyoti Ghosh 
7357d083f849SBarry Smith   Collective
735893b34091SDebojyoti Ghosh 
735993b34091SDebojyoti Ghosh   Input Parameter:
736093b34091SDebojyoti Ghosh . tsin    - The input TS
736193b34091SDebojyoti Ghosh 
736293b34091SDebojyoti Ghosh   Output Parameter:
7363e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
736493b34091SDebojyoti Ghosh 
73655eca1a21SEmil Constantinescu   Notes:
73665eca1a21SEmil 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.
73675eca1a21SEmil Constantinescu 
7368928bb9adSStefano 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);
73695eca1a21SEmil Constantinescu 
73705eca1a21SEmil Constantinescu   Level: developer
737193b34091SDebojyoti Ghosh 
7372e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
737393b34091SDebojyoti Ghosh @*/
7374baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
737593b34091SDebojyoti Ghosh {
737693b34091SDebojyoti Ghosh   TS             t;
737793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7378dc846ba4SSatish Balay   SNES           snes_start;
7379dc846ba4SSatish Balay   DM             dm;
7380dc846ba4SSatish Balay   TSType         type;
738193b34091SDebojyoti Ghosh 
738293b34091SDebojyoti Ghosh   PetscFunctionBegin;
738393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
738493b34091SDebojyoti Ghosh   *tsout = NULL;
738593b34091SDebojyoti Ghosh 
73867a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
738793b34091SDebojyoti Ghosh 
738893b34091SDebojyoti Ghosh   /* General TS description */
738993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
739093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
739193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
739293b34091SDebojyoti Ghosh   t->snes_its          = 0;
739393b34091SDebojyoti Ghosh   t->nwork             = 0;
73947d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
739593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
739693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
739793b34091SDebojyoti Ghosh 
739834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
739934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7400d15a3a53SEmil Constantinescu 
740193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
740293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
740393b34091SDebojyoti Ghosh 
740493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
740551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
740693b34091SDebojyoti Ghosh 
7407e7069c78SShri   t->trajectory = tsin->trajectory;
7408e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7409e7069c78SShri 
7410e7069c78SShri   t->event = tsin->event;
74116b10a48eSSatish Balay   if (t->event) t->event->refct++;
7412e7069c78SShri 
741393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
741493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7415e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
741693b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
741793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
741893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
741993b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
742093b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
742193b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
742293b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
742393b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
742493b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
742593b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
742693b34091SDebojyoti Ghosh 
742793b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
742893b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
742993b34091SDebojyoti Ghosh 
743093b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
743193b34091SDebojyoti Ghosh 
743293b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
743393b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
743493b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
743593b34091SDebojyoti Ghosh 
743693b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
743793b34091SDebojyoti Ghosh 
74380d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
74390d4fed19SBarry Smith     PetscInt i;
74400d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
74410d4fed19SBarry Smith     for (i=0; i<10; i++) {
74420d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
74430d4fed19SBarry Smith     }
74440d4fed19SBarry Smith   }
744593b34091SDebojyoti Ghosh   *tsout = t;
744693b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
744793b34091SDebojyoti Ghosh }
7448f3b1f45cSBarry Smith 
7449f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7450f3b1f45cSBarry Smith {
7451f3b1f45cSBarry Smith   PetscErrorCode ierr;
7452f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7453f3b1f45cSBarry Smith 
7454f3b1f45cSBarry Smith   PetscFunctionBegin;
7455f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7456f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7457f3b1f45cSBarry Smith }
7458f3b1f45cSBarry Smith 
7459f3b1f45cSBarry Smith /*@
7460f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7461f3b1f45cSBarry Smith 
7462d083f849SBarry Smith    Logically Collective on TS
7463f3b1f45cSBarry Smith 
7464f3b1f45cSBarry Smith     Input Parameters:
7465f3b1f45cSBarry Smith     TS - the time stepping routine
7466f3b1f45cSBarry Smith 
7467f3b1f45cSBarry Smith    Output Parameter:
7468f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7469f3b1f45cSBarry Smith 
7470f3b1f45cSBarry Smith    Options Database:
7471f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7472f3b1f45cSBarry Smith 
7473f3b1f45cSBarry Smith    Level: advanced
7474f3b1f45cSBarry Smith 
747595452b02SPatrick Sanan    Notes:
747695452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7477f3b1f45cSBarry Smith 
7478f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7479f3b1f45cSBarry Smith @*/
7480f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7481f3b1f45cSBarry Smith {
7482f3b1f45cSBarry Smith   Mat            J,B;
7483f3b1f45cSBarry Smith   PetscErrorCode ierr;
7484f3b1f45cSBarry Smith   TSRHSJacobian  func;
7485f3b1f45cSBarry Smith   void*          ctx;
7486f3b1f45cSBarry Smith 
7487f3b1f45cSBarry Smith   PetscFunctionBegin;
7488f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7489f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7490f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7491f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7492f3b1f45cSBarry Smith }
7493f3b1f45cSBarry Smith 
7494f3b1f45cSBarry Smith /*@C
7495f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7496f3b1f45cSBarry Smith 
7497d083f849SBarry Smith    Logically Collective on TS
7498f3b1f45cSBarry Smith 
7499f3b1f45cSBarry Smith     Input Parameters:
7500f3b1f45cSBarry Smith     TS - the time stepping routine
7501f3b1f45cSBarry Smith 
7502f3b1f45cSBarry Smith    Output Parameter:
7503f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7504f3b1f45cSBarry Smith 
7505f3b1f45cSBarry Smith    Options Database:
7506f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7507f3b1f45cSBarry Smith 
750895452b02SPatrick Sanan    Notes:
750995452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7510f3b1f45cSBarry Smith 
7511f3b1f45cSBarry Smith    Level: advanced
7512f3b1f45cSBarry Smith 
7513f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7514f3b1f45cSBarry Smith @*/
7515f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7516f3b1f45cSBarry Smith {
7517f3b1f45cSBarry Smith   Mat            J,B;
7518f3b1f45cSBarry Smith   PetscErrorCode ierr;
7519f3b1f45cSBarry Smith   void           *ctx;
7520f3b1f45cSBarry Smith   TSRHSJacobian  func;
7521f3b1f45cSBarry Smith 
7522f3b1f45cSBarry Smith   PetscFunctionBegin;
7523f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7524f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7525f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7526f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7527f3b1f45cSBarry Smith }
75280fe4d17eSHong Zhang 
75290fe4d17eSHong Zhang /*@
75300fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
75310fe4d17eSHong Zhang 
75320fe4d17eSHong Zhang   Logically collective
75330fe4d17eSHong Zhang 
75340fe4d17eSHong Zhang   Input Parameter:
75350fe4d17eSHong Zhang +  ts - timestepping context
75360fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
75370fe4d17eSHong Zhang 
75380fe4d17eSHong Zhang   Options Database:
75390fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
75400fe4d17eSHong Zhang 
75410fe4d17eSHong Zhang   Notes:
75420fe4d17eSHong Zhang     This is only useful for multirate methods
75430fe4d17eSHong Zhang 
75440fe4d17eSHong Zhang   Level: intermediate
75450fe4d17eSHong Zhang 
75460fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
75470fe4d17eSHong Zhang @*/
75480fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
75490fe4d17eSHong Zhang {
75500fe4d17eSHong Zhang   PetscFunctionBegin;
75510fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
75520fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
75530fe4d17eSHong Zhang   PetscFunctionReturn(0);
75540fe4d17eSHong Zhang }
75550fe4d17eSHong Zhang 
75560fe4d17eSHong Zhang /*@
75570fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
75580fe4d17eSHong Zhang 
75590fe4d17eSHong Zhang   Not collective
75600fe4d17eSHong Zhang 
75610fe4d17eSHong Zhang   Input Parameter:
75620fe4d17eSHong Zhang .  ts - timestepping context
75630fe4d17eSHong Zhang 
75640fe4d17eSHong Zhang   Output Parameter:
75650fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
75660fe4d17eSHong Zhang 
75670fe4d17eSHong Zhang   Level: intermediate
75680fe4d17eSHong Zhang 
75690fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
75700fe4d17eSHong Zhang @*/
75710fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
75720fe4d17eSHong Zhang {
75730fe4d17eSHong Zhang   PetscFunctionBegin;
75740fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
75750fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
75760fe4d17eSHong Zhang   PetscFunctionReturn(0);
75770fe4d17eSHong Zhang }
7578