xref: /petsc/src/ts/interface/ts.c (revision e3c11fc18b1c7fa42b319a982ded77e55015b34a)
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 /*@
1769*e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1770*e3c11fc1SJed Brown 
1771*e3c11fc1SJed Brown    Logically Collective
1772*e3c11fc1SJed Brown 
1773*e3c11fc1SJed Brown    Input Parameters:
1774*e3c11fc1SJed Brown +  ts - TS on which to compute
1775*e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1776*e3c11fc1SJed Brown 
1777*e3c11fc1SJed Brown    Output Parameters:
1778*e3c11fc1SJed Brown .  C - transient (conservative) variable
1779*e3c11fc1SJed Brown 
1780*e3c11fc1SJed Brown    Developer Notes:
1781*e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1782*e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1783*e3c11fc1SJed Brown    being used.
1784*e3c11fc1SJed Brown 
1785*e3c11fc1SJed Brown    Level: developer
1786*e3c11fc1SJed Brown 
1787*e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1788*e3c11fc1SJed Brown @*/
1789*e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1790*e3c11fc1SJed Brown {
1791*e3c11fc1SJed Brown   PetscErrorCode ierr;
1792*e3c11fc1SJed Brown   DM             dm;
1793*e3c11fc1SJed Brown   DMTS           dmts;
1794*e3c11fc1SJed Brown 
1795*e3c11fc1SJed Brown   PetscFunctionBegin;
1796*e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1797*e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1798*e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1799*e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1800*e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1801*e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1802*e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1803*e3c11fc1SJed Brown   }
1804*e3c11fc1SJed Brown   PetscFunctionReturn(0);
1805*e3c11fc1SJed Brown }
1806*e3c11fc1SJed Brown 
1807*e3c11fc1SJed Brown /*@
1808*e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1809*e3c11fc1SJed Brown 
1810*e3c11fc1SJed Brown    Logically Collective
1811*e3c11fc1SJed Brown 
1812*e3c11fc1SJed Brown    Input Parameters:
1813*e3c11fc1SJed Brown .  ts - TS on which to compute
1814*e3c11fc1SJed Brown 
1815*e3c11fc1SJed Brown    Output Parameters:
1816*e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1817*e3c11fc1SJed Brown 
1818*e3c11fc1SJed Brown    Level: developer
1819*e3c11fc1SJed Brown 
1820*e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1821*e3c11fc1SJed Brown @*/
1822*e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1823*e3c11fc1SJed Brown {
1824*e3c11fc1SJed Brown   PetscErrorCode ierr;
1825*e3c11fc1SJed Brown   DM             dm;
1826*e3c11fc1SJed Brown   DMTS           dmts;
1827*e3c11fc1SJed Brown 
1828*e3c11fc1SJed Brown   PetscFunctionBegin;
1829*e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1830*e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1831*e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1832*e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1833*e3c11fc1SJed Brown   PetscFunctionReturn(0);
1834*e3c11fc1SJed Brown }
1835*e3c11fc1SJed Brown 
1836*e3c11fc1SJed Brown /*@
1837efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1838efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1839efe9872eSLisandro Dalcin 
1840d083f849SBarry Smith    Logically Collective on TS
1841efe9872eSLisandro Dalcin 
1842efe9872eSLisandro Dalcin    Input Parameters:
1843efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1844efe9872eSLisandro Dalcin .  u - the solution vector
1845efe9872eSLisandro Dalcin -  v - the time derivative vector
1846efe9872eSLisandro Dalcin 
1847efe9872eSLisandro Dalcin    Level: beginner
1848efe9872eSLisandro Dalcin 
1849efe9872eSLisandro Dalcin @*/
1850efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1851efe9872eSLisandro Dalcin {
1852efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1853efe9872eSLisandro Dalcin 
1854efe9872eSLisandro Dalcin   PetscFunctionBegin;
1855efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1856efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1857efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1858efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1859efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1860efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1861efe9872eSLisandro Dalcin   ts->vec_dot = v;
1862efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1863efe9872eSLisandro Dalcin }
1864efe9872eSLisandro Dalcin 
1865efe9872eSLisandro Dalcin /*@
1866efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1867efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1868efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1869efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1870efe9872eSLisandro Dalcin 
1871efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1872efe9872eSLisandro Dalcin 
1873efe9872eSLisandro Dalcin    Input Parameter:
1874efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1875efe9872eSLisandro Dalcin 
1876efe9872eSLisandro Dalcin    Output Parameter:
1877efe9872eSLisandro Dalcin +  u - the vector containing the solution
1878efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1879efe9872eSLisandro Dalcin 
1880efe9872eSLisandro Dalcin    Level: intermediate
1881efe9872eSLisandro Dalcin 
1882efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1883efe9872eSLisandro Dalcin 
1884efe9872eSLisandro Dalcin @*/
1885efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1886efe9872eSLisandro Dalcin {
1887efe9872eSLisandro Dalcin   PetscFunctionBegin;
1888efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1889efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1890efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1891efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1892efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1893efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1894efe9872eSLisandro Dalcin }
1895efe9872eSLisandro Dalcin 
189655849f57SBarry Smith /*@C
189755849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
189855849f57SBarry Smith 
189955849f57SBarry Smith   Collective on PetscViewer
190055849f57SBarry Smith 
190155849f57SBarry Smith   Input Parameters:
190255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
190355849f57SBarry Smith            some related function before a call to TSLoad().
190455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
190555849f57SBarry Smith 
190655849f57SBarry Smith    Level: intermediate
190755849f57SBarry Smith 
190855849f57SBarry Smith   Notes:
190955849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
191055849f57SBarry Smith 
191155849f57SBarry Smith   Notes for advanced users:
191255849f57SBarry Smith   Most users should not need to know the details of the binary storage
191355849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
191455849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
191555849f57SBarry Smith   format is
191655849f57SBarry Smith .vb
191755849f57SBarry Smith      has not yet been determined
191855849f57SBarry Smith .ve
191955849f57SBarry Smith 
192055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
192155849f57SBarry Smith @*/
1922f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
192355849f57SBarry Smith {
192455849f57SBarry Smith   PetscErrorCode ierr;
192555849f57SBarry Smith   PetscBool      isbinary;
192655849f57SBarry Smith   PetscInt       classid;
192755849f57SBarry Smith   char           type[256];
19282d53ad75SBarry Smith   DMTS           sdm;
1929ad6bc421SBarry Smith   DM             dm;
193055849f57SBarry Smith 
193155849f57SBarry Smith   PetscFunctionBegin;
1932f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
193355849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
193455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
193555849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
193655849f57SBarry Smith 
1937060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1938ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1939060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1940f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1941f2c2a1b9SBarry Smith   if (ts->ops->load) {
1942f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1943f2c2a1b9SBarry Smith   }
1944ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1945ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1946ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1947f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1948f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19492d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19502d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
195155849f57SBarry Smith   PetscFunctionReturn(0);
195255849f57SBarry Smith }
195355849f57SBarry Smith 
19549804daf3SBarry Smith #include <petscdraw.h>
1955e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1956e04113cfSBarry Smith #include <petscviewersaws.h>
1957f05ece33SBarry Smith #endif
1958fe2efc57SMark 
1959fe2efc57SMark /*@C
1960fe2efc57SMark    TSViewFromOptions - View from Options
1961fe2efc57SMark 
1962fe2efc57SMark    Collective on TS
1963fe2efc57SMark 
1964fe2efc57SMark    Input Parameters:
1965fe2efc57SMark +  A - the application ordering context
1966736c3998SJose E. Roman .  obj - Optional object
1967736c3998SJose E. Roman -  name - command line option
1968fe2efc57SMark 
1969fe2efc57SMark    Level: intermediate
1970fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
1971fe2efc57SMark @*/
1972fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
1973fe2efc57SMark {
1974fe2efc57SMark   PetscErrorCode ierr;
1975fe2efc57SMark 
1976fe2efc57SMark   PetscFunctionBegin;
1977fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
1978fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
1979fe2efc57SMark   PetscFunctionReturn(0);
1980fe2efc57SMark }
1981fe2efc57SMark 
19827e2c5f70SBarry Smith /*@C
1983d763cef2SBarry Smith     TSView - Prints the TS data structure.
1984d763cef2SBarry Smith 
19854c49b128SBarry Smith     Collective on TS
1986d763cef2SBarry Smith 
1987d763cef2SBarry Smith     Input Parameters:
1988d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1989d763cef2SBarry Smith -   viewer - visualization context
1990d763cef2SBarry Smith 
1991d763cef2SBarry Smith     Options Database Key:
1992d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith     Notes:
1995d763cef2SBarry Smith     The available visualization contexts include
1996b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1997b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1998d763cef2SBarry Smith          output where only the first processor opens
1999d763cef2SBarry Smith          the file.  All other processors send their
2000d763cef2SBarry Smith          data to the first processor to print.
2001d763cef2SBarry Smith 
2002d763cef2SBarry Smith     The user can open an alternative visualization context with
2003b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith     Level: beginner
2006d763cef2SBarry Smith 
2007b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2008d763cef2SBarry Smith @*/
20097087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2010d763cef2SBarry Smith {
2011dfbe8321SBarry Smith   PetscErrorCode ierr;
201219fd82e9SBarry Smith   TSType         type;
20132b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20142d53ad75SBarry Smith   DMTS           sdm;
2015e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2016536b137fSBarry Smith   PetscBool      issaws;
2017f05ece33SBarry Smith #endif
2018d763cef2SBarry Smith 
2019d763cef2SBarry Smith   PetscFunctionBegin;
20200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20213050cee2SBarry Smith   if (!viewer) {
2022ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20233050cee2SBarry Smith   }
20240700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2025c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2026fd16b177SBarry Smith 
2027251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2028251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
202955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20302b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2031e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2032536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2033f05ece33SBarry Smith #endif
203432077d6dSBarry Smith   if (iascii) {
2035dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2036efd4aadfSBarry Smith     if (ts->ops->view) {
2037efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2038efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2039efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2040efd4aadfSBarry Smith     }
2041ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
204277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2043ef85077eSLisandro Dalcin     }
2044ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20457c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2046ef85077eSLisandro Dalcin     }
2047efd4aadfSBarry Smith     if (ts->usessnes) {
2048efd4aadfSBarry Smith       PetscBool lin;
2049d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20505ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2051d763cef2SBarry Smith       }
20525ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
20531ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2054efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2055efd4aadfSBarry Smith     }
2056193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2057a0af407cSBarry Smith     if (ts->vrtol) {
2058a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2059a0af407cSBarry Smith     } else {
2060a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2061a0af407cSBarry Smith     }
2062a0af407cSBarry Smith     if (ts->vatol) {
2063a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2064a0af407cSBarry Smith     } else {
2065a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2066a0af407cSBarry Smith     }
2067825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2068efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2069825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
20700f5bd95cSBarry Smith   } else if (isstring) {
2071a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
207236a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
207336a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
207455849f57SBarry Smith   } else if (isbinary) {
207555849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
207655849f57SBarry Smith     MPI_Comm    comm;
207755849f57SBarry Smith     PetscMPIInt rank;
207855849f57SBarry Smith     char        type[256];
207955849f57SBarry Smith 
208055849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
208155849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
208255849f57SBarry Smith     if (!rank) {
208355849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
208455849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
208555849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
208655849f57SBarry Smith     }
208755849f57SBarry Smith     if (ts->ops->view) {
208855849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
208955849f57SBarry Smith     }
2090efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2091f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2092f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
20932d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20942d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20952b0a91c0SBarry Smith   } else if (isdraw) {
20962b0a91c0SBarry Smith     PetscDraw draw;
20972b0a91c0SBarry Smith     char      str[36];
209889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20992b0a91c0SBarry Smith 
21002b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21012b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21022b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21032b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
210451fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
210589fd9fafSBarry Smith     bottom = y - h;
21062b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21072b0a91c0SBarry Smith     if (ts->ops->view) {
21082b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21092b0a91c0SBarry Smith     }
2110efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2111efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21122b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2113e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2114536b137fSBarry Smith   } else if (issaws) {
2115d45a07a7SBarry Smith     PetscMPIInt rank;
21162657e9d9SBarry Smith     const char  *name;
21172657e9d9SBarry Smith 
21182657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2119d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2120d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2121d45a07a7SBarry Smith       char       dir[1024];
2122d45a07a7SBarry Smith 
2123e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2124a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21252657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21262657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21272657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2128d763cef2SBarry Smith     }
21290acecf5bSBarry Smith     if (ts->ops->view) {
21300acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21310acecf5bSBarry Smith     }
2132f05ece33SBarry Smith #endif
2133f05ece33SBarry Smith   }
213436a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
213536a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
213636a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
213736a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
213836a9e3b9SBarry Smith   }
213936a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
214036a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2141f05ece33SBarry Smith 
2142b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2143251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2144b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2145d763cef2SBarry Smith   PetscFunctionReturn(0);
2146d763cef2SBarry Smith }
2147d763cef2SBarry Smith 
2148b07ff414SBarry Smith /*@
2149d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2150d763cef2SBarry Smith    the timesteppers.
2151d763cef2SBarry Smith 
21523f9fe445SBarry Smith    Logically Collective on TS
2153d763cef2SBarry Smith 
2154d763cef2SBarry Smith    Input Parameters:
2155d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2156d763cef2SBarry Smith -  usrP - optional user context
2157d763cef2SBarry Smith 
215895452b02SPatrick Sanan    Fortran Notes:
215995452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2160daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2161daf670e6SBarry Smith 
2162d763cef2SBarry Smith    Level: intermediate
2163d763cef2SBarry Smith 
2164d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2165d763cef2SBarry Smith @*/
21667087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2167d763cef2SBarry Smith {
2168d763cef2SBarry Smith   PetscFunctionBegin;
21690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2170d763cef2SBarry Smith   ts->user = usrP;
2171d763cef2SBarry Smith   PetscFunctionReturn(0);
2172d763cef2SBarry Smith }
2173d763cef2SBarry Smith 
2174b07ff414SBarry Smith /*@
2175d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2176d763cef2SBarry Smith     timestepper.
2177d763cef2SBarry Smith 
2178d763cef2SBarry Smith     Not Collective
2179d763cef2SBarry Smith 
2180d763cef2SBarry Smith     Input Parameter:
2181d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2182d763cef2SBarry Smith 
2183d763cef2SBarry Smith     Output Parameter:
2184d763cef2SBarry Smith .   usrP - user context
2185d763cef2SBarry Smith 
218695452b02SPatrick Sanan    Fortran Notes:
218795452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2188daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2189daf670e6SBarry Smith 
2190d763cef2SBarry Smith     Level: intermediate
2191d763cef2SBarry Smith 
2192d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2193d763cef2SBarry Smith @*/
2194e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2195d763cef2SBarry Smith {
2196d763cef2SBarry Smith   PetscFunctionBegin;
21970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2198e71120c6SJed Brown   *(void**)usrP = ts->user;
2199d763cef2SBarry Smith   PetscFunctionReturn(0);
2200d763cef2SBarry Smith }
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith /*@
220380275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2204d763cef2SBarry Smith 
2205d763cef2SBarry Smith    Not Collective
2206d763cef2SBarry Smith 
2207d763cef2SBarry Smith    Input Parameter:
2208d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2209d763cef2SBarry Smith 
2210d763cef2SBarry Smith    Output Parameter:
221180275a0aSLisandro Dalcin .  steps - number of steps completed so far
2212d763cef2SBarry Smith 
2213d763cef2SBarry Smith    Level: intermediate
2214d763cef2SBarry Smith 
22159be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2216d763cef2SBarry Smith @*/
221780275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2218d763cef2SBarry Smith {
2219d763cef2SBarry Smith   PetscFunctionBegin;
22200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
222180275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
222280275a0aSLisandro Dalcin   *steps = ts->steps;
222380275a0aSLisandro Dalcin   PetscFunctionReturn(0);
222480275a0aSLisandro Dalcin }
222580275a0aSLisandro Dalcin 
222680275a0aSLisandro Dalcin /*@
222780275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
222880275a0aSLisandro Dalcin 
222980275a0aSLisandro Dalcin    Logically Collective on TS
223080275a0aSLisandro Dalcin 
223180275a0aSLisandro Dalcin    Input Parameters:
223280275a0aSLisandro Dalcin +  ts - the TS context
223380275a0aSLisandro Dalcin -  steps - number of steps completed so far
223480275a0aSLisandro Dalcin 
223580275a0aSLisandro Dalcin    Notes:
223680275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
223780275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
223880275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
223980275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
224080275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
224180275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
224280275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
224380275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
224480275a0aSLisandro Dalcin 
224580275a0aSLisandro Dalcin    Level: advanced
224680275a0aSLisandro Dalcin 
224780275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
224880275a0aSLisandro Dalcin @*/
224980275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
225080275a0aSLisandro Dalcin {
225180275a0aSLisandro Dalcin   PetscFunctionBegin;
225280275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
225380275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
225480275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
225580275a0aSLisandro Dalcin   ts->steps = steps;
2256d763cef2SBarry Smith   PetscFunctionReturn(0);
2257d763cef2SBarry Smith }
2258d763cef2SBarry Smith 
2259d763cef2SBarry Smith /*@
2260d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2261d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2262d763cef2SBarry Smith 
22633f9fe445SBarry Smith    Logically Collective on TS
2264d763cef2SBarry Smith 
2265d763cef2SBarry Smith    Input Parameters:
2266d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2267d763cef2SBarry Smith -  time_step - the size of the timestep
2268d763cef2SBarry Smith 
2269d763cef2SBarry Smith    Level: intermediate
2270d763cef2SBarry Smith 
2271aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2272d763cef2SBarry Smith 
2273d763cef2SBarry Smith @*/
22747087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2275d763cef2SBarry Smith {
2276d763cef2SBarry Smith   PetscFunctionBegin;
22770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2278c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2279d763cef2SBarry Smith   ts->time_step = time_step;
2280d763cef2SBarry Smith   PetscFunctionReturn(0);
2281d763cef2SBarry Smith }
2282d763cef2SBarry Smith 
2283a43b19c4SJed Brown /*@
228449354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
228549354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
228649354f04SShri Abhyankar      or just return at the final time TS computed.
2287a43b19c4SJed Brown 
2288a43b19c4SJed Brown   Logically Collective on TS
2289a43b19c4SJed Brown 
2290a43b19c4SJed Brown    Input Parameter:
2291a43b19c4SJed Brown +   ts - the time-step context
229249354f04SShri Abhyankar -   eftopt - exact final time option
2293a43b19c4SJed Brown 
2294feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2295feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2296feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2297feed9e9dSBarry Smith 
2298feed9e9dSBarry Smith    Options Database:
2299feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2300feed9e9dSBarry Smith 
2301ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2302ee346746SBarry Smith     then the final time you selected.
2303ee346746SBarry Smith 
2304a43b19c4SJed Brown    Level: beginner
2305a43b19c4SJed Brown 
2306f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2307a43b19c4SJed Brown @*/
230849354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2309a43b19c4SJed Brown {
2310a43b19c4SJed Brown   PetscFunctionBegin;
2311a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
231249354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
231349354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2314a43b19c4SJed Brown   PetscFunctionReturn(0);
2315a43b19c4SJed Brown }
2316a43b19c4SJed Brown 
2317d763cef2SBarry Smith /*@
2318f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2319f6953c82SLisandro Dalcin 
2320f6953c82SLisandro Dalcin    Not Collective
2321f6953c82SLisandro Dalcin 
2322f6953c82SLisandro Dalcin    Input Parameter:
2323f6953c82SLisandro Dalcin .  ts - the TS context
2324f6953c82SLisandro Dalcin 
2325f6953c82SLisandro Dalcin    Output Parameter:
2326f6953c82SLisandro Dalcin .  eftopt - exact final time option
2327f6953c82SLisandro Dalcin 
2328f6953c82SLisandro Dalcin    Level: beginner
2329f6953c82SLisandro Dalcin 
2330f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2331f6953c82SLisandro Dalcin @*/
2332f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2333f6953c82SLisandro Dalcin {
2334f6953c82SLisandro Dalcin   PetscFunctionBegin;
2335f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2336f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2337f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2338f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2339f6953c82SLisandro Dalcin }
2340f6953c82SLisandro Dalcin 
2341f6953c82SLisandro Dalcin /*@
2342d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2343d763cef2SBarry Smith 
2344d763cef2SBarry Smith    Not Collective
2345d763cef2SBarry Smith 
2346d763cef2SBarry Smith    Input Parameter:
2347d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2348d763cef2SBarry Smith 
2349d763cef2SBarry Smith    Output Parameter:
2350d763cef2SBarry Smith .  dt - the current timestep size
2351d763cef2SBarry Smith 
2352d763cef2SBarry Smith    Level: intermediate
2353d763cef2SBarry Smith 
2354aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2355d763cef2SBarry Smith 
2356d763cef2SBarry Smith @*/
23577087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2358d763cef2SBarry Smith {
2359d763cef2SBarry Smith   PetscFunctionBegin;
23600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2361f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2362d763cef2SBarry Smith   *dt = ts->time_step;
2363d763cef2SBarry Smith   PetscFunctionReturn(0);
2364d763cef2SBarry Smith }
2365d763cef2SBarry Smith 
2366d8e5e3e6SSatish Balay /*@
2367d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2368d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2369d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2370d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2371d763cef2SBarry Smith 
2372d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2373d763cef2SBarry Smith 
2374d763cef2SBarry Smith    Input Parameter:
2375d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2376d763cef2SBarry Smith 
2377d763cef2SBarry Smith    Output Parameter:
2378d763cef2SBarry Smith .  v - the vector containing the solution
2379d763cef2SBarry Smith 
238063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
238163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
238263e21af5SBarry Smith 
2383d763cef2SBarry Smith    Level: intermediate
2384d763cef2SBarry Smith 
23850ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2386d763cef2SBarry Smith 
2387d763cef2SBarry Smith @*/
23887087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2389d763cef2SBarry Smith {
2390d763cef2SBarry Smith   PetscFunctionBegin;
23910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23924482741eSBarry Smith   PetscValidPointer(v,2);
23938737fe31SLisandro Dalcin   *v = ts->vec_sol;
2394d763cef2SBarry Smith   PetscFunctionReturn(0);
2395d763cef2SBarry Smith }
2396d763cef2SBarry Smith 
239703fe5f5eSDebojyoti Ghosh /*@
2398b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
239903fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2400b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
240103fe5f5eSDebojyoti Ghosh    structure as the solution vector.
240203fe5f5eSDebojyoti Ghosh 
240303fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
240403fe5f5eSDebojyoti Ghosh 
240503fe5f5eSDebojyoti Ghosh    Parameters :
2406a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2407b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2408b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
240903fe5f5eSDebojyoti Ghosh        returned in v.
2410a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
241103fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2412b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
241303fe5f5eSDebojyoti Ghosh 
24144cdd57e5SDebojyoti Ghosh    Level: advanced
241503fe5f5eSDebojyoti Ghosh 
241603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
241703fe5f5eSDebojyoti Ghosh 
241803fe5f5eSDebojyoti Ghosh @*/
2419b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
242003fe5f5eSDebojyoti Ghosh {
242103fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
242203fe5f5eSDebojyoti Ghosh 
242303fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
242403fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2425b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
242603fe5f5eSDebojyoti Ghosh   else {
2427b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
242803fe5f5eSDebojyoti Ghosh   }
242903fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
243003fe5f5eSDebojyoti Ghosh }
243103fe5f5eSDebojyoti Ghosh 
24324cdd57e5SDebojyoti Ghosh /*@
24334cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24344cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24354cdd57e5SDebojyoti Ghosh 
24364cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24374cdd57e5SDebojyoti Ghosh 
24384cdd57e5SDebojyoti Ghosh    Parameters :
2439a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2440a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24414cdd57e5SDebojyoti Ghosh 
24424cdd57e5SDebojyoti Ghosh    Level: intermediate
24434cdd57e5SDebojyoti Ghosh 
24444cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24454cdd57e5SDebojyoti Ghosh 
24464cdd57e5SDebojyoti Ghosh @*/
24474cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24484cdd57e5SDebojyoti Ghosh {
24494cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24504cdd57e5SDebojyoti Ghosh 
24514cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24524cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2453f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24544cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2455f6356ec7SDebojyoti Ghosh   } else {
2456f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2457f6356ec7SDebojyoti Ghosh   }
24584cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24594cdd57e5SDebojyoti Ghosh }
24604cdd57e5SDebojyoti Ghosh 
24614cdd57e5SDebojyoti Ghosh /*@
24624cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24634cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24644cdd57e5SDebojyoti Ghosh 
24654cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24664cdd57e5SDebojyoti Ghosh 
24679657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24689657682dSDebojyoti Ghosh 
24694cdd57e5SDebojyoti Ghosh    Parameters :
2470a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2471657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2472a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
24734cdd57e5SDebojyoti Ghosh 
24744cdd57e5SDebojyoti Ghosh    Level: intermediate
24754cdd57e5SDebojyoti Ghosh 
247657df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24774cdd57e5SDebojyoti Ghosh 
24784cdd57e5SDebojyoti Ghosh @*/
24790a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24804cdd57e5SDebojyoti Ghosh {
24814cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24824cdd57e5SDebojyoti Ghosh 
24834cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24844cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2485f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
24860a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2487f6356ec7SDebojyoti Ghosh   } else {
2488f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2489f6356ec7SDebojyoti Ghosh   }
24904cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24914cdd57e5SDebojyoti Ghosh }
24924cdd57e5SDebojyoti Ghosh 
249357df6a1bSDebojyoti Ghosh /*@
249457df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
249557df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
249657df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
249757df6a1bSDebojyoti Ghosh 
249857df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
249957df6a1bSDebojyoti Ghosh 
250057df6a1bSDebojyoti Ghosh    Parameters :
2501a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2502a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
250357df6a1bSDebojyoti Ghosh 
250457df6a1bSDebojyoti Ghosh    Level: intermediate
250557df6a1bSDebojyoti Ghosh 
250657df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
250757df6a1bSDebojyoti Ghosh 
250857df6a1bSDebojyoti Ghosh @*/
250957df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
251057df6a1bSDebojyoti Ghosh {
251157df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
251257df6a1bSDebojyoti Ghosh 
251357df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
251457df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25159657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
251657df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
251757df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
251857df6a1bSDebojyoti Ghosh   }
251957df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
252057df6a1bSDebojyoti Ghosh }
252157df6a1bSDebojyoti Ghosh 
2522bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2523d8e5e3e6SSatish Balay /*@
2524bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2525d763cef2SBarry Smith 
2526bdad233fSMatthew Knepley   Not collective
2527d763cef2SBarry Smith 
2528bdad233fSMatthew Knepley   Input Parameters:
2529bdad233fSMatthew Knepley + ts   - The TS
2530bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2531d763cef2SBarry Smith .vb
25320910c330SBarry Smith          U_t - A U = 0      (linear)
25330910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25340910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2535d763cef2SBarry Smith .ve
2536d763cef2SBarry Smith 
2537d763cef2SBarry Smith    Level: beginner
2538d763cef2SBarry Smith 
2539bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2540d763cef2SBarry Smith @*/
25417087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2542a7cc72afSBarry Smith {
25439e2a6581SJed Brown   PetscErrorCode ierr;
25449e2a6581SJed Brown 
2545d763cef2SBarry Smith   PetscFunctionBegin;
25460700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2547bdad233fSMatthew Knepley   ts->problem_type = type;
25489e2a6581SJed Brown   if (type == TS_LINEAR) {
25499e2a6581SJed Brown     SNES snes;
25509e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25519e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25529e2a6581SJed Brown   }
2553d763cef2SBarry Smith   PetscFunctionReturn(0);
2554d763cef2SBarry Smith }
2555d763cef2SBarry Smith 
2556bdad233fSMatthew Knepley /*@C
2557bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2558bdad233fSMatthew Knepley 
2559bdad233fSMatthew Knepley   Not collective
2560bdad233fSMatthew Knepley 
2561bdad233fSMatthew Knepley   Input Parameter:
2562bdad233fSMatthew Knepley . ts   - The TS
2563bdad233fSMatthew Knepley 
2564bdad233fSMatthew Knepley   Output Parameter:
2565bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2566bdad233fSMatthew Knepley .vb
2567089b2837SJed Brown          M U_t = A U
2568089b2837SJed Brown          M(t) U_t = A(t) U
2569b5abc632SBarry Smith          F(t,U,U_t)
2570bdad233fSMatthew Knepley .ve
2571bdad233fSMatthew Knepley 
2572bdad233fSMatthew Knepley    Level: beginner
2573bdad233fSMatthew Knepley 
2574bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2575bdad233fSMatthew Knepley @*/
25767087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2577a7cc72afSBarry Smith {
2578bdad233fSMatthew Knepley   PetscFunctionBegin;
25790700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25804482741eSBarry Smith   PetscValidIntPointer(type,2);
2581bdad233fSMatthew Knepley   *type = ts->problem_type;
2582bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2583bdad233fSMatthew Knepley }
2584d763cef2SBarry Smith 
2585d763cef2SBarry Smith /*@
2586d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2587d763cef2SBarry Smith    of a timestepper.
2588d763cef2SBarry Smith 
2589d763cef2SBarry Smith    Collective on TS
2590d763cef2SBarry Smith 
2591d763cef2SBarry Smith    Input Parameter:
2592d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2593d763cef2SBarry Smith 
2594d763cef2SBarry Smith    Notes:
2595d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2596d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2597141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2598d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2599141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2600d763cef2SBarry Smith 
2601d763cef2SBarry Smith    Level: advanced
2602d763cef2SBarry Smith 
2603141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2604d763cef2SBarry Smith @*/
26057087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2606d763cef2SBarry Smith {
2607dfbe8321SBarry Smith   PetscErrorCode ierr;
26086c6b9e74SPeter Brune   DM             dm;
26096c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2610d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2611cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26126c6b9e74SPeter Brune   TSIJacobian    ijac;
2613efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26146c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
26152ffb9264SLisandro Dalcin   PetscBool      isnone;
2616d763cef2SBarry Smith 
2617d763cef2SBarry Smith   PetscFunctionBegin;
26180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2619277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2620277b19d0SLisandro Dalcin 
26217adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2622cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2623cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2624d763cef2SBarry Smith   }
2625277b19d0SLisandro Dalcin 
26261a638600SStefano Zampini   if (!ts->vec_sol) {
26271a638600SStefano Zampini     if (ts->dm) {
26281a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
26291a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
26301a638600SStefano Zampini   }
2631277b19d0SLisandro Dalcin 
2632298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2633298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2634298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2635298bade4SHong Zhang   }
2636298bade4SHong Zhang 
2637cd4cee2dSHong Zhang   if (ts->quadraturets) {
2638cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2639ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2640cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2641cd4cee2dSHong Zhang   }
2642cd4cee2dSHong Zhang 
2643971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2644971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2645e1244c69SJed Brown     Mat Amat,Pmat;
2646e1244c69SJed Brown     SNES snes;
2647e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2648e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2649e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2650e1244c69SJed Brown      * have displaced the RHS matrix */
2651971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2652abc0d4abSBarry 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 */
2653abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2654e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2655e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2656e1244c69SJed Brown     }
2657971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2658abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2659e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2660e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2661e1244c69SJed Brown     }
2662e1244c69SJed Brown   }
26632ffb9264SLisandro Dalcin 
26642ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
26652ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
26662ffb9264SLisandro Dalcin 
2667277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2668000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2669277b19d0SLisandro Dalcin   }
2670277b19d0SLisandro Dalcin 
26712ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
26722ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
26732ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
26742ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
26752ffb9264SLisandro Dalcin 
2676a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26776c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26786c6b9e74SPeter Brune    */
26796c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
26800298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
26816c6b9e74SPeter Brune   if (!func) {
26826c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
26836c6b9e74SPeter Brune   }
2684a6772fa2SLisandro 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.
26856c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26866c6b9e74SPeter Brune    */
26870298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26880298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2689efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26900298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2691efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
26926c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
26936c6b9e74SPeter Brune   }
2694c0517034SDebojyoti Ghosh 
2695c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2696c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2697c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2698c0517034SDebojyoti Ghosh   }
2699c0517034SDebojyoti Ghosh 
2700277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2701277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2702277b19d0SLisandro Dalcin }
2703277b19d0SLisandro Dalcin 
2704f6a906c0SBarry Smith /*@
2705277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2706277b19d0SLisandro Dalcin 
2707277b19d0SLisandro Dalcin    Collective on TS
2708277b19d0SLisandro Dalcin 
2709277b19d0SLisandro Dalcin    Input Parameter:
2710277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2711277b19d0SLisandro Dalcin 
2712277b19d0SLisandro Dalcin    Level: beginner
2713277b19d0SLisandro Dalcin 
2714277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2715277b19d0SLisandro Dalcin @*/
2716277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2717277b19d0SLisandro Dalcin {
27181d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2719277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2720277b19d0SLisandro Dalcin 
2721277b19d0SLisandro Dalcin   PetscFunctionBegin;
2722277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2723b18ea86cSHong Zhang 
2724277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2725277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2726277b19d0SLisandro Dalcin   }
2727277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2728e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2729bbd56ea5SKarl Rupp 
27304e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27314e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2732214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27336bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2734efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2735e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2736e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
273738637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2738bbd56ea5SKarl Rupp 
2739cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2740ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2741ecf68647SHong Zhang   if (ts->forward_solve) {
2742ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2743ecf68647SHong Zhang   }
2744cd4cee2dSHong Zhang   if (ts->quadraturets) {
2745cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
274636eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2747cd4cee2dSHong Zhang   }
27481d06f6b3SHong Zhang   while (ilink) {
27491d06f6b3SHong Zhang     next = ilink->next;
27501d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
27511d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
27521d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
27531d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
27541d06f6b3SHong Zhang     ilink = next;
275587f4e208SHong Zhang   }
2756545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2757277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2758d763cef2SBarry Smith   PetscFunctionReturn(0);
2759d763cef2SBarry Smith }
2760d763cef2SBarry Smith 
2761d8e5e3e6SSatish Balay /*@
2762d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2763d763cef2SBarry Smith    with TSCreate().
2764d763cef2SBarry Smith 
2765d763cef2SBarry Smith    Collective on TS
2766d763cef2SBarry Smith 
2767d763cef2SBarry Smith    Input Parameter:
2768d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2769d763cef2SBarry Smith 
2770d763cef2SBarry Smith    Level: beginner
2771d763cef2SBarry Smith 
2772d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2773d763cef2SBarry Smith @*/
27746bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2775d763cef2SBarry Smith {
27766849ba73SBarry Smith   PetscErrorCode ierr;
2777d763cef2SBarry Smith 
2778d763cef2SBarry Smith   PetscFunctionBegin;
27796bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2780ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
27816bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2782d763cef2SBarry Smith 
2783ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2784ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2785ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2786ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2787ecf68647SHong Zhang   }
2788e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2789e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27906bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27916d4c513bSLisandro Dalcin 
2792bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2793bc952696SBarry Smith 
279484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27956427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27966427ac75SLisandro Dalcin 
27976bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27986bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27996bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28000dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28016d4c513bSLisandro Dalcin 
2802cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2803a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2804d763cef2SBarry Smith   PetscFunctionReturn(0);
2805d763cef2SBarry Smith }
2806d763cef2SBarry Smith 
2807d8e5e3e6SSatish Balay /*@
2808d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2809d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2810d763cef2SBarry Smith 
2811d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2812d763cef2SBarry Smith 
2813d763cef2SBarry Smith    Input Parameter:
2814d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2815d763cef2SBarry Smith 
2816d763cef2SBarry Smith    Output Parameter:
2817d763cef2SBarry Smith .  snes - the nonlinear solver context
2818d763cef2SBarry Smith 
2819d763cef2SBarry Smith    Notes:
2820d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2821d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
282294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2823d763cef2SBarry Smith 
2824d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28250298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2826d763cef2SBarry Smith 
2827d763cef2SBarry Smith    Level: beginner
2828d763cef2SBarry Smith 
2829d763cef2SBarry Smith @*/
28307087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2831d763cef2SBarry Smith {
2832d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2833d372ba47SLisandro Dalcin 
2834d763cef2SBarry Smith   PetscFunctionBegin;
28350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28364482741eSBarry Smith   PetscValidPointer(snes,2);
2837d372ba47SLisandro Dalcin   if (!ts->snes) {
2838ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
283916413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
28400298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28413bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2842d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2843496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28449e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28459e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
28469e2a6581SJed Brown     }
2847d372ba47SLisandro Dalcin   }
2848d763cef2SBarry Smith   *snes = ts->snes;
2849d763cef2SBarry Smith   PetscFunctionReturn(0);
2850d763cef2SBarry Smith }
2851d763cef2SBarry Smith 
2852deb2cd25SJed Brown /*@
2853deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2854deb2cd25SJed Brown 
2855deb2cd25SJed Brown    Collective
2856deb2cd25SJed Brown 
2857deb2cd25SJed Brown    Input Parameter:
2858deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2859deb2cd25SJed Brown -  snes - the nonlinear solver context
2860deb2cd25SJed Brown 
2861deb2cd25SJed Brown    Notes:
2862deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2863deb2cd25SJed Brown 
2864deb2cd25SJed Brown    Level: developer
2865deb2cd25SJed Brown 
2866deb2cd25SJed Brown @*/
2867deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2868deb2cd25SJed Brown {
2869deb2cd25SJed Brown   PetscErrorCode ierr;
2870d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2871deb2cd25SJed Brown 
2872deb2cd25SJed Brown   PetscFunctionBegin;
2873deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2874deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2875deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2876deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2877bbd56ea5SKarl Rupp 
2878deb2cd25SJed Brown   ts->snes = snes;
2879bbd56ea5SKarl Rupp 
28800298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28810298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2882740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28830298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2884740132f1SEmil Constantinescu   }
2885deb2cd25SJed Brown   PetscFunctionReturn(0);
2886deb2cd25SJed Brown }
2887deb2cd25SJed Brown 
2888d8e5e3e6SSatish Balay /*@
288994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2890d763cef2SBarry Smith    a TS (timestepper) context.
2891d763cef2SBarry Smith 
289294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2893d763cef2SBarry Smith 
2894d763cef2SBarry Smith    Input Parameter:
2895d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2896d763cef2SBarry Smith 
2897d763cef2SBarry Smith    Output Parameter:
289894b7f48cSBarry Smith .  ksp - the nonlinear solver context
2899d763cef2SBarry Smith 
2900d763cef2SBarry Smith    Notes:
290194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2902d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2903d763cef2SBarry Smith    KSP and PC contexts as well.
2904d763cef2SBarry Smith 
290594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29060298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2907d763cef2SBarry Smith 
2908d763cef2SBarry Smith    Level: beginner
2909d763cef2SBarry Smith 
2910d763cef2SBarry Smith @*/
29117087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2912d763cef2SBarry Smith {
2913d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2914089b2837SJed Brown   SNES           snes;
2915d372ba47SLisandro Dalcin 
2916d763cef2SBarry Smith   PetscFunctionBegin;
29170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29184482741eSBarry Smith   PetscValidPointer(ksp,2);
291917186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2920e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2921089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2922089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2923d763cef2SBarry Smith   PetscFunctionReturn(0);
2924d763cef2SBarry Smith }
2925d763cef2SBarry Smith 
2926d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2927d763cef2SBarry Smith 
2928adb62b0dSMatthew Knepley /*@
2929618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2930618ce8baSLisandro Dalcin 
2931618ce8baSLisandro Dalcin    Logically Collective on TS
2932618ce8baSLisandro Dalcin 
2933618ce8baSLisandro Dalcin    Input Parameters:
2934618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2935618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2936618ce8baSLisandro Dalcin 
2937618ce8baSLisandro Dalcin    Options Database Keys:
2938618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2939618ce8baSLisandro Dalcin 
2940618ce8baSLisandro Dalcin    Notes:
2941618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2942618ce8baSLisandro Dalcin 
2943618ce8baSLisandro Dalcin    Level: intermediate
2944618ce8baSLisandro Dalcin 
2945618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2946618ce8baSLisandro Dalcin @*/
2947618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2948618ce8baSLisandro Dalcin {
2949618ce8baSLisandro Dalcin   PetscFunctionBegin;
2950618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2951618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2952618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2953618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2954618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2955618ce8baSLisandro Dalcin }
2956618ce8baSLisandro Dalcin 
2957618ce8baSLisandro Dalcin /*@
2958618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2959618ce8baSLisandro Dalcin 
2960618ce8baSLisandro Dalcin    Not Collective
2961618ce8baSLisandro Dalcin 
2962618ce8baSLisandro Dalcin    Input Parameters:
2963618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2964618ce8baSLisandro Dalcin 
2965618ce8baSLisandro Dalcin    Output Parameter:
2966618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2967618ce8baSLisandro Dalcin 
2968618ce8baSLisandro Dalcin    Level: advanced
2969618ce8baSLisandro Dalcin 
2970618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2971618ce8baSLisandro Dalcin @*/
2972618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2973618ce8baSLisandro Dalcin {
2974618ce8baSLisandro Dalcin   PetscFunctionBegin;
2975618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2976618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2977618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2978618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2979618ce8baSLisandro Dalcin }
2980618ce8baSLisandro Dalcin 
2981618ce8baSLisandro Dalcin /*@
2982618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2983618ce8baSLisandro Dalcin 
2984618ce8baSLisandro Dalcin    Logically Collective on TS
2985618ce8baSLisandro Dalcin 
2986618ce8baSLisandro Dalcin    Input Parameters:
2987618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2988618ce8baSLisandro Dalcin -  maxtime - final time to step to
2989618ce8baSLisandro Dalcin 
2990618ce8baSLisandro Dalcin    Options Database Keys:
2991ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2992618ce8baSLisandro Dalcin 
2993618ce8baSLisandro Dalcin    Notes:
2994618ce8baSLisandro Dalcin    The default maximum time is 5.0
2995618ce8baSLisandro Dalcin 
2996618ce8baSLisandro Dalcin    Level: intermediate
2997618ce8baSLisandro Dalcin 
2998618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2999618ce8baSLisandro Dalcin @*/
3000618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3001618ce8baSLisandro Dalcin {
3002618ce8baSLisandro Dalcin   PetscFunctionBegin;
3003618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3004618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3005618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3006618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3007618ce8baSLisandro Dalcin }
3008618ce8baSLisandro Dalcin 
3009618ce8baSLisandro Dalcin /*@
3010618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3011618ce8baSLisandro Dalcin 
3012618ce8baSLisandro Dalcin    Not Collective
3013618ce8baSLisandro Dalcin 
3014618ce8baSLisandro Dalcin    Input Parameters:
3015618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3016618ce8baSLisandro Dalcin 
3017618ce8baSLisandro Dalcin    Output Parameter:
3018618ce8baSLisandro Dalcin .  maxtime - final time to step to
3019618ce8baSLisandro Dalcin 
3020618ce8baSLisandro Dalcin    Level: advanced
3021618ce8baSLisandro Dalcin 
3022618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3023618ce8baSLisandro Dalcin @*/
3024618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3025618ce8baSLisandro Dalcin {
3026618ce8baSLisandro Dalcin   PetscFunctionBegin;
3027618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3028618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3029618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3030618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3031618ce8baSLisandro Dalcin }
3032618ce8baSLisandro Dalcin 
3033618ce8baSLisandro Dalcin /*@
3034aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3035edc382c3SSatish Balay 
3036edc382c3SSatish Balay    Level: deprecated
3037edc382c3SSatish Balay 
3038aaa6c58dSLisandro Dalcin @*/
3039aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3040aaa6c58dSLisandro Dalcin {
3041aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3042aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3043aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3044aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3045aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3046aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3047aaa6c58dSLisandro Dalcin }
3048aaa6c58dSLisandro Dalcin 
3049aaa6c58dSLisandro Dalcin /*@
305019eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3051edc382c3SSatish Balay 
3052edc382c3SSatish Balay    Level: deprecated
3053edc382c3SSatish Balay 
3054adb62b0dSMatthew Knepley @*/
30557087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3056adb62b0dSMatthew Knepley {
3057adb62b0dSMatthew Knepley   PetscFunctionBegin;
30580700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3059abc0a331SBarry Smith   if (maxsteps) {
30604482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3061adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3062adb62b0dSMatthew Knepley   }
3063abc0a331SBarry Smith   if (maxtime) {
30644482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3065adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3066adb62b0dSMatthew Knepley   }
3067adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3068adb62b0dSMatthew Knepley }
3069adb62b0dSMatthew Knepley 
3070d763cef2SBarry Smith /*@
307119eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3072edc382c3SSatish Balay 
3073edc382c3SSatish Balay    Level: deprecated
3074edc382c3SSatish Balay 
3075d763cef2SBarry Smith @*/
30767087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3077d763cef2SBarry Smith {
3078d763cef2SBarry Smith   PetscFunctionBegin;
30790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3080c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3081c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
308239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
308339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3084d763cef2SBarry Smith   PetscFunctionReturn(0);
3085d763cef2SBarry Smith }
3086d763cef2SBarry Smith 
3087d763cef2SBarry Smith /*@
30885c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3089edc382c3SSatish Balay 
3090edc382c3SSatish Balay    Level: deprecated
3091edc382c3SSatish Balay 
30925c5f5948SLisandro Dalcin @*/
3093e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30945c5f5948SLisandro Dalcin 
309519eac22cSLisandro Dalcin /*@
30964f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3097edc382c3SSatish Balay 
3098edc382c3SSatish Balay    Level: deprecated
3099edc382c3SSatish Balay 
31004f4e0956SLisandro Dalcin @*/
31014f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31024f4e0956SLisandro Dalcin 
31034f4e0956SLisandro Dalcin /*@
3104d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3105d763cef2SBarry Smith    for use by the TS routines.
3106d763cef2SBarry Smith 
3107d083f849SBarry Smith    Logically Collective on TS
3108d763cef2SBarry Smith 
3109d763cef2SBarry Smith    Input Parameters:
3110d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31110910c330SBarry Smith -  u - the solution vector
3112d763cef2SBarry Smith 
3113d763cef2SBarry Smith    Level: beginner
3114d763cef2SBarry Smith 
31150ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3116d763cef2SBarry Smith @*/
31170910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3118d763cef2SBarry Smith {
31198737fe31SLisandro Dalcin   PetscErrorCode ierr;
3120496e6a7aSJed Brown   DM             dm;
31218737fe31SLisandro Dalcin 
3122d763cef2SBarry Smith   PetscFunctionBegin;
31230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31240910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31250910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31266bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31270910c330SBarry Smith   ts->vec_sol = u;
3128bbd56ea5SKarl Rupp 
3129496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
31300910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3131d763cef2SBarry Smith   PetscFunctionReturn(0);
3132d763cef2SBarry Smith }
3133d763cef2SBarry Smith 
3134ac226902SBarry Smith /*@C
3135000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31363f2090d5SJed Brown   called once at the beginning of each time step.
3137000e7ae3SMatthew Knepley 
31383f9fe445SBarry Smith   Logically Collective on TS
3139000e7ae3SMatthew Knepley 
3140000e7ae3SMatthew Knepley   Input Parameters:
3141000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3142000e7ae3SMatthew Knepley - func - The function
3143000e7ae3SMatthew Knepley 
3144000e7ae3SMatthew Knepley   Calling sequence of func:
31456bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3146000e7ae3SMatthew Knepley 
3147000e7ae3SMatthew Knepley   Level: intermediate
3148000e7ae3SMatthew Knepley 
3149dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3150000e7ae3SMatthew Knepley @*/
31517087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3152000e7ae3SMatthew Knepley {
3153000e7ae3SMatthew Knepley   PetscFunctionBegin;
31540700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3155ae60f76fSBarry Smith   ts->prestep = func;
3156000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3157000e7ae3SMatthew Knepley }
3158000e7ae3SMatthew Knepley 
315909ee8438SJed Brown /*@
31603f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31613f2090d5SJed Brown 
31623f2090d5SJed Brown   Collective on TS
31633f2090d5SJed Brown 
31643f2090d5SJed Brown   Input Parameters:
31653f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
31663f2090d5SJed Brown 
31673f2090d5SJed Brown   Notes:
31683f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
31693f2090d5SJed Brown   so most users would not generally call this routine themselves.
31703f2090d5SJed Brown 
31713f2090d5SJed Brown   Level: developer
31723f2090d5SJed Brown 
31739be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
31743f2090d5SJed Brown @*/
31757087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
31763f2090d5SJed Brown {
31773f2090d5SJed Brown   PetscErrorCode ierr;
31783f2090d5SJed Brown 
31793f2090d5SJed Brown   PetscFunctionBegin;
31800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3181ae60f76fSBarry Smith   if (ts->prestep) {
31825efd42a4SStefano Zampini     Vec              U;
31835efd42a4SStefano Zampini     PetscObjectState sprev,spost;
31845efd42a4SStefano Zampini 
31855efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
31865efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3187ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
31885efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3189dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3190312ce896SJed Brown   }
31913f2090d5SJed Brown   PetscFunctionReturn(0);
31923f2090d5SJed Brown }
31933f2090d5SJed Brown 
3194b8123daeSJed Brown /*@C
3195b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3196b8123daeSJed Brown   called once at the beginning of each stage.
3197b8123daeSJed Brown 
3198b8123daeSJed Brown   Logically Collective on TS
3199b8123daeSJed Brown 
3200b8123daeSJed Brown   Input Parameters:
3201b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3202b8123daeSJed Brown - func - The function
3203b8123daeSJed Brown 
3204b8123daeSJed Brown   Calling sequence of func:
3205b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3206b8123daeSJed Brown 
3207b8123daeSJed Brown   Level: intermediate
3208b8123daeSJed Brown 
3209b8123daeSJed Brown   Note:
3210b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
321180275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3212b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3213b8123daeSJed Brown 
32149be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3215b8123daeSJed Brown @*/
3216b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3217b8123daeSJed Brown {
3218b8123daeSJed Brown   PetscFunctionBegin;
3219b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3220ae60f76fSBarry Smith   ts->prestage = func;
3221b8123daeSJed Brown   PetscFunctionReturn(0);
3222b8123daeSJed Brown }
3223b8123daeSJed Brown 
32249be3e283SDebojyoti Ghosh /*@C
32259be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32269be3e283SDebojyoti Ghosh   called once at the end of each stage.
32279be3e283SDebojyoti Ghosh 
32289be3e283SDebojyoti Ghosh   Logically Collective on TS
32299be3e283SDebojyoti Ghosh 
32309be3e283SDebojyoti Ghosh   Input Parameters:
32319be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32329be3e283SDebojyoti Ghosh - func - The function
32339be3e283SDebojyoti Ghosh 
32349be3e283SDebojyoti Ghosh   Calling sequence of func:
32359be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32369be3e283SDebojyoti Ghosh 
32379be3e283SDebojyoti Ghosh   Level: intermediate
32389be3e283SDebojyoti Ghosh 
32399be3e283SDebojyoti Ghosh   Note:
32409be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
324180275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32429be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32439be3e283SDebojyoti Ghosh 
32449be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32459be3e283SDebojyoti Ghosh @*/
32469be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32479be3e283SDebojyoti Ghosh {
32489be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32499be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32509be3e283SDebojyoti Ghosh   ts->poststage = func;
32519be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32529be3e283SDebojyoti Ghosh }
32539be3e283SDebojyoti Ghosh 
3254c688d042SShri Abhyankar /*@C
3255c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3256c688d042SShri Abhyankar   called once at the end of each step evaluation.
3257c688d042SShri Abhyankar 
3258c688d042SShri Abhyankar   Logically Collective on TS
3259c688d042SShri Abhyankar 
3260c688d042SShri Abhyankar   Input Parameters:
3261c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3262c688d042SShri Abhyankar - func - The function
3263c688d042SShri Abhyankar 
3264c688d042SShri Abhyankar   Calling sequence of func:
3265c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3266c688d042SShri Abhyankar 
3267c688d042SShri Abhyankar   Level: intermediate
3268c688d042SShri Abhyankar 
3269c688d042SShri Abhyankar   Note:
32701785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
32711785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3272e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3273e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3274e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3275c688d042SShri Abhyankar 
3276c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3277c688d042SShri Abhyankar @*/
3278c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3279c688d042SShri Abhyankar {
3280c688d042SShri Abhyankar   PetscFunctionBegin;
3281c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3282c688d042SShri Abhyankar   ts->postevaluate = func;
3283c688d042SShri Abhyankar   PetscFunctionReturn(0);
3284c688d042SShri Abhyankar }
3285c688d042SShri Abhyankar 
3286b8123daeSJed Brown /*@
3287b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3288b8123daeSJed Brown 
3289b8123daeSJed Brown   Collective on TS
3290b8123daeSJed Brown 
3291b8123daeSJed Brown   Input Parameters:
3292b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
32939be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3294b8123daeSJed Brown 
3295b8123daeSJed Brown   Notes:
3296b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3297b8123daeSJed Brown   most users would not generally call this routine themselves.
3298b8123daeSJed Brown 
3299b8123daeSJed Brown   Level: developer
3300b8123daeSJed Brown 
33019be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3302b8123daeSJed Brown @*/
3303b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3304b8123daeSJed Brown {
3305b8123daeSJed Brown   PetscFunctionBegin;
3306b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3307ae60f76fSBarry Smith   if (ts->prestage) {
3308ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3309b8123daeSJed Brown   }
3310b8123daeSJed Brown   PetscFunctionReturn(0);
3311b8123daeSJed Brown }
3312b8123daeSJed Brown 
33139be3e283SDebojyoti Ghosh /*@
33149be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33159be3e283SDebojyoti Ghosh 
33169be3e283SDebojyoti Ghosh   Collective on TS
33179be3e283SDebojyoti Ghosh 
33189be3e283SDebojyoti Ghosh   Input Parameters:
33199be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33209be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33219be3e283SDebojyoti Ghosh   stageindex  - Stage number
33229be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33239be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33249be3e283SDebojyoti Ghosh 
33259be3e283SDebojyoti Ghosh   Notes:
33269be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33279be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33289be3e283SDebojyoti Ghosh 
33299be3e283SDebojyoti Ghosh   Level: developer
33309be3e283SDebojyoti Ghosh 
33319be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33329be3e283SDebojyoti Ghosh @*/
33339be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33349be3e283SDebojyoti Ghosh {
33359be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33369be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33374beae5d8SLisandro Dalcin   if (ts->poststage) {
33389be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33399be3e283SDebojyoti Ghosh   }
33409be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33419be3e283SDebojyoti Ghosh }
33429be3e283SDebojyoti Ghosh 
3343c688d042SShri Abhyankar /*@
3344c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3345c688d042SShri Abhyankar 
3346c688d042SShri Abhyankar   Collective on TS
3347c688d042SShri Abhyankar 
3348c688d042SShri Abhyankar   Input Parameters:
3349c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3350c688d042SShri Abhyankar 
3351c688d042SShri Abhyankar   Notes:
3352c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3353c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3354c688d042SShri Abhyankar 
3355c688d042SShri Abhyankar   Level: developer
3356c688d042SShri Abhyankar 
3357c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3358c688d042SShri Abhyankar @*/
3359c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3360c688d042SShri Abhyankar {
3361c688d042SShri Abhyankar   PetscErrorCode ierr;
3362c688d042SShri Abhyankar 
3363c688d042SShri Abhyankar   PetscFunctionBegin;
3364c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3365c688d042SShri Abhyankar   if (ts->postevaluate) {
3366dcb233daSLisandro Dalcin     Vec              U;
3367dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3368dcb233daSLisandro Dalcin 
3369dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3370dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3371c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3372dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3373dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3374c688d042SShri Abhyankar   }
3375c688d042SShri Abhyankar   PetscFunctionReturn(0);
3376c688d042SShri Abhyankar }
3377c688d042SShri Abhyankar 
3378ac226902SBarry Smith /*@C
3379000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
33803f2090d5SJed Brown   called once at the end of each time step.
3381000e7ae3SMatthew Knepley 
33823f9fe445SBarry Smith   Logically Collective on TS
3383000e7ae3SMatthew Knepley 
3384000e7ae3SMatthew Knepley   Input Parameters:
3385000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3386000e7ae3SMatthew Knepley - func - The function
3387000e7ae3SMatthew Knepley 
3388000e7ae3SMatthew Knepley   Calling sequence of func:
3389b8123daeSJed Brown $ func (TS ts);
3390000e7ae3SMatthew Knepley 
33911785ff2aSShri Abhyankar   Notes:
33921785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
33931785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
33941785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
33951785ff2aSShri Abhyankar 
3396000e7ae3SMatthew Knepley   Level: intermediate
3397000e7ae3SMatthew Knepley 
3398dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3399000e7ae3SMatthew Knepley @*/
34007087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3401000e7ae3SMatthew Knepley {
3402000e7ae3SMatthew Knepley   PetscFunctionBegin;
34030700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3404ae60f76fSBarry Smith   ts->poststep = func;
3405000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3406000e7ae3SMatthew Knepley }
3407000e7ae3SMatthew Knepley 
340809ee8438SJed Brown /*@
34093f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34103f2090d5SJed Brown 
34113f2090d5SJed Brown   Collective on TS
34123f2090d5SJed Brown 
34133f2090d5SJed Brown   Input Parameters:
34143f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34153f2090d5SJed Brown 
34163f2090d5SJed Brown   Notes:
34173f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34183f2090d5SJed Brown   so most users would not generally call this routine themselves.
34193f2090d5SJed Brown 
34203f2090d5SJed Brown   Level: developer
34213f2090d5SJed Brown 
34223f2090d5SJed Brown @*/
34237087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34243f2090d5SJed Brown {
34253f2090d5SJed Brown   PetscErrorCode ierr;
34263f2090d5SJed Brown 
34273f2090d5SJed Brown   PetscFunctionBegin;
34280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3429ae60f76fSBarry Smith   if (ts->poststep) {
34305efd42a4SStefano Zampini     Vec              U;
34315efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34325efd42a4SStefano Zampini 
34335efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
34345efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3435ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
34365efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3437dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
343872ac3e02SJed Brown   }
34393f2090d5SJed Brown   PetscFunctionReturn(0);
34403f2090d5SJed Brown }
34413f2090d5SJed Brown 
3442d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3443d763cef2SBarry Smith 
3444d763cef2SBarry Smith /*@C
3445a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3446d763cef2SBarry Smith    timestep to display the iteration's  progress.
3447d763cef2SBarry Smith 
34483f9fe445SBarry Smith    Logically Collective on TS
3449d763cef2SBarry Smith 
3450d763cef2SBarry Smith    Input Parameters:
3451d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3452e213d8f1SJed Brown .  monitor - monitoring routine
3453329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34540298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3455b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34560298fd71SBarry Smith           (may be NULL)
3457d763cef2SBarry Smith 
3458e213d8f1SJed Brown    Calling sequence of monitor:
3459dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3460d763cef2SBarry Smith 
3461d763cef2SBarry Smith +    ts - the TS context
346263e21af5SBarry 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)
34631f06c33eSBarry Smith .    time - current time
34640910c330SBarry Smith .    u - current iterate
3465d763cef2SBarry Smith -    mctx - [optional] monitoring context
3466d763cef2SBarry Smith 
3467d763cef2SBarry Smith    Notes:
3468d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3469d763cef2SBarry Smith    already has been loaded.
3470d763cef2SBarry Smith 
347195452b02SPatrick Sanan    Fortran Notes:
347295452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3473025f1a04SBarry Smith 
3474d763cef2SBarry Smith    Level: intermediate
3475d763cef2SBarry Smith 
3476a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3477d763cef2SBarry Smith @*/
3478c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3479d763cef2SBarry Smith {
348078064530SBarry Smith   PetscErrorCode ierr;
348178064530SBarry Smith   PetscInt       i;
348278064530SBarry Smith   PetscBool      identical;
348378064530SBarry Smith 
3484d763cef2SBarry Smith   PetscFunctionBegin;
34850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
348678064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
348778064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
348878064530SBarry Smith     if (identical) PetscFunctionReturn(0);
348978064530SBarry Smith   }
349017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3491d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
34928704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3493d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3494d763cef2SBarry Smith   PetscFunctionReturn(0);
3495d763cef2SBarry Smith }
3496d763cef2SBarry Smith 
3497d763cef2SBarry Smith /*@C
3498a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3499d763cef2SBarry Smith 
35003f9fe445SBarry Smith    Logically Collective on TS
3501d763cef2SBarry Smith 
3502d763cef2SBarry Smith    Input Parameters:
3503d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3504d763cef2SBarry Smith 
3505d763cef2SBarry Smith    Notes:
3506d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3507d763cef2SBarry Smith 
3508d763cef2SBarry Smith    Level: intermediate
3509d763cef2SBarry Smith 
3510a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3511d763cef2SBarry Smith @*/
35127087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3513d763cef2SBarry Smith {
3514d952e501SBarry Smith   PetscErrorCode ierr;
3515d952e501SBarry Smith   PetscInt       i;
3516d952e501SBarry Smith 
3517d763cef2SBarry Smith   PetscFunctionBegin;
35180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3519d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35208704b422SBarry Smith     if (ts->monitordestroy[i]) {
35218704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3522d952e501SBarry Smith     }
3523d952e501SBarry Smith   }
3524d763cef2SBarry Smith   ts->numbermonitors = 0;
3525d763cef2SBarry Smith   PetscFunctionReturn(0);
3526d763cef2SBarry Smith }
3527d763cef2SBarry Smith 
3528721cd6eeSBarry Smith /*@C
3529721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35305516499fSSatish Balay 
35315516499fSSatish Balay    Level: intermediate
353241251cbbSSatish Balay 
353363e21af5SBarry Smith .seealso:  TSMonitorSet()
353441251cbbSSatish Balay @*/
3535721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3536d763cef2SBarry Smith {
3537dfbe8321SBarry Smith   PetscErrorCode ierr;
3538721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
353941aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3540d132466eSBarry Smith 
3541d763cef2SBarry Smith   PetscFunctionBegin;
35424d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
354341aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
354441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3545721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
354641aca3d6SBarry Smith   if (iascii) {
3547649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
354863e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
354963e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
355063e21af5SBarry Smith     } else {
35518392e04aSShri 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);
355263e21af5SBarry Smith     }
3553649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
355441aca3d6SBarry Smith   } else if (ibinary) {
355541aca3d6SBarry Smith     PetscMPIInt rank;
355641aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
355741aca3d6SBarry Smith     if (!rank) {
3558450a797fSBarry Smith       PetscBool skipHeader;
3559450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3560450a797fSBarry Smith 
3561450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3562450a797fSBarry Smith       if (!skipHeader) {
3563450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3564450a797fSBarry Smith        }
356541aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
356641aca3d6SBarry Smith     } else {
356741aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
356841aca3d6SBarry Smith     }
356941aca3d6SBarry Smith   }
3570721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3571d763cef2SBarry Smith   PetscFunctionReturn(0);
3572d763cef2SBarry Smith }
3573d763cef2SBarry Smith 
3574cc9c3a59SBarry Smith /*@C
3575cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3576cc9c3a59SBarry Smith 
3577cc9c3a59SBarry Smith    Level: intermediate
3578cc9c3a59SBarry Smith 
3579cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3580cc9c3a59SBarry Smith @*/
3581cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3582cc9c3a59SBarry Smith {
3583cc9c3a59SBarry Smith   PetscErrorCode ierr;
3584cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3585cc9c3a59SBarry Smith   PetscBool      iascii;
3586cc9c3a59SBarry Smith   PetscReal      max,min;
3587cc9c3a59SBarry Smith 
3588cc9c3a59SBarry Smith 
3589cc9c3a59SBarry Smith   PetscFunctionBegin;
3590cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3591cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3592cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3593cc9c3a59SBarry Smith   if (iascii) {
3594cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3595cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3596cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
35975132926bSBarry 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);
3598cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3599cc9c3a59SBarry Smith   }
3600cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3601cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3602cc9c3a59SBarry Smith }
3603cc9c3a59SBarry Smith 
3604cd652676SJed Brown /*@
3605cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3606cd652676SJed Brown 
3607cd652676SJed Brown    Collective on TS
3608cd652676SJed Brown 
3609cd652676SJed Brown    Input Argument:
3610cd652676SJed Brown +  ts - time stepping context
3611cd652676SJed Brown -  t - time to interpolate to
3612cd652676SJed Brown 
3613cd652676SJed Brown    Output Argument:
36140910c330SBarry Smith .  U - state at given time
3615cd652676SJed Brown 
3616cd652676SJed Brown    Level: intermediate
3617cd652676SJed Brown 
3618cd652676SJed Brown    Developer Notes:
3619cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3620cd652676SJed Brown 
3621874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3622cd652676SJed Brown @*/
36230910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3624cd652676SJed Brown {
3625cd652676SJed Brown   PetscErrorCode ierr;
3626cd652676SJed Brown 
3627cd652676SJed Brown   PetscFunctionBegin;
3628cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3629b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3630be5899b3SLisandro 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);
3631ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36320910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3633cd652676SJed Brown   PetscFunctionReturn(0);
3634cd652676SJed Brown }
3635cd652676SJed Brown 
3636d763cef2SBarry Smith /*@
36376d9e5789SSean Farley    TSStep - Steps one time step
3638d763cef2SBarry Smith 
3639d763cef2SBarry Smith    Collective on TS
3640d763cef2SBarry Smith 
3641d763cef2SBarry Smith    Input Parameter:
3642d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3643d763cef2SBarry Smith 
364427829d71SBarry Smith    Level: developer
3645d763cef2SBarry Smith 
3646b8123daeSJed Brown    Notes:
364727829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
364827829d71SBarry Smith 
3649b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3650b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3651b8123daeSJed Brown 
365219eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
365319eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
365425cb2221SBarry Smith 
36559be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3656d763cef2SBarry Smith @*/
3657193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3658d763cef2SBarry Smith {
3659dfbe8321SBarry Smith   PetscErrorCode   ierr;
3660fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3661be5899b3SLisandro Dalcin   PetscReal        ptime;
3662d763cef2SBarry Smith 
3663d763cef2SBarry Smith   PetscFunctionBegin;
36640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3665fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3666fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3667fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3668fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3669fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3670fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3671302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3672fffbeea8SBarry Smith 
3673d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
367468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3675d405a339SMatthew Knepley 
3676fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3677ef85077eSLisandro 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>");
3678a6772fa2SLisandro 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()");
3679be5899b3SLisandro 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");
3680a6772fa2SLisandro Dalcin 
3681be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3682be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
36832808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3684fc8dbba5SLisandro Dalcin 
3685d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3686193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3687d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3688fc8dbba5SLisandro Dalcin 
3689fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3690be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
36912808aa04SLisandro Dalcin     ts->steps++;
3692be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
369328d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3694d2daff3dSHong Zhang   }
3695fc8dbba5SLisandro Dalcin 
3696fc8dbba5SLisandro Dalcin   if (!ts->reason) {
369708c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
369808c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
369908c7845fSBarry Smith   }
3700fc8dbba5SLisandro Dalcin 
3701fc8dbba5SLisandro 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]);
3702fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
370308c7845fSBarry Smith   PetscFunctionReturn(0);
370408c7845fSBarry Smith }
370508c7845fSBarry Smith 
370608c7845fSBarry Smith /*@
37077cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37087cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37097cbde773SLisandro Dalcin 
37107cbde773SLisandro Dalcin    Collective on TS
37117cbde773SLisandro Dalcin 
37127cbde773SLisandro Dalcin    Input Arguments:
37137cbde773SLisandro Dalcin +  ts - time stepping context
37147cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37157cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37167cbde773SLisandro Dalcin 
37177cbde773SLisandro Dalcin    Output Arguments:
37187cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37197cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37207cbde773SLisandro Dalcin 
37217cbde773SLisandro Dalcin    Level: advanced
37227cbde773SLisandro Dalcin 
37237cbde773SLisandro Dalcin    Notes:
37247cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37257cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37267cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37277cbde773SLisandro Dalcin 
37287cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37297cbde773SLisandro Dalcin @*/
37307cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37317cbde773SLisandro Dalcin {
37327cbde773SLisandro Dalcin   PetscErrorCode ierr;
37337cbde773SLisandro Dalcin 
37347cbde773SLisandro Dalcin   PetscFunctionBegin;
37357cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37367cbde773SLisandro Dalcin   PetscValidType(ts,1);
37377cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37387cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37397cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37407cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37417cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37427cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37437cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37447cbde773SLisandro Dalcin   PetscFunctionReturn(0);
37457cbde773SLisandro Dalcin }
37467cbde773SLisandro Dalcin 
374705175c85SJed Brown /*@
374805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
374905175c85SJed Brown 
37501c3436cfSJed Brown    Collective on TS
375105175c85SJed Brown 
375205175c85SJed Brown    Input Arguments:
37531c3436cfSJed Brown +  ts - time stepping context
37541c3436cfSJed Brown .  order - desired order of accuracy
37550298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
375605175c85SJed Brown 
375705175c85SJed Brown    Output Arguments:
37580910c330SBarry Smith .  U - state at the end of the current step
375905175c85SJed Brown 
376005175c85SJed Brown    Level: advanced
376105175c85SJed Brown 
3762108c343cSJed Brown    Notes:
3763108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3764108c343cSJed 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.
3765108c343cSJed Brown 
37661c3436cfSJed Brown .seealso: TSStep(), TSAdapt
376705175c85SJed Brown @*/
37680910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
376905175c85SJed Brown {
377005175c85SJed Brown   PetscErrorCode ierr;
377105175c85SJed Brown 
377205175c85SJed Brown   PetscFunctionBegin;
377305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
377405175c85SJed Brown   PetscValidType(ts,1);
37750910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3776ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
37770910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
377805175c85SJed Brown   PetscFunctionReturn(0);
377905175c85SJed Brown }
378005175c85SJed Brown 
3781aad739acSMatthew G. Knepley /*@C
37822e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3783aad739acSMatthew G. Knepley 
3784aad739acSMatthew G. Knepley   Not collective
3785aad739acSMatthew G. Knepley 
3786aad739acSMatthew G. Knepley   Input Argument:
3787aad739acSMatthew G. Knepley . ts        - time stepping context
3788aad739acSMatthew G. Knepley 
3789aad739acSMatthew G. Knepley   Output Argument:
37902e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3791aad739acSMatthew G. Knepley 
3792aad739acSMatthew G. Knepley    Level: advanced
3793aad739acSMatthew G. Knepley 
3794aad739acSMatthew G. Knepley    Notes:
3795aad739acSMatthew G. Knepley    The calling sequence for the function is
37962e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3797aad739acSMatthew G. Knepley $ ts - The timestepping context
37982e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3799aad739acSMatthew G. Knepley 
38002e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3801aad739acSMatthew G. Knepley @*/
38022e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3803aad739acSMatthew G. Knepley {
3804aad739acSMatthew G. Knepley   PetscFunctionBegin;
3805aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38062e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38072e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3808aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3809aad739acSMatthew G. Knepley }
3810aad739acSMatthew G. Knepley 
3811aad739acSMatthew G. Knepley /*@C
38122e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3813aad739acSMatthew G. Knepley 
3814aad739acSMatthew G. Knepley   Logically collective on ts
3815aad739acSMatthew G. Knepley 
3816aad739acSMatthew G. Knepley   Input Arguments:
3817aad739acSMatthew G. Knepley + ts        - time stepping context
38182e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3819aad739acSMatthew G. Knepley 
3820aad739acSMatthew G. Knepley   Level: advanced
3821aad739acSMatthew G. Knepley 
3822aad739acSMatthew G. Knepley   Notes:
3823aad739acSMatthew G. Knepley   The calling sequence for the function is
38242e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3825aad739acSMatthew G. Knepley $ ts - The timestepping context
38262e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3827aad739acSMatthew G. Knepley 
38282e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3829aad739acSMatthew G. Knepley @*/
38302e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3831aad739acSMatthew G. Knepley {
3832aad739acSMatthew G. Knepley   PetscFunctionBegin;
3833aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38342e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
38352e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3836aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3837aad739acSMatthew G. Knepley }
3838aad739acSMatthew G. Knepley 
3839aad739acSMatthew G. Knepley /*@
38402e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3841aad739acSMatthew G. Knepley 
3842aad739acSMatthew G. Knepley   Collective on ts
3843aad739acSMatthew G. Knepley 
3844aad739acSMatthew G. Knepley   Input Arguments:
3845aad739acSMatthew G. Knepley + ts - time stepping context
38462e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3847aad739acSMatthew G. Knepley 
3848aad739acSMatthew G. Knepley   Level: advanced
3849aad739acSMatthew G. Knepley 
3850aad739acSMatthew G. Knepley   Notes:
3851aad739acSMatthew G. Knepley   The calling sequence for the function is
38522e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3853aad739acSMatthew G. Knepley $ ts - The timestepping context
38542e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3855aad739acSMatthew G. Knepley 
38562e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3857aad739acSMatthew G. Knepley @*/
38582e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3859aad739acSMatthew G. Knepley {
3860aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3861aad739acSMatthew G. Knepley 
3862aad739acSMatthew G. Knepley   PetscFunctionBegin;
3863aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3864aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
38652e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3866aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3867aad739acSMatthew G. Knepley }
3868aad739acSMatthew G. Knepley 
3869aad739acSMatthew G. Knepley /*@C
3870aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3871aad739acSMatthew G. Knepley 
3872aad739acSMatthew G. Knepley   Not collective
3873aad739acSMatthew G. Knepley 
3874aad739acSMatthew G. Knepley   Input Argument:
3875aad739acSMatthew G. Knepley . ts         - time stepping context
3876aad739acSMatthew G. Knepley 
3877aad739acSMatthew G. Knepley   Output Argument:
3878aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3879aad739acSMatthew G. Knepley 
3880aad739acSMatthew G. Knepley   Level: advanced
3881aad739acSMatthew G. Knepley 
3882aad739acSMatthew G. Knepley   Notes:
3883aad739acSMatthew G. Knepley   The calling sequence for the function is
3884aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u)
3885aad739acSMatthew G. Knepley $ ts - The timestepping context
3886aad739acSMatthew G. Knepley $ u  - The approximate solution vector
3887aad739acSMatthew G. Knepley $ e  - The input vector in which the error is stored
3888aad739acSMatthew G. Knepley 
3889aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3890aad739acSMatthew G. Knepley @*/
3891aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3892aad739acSMatthew G. Knepley {
3893aad739acSMatthew G. Knepley   PetscFunctionBegin;
3894aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3895aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3896aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3897aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3898aad739acSMatthew G. Knepley }
3899aad739acSMatthew G. Knepley 
3900aad739acSMatthew G. Knepley /*@C
3901aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3902aad739acSMatthew G. Knepley 
3903aad739acSMatthew G. Knepley   Logically collective on ts
3904aad739acSMatthew G. Knepley 
3905aad739acSMatthew G. Knepley   Input Arguments:
3906aad739acSMatthew G. Knepley + ts         - time stepping context
3907aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3908aad739acSMatthew G. Knepley 
3909aad739acSMatthew G. Knepley   Level: advanced
3910aad739acSMatthew G. Knepley 
3911aad739acSMatthew G. Knepley   Notes:
3912aad739acSMatthew G. Knepley   The calling sequence for the function is
3913aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u)
3914aad739acSMatthew G. Knepley $ ts - The timestepping context
3915aad739acSMatthew G. Knepley $ u  - The approximate solution vector
3916aad739acSMatthew G. Knepley $ e  - The input vector in which the error is stored
3917aad739acSMatthew G. Knepley 
3918aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3919aad739acSMatthew G. Knepley @*/
3920aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3921aad739acSMatthew G. Knepley {
3922aad739acSMatthew G. Knepley   PetscFunctionBegin;
3923aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3924f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3925aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3926aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3927aad739acSMatthew G. Knepley }
3928aad739acSMatthew G. Knepley 
3929aad739acSMatthew G. Knepley /*@
3930aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3931aad739acSMatthew G. Knepley 
3932aad739acSMatthew G. Knepley   Collective on ts
3933aad739acSMatthew G. Knepley 
3934aad739acSMatthew G. Knepley   Input Arguments:
3935aad739acSMatthew G. Knepley + ts - time stepping context
3936aad739acSMatthew G. Knepley . u  - The approximate solution
3937aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3938aad739acSMatthew G. Knepley 
3939aad739acSMatthew G. Knepley   Level: advanced
3940aad739acSMatthew G. Knepley 
3941aad739acSMatthew G. Knepley   Notes:
3942aad739acSMatthew G. Knepley   The calling sequence for the function is
3943aad739acSMatthew G. Knepley $ exactError(TS ts, Vec u)
3944aad739acSMatthew G. Knepley $ ts - The timestepping context
3945aad739acSMatthew G. Knepley $ u  - The approximate solution vector
3946aad739acSMatthew G. Knepley $ e  - The input vector in which the error is stored
3947aad739acSMatthew G. Knepley 
39482e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3949aad739acSMatthew G. Knepley @*/
3950aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3951aad739acSMatthew G. Knepley {
3952aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3953aad739acSMatthew G. Knepley 
3954aad739acSMatthew G. Knepley   PetscFunctionBegin;
3955aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3956aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3957aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
3958aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
3959aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3960aad739acSMatthew G. Knepley }
3961aad739acSMatthew G. Knepley 
3962b1cb36f3SHong Zhang /*@
39636a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
39646a4d4014SLisandro Dalcin 
39656a4d4014SLisandro Dalcin    Collective on TS
39666a4d4014SLisandro Dalcin 
39676a4d4014SLisandro Dalcin    Input Parameter:
39686a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
396963e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
397063e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
39715a3a76d0SJed Brown 
39726a4d4014SLisandro Dalcin    Level: beginner
39736a4d4014SLisandro Dalcin 
39745a3a76d0SJed Brown    Notes:
39755a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
39765a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
39775a3a76d0SJed Brown    stepped over the final time.
39785a3a76d0SJed Brown 
397963e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
39806a4d4014SLisandro Dalcin @*/
3981cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
39826a4d4014SLisandro Dalcin {
3983b06615a5SLisandro Dalcin   Vec               solution;
39846a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3985f22f69f0SBarry Smith 
39866a4d4014SLisandro Dalcin   PetscFunctionBegin;
39870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3988f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3989ee41a567SStefano 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 */
39900910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3991b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3992b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3993b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
39945a3a76d0SJed Brown     }
39950910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3996715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3997bbd56ea5SKarl Rupp   } else if (u) {
39980910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
39995a3a76d0SJed Brown   }
4000b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
400168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4002a6772fa2SLisandro Dalcin 
4003ef85077eSLisandro 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>");
4004a6772fa2SLisandro 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()");
4005a6772fa2SLisandro 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");
4006a6772fa2SLisandro Dalcin 
4007715f1b00SHong Zhang   if (ts->forward_solve) {
4008715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4009715f1b00SHong Zhang   }
4010715f1b00SHong Zhang 
4011e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4012715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4013e7069c78SShri      TSTrajectory to incorrectly save the output files
4014e7069c78SShri   */
4015715f1b00SHong Zhang   /* reset time step and iteration counters */
40162808aa04SLisandro Dalcin   if (!ts->steps) {
40175ef26d82SJed Brown     ts->ksp_its           = 0;
40185ef26d82SJed Brown     ts->snes_its          = 0;
4019c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4020c610991cSLisandro Dalcin     ts->reject            = 0;
40212808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40222808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40237d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
40242808aa04SLisandro Dalcin   }
4025e97c63d7SStefano Zampini 
4026e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4027e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4028e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4029e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4030e97c63d7SStefano Zampini 
4031e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4032e97c63d7SStefano Zampini   }
4033193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4034193ac0bcSJed Brown 
4035900f6b5bSMatthew G. Knepley   {
4036900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4037900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4038900f6b5bSMatthew G. Knepley     PetscBool         flg;
4039900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4040900f6b5bSMatthew G. Knepley 
4041900f6b5bSMatthew G. Knepley     if (!incall) {
4042900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4043900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4044900f6b5bSMatthew G. Knepley       if (flg) {
4045900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4046900f6b5bSMatthew G. Knepley         DM           dm;
4047900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4048900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4049900f6b5bSMatthew G. Knepley 
4050900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4051900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4052900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4053900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4054900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4055900f6b5bSMatthew G. Knepley         ierr = PetscConvEstUseTS(conv);CHKERRQ(ierr);
4056900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4057900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4058900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4059900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4060900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4061900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4062900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4063900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4064900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4065900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4066900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4067900f6b5bSMatthew G. Knepley       }
4068900f6b5bSMatthew G. Knepley     }
4069900f6b5bSMatthew G. Knepley   }
4070900f6b5bSMatthew G. Knepley 
4071ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4072f05ece33SBarry Smith 
4073193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4074193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4075a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4076cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4077a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4078be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4079db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4080db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4081e7069c78SShri 
40822808aa04SLisandro Dalcin     if (!ts->steps) {
40832808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40846427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40852808aa04SLisandro Dalcin     }
40866427ac75SLisandro Dalcin 
4087e1a7a14fSJed Brown     while (!ts->reason) {
40882808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40899687d888SLisandro Dalcin       if (!ts->steprollback) {
40909687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
40919687d888SLisandro Dalcin       }
4092193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4093f3b1f45cSBarry Smith       if (ts->testjacobian) {
4094f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4095f3b1f45cSBarry Smith       }
4096f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4097f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4098f3b1f45cSBarry Smith       }
4099cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
4100b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4101b1cb36f3SHong Zhang       }
410258818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
4103715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
4104715f1b00SHong Zhang       }
410510b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4106e783b05fSHong 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. */
410758818c2dSLisandro Dalcin       if (ts->steprollback) {
410858818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
410958818c2dSLisandro Dalcin       }
4110e783b05fSHong Zhang       if (!ts->steprollback) {
41112808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41121eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4113aeb4809dSShri Abhyankar       }
4114193ac0bcSJed Brown     }
41152808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41166427ac75SLisandro Dalcin 
411749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41180910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4119cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4120b06615a5SLisandro Dalcin       solution = u;
412163e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41220574a7fbSJed Brown     } else {
4123ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4124cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4125b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41260574a7fbSJed Brown     }
4127193ac0bcSJed Brown   }
4128d2daff3dSHong Zhang 
4129ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
413063e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
413156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4132ef222394SBarry Smith   if (ts->adjoint_solve) {
4133ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41342b0a91c0SBarry Smith   }
41356a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41366a4d4014SLisandro Dalcin }
41376a4d4014SLisandro Dalcin 
41387db568b7SBarry Smith /*@C
4139228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4140228d79bcSJed Brown 
4141228d79bcSJed Brown    Collective on TS
4142228d79bcSJed Brown 
4143228d79bcSJed Brown    Input Parameters:
4144228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4145228d79bcSJed Brown .  step - step number that has just completed
4146228d79bcSJed Brown .  ptime - model time of the state
41470910c330SBarry Smith -  u - state at the current model time
4148228d79bcSJed Brown 
4149228d79bcSJed Brown    Notes:
41507db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
41517db568b7SBarry Smith    Users would almost never call this routine directly.
4152228d79bcSJed Brown 
415363e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
415463e21af5SBarry Smith 
41557db568b7SBarry Smith    Level: developer
4156228d79bcSJed Brown 
4157228d79bcSJed Brown @*/
41580910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4159d763cef2SBarry Smith {
4160d6152f81SLisandro Dalcin   DM             dm;
4161a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4162d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4163d763cef2SBarry Smith 
4164d763cef2SBarry Smith   PetscFunctionBegin;
4165b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4166b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4167d6152f81SLisandro Dalcin 
4168d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4169d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4170d6152f81SLisandro Dalcin 
41718860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4172d763cef2SBarry Smith   for (i=0; i<n; i++) {
41730910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4174d763cef2SBarry Smith   }
41758860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4176d763cef2SBarry Smith   PetscFunctionReturn(0);
4177d763cef2SBarry Smith }
4178d763cef2SBarry Smith 
4179d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4180d763cef2SBarry Smith /*@C
41817db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4182a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4183d763cef2SBarry Smith 
4184d763cef2SBarry Smith    Collective on TS
4185d763cef2SBarry Smith 
4186d763cef2SBarry Smith    Input Parameters:
4187d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4188d763cef2SBarry Smith .  label - the title to put in the title bar
41897c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4190a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4191a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4192d763cef2SBarry Smith 
4193d763cef2SBarry Smith    Output Parameter:
41940b039ecaSBarry Smith .  ctx - the context
4195d763cef2SBarry Smith 
4196d763cef2SBarry Smith    Options Database Key:
41974f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
41988b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
41997db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42007db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42017db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42027db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4203b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4204d763cef2SBarry Smith 
4205d763cef2SBarry Smith    Notes:
4206a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4207d763cef2SBarry Smith 
42087db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42097db568b7SBarry Smith 
42107db568b7SBarry 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
42117db568b7SBarry 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
42127db568b7SBarry Smith    as the first argument.
42137db568b7SBarry Smith 
42147db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42157db568b7SBarry Smith 
4216d763cef2SBarry Smith    Level: intermediate
4217d763cef2SBarry Smith 
42187db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
42197db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
42207db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
42217db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
42227db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
42237c922b88SBarry Smith 
4224d763cef2SBarry Smith @*/
4225a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4226d763cef2SBarry Smith {
42277f52daa2SLisandro Dalcin   PetscDraw      draw;
4228dfbe8321SBarry Smith   PetscErrorCode ierr;
4229d763cef2SBarry Smith 
4230d763cef2SBarry Smith   PetscFunctionBegin;
4231b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
42327f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
42337f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
42347f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4235287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
42367f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4237a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4238d763cef2SBarry Smith   PetscFunctionReturn(0);
4239d763cef2SBarry Smith }
4240d763cef2SBarry Smith 
4241b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4242d763cef2SBarry Smith {
42430b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4244c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4245dfbe8321SBarry Smith   PetscErrorCode ierr;
4246d763cef2SBarry Smith 
4247d763cef2SBarry Smith   PetscFunctionBegin;
424863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4249b06615a5SLisandro Dalcin   if (!step) {
4250a9f9c1f6SBarry Smith     PetscDrawAxis axis;
42518b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4252a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
42538b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4254a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4255a9f9c1f6SBarry Smith   }
4256c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
42578b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
42580b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4259b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
42600b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
42616934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
42623923b477SBarry Smith   }
4263d763cef2SBarry Smith   PetscFunctionReturn(0);
4264d763cef2SBarry Smith }
4265d763cef2SBarry Smith 
4266d763cef2SBarry Smith /*@C
4267a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4268a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4269d763cef2SBarry Smith 
42700b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4271d763cef2SBarry Smith 
4272d763cef2SBarry Smith    Input Parameter:
42730b039ecaSBarry Smith .  ctx - the monitor context
4274d763cef2SBarry Smith 
4275d763cef2SBarry Smith    Level: intermediate
4276d763cef2SBarry Smith 
42774f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4278d763cef2SBarry Smith @*/
4279a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4280d763cef2SBarry Smith {
4281dfbe8321SBarry Smith   PetscErrorCode ierr;
4282d763cef2SBarry Smith 
4283d763cef2SBarry Smith   PetscFunctionBegin;
42847684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
42857684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
42867684fa3eSBarry Smith   }
42870b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
428831152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4289387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4290387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4291387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
42920b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4293d763cef2SBarry Smith   PetscFunctionReturn(0);
4294d763cef2SBarry Smith }
4295d763cef2SBarry Smith 
42961b575b74SJoseph Pusztay /*
42971b575b74SJoseph Pusztay 
42981b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
42991b575b74SJoseph Pusztay 
43001b575b74SJoseph Pusztay */
43011b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43021b575b74SJoseph Pusztay {
43031b575b74SJoseph Pusztay   PetscDraw      draw;
43041b575b74SJoseph Pusztay   PetscErrorCode ierr;
43051b575b74SJoseph Pusztay 
43061b575b74SJoseph Pusztay   PetscFunctionBegin;
43071b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43081b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43091b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43101b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43111b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43121b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43131b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43141b575b74SJoseph Pusztay 
43151b575b74SJoseph Pusztay }
43161b575b74SJoseph Pusztay 
43171b575b74SJoseph Pusztay /*
43181b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
43191b575b74SJoseph Pusztay */
43201b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
43211b575b74SJoseph Pusztay {
43221b575b74SJoseph Pusztay   PetscErrorCode ierr;
43231b575b74SJoseph Pusztay 
43241b575b74SJoseph Pusztay   PetscFunctionBegin;
43251b575b74SJoseph Pusztay 
43261b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
43271b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
43281b575b74SJoseph Pusztay 
43291b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43301b575b74SJoseph Pusztay 
43311b575b74SJoseph Pusztay }
43321b575b74SJoseph Pusztay 
4333d763cef2SBarry Smith /*@
4334b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4335d763cef2SBarry Smith 
4336d763cef2SBarry Smith    Not Collective
4337d763cef2SBarry Smith 
4338d763cef2SBarry Smith    Input Parameter:
4339d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4340d763cef2SBarry Smith 
4341d763cef2SBarry Smith    Output Parameter:
434219eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4343d763cef2SBarry Smith 
4344d763cef2SBarry Smith    Level: beginner
4345d763cef2SBarry Smith 
4346b8123daeSJed Brown    Note:
4347b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
43489be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4349b8123daeSJed Brown 
43508f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4351d763cef2SBarry Smith 
4352d763cef2SBarry Smith @*/
43537087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4354d763cef2SBarry Smith {
4355d763cef2SBarry Smith   PetscFunctionBegin;
43560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4357f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4358d763cef2SBarry Smith   *t = ts->ptime;
4359d763cef2SBarry Smith   PetscFunctionReturn(0);
4360d763cef2SBarry Smith }
4361d763cef2SBarry Smith 
4362e5e524a1SHong Zhang /*@
4363e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4364e5e524a1SHong Zhang 
4365e5e524a1SHong Zhang    Not Collective
4366e5e524a1SHong Zhang 
4367e5e524a1SHong Zhang    Input Parameter:
4368e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4369e5e524a1SHong Zhang 
4370e5e524a1SHong Zhang    Output Parameter:
4371e5e524a1SHong Zhang .  t  - the previous time
4372e5e524a1SHong Zhang 
4373e5e524a1SHong Zhang    Level: beginner
4374e5e524a1SHong Zhang 
4375aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4376e5e524a1SHong Zhang 
4377e5e524a1SHong Zhang @*/
4378e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4379e5e524a1SHong Zhang {
4380e5e524a1SHong Zhang   PetscFunctionBegin;
4381e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4382e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4383e5e524a1SHong Zhang   *t = ts->ptime_prev;
4384e5e524a1SHong Zhang   PetscFunctionReturn(0);
4385e5e524a1SHong Zhang }
4386e5e524a1SHong Zhang 
43876a4d4014SLisandro Dalcin /*@
43886a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
43896a4d4014SLisandro Dalcin 
43903f9fe445SBarry Smith    Logically Collective on TS
43916a4d4014SLisandro Dalcin 
43926a4d4014SLisandro Dalcin    Input Parameters:
43936a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
43946a4d4014SLisandro Dalcin -  time - the time
43956a4d4014SLisandro Dalcin 
43966a4d4014SLisandro Dalcin    Level: intermediate
43976a4d4014SLisandro Dalcin 
439819eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
43996a4d4014SLisandro Dalcin 
44006a4d4014SLisandro Dalcin @*/
44017087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44026a4d4014SLisandro Dalcin {
44036a4d4014SLisandro Dalcin   PetscFunctionBegin;
44040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4405c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44066a4d4014SLisandro Dalcin   ts->ptime = t;
44076a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44086a4d4014SLisandro Dalcin }
44096a4d4014SLisandro Dalcin 
4410d763cef2SBarry Smith /*@C
4411d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4412d763cef2SBarry Smith    TS options in the database.
4413d763cef2SBarry Smith 
44143f9fe445SBarry Smith    Logically Collective on TS
4415d763cef2SBarry Smith 
4416d763cef2SBarry Smith    Input Parameter:
4417d763cef2SBarry Smith +  ts     - The TS context
4418d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4419d763cef2SBarry Smith 
4420d763cef2SBarry Smith    Notes:
4421d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4422d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4423d763cef2SBarry Smith    hyphen.
4424d763cef2SBarry Smith 
4425d763cef2SBarry Smith    Level: advanced
4426d763cef2SBarry Smith 
4427d763cef2SBarry Smith .seealso: TSSetFromOptions()
4428d763cef2SBarry Smith 
4429d763cef2SBarry Smith @*/
44307087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4431d763cef2SBarry Smith {
4432dfbe8321SBarry Smith   PetscErrorCode ierr;
4433089b2837SJed Brown   SNES           snes;
4434d763cef2SBarry Smith 
4435d763cef2SBarry Smith   PetscFunctionBegin;
44360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4437d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4438089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4439089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4440d763cef2SBarry Smith   PetscFunctionReturn(0);
4441d763cef2SBarry Smith }
4442d763cef2SBarry Smith 
4443d763cef2SBarry Smith /*@C
4444d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4445d763cef2SBarry Smith    TS options in the database.
4446d763cef2SBarry Smith 
44473f9fe445SBarry Smith    Logically Collective on TS
4448d763cef2SBarry Smith 
4449d763cef2SBarry Smith    Input Parameter:
4450d763cef2SBarry Smith +  ts     - The TS context
4451d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4452d763cef2SBarry Smith 
4453d763cef2SBarry Smith    Notes:
4454d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4455d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4456d763cef2SBarry Smith    hyphen.
4457d763cef2SBarry Smith 
4458d763cef2SBarry Smith    Level: advanced
4459d763cef2SBarry Smith 
4460d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4461d763cef2SBarry Smith 
4462d763cef2SBarry Smith @*/
44637087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4464d763cef2SBarry Smith {
4465dfbe8321SBarry Smith   PetscErrorCode ierr;
4466089b2837SJed Brown   SNES           snes;
4467d763cef2SBarry Smith 
4468d763cef2SBarry Smith   PetscFunctionBegin;
44690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4470d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4471089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4472089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4473d763cef2SBarry Smith   PetscFunctionReturn(0);
4474d763cef2SBarry Smith }
4475d763cef2SBarry Smith 
4476d763cef2SBarry Smith /*@C
4477d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4478d763cef2SBarry Smith    TS options in the database.
4479d763cef2SBarry Smith 
4480d763cef2SBarry Smith    Not Collective
4481d763cef2SBarry Smith 
4482d763cef2SBarry Smith    Input Parameter:
4483d763cef2SBarry Smith .  ts - The TS context
4484d763cef2SBarry Smith 
4485d763cef2SBarry Smith    Output Parameter:
4486d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4487d763cef2SBarry Smith 
448895452b02SPatrick Sanan    Notes:
448995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4490d763cef2SBarry Smith    sufficient length to hold the prefix.
4491d763cef2SBarry Smith 
4492d763cef2SBarry Smith    Level: intermediate
4493d763cef2SBarry Smith 
4494d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4495d763cef2SBarry Smith @*/
44967087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4497d763cef2SBarry Smith {
4498dfbe8321SBarry Smith   PetscErrorCode ierr;
4499d763cef2SBarry Smith 
4500d763cef2SBarry Smith   PetscFunctionBegin;
45010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45024482741eSBarry Smith   PetscValidPointer(prefix,2);
4503d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4504d763cef2SBarry Smith   PetscFunctionReturn(0);
4505d763cef2SBarry Smith }
4506d763cef2SBarry Smith 
4507d763cef2SBarry Smith /*@C
4508d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4509d763cef2SBarry Smith 
4510d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4511d763cef2SBarry Smith 
4512d763cef2SBarry Smith    Input Parameter:
4513d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4514d763cef2SBarry Smith 
4515d763cef2SBarry Smith    Output Parameters:
4516e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4517e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4518e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4519e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4520d763cef2SBarry Smith 
452195452b02SPatrick Sanan    Notes:
452295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4523d763cef2SBarry Smith 
4524d763cef2SBarry Smith    Level: intermediate
4525d763cef2SBarry Smith 
452680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4527d763cef2SBarry Smith 
4528d763cef2SBarry Smith @*/
4529e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4530d763cef2SBarry Smith {
4531089b2837SJed Brown   PetscErrorCode ierr;
453224989b8cSPeter Brune   DM             dm;
4533089b2837SJed Brown 
4534d763cef2SBarry Smith   PetscFunctionBegin;
453523a57915SBarry Smith   if (Amat || Pmat) {
453623a57915SBarry Smith     SNES snes;
4537089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
453823a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4539e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
454023a57915SBarry Smith   }
454124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
454224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4543d763cef2SBarry Smith   PetscFunctionReturn(0);
4544d763cef2SBarry Smith }
4545d763cef2SBarry Smith 
45462eca1d9cSJed Brown /*@C
45472eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
45482eca1d9cSJed Brown 
45492eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
45502eca1d9cSJed Brown 
45512eca1d9cSJed Brown    Input Parameter:
45522eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
45532eca1d9cSJed Brown 
45542eca1d9cSJed Brown    Output Parameters:
4555e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4556e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
45572eca1d9cSJed Brown .  f   - The function to compute the matrices
45582eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
45592eca1d9cSJed Brown 
456095452b02SPatrick Sanan    Notes:
456195452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
45622eca1d9cSJed Brown 
45632eca1d9cSJed Brown    Level: advanced
45642eca1d9cSJed Brown 
456580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
45662eca1d9cSJed Brown 
45672eca1d9cSJed Brown @*/
4568e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
45692eca1d9cSJed Brown {
4570089b2837SJed Brown   PetscErrorCode ierr;
457124989b8cSPeter Brune   DM             dm;
45720910c330SBarry Smith 
45732eca1d9cSJed Brown   PetscFunctionBegin;
4574c0aab802Sstefano_zampini   if (Amat || Pmat) {
4575c0aab802Sstefano_zampini     SNES snes;
4576089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4577f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4578e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4579c0aab802Sstefano_zampini   }
458024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
458124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
45822eca1d9cSJed Brown   PetscFunctionReturn(0);
45832eca1d9cSJed Brown }
45842eca1d9cSJed Brown 
45851713a123SBarry Smith /*@C
458683a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
45871713a123SBarry Smith    VecView() for the solution at each timestep
45881713a123SBarry Smith 
45891713a123SBarry Smith    Collective on TS
45901713a123SBarry Smith 
45911713a123SBarry Smith    Input Parameters:
45921713a123SBarry Smith +  ts - the TS context
45931713a123SBarry Smith .  step - current time-step
4594142b95e3SSatish Balay .  ptime - current time
45950298fd71SBarry Smith -  dummy - either a viewer or NULL
45961713a123SBarry Smith 
459799fdda47SBarry Smith    Options Database:
459899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
459999fdda47SBarry Smith 
460095452b02SPatrick Sanan    Notes:
460195452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
460299fdda47SBarry Smith        will look bad
460399fdda47SBarry Smith 
46041713a123SBarry Smith    Level: intermediate
46051713a123SBarry Smith 
4606a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46071713a123SBarry Smith @*/
46080910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46091713a123SBarry Smith {
4610dfbe8321SBarry Smith   PetscErrorCode   ierr;
461183a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4612473a3ab2SBarry Smith   PetscDraw        draw;
46131713a123SBarry Smith 
46141713a123SBarry Smith   PetscFunctionBegin;
46156083293cSBarry Smith   if (!step && ictx->showinitial) {
46166083293cSBarry Smith     if (!ictx->initialsolution) {
46170910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46181713a123SBarry Smith     }
46190910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
46206083293cSBarry Smith   }
4621b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46220dcf80beSBarry Smith 
46236083293cSBarry Smith   if (ictx->showinitial) {
46246083293cSBarry Smith     PetscReal pause;
46256083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
46266083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
46276083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
46286083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
46296083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
46306083293cSBarry Smith   }
46310910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4632473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
463351fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4634473a3ab2SBarry Smith     char      time[32];
4635473a3ab2SBarry Smith 
4636473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
463785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4638473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4639473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
464051fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4641473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4642473a3ab2SBarry Smith   }
4643473a3ab2SBarry Smith 
46446083293cSBarry Smith   if (ictx->showinitial) {
46456083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
46466083293cSBarry Smith   }
46471713a123SBarry Smith   PetscFunctionReturn(0);
46481713a123SBarry Smith }
46491713a123SBarry Smith 
46509110b2e7SHong Zhang /*@C
46512d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
46522d5ee99bSBarry Smith 
46532d5ee99bSBarry Smith    Collective on TS
46542d5ee99bSBarry Smith 
46552d5ee99bSBarry Smith    Input Parameters:
46562d5ee99bSBarry Smith +  ts - the TS context
46572d5ee99bSBarry Smith .  step - current time-step
46582d5ee99bSBarry Smith .  ptime - current time
46592d5ee99bSBarry Smith -  dummy - either a viewer or NULL
46602d5ee99bSBarry Smith 
46612d5ee99bSBarry Smith    Level: intermediate
46622d5ee99bSBarry Smith 
46632d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46642d5ee99bSBarry Smith @*/
46652d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46662d5ee99bSBarry Smith {
46672d5ee99bSBarry Smith   PetscErrorCode    ierr;
46682d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
46692d5ee99bSBarry Smith   PetscDraw         draw;
46706934998bSLisandro Dalcin   PetscDrawAxis     axis;
46712d5ee99bSBarry Smith   PetscInt          n;
46722d5ee99bSBarry Smith   PetscMPIInt       size;
46736934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
46742d5ee99bSBarry Smith   char              time[32];
46752d5ee99bSBarry Smith   const PetscScalar *U;
46762d5ee99bSBarry Smith 
46772d5ee99bSBarry Smith   PetscFunctionBegin;
46786934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
46796934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
46802d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
46816934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
46822d5ee99bSBarry Smith 
46832d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
46846934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
46856934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
46866934998bSLisandro Dalcin   if (!step) {
46876934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
46886934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
46896934998bSLisandro Dalcin   }
46902d5ee99bSBarry Smith 
46912d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
46926934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
46936934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4694a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
46956934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
46962d5ee99bSBarry Smith 
46976934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
46986934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
46992d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47004b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
470185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47022d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
470351fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47042d5ee99bSBarry Smith   }
47056934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47062d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
470756d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47086934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47092d5ee99bSBarry Smith   PetscFunctionReturn(0);
47102d5ee99bSBarry Smith }
47112d5ee99bSBarry Smith 
47126083293cSBarry Smith /*@C
471383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
47146083293cSBarry Smith 
47156083293cSBarry Smith    Collective on TS
47166083293cSBarry Smith 
47176083293cSBarry Smith    Input Parameters:
47186083293cSBarry Smith .    ctx - the monitor context
47196083293cSBarry Smith 
47206083293cSBarry Smith    Level: intermediate
47216083293cSBarry Smith 
472283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
47236083293cSBarry Smith @*/
472483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
47256083293cSBarry Smith {
47266083293cSBarry Smith   PetscErrorCode ierr;
47276083293cSBarry Smith 
47286083293cSBarry Smith   PetscFunctionBegin;
472983a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
473083a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
473183a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
47326083293cSBarry Smith   PetscFunctionReturn(0);
47336083293cSBarry Smith }
47346083293cSBarry Smith 
47356083293cSBarry Smith /*@C
473683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
47376083293cSBarry Smith 
47386083293cSBarry Smith    Collective on TS
47396083293cSBarry Smith 
47406083293cSBarry Smith    Input Parameter:
47416083293cSBarry Smith .    ts - time-step context
47426083293cSBarry Smith 
47436083293cSBarry Smith    Output Patameter:
47446083293cSBarry Smith .    ctx - the monitor context
47456083293cSBarry Smith 
474699fdda47SBarry Smith    Options Database:
474799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
474899fdda47SBarry Smith 
47496083293cSBarry Smith    Level: intermediate
47506083293cSBarry Smith 
475183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
47526083293cSBarry Smith @*/
475383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
47546083293cSBarry Smith {
47556083293cSBarry Smith   PetscErrorCode   ierr;
47566083293cSBarry Smith 
47576083293cSBarry Smith   PetscFunctionBegin;
4758b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
475983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4760e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4761bbd56ea5SKarl Rupp 
476283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4763473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4764c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4765473a3ab2SBarry Smith 
4766473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4767c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
47686083293cSBarry Smith   PetscFunctionReturn(0);
47696083293cSBarry Smith }
47706083293cSBarry Smith 
47713a471f94SBarry Smith /*@C
47720ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
47730ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
47740ed3bfb6SBarry Smith 
47750ed3bfb6SBarry Smith    Collective on TS
47760ed3bfb6SBarry Smith 
47770ed3bfb6SBarry Smith    Input Parameters:
47780ed3bfb6SBarry Smith +  ts - the TS context
47790ed3bfb6SBarry Smith .  step - current time-step
47800ed3bfb6SBarry Smith .  ptime - current time
47810ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
47820ed3bfb6SBarry Smith 
47830ed3bfb6SBarry Smith    Options Database:
47840ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
47850ed3bfb6SBarry Smith 
47860ed3bfb6SBarry Smith    Level: intermediate
47870ed3bfb6SBarry Smith 
47880ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
47890ed3bfb6SBarry Smith @*/
47900ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47910ed3bfb6SBarry Smith {
47920ed3bfb6SBarry Smith   PetscErrorCode   ierr;
47930ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
47940ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
47950ed3bfb6SBarry Smith   Vec              work;
47960ed3bfb6SBarry Smith 
47970ed3bfb6SBarry Smith   PetscFunctionBegin;
47980ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47990ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48000ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48010ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48020ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48030ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48040ed3bfb6SBarry Smith }
48050ed3bfb6SBarry Smith 
48060ed3bfb6SBarry Smith /*@C
480783a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48083a471f94SBarry Smith    VecView() for the error at each timestep
48093a471f94SBarry Smith 
48103a471f94SBarry Smith    Collective on TS
48113a471f94SBarry Smith 
48123a471f94SBarry Smith    Input Parameters:
48133a471f94SBarry Smith +  ts - the TS context
48143a471f94SBarry Smith .  step - current time-step
48153a471f94SBarry Smith .  ptime - current time
48160298fd71SBarry Smith -  dummy - either a viewer or NULL
48173a471f94SBarry Smith 
48180ed3bfb6SBarry Smith    Options Database:
48190ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48200ed3bfb6SBarry Smith 
48213a471f94SBarry Smith    Level: intermediate
48223a471f94SBarry Smith 
48230ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48243a471f94SBarry Smith @*/
48250910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48263a471f94SBarry Smith {
48273a471f94SBarry Smith   PetscErrorCode   ierr;
482883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
482983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
48303a471f94SBarry Smith   Vec              work;
48313a471f94SBarry Smith 
48323a471f94SBarry Smith   PetscFunctionBegin;
4833b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48340910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48353a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48360910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
48373a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48383a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48393a471f94SBarry Smith   PetscFunctionReturn(0);
48403a471f94SBarry Smith }
48413a471f94SBarry Smith 
4842af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
48436c699258SBarry Smith /*@
48442a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
48456c699258SBarry Smith 
4846d083f849SBarry Smith    Logically Collective on ts
48476c699258SBarry Smith 
48486c699258SBarry Smith    Input Parameters:
48492a808120SBarry Smith +  ts - the ODE integrator object
48502a808120SBarry Smith -  dm - the dm, cannot be NULL
48516c699258SBarry Smith 
4852e03a659cSJed Brown    Notes:
4853e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4854e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4855e03a659cSJed Brown    different problems using the same function space.
4856e03a659cSJed Brown 
48576c699258SBarry Smith    Level: intermediate
48586c699258SBarry Smith 
48596c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
48606c699258SBarry Smith @*/
48617087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
48626c699258SBarry Smith {
48636c699258SBarry Smith   PetscErrorCode ierr;
4864089b2837SJed Brown   SNES           snes;
4865942e3340SBarry Smith   DMTS           tsdm;
48666c699258SBarry Smith 
48676c699258SBarry Smith   PetscFunctionBegin;
48680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48692a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
487070663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4871942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
48722a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4873942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4874942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
487524989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
487624989b8cSPeter Brune         tsdm->originaldm = dm;
487724989b8cSPeter Brune       }
487824989b8cSPeter Brune     }
48796bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
488024989b8cSPeter Brune   }
48816c699258SBarry Smith   ts->dm = dm;
4882bbd56ea5SKarl Rupp 
4883089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4884089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
48856c699258SBarry Smith   PetscFunctionReturn(0);
48866c699258SBarry Smith }
48876c699258SBarry Smith 
48886c699258SBarry Smith /*@
48896c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
48906c699258SBarry Smith 
48913f9fe445SBarry Smith    Not Collective
48926c699258SBarry Smith 
48936c699258SBarry Smith    Input Parameter:
48946c699258SBarry Smith . ts - the preconditioner context
48956c699258SBarry Smith 
48966c699258SBarry Smith    Output Parameter:
48976c699258SBarry Smith .  dm - the dm
48986c699258SBarry Smith 
48996c699258SBarry Smith    Level: intermediate
49006c699258SBarry Smith 
49016c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49026c699258SBarry Smith @*/
49037087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49046c699258SBarry Smith {
4905496e6a7aSJed Brown   PetscErrorCode ierr;
4906496e6a7aSJed Brown 
49076c699258SBarry Smith   PetscFunctionBegin;
49080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4909496e6a7aSJed Brown   if (!ts->dm) {
4910ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4911496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4912496e6a7aSJed Brown   }
49136c699258SBarry Smith   *dm = ts->dm;
49146c699258SBarry Smith   PetscFunctionReturn(0);
49156c699258SBarry Smith }
49161713a123SBarry Smith 
49170f5c6efeSJed Brown /*@
49180f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49190f5c6efeSJed Brown 
49203f9fe445SBarry Smith    Logically Collective on SNES
49210f5c6efeSJed Brown 
49220f5c6efeSJed Brown    Input Parameter:
4923d42a1c89SJed Brown + snes - nonlinear solver
49240910c330SBarry Smith . U - the current state at which to evaluate the residual
4925d42a1c89SJed Brown - ctx - user context, must be a TS
49260f5c6efeSJed Brown 
49270f5c6efeSJed Brown    Output Parameter:
49280f5c6efeSJed Brown . F - the nonlinear residual
49290f5c6efeSJed Brown 
49300f5c6efeSJed Brown    Notes:
49310f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49320f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
49330f5c6efeSJed Brown 
49340f5c6efeSJed Brown    Level: advanced
49350f5c6efeSJed Brown 
49360f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
49370f5c6efeSJed Brown @*/
49380910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
49390f5c6efeSJed Brown {
49400f5c6efeSJed Brown   TS             ts = (TS)ctx;
49410f5c6efeSJed Brown   PetscErrorCode ierr;
49420f5c6efeSJed Brown 
49430f5c6efeSJed Brown   PetscFunctionBegin;
49440f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
49450910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
49460f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
49470f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
49480910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
49490f5c6efeSJed Brown   PetscFunctionReturn(0);
49500f5c6efeSJed Brown }
49510f5c6efeSJed Brown 
49520f5c6efeSJed Brown /*@
49530f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
49540f5c6efeSJed Brown 
49550f5c6efeSJed Brown    Collective on SNES
49560f5c6efeSJed Brown 
49570f5c6efeSJed Brown    Input Parameter:
49580f5c6efeSJed Brown + snes - nonlinear solver
49590910c330SBarry Smith . U - the current state at which to evaluate the residual
49600f5c6efeSJed Brown - ctx - user context, must be a TS
49610f5c6efeSJed Brown 
49620f5c6efeSJed Brown    Output Parameter:
49630f5c6efeSJed Brown + A - the Jacobian
49640f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
49650f5c6efeSJed Brown - flag - indicates any structure change in the matrix
49660f5c6efeSJed Brown 
49670f5c6efeSJed Brown    Notes:
49680f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49690f5c6efeSJed Brown 
49700f5c6efeSJed Brown    Level: developer
49710f5c6efeSJed Brown 
49720f5c6efeSJed Brown .seealso: SNESSetJacobian()
49730f5c6efeSJed Brown @*/
4974d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
49750f5c6efeSJed Brown {
49760f5c6efeSJed Brown   TS             ts = (TS)ctx;
49770f5c6efeSJed Brown   PetscErrorCode ierr;
49780f5c6efeSJed Brown 
49790f5c6efeSJed Brown   PetscFunctionBegin;
49800f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
49810910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
49820f5c6efeSJed Brown   PetscValidPointer(A,3);
498394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
49840f5c6efeSJed Brown   PetscValidPointer(B,4);
498594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
49860f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4987d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
49880f5c6efeSJed Brown   PetscFunctionReturn(0);
49890f5c6efeSJed Brown }
4990325fc9f4SBarry Smith 
49910e4ef248SJed Brown /*@C
49929ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
49930e4ef248SJed Brown 
49940e4ef248SJed Brown    Collective on TS
49950e4ef248SJed Brown 
49960e4ef248SJed Brown    Input Arguments:
49970e4ef248SJed Brown +  ts - time stepping context
49980e4ef248SJed Brown .  t - time at which to evaluate
49990910c330SBarry Smith .  U - state at which to evaluate
50000e4ef248SJed Brown -  ctx - context
50010e4ef248SJed Brown 
50020e4ef248SJed Brown    Output Arguments:
50030e4ef248SJed Brown .  F - right hand side
50040e4ef248SJed Brown 
50050e4ef248SJed Brown    Level: intermediate
50060e4ef248SJed Brown 
50070e4ef248SJed Brown    Notes:
50080e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50090e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50100e4ef248SJed Brown 
50110e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50120e4ef248SJed Brown @*/
50130910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50140e4ef248SJed Brown {
50150e4ef248SJed Brown   PetscErrorCode ierr;
50160e4ef248SJed Brown   Mat            Arhs,Brhs;
50170e4ef248SJed Brown 
50180e4ef248SJed Brown   PetscFunctionBegin;
50190e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5020d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
50210910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
50220e4ef248SJed Brown   PetscFunctionReturn(0);
50230e4ef248SJed Brown }
50240e4ef248SJed Brown 
50250e4ef248SJed Brown /*@C
50260e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
50270e4ef248SJed Brown 
50280e4ef248SJed Brown    Collective on TS
50290e4ef248SJed Brown 
50300e4ef248SJed Brown    Input Arguments:
50310e4ef248SJed Brown +  ts - time stepping context
50320e4ef248SJed Brown .  t - time at which to evaluate
50330910c330SBarry Smith .  U - state at which to evaluate
50340e4ef248SJed Brown -  ctx - context
50350e4ef248SJed Brown 
50360e4ef248SJed Brown    Output Arguments:
50370e4ef248SJed Brown +  A - pointer to operator
50380e4ef248SJed Brown .  B - pointer to preconditioning matrix
50390e4ef248SJed Brown -  flg - matrix structure flag
50400e4ef248SJed Brown 
50410e4ef248SJed Brown    Level: intermediate
50420e4ef248SJed Brown 
50430e4ef248SJed Brown    Notes:
50440e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
50450e4ef248SJed Brown 
50460e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
50470e4ef248SJed Brown @*/
5048d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
50490e4ef248SJed Brown {
50500e4ef248SJed Brown   PetscFunctionBegin;
50510e4ef248SJed Brown   PetscFunctionReturn(0);
50520e4ef248SJed Brown }
50530e4ef248SJed Brown 
50540026cea9SSean Farley /*@C
50550026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
50560026cea9SSean Farley 
50570026cea9SSean Farley    Collective on TS
50580026cea9SSean Farley 
50590026cea9SSean Farley    Input Arguments:
50600026cea9SSean Farley +  ts - time stepping context
50610026cea9SSean Farley .  t - time at which to evaluate
50620910c330SBarry Smith .  U - state at which to evaluate
50630910c330SBarry Smith .  Udot - time derivative of state vector
50640026cea9SSean Farley -  ctx - context
50650026cea9SSean Farley 
50660026cea9SSean Farley    Output Arguments:
50670026cea9SSean Farley .  F - left hand side
50680026cea9SSean Farley 
50690026cea9SSean Farley    Level: intermediate
50700026cea9SSean Farley 
50710026cea9SSean Farley    Notes:
50720910c330SBarry 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
50730026cea9SSean Farley    user is required to write their own TSComputeIFunction.
50740026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
50750026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
50760026cea9SSean Farley 
50779ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
50789ae8fd06SBarry Smith 
50799ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
50800026cea9SSean Farley @*/
50810910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
50820026cea9SSean Farley {
50830026cea9SSean Farley   PetscErrorCode ierr;
50840026cea9SSean Farley   Mat            A,B;
50850026cea9SSean Farley 
50860026cea9SSean Farley   PetscFunctionBegin;
50870298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5088d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
50890910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
50900026cea9SSean Farley   PetscFunctionReturn(0);
50910026cea9SSean Farley }
50920026cea9SSean Farley 
50930026cea9SSean Farley /*@C
5094b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
50950026cea9SSean Farley 
50960026cea9SSean Farley    Collective on TS
50970026cea9SSean Farley 
50980026cea9SSean Farley    Input Arguments:
50990026cea9SSean Farley +  ts - time stepping context
51000026cea9SSean Farley .  t - time at which to evaluate
51010910c330SBarry Smith .  U - state at which to evaluate
51020910c330SBarry Smith .  Udot - time derivative of state vector
51030026cea9SSean Farley .  shift - shift to apply
51040026cea9SSean Farley -  ctx - context
51050026cea9SSean Farley 
51060026cea9SSean Farley    Output Arguments:
51070026cea9SSean Farley +  A - pointer to operator
51080026cea9SSean Farley .  B - pointer to preconditioning matrix
51090026cea9SSean Farley -  flg - matrix structure flag
51100026cea9SSean Farley 
5111b41af12eSJed Brown    Level: advanced
51120026cea9SSean Farley 
51130026cea9SSean Farley    Notes:
51140026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51150026cea9SSean Farley 
5116b41af12eSJed Brown    It is only appropriate for problems of the form
5117b41af12eSJed Brown 
5118b41af12eSJed Brown $     M Udot = F(U,t)
5119b41af12eSJed Brown 
5120b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5121b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5122b41af12eSJed Brown   an implicit operator of the form
5123b41af12eSJed Brown 
5124b41af12eSJed Brown $    shift*M + J
5125b41af12eSJed Brown 
5126b41af12eSJed 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
5127b41af12eSJed Brown   a copy of M or reassemble it when requested.
5128b41af12eSJed Brown 
51290026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
51300026cea9SSean Farley @*/
5131d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
51320026cea9SSean Farley {
5133b41af12eSJed Brown   PetscErrorCode ierr;
5134b41af12eSJed Brown 
51350026cea9SSean Farley   PetscFunctionBegin;
513694ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5137b41af12eSJed Brown   ts->ijacobian.shift = shift;
51380026cea9SSean Farley   PetscFunctionReturn(0);
51390026cea9SSean Farley }
5140b41af12eSJed Brown 
5141e817cc15SEmil Constantinescu /*@
5142e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5143e817cc15SEmil Constantinescu 
5144e817cc15SEmil Constantinescu    Not Collective
5145e817cc15SEmil Constantinescu 
5146e817cc15SEmil Constantinescu    Input Parameter:
5147e817cc15SEmil Constantinescu .  ts - the TS context
5148e817cc15SEmil Constantinescu 
5149e817cc15SEmil Constantinescu    Output Parameter:
51504e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5151e817cc15SEmil Constantinescu 
5152e817cc15SEmil Constantinescu    Level: beginner
5153e817cc15SEmil Constantinescu 
5154e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5155e817cc15SEmil Constantinescu @*/
5156e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5157e817cc15SEmil Constantinescu {
5158e817cc15SEmil Constantinescu   PetscFunctionBegin;
5159e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5160e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5161e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5162e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5163e817cc15SEmil Constantinescu }
5164e817cc15SEmil Constantinescu 
5165e817cc15SEmil Constantinescu /*@
5166e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5167e817cc15SEmil Constantinescu 
5168e817cc15SEmil Constantinescu    Not Collective
5169e817cc15SEmil Constantinescu 
5170e817cc15SEmil Constantinescu    Input Parameter:
5171e817cc15SEmil Constantinescu +  ts - the TS context
51721297b224SEmil Constantinescu -  equation_type - see TSEquationType
5173e817cc15SEmil Constantinescu 
5174e817cc15SEmil Constantinescu    Level: advanced
5175e817cc15SEmil Constantinescu 
5176e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5177e817cc15SEmil Constantinescu @*/
5178e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5179e817cc15SEmil Constantinescu {
5180e817cc15SEmil Constantinescu   PetscFunctionBegin;
5181e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5182e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5183e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5184e817cc15SEmil Constantinescu }
51850026cea9SSean Farley 
51864af1b03aSJed Brown /*@
51874af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
51884af1b03aSJed Brown 
51894af1b03aSJed Brown    Not Collective
51904af1b03aSJed Brown 
51914af1b03aSJed Brown    Input Parameter:
51924af1b03aSJed Brown .  ts - the TS context
51934af1b03aSJed Brown 
51944af1b03aSJed Brown    Output Parameter:
51954af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
51964af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
51974af1b03aSJed Brown 
5198487e0bb9SJed Brown    Level: beginner
51994af1b03aSJed Brown 
5200cd652676SJed Brown    Notes:
5201cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52024af1b03aSJed Brown 
52034af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52044af1b03aSJed Brown @*/
52054af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52064af1b03aSJed Brown {
52074af1b03aSJed Brown   PetscFunctionBegin;
52084af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52094af1b03aSJed Brown   PetscValidPointer(reason,2);
52104af1b03aSJed Brown   *reason = ts->reason;
52114af1b03aSJed Brown   PetscFunctionReturn(0);
52124af1b03aSJed Brown }
52134af1b03aSJed Brown 
5214d6ad946cSShri Abhyankar /*@
5215d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5216d6ad946cSShri Abhyankar 
52176b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5218d6ad946cSShri Abhyankar 
52196b221cbeSPatrick Sanan    Input Parameters:
5220d6ad946cSShri Abhyankar +  ts - the TS context
52216b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5222d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5223d6ad946cSShri Abhyankar 
5224f5abba47SShri Abhyankar    Level: advanced
5225d6ad946cSShri Abhyankar 
5226d6ad946cSShri Abhyankar    Notes:
52276b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5228d6ad946cSShri Abhyankar 
5229d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5230d6ad946cSShri Abhyankar @*/
5231d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5232d6ad946cSShri Abhyankar {
5233d6ad946cSShri Abhyankar   PetscFunctionBegin;
5234d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5235d6ad946cSShri Abhyankar   ts->reason = reason;
5236d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5237d6ad946cSShri Abhyankar }
5238d6ad946cSShri Abhyankar 
5239cc708dedSBarry Smith /*@
5240cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5241cc708dedSBarry Smith 
5242cc708dedSBarry Smith    Not Collective
5243cc708dedSBarry Smith 
5244cc708dedSBarry Smith    Input Parameter:
5245cc708dedSBarry Smith .  ts - the TS context
5246cc708dedSBarry Smith 
5247cc708dedSBarry Smith    Output Parameter:
524819eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5249cc708dedSBarry Smith 
5250487e0bb9SJed Brown    Level: beginner
5251cc708dedSBarry Smith 
5252cc708dedSBarry Smith    Notes:
5253cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5254cc708dedSBarry Smith 
5255cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5256cc708dedSBarry Smith @*/
5257cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5258cc708dedSBarry Smith {
5259cc708dedSBarry Smith   PetscFunctionBegin;
5260cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5261cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5262cc708dedSBarry Smith   *ftime = ts->solvetime;
5263cc708dedSBarry Smith   PetscFunctionReturn(0);
5264cc708dedSBarry Smith }
5265cc708dedSBarry Smith 
52662c18e0fdSBarry Smith /*@
52675ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
52689f67acb7SJed Brown    used by the time integrator.
52699f67acb7SJed Brown 
52709f67acb7SJed Brown    Not Collective
52719f67acb7SJed Brown 
52729f67acb7SJed Brown    Input Parameter:
52739f67acb7SJed Brown .  ts - TS context
52749f67acb7SJed Brown 
52759f67acb7SJed Brown    Output Parameter:
52769f67acb7SJed Brown .  nits - number of nonlinear iterations
52779f67acb7SJed Brown 
52789f67acb7SJed Brown    Notes:
52799f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
52809f67acb7SJed Brown 
52819f67acb7SJed Brown    Level: intermediate
52829f67acb7SJed Brown 
52835ef26d82SJed Brown .seealso:  TSGetKSPIterations()
52849f67acb7SJed Brown @*/
52855ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
52869f67acb7SJed Brown {
52879f67acb7SJed Brown   PetscFunctionBegin;
52889f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52899f67acb7SJed Brown   PetscValidIntPointer(nits,2);
52905ef26d82SJed Brown   *nits = ts->snes_its;
52919f67acb7SJed Brown   PetscFunctionReturn(0);
52929f67acb7SJed Brown }
52939f67acb7SJed Brown 
52949f67acb7SJed Brown /*@
52955ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
52969f67acb7SJed Brown    used by the time integrator.
52979f67acb7SJed Brown 
52989f67acb7SJed Brown    Not Collective
52999f67acb7SJed Brown 
53009f67acb7SJed Brown    Input Parameter:
53019f67acb7SJed Brown .  ts - TS context
53029f67acb7SJed Brown 
53039f67acb7SJed Brown    Output Parameter:
53049f67acb7SJed Brown .  lits - number of linear iterations
53059f67acb7SJed Brown 
53069f67acb7SJed Brown    Notes:
53079f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53089f67acb7SJed Brown 
53099f67acb7SJed Brown    Level: intermediate
53109f67acb7SJed Brown 
53115ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
53129f67acb7SJed Brown @*/
53135ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
53149f67acb7SJed Brown {
53159f67acb7SJed Brown   PetscFunctionBegin;
53169f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53179f67acb7SJed Brown   PetscValidIntPointer(lits,2);
53185ef26d82SJed Brown   *lits = ts->ksp_its;
53199f67acb7SJed Brown   PetscFunctionReturn(0);
53209f67acb7SJed Brown }
53219f67acb7SJed Brown 
5322cef5090cSJed Brown /*@
5323cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5324cef5090cSJed Brown 
5325cef5090cSJed Brown    Not Collective
5326cef5090cSJed Brown 
5327cef5090cSJed Brown    Input Parameter:
5328cef5090cSJed Brown .  ts - TS context
5329cef5090cSJed Brown 
5330cef5090cSJed Brown    Output Parameter:
5331cef5090cSJed Brown .  rejects - number of steps rejected
5332cef5090cSJed Brown 
5333cef5090cSJed Brown    Notes:
5334cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5335cef5090cSJed Brown 
5336cef5090cSJed Brown    Level: intermediate
5337cef5090cSJed Brown 
53385ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5339cef5090cSJed Brown @*/
5340cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5341cef5090cSJed Brown {
5342cef5090cSJed Brown   PetscFunctionBegin;
5343cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5344cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5345cef5090cSJed Brown   *rejects = ts->reject;
5346cef5090cSJed Brown   PetscFunctionReturn(0);
5347cef5090cSJed Brown }
5348cef5090cSJed Brown 
5349cef5090cSJed Brown /*@
5350cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5351cef5090cSJed Brown 
5352cef5090cSJed Brown    Not Collective
5353cef5090cSJed Brown 
5354cef5090cSJed Brown    Input Parameter:
5355cef5090cSJed Brown .  ts - TS context
5356cef5090cSJed Brown 
5357cef5090cSJed Brown    Output Parameter:
5358cef5090cSJed Brown .  fails - number of failed nonlinear solves
5359cef5090cSJed Brown 
5360cef5090cSJed Brown    Notes:
5361cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5362cef5090cSJed Brown 
5363cef5090cSJed Brown    Level: intermediate
5364cef5090cSJed Brown 
53655ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5366cef5090cSJed Brown @*/
5367cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5368cef5090cSJed Brown {
5369cef5090cSJed Brown   PetscFunctionBegin;
5370cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5371cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5372cef5090cSJed Brown   *fails = ts->num_snes_failures;
5373cef5090cSJed Brown   PetscFunctionReturn(0);
5374cef5090cSJed Brown }
5375cef5090cSJed Brown 
5376cef5090cSJed Brown /*@
5377cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5378cef5090cSJed Brown 
5379cef5090cSJed Brown    Not Collective
5380cef5090cSJed Brown 
5381cef5090cSJed Brown    Input Parameter:
5382cef5090cSJed Brown +  ts - TS context
5383cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5384cef5090cSJed Brown 
5385cef5090cSJed Brown    Notes:
5386cef5090cSJed Brown    The counter is reset to zero for each step
5387cef5090cSJed Brown 
5388cef5090cSJed Brown    Options Database Key:
5389cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5390cef5090cSJed Brown 
5391cef5090cSJed Brown    Level: intermediate
5392cef5090cSJed Brown 
53935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5394cef5090cSJed Brown @*/
5395cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5396cef5090cSJed Brown {
5397cef5090cSJed Brown   PetscFunctionBegin;
5398cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5399cef5090cSJed Brown   ts->max_reject = rejects;
5400cef5090cSJed Brown   PetscFunctionReturn(0);
5401cef5090cSJed Brown }
5402cef5090cSJed Brown 
5403cef5090cSJed Brown /*@
5404cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5405cef5090cSJed Brown 
5406cef5090cSJed Brown    Not Collective
5407cef5090cSJed Brown 
5408cef5090cSJed Brown    Input Parameter:
5409cef5090cSJed Brown +  ts - TS context
5410cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5411cef5090cSJed Brown 
5412cef5090cSJed Brown    Notes:
5413cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5414cef5090cSJed Brown 
5415cef5090cSJed Brown    Options Database Key:
5416cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5417cef5090cSJed Brown 
5418cef5090cSJed Brown    Level: intermediate
5419cef5090cSJed Brown 
54205ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5421cef5090cSJed Brown @*/
5422cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5423cef5090cSJed Brown {
5424cef5090cSJed Brown   PetscFunctionBegin;
5425cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5426cef5090cSJed Brown   ts->max_snes_failures = fails;
5427cef5090cSJed Brown   PetscFunctionReturn(0);
5428cef5090cSJed Brown }
5429cef5090cSJed Brown 
5430cef5090cSJed Brown /*@
5431cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5432cef5090cSJed Brown 
5433cef5090cSJed Brown    Not Collective
5434cef5090cSJed Brown 
5435cef5090cSJed Brown    Input Parameter:
5436cef5090cSJed Brown +  ts - TS context
5437cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5438cef5090cSJed Brown 
5439cef5090cSJed Brown    Options Database Key:
5440cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5441cef5090cSJed Brown 
5442cef5090cSJed Brown    Level: intermediate
5443cef5090cSJed Brown 
54445ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5445cef5090cSJed Brown @*/
5446cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5447cef5090cSJed Brown {
5448cef5090cSJed Brown   PetscFunctionBegin;
5449cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5450cef5090cSJed Brown   ts->errorifstepfailed = err;
5451cef5090cSJed Brown   PetscFunctionReturn(0);
5452cef5090cSJed Brown }
5453cef5090cSJed Brown 
5454fb1732b5SBarry Smith /*@C
5455fde5950dSBarry 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
5456fb1732b5SBarry Smith 
5457fb1732b5SBarry Smith    Collective on TS
5458fb1732b5SBarry Smith 
5459fb1732b5SBarry Smith    Input Parameters:
5460fb1732b5SBarry Smith +  ts - the TS context
5461fb1732b5SBarry Smith .  step - current time-step
5462fb1732b5SBarry Smith .  ptime - current time
54630910c330SBarry Smith .  u - current state
5464721cd6eeSBarry Smith -  vf - viewer and its format
5465fb1732b5SBarry Smith 
5466fb1732b5SBarry Smith    Level: intermediate
5467fb1732b5SBarry Smith 
5468fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5469fb1732b5SBarry Smith @*/
5470721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5471fb1732b5SBarry Smith {
5472fb1732b5SBarry Smith   PetscErrorCode ierr;
5473fb1732b5SBarry Smith 
5474fb1732b5SBarry Smith   PetscFunctionBegin;
5475721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5476721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5477721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5478ed81e22dSJed Brown   PetscFunctionReturn(0);
5479ed81e22dSJed Brown }
5480ed81e22dSJed Brown 
5481ed81e22dSJed Brown /*@C
5482ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5483ed81e22dSJed Brown 
5484ed81e22dSJed Brown    Collective on TS
5485ed81e22dSJed Brown 
5486ed81e22dSJed Brown    Input Parameters:
5487ed81e22dSJed Brown +  ts - the TS context
5488ed81e22dSJed Brown .  step - current time-step
5489ed81e22dSJed Brown .  ptime - current time
54900910c330SBarry Smith .  u - current state
5491ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5492ed81e22dSJed Brown 
5493ed81e22dSJed Brown    Level: intermediate
5494ed81e22dSJed Brown 
5495ed81e22dSJed Brown    Notes:
5496ed81e22dSJed 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.
5497ed81e22dSJed Brown    These are named according to the file name template.
5498ed81e22dSJed Brown 
5499ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5500ed81e22dSJed Brown 
5501ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5502ed81e22dSJed Brown @*/
55030910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5504ed81e22dSJed Brown {
5505ed81e22dSJed Brown   PetscErrorCode ierr;
5506ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5507ed81e22dSJed Brown   PetscViewer    viewer;
5508ed81e22dSJed Brown 
5509ed81e22dSJed Brown   PetscFunctionBegin;
551063e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55118caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5512ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
55130910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5514ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5515ed81e22dSJed Brown   PetscFunctionReturn(0);
5516ed81e22dSJed Brown }
5517ed81e22dSJed Brown 
5518ed81e22dSJed Brown /*@C
5519ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5520ed81e22dSJed Brown 
5521ed81e22dSJed Brown    Collective on TS
5522ed81e22dSJed Brown 
5523ed81e22dSJed Brown    Input Parameters:
5524ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5525ed81e22dSJed Brown 
5526ed81e22dSJed Brown    Level: intermediate
5527ed81e22dSJed Brown 
5528ed81e22dSJed Brown    Note:
5529ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5530ed81e22dSJed Brown 
5531ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5532ed81e22dSJed Brown @*/
5533ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5534ed81e22dSJed Brown {
5535ed81e22dSJed Brown   PetscErrorCode ierr;
5536ed81e22dSJed Brown 
5537ed81e22dSJed Brown   PetscFunctionBegin;
5538ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5539fb1732b5SBarry Smith   PetscFunctionReturn(0);
5540fb1732b5SBarry Smith }
5541fb1732b5SBarry Smith 
554284df9cb4SJed Brown /*@
5543552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
554484df9cb4SJed Brown 
5545ed81e22dSJed Brown    Collective on TS if controller has not been created yet
554684df9cb4SJed Brown 
554784df9cb4SJed Brown    Input Arguments:
5548ed81e22dSJed Brown .  ts - time stepping context
554984df9cb4SJed Brown 
555084df9cb4SJed Brown    Output Arguments:
5551ed81e22dSJed Brown .  adapt - adaptive controller
555284df9cb4SJed Brown 
555384df9cb4SJed Brown    Level: intermediate
555484df9cb4SJed Brown 
5555ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
555684df9cb4SJed Brown @*/
5557552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
555884df9cb4SJed Brown {
555984df9cb4SJed Brown   PetscErrorCode ierr;
556084df9cb4SJed Brown 
556184df9cb4SJed Brown   PetscFunctionBegin;
556284df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5563bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
556484df9cb4SJed Brown   if (!ts->adapt) {
5565ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
55663bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
55671c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
556884df9cb4SJed Brown   }
5569bec58848SLisandro Dalcin   *adapt = ts->adapt;
557084df9cb4SJed Brown   PetscFunctionReturn(0);
557184df9cb4SJed Brown }
5572d6ebe24aSShri Abhyankar 
55731c3436cfSJed Brown /*@
55741c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
55751c3436cfSJed Brown 
55761c3436cfSJed Brown    Logically Collective
55771c3436cfSJed Brown 
55781c3436cfSJed Brown    Input Arguments:
55791c3436cfSJed Brown +  ts - time integration context
55801c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
55810298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
55821c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
55830298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
55841c3436cfSJed Brown 
5585a3cdaa26SBarry Smith    Options Database keys:
5586a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5587a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5588a3cdaa26SBarry Smith 
55893ff766beSShri Abhyankar    Notes:
55903ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
55913ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
55923ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
55933ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
55943ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
55953ff766beSShri Abhyankar    differential variables.
55963ff766beSShri Abhyankar 
55971c3436cfSJed Brown    Level: beginner
55981c3436cfSJed Brown 
5599c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
56001c3436cfSJed Brown @*/
56011c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56021c3436cfSJed Brown {
56031c3436cfSJed Brown   PetscErrorCode ierr;
56041c3436cfSJed Brown 
56051c3436cfSJed Brown   PetscFunctionBegin;
5606c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56071c3436cfSJed Brown   if (vatol) {
56081c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56091c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56101c3436cfSJed Brown     ts->vatol = vatol;
56111c3436cfSJed Brown   }
5612c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
56131c3436cfSJed Brown   if (vrtol) {
56141c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
56151c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
56161c3436cfSJed Brown     ts->vrtol = vrtol;
56171c3436cfSJed Brown   }
56181c3436cfSJed Brown   PetscFunctionReturn(0);
56191c3436cfSJed Brown }
56201c3436cfSJed Brown 
5621c5033834SJed Brown /*@
5622c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5623c5033834SJed Brown 
5624c5033834SJed Brown    Logically Collective
5625c5033834SJed Brown 
5626c5033834SJed Brown    Input Arguments:
5627c5033834SJed Brown .  ts - time integration context
5628c5033834SJed Brown 
5629c5033834SJed Brown    Output Arguments:
56300298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
56310298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
56320298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
56330298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5634c5033834SJed Brown 
5635c5033834SJed Brown    Level: beginner
5636c5033834SJed Brown 
5637c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5638c5033834SJed Brown @*/
5639c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5640c5033834SJed Brown {
5641c5033834SJed Brown   PetscFunctionBegin;
5642c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5643c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5644c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5645c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5646c5033834SJed Brown   PetscFunctionReturn(0);
5647c5033834SJed Brown }
5648c5033834SJed Brown 
56499c6b16b5SShri Abhyankar /*@
5650a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
56519c6b16b5SShri Abhyankar 
56529c6b16b5SShri Abhyankar    Collective on TS
56539c6b16b5SShri Abhyankar 
56549c6b16b5SShri Abhyankar    Input Arguments:
56559c6b16b5SShri Abhyankar +  ts - time stepping context
5656a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5657a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
56589c6b16b5SShri Abhyankar 
56599c6b16b5SShri Abhyankar    Output Arguments:
5660a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
56617453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5662a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
56639c6b16b5SShri Abhyankar 
56649c6b16b5SShri Abhyankar    Level: developer
56659c6b16b5SShri Abhyankar 
5666deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
56679c6b16b5SShri Abhyankar @*/
56687453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56699c6b16b5SShri Abhyankar {
56709c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
56719c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
56727453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
56737453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
56749c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
56757453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
56767453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
56777453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
56789c6b16b5SShri Abhyankar 
56799c6b16b5SShri Abhyankar   PetscFunctionBegin;
56809c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5681a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5682a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5683a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5684a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5685a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5686a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
56878a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
56888a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5689a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
56909c6b16b5SShri Abhyankar 
56919c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
56929c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
56939c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
56949c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
56959c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
56967453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
56977453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
56987453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
56999c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57009c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57019c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57029c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
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 = PetscRealPart(atol[i]);
57077453f775SEmil Constantinescu       if(tola>0.){
57087453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57097453f775SEmil Constantinescu         na_loc++;
57107453f775SEmil Constantinescu       }
57117453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * 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     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57239c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57249c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57259c6b16b5SShri Abhyankar     const PetscScalar *atol;
57269c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57279c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
572876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57297453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57307453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57317453f775SEmil Constantinescu       if(tola>0.){
57327453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57337453f775SEmil Constantinescu         na_loc++;
57347453f775SEmil Constantinescu       }
57357453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57367453f775SEmil Constantinescu       if(tolr>0.){
57377453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57387453f775SEmil Constantinescu         nr_loc++;
57397453f775SEmil Constantinescu       }
57407453f775SEmil Constantinescu       tol=tola+tolr;
57417453f775SEmil Constantinescu       if(tol>0.){
57427453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57437453f775SEmil Constantinescu         n_loc++;
57447453f775SEmil Constantinescu       }
57459c6b16b5SShri Abhyankar     }
57469c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57479c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57489c6b16b5SShri Abhyankar     const PetscScalar *rtol;
57499c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57509c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
575176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57527453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57537453f775SEmil Constantinescu       tola = ts->atol;
57547453f775SEmil Constantinescu       if(tola>0.){
57557453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57567453f775SEmil Constantinescu         na_loc++;
57577453f775SEmil Constantinescu       }
57587453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57597453f775SEmil Constantinescu       if(tolr>0.){
57607453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57617453f775SEmil Constantinescu         nr_loc++;
57627453f775SEmil Constantinescu       }
57637453f775SEmil Constantinescu       tol=tola+tolr;
57647453f775SEmil Constantinescu       if(tol>0.){
57657453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57667453f775SEmil Constantinescu         n_loc++;
57677453f775SEmil Constantinescu       }
57689c6b16b5SShri Abhyankar     }
57699c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57709c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
57719c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
577276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57737453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57747453f775SEmil Constantinescu       tola = ts->atol;
57757453f775SEmil Constantinescu       if(tola>0.){
57767453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57777453f775SEmil Constantinescu         na_loc++;
57787453f775SEmil Constantinescu       }
57797453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57807453f775SEmil Constantinescu       if(tolr>0.){
57817453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57827453f775SEmil Constantinescu         nr_loc++;
57837453f775SEmil Constantinescu       }
57847453f775SEmil Constantinescu       tol=tola+tolr;
57857453f775SEmil Constantinescu       if(tol>0.){
57867453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57877453f775SEmil Constantinescu         n_loc++;
57887453f775SEmil Constantinescu       }
57899c6b16b5SShri Abhyankar     }
57909c6b16b5SShri Abhyankar   }
57919c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
57929c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
57939c6b16b5SShri Abhyankar 
57947453f775SEmil Constantinescu   err_loc[0] = sum;
57957453f775SEmil Constantinescu   err_loc[1] = suma;
57967453f775SEmil Constantinescu   err_loc[2] = sumr;
57977453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
57987453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
57997453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58007453f775SEmil Constantinescu 
5801a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58027453f775SEmil Constantinescu 
58037453f775SEmil Constantinescu   gsum   = err_glb[0];
58047453f775SEmil Constantinescu   gsuma  = err_glb[1];
58057453f775SEmil Constantinescu   gsumr  = err_glb[2];
58067453f775SEmil Constantinescu   n_glb  = err_glb[3];
58077453f775SEmil Constantinescu   na_glb = err_glb[4];
58087453f775SEmil Constantinescu   nr_glb = err_glb[5];
58097453f775SEmil Constantinescu 
5810b1316ef9SEmil Constantinescu   *norm  = 0.;
5811b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5812b1316ef9SEmil Constantinescu   *norma = 0.;
5813b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5814b1316ef9SEmil Constantinescu   *normr = 0.;
5815b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58169c6b16b5SShri Abhyankar 
58179c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58187453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58197453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58209c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
58219c6b16b5SShri Abhyankar }
58229c6b16b5SShri Abhyankar 
58239c6b16b5SShri Abhyankar /*@
5824a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
58259c6b16b5SShri Abhyankar 
58269c6b16b5SShri Abhyankar    Collective on TS
58279c6b16b5SShri Abhyankar 
58289c6b16b5SShri Abhyankar    Input Arguments:
58299c6b16b5SShri Abhyankar +  ts - time stepping context
5830a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5831a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
58329c6b16b5SShri Abhyankar 
58339c6b16b5SShri Abhyankar    Output Arguments:
5834a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58357453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5836a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58379c6b16b5SShri Abhyankar 
58389c6b16b5SShri Abhyankar    Level: developer
58399c6b16b5SShri Abhyankar 
5840deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
58419c6b16b5SShri Abhyankar @*/
58427453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58439c6b16b5SShri Abhyankar {
58449c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
58457453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
58469c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
58477453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
58487453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
58497453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
58509c6b16b5SShri Abhyankar 
58519c6b16b5SShri Abhyankar   PetscFunctionBegin;
58529c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5853a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5854a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5855a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5856a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5857a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5858a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
58598a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
58608a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5861a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
58629c6b16b5SShri Abhyankar 
58639c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
58649c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
58659c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
58669c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58679c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58687453f775SEmil Constantinescu 
58697453f775SEmil Constantinescu   max=0.;
58707453f775SEmil Constantinescu   maxa=0.;
58717453f775SEmil Constantinescu   maxr=0.;
58727453f775SEmil Constantinescu 
58737453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
58749c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
58759c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58769c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58777453f775SEmil Constantinescu 
58787453f775SEmil Constantinescu     for (i=0; i<n; i++) {
587976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58807453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58817453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58827453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58837453f775SEmil Constantinescu       tol  = tola+tolr;
58847453f775SEmil Constantinescu       if(tola>0.){
58857453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
58867453f775SEmil Constantinescu       }
58877453f775SEmil Constantinescu       if(tolr>0.){
58887453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
58897453f775SEmil Constantinescu       }
58907453f775SEmil Constantinescu       if(tol>0.){
58917453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
58927453f775SEmil Constantinescu       }
58939c6b16b5SShri Abhyankar     }
58949c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58959c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58969c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58979c6b16b5SShri Abhyankar     const PetscScalar *atol;
58989c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58997453f775SEmil Constantinescu     for (i=0; i<n; i++) {
590076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59017453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59027453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59037453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59047453f775SEmil Constantinescu       tol  = tola+tolr;
59057453f775SEmil Constantinescu       if(tola>0.){
59067453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59077453f775SEmil Constantinescu       }
59087453f775SEmil Constantinescu       if(tolr>0.){
59097453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59107453f775SEmil Constantinescu       }
59117453f775SEmil Constantinescu       if(tol>0.){
59127453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59137453f775SEmil Constantinescu       }
59149c6b16b5SShri Abhyankar     }
59159c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59169c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59179c6b16b5SShri Abhyankar     const PetscScalar *rtol;
59189c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59197453f775SEmil Constantinescu 
59207453f775SEmil Constantinescu     for (i=0; i<n; i++) {
592176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59237453f775SEmil Constantinescu       tola = ts->atol;
59247453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59257453f775SEmil Constantinescu       tol  = tola+tolr;
59267453f775SEmil Constantinescu       if(tola>0.){
59277453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59287453f775SEmil Constantinescu       }
59297453f775SEmil Constantinescu       if(tolr>0.){
59307453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59317453f775SEmil Constantinescu       }
59327453f775SEmil Constantinescu       if(tol>0.){
59337453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59347453f775SEmil Constantinescu       }
59359c6b16b5SShri Abhyankar     }
59369c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59379c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
59387453f775SEmil Constantinescu 
59397453f775SEmil Constantinescu     for (i=0; i<n; i++) {
594076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59417453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59427453f775SEmil Constantinescu       tola = ts->atol;
59437453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59447453f775SEmil Constantinescu       tol  = tola+tolr;
59457453f775SEmil Constantinescu       if(tola>0.){
59467453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59477453f775SEmil Constantinescu       }
59487453f775SEmil Constantinescu       if(tolr>0.){
59497453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59507453f775SEmil Constantinescu       }
59517453f775SEmil Constantinescu       if(tol>0.){
59527453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59537453f775SEmil Constantinescu       }
59549c6b16b5SShri Abhyankar     }
59559c6b16b5SShri Abhyankar   }
59569c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59579c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59587453f775SEmil Constantinescu   err_loc[0] = max;
59597453f775SEmil Constantinescu   err_loc[1] = maxa;
59607453f775SEmil Constantinescu   err_loc[2] = maxr;
5961a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59627453f775SEmil Constantinescu   gmax   = err_glb[0];
59637453f775SEmil Constantinescu   gmaxa  = err_glb[1];
59647453f775SEmil Constantinescu   gmaxr  = err_glb[2];
59659c6b16b5SShri Abhyankar 
59669c6b16b5SShri Abhyankar   *norm = gmax;
59677453f775SEmil Constantinescu   *norma = gmaxa;
59687453f775SEmil Constantinescu   *normr = gmaxr;
59699c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59707453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59717453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59729c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59739c6b16b5SShri Abhyankar }
59749c6b16b5SShri Abhyankar 
59751c3436cfSJed Brown /*@
59768a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
59771c3436cfSJed Brown 
59781c3436cfSJed Brown    Collective on TS
59791c3436cfSJed Brown 
59801c3436cfSJed Brown    Input Arguments:
59811c3436cfSJed Brown +  ts - time stepping context
5982a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5983a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5984a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
59857619abb3SShri 
59861c3436cfSJed Brown    Output Arguments:
5987a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
59888a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5989a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5990a4868fbcSLisandro Dalcin 
5991a4868fbcSLisandro Dalcin    Options Database Keys:
5992a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5993a4868fbcSLisandro Dalcin 
59941c3436cfSJed Brown    Level: developer
59951c3436cfSJed Brown 
59968a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
59971c3436cfSJed Brown @*/
59987453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59991c3436cfSJed Brown {
60008beabaa1SBarry Smith   PetscErrorCode ierr;
60011c3436cfSJed Brown 
60021c3436cfSJed Brown   PetscFunctionBegin;
6003a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60047453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6005a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
60067453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6007a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60081c3436cfSJed Brown   PetscFunctionReturn(0);
60091c3436cfSJed Brown }
60101c3436cfSJed Brown 
60118a175baeSEmil Constantinescu 
60128a175baeSEmil Constantinescu /*@
60138a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
60148a175baeSEmil Constantinescu 
60158a175baeSEmil Constantinescu    Collective on TS
60168a175baeSEmil Constantinescu 
60178a175baeSEmil Constantinescu    Input Arguments:
60188a175baeSEmil Constantinescu +  ts - time stepping context
60198a175baeSEmil Constantinescu .  E - error vector
60208a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60218a175baeSEmil Constantinescu -  Y - state vector, previous time step
60228a175baeSEmil Constantinescu 
60238a175baeSEmil Constantinescu    Output Arguments:
6024a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60258a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6026a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60278a175baeSEmil Constantinescu 
60288a175baeSEmil Constantinescu    Level: developer
60298a175baeSEmil Constantinescu 
60308a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
60318a175baeSEmil Constantinescu @*/
60328a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60338a175baeSEmil Constantinescu {
60348a175baeSEmil Constantinescu   PetscErrorCode    ierr;
60358a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
60368a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
60378a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
60388a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
60398a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
60408a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
60418a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
60428a175baeSEmil Constantinescu 
60438a175baeSEmil Constantinescu   PetscFunctionBegin;
60448a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60458a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
60468a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
60478a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
60488a175baeSEmil Constantinescu   PetscValidType(E,2);
60498a175baeSEmil Constantinescu   PetscValidType(U,3);
60508a175baeSEmil Constantinescu   PetscValidType(Y,4);
60518a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
60528a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
60538a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
60548a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
60558a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
60568a175baeSEmil Constantinescu 
60578a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
60588a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
60598a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
60608a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
60618a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60628a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60638a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
60648a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
60658a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
60668a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
60678a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
60688a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60698a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
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 = PetscRealPart(atol[i]);
60748a175baeSEmil Constantinescu       if(tola>0.){
60758a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
60768a175baeSEmil Constantinescu         na_loc++;
60778a175baeSEmil Constantinescu       }
60788a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * 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     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60908a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60918a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60928a175baeSEmil Constantinescu     const PetscScalar *atol;
60938a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60948a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
609576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60968a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60978a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60988a175baeSEmil Constantinescu       if(tola>0.){
60998a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61008a175baeSEmil Constantinescu         na_loc++;
61018a175baeSEmil Constantinescu       }
61028a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61038a175baeSEmil Constantinescu       if(tolr>0.){
61048a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61058a175baeSEmil Constantinescu         nr_loc++;
61068a175baeSEmil Constantinescu       }
61078a175baeSEmil Constantinescu       tol=tola+tolr;
61088a175baeSEmil Constantinescu       if(tol>0.){
61098a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61108a175baeSEmil Constantinescu         n_loc++;
61118a175baeSEmil Constantinescu       }
61128a175baeSEmil Constantinescu     }
61138a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61148a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61158a175baeSEmil Constantinescu     const PetscScalar *rtol;
61168a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61178a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
611876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61198a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61208a175baeSEmil Constantinescu       tola = ts->atol;
61218a175baeSEmil Constantinescu       if(tola>0.){
61228a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61238a175baeSEmil Constantinescu         na_loc++;
61248a175baeSEmil Constantinescu       }
61258a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61268a175baeSEmil Constantinescu       if(tolr>0.){
61278a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61288a175baeSEmil Constantinescu         nr_loc++;
61298a175baeSEmil Constantinescu       }
61308a175baeSEmil Constantinescu       tol=tola+tolr;
61318a175baeSEmil Constantinescu       if(tol>0.){
61328a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61338a175baeSEmil Constantinescu         n_loc++;
61348a175baeSEmil Constantinescu       }
61358a175baeSEmil Constantinescu     }
61368a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61378a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
61388a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
613976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61408a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61418a175baeSEmil Constantinescu       tola = ts->atol;
61428a175baeSEmil Constantinescu       if(tola>0.){
61438a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61448a175baeSEmil Constantinescu         na_loc++;
61458a175baeSEmil Constantinescu       }
61468a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61478a175baeSEmil Constantinescu       if(tolr>0.){
61488a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61498a175baeSEmil Constantinescu         nr_loc++;
61508a175baeSEmil Constantinescu       }
61518a175baeSEmil Constantinescu       tol=tola+tolr;
61528a175baeSEmil Constantinescu       if(tol>0.){
61538a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61548a175baeSEmil Constantinescu         n_loc++;
61558a175baeSEmil Constantinescu       }
61568a175baeSEmil Constantinescu     }
61578a175baeSEmil Constantinescu   }
61588a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
61598a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61608a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61618a175baeSEmil Constantinescu 
61628a175baeSEmil Constantinescu   err_loc[0] = sum;
61638a175baeSEmil Constantinescu   err_loc[1] = suma;
61648a175baeSEmil Constantinescu   err_loc[2] = sumr;
61658a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
61668a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
61678a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
61688a175baeSEmil Constantinescu 
6169a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61708a175baeSEmil Constantinescu 
61718a175baeSEmil Constantinescu   gsum   = err_glb[0];
61728a175baeSEmil Constantinescu   gsuma  = err_glb[1];
61738a175baeSEmil Constantinescu   gsumr  = err_glb[2];
61748a175baeSEmil Constantinescu   n_glb  = err_glb[3];
61758a175baeSEmil Constantinescu   na_glb = err_glb[4];
61768a175baeSEmil Constantinescu   nr_glb = err_glb[5];
61778a175baeSEmil Constantinescu 
61788a175baeSEmil Constantinescu   *norm  = 0.;
61798a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
61808a175baeSEmil Constantinescu   *norma = 0.;
61818a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
61828a175baeSEmil Constantinescu   *normr = 0.;
61838a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
61848a175baeSEmil Constantinescu 
61858a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61868a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61878a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61888a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61898a175baeSEmil Constantinescu }
61908a175baeSEmil Constantinescu 
61918a175baeSEmil Constantinescu /*@
61928a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
61938a175baeSEmil Constantinescu    Collective on TS
61948a175baeSEmil Constantinescu 
61958a175baeSEmil Constantinescu    Input Arguments:
61968a175baeSEmil Constantinescu +  ts - time stepping context
61978a175baeSEmil Constantinescu .  E - error vector
61988a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61998a175baeSEmil Constantinescu -  Y - state vector, previous time step
62008a175baeSEmil Constantinescu 
62018a175baeSEmil Constantinescu    Output Arguments:
6202a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62038a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6204a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62058a175baeSEmil Constantinescu 
62068a175baeSEmil Constantinescu    Level: developer
62078a175baeSEmil Constantinescu 
62088a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62098a175baeSEmil Constantinescu @*/
62108a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62118a175baeSEmil Constantinescu {
62128a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62138a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62148a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62158a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
62168a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62178a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
62188a175baeSEmil Constantinescu 
62198a175baeSEmil Constantinescu   PetscFunctionBegin;
62208a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62218a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
62228a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
62238a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
62248a175baeSEmil Constantinescu   PetscValidType(E,2);
62258a175baeSEmil Constantinescu   PetscValidType(U,3);
62268a175baeSEmil Constantinescu   PetscValidType(Y,4);
62278a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
62288a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
62298a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
62308a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
62318a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
62328a175baeSEmil Constantinescu 
62338a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
62348a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
62358a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
62368a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
62378a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62388a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62398a175baeSEmil Constantinescu 
62408a175baeSEmil Constantinescu   max=0.;
62418a175baeSEmil Constantinescu   maxa=0.;
62428a175baeSEmil Constantinescu   maxr=0.;
62438a175baeSEmil Constantinescu 
62448a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
62458a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
62468a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62478a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62488a175baeSEmil Constantinescu 
62498a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
625076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62518a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62528a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62538a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62548a175baeSEmil Constantinescu       tol  = tola+tolr;
62558a175baeSEmil Constantinescu       if(tola>0.){
62568a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62578a175baeSEmil Constantinescu       }
62588a175baeSEmil Constantinescu       if(tolr>0.){
62598a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
62608a175baeSEmil Constantinescu       }
62618a175baeSEmil Constantinescu       if(tol>0.){
62628a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
62638a175baeSEmil Constantinescu       }
62648a175baeSEmil Constantinescu     }
62658a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62668a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62678a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62688a175baeSEmil Constantinescu     const PetscScalar *atol;
62698a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62708a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
627176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62728a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62738a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62748a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62758a175baeSEmil Constantinescu       tol  = tola+tolr;
62768a175baeSEmil Constantinescu       if(tola>0.){
62778a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62788a175baeSEmil Constantinescu       }
62798a175baeSEmil Constantinescu       if(tolr>0.){
62808a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
62818a175baeSEmil Constantinescu       }
62828a175baeSEmil Constantinescu       if(tol>0.){
62838a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
62848a175baeSEmil Constantinescu       }
62858a175baeSEmil Constantinescu     }
62868a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62878a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62888a175baeSEmil Constantinescu     const PetscScalar *rtol;
62898a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62908a175baeSEmil Constantinescu 
62918a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
629276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62948a175baeSEmil Constantinescu       tola = ts->atol;
62958a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62968a175baeSEmil Constantinescu       tol  = tola+tolr;
62978a175baeSEmil Constantinescu       if(tola>0.){
62988a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
62998a175baeSEmil Constantinescu       }
63008a175baeSEmil Constantinescu       if(tolr>0.){
63018a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63028a175baeSEmil Constantinescu       }
63038a175baeSEmil Constantinescu       if(tol>0.){
63048a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63058a175baeSEmil Constantinescu       }
63068a175baeSEmil Constantinescu     }
63078a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63088a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63098a175baeSEmil Constantinescu 
63108a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
631176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63128a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63138a175baeSEmil Constantinescu       tola = ts->atol;
63148a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63158a175baeSEmil Constantinescu       tol  = tola+tolr;
63168a175baeSEmil Constantinescu       if(tola>0.){
63178a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63188a175baeSEmil Constantinescu       }
63198a175baeSEmil Constantinescu       if(tolr>0.){
63208a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63218a175baeSEmil Constantinescu       }
63228a175baeSEmil Constantinescu       if(tol>0.){
63238a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63248a175baeSEmil Constantinescu       }
63258a175baeSEmil Constantinescu     }
63268a175baeSEmil Constantinescu   }
63278a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
63288a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63298a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63308a175baeSEmil Constantinescu   err_loc[0] = max;
63318a175baeSEmil Constantinescu   err_loc[1] = maxa;
63328a175baeSEmil Constantinescu   err_loc[2] = maxr;
6333a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63348a175baeSEmil Constantinescu   gmax   = err_glb[0];
63358a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
63368a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
63378a175baeSEmil Constantinescu 
63388a175baeSEmil Constantinescu   *norm = gmax;
63398a175baeSEmil Constantinescu   *norma = gmaxa;
63408a175baeSEmil Constantinescu   *normr = gmaxr;
63418a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63428a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63438a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63448a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63458a175baeSEmil Constantinescu }
63468a175baeSEmil Constantinescu 
63478a175baeSEmil Constantinescu /*@
63488a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
63498a175baeSEmil Constantinescu 
63508a175baeSEmil Constantinescu    Collective on TS
63518a175baeSEmil Constantinescu 
63528a175baeSEmil Constantinescu    Input Arguments:
63538a175baeSEmil Constantinescu +  ts - time stepping context
63548a175baeSEmil Constantinescu .  E - error vector
63558a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63568a175baeSEmil Constantinescu .  Y - state vector, previous time step
63578a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
63588a175baeSEmil Constantinescu 
63598a175baeSEmil Constantinescu    Output Arguments:
6360a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
63618a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6362a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
63638a175baeSEmil Constantinescu 
63648a175baeSEmil Constantinescu    Options Database Keys:
63658a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
63668a175baeSEmil Constantinescu 
63678a175baeSEmil Constantinescu    Level: developer
63688a175baeSEmil Constantinescu 
63698a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
63708a175baeSEmil Constantinescu @*/
63718a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63728a175baeSEmil Constantinescu {
63738a175baeSEmil Constantinescu   PetscErrorCode ierr;
63748a175baeSEmil Constantinescu 
63758a175baeSEmil Constantinescu   PetscFunctionBegin;
63768a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
63778a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
63788a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
63798a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
63808a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
63818a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63828a175baeSEmil Constantinescu }
63838a175baeSEmil Constantinescu 
63848a175baeSEmil Constantinescu 
63858d59e960SJed Brown /*@
63868d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
63878d59e960SJed Brown 
63888d59e960SJed Brown    Logically Collective on TS
63898d59e960SJed Brown 
63908d59e960SJed Brown    Input Arguments:
63918d59e960SJed Brown +  ts - time stepping context
63928d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
63938d59e960SJed Brown 
63948d59e960SJed Brown    Note:
63958d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
63968d59e960SJed Brown 
63978d59e960SJed Brown    Level: intermediate
63988d59e960SJed Brown 
63998d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64008d59e960SJed Brown @*/
64018d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64028d59e960SJed Brown {
64038d59e960SJed Brown   PetscFunctionBegin;
64048d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64058d59e960SJed Brown   ts->cfltime_local = cfltime;
64068d59e960SJed Brown   ts->cfltime       = -1.;
64078d59e960SJed Brown   PetscFunctionReturn(0);
64088d59e960SJed Brown }
64098d59e960SJed Brown 
64108d59e960SJed Brown /*@
64118d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
64128d59e960SJed Brown 
64138d59e960SJed Brown    Collective on TS
64148d59e960SJed Brown 
64158d59e960SJed Brown    Input Arguments:
64168d59e960SJed Brown .  ts - time stepping context
64178d59e960SJed Brown 
64188d59e960SJed Brown    Output Arguments:
64198d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
64208d59e960SJed Brown 
64218d59e960SJed Brown    Level: advanced
64228d59e960SJed Brown 
64238d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
64248d59e960SJed Brown @*/
64258d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
64268d59e960SJed Brown {
64278d59e960SJed Brown   PetscErrorCode ierr;
64288d59e960SJed Brown 
64298d59e960SJed Brown   PetscFunctionBegin;
64308d59e960SJed Brown   if (ts->cfltime < 0) {
6431b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64328d59e960SJed Brown   }
64338d59e960SJed Brown   *cfltime = ts->cfltime;
64348d59e960SJed Brown   PetscFunctionReturn(0);
64358d59e960SJed Brown }
64368d59e960SJed Brown 
6437d6ebe24aSShri Abhyankar /*@
6438d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6439d6ebe24aSShri Abhyankar 
6440d6ebe24aSShri Abhyankar    Input Parameters:
6441a2b725a8SWilliam Gropp +  ts   - the TS context.
6442d6ebe24aSShri Abhyankar .  xl   - lower bound.
6443a2b725a8SWilliam Gropp -  xu   - upper bound.
6444d6ebe24aSShri Abhyankar 
6445d6ebe24aSShri Abhyankar    Notes:
6446d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6447e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6448d6ebe24aSShri Abhyankar 
64492bd2b0e6SSatish Balay    Level: advanced
64502bd2b0e6SSatish Balay 
6451d6ebe24aSShri Abhyankar @*/
6452d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6453d6ebe24aSShri Abhyankar {
6454d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6455d6ebe24aSShri Abhyankar   SNES           snes;
6456d6ebe24aSShri Abhyankar 
6457d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6458d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6459d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6460d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6461d6ebe24aSShri Abhyankar }
6462d6ebe24aSShri Abhyankar 
6463b3603a34SBarry Smith /*@C
64644f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6465b3603a34SBarry Smith        in a time based line graph
6466b3603a34SBarry Smith 
6467b3603a34SBarry Smith    Collective on TS
6468b3603a34SBarry Smith 
6469b3603a34SBarry Smith    Input Parameters:
6470b3603a34SBarry Smith +  ts - the TS context
6471b3603a34SBarry Smith .  step - current time-step
6472b3603a34SBarry Smith .  ptime - current time
64737db568b7SBarry Smith .  u - current solution
64747db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6475b3603a34SBarry Smith 
6476b3d3934dSBarry Smith    Options Database:
64779ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6478b3d3934dSBarry Smith 
6479b3603a34SBarry Smith    Level: intermediate
6480b3603a34SBarry Smith 
648195452b02SPatrick Sanan    Notes:
648295452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6483b3603a34SBarry Smith 
64847db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
64857db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
64867db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
64877db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6488b3603a34SBarry Smith @*/
64897db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6490b3603a34SBarry Smith {
6491b3603a34SBarry Smith   PetscErrorCode    ierr;
64927db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6493b3603a34SBarry Smith   const PetscScalar *yy;
649480666b62SBarry Smith   Vec               v;
6495b3603a34SBarry Smith 
6496b3603a34SBarry Smith   PetscFunctionBegin;
649763e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
649858ff32f7SBarry Smith   if (!step) {
6499a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65006934998bSLisandro Dalcin     PetscInt      dim;
6501a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65020ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6503bab0a581SBarry Smith     if (!ctx->names) {
6504bab0a581SBarry Smith       PetscBool flg;
6505bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6506bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6507bab0a581SBarry Smith       if (flg) {
6508bab0a581SBarry Smith         PetscInt i,n;
6509bab0a581SBarry Smith         char     **names;
6510bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6511bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6512bab0a581SBarry Smith         for (i=0; i<n; i++) {
6513bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6514bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6515bab0a581SBarry Smith         }
6516bab0a581SBarry Smith         names[n] = NULL;
6517bab0a581SBarry Smith         ctx->names = names;
6518bab0a581SBarry Smith       }
6519bab0a581SBarry Smith     }
6520387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6521387f4636SBarry Smith       char      **displaynames;
6522387f4636SBarry Smith       PetscBool flg;
6523387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6524580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6525c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6526387f4636SBarry Smith       if (flg) {
6527a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6528387f4636SBarry Smith       }
6529387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6530387f4636SBarry Smith     }
6531387f4636SBarry Smith     if (ctx->displaynames) {
6532387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6533387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6534387f4636SBarry Smith     } else if (ctx->names) {
65350910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65360b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6537387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6538b0bc92c6SBarry Smith     } else {
6539b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6540b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6541387f4636SBarry Smith     }
65420b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
654358ff32f7SBarry Smith   }
65446934998bSLisandro Dalcin 
65456934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65466934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
654780666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
65486934998bSLisandro Dalcin   if (ctx->displaynames) {
65496934998bSLisandro Dalcin     PetscInt i;
65506934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
65516934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
65526934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
65536934998bSLisandro Dalcin   } else {
6554e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6555e3efe391SJed Brown     PetscInt  i,n;
65566934998bSLisandro Dalcin     PetscReal *yreal;
655780666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6558785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6559e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
65600b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6561e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6562e3efe391SJed Brown #else
65630b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6564e3efe391SJed Brown #endif
656580666b62SBarry Smith   }
65666934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
65676934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
65686934998bSLisandro Dalcin 
6569b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65700b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65716934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65723923b477SBarry Smith   }
6573b3603a34SBarry Smith   PetscFunctionReturn(0);
6574b3603a34SBarry Smith }
6575b3603a34SBarry Smith 
6576b037adc7SBarry Smith /*@C
657731152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6578b037adc7SBarry Smith 
6579b037adc7SBarry Smith    Collective on TS
6580b037adc7SBarry Smith 
6581b037adc7SBarry Smith    Input Parameters:
6582b037adc7SBarry Smith +  ts - the TS context
6583b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6584b037adc7SBarry Smith 
6585b037adc7SBarry Smith    Level: intermediate
6586b037adc7SBarry Smith 
658795452b02SPatrick Sanan    Notes:
658895452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
65897db568b7SBarry Smith 
6590a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6591b037adc7SBarry Smith @*/
659231152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6593b037adc7SBarry Smith {
6594b037adc7SBarry Smith   PetscErrorCode    ierr;
6595b037adc7SBarry Smith   PetscInt          i;
6596b037adc7SBarry Smith 
6597b037adc7SBarry Smith   PetscFunctionBegin;
6598b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6599b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66005537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6601b3d3934dSBarry Smith       break;
6602b3d3934dSBarry Smith     }
6603b3d3934dSBarry Smith   }
6604b3d3934dSBarry Smith   PetscFunctionReturn(0);
6605b3d3934dSBarry Smith }
6606b3d3934dSBarry Smith 
6607e673d494SBarry Smith /*@C
6608e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6609e673d494SBarry Smith 
6610e673d494SBarry Smith    Collective on TS
6611e673d494SBarry Smith 
6612e673d494SBarry Smith    Input Parameters:
6613e673d494SBarry Smith +  ts - the TS context
6614e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6615e673d494SBarry Smith 
6616e673d494SBarry Smith    Level: intermediate
6617e673d494SBarry Smith 
6618a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6619e673d494SBarry Smith @*/
6620e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6621e673d494SBarry Smith {
6622e673d494SBarry Smith   PetscErrorCode    ierr;
6623e673d494SBarry Smith 
6624e673d494SBarry Smith   PetscFunctionBegin;
6625e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6626e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6627e673d494SBarry Smith   PetscFunctionReturn(0);
6628e673d494SBarry Smith }
6629e673d494SBarry Smith 
6630b3d3934dSBarry Smith /*@C
6631b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6632b3d3934dSBarry Smith 
6633b3d3934dSBarry Smith    Collective on TS
6634b3d3934dSBarry Smith 
6635b3d3934dSBarry Smith    Input Parameter:
6636b3d3934dSBarry Smith .  ts - the TS context
6637b3d3934dSBarry Smith 
6638b3d3934dSBarry Smith    Output Parameter:
6639b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6640b3d3934dSBarry Smith 
6641b3d3934dSBarry Smith    Level: intermediate
6642b3d3934dSBarry Smith 
664395452b02SPatrick Sanan    Notes:
664495452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66457db568b7SBarry Smith 
6646b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6647b3d3934dSBarry Smith @*/
6648b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6649b3d3934dSBarry Smith {
6650b3d3934dSBarry Smith   PetscInt       i;
6651b3d3934dSBarry Smith 
6652b3d3934dSBarry Smith   PetscFunctionBegin;
6653b3d3934dSBarry Smith   *names = NULL;
6654b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6655b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66565537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6657b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6658b3d3934dSBarry Smith       break;
6659387f4636SBarry Smith     }
6660387f4636SBarry Smith   }
6661387f4636SBarry Smith   PetscFunctionReturn(0);
6662387f4636SBarry Smith }
6663387f4636SBarry Smith 
6664a66092f1SBarry Smith /*@C
6665a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6666a66092f1SBarry Smith 
6667a66092f1SBarry Smith    Collective on TS
6668a66092f1SBarry Smith 
6669a66092f1SBarry Smith    Input Parameters:
6670a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6671a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6672a66092f1SBarry Smith 
6673a66092f1SBarry Smith    Level: intermediate
6674a66092f1SBarry Smith 
6675a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6676a66092f1SBarry Smith @*/
6677a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6678a66092f1SBarry Smith {
6679a66092f1SBarry Smith   PetscInt          j = 0,k;
6680a66092f1SBarry Smith   PetscErrorCode    ierr;
6681a66092f1SBarry Smith 
6682a66092f1SBarry Smith   PetscFunctionBegin;
6683a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6684a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6685a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6686a66092f1SBarry Smith   while (displaynames[j]) j++;
6687a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6688a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6689a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6690a66092f1SBarry Smith   j = 0;
6691a66092f1SBarry Smith   while (displaynames[j]) {
6692a66092f1SBarry Smith     k = 0;
6693a66092f1SBarry Smith     while (ctx->names[k]) {
6694a66092f1SBarry Smith       PetscBool flg;
6695a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6696a66092f1SBarry Smith       if (flg) {
6697a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6698a66092f1SBarry Smith         break;
6699a66092f1SBarry Smith       }
6700a66092f1SBarry Smith       k++;
6701a66092f1SBarry Smith     }
6702a66092f1SBarry Smith     j++;
6703a66092f1SBarry Smith   }
6704a66092f1SBarry Smith   PetscFunctionReturn(0);
6705a66092f1SBarry Smith }
6706a66092f1SBarry Smith 
6707387f4636SBarry Smith /*@C
6708387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6709387f4636SBarry Smith 
6710387f4636SBarry Smith    Collective on TS
6711387f4636SBarry Smith 
6712387f4636SBarry Smith    Input Parameters:
6713387f4636SBarry Smith +  ts - the TS context
6714a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6715387f4636SBarry Smith 
671695452b02SPatrick Sanan    Notes:
671795452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67187db568b7SBarry Smith 
6719387f4636SBarry Smith    Level: intermediate
6720387f4636SBarry Smith 
6721387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6722387f4636SBarry Smith @*/
6723387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6724387f4636SBarry Smith {
6725a66092f1SBarry Smith   PetscInt          i;
6726387f4636SBarry Smith   PetscErrorCode    ierr;
6727387f4636SBarry Smith 
6728387f4636SBarry Smith   PetscFunctionBegin;
6729387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6730387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67315537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6732b3d3934dSBarry Smith       break;
6733b037adc7SBarry Smith     }
6734b037adc7SBarry Smith   }
6735b037adc7SBarry Smith   PetscFunctionReturn(0);
6736b037adc7SBarry Smith }
6737b037adc7SBarry Smith 
673880666b62SBarry Smith /*@C
673980666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
674080666b62SBarry Smith 
674180666b62SBarry Smith    Collective on TS
674280666b62SBarry Smith 
674380666b62SBarry Smith    Input Parameters:
674480666b62SBarry Smith +  ts - the TS context
674580666b62SBarry Smith .  transform - the transform function
67467684fa3eSBarry Smith .  destroy - function to destroy the optional context
674780666b62SBarry Smith -  ctx - optional context used by transform function
674880666b62SBarry Smith 
674995452b02SPatrick Sanan    Notes:
675095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67517db568b7SBarry Smith 
675280666b62SBarry Smith    Level: intermediate
675380666b62SBarry Smith 
6754a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
675580666b62SBarry Smith @*/
67567684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
675780666b62SBarry Smith {
675880666b62SBarry Smith   PetscInt          i;
6759a66092f1SBarry Smith   PetscErrorCode    ierr;
676080666b62SBarry Smith 
676180666b62SBarry Smith   PetscFunctionBegin;
676280666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
676380666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67645537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
676580666b62SBarry Smith     }
676680666b62SBarry Smith   }
676780666b62SBarry Smith   PetscFunctionReturn(0);
676880666b62SBarry Smith }
676980666b62SBarry Smith 
6770e673d494SBarry Smith /*@C
6771e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6772e673d494SBarry Smith 
6773e673d494SBarry Smith    Collective on TSLGCtx
6774e673d494SBarry Smith 
6775e673d494SBarry Smith    Input Parameters:
6776e673d494SBarry Smith +  ts - the TS context
6777e673d494SBarry Smith .  transform - the transform function
67787684fa3eSBarry Smith .  destroy - function to destroy the optional context
6779e673d494SBarry Smith -  ctx - optional context used by transform function
6780e673d494SBarry Smith 
6781e673d494SBarry Smith    Level: intermediate
6782e673d494SBarry Smith 
6783a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6784e673d494SBarry Smith @*/
67857684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6786e673d494SBarry Smith {
6787e673d494SBarry Smith   PetscFunctionBegin;
6788e673d494SBarry Smith   ctx->transform    = transform;
67897684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6790e673d494SBarry Smith   ctx->transformctx = tctx;
6791e673d494SBarry Smith   PetscFunctionReturn(0);
6792e673d494SBarry Smith }
6793e673d494SBarry Smith 
6794ef20d060SBarry Smith /*@C
67958b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6796ef20d060SBarry Smith        in a time based line graph
6797ef20d060SBarry Smith 
6798ef20d060SBarry Smith    Collective on TS
6799ef20d060SBarry Smith 
6800ef20d060SBarry Smith    Input Parameters:
6801ef20d060SBarry Smith +  ts - the TS context
6802ef20d060SBarry Smith .  step - current time-step
6803ef20d060SBarry Smith .  ptime - current time
68047db568b7SBarry Smith .  u - current solution
68057db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6806ef20d060SBarry Smith 
6807ef20d060SBarry Smith    Level: intermediate
6808ef20d060SBarry Smith 
680995452b02SPatrick Sanan    Notes:
681095452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6811abd5a294SJed Brown 
6812abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6813abd5a294SJed Brown 
6814abd5a294SJed Brown    Options Database Keys:
68154f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6816ef20d060SBarry Smith 
6817abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6818ef20d060SBarry Smith @*/
68190910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6820ef20d060SBarry Smith {
6821ef20d060SBarry Smith   PetscErrorCode    ierr;
68220b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6823ef20d060SBarry Smith   const PetscScalar *yy;
6824ef20d060SBarry Smith   Vec               y;
6825ef20d060SBarry Smith 
6826ef20d060SBarry Smith   PetscFunctionBegin;
6827a9f9c1f6SBarry Smith   if (!step) {
6828a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68296934998bSLisandro Dalcin     PetscInt      dim;
6830a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68318b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68320910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6833a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6834a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6835a9f9c1f6SBarry Smith   }
68360910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6837ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68380910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6839ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6840e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6841e3efe391SJed Brown   {
6842e3efe391SJed Brown     PetscReal *yreal;
6843e3efe391SJed Brown     PetscInt  i,n;
6844e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6845785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6846e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
68470b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6848e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6849e3efe391SJed Brown   }
6850e3efe391SJed Brown #else
68510b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6852e3efe391SJed Brown #endif
6853ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6854ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6855b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68560b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
68576934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
68583923b477SBarry Smith   }
6859ef20d060SBarry Smith   PetscFunctionReturn(0);
6860ef20d060SBarry Smith }
6861ef20d060SBarry Smith 
68625e3b7effSJoseph Pusztay /*@C
68635e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
68645e3b7effSJoseph Pusztay 
68655e3b7effSJoseph Pusztay    Input Parameters:
68665e3b7effSJoseph Pusztay +  ts - the TS context
68675e3b7effSJoseph Pusztay .  step - current time-step
68685e3b7effSJoseph Pusztay .  ptime - current time
68695e3b7effSJoseph Pusztay .  u - current solution
68705e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
68715e3b7effSJoseph Pusztay 
68725e3b7effSJoseph Pusztay    Options Database:
6873918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
68745e3b7effSJoseph Pusztay 
68755e3b7effSJoseph Pusztay    Level: intermediate
68765e3b7effSJoseph Pusztay 
68775e3b7effSJoseph Pusztay @*/
68780ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
68791b575b74SJoseph Pusztay {
68801b575b74SJoseph Pusztay   PetscErrorCode    ierr;
68811b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
68821b575b74SJoseph Pusztay   const PetscScalar *yy;
6883b1670a61SJoseph Pusztay   PetscReal       *y,*x;
68841b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
68851b575b74SJoseph Pusztay   DM                dm;
68861b575b74SJoseph Pusztay 
68871b575b74SJoseph Pusztay   PetscFunctionBegin;
68881b575b74SJoseph Pusztay 
68891b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
68901b575b74SJoseph Pusztay   if (!step) {
68911b575b74SJoseph Pusztay     PetscDrawAxis axis;
68921b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
68931b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6894895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6895895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
68961b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
68971b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
68981b575b74SJoseph Pusztay     if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
68991b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69001b575b74SJoseph Pusztay     Np /= 2*dim;
69011b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69021b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69031b575b74SJoseph Pusztay   }
69041b575b74SJoseph Pusztay 
69051b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69061b575b74SJoseph Pusztay   Np /= 2*dim;
69071b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6908895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69091b575b74SJoseph Pusztay   /* get points from solution vector */
69101b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6911b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6912b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6913895f37d5SJoseph Pusztay   }
69141b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
69151b575b74SJoseph Pusztay 
69161b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69171b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
69181b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
69191b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
69201b575b74SJoseph Pusztay   }
69211b575b74SJoseph Pusztay 
6922918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6923918b1d10SJoseph Pusztay 
69241b575b74SJoseph Pusztay   PetscFunctionReturn(0);
69251b575b74SJoseph Pusztay }
69261b575b74SJoseph Pusztay 
69271b575b74SJoseph Pusztay 
69281b575b74SJoseph Pusztay 
69297cf37e64SBarry Smith /*@C
69307cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69317cf37e64SBarry Smith 
69327cf37e64SBarry Smith    Collective on TS
69337cf37e64SBarry Smith 
69347cf37e64SBarry Smith    Input Parameters:
69357cf37e64SBarry Smith +  ts - the TS context
69367cf37e64SBarry Smith .  step - current time-step
69377cf37e64SBarry Smith .  ptime - current time
69387cf37e64SBarry Smith .  u - current solution
69397cf37e64SBarry Smith -  dctx - unused context
69407cf37e64SBarry Smith 
69417cf37e64SBarry Smith    Level: intermediate
69427cf37e64SBarry Smith 
69437cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
69447cf37e64SBarry Smith 
69457cf37e64SBarry Smith    Options Database Keys:
69467cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
69477cf37e64SBarry Smith 
69487cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
69497cf37e64SBarry Smith @*/
6950edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
69517cf37e64SBarry Smith {
69527cf37e64SBarry Smith   PetscErrorCode    ierr;
69537cf37e64SBarry Smith   Vec               y;
69547cf37e64SBarry Smith   PetscReal         nrm;
6955edbaebb3SBarry Smith   PetscBool         flg;
69567cf37e64SBarry Smith 
69577cf37e64SBarry Smith   PetscFunctionBegin;
69587cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
69597cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69607cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6961edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6962edbaebb3SBarry Smith   if (flg) {
69637cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6964edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6965edbaebb3SBarry Smith   }
6966edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6967edbaebb3SBarry Smith   if (flg) {
6968edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6969edbaebb3SBarry Smith   }
6970edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
69717cf37e64SBarry Smith   PetscFunctionReturn(0);
69727cf37e64SBarry Smith }
69737cf37e64SBarry Smith 
6974201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6975201da799SBarry Smith {
6976201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6977201da799SBarry Smith   PetscReal      x   = ptime,y;
6978201da799SBarry Smith   PetscErrorCode ierr;
6979201da799SBarry Smith   PetscInt       its;
6980201da799SBarry Smith 
6981201da799SBarry Smith   PetscFunctionBegin;
698263e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6983201da799SBarry Smith   if (!n) {
6984201da799SBarry Smith     PetscDrawAxis axis;
6985201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6986201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6987201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6988201da799SBarry Smith     ctx->snes_its = 0;
6989201da799SBarry Smith   }
6990201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6991201da799SBarry Smith   y    = its - ctx->snes_its;
6992201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
69933fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6994201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69956934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6996201da799SBarry Smith   }
6997201da799SBarry Smith   ctx->snes_its = its;
6998201da799SBarry Smith   PetscFunctionReturn(0);
6999201da799SBarry Smith }
7000201da799SBarry Smith 
7001201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7002201da799SBarry Smith {
7003201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7004201da799SBarry Smith   PetscReal      x   = ptime,y;
7005201da799SBarry Smith   PetscErrorCode ierr;
7006201da799SBarry Smith   PetscInt       its;
7007201da799SBarry Smith 
7008201da799SBarry Smith   PetscFunctionBegin;
700963e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7010201da799SBarry Smith   if (!n) {
7011201da799SBarry Smith     PetscDrawAxis axis;
7012201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7013201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7014201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7015201da799SBarry Smith     ctx->ksp_its = 0;
7016201da799SBarry Smith   }
7017201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7018201da799SBarry Smith   y    = its - ctx->ksp_its;
7019201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
702099fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7021201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70226934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7023201da799SBarry Smith   }
7024201da799SBarry Smith   ctx->ksp_its = its;
7025201da799SBarry Smith   PetscFunctionReturn(0);
7026201da799SBarry Smith }
7027f9c1d6abSBarry Smith 
7028f9c1d6abSBarry Smith /*@
7029f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7030f9c1d6abSBarry Smith 
7031d083f849SBarry Smith    Collective on TS
7032f9c1d6abSBarry Smith 
7033f9c1d6abSBarry Smith    Input Parameters:
7034f9c1d6abSBarry Smith +  ts - the TS context
7035f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7036f9c1d6abSBarry Smith 
7037f9c1d6abSBarry Smith    Output Parameters:
7038f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7039f9c1d6abSBarry Smith 
7040f9c1d6abSBarry Smith    Level: developer
7041f9c1d6abSBarry Smith 
7042f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7043f9c1d6abSBarry Smith @*/
7044f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7045f9c1d6abSBarry Smith {
7046f9c1d6abSBarry Smith   PetscErrorCode ierr;
7047f9c1d6abSBarry Smith 
7048f9c1d6abSBarry Smith   PetscFunctionBegin;
7049f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7050ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7051f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7052f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7053f9c1d6abSBarry Smith }
705424655328SShri 
7055b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7056b3d3934dSBarry Smith /*@C
7057b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7058b3d3934dSBarry Smith 
7059b3d3934dSBarry Smith    Collective on TS
7060b3d3934dSBarry Smith 
7061b3d3934dSBarry Smith    Input Parameters:
7062b3d3934dSBarry Smith .  ts  - the ODE solver object
7063b3d3934dSBarry Smith 
7064b3d3934dSBarry Smith    Output Parameter:
7065b3d3934dSBarry Smith .  ctx - the context
7066b3d3934dSBarry Smith 
7067b3d3934dSBarry Smith    Level: intermediate
7068b3d3934dSBarry Smith 
7069b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7070b3d3934dSBarry Smith 
7071b3d3934dSBarry Smith @*/
7072b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7073b3d3934dSBarry Smith {
7074b3d3934dSBarry Smith   PetscErrorCode ierr;
7075b3d3934dSBarry Smith 
7076b3d3934dSBarry Smith   PetscFunctionBegin;
7077a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7078b3d3934dSBarry Smith   PetscFunctionReturn(0);
7079b3d3934dSBarry Smith }
7080b3d3934dSBarry Smith 
7081b3d3934dSBarry Smith /*@C
7082b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7083b3d3934dSBarry Smith 
7084b3d3934dSBarry Smith    Collective on TS
7085b3d3934dSBarry Smith 
7086b3d3934dSBarry Smith    Input Parameters:
7087b3d3934dSBarry Smith +  ts - the TS context
7088b3d3934dSBarry Smith .  step - current time-step
7089b3d3934dSBarry Smith .  ptime - current time
70907db568b7SBarry Smith .  u  - current solution
70917db568b7SBarry Smith -  dctx - the envelope context
7092b3d3934dSBarry Smith 
7093b3d3934dSBarry Smith    Options Database:
7094b3d3934dSBarry Smith .  -ts_monitor_envelope
7095b3d3934dSBarry Smith 
7096b3d3934dSBarry Smith    Level: intermediate
7097b3d3934dSBarry Smith 
709895452b02SPatrick Sanan    Notes:
709995452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7100b3d3934dSBarry Smith 
71017db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7102b3d3934dSBarry Smith @*/
71037db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7104b3d3934dSBarry Smith {
7105b3d3934dSBarry Smith   PetscErrorCode       ierr;
71067db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7107b3d3934dSBarry Smith 
7108b3d3934dSBarry Smith   PetscFunctionBegin;
7109b3d3934dSBarry Smith   if (!ctx->max) {
7110b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7111b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7112b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7113b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7114b3d3934dSBarry Smith   } else {
7115b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7116b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7117b3d3934dSBarry Smith   }
7118b3d3934dSBarry Smith   PetscFunctionReturn(0);
7119b3d3934dSBarry Smith }
7120b3d3934dSBarry Smith 
7121b3d3934dSBarry Smith /*@C
7122b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7123b3d3934dSBarry Smith 
7124b3d3934dSBarry Smith    Collective on TS
7125b3d3934dSBarry Smith 
7126b3d3934dSBarry Smith    Input Parameter:
7127b3d3934dSBarry Smith .  ts - the TS context
7128b3d3934dSBarry Smith 
7129b3d3934dSBarry Smith    Output Parameter:
7130b3d3934dSBarry Smith +  max - the maximum values
7131b3d3934dSBarry Smith -  min - the minimum values
7132b3d3934dSBarry Smith 
713395452b02SPatrick Sanan    Notes:
713495452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71357db568b7SBarry Smith 
7136b3d3934dSBarry Smith    Level: intermediate
7137b3d3934dSBarry Smith 
7138b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7139b3d3934dSBarry Smith @*/
7140b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7141b3d3934dSBarry Smith {
7142b3d3934dSBarry Smith   PetscInt i;
7143b3d3934dSBarry Smith 
7144b3d3934dSBarry Smith   PetscFunctionBegin;
7145b3d3934dSBarry Smith   if (max) *max = NULL;
7146b3d3934dSBarry Smith   if (min) *min = NULL;
7147b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7148b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
71495537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7150b3d3934dSBarry Smith       if (max) *max = ctx->max;
7151b3d3934dSBarry Smith       if (min) *min = ctx->min;
7152b3d3934dSBarry Smith       break;
7153b3d3934dSBarry Smith     }
7154b3d3934dSBarry Smith   }
7155b3d3934dSBarry Smith   PetscFunctionReturn(0);
7156b3d3934dSBarry Smith }
7157b3d3934dSBarry Smith 
7158b3d3934dSBarry Smith /*@C
7159b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7160b3d3934dSBarry Smith 
7161b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7162b3d3934dSBarry Smith 
7163b3d3934dSBarry Smith    Input Parameter:
7164b3d3934dSBarry Smith .  ctx - the monitor context
7165b3d3934dSBarry Smith 
7166b3d3934dSBarry Smith    Level: intermediate
7167b3d3934dSBarry Smith 
71687db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7169b3d3934dSBarry Smith @*/
7170b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7171b3d3934dSBarry Smith {
7172b3d3934dSBarry Smith   PetscErrorCode ierr;
7173b3d3934dSBarry Smith 
7174b3d3934dSBarry Smith   PetscFunctionBegin;
7175b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7176b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7177b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7178b3d3934dSBarry Smith   PetscFunctionReturn(0);
7179b3d3934dSBarry Smith }
7180f2dee214SBarry Smith 
718124655328SShri /*@
7182dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7183dcb233daSLisandro Dalcin 
7184dcb233daSLisandro Dalcin    Collective on TS
7185dcb233daSLisandro Dalcin 
7186dcb233daSLisandro Dalcin    Input Parameter:
7187dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7188dcb233daSLisandro Dalcin 
7189dcb233daSLisandro Dalcin    Level: advanced
7190dcb233daSLisandro Dalcin 
7191dcb233daSLisandro Dalcin    Notes:
7192dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7193dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7194dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7195dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7196dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7197dcb233daSLisandro Dalcin    discontinuous source terms).
7198dcb233daSLisandro Dalcin 
7199dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7200dcb233daSLisandro Dalcin @*/
7201dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7202dcb233daSLisandro Dalcin {
7203dcb233daSLisandro Dalcin   PetscFunctionBegin;
7204dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7205dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7206dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7207dcb233daSLisandro Dalcin }
7208dcb233daSLisandro Dalcin 
7209dcb233daSLisandro Dalcin /*@
721024655328SShri    TSRollBack - Rolls back one time step
721124655328SShri 
721224655328SShri    Collective on TS
721324655328SShri 
721424655328SShri    Input Parameter:
721524655328SShri .  ts - the TS context obtained from TSCreate()
721624655328SShri 
721724655328SShri    Level: advanced
721824655328SShri 
721924655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
722024655328SShri @*/
722124655328SShri PetscErrorCode  TSRollBack(TS ts)
722224655328SShri {
722324655328SShri   PetscErrorCode ierr;
722424655328SShri 
722524655328SShri   PetscFunctionBegin;
722624655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7227b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
722824655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
722924655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
723024655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
723124655328SShri   ts->ptime = ts->ptime_prev;
7232be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
72332808aa04SLisandro Dalcin   ts->steps--;
7234b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
723524655328SShri   PetscFunctionReturn(0);
723624655328SShri }
7237aeb4809dSShri Abhyankar 
7238ff22ae23SHong Zhang /*@
7239ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7240ff22ae23SHong Zhang 
7241ff22ae23SHong Zhang    Input Parameter:
7242ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7243ff22ae23SHong Zhang 
72440429704eSStefano Zampini    Output Parameters:
72450429704eSStefano Zampini +  ns - the number of stages
72460429704eSStefano Zampini -  Y - the current stage vectors
72470429704eSStefano Zampini 
7248ff22ae23SHong Zhang    Level: advanced
7249ff22ae23SHong Zhang 
72500429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
72510429704eSStefano Zampini 
7252ff22ae23SHong Zhang .seealso: TSCreate()
7253ff22ae23SHong Zhang @*/
7254ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7255ff22ae23SHong Zhang {
7256ff22ae23SHong Zhang   PetscErrorCode ierr;
7257ff22ae23SHong Zhang 
7258ff22ae23SHong Zhang   PetscFunctionBegin;
7259ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
72600429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
72610429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
72620429704eSStefano Zampini   if (!ts->ops->getstages) {
72630429704eSStefano Zampini     if (ns) *ns = 0;
72640429704eSStefano Zampini     if (Y) *Y = NULL;
72650429704eSStefano Zampini   } else {
7266ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7267ff22ae23SHong Zhang   }
7268ff22ae23SHong Zhang   PetscFunctionReturn(0);
7269ff22ae23SHong Zhang }
7270ff22ae23SHong Zhang 
7271847ff0e1SMatthew G. Knepley /*@C
7272847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7273847ff0e1SMatthew G. Knepley 
7274847ff0e1SMatthew G. Knepley   Collective on SNES
7275847ff0e1SMatthew G. Knepley 
7276847ff0e1SMatthew G. Knepley   Input Parameters:
7277847ff0e1SMatthew G. Knepley + ts - the TS context
7278847ff0e1SMatthew G. Knepley . t - current timestep
7279847ff0e1SMatthew G. Knepley . U - state vector
7280847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7281847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7282847ff0e1SMatthew G. Knepley - ctx - an optional user context
7283847ff0e1SMatthew G. Knepley 
7284847ff0e1SMatthew G. Knepley   Output Parameters:
7285847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7286847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7287847ff0e1SMatthew G. Knepley 
7288847ff0e1SMatthew G. Knepley   Level: intermediate
7289847ff0e1SMatthew G. Knepley 
7290847ff0e1SMatthew G. Knepley   Notes:
7291847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7292847ff0e1SMatthew G. Knepley 
7293847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7294847ff0e1SMatthew G. Knepley 
7295847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7296847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7297847ff0e1SMatthew G. Knepley 
7298847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7299847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7300847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7301847ff0e1SMatthew G. Knepley 
7302847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7303847ff0e1SMatthew G. Knepley @*/
7304847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7305847ff0e1SMatthew G. Knepley {
7306847ff0e1SMatthew G. Knepley   SNES           snes;
7307847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7308847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7309847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7310847ff0e1SMatthew G. Knepley 
7311847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7312c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7313847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7314847ff0e1SMatthew G. Knepley   if (!color) {
7315847ff0e1SMatthew G. Knepley     DM         dm;
7316847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7317847ff0e1SMatthew G. Knepley 
7318847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7319847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7320847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7321847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7322847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7323847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7324847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7325847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7326847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7327847ff0e1SMatthew G. Knepley     } else {
7328847ff0e1SMatthew G. Knepley       MatColoring mc;
7329847ff0e1SMatthew G. Knepley 
7330847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7331847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7332847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7333847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7334847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7335847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7336847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7337847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7338847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7339847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7340847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7341847ff0e1SMatthew G. Knepley     }
7342847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7343847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7344847ff0e1SMatthew G. Knepley   }
7345847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7346847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7347847ff0e1SMatthew G. Knepley   if (J != B) {
7348847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7349847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7350847ff0e1SMatthew G. Knepley   }
7351847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7352847ff0e1SMatthew G. Knepley }
735393b34091SDebojyoti Ghosh 
7354cb9d8021SPierre Barbier de Reuille /*@
73556bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7356cb9d8021SPierre Barbier de Reuille 
7357cb9d8021SPierre Barbier de Reuille     Input Parameters:
73586bc98fa9SBarry Smith +    ts - the TS context
73596bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
73606bc98fa9SBarry Smith 
73616bc98fa9SBarry Smith     Calling sequence of func:
73626bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
73636bc98fa9SBarry Smith 
73646bc98fa9SBarry Smith +   ts - the TS context
73656bc98fa9SBarry Smith .   time - the current time (of the stage)
73666bc98fa9SBarry Smith .   state - the state to check if it is valid
73676bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7368cb9d8021SPierre Barbier de Reuille 
7369cb9d8021SPierre Barbier de Reuille     Level: intermediate
7370cb9d8021SPierre Barbier de Reuille 
73716bc98fa9SBarry Smith     Notes:
73726bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
73736bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
73746bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
73756bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
73766bc98fa9SBarry Smith 
73776bc98fa9SBarry Smith     Developer Notes:
73786bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
73796bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
73806bc98fa9SBarry Smith 
73816bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7382cb9d8021SPierre Barbier de Reuille @*/
7383cb9d8021SPierre Barbier de Reuille 
7384d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7385cb9d8021SPierre Barbier de Reuille {
7386cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7387cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7388cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7389cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7390cb9d8021SPierre Barbier de Reuille }
7391cb9d8021SPierre Barbier de Reuille 
7392cb9d8021SPierre Barbier de Reuille /*@
73936bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7394cb9d8021SPierre Barbier de Reuille 
7395cb9d8021SPierre Barbier de Reuille     Input Parameters:
73966bc98fa9SBarry Smith +    ts - the TS context
73976bc98fa9SBarry Smith .    stagetime - time of the simulation
73986bc98fa9SBarry Smith -    Y - state vector to check.
7399cb9d8021SPierre Barbier de Reuille 
7400cb9d8021SPierre Barbier de Reuille     Output Parameter:
74016bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7402cb9d8021SPierre Barbier de Reuille 
7403cb9d8021SPierre Barbier de Reuille     Note:
74046bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74056bc98fa9SBarry Smith     to check if the current state is valid.
740696a0c994SBarry Smith 
74076bc98fa9SBarry Smith     Level: developer
74086bc98fa9SBarry Smith 
74096bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7410cb9d8021SPierre Barbier de Reuille @*/
7411d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7412cb9d8021SPierre Barbier de Reuille {
7413cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7414cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7415cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7416cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7417d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7418cb9d8021SPierre Barbier de Reuille   }
7419cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7420cb9d8021SPierre Barbier de Reuille }
74211ceb14c0SBarry Smith 
742293b34091SDebojyoti Ghosh /*@C
7423e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
742493b34091SDebojyoti Ghosh 
7425d083f849SBarry Smith   Collective
742693b34091SDebojyoti Ghosh 
742793b34091SDebojyoti Ghosh   Input Parameter:
742893b34091SDebojyoti Ghosh . tsin    - The input TS
742993b34091SDebojyoti Ghosh 
743093b34091SDebojyoti Ghosh   Output Parameter:
7431e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
743293b34091SDebojyoti Ghosh 
74335eca1a21SEmil Constantinescu   Notes:
74345eca1a21SEmil 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.
74355eca1a21SEmil Constantinescu 
7436928bb9adSStefano 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);
74375eca1a21SEmil Constantinescu 
74385eca1a21SEmil Constantinescu   Level: developer
743993b34091SDebojyoti Ghosh 
7440e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
744193b34091SDebojyoti Ghosh @*/
7442baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
744393b34091SDebojyoti Ghosh {
744493b34091SDebojyoti Ghosh   TS             t;
744593b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7446dc846ba4SSatish Balay   SNES           snes_start;
7447dc846ba4SSatish Balay   DM             dm;
7448dc846ba4SSatish Balay   TSType         type;
744993b34091SDebojyoti Ghosh 
745093b34091SDebojyoti Ghosh   PetscFunctionBegin;
745193b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
745293b34091SDebojyoti Ghosh   *tsout = NULL;
745393b34091SDebojyoti Ghosh 
74547a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
745593b34091SDebojyoti Ghosh 
745693b34091SDebojyoti Ghosh   /* General TS description */
745793b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
745893b34091SDebojyoti Ghosh   t->setupcalled       = 0;
745993b34091SDebojyoti Ghosh   t->ksp_its           = 0;
746093b34091SDebojyoti Ghosh   t->snes_its          = 0;
746193b34091SDebojyoti Ghosh   t->nwork             = 0;
74627d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
746393b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
746493b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
746593b34091SDebojyoti Ghosh 
746634561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
746734561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7468d15a3a53SEmil Constantinescu 
746993b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
747093b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
747193b34091SDebojyoti Ghosh 
747293b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
747351699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
747493b34091SDebojyoti Ghosh 
7475e7069c78SShri   t->trajectory = tsin->trajectory;
7476e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7477e7069c78SShri 
7478e7069c78SShri   t->event = tsin->event;
74796b10a48eSSatish Balay   if (t->event) t->event->refct++;
7480e7069c78SShri 
748193b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
748293b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7483e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
748493b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
748593b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
748693b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
748793b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
748893b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
748993b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
749093b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
749193b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
749293b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
749393b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
749493b34091SDebojyoti Ghosh 
749593b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
749693b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
749793b34091SDebojyoti Ghosh 
749893b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
749993b34091SDebojyoti Ghosh 
750093b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
750193b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
750293b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
750393b34091SDebojyoti Ghosh 
750493b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
750593b34091SDebojyoti Ghosh 
75060d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75070d4fed19SBarry Smith     PetscInt i;
75080d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75090d4fed19SBarry Smith     for (i=0; i<10; i++) {
75100d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75110d4fed19SBarry Smith     }
75120d4fed19SBarry Smith   }
751393b34091SDebojyoti Ghosh   *tsout = t;
751493b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
751593b34091SDebojyoti Ghosh }
7516f3b1f45cSBarry Smith 
7517f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7518f3b1f45cSBarry Smith {
7519f3b1f45cSBarry Smith   PetscErrorCode ierr;
7520f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7521f3b1f45cSBarry Smith 
7522f3b1f45cSBarry Smith   PetscFunctionBegin;
7523f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7524f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7525f3b1f45cSBarry Smith }
7526f3b1f45cSBarry Smith 
7527f3b1f45cSBarry Smith /*@
7528f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7529f3b1f45cSBarry Smith 
7530d083f849SBarry Smith    Logically Collective on TS
7531f3b1f45cSBarry Smith 
7532f3b1f45cSBarry Smith     Input Parameters:
7533f3b1f45cSBarry Smith     TS - the time stepping routine
7534f3b1f45cSBarry Smith 
7535f3b1f45cSBarry Smith    Output Parameter:
7536f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7537f3b1f45cSBarry Smith 
7538f3b1f45cSBarry Smith    Options Database:
7539f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7540f3b1f45cSBarry Smith 
7541f3b1f45cSBarry Smith    Level: advanced
7542f3b1f45cSBarry Smith 
754395452b02SPatrick Sanan    Notes:
754495452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7545f3b1f45cSBarry Smith 
7546f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7547f3b1f45cSBarry Smith @*/
7548f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7549f3b1f45cSBarry Smith {
7550f3b1f45cSBarry Smith   Mat            J,B;
7551f3b1f45cSBarry Smith   PetscErrorCode ierr;
7552f3b1f45cSBarry Smith   TSRHSJacobian  func;
7553f3b1f45cSBarry Smith   void*          ctx;
7554f3b1f45cSBarry Smith 
7555f3b1f45cSBarry Smith   PetscFunctionBegin;
7556f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7557f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7558f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7559f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7560f3b1f45cSBarry Smith }
7561f3b1f45cSBarry Smith 
7562f3b1f45cSBarry Smith /*@C
7563f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7564f3b1f45cSBarry Smith 
7565d083f849SBarry Smith    Logically Collective on TS
7566f3b1f45cSBarry Smith 
7567f3b1f45cSBarry Smith     Input Parameters:
7568f3b1f45cSBarry Smith     TS - the time stepping routine
7569f3b1f45cSBarry Smith 
7570f3b1f45cSBarry Smith    Output Parameter:
7571f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7572f3b1f45cSBarry Smith 
7573f3b1f45cSBarry Smith    Options Database:
7574f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7575f3b1f45cSBarry Smith 
757695452b02SPatrick Sanan    Notes:
757795452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7578f3b1f45cSBarry Smith 
7579f3b1f45cSBarry Smith    Level: advanced
7580f3b1f45cSBarry Smith 
7581f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7582f3b1f45cSBarry Smith @*/
7583f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7584f3b1f45cSBarry Smith {
7585f3b1f45cSBarry Smith   Mat            J,B;
7586f3b1f45cSBarry Smith   PetscErrorCode ierr;
7587f3b1f45cSBarry Smith   void           *ctx;
7588f3b1f45cSBarry Smith   TSRHSJacobian  func;
7589f3b1f45cSBarry Smith 
7590f3b1f45cSBarry Smith   PetscFunctionBegin;
7591f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7592f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7593f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7594f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7595f3b1f45cSBarry Smith }
75960fe4d17eSHong Zhang 
75970fe4d17eSHong Zhang /*@
75980fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
75990fe4d17eSHong Zhang 
76000fe4d17eSHong Zhang   Logically collective
76010fe4d17eSHong Zhang 
76020fe4d17eSHong Zhang   Input Parameter:
76030fe4d17eSHong Zhang +  ts - timestepping context
76040fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76050fe4d17eSHong Zhang 
76060fe4d17eSHong Zhang   Options Database:
76070fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76080fe4d17eSHong Zhang 
76090fe4d17eSHong Zhang   Notes:
76100fe4d17eSHong Zhang     This is only useful for multirate methods
76110fe4d17eSHong Zhang 
76120fe4d17eSHong Zhang   Level: intermediate
76130fe4d17eSHong Zhang 
76140fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
76150fe4d17eSHong Zhang @*/
76160fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
76170fe4d17eSHong Zhang {
76180fe4d17eSHong Zhang   PetscFunctionBegin;
76190fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76200fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
76210fe4d17eSHong Zhang   PetscFunctionReturn(0);
76220fe4d17eSHong Zhang }
76230fe4d17eSHong Zhang 
76240fe4d17eSHong Zhang /*@
76250fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
76260fe4d17eSHong Zhang 
76270fe4d17eSHong Zhang   Not collective
76280fe4d17eSHong Zhang 
76290fe4d17eSHong Zhang   Input Parameter:
76300fe4d17eSHong Zhang .  ts - timestepping context
76310fe4d17eSHong Zhang 
76320fe4d17eSHong Zhang   Output Parameter:
76330fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76340fe4d17eSHong Zhang 
76350fe4d17eSHong Zhang   Level: intermediate
76360fe4d17eSHong Zhang 
76370fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
76380fe4d17eSHong Zhang @*/
76390fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
76400fe4d17eSHong Zhang {
76410fe4d17eSHong Zhang   PetscFunctionBegin;
76420fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76430fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
76440fe4d17eSHong Zhang   PetscFunctionReturn(0);
76450fe4d17eSHong Zhang }
7646