xref: /petsc/src/ts/interface/ts.c (revision 5bd1e5768a3b141382a3ebb2e780cd2d3b3bfbf0)
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>
6d763cef2SBarry Smith 
71c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if(PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;
81c167fc2SEmil Constantinescu 
9d5ba7fb7SMatthew Knepley /* Logging support */
10d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
11a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
12d405a339SMatthew Knepley 
13feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1449354f04SShri Abhyankar 
151c167fc2SEmil Constantinescu 
16fde5950dSBarry Smith /*@C
17fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
18fde5950dSBarry Smith 
19fde5950dSBarry Smith    Collective on TS
20fde5950dSBarry Smith 
21fde5950dSBarry Smith    Input Parameters:
22fde5950dSBarry Smith +  ts - TS object you wish to monitor
23fde5950dSBarry Smith .  name - the monitor type one is seeking
24fde5950dSBarry Smith .  help - message indicating what monitoring is done
25fde5950dSBarry Smith .  manual - manual page for the monitor
26fde5950dSBarry Smith .  monitor - the monitor function
27fde5950dSBarry 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
28fde5950dSBarry Smith 
29fde5950dSBarry Smith    Level: developer
30fde5950dSBarry Smith 
31fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
32fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
33fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
34fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
35fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
36fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
37fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
38fde5950dSBarry Smith @*/
39721cd6eeSBarry 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*))
40fde5950dSBarry Smith {
41fde5950dSBarry Smith   PetscErrorCode    ierr;
42fde5950dSBarry Smith   PetscViewer       viewer;
43fde5950dSBarry Smith   PetscViewerFormat format;
44fde5950dSBarry Smith   PetscBool         flg;
45fde5950dSBarry Smith 
46fde5950dSBarry Smith   PetscFunctionBegin;
4716413a6aSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
48fde5950dSBarry Smith   if (flg) {
49721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
50721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
51721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
52fde5950dSBarry Smith     if (monitorsetup) {
53721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
54fde5950dSBarry Smith     }
55721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
56fde5950dSBarry Smith   }
57fde5950dSBarry Smith   PetscFunctionReturn(0);
58fde5950dSBarry Smith }
59fde5950dSBarry Smith 
602ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
612ffb9264SLisandro Dalcin {
622ffb9264SLisandro Dalcin   PetscErrorCode ierr;
632ffb9264SLisandro Dalcin 
642ffb9264SLisandro Dalcin   PetscFunctionBegin;
65b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
66b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
672ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
682ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
692ffb9264SLisandro Dalcin   }
702ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
712ffb9264SLisandro Dalcin }
722ffb9264SLisandro Dalcin 
73bdad233fSMatthew Knepley /*@
74bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
75bdad233fSMatthew Knepley 
76bdad233fSMatthew Knepley    Collective on TS
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley    Input Parameter:
79bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
80bdad233fSMatthew Knepley 
81bdad233fSMatthew Knepley    Options Database Keys:
82e49d4f37SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
83ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
84ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
85ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
86ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
874dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
883e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
89a3cdaa26SBarry 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
90a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
91a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
92a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
93a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
94a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
95f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
96f3b1f45cSBarry 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
97ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
98847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
99bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
100de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
101de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1027cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1036934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1048b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
105de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
106de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
107de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
108de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1093e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1103e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
111fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
112e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
1139e336e28SPatrick Sanan -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11453ea634cSHong Zhang 
1159e336e28SPatrick Sanan    Developer Note:
1169e336e28SPatrick Sanan    We should unify all the -ts_monitor options in the way that -xxx_view has been unified
117bdad233fSMatthew Knepley 
118bdad233fSMatthew Knepley    Level: beginner
119bdad233fSMatthew Knepley 
120a313700dSBarry Smith .seealso: TSGetType()
121bdad233fSMatthew Knepley @*/
1227087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
123bdad233fSMatthew Knepley {
124bc952696SBarry Smith   PetscBool              opt,flg,tflg;
125dfbe8321SBarry Smith   PetscErrorCode         ierr;
126eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
12731748224SBarry Smith   PetscReal              time_step;
12849354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
129d1212d36SBarry Smith   char                   dir[16];
130cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1316991f827SBarry Smith   const char             *defaultType;
1326991f827SBarry Smith   char                   typeName[256];
133bdad233fSMatthew Knepley 
134bdad233fSMatthew Knepley   PetscFunctionBegin;
1350700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1366991f827SBarry Smith 
1376991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
138cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
139cd11d68dSLisandro Dalcin 
140cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1411ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1421ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1436991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1446991f827SBarry Smith   if (opt) {
1456991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1466991f827SBarry Smith   } else {
1476991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1486991f827SBarry Smith   }
149bdad233fSMatthew Knepley 
150bdad233fSMatthew Knepley   /* Handle generic TS options */
1514dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
152ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15319eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1540298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
15531748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
156cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
15749354f04SShri 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);
15849354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1590298fd71SBarry 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);
1600298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1610298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1630298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
164bdad233fSMatthew Knepley 
165f3b1f45cSBarry 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);
166f3b1f45cSBarry 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);
1670fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr);
16856f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
16956f85f32SBarry Smith   {
17056f85f32SBarry Smith   PetscBool set;
17156f85f32SBarry Smith   flg  = PETSC_FALSE;
17256f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
17356f85f32SBarry Smith   if (set) {
17456f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
17556f85f32SBarry Smith   }
17656f85f32SBarry Smith   }
17756f85f32SBarry Smith #endif
17856f85f32SBarry Smith 
179bdad233fSMatthew Knepley   /* Monitor options */
180cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
181cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
182fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
183fde5950dSBarry Smith 
1845180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1855180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1865180491cSLisandro Dalcin 
1874f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
188b3603a34SBarry Smith   if (opt) {
1890b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1903923b477SBarry Smith     PetscInt       howoften = 1;
191b3603a34SBarry Smith 
1920298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
1936ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1944f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
195bdad233fSMatthew Knepley   }
1966ba87a44SLisandro Dalcin 
1974f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
198ef20d060SBarry Smith   if (opt) {
1990b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2003923b477SBarry Smith     PetscInt       howoften = 1;
201ef20d060SBarry Smith 
2020298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2036ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2044f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
205ef20d060SBarry Smith   }
206edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2077cf37e64SBarry Smith 
2086934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2096934998bSLisandro Dalcin   if (opt) {
2106934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2116934998bSLisandro Dalcin     PetscInt       howoften = 1;
2126934998bSLisandro Dalcin 
2136934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2146ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2156934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2166934998bSLisandro Dalcin   }
2178b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2188b668821SLisandro Dalcin   if (opt) {
2198b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2208b668821SLisandro Dalcin     PetscInt       howoften = 1;
2218b668821SLisandro Dalcin 
2228b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2238b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2248b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2258b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2268b668821SLisandro Dalcin   }
2278b668821SLisandro Dalcin 
228201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
229201da799SBarry Smith   if (opt) {
230201da799SBarry Smith     TSMonitorLGCtx ctx;
231201da799SBarry Smith     PetscInt       howoften = 1;
232201da799SBarry Smith 
2330298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2346ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
235201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
236201da799SBarry Smith   }
237201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
238201da799SBarry Smith   if (opt) {
239201da799SBarry Smith     TSMonitorLGCtx ctx;
240201da799SBarry Smith     PetscInt       howoften = 1;
241201da799SBarry Smith 
2420298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2436ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
244201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
245201da799SBarry Smith   }
2468189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2478189c53fSBarry Smith   if (opt) {
2488189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2498189c53fSBarry Smith     PetscInt          howoften = 1;
2508189c53fSBarry Smith 
2510298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2526934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2538189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2548189c53fSBarry Smith   }
2550ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2561b575b74SJoseph Pusztay   if (opt) {
2571b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2581b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2590ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2601b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2610ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2621b575b74SJoseph Pusztay   }
263ef20d060SBarry Smith   opt  = PETSC_FALSE;
2640dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
265a7cc72afSBarry Smith   if (opt) {
26683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
26783a4ac43SBarry Smith     PetscInt         howoften = 1;
268a80ad3e0SBarry Smith 
2690298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2700ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
27183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
272bdad233fSMatthew Knepley   }
273fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2742d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2752d5ee99bSBarry Smith   if (opt) {
2762d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2772d5ee99bSBarry Smith     PetscReal        bounds[4];
2782d5ee99bSBarry Smith     PetscInt         n = 4;
2792d5ee99bSBarry Smith     PetscDraw        draw;
2806934998bSLisandro Dalcin     PetscDrawAxis    axis;
2812d5ee99bSBarry Smith 
2822d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2832d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2846934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2852d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2866934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2876934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2886934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2892d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2902d5ee99bSBarry Smith   }
2912d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2920dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2933a471f94SBarry Smith   if (opt) {
29483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
29583a4ac43SBarry Smith     PetscInt         howoften = 1;
2963a471f94SBarry Smith 
2970298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
2980ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
29983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3003a471f94SBarry Smith   }
3010ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3020ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3030ed3bfb6SBarry Smith   if (opt) {
3040ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3050ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3060ed3bfb6SBarry Smith 
3070ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
3080ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3090ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3100ed3bfb6SBarry Smith   }
311fde5950dSBarry Smith 
312ed81e22dSJed Brown   opt  = PETSC_FALSE;
31391b97e58SBarry 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);
314ed81e22dSJed Brown   if (flg) {
315ed81e22dSJed Brown     const char *ptr,*ptr2;
316ed81e22dSJed Brown     char       *filetemplate;
317ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
318ed81e22dSJed Brown     /* Do some cursory validation of the input. */
319ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
320ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
321ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
322ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
323ce94432eSBarry 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");
324ed81e22dSJed Brown       if (ptr2) break;
325ed81e22dSJed Brown     }
326ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
327ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
328ed81e22dSJed Brown   }
329bdad233fSMatthew Knepley 
330d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
331d1212d36SBarry Smith   if (flg) {
332d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
333d1212d36SBarry Smith     int                  ray = 0;
334d1212d36SBarry Smith     DMDADirection        ddir;
335d1212d36SBarry Smith     DM                   da;
336d1212d36SBarry Smith     PetscMPIInt          rank;
337d1212d36SBarry Smith 
338ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
339d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
340d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
341ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
342d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
343d1212d36SBarry Smith 
344*5bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr);
345b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
346d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
347d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
348ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
349d1212d36SBarry Smith     if (!rank) {
350d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
351d1212d36SBarry Smith     }
35251b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
353d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
354d1212d36SBarry Smith   }
35551b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
35651b4a12fSMatthew G. Knepley   if (flg) {
35751b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
35851b4a12fSMatthew G. Knepley     int                 ray = 0;
35951b4a12fSMatthew G. Knepley     DMDADirection       ddir;
36051b4a12fSMatthew G. Knepley     DM                  da;
36151b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
362d1212d36SBarry Smith 
36351b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
36451b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
36551b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
36651b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
36751b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3681c3436cfSJed Brown 
369*5bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr);
370b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
37151b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
37251b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
37351b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
37451b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
37551b4a12fSMatthew G. Knepley   }
376a7a1495cSBarry Smith 
377b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
378b3d3934dSBarry Smith   if (opt) {
379b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
380b3d3934dSBarry Smith 
381b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
382b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
383b3d3934dSBarry Smith   }
384b3d3934dSBarry Smith 
385847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
386847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
387847ff0e1SMatthew G. Knepley   if (flg) {
388847ff0e1SMatthew G. Knepley     DM   dm;
389847ff0e1SMatthew G. Knepley     DMTS tdm;
390847ff0e1SMatthew G. Knepley 
391847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
392847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
393847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
394847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
395847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
396847ff0e1SMatthew G. Knepley   }
397847ff0e1SMatthew G. Knepley 
398d763cef2SBarry Smith   /* Handle specific TS options */
399d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4006991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
401d763cef2SBarry Smith   }
402fbc52257SHong Zhang 
403a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
404a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
405a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
406a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
407a7bdc993SLisandro Dalcin 
40868bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4094f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
41068bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
41168bece0bSHong Zhang   if (tflg) {
41268bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
41368bece0bSHong Zhang   }
414a05bf03eSHong Zhang 
415a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
416d763cef2SBarry Smith 
417d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4180633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
419fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
420d763cef2SBarry Smith 
4214f122a70SLisandro Dalcin   if (ts->trajectory) {
4224f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
423d763cef2SBarry Smith   }
42468bece0bSHong Zhang 
4251ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4264f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4271ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4281ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4291ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4301ef27442SStefano Zampini   }
4314f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
432d763cef2SBarry Smith   PetscFunctionReturn(0);
433d763cef2SBarry Smith }
434d763cef2SBarry Smith 
435d2daff3dSHong Zhang /*@
43678fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
43778fbdcc8SBarry Smith 
43878fbdcc8SBarry Smith    Collective on TS
43978fbdcc8SBarry Smith 
44078fbdcc8SBarry Smith    Input Parameters:
44178fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
44278fbdcc8SBarry Smith 
44378fbdcc8SBarry Smith    Output Parameters;
44478fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
44578fbdcc8SBarry Smith 
44678fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
44778fbdcc8SBarry Smith 
44878fbdcc8SBarry Smith    Level: advanced
44978fbdcc8SBarry Smith 
45078fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
45178fbdcc8SBarry Smith 
45278fbdcc8SBarry Smith @*/
45378fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
45478fbdcc8SBarry Smith {
45578fbdcc8SBarry Smith   PetscFunctionBegin;
45678fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45778fbdcc8SBarry Smith   *tr = ts->trajectory;
45878fbdcc8SBarry Smith   PetscFunctionReturn(0);
45978fbdcc8SBarry Smith }
46078fbdcc8SBarry Smith 
46178fbdcc8SBarry Smith /*@
462bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
463d2daff3dSHong Zhang 
464d2daff3dSHong Zhang    Collective on TS
465d2daff3dSHong Zhang 
466d2daff3dSHong Zhang    Input Parameters:
467bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
468bc952696SBarry Smith 
46978fbdcc8SBarry Smith    Options Database:
47078fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
47178fbdcc8SBarry Smith -  -ts_trajectory_type type
47278fbdcc8SBarry Smith 
47368bece0bSHong Zhang Note: This routine should be called after all TS options have been set
474d2daff3dSHong Zhang 
475c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
47678fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
47778fbdcc8SBarry Smith 
478d2daff3dSHong Zhang    Level: intermediate
479d2daff3dSHong Zhang 
4802d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
481d2daff3dSHong Zhang 
482d2daff3dSHong Zhang @*/
483bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
484d2daff3dSHong Zhang {
485bc952696SBarry Smith   PetscErrorCode ierr;
486bc952696SBarry Smith 
487d2daff3dSHong Zhang   PetscFunctionBegin;
488d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
489bc952696SBarry Smith   if (!ts->trajectory) {
490bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
491bc952696SBarry Smith   }
492d2daff3dSHong Zhang   PetscFunctionReturn(0);
493d2daff3dSHong Zhang }
494d2daff3dSHong Zhang 
495a7a1495cSBarry Smith /*@
4962d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
4972d29f1f2SStefano Zampini 
4982d29f1f2SStefano Zampini    Collective on TS
4992d29f1f2SStefano Zampini 
5002d29f1f2SStefano Zampini    Input Parameters:
5012d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5022d29f1f2SStefano Zampini 
5032d29f1f2SStefano Zampini    Level: intermediate
5042d29f1f2SStefano Zampini 
5052d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5062d29f1f2SStefano Zampini 
5072d29f1f2SStefano Zampini @*/
5082d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5092d29f1f2SStefano Zampini {
5102d29f1f2SStefano Zampini   PetscErrorCode ierr;
5112d29f1f2SStefano Zampini 
5122d29f1f2SStefano Zampini   PetscFunctionBegin;
5132d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5142d29f1f2SStefano Zampini   if (ts->trajectory) {
5152d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5162d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5172d29f1f2SStefano Zampini   }
5182d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5192d29f1f2SStefano Zampini }
5202d29f1f2SStefano Zampini 
5212d29f1f2SStefano Zampini /*@
522a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
523a7a1495cSBarry Smith       set with TSSetRHSJacobian().
524a7a1495cSBarry Smith 
525d083f849SBarry Smith    Collective on TS
526a7a1495cSBarry Smith 
527a7a1495cSBarry Smith    Input Parameters:
528316643e7SJed Brown +  ts - the TS context
529a7a1495cSBarry Smith .  t - current timestep
5300910c330SBarry Smith -  U - input vector
531a7a1495cSBarry Smith 
532a7a1495cSBarry Smith    Output Parameters:
533a7a1495cSBarry Smith +  A - Jacobian matrix
534a7a1495cSBarry Smith .  B - optional preconditioning matrix
535a7a1495cSBarry Smith -  flag - flag indicating matrix structure
536a7a1495cSBarry Smith 
537a7a1495cSBarry Smith    Notes:
538a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
539a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
540a7a1495cSBarry Smith 
54194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
542a7a1495cSBarry Smith    flag parameter.
543a7a1495cSBarry Smith 
544a7a1495cSBarry Smith    Level: developer
545a7a1495cSBarry Smith 
54694b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
547a7a1495cSBarry Smith @*/
548d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
549a7a1495cSBarry Smith {
550dfbe8321SBarry Smith   PetscErrorCode   ierr;
551270bf2e7SJed Brown   PetscObjectState Ustate;
5526c1e1eecSBarry Smith   PetscObjectId    Uid;
55324989b8cSPeter Brune   DM               dm;
554942e3340SBarry Smith   DMTS             tsdm;
55524989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
55624989b8cSPeter Brune   void             *ctx;
55724989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
558b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
559a7a1495cSBarry Smith 
560a7a1495cSBarry Smith   PetscFunctionBegin;
5610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5620910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5630910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
56424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
565942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
56624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5670298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
568b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
56959e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5706c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
571971015bcSStefano Zampini 
5726c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
573971015bcSStefano Zampini     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
574971015bcSStefano Zampini     if (A == ts->Arhs) {
575971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
576971015bcSStefano Zampini         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
577971015bcSStefano Zampini       }
578971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
579971015bcSStefano Zampini         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
580971015bcSStefano Zampini       }
581971015bcSStefano Zampini     }
582971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
583971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
584971015bcSStefano Zampini         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
585971015bcSStefano Zampini       }
586971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
587971015bcSStefano Zampini         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
588971015bcSStefano Zampini       }
589971015bcSStefano Zampini     }
590971015bcSStefano Zampini     ts->rhsjacobian.shift = 0;
591971015bcSStefano Zampini     ts->rhsjacobian.scale = 1.;
5920e4ef248SJed Brown     PetscFunctionReturn(0);
593f8ede8e7SJed Brown   }
594d90be118SSean Farley 
595ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
596d90be118SSean Farley 
597e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
598971015bcSStefano Zampini     if (A == ts->Arhs) {
599971015bcSStefano Zampini       /* MatScale has a short path for this case.
600971015bcSStefano Zampini          However, this code path is taken the first time TSComputeRHSJacobian is called
601971015bcSStefano Zampini          and the matrices have not assembled yet */
602971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
60394ab13aaSBarry Smith         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
604971015bcSStefano Zampini       }
605971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
60694ab13aaSBarry Smith         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
607971015bcSStefano Zampini       }
608971015bcSStefano Zampini     }
609971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
610971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
61194ab13aaSBarry Smith         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
612971015bcSStefano Zampini       }
613971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
61494ab13aaSBarry Smith         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
615e1244c69SJed Brown       }
616971015bcSStefano Zampini     }
617e1244c69SJed Brown   }
618e1244c69SJed Brown 
61924989b8cSPeter Brune   if (rhsjacobianfunc) {
6206cd88445SBarry Smith     PetscBool missing;
62194ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
622a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
623d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
624a7a1495cSBarry Smith     PetscStackPop;
62594ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6266cd88445SBarry Smith     if (A) {
6276cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6286cd88445SBarry 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");
6296cd88445SBarry Smith     }
6306cd88445SBarry Smith     if (B && B != A) {
6316cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6326cd88445SBarry 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");
6336cd88445SBarry Smith     }
634ef66eb69SBarry Smith   } else {
63594ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
636971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
637ef66eb69SBarry Smith   }
6380e4ef248SJed Brown   ts->rhsjacobian.time  = t;
639971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
640971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6417f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
64259e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
643a7a1495cSBarry Smith   PetscFunctionReturn(0);
644a7a1495cSBarry Smith }
645a7a1495cSBarry Smith 
646316643e7SJed Brown /*@
647d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
648d763cef2SBarry Smith 
649d083f849SBarry Smith    Collective on TS
650316643e7SJed Brown 
651316643e7SJed Brown    Input Parameters:
652316643e7SJed Brown +  ts - the TS context
653316643e7SJed Brown .  t - current time
6540910c330SBarry Smith -  U - state vector
655316643e7SJed Brown 
656316643e7SJed Brown    Output Parameter:
657316643e7SJed Brown .  y - right hand side
658316643e7SJed Brown 
659316643e7SJed Brown    Note:
660316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
661316643e7SJed Brown    is used internally within the nonlinear solvers.
662316643e7SJed Brown 
663316643e7SJed Brown    Level: developer
664316643e7SJed Brown 
665316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
666316643e7SJed Brown @*/
6670910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
668d763cef2SBarry Smith {
669dfbe8321SBarry Smith   PetscErrorCode ierr;
67024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
67124989b8cSPeter Brune   TSIFunction    ifunction;
67224989b8cSPeter Brune   void           *ctx;
67324989b8cSPeter Brune   DM             dm;
67424989b8cSPeter Brune 
675d763cef2SBarry Smith   PetscFunctionBegin;
6760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6780700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
67924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
68024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6810298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
682d763cef2SBarry Smith 
683ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
684d763cef2SBarry Smith 
6850910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
68624989b8cSPeter Brune   if (rhsfunction) {
687d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6880910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
689d763cef2SBarry Smith     PetscStackPop;
690214bc6a2SJed Brown   } else {
691214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
692b2cd27e8SJed Brown   }
69344a41b28SSean Farley 
6940910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
695d763cef2SBarry Smith   PetscFunctionReturn(0);
696d763cef2SBarry Smith }
697d763cef2SBarry Smith 
698ef20d060SBarry Smith /*@
699ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
700ef20d060SBarry Smith 
701d083f849SBarry Smith    Collective on TS
702ef20d060SBarry Smith 
703ef20d060SBarry Smith    Input Parameters:
704ef20d060SBarry Smith +  ts - the TS context
705ef20d060SBarry Smith -  t - current time
706ef20d060SBarry Smith 
707ef20d060SBarry Smith    Output Parameter:
7080910c330SBarry Smith .  U - the solution
709ef20d060SBarry Smith 
710ef20d060SBarry Smith    Note:
711ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
712ef20d060SBarry Smith    is used internally within the nonlinear solvers.
713ef20d060SBarry Smith 
714ef20d060SBarry Smith    Level: developer
715ef20d060SBarry Smith 
716abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
717ef20d060SBarry Smith @*/
7180910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
719ef20d060SBarry Smith {
720ef20d060SBarry Smith   PetscErrorCode     ierr;
721ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
722ef20d060SBarry Smith   void               *ctx;
723ef20d060SBarry Smith   DM                 dm;
724ef20d060SBarry Smith 
725ef20d060SBarry Smith   PetscFunctionBegin;
726ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
728ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
729ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
730ef20d060SBarry Smith 
731ef20d060SBarry Smith   if (solutionfunction) {
7329b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7330910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
734ef20d060SBarry Smith     PetscStackPop;
735ef20d060SBarry Smith   }
736ef20d060SBarry Smith   PetscFunctionReturn(0);
737ef20d060SBarry Smith }
7389b7cd975SBarry Smith /*@
7399b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7409b7cd975SBarry Smith 
741d083f849SBarry Smith    Collective on TS
7429b7cd975SBarry Smith 
7439b7cd975SBarry Smith    Input Parameters:
7449b7cd975SBarry Smith +  ts - the TS context
7459b7cd975SBarry Smith -  t - current time
7469b7cd975SBarry Smith 
7479b7cd975SBarry Smith    Output Parameter:
7489b7cd975SBarry Smith .  U - the function value
7499b7cd975SBarry Smith 
7509b7cd975SBarry Smith    Note:
7519b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7529b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7539b7cd975SBarry Smith 
7549b7cd975SBarry Smith    Level: developer
7559b7cd975SBarry Smith 
7569b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7579b7cd975SBarry Smith @*/
7589b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7599b7cd975SBarry Smith {
7609b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7619b7cd975SBarry Smith   void               *ctx;
7629b7cd975SBarry Smith   DM                 dm;
7639b7cd975SBarry Smith 
7649b7cd975SBarry Smith   PetscFunctionBegin;
7659b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7669b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7679b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7689b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7699b7cd975SBarry Smith 
7709b7cd975SBarry Smith   if (forcing) {
7719b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7729b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7739b7cd975SBarry Smith     PetscStackPop;
7749b7cd975SBarry Smith   }
7759b7cd975SBarry Smith   PetscFunctionReturn(0);
7769b7cd975SBarry Smith }
777ef20d060SBarry Smith 
778214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
779214bc6a2SJed Brown {
7802dd45cf8SJed Brown   Vec            F;
781214bc6a2SJed Brown   PetscErrorCode ierr;
782214bc6a2SJed Brown 
783214bc6a2SJed Brown   PetscFunctionBegin;
7840298fd71SBarry Smith   *Frhs = NULL;
7850298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
786214bc6a2SJed Brown   if (!ts->Frhs) {
7872dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
788214bc6a2SJed Brown   }
789214bc6a2SJed Brown   *Frhs = ts->Frhs;
790214bc6a2SJed Brown   PetscFunctionReturn(0);
791214bc6a2SJed Brown }
792214bc6a2SJed Brown 
793971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
794214bc6a2SJed Brown {
795214bc6a2SJed Brown   Mat            A,B;
7962dd45cf8SJed Brown   PetscErrorCode ierr;
79741a1d4d2SBarry Smith   TSIJacobian    ijacobian;
798214bc6a2SJed Brown 
799214bc6a2SJed Brown   PetscFunctionBegin;
800c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
801c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
80241a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
803214bc6a2SJed Brown   if (Arhs) {
804214bc6a2SJed Brown     if (!ts->Arhs) {
80541a1d4d2SBarry Smith       if (ijacobian) {
806214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
80741a1d4d2SBarry Smith       } else {
80841a1d4d2SBarry Smith         ts->Arhs = A;
80941a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
81041a1d4d2SBarry Smith       }
8113565c898SBarry Smith     } else {
8123565c898SBarry Smith       PetscBool flg;
8133565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8143565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8153565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8163565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8173565c898SBarry Smith         ts->Arhs = A;
8183565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8193565c898SBarry Smith       }
820214bc6a2SJed Brown     }
821214bc6a2SJed Brown     *Arhs = ts->Arhs;
822214bc6a2SJed Brown   }
823214bc6a2SJed Brown   if (Brhs) {
824214bc6a2SJed Brown     if (!ts->Brhs) {
825bdb70873SJed Brown       if (A != B) {
82641a1d4d2SBarry Smith         if (ijacobian) {
827214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
828bdb70873SJed Brown         } else {
82941a1d4d2SBarry Smith           ts->Brhs = B;
83041a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
83141a1d4d2SBarry Smith         }
83241a1d4d2SBarry Smith       } else {
833bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
83451699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
835bdb70873SJed Brown       }
836214bc6a2SJed Brown     }
837214bc6a2SJed Brown     *Brhs = ts->Brhs;
838214bc6a2SJed Brown   }
839214bc6a2SJed Brown   PetscFunctionReturn(0);
840214bc6a2SJed Brown }
841214bc6a2SJed Brown 
842316643e7SJed Brown /*@
8430910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
844316643e7SJed Brown 
845d083f849SBarry Smith    Collective on TS
846316643e7SJed Brown 
847316643e7SJed Brown    Input Parameters:
848316643e7SJed Brown +  ts - the TS context
849316643e7SJed Brown .  t - current time
8500910c330SBarry Smith .  U - state vector
8510910c330SBarry Smith .  Udot - time derivative of state vector
852214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
853316643e7SJed Brown 
854316643e7SJed Brown    Output Parameter:
855316643e7SJed Brown .  Y - right hand side
856316643e7SJed Brown 
857316643e7SJed Brown    Note:
858316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
859316643e7SJed Brown    is used internally within the nonlinear solvers.
860316643e7SJed Brown 
861316643e7SJed Brown    If the user did did not write their equations in implicit form, this
862316643e7SJed Brown    function recasts them in implicit form.
863316643e7SJed Brown 
864316643e7SJed Brown    Level: developer
865316643e7SJed Brown 
866316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
867316643e7SJed Brown @*/
8680910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
869316643e7SJed Brown {
870316643e7SJed Brown   PetscErrorCode ierr;
87124989b8cSPeter Brune   TSIFunction    ifunction;
87224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
87324989b8cSPeter Brune   void           *ctx;
87424989b8cSPeter Brune   DM             dm;
875316643e7SJed Brown 
876316643e7SJed Brown   PetscFunctionBegin;
8770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8780910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8790910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8800700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
881316643e7SJed Brown 
88224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8840298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
88524989b8cSPeter Brune 
886ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
887d90be118SSean Farley 
8880910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
88924989b8cSPeter Brune   if (ifunction) {
890316643e7SJed Brown     PetscStackPush("TS user implicit function");
8910910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
892316643e7SJed Brown     PetscStackPop;
893214bc6a2SJed Brown   }
894214bc6a2SJed Brown   if (imex) {
89524989b8cSPeter Brune     if (!ifunction) {
8960910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8972dd45cf8SJed Brown     }
89824989b8cSPeter Brune   } else if (rhsfunction) {
89924989b8cSPeter Brune     if (ifunction) {
900214bc6a2SJed Brown       Vec Frhs;
901214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9020910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
903214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9042dd45cf8SJed Brown     } else {
9050910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9060910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
907316643e7SJed Brown     }
9084a6899ffSJed Brown   }
9090910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
910316643e7SJed Brown   PetscFunctionReturn(0);
911316643e7SJed Brown }
912316643e7SJed Brown 
913316643e7SJed Brown /*@
914316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
915316643e7SJed Brown 
916d083f849SBarry Smith    Collective on TS
917316643e7SJed Brown 
918316643e7SJed Brown    Input
919316643e7SJed Brown       Input Parameters:
920316643e7SJed Brown +  ts - the TS context
921316643e7SJed Brown .  t - current timestep
9220910c330SBarry Smith .  U - state vector
9230910c330SBarry Smith .  Udot - time derivative of state vector
924214bc6a2SJed Brown .  shift - shift to apply, see note below
925214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
926316643e7SJed Brown 
927316643e7SJed Brown    Output Parameters:
928316643e7SJed Brown +  A - Jacobian matrix
9293565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
930316643e7SJed Brown 
931316643e7SJed Brown    Notes:
9320910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
933316643e7SJed Brown 
9340910c330SBarry Smith    dF/dU + shift*dF/dUdot
935316643e7SJed Brown 
936316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
937316643e7SJed Brown    is used internally within the nonlinear solvers.
938316643e7SJed Brown 
939316643e7SJed Brown    Level: developer
940316643e7SJed Brown 
941316643e7SJed Brown .seealso:  TSSetIJacobian()
94263495f91SJed Brown @*/
943d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
944316643e7SJed Brown {
945316643e7SJed Brown   PetscErrorCode ierr;
94624989b8cSPeter Brune   TSIJacobian    ijacobian;
94724989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
94824989b8cSPeter Brune   DM             dm;
94924989b8cSPeter Brune   void           *ctx;
950316643e7SJed Brown 
951316643e7SJed Brown   PetscFunctionBegin;
9520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9530910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9540910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
955316643e7SJed Brown   PetscValidPointer(A,6);
95694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
957316643e7SJed Brown   PetscValidPointer(B,7);
95894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
95924989b8cSPeter Brune 
96024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9620298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96324989b8cSPeter Brune 
964ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
965316643e7SJed Brown 
96694ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
96724989b8cSPeter Brune   if (ijacobian) {
9686cd88445SBarry Smith     PetscBool missing;
969316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
970d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
971316643e7SJed Brown     PetscStackPop;
9726cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9736cd88445SBarry 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");
9743565c898SBarry Smith     if (B != A) {
9756cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9766cd88445SBarry 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");
9776cd88445SBarry Smith     }
9784a6899ffSJed Brown   }
979214bc6a2SJed Brown   if (imex) {
980b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9814c26be97Sstefano_zampini       PetscBool assembled;
982971015bcSStefano Zampini       if (rhsjacobian) {
983971015bcSStefano Zampini         Mat Arhs = NULL;
984971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
985971015bcSStefano Zampini         if (A == Arhs) {
986971015bcSStefano Zampini           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant");
987971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
988971015bcSStefano Zampini         }
989971015bcSStefano Zampini       }
99094ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9914c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9924c26be97Sstefano_zampini       if (!assembled) {
9934c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9944c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9954c26be97Sstefano_zampini       }
99694ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99794ab13aaSBarry Smith       if (A != B) {
99894ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9994c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
10004c26be97Sstefano_zampini         if (!assembled) {
10014c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10024c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10034c26be97Sstefano_zampini         }
100494ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1005214bc6a2SJed Brown       }
1006214bc6a2SJed Brown     }
1007214bc6a2SJed Brown   } else {
1008e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1009e1244c69SJed Brown     if (rhsjacobian) {
1010214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1011d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1012e1244c69SJed Brown     }
101394ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10143565c898SBarry Smith       PetscBool flg;
1015e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1016e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10173565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10183565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10193565c898SBarry Smith       if (!flg) {
102094ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
102194ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10223565c898SBarry Smith       }
102394ab13aaSBarry Smith       if (A != B) {
102494ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
102594ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1026316643e7SJed Brown       }
1027e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1028e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1029e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
103094ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
103194ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
103294ab13aaSBarry Smith         if (A != B) {
103394ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
103494ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1035214bc6a2SJed Brown         }
1036316643e7SJed Brown       }
103794ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
103894ab13aaSBarry Smith       if (A != B) {
103994ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1040316643e7SJed Brown       }
1041316643e7SJed Brown     }
1042316643e7SJed Brown   }
104394ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1044316643e7SJed Brown   PetscFunctionReturn(0);
1045316643e7SJed Brown }
1046316643e7SJed Brown 
1047d763cef2SBarry Smith /*@C
1048d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1049b5abc632SBarry Smith     where U_t = G(t,u).
1050d763cef2SBarry Smith 
10513f9fe445SBarry Smith     Logically Collective on TS
1052d763cef2SBarry Smith 
1053d763cef2SBarry Smith     Input Parameters:
1054d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10550298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1056d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1057d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10580298fd71SBarry Smith           function evaluation routine (may be NULL)
1059d763cef2SBarry Smith 
1060d763cef2SBarry Smith     Calling sequence of func:
10616bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1062d763cef2SBarry Smith 
1063d763cef2SBarry Smith +   t - current timestep
1064d763cef2SBarry Smith .   u - input vector
1065d763cef2SBarry Smith .   F - function vector
1066d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1067d763cef2SBarry Smith 
1068d763cef2SBarry Smith     Level: beginner
1069d763cef2SBarry Smith 
107095452b02SPatrick Sanan     Notes:
107195452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10722bbac0d3SBarry Smith 
1073ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1074d763cef2SBarry Smith @*/
1075089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1076d763cef2SBarry Smith {
1077089b2837SJed Brown   PetscErrorCode ierr;
1078089b2837SJed Brown   SNES           snes;
10790298fd71SBarry Smith   Vec            ralloc = NULL;
108024989b8cSPeter Brune   DM             dm;
1081d763cef2SBarry Smith 
1082089b2837SJed Brown   PetscFunctionBegin;
10830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1084ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
108524989b8cSPeter Brune 
108624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1088089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1089e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1090e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1091e856ceecSJed Brown     r = ralloc;
1092e856ceecSJed Brown   }
1093089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1094e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1095d763cef2SBarry Smith   PetscFunctionReturn(0);
1096d763cef2SBarry Smith }
1097d763cef2SBarry Smith 
1098ef20d060SBarry Smith /*@C
1099abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1100ef20d060SBarry Smith 
1101ef20d060SBarry Smith     Logically Collective on TS
1102ef20d060SBarry Smith 
1103ef20d060SBarry Smith     Input Parameters:
1104ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1105ef20d060SBarry Smith .   f - routine for evaluating the solution
1106ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11070298fd71SBarry Smith           function evaluation routine (may be NULL)
1108ef20d060SBarry Smith 
1109ef20d060SBarry Smith     Calling sequence of func:
11106bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec u,void *ctx);
1111ef20d060SBarry Smith 
1112ef20d060SBarry Smith +   t - current timestep
1113ef20d060SBarry Smith .   u - output vector
1114ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1115ef20d060SBarry Smith 
11160ed3bfb6SBarry Smith     Options Database:
11170ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11180ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11190ed3bfb6SBarry Smith 
1120abd5a294SJed Brown     Notes:
1121abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1122abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1123abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1124abd5a294SJed Brown 
11254f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1126abd5a294SJed Brown 
1127ef20d060SBarry Smith     Level: beginner
1128ef20d060SBarry Smith 
11290ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1130ef20d060SBarry Smith @*/
1131ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1132ef20d060SBarry Smith {
1133ef20d060SBarry Smith   PetscErrorCode ierr;
1134ef20d060SBarry Smith   DM             dm;
1135ef20d060SBarry Smith 
1136ef20d060SBarry Smith   PetscFunctionBegin;
1137ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1138ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1139ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1140ef20d060SBarry Smith   PetscFunctionReturn(0);
1141ef20d060SBarry Smith }
1142ef20d060SBarry Smith 
11439b7cd975SBarry Smith /*@C
11449b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11459b7cd975SBarry Smith 
11469b7cd975SBarry Smith     Logically Collective on TS
11479b7cd975SBarry Smith 
11489b7cd975SBarry Smith     Input Parameters:
11499b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1150e162b725SBarry Smith .   func - routine for evaluating the forcing function
11519b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11520298fd71SBarry Smith           function evaluation routine (may be NULL)
11539b7cd975SBarry Smith 
11549b7cd975SBarry Smith     Calling sequence of func:
11556bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11569b7cd975SBarry Smith 
11579b7cd975SBarry Smith +   t - current timestep
1158e162b725SBarry Smith .   f - output vector
11599b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11609b7cd975SBarry Smith 
11619b7cd975SBarry Smith     Notes:
11629b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1163e162b725SBarry 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
1164e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1165e162b725SBarry Smith 
1166e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1167e162b725SBarry Smith 
1168e162b725SBarry 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
1169e162b725SBarry Smith     parameters can be passed in the ctx variable.
11709b7cd975SBarry Smith 
11719b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11729b7cd975SBarry Smith 
11739b7cd975SBarry Smith     Level: beginner
11749b7cd975SBarry Smith 
11759b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11769b7cd975SBarry Smith @*/
1177e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11789b7cd975SBarry Smith {
11799b7cd975SBarry Smith   PetscErrorCode ierr;
11809b7cd975SBarry Smith   DM             dm;
11819b7cd975SBarry Smith 
11829b7cd975SBarry Smith   PetscFunctionBegin;
11839b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11849b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1185e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11869b7cd975SBarry Smith   PetscFunctionReturn(0);
11879b7cd975SBarry Smith }
11889b7cd975SBarry Smith 
1189d763cef2SBarry Smith /*@C
1190f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1191b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1192d763cef2SBarry Smith 
11933f9fe445SBarry Smith    Logically Collective on TS
1194d763cef2SBarry Smith 
1195d763cef2SBarry Smith    Input Parameters:
1196d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1197e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1198e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1199d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1200d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12010298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1202d763cef2SBarry Smith 
1203f7ab8db6SBarry Smith    Calling sequence of f:
12046bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1205d763cef2SBarry Smith 
1206d763cef2SBarry Smith +  t - current timestep
1207d763cef2SBarry Smith .  u - input vector
1208e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1209e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1210d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1211d763cef2SBarry Smith 
12126cd88445SBarry Smith    Notes:
12136cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12146cd88445SBarry Smith 
12156cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1216ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1217d763cef2SBarry Smith 
1218d763cef2SBarry Smith    Level: beginner
1219d763cef2SBarry Smith 
1220ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1221d763cef2SBarry Smith 
1222d763cef2SBarry Smith @*/
1223e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1224d763cef2SBarry Smith {
1225277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1226089b2837SJed Brown   SNES           snes;
122724989b8cSPeter Brune   DM             dm;
122824989b8cSPeter Brune   TSIJacobian    ijacobian;
1229277b19d0SLisandro Dalcin 
1230d763cef2SBarry Smith   PetscFunctionBegin;
12310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1232e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1233e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1234e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1235e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1236d763cef2SBarry Smith 
123724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
123824989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1239e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1240e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1241e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1242e1244c69SJed Brown   }
12430298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1244089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12455f659677SPeter Brune   if (!ijacobian) {
1246e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12470e4ef248SJed Brown   }
1248e5d3d808SBarry Smith   if (Amat) {
1249e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12500e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1251e5d3d808SBarry Smith     ts->Arhs = Amat;
12520e4ef248SJed Brown   }
1253e5d3d808SBarry Smith   if (Pmat) {
1254e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12550e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1256e5d3d808SBarry Smith     ts->Brhs = Pmat;
12570e4ef248SJed Brown   }
1258d763cef2SBarry Smith   PetscFunctionReturn(0);
1259d763cef2SBarry Smith }
1260d763cef2SBarry Smith 
1261316643e7SJed Brown /*@C
1262b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1263316643e7SJed Brown 
12643f9fe445SBarry Smith    Logically Collective on TS
1265316643e7SJed Brown 
1266316643e7SJed Brown    Input Parameters:
1267316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12680298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1269316643e7SJed Brown .  f   - the function evaluation routine
12700298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1271316643e7SJed Brown 
1272316643e7SJed Brown    Calling sequence of f:
12736bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1274316643e7SJed Brown 
1275316643e7SJed Brown +  t   - time at step/stage being solved
1276316643e7SJed Brown .  u   - state vector
1277316643e7SJed Brown .  u_t - time derivative of state vector
1278316643e7SJed Brown .  F   - function vector
1279316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1280316643e7SJed Brown 
1281316643e7SJed Brown    Important:
12822bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1283316643e7SJed Brown 
1284316643e7SJed Brown    Level: beginner
1285316643e7SJed Brown 
1286d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1287316643e7SJed Brown @*/
128851699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1289316643e7SJed Brown {
1290089b2837SJed Brown   PetscErrorCode ierr;
1291089b2837SJed Brown   SNES           snes;
129251699248SLisandro Dalcin   Vec            ralloc = NULL;
129324989b8cSPeter Brune   DM             dm;
1294316643e7SJed Brown 
1295316643e7SJed Brown   PetscFunctionBegin;
12960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
129751699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
129824989b8cSPeter Brune 
129924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130024989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
130124989b8cSPeter Brune 
1302089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
130351699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
130451699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
130551699248SLisandro Dalcin     r  = ralloc;
1306e856ceecSJed Brown   }
130751699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
130851699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1309089b2837SJed Brown   PetscFunctionReturn(0);
1310089b2837SJed Brown }
1311089b2837SJed Brown 
1312089b2837SJed Brown /*@C
1313089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1314089b2837SJed Brown 
1315089b2837SJed Brown    Not Collective
1316089b2837SJed Brown 
1317089b2837SJed Brown    Input Parameter:
1318089b2837SJed Brown .  ts - the TS context
1319089b2837SJed Brown 
1320089b2837SJed Brown    Output Parameter:
13210298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13220298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13230298fd71SBarry Smith -  ctx - the function context (or NULL)
1324089b2837SJed Brown 
1325089b2837SJed Brown    Level: advanced
1326089b2837SJed Brown 
1327089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1328089b2837SJed Brown @*/
1329089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1330089b2837SJed Brown {
1331089b2837SJed Brown   PetscErrorCode ierr;
1332089b2837SJed Brown   SNES           snes;
133324989b8cSPeter Brune   DM             dm;
1334089b2837SJed Brown 
1335089b2837SJed Brown   PetscFunctionBegin;
1336089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1337089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13380298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
133924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
134024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1341089b2837SJed Brown   PetscFunctionReturn(0);
1342089b2837SJed Brown }
1343089b2837SJed Brown 
1344089b2837SJed Brown /*@C
1345089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1346089b2837SJed Brown 
1347089b2837SJed Brown    Not Collective
1348089b2837SJed Brown 
1349089b2837SJed Brown    Input Parameter:
1350089b2837SJed Brown .  ts - the TS context
1351089b2837SJed Brown 
1352089b2837SJed Brown    Output Parameter:
13530298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13540298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13550298fd71SBarry Smith -  ctx - the function context (or NULL)
1356089b2837SJed Brown 
1357089b2837SJed Brown    Level: advanced
1358089b2837SJed Brown 
13592bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1360089b2837SJed Brown @*/
1361089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1362089b2837SJed Brown {
1363089b2837SJed Brown   PetscErrorCode ierr;
1364089b2837SJed Brown   SNES           snes;
136524989b8cSPeter Brune   DM             dm;
1366089b2837SJed Brown 
1367089b2837SJed Brown   PetscFunctionBegin;
1368089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1369089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13700298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
137124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1373316643e7SJed Brown   PetscFunctionReturn(0);
1374316643e7SJed Brown }
1375316643e7SJed Brown 
1376316643e7SJed Brown /*@C
1377a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1378ae8867d6SBarry Smith         provided with TSSetIFunction().
1379316643e7SJed Brown 
13803f9fe445SBarry Smith    Logically Collective on TS
1381316643e7SJed Brown 
1382316643e7SJed Brown    Input Parameters:
1383316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1384e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1385e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1386316643e7SJed Brown .  f   - the Jacobian evaluation routine
13870298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1388316643e7SJed Brown 
1389316643e7SJed Brown    Calling sequence of f:
13906bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1391316643e7SJed Brown 
1392316643e7SJed Brown +  t    - time at step/stage being solved
13931b4a444bSJed Brown .  U    - state vector
13941b4a444bSJed Brown .  U_t  - time derivative of state vector
1395316643e7SJed Brown .  a    - shift
1396e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1397e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1398316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1399316643e7SJed Brown 
1400316643e7SJed Brown    Notes:
1401e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1402316643e7SJed Brown 
1403895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1404895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1405895c21f2SBarry Smith 
1406a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1407b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1408a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1409a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1410a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1411a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1412a4f0a591SBarry Smith 
14136cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14146cd88445SBarry Smith 
14156cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1416ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1417ca5f011dSBarry Smith 
1418316643e7SJed Brown    Level: beginner
1419316643e7SJed Brown 
1420ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1421316643e7SJed Brown 
1422316643e7SJed Brown @*/
1423e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1424316643e7SJed Brown {
1425316643e7SJed Brown   PetscErrorCode ierr;
1426089b2837SJed Brown   SNES           snes;
142724989b8cSPeter Brune   DM             dm;
1428316643e7SJed Brown 
1429316643e7SJed Brown   PetscFunctionBegin;
14300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1431e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1432e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1433e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1434e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
143524989b8cSPeter Brune 
143624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
143724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
143824989b8cSPeter Brune 
1439089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1440e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1441316643e7SJed Brown   PetscFunctionReturn(0);
1442316643e7SJed Brown }
1443316643e7SJed Brown 
1444e1244c69SJed Brown /*@
1445e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1446e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1447e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1448e1244c69SJed Brown    not been changed by the TS.
1449e1244c69SJed Brown 
1450e1244c69SJed Brown    Logically Collective
1451e1244c69SJed Brown 
1452e1244c69SJed Brown    Input Arguments:
1453e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1454e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1455e1244c69SJed Brown 
1456e1244c69SJed Brown    Level: intermediate
1457e1244c69SJed Brown 
1458e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1459e1244c69SJed Brown @*/
1460e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1461e1244c69SJed Brown {
1462e1244c69SJed Brown   PetscFunctionBegin;
1463e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1464e1244c69SJed Brown   PetscFunctionReturn(0);
1465e1244c69SJed Brown }
1466e1244c69SJed Brown 
1467efe9872eSLisandro Dalcin /*@C
1468efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1469efe9872eSLisandro Dalcin 
1470efe9872eSLisandro Dalcin    Logically Collective on TS
1471efe9872eSLisandro Dalcin 
1472efe9872eSLisandro Dalcin    Input Parameters:
1473efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1474efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1475efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1476efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1477efe9872eSLisandro Dalcin 
1478efe9872eSLisandro Dalcin    Calling sequence of fun:
14796bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1480efe9872eSLisandro Dalcin 
1481efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1482efe9872eSLisandro Dalcin .  U    - state vector
1483efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1484efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1485efe9872eSLisandro Dalcin .  F    - function vector
1486efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1487efe9872eSLisandro Dalcin 
1488efe9872eSLisandro Dalcin    Level: beginner
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1491efe9872eSLisandro Dalcin @*/
1492efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1493efe9872eSLisandro Dalcin {
1494efe9872eSLisandro Dalcin   DM             dm;
1495efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1496efe9872eSLisandro Dalcin 
1497efe9872eSLisandro Dalcin   PetscFunctionBegin;
1498efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1499efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1500efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1501efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1502efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1503efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1504efe9872eSLisandro Dalcin }
1505efe9872eSLisandro Dalcin 
1506efe9872eSLisandro Dalcin /*@C
1507efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1508efe9872eSLisandro Dalcin 
1509efe9872eSLisandro Dalcin   Not Collective
1510efe9872eSLisandro Dalcin 
1511efe9872eSLisandro Dalcin   Input Parameter:
1512efe9872eSLisandro Dalcin . ts - the TS context
1513efe9872eSLisandro Dalcin 
1514efe9872eSLisandro Dalcin   Output Parameter:
1515efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1516efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1517efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1518efe9872eSLisandro Dalcin 
1519efe9872eSLisandro Dalcin   Level: advanced
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1522efe9872eSLisandro Dalcin @*/
1523efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1524efe9872eSLisandro Dalcin {
1525efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1526efe9872eSLisandro Dalcin   SNES           snes;
1527efe9872eSLisandro Dalcin   DM             dm;
1528efe9872eSLisandro Dalcin 
1529efe9872eSLisandro Dalcin   PetscFunctionBegin;
1530efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1531efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1532efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1533efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1534efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1535efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1536efe9872eSLisandro Dalcin }
1537efe9872eSLisandro Dalcin 
1538efe9872eSLisandro Dalcin /*@C
1539bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1540efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1541efe9872eSLisandro Dalcin 
1542efe9872eSLisandro Dalcin    Logically Collective on TS
1543efe9872eSLisandro Dalcin 
1544efe9872eSLisandro Dalcin    Input Parameters:
1545efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1546efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1547efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1548efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1549efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin    Calling sequence of jac:
15526bc98fa9SBarry 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);
1553efe9872eSLisandro Dalcin 
1554efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1555efe9872eSLisandro Dalcin .  U    - state vector
1556efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1557efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1558efe9872eSLisandro Dalcin .  v    - shift for U_t
1559efe9872eSLisandro Dalcin .  a    - shift for U_tt
1560efe9872eSLisandro 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
1561efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1562efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1563efe9872eSLisandro Dalcin 
1564efe9872eSLisandro Dalcin    Notes:
1565efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1566efe9872eSLisandro Dalcin 
1567efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1568efe9872eSLisandro 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.
1569efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1570bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1571efe9872eSLisandro Dalcin 
1572efe9872eSLisandro Dalcin    Level: beginner
1573efe9872eSLisandro Dalcin 
1574efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1575efe9872eSLisandro Dalcin @*/
1576efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1577efe9872eSLisandro Dalcin {
1578efe9872eSLisandro Dalcin   DM             dm;
1579efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1580efe9872eSLisandro Dalcin 
1581efe9872eSLisandro Dalcin   PetscFunctionBegin;
1582efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1583efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1584efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1585efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1586efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1587efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1588efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1589efe9872eSLisandro Dalcin }
1590efe9872eSLisandro Dalcin 
1591efe9872eSLisandro Dalcin /*@C
1592efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1593efe9872eSLisandro Dalcin 
1594efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   Input Parameter:
1597efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1598efe9872eSLisandro Dalcin 
1599efe9872eSLisandro Dalcin   Output Parameters:
1600efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1601efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1602efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1603efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1604efe9872eSLisandro Dalcin 
160595452b02SPatrick Sanan   Notes:
160695452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1607efe9872eSLisandro Dalcin 
1608efe9872eSLisandro Dalcin   Level: advanced
1609efe9872eSLisandro Dalcin 
161080275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1611efe9872eSLisandro Dalcin 
1612efe9872eSLisandro Dalcin @*/
1613efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1614efe9872eSLisandro Dalcin {
1615efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1616efe9872eSLisandro Dalcin   SNES           snes;
1617efe9872eSLisandro Dalcin   DM             dm;
1618efe9872eSLisandro Dalcin 
1619efe9872eSLisandro Dalcin   PetscFunctionBegin;
1620efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1621efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1622efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1623efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1626efe9872eSLisandro Dalcin }
1627efe9872eSLisandro Dalcin 
1628efe9872eSLisandro Dalcin /*@
1629efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1630efe9872eSLisandro Dalcin 
1631d083f849SBarry Smith   Collective on TS
1632efe9872eSLisandro Dalcin 
1633efe9872eSLisandro Dalcin   Input Parameters:
1634efe9872eSLisandro Dalcin + ts - the TS context
1635efe9872eSLisandro Dalcin . t - current time
1636efe9872eSLisandro Dalcin . U - state vector
1637efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1638efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1639efe9872eSLisandro Dalcin 
1640efe9872eSLisandro Dalcin   Output Parameter:
1641efe9872eSLisandro Dalcin . F - the residual vector
1642efe9872eSLisandro Dalcin 
1643efe9872eSLisandro Dalcin   Note:
1644efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1645efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1646efe9872eSLisandro Dalcin 
1647efe9872eSLisandro Dalcin   Level: developer
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1650efe9872eSLisandro Dalcin @*/
1651efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1652efe9872eSLisandro Dalcin {
1653efe9872eSLisandro Dalcin   DM             dm;
1654efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1655efe9872eSLisandro Dalcin   void           *ctx;
1656efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1657efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1658efe9872eSLisandro Dalcin 
1659efe9872eSLisandro Dalcin   PetscFunctionBegin;
1660efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1661efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1662efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1663efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1664efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1665efe9872eSLisandro Dalcin 
1666efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1667efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1668efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1669efe9872eSLisandro Dalcin 
1670efe9872eSLisandro Dalcin   if (!I2Function) {
1671efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1672efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1673efe9872eSLisandro Dalcin   }
1674efe9872eSLisandro Dalcin 
1675efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1676efe9872eSLisandro Dalcin 
1677efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1678efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1679efe9872eSLisandro Dalcin   PetscStackPop;
1680efe9872eSLisandro Dalcin 
1681efe9872eSLisandro Dalcin   if (rhsfunction) {
1682efe9872eSLisandro Dalcin     Vec Frhs;
1683efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1684efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1685efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1686efe9872eSLisandro Dalcin   }
1687efe9872eSLisandro Dalcin 
1688efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1689efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1690efe9872eSLisandro Dalcin }
1691efe9872eSLisandro Dalcin 
1692efe9872eSLisandro Dalcin /*@
1693efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1694efe9872eSLisandro Dalcin 
1695d083f849SBarry Smith   Collective on TS
1696efe9872eSLisandro Dalcin 
1697efe9872eSLisandro Dalcin   Input Parameters:
1698efe9872eSLisandro Dalcin + ts - the TS context
1699efe9872eSLisandro Dalcin . t - current timestep
1700efe9872eSLisandro Dalcin . U - state vector
1701efe9872eSLisandro Dalcin . V - time derivative of state vector
1702efe9872eSLisandro Dalcin . A - second time derivative of state vector
1703efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1704efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1705efe9872eSLisandro Dalcin 
1706efe9872eSLisandro Dalcin   Output Parameters:
1707efe9872eSLisandro Dalcin + J - Jacobian matrix
1708efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1709efe9872eSLisandro Dalcin 
1710efe9872eSLisandro Dalcin   Notes:
1711efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1714efe9872eSLisandro Dalcin 
1715efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1716efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   Level: developer
1719efe9872eSLisandro Dalcin 
1720efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1721efe9872eSLisandro Dalcin @*/
1722efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1723efe9872eSLisandro Dalcin {
1724efe9872eSLisandro Dalcin   DM             dm;
1725efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1726efe9872eSLisandro Dalcin   void           *ctx;
1727efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1728efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1729efe9872eSLisandro Dalcin 
1730efe9872eSLisandro Dalcin   PetscFunctionBegin;
1731efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1732efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1733efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1734efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1735efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1736efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1737efe9872eSLisandro Dalcin 
1738efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1739efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1740efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1741efe9872eSLisandro Dalcin 
1742efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1743efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1744efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1745efe9872eSLisandro Dalcin   }
1746efe9872eSLisandro Dalcin 
1747efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1748efe9872eSLisandro Dalcin 
1749efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1750efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1751efe9872eSLisandro Dalcin   PetscStackPop;
1752efe9872eSLisandro Dalcin 
1753efe9872eSLisandro Dalcin   if (rhsjacobian) {
1754efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1755efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1756efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1757efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1758efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1759efe9872eSLisandro Dalcin   }
1760efe9872eSLisandro Dalcin 
1761efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1762efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1763efe9872eSLisandro Dalcin }
1764efe9872eSLisandro Dalcin 
1765efe9872eSLisandro Dalcin /*@
1766efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1767efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1768efe9872eSLisandro Dalcin 
1769d083f849SBarry Smith    Logically Collective on TS
1770efe9872eSLisandro Dalcin 
1771efe9872eSLisandro Dalcin    Input Parameters:
1772efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1773efe9872eSLisandro Dalcin .  u - the solution vector
1774efe9872eSLisandro Dalcin -  v - the time derivative vector
1775efe9872eSLisandro Dalcin 
1776efe9872eSLisandro Dalcin    Level: beginner
1777efe9872eSLisandro Dalcin 
1778efe9872eSLisandro Dalcin @*/
1779efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1780efe9872eSLisandro Dalcin {
1781efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1782efe9872eSLisandro Dalcin 
1783efe9872eSLisandro Dalcin   PetscFunctionBegin;
1784efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1785efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1786efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1787efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1788efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1789efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1790efe9872eSLisandro Dalcin   ts->vec_dot = v;
1791efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1792efe9872eSLisandro Dalcin }
1793efe9872eSLisandro Dalcin 
1794efe9872eSLisandro Dalcin /*@
1795efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1796efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1797efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1798efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1799efe9872eSLisandro Dalcin 
1800efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1801efe9872eSLisandro Dalcin 
1802efe9872eSLisandro Dalcin    Input Parameter:
1803efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1804efe9872eSLisandro Dalcin 
1805efe9872eSLisandro Dalcin    Output Parameter:
1806efe9872eSLisandro Dalcin +  u - the vector containing the solution
1807efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1808efe9872eSLisandro Dalcin 
1809efe9872eSLisandro Dalcin    Level: intermediate
1810efe9872eSLisandro Dalcin 
1811efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1812efe9872eSLisandro Dalcin 
1813efe9872eSLisandro Dalcin @*/
1814efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1815efe9872eSLisandro Dalcin {
1816efe9872eSLisandro Dalcin   PetscFunctionBegin;
1817efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1818efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1819efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1820efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1821efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1822efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1823efe9872eSLisandro Dalcin }
1824efe9872eSLisandro Dalcin 
182555849f57SBarry Smith /*@C
182655849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
182755849f57SBarry Smith 
182855849f57SBarry Smith   Collective on PetscViewer
182955849f57SBarry Smith 
183055849f57SBarry Smith   Input Parameters:
183155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
183255849f57SBarry Smith            some related function before a call to TSLoad().
183355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
183455849f57SBarry Smith 
183555849f57SBarry Smith    Level: intermediate
183655849f57SBarry Smith 
183755849f57SBarry Smith   Notes:
183855849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
183955849f57SBarry Smith 
184055849f57SBarry Smith   Notes for advanced users:
184155849f57SBarry Smith   Most users should not need to know the details of the binary storage
184255849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
184355849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
184455849f57SBarry Smith   format is
184555849f57SBarry Smith .vb
184655849f57SBarry Smith      has not yet been determined
184755849f57SBarry Smith .ve
184855849f57SBarry Smith 
184955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
185055849f57SBarry Smith @*/
1851f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
185255849f57SBarry Smith {
185355849f57SBarry Smith   PetscErrorCode ierr;
185455849f57SBarry Smith   PetscBool      isbinary;
185555849f57SBarry Smith   PetscInt       classid;
185655849f57SBarry Smith   char           type[256];
18572d53ad75SBarry Smith   DMTS           sdm;
1858ad6bc421SBarry Smith   DM             dm;
185955849f57SBarry Smith 
186055849f57SBarry Smith   PetscFunctionBegin;
1861f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
186255849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
186355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
186455849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
186555849f57SBarry Smith 
1866060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1867ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1868060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1869f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1870f2c2a1b9SBarry Smith   if (ts->ops->load) {
1871f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1872f2c2a1b9SBarry Smith   }
1873ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1874ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1875ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1876f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1877f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18782d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18792d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
188055849f57SBarry Smith   PetscFunctionReturn(0);
188155849f57SBarry Smith }
188255849f57SBarry Smith 
18839804daf3SBarry Smith #include <petscdraw.h>
1884e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1885e04113cfSBarry Smith #include <petscviewersaws.h>
1886f05ece33SBarry Smith #endif
18877e2c5f70SBarry Smith /*@C
1888d763cef2SBarry Smith     TSView - Prints the TS data structure.
1889d763cef2SBarry Smith 
18904c49b128SBarry Smith     Collective on TS
1891d763cef2SBarry Smith 
1892d763cef2SBarry Smith     Input Parameters:
1893d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1894d763cef2SBarry Smith -   viewer - visualization context
1895d763cef2SBarry Smith 
1896d763cef2SBarry Smith     Options Database Key:
1897d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1898d763cef2SBarry Smith 
1899d763cef2SBarry Smith     Notes:
1900d763cef2SBarry Smith     The available visualization contexts include
1901b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1902b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1903d763cef2SBarry Smith          output where only the first processor opens
1904d763cef2SBarry Smith          the file.  All other processors send their
1905d763cef2SBarry Smith          data to the first processor to print.
1906d763cef2SBarry Smith 
1907d763cef2SBarry Smith     The user can open an alternative visualization context with
1908b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1909d763cef2SBarry Smith 
1910d763cef2SBarry Smith     Level: beginner
1911d763cef2SBarry Smith 
1912b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1913d763cef2SBarry Smith @*/
19147087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1915d763cef2SBarry Smith {
1916dfbe8321SBarry Smith   PetscErrorCode ierr;
191719fd82e9SBarry Smith   TSType         type;
19182b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19192d53ad75SBarry Smith   DMTS           sdm;
1920e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1921536b137fSBarry Smith   PetscBool      issaws;
1922f05ece33SBarry Smith #endif
1923d763cef2SBarry Smith 
1924d763cef2SBarry Smith   PetscFunctionBegin;
19250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19263050cee2SBarry Smith   if (!viewer) {
1927ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19283050cee2SBarry Smith   }
19290700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1930c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1931fd16b177SBarry Smith 
1932251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1933251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
193455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19352b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1936e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1937536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1938f05ece33SBarry Smith #endif
193932077d6dSBarry Smith   if (iascii) {
1940dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1941efd4aadfSBarry Smith     if (ts->ops->view) {
1942efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1943efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1944efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1945efd4aadfSBarry Smith     }
1946ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
194777431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1948ef85077eSLisandro Dalcin     }
1949ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19507c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1951ef85077eSLisandro Dalcin     }
1952efd4aadfSBarry Smith     if (ts->usessnes) {
1953efd4aadfSBarry Smith       PetscBool lin;
1954d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19555ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1956d763cef2SBarry Smith       }
19575ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
19581ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
1959efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1960efd4aadfSBarry Smith     }
1961193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1962a0af407cSBarry Smith     if (ts->vrtol) {
1963a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1964a0af407cSBarry Smith     } else {
1965a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1966a0af407cSBarry Smith     }
1967a0af407cSBarry Smith     if (ts->vatol) {
1968a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1969a0af407cSBarry Smith     } else {
1970a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1971a0af407cSBarry Smith     }
1972825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1973efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
1974825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
19750f5bd95cSBarry Smith   } else if (isstring) {
1976a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
197736a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
197836a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
197955849f57SBarry Smith   } else if (isbinary) {
198055849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
198155849f57SBarry Smith     MPI_Comm    comm;
198255849f57SBarry Smith     PetscMPIInt rank;
198355849f57SBarry Smith     char        type[256];
198455849f57SBarry Smith 
198555849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
198655849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
198755849f57SBarry Smith     if (!rank) {
198855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
198955849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
199055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
199155849f57SBarry Smith     }
199255849f57SBarry Smith     if (ts->ops->view) {
199355849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
199455849f57SBarry Smith     }
1995efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1996f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1997f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19982d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19992d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20002b0a91c0SBarry Smith   } else if (isdraw) {
20012b0a91c0SBarry Smith     PetscDraw draw;
20022b0a91c0SBarry Smith     char      str[36];
200389fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20042b0a91c0SBarry Smith 
20052b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20062b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20072b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20082b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
200951fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
201089fd9fafSBarry Smith     bottom = y - h;
20112b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20122b0a91c0SBarry Smith     if (ts->ops->view) {
20132b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20142b0a91c0SBarry Smith     }
2015efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2016efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20172b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2018e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2019536b137fSBarry Smith   } else if (issaws) {
2020d45a07a7SBarry Smith     PetscMPIInt rank;
20212657e9d9SBarry Smith     const char  *name;
20222657e9d9SBarry Smith 
20232657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2024d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2025d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2026d45a07a7SBarry Smith       char       dir[1024];
2027d45a07a7SBarry Smith 
2028e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2029a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20302657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20312657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20322657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2033d763cef2SBarry Smith     }
20340acecf5bSBarry Smith     if (ts->ops->view) {
20350acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20360acecf5bSBarry Smith     }
2037f05ece33SBarry Smith #endif
2038f05ece33SBarry Smith   }
203936a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
204036a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
204136a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
204236a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
204336a9e3b9SBarry Smith   }
204436a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
204536a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2046f05ece33SBarry Smith 
2047b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2048251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2049b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2050d763cef2SBarry Smith   PetscFunctionReturn(0);
2051d763cef2SBarry Smith }
2052d763cef2SBarry Smith 
2053b07ff414SBarry Smith /*@
2054d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2055d763cef2SBarry Smith    the timesteppers.
2056d763cef2SBarry Smith 
20573f9fe445SBarry Smith    Logically Collective on TS
2058d763cef2SBarry Smith 
2059d763cef2SBarry Smith    Input Parameters:
2060d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2061d763cef2SBarry Smith -  usrP - optional user context
2062d763cef2SBarry Smith 
206395452b02SPatrick Sanan    Fortran Notes:
206495452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2065daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2066daf670e6SBarry Smith 
2067d763cef2SBarry Smith    Level: intermediate
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2070d763cef2SBarry Smith @*/
20717087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2072d763cef2SBarry Smith {
2073d763cef2SBarry Smith   PetscFunctionBegin;
20740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2075d763cef2SBarry Smith   ts->user = usrP;
2076d763cef2SBarry Smith   PetscFunctionReturn(0);
2077d763cef2SBarry Smith }
2078d763cef2SBarry Smith 
2079b07ff414SBarry Smith /*@
2080d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2081d763cef2SBarry Smith     timestepper.
2082d763cef2SBarry Smith 
2083d763cef2SBarry Smith     Not Collective
2084d763cef2SBarry Smith 
2085d763cef2SBarry Smith     Input Parameter:
2086d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2087d763cef2SBarry Smith 
2088d763cef2SBarry Smith     Output Parameter:
2089d763cef2SBarry Smith .   usrP - user context
2090d763cef2SBarry Smith 
209195452b02SPatrick Sanan    Fortran Notes:
209295452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2093daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2094daf670e6SBarry Smith 
2095d763cef2SBarry Smith     Level: intermediate
2096d763cef2SBarry Smith 
2097d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2098d763cef2SBarry Smith @*/
2099e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2100d763cef2SBarry Smith {
2101d763cef2SBarry Smith   PetscFunctionBegin;
21020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2103e71120c6SJed Brown   *(void**)usrP = ts->user;
2104d763cef2SBarry Smith   PetscFunctionReturn(0);
2105d763cef2SBarry Smith }
2106d763cef2SBarry Smith 
2107d763cef2SBarry Smith /*@
210880275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith    Not Collective
2111d763cef2SBarry Smith 
2112d763cef2SBarry Smith    Input Parameter:
2113d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith    Output Parameter:
211680275a0aSLisandro Dalcin .  steps - number of steps completed so far
2117d763cef2SBarry Smith 
2118d763cef2SBarry Smith    Level: intermediate
2119d763cef2SBarry Smith 
21209be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2121d763cef2SBarry Smith @*/
212280275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2123d763cef2SBarry Smith {
2124d763cef2SBarry Smith   PetscFunctionBegin;
21250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
212680275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
212780275a0aSLisandro Dalcin   *steps = ts->steps;
212880275a0aSLisandro Dalcin   PetscFunctionReturn(0);
212980275a0aSLisandro Dalcin }
213080275a0aSLisandro Dalcin 
213180275a0aSLisandro Dalcin /*@
213280275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
213380275a0aSLisandro Dalcin 
213480275a0aSLisandro Dalcin    Logically Collective on TS
213580275a0aSLisandro Dalcin 
213680275a0aSLisandro Dalcin    Input Parameters:
213780275a0aSLisandro Dalcin +  ts - the TS context
213880275a0aSLisandro Dalcin -  steps - number of steps completed so far
213980275a0aSLisandro Dalcin 
214080275a0aSLisandro Dalcin    Notes:
214180275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
214280275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
214380275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
214480275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
214580275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
214680275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
214780275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
214880275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
214980275a0aSLisandro Dalcin 
215080275a0aSLisandro Dalcin    Level: advanced
215180275a0aSLisandro Dalcin 
215280275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
215380275a0aSLisandro Dalcin @*/
215480275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
215580275a0aSLisandro Dalcin {
215680275a0aSLisandro Dalcin   PetscFunctionBegin;
215780275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
215880275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
215980275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
216080275a0aSLisandro Dalcin   ts->steps = steps;
2161d763cef2SBarry Smith   PetscFunctionReturn(0);
2162d763cef2SBarry Smith }
2163d763cef2SBarry Smith 
2164d763cef2SBarry Smith /*@
2165d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2166d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2167d763cef2SBarry Smith 
21683f9fe445SBarry Smith    Logically Collective on TS
2169d763cef2SBarry Smith 
2170d763cef2SBarry Smith    Input Parameters:
2171d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2172d763cef2SBarry Smith -  time_step - the size of the timestep
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Level: intermediate
2175d763cef2SBarry Smith 
2176aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2177d763cef2SBarry Smith 
2178d763cef2SBarry Smith @*/
21797087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2180d763cef2SBarry Smith {
2181d763cef2SBarry Smith   PetscFunctionBegin;
21820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2183c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2184d763cef2SBarry Smith   ts->time_step = time_step;
2185d763cef2SBarry Smith   PetscFunctionReturn(0);
2186d763cef2SBarry Smith }
2187d763cef2SBarry Smith 
2188a43b19c4SJed Brown /*@
218949354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
219049354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
219149354f04SShri Abhyankar      or just return at the final time TS computed.
2192a43b19c4SJed Brown 
2193a43b19c4SJed Brown   Logically Collective on TS
2194a43b19c4SJed Brown 
2195a43b19c4SJed Brown    Input Parameter:
2196a43b19c4SJed Brown +   ts - the time-step context
219749354f04SShri Abhyankar -   eftopt - exact final time option
2198a43b19c4SJed Brown 
2199feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2200feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2201feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2202feed9e9dSBarry Smith 
2203feed9e9dSBarry Smith    Options Database:
2204feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2205feed9e9dSBarry Smith 
2206ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2207ee346746SBarry Smith     then the final time you selected.
2208ee346746SBarry Smith 
2209a43b19c4SJed Brown    Level: beginner
2210a43b19c4SJed Brown 
2211f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2212a43b19c4SJed Brown @*/
221349354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2214a43b19c4SJed Brown {
2215a43b19c4SJed Brown   PetscFunctionBegin;
2216a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
221749354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
221849354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2219a43b19c4SJed Brown   PetscFunctionReturn(0);
2220a43b19c4SJed Brown }
2221a43b19c4SJed Brown 
2222d763cef2SBarry Smith /*@
2223f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2224f6953c82SLisandro Dalcin 
2225f6953c82SLisandro Dalcin    Not Collective
2226f6953c82SLisandro Dalcin 
2227f6953c82SLisandro Dalcin    Input Parameter:
2228f6953c82SLisandro Dalcin .  ts - the TS context
2229f6953c82SLisandro Dalcin 
2230f6953c82SLisandro Dalcin    Output Parameter:
2231f6953c82SLisandro Dalcin .  eftopt - exact final time option
2232f6953c82SLisandro Dalcin 
2233f6953c82SLisandro Dalcin    Level: beginner
2234f6953c82SLisandro Dalcin 
2235f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2236f6953c82SLisandro Dalcin @*/
2237f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2238f6953c82SLisandro Dalcin {
2239f6953c82SLisandro Dalcin   PetscFunctionBegin;
2240f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2241f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2242f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2243f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2244f6953c82SLisandro Dalcin }
2245f6953c82SLisandro Dalcin 
2246f6953c82SLisandro Dalcin /*@
2247d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2248d763cef2SBarry Smith 
2249d763cef2SBarry Smith    Not Collective
2250d763cef2SBarry Smith 
2251d763cef2SBarry Smith    Input Parameter:
2252d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2253d763cef2SBarry Smith 
2254d763cef2SBarry Smith    Output Parameter:
2255d763cef2SBarry Smith .  dt - the current timestep size
2256d763cef2SBarry Smith 
2257d763cef2SBarry Smith    Level: intermediate
2258d763cef2SBarry Smith 
2259aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2260d763cef2SBarry Smith 
2261d763cef2SBarry Smith @*/
22627087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2263d763cef2SBarry Smith {
2264d763cef2SBarry Smith   PetscFunctionBegin;
22650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2266f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2267d763cef2SBarry Smith   *dt = ts->time_step;
2268d763cef2SBarry Smith   PetscFunctionReturn(0);
2269d763cef2SBarry Smith }
2270d763cef2SBarry Smith 
2271d8e5e3e6SSatish Balay /*@
2272d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2273d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2274d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2275d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2276d763cef2SBarry Smith 
2277d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2278d763cef2SBarry Smith 
2279d763cef2SBarry Smith    Input Parameter:
2280d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2281d763cef2SBarry Smith 
2282d763cef2SBarry Smith    Output Parameter:
2283d763cef2SBarry Smith .  v - the vector containing the solution
2284d763cef2SBarry Smith 
228563e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
228663e21af5SBarry Smith    final time. It returns the solution at the next timestep.
228763e21af5SBarry Smith 
2288d763cef2SBarry Smith    Level: intermediate
2289d763cef2SBarry Smith 
22900ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2291d763cef2SBarry Smith 
2292d763cef2SBarry Smith @*/
22937087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2294d763cef2SBarry Smith {
2295d763cef2SBarry Smith   PetscFunctionBegin;
22960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22974482741eSBarry Smith   PetscValidPointer(v,2);
22988737fe31SLisandro Dalcin   *v = ts->vec_sol;
2299d763cef2SBarry Smith   PetscFunctionReturn(0);
2300d763cef2SBarry Smith }
2301d763cef2SBarry Smith 
230203fe5f5eSDebojyoti Ghosh /*@
2303b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
230403fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2305b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
230603fe5f5eSDebojyoti Ghosh    structure as the solution vector.
230703fe5f5eSDebojyoti Ghosh 
230803fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
230903fe5f5eSDebojyoti Ghosh 
231003fe5f5eSDebojyoti Ghosh    Parameters :
2311a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2312b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2313b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
231403fe5f5eSDebojyoti Ghosh        returned in v.
2315a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
231603fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2317b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
231803fe5f5eSDebojyoti Ghosh 
23194cdd57e5SDebojyoti Ghosh    Level: advanced
232003fe5f5eSDebojyoti Ghosh 
232103fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
232203fe5f5eSDebojyoti Ghosh 
232303fe5f5eSDebojyoti Ghosh @*/
2324b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
232503fe5f5eSDebojyoti Ghosh {
232603fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
232703fe5f5eSDebojyoti Ghosh 
232803fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
232903fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2330b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
233103fe5f5eSDebojyoti Ghosh   else {
2332b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
233303fe5f5eSDebojyoti Ghosh   }
233403fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
233503fe5f5eSDebojyoti Ghosh }
233603fe5f5eSDebojyoti Ghosh 
23374cdd57e5SDebojyoti Ghosh /*@
23384cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23394cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23404cdd57e5SDebojyoti Ghosh 
23414cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23424cdd57e5SDebojyoti Ghosh 
23434cdd57e5SDebojyoti Ghosh    Parameters :
2344a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2345a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
23464cdd57e5SDebojyoti Ghosh 
23474cdd57e5SDebojyoti Ghosh    Level: intermediate
23484cdd57e5SDebojyoti Ghosh 
23494cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23504cdd57e5SDebojyoti Ghosh 
23514cdd57e5SDebojyoti Ghosh @*/
23524cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23534cdd57e5SDebojyoti Ghosh {
23544cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23554cdd57e5SDebojyoti Ghosh 
23564cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23574cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2358f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23594cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2360f6356ec7SDebojyoti Ghosh   } else {
2361f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2362f6356ec7SDebojyoti Ghosh   }
23634cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23644cdd57e5SDebojyoti Ghosh }
23654cdd57e5SDebojyoti Ghosh 
23664cdd57e5SDebojyoti Ghosh /*@
23674cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23684cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23694cdd57e5SDebojyoti Ghosh 
23704cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23714cdd57e5SDebojyoti Ghosh 
23729657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23739657682dSDebojyoti Ghosh 
23744cdd57e5SDebojyoti Ghosh    Parameters :
2375a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2376657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2377a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
23784cdd57e5SDebojyoti Ghosh 
23794cdd57e5SDebojyoti Ghosh    Level: intermediate
23804cdd57e5SDebojyoti Ghosh 
238157df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
23824cdd57e5SDebojyoti Ghosh 
23834cdd57e5SDebojyoti Ghosh @*/
23840a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
23854cdd57e5SDebojyoti Ghosh {
23864cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23874cdd57e5SDebojyoti Ghosh 
23884cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23894cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2390f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
23910a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2392f6356ec7SDebojyoti Ghosh   } else {
2393f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2394f6356ec7SDebojyoti Ghosh   }
23954cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23964cdd57e5SDebojyoti Ghosh }
23974cdd57e5SDebojyoti Ghosh 
239857df6a1bSDebojyoti Ghosh /*@
239957df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
240057df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
240157df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
240257df6a1bSDebojyoti Ghosh 
240357df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
240457df6a1bSDebojyoti Ghosh 
240557df6a1bSDebojyoti Ghosh    Parameters :
2406a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2407a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
240857df6a1bSDebojyoti Ghosh 
240957df6a1bSDebojyoti Ghosh    Level: intermediate
241057df6a1bSDebojyoti Ghosh 
241157df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
241257df6a1bSDebojyoti Ghosh 
241357df6a1bSDebojyoti Ghosh @*/
241457df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
241557df6a1bSDebojyoti Ghosh {
241657df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
241757df6a1bSDebojyoti Ghosh 
241857df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
241957df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24209657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
242157df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
242257df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
242357df6a1bSDebojyoti Ghosh   }
242457df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
242557df6a1bSDebojyoti Ghosh }
242657df6a1bSDebojyoti Ghosh 
2427bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2428d8e5e3e6SSatish Balay /*@
2429bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2430d763cef2SBarry Smith 
2431bdad233fSMatthew Knepley   Not collective
2432d763cef2SBarry Smith 
2433bdad233fSMatthew Knepley   Input Parameters:
2434bdad233fSMatthew Knepley + ts   - The TS
2435bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2436d763cef2SBarry Smith .vb
24370910c330SBarry Smith          U_t - A U = 0      (linear)
24380910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24390910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2440d763cef2SBarry Smith .ve
2441d763cef2SBarry Smith 
2442d763cef2SBarry Smith    Level: beginner
2443d763cef2SBarry Smith 
2444bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2445d763cef2SBarry Smith @*/
24467087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2447a7cc72afSBarry Smith {
24489e2a6581SJed Brown   PetscErrorCode ierr;
24499e2a6581SJed Brown 
2450d763cef2SBarry Smith   PetscFunctionBegin;
24510700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2452bdad233fSMatthew Knepley   ts->problem_type = type;
24539e2a6581SJed Brown   if (type == TS_LINEAR) {
24549e2a6581SJed Brown     SNES snes;
24559e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24569e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24579e2a6581SJed Brown   }
2458d763cef2SBarry Smith   PetscFunctionReturn(0);
2459d763cef2SBarry Smith }
2460d763cef2SBarry Smith 
2461bdad233fSMatthew Knepley /*@C
2462bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2463bdad233fSMatthew Knepley 
2464bdad233fSMatthew Knepley   Not collective
2465bdad233fSMatthew Knepley 
2466bdad233fSMatthew Knepley   Input Parameter:
2467bdad233fSMatthew Knepley . ts   - The TS
2468bdad233fSMatthew Knepley 
2469bdad233fSMatthew Knepley   Output Parameter:
2470bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2471bdad233fSMatthew Knepley .vb
2472089b2837SJed Brown          M U_t = A U
2473089b2837SJed Brown          M(t) U_t = A(t) U
2474b5abc632SBarry Smith          F(t,U,U_t)
2475bdad233fSMatthew Knepley .ve
2476bdad233fSMatthew Knepley 
2477bdad233fSMatthew Knepley    Level: beginner
2478bdad233fSMatthew Knepley 
2479bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2480bdad233fSMatthew Knepley @*/
24817087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2482a7cc72afSBarry Smith {
2483bdad233fSMatthew Knepley   PetscFunctionBegin;
24840700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24854482741eSBarry Smith   PetscValidIntPointer(type,2);
2486bdad233fSMatthew Knepley   *type = ts->problem_type;
2487bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2488bdad233fSMatthew Knepley }
2489d763cef2SBarry Smith 
2490d763cef2SBarry Smith /*@
2491d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2492d763cef2SBarry Smith    of a timestepper.
2493d763cef2SBarry Smith 
2494d763cef2SBarry Smith    Collective on TS
2495d763cef2SBarry Smith 
2496d763cef2SBarry Smith    Input Parameter:
2497d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2498d763cef2SBarry Smith 
2499d763cef2SBarry Smith    Notes:
2500d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2501d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2502141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2503d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2504141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2505d763cef2SBarry Smith 
2506d763cef2SBarry Smith    Level: advanced
2507d763cef2SBarry Smith 
2508141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2509d763cef2SBarry Smith @*/
25107087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2511d763cef2SBarry Smith {
2512dfbe8321SBarry Smith   PetscErrorCode ierr;
25136c6b9e74SPeter Brune   DM             dm;
25146c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2515d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2516cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25176c6b9e74SPeter Brune   TSIJacobian    ijac;
2518efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25196c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25202ffb9264SLisandro Dalcin   PetscBool      isnone;
2521d763cef2SBarry Smith 
2522d763cef2SBarry Smith   PetscFunctionBegin;
25230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2524277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2525277b19d0SLisandro Dalcin 
25267adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2527cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2528cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2529d763cef2SBarry Smith   }
2530277b19d0SLisandro Dalcin 
2531277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2532277b19d0SLisandro Dalcin 
2533298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2534298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2535298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2536298bade4SHong Zhang   }
2537298bade4SHong Zhang 
2538cd4cee2dSHong Zhang   if (ts->quadraturets) {
2539cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2540ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2541cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2542cd4cee2dSHong Zhang   }
2543cd4cee2dSHong Zhang 
2544971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2545971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2546e1244c69SJed Brown     Mat Amat,Pmat;
2547e1244c69SJed Brown     SNES snes;
2548e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2549e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2550e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2551e1244c69SJed Brown      * have displaced the RHS matrix */
2552971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2553abc0d4abSBarry 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 */
2554abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2555e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2556e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2557e1244c69SJed Brown     }
2558971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2559abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2560e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2561e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2562e1244c69SJed Brown     }
2563e1244c69SJed Brown   }
25642ffb9264SLisandro Dalcin 
25652ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25662ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25672ffb9264SLisandro Dalcin 
2568277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2569000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2570277b19d0SLisandro Dalcin   }
2571277b19d0SLisandro Dalcin 
25722ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
25732ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
25742ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
25752ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25762ffb9264SLisandro Dalcin 
2577a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25786c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25796c6b9e74SPeter Brune    */
25806c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25810298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25826c6b9e74SPeter Brune   if (!func) {
25836c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25846c6b9e74SPeter Brune   }
2585a6772fa2SLisandro 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.
25866c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25876c6b9e74SPeter Brune    */
25880298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25890298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2590efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25910298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2592efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25936c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25946c6b9e74SPeter Brune   }
2595c0517034SDebojyoti Ghosh 
2596c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2597c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2598c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2599c0517034SDebojyoti Ghosh   }
2600c0517034SDebojyoti Ghosh 
2601277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2602277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2603277b19d0SLisandro Dalcin }
2604277b19d0SLisandro Dalcin 
2605f6a906c0SBarry Smith /*@
2606277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2607277b19d0SLisandro Dalcin 
2608277b19d0SLisandro Dalcin    Collective on TS
2609277b19d0SLisandro Dalcin 
2610277b19d0SLisandro Dalcin    Input Parameter:
2611277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2612277b19d0SLisandro Dalcin 
2613277b19d0SLisandro Dalcin    Level: beginner
2614277b19d0SLisandro Dalcin 
2615277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2616277b19d0SLisandro Dalcin @*/
2617277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2618277b19d0SLisandro Dalcin {
26191d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2620277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2621277b19d0SLisandro Dalcin 
2622277b19d0SLisandro Dalcin   PetscFunctionBegin;
2623277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2624b18ea86cSHong Zhang 
2625277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2626277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2627277b19d0SLisandro Dalcin   }
2628277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2629e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2630bbd56ea5SKarl Rupp 
26314e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26324e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2633214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26346bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2635efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2636e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2637e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
263838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2639bbd56ea5SKarl Rupp 
2640cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2641ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2642ecf68647SHong Zhang   if (ts->forward_solve) {
2643ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2644ecf68647SHong Zhang   }
2645cd4cee2dSHong Zhang   if (ts->quadraturets) {
2646cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
264736eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2648cd4cee2dSHong Zhang   }
26491d06f6b3SHong Zhang   while (ilink) {
26501d06f6b3SHong Zhang     next = ilink->next;
26511d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
26521d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
26531d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
26541d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
26551d06f6b3SHong Zhang     ilink = next;
265687f4e208SHong Zhang   }
2657545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2658277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2659d763cef2SBarry Smith   PetscFunctionReturn(0);
2660d763cef2SBarry Smith }
2661d763cef2SBarry Smith 
2662d8e5e3e6SSatish Balay /*@
2663d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2664d763cef2SBarry Smith    with TSCreate().
2665d763cef2SBarry Smith 
2666d763cef2SBarry Smith    Collective on TS
2667d763cef2SBarry Smith 
2668d763cef2SBarry Smith    Input Parameter:
2669d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2670d763cef2SBarry Smith 
2671d763cef2SBarry Smith    Level: beginner
2672d763cef2SBarry Smith 
2673d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2674d763cef2SBarry Smith @*/
26756bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2676d763cef2SBarry Smith {
26776849ba73SBarry Smith   PetscErrorCode ierr;
2678d763cef2SBarry Smith 
2679d763cef2SBarry Smith   PetscFunctionBegin;
26806bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2681ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
26826bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2683d763cef2SBarry Smith 
2684ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2685ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2686ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2687ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2688ecf68647SHong Zhang   }
2689e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2690e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26916bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26926d4c513bSLisandro Dalcin 
2693bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2694bc952696SBarry Smith 
269584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26966427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26976427ac75SLisandro Dalcin 
26986bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26996bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27006bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27010dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27026d4c513bSLisandro Dalcin 
2703cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2704a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2705d763cef2SBarry Smith   PetscFunctionReturn(0);
2706d763cef2SBarry Smith }
2707d763cef2SBarry Smith 
2708d8e5e3e6SSatish Balay /*@
2709d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2710d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2711d763cef2SBarry Smith 
2712d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2713d763cef2SBarry Smith 
2714d763cef2SBarry Smith    Input Parameter:
2715d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2716d763cef2SBarry Smith 
2717d763cef2SBarry Smith    Output Parameter:
2718d763cef2SBarry Smith .  snes - the nonlinear solver context
2719d763cef2SBarry Smith 
2720d763cef2SBarry Smith    Notes:
2721d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2722d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
272394b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2724d763cef2SBarry Smith 
2725d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27260298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2727d763cef2SBarry Smith 
2728d763cef2SBarry Smith    Level: beginner
2729d763cef2SBarry Smith 
2730d763cef2SBarry Smith @*/
27317087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2732d763cef2SBarry Smith {
2733d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2734d372ba47SLisandro Dalcin 
2735d763cef2SBarry Smith   PetscFunctionBegin;
27360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27374482741eSBarry Smith   PetscValidPointer(snes,2);
2738d372ba47SLisandro Dalcin   if (!ts->snes) {
2739ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
274016413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
27410298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27423bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2743d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2744496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27459e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27469e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27479e2a6581SJed Brown     }
2748d372ba47SLisandro Dalcin   }
2749d763cef2SBarry Smith   *snes = ts->snes;
2750d763cef2SBarry Smith   PetscFunctionReturn(0);
2751d763cef2SBarry Smith }
2752d763cef2SBarry Smith 
2753deb2cd25SJed Brown /*@
2754deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2755deb2cd25SJed Brown 
2756deb2cd25SJed Brown    Collective
2757deb2cd25SJed Brown 
2758deb2cd25SJed Brown    Input Parameter:
2759deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2760deb2cd25SJed Brown -  snes - the nonlinear solver context
2761deb2cd25SJed Brown 
2762deb2cd25SJed Brown    Notes:
2763deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2764deb2cd25SJed Brown 
2765deb2cd25SJed Brown    Level: developer
2766deb2cd25SJed Brown 
2767deb2cd25SJed Brown @*/
2768deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2769deb2cd25SJed Brown {
2770deb2cd25SJed Brown   PetscErrorCode ierr;
2771d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2772deb2cd25SJed Brown 
2773deb2cd25SJed Brown   PetscFunctionBegin;
2774deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2775deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2776deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2777deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2778bbd56ea5SKarl Rupp 
2779deb2cd25SJed Brown   ts->snes = snes;
2780bbd56ea5SKarl Rupp 
27810298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27820298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2783740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27840298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2785740132f1SEmil Constantinescu   }
2786deb2cd25SJed Brown   PetscFunctionReturn(0);
2787deb2cd25SJed Brown }
2788deb2cd25SJed Brown 
2789d8e5e3e6SSatish Balay /*@
279094b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2791d763cef2SBarry Smith    a TS (timestepper) context.
2792d763cef2SBarry Smith 
279394b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2794d763cef2SBarry Smith 
2795d763cef2SBarry Smith    Input Parameter:
2796d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2797d763cef2SBarry Smith 
2798d763cef2SBarry Smith    Output Parameter:
279994b7f48cSBarry Smith .  ksp - the nonlinear solver context
2800d763cef2SBarry Smith 
2801d763cef2SBarry Smith    Notes:
280294b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2803d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2804d763cef2SBarry Smith    KSP and PC contexts as well.
2805d763cef2SBarry Smith 
280694b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28070298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith    Level: beginner
2810d763cef2SBarry Smith 
2811d763cef2SBarry Smith @*/
28127087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2813d763cef2SBarry Smith {
2814d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2815089b2837SJed Brown   SNES           snes;
2816d372ba47SLisandro Dalcin 
2817d763cef2SBarry Smith   PetscFunctionBegin;
28180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28194482741eSBarry Smith   PetscValidPointer(ksp,2);
282017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2821e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2822089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2823089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2824d763cef2SBarry Smith   PetscFunctionReturn(0);
2825d763cef2SBarry Smith }
2826d763cef2SBarry Smith 
2827d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2828d763cef2SBarry Smith 
2829adb62b0dSMatthew Knepley /*@
2830618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2831618ce8baSLisandro Dalcin 
2832618ce8baSLisandro Dalcin    Logically Collective on TS
2833618ce8baSLisandro Dalcin 
2834618ce8baSLisandro Dalcin    Input Parameters:
2835618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2836618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2837618ce8baSLisandro Dalcin 
2838618ce8baSLisandro Dalcin    Options Database Keys:
2839618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2840618ce8baSLisandro Dalcin 
2841618ce8baSLisandro Dalcin    Notes:
2842618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2843618ce8baSLisandro Dalcin 
2844618ce8baSLisandro Dalcin    Level: intermediate
2845618ce8baSLisandro Dalcin 
2846618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2847618ce8baSLisandro Dalcin @*/
2848618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2849618ce8baSLisandro Dalcin {
2850618ce8baSLisandro Dalcin   PetscFunctionBegin;
2851618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2852618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2853618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2854618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2855618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2856618ce8baSLisandro Dalcin }
2857618ce8baSLisandro Dalcin 
2858618ce8baSLisandro Dalcin /*@
2859618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2860618ce8baSLisandro Dalcin 
2861618ce8baSLisandro Dalcin    Not Collective
2862618ce8baSLisandro Dalcin 
2863618ce8baSLisandro Dalcin    Input Parameters:
2864618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2865618ce8baSLisandro Dalcin 
2866618ce8baSLisandro Dalcin    Output Parameter:
2867618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2868618ce8baSLisandro Dalcin 
2869618ce8baSLisandro Dalcin    Level: advanced
2870618ce8baSLisandro Dalcin 
2871618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2872618ce8baSLisandro Dalcin @*/
2873618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2874618ce8baSLisandro Dalcin {
2875618ce8baSLisandro Dalcin   PetscFunctionBegin;
2876618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2877618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2878618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2879618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2880618ce8baSLisandro Dalcin }
2881618ce8baSLisandro Dalcin 
2882618ce8baSLisandro Dalcin /*@
2883618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2884618ce8baSLisandro Dalcin 
2885618ce8baSLisandro Dalcin    Logically Collective on TS
2886618ce8baSLisandro Dalcin 
2887618ce8baSLisandro Dalcin    Input Parameters:
2888618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2889618ce8baSLisandro Dalcin -  maxtime - final time to step to
2890618ce8baSLisandro Dalcin 
2891618ce8baSLisandro Dalcin    Options Database Keys:
2892ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2893618ce8baSLisandro Dalcin 
2894618ce8baSLisandro Dalcin    Notes:
2895618ce8baSLisandro Dalcin    The default maximum time is 5.0
2896618ce8baSLisandro Dalcin 
2897618ce8baSLisandro Dalcin    Level: intermediate
2898618ce8baSLisandro Dalcin 
2899618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2900618ce8baSLisandro Dalcin @*/
2901618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2902618ce8baSLisandro Dalcin {
2903618ce8baSLisandro Dalcin   PetscFunctionBegin;
2904618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2905618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2906618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2907618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2908618ce8baSLisandro Dalcin }
2909618ce8baSLisandro Dalcin 
2910618ce8baSLisandro Dalcin /*@
2911618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2912618ce8baSLisandro Dalcin 
2913618ce8baSLisandro Dalcin    Not Collective
2914618ce8baSLisandro Dalcin 
2915618ce8baSLisandro Dalcin    Input Parameters:
2916618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2917618ce8baSLisandro Dalcin 
2918618ce8baSLisandro Dalcin    Output Parameter:
2919618ce8baSLisandro Dalcin .  maxtime - final time to step to
2920618ce8baSLisandro Dalcin 
2921618ce8baSLisandro Dalcin    Level: advanced
2922618ce8baSLisandro Dalcin 
2923618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2924618ce8baSLisandro Dalcin @*/
2925618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2926618ce8baSLisandro Dalcin {
2927618ce8baSLisandro Dalcin   PetscFunctionBegin;
2928618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2929618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2930618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2931618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2932618ce8baSLisandro Dalcin }
2933618ce8baSLisandro Dalcin 
2934618ce8baSLisandro Dalcin /*@
2935aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2936edc382c3SSatish Balay 
2937edc382c3SSatish Balay    Level: deprecated
2938edc382c3SSatish Balay 
2939aaa6c58dSLisandro Dalcin @*/
2940aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2941aaa6c58dSLisandro Dalcin {
2942aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
2943aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2944aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2945aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2946aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2947aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
2948aaa6c58dSLisandro Dalcin }
2949aaa6c58dSLisandro Dalcin 
2950aaa6c58dSLisandro Dalcin /*@
295119eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
2952edc382c3SSatish Balay 
2953edc382c3SSatish Balay    Level: deprecated
2954edc382c3SSatish Balay 
2955adb62b0dSMatthew Knepley @*/
29567087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2957adb62b0dSMatthew Knepley {
2958adb62b0dSMatthew Knepley   PetscFunctionBegin;
29590700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2960abc0a331SBarry Smith   if (maxsteps) {
29614482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2962adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2963adb62b0dSMatthew Knepley   }
2964abc0a331SBarry Smith   if (maxtime) {
29654482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2966adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2967adb62b0dSMatthew Knepley   }
2968adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2969adb62b0dSMatthew Knepley }
2970adb62b0dSMatthew Knepley 
2971d763cef2SBarry Smith /*@
297219eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
2973edc382c3SSatish Balay 
2974edc382c3SSatish Balay    Level: deprecated
2975edc382c3SSatish Balay 
2976d763cef2SBarry Smith @*/
29777087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2978d763cef2SBarry Smith {
2979d763cef2SBarry Smith   PetscFunctionBegin;
29800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2981c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2982c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
298339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
298439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2985d763cef2SBarry Smith   PetscFunctionReturn(0);
2986d763cef2SBarry Smith }
2987d763cef2SBarry Smith 
2988d763cef2SBarry Smith /*@
29895c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
2990edc382c3SSatish Balay 
2991edc382c3SSatish Balay    Level: deprecated
2992edc382c3SSatish Balay 
29935c5f5948SLisandro Dalcin @*/
2994e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
29955c5f5948SLisandro Dalcin 
299619eac22cSLisandro Dalcin /*@
29974f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
2998edc382c3SSatish Balay 
2999edc382c3SSatish Balay    Level: deprecated
3000edc382c3SSatish Balay 
30014f4e0956SLisandro Dalcin @*/
30024f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30034f4e0956SLisandro Dalcin 
30044f4e0956SLisandro Dalcin /*@
3005d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3006d763cef2SBarry Smith    for use by the TS routines.
3007d763cef2SBarry Smith 
3008d083f849SBarry Smith    Logically Collective on TS
3009d763cef2SBarry Smith 
3010d763cef2SBarry Smith    Input Parameters:
3011d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
30120910c330SBarry Smith -  u - the solution vector
3013d763cef2SBarry Smith 
3014d763cef2SBarry Smith    Level: beginner
3015d763cef2SBarry Smith 
30160ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3017d763cef2SBarry Smith @*/
30180910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3019d763cef2SBarry Smith {
30208737fe31SLisandro Dalcin   PetscErrorCode ierr;
3021496e6a7aSJed Brown   DM             dm;
30228737fe31SLisandro Dalcin 
3023d763cef2SBarry Smith   PetscFunctionBegin;
30240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30250910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30260910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30276bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30280910c330SBarry Smith   ts->vec_sol = u;
3029bbd56ea5SKarl Rupp 
3030496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30310910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3032d763cef2SBarry Smith   PetscFunctionReturn(0);
3033d763cef2SBarry Smith }
3034d763cef2SBarry Smith 
3035ac226902SBarry Smith /*@C
3036000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30373f2090d5SJed Brown   called once at the beginning of each time step.
3038000e7ae3SMatthew Knepley 
30393f9fe445SBarry Smith   Logically Collective on TS
3040000e7ae3SMatthew Knepley 
3041000e7ae3SMatthew Knepley   Input Parameters:
3042000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3043000e7ae3SMatthew Knepley - func - The function
3044000e7ae3SMatthew Knepley 
3045000e7ae3SMatthew Knepley   Calling sequence of func:
30466bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3047000e7ae3SMatthew Knepley 
3048000e7ae3SMatthew Knepley   Level: intermediate
3049000e7ae3SMatthew Knepley 
3050dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3051000e7ae3SMatthew Knepley @*/
30527087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3053000e7ae3SMatthew Knepley {
3054000e7ae3SMatthew Knepley   PetscFunctionBegin;
30550700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3056ae60f76fSBarry Smith   ts->prestep = func;
3057000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3058000e7ae3SMatthew Knepley }
3059000e7ae3SMatthew Knepley 
306009ee8438SJed Brown /*@
30613f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
30623f2090d5SJed Brown 
30633f2090d5SJed Brown   Collective on TS
30643f2090d5SJed Brown 
30653f2090d5SJed Brown   Input Parameters:
30663f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
30673f2090d5SJed Brown 
30683f2090d5SJed Brown   Notes:
30693f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
30703f2090d5SJed Brown   so most users would not generally call this routine themselves.
30713f2090d5SJed Brown 
30723f2090d5SJed Brown   Level: developer
30733f2090d5SJed Brown 
30749be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
30753f2090d5SJed Brown @*/
30767087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
30773f2090d5SJed Brown {
30783f2090d5SJed Brown   PetscErrorCode ierr;
30793f2090d5SJed Brown 
30803f2090d5SJed Brown   PetscFunctionBegin;
30810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3082ae60f76fSBarry Smith   if (ts->prestep) {
30835efd42a4SStefano Zampini     Vec              U;
30845efd42a4SStefano Zampini     PetscObjectState sprev,spost;
30855efd42a4SStefano Zampini 
30865efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
30875efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3088ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
30895efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3090dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3091312ce896SJed Brown   }
30923f2090d5SJed Brown   PetscFunctionReturn(0);
30933f2090d5SJed Brown }
30943f2090d5SJed Brown 
3095b8123daeSJed Brown /*@C
3096b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3097b8123daeSJed Brown   called once at the beginning of each stage.
3098b8123daeSJed Brown 
3099b8123daeSJed Brown   Logically Collective on TS
3100b8123daeSJed Brown 
3101b8123daeSJed Brown   Input Parameters:
3102b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3103b8123daeSJed Brown - func - The function
3104b8123daeSJed Brown 
3105b8123daeSJed Brown   Calling sequence of func:
3106b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3107b8123daeSJed Brown 
3108b8123daeSJed Brown   Level: intermediate
3109b8123daeSJed Brown 
3110b8123daeSJed Brown   Note:
3111b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
311280275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3113b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3114b8123daeSJed Brown 
31159be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3116b8123daeSJed Brown @*/
3117b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3118b8123daeSJed Brown {
3119b8123daeSJed Brown   PetscFunctionBegin;
3120b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3121ae60f76fSBarry Smith   ts->prestage = func;
3122b8123daeSJed Brown   PetscFunctionReturn(0);
3123b8123daeSJed Brown }
3124b8123daeSJed Brown 
31259be3e283SDebojyoti Ghosh /*@C
31269be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31279be3e283SDebojyoti Ghosh   called once at the end of each stage.
31289be3e283SDebojyoti Ghosh 
31299be3e283SDebojyoti Ghosh   Logically Collective on TS
31309be3e283SDebojyoti Ghosh 
31319be3e283SDebojyoti Ghosh   Input Parameters:
31329be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
31339be3e283SDebojyoti Ghosh - func - The function
31349be3e283SDebojyoti Ghosh 
31359be3e283SDebojyoti Ghosh   Calling sequence of func:
31369be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
31379be3e283SDebojyoti Ghosh 
31389be3e283SDebojyoti Ghosh   Level: intermediate
31399be3e283SDebojyoti Ghosh 
31409be3e283SDebojyoti Ghosh   Note:
31419be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
314280275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
31439be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
31449be3e283SDebojyoti Ghosh 
31459be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
31469be3e283SDebojyoti Ghosh @*/
31479be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
31489be3e283SDebojyoti Ghosh {
31499be3e283SDebojyoti Ghosh   PetscFunctionBegin;
31509be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
31519be3e283SDebojyoti Ghosh   ts->poststage = func;
31529be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
31539be3e283SDebojyoti Ghosh }
31549be3e283SDebojyoti Ghosh 
3155c688d042SShri Abhyankar /*@C
3156c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3157c688d042SShri Abhyankar   called once at the end of each step evaluation.
3158c688d042SShri Abhyankar 
3159c688d042SShri Abhyankar   Logically Collective on TS
3160c688d042SShri Abhyankar 
3161c688d042SShri Abhyankar   Input Parameters:
3162c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3163c688d042SShri Abhyankar - func - The function
3164c688d042SShri Abhyankar 
3165c688d042SShri Abhyankar   Calling sequence of func:
3166c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3167c688d042SShri Abhyankar 
3168c688d042SShri Abhyankar   Level: intermediate
3169c688d042SShri Abhyankar 
3170c688d042SShri Abhyankar   Note:
31711785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
31721785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3173e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3174e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3175e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3176c688d042SShri Abhyankar 
3177c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3178c688d042SShri Abhyankar @*/
3179c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3180c688d042SShri Abhyankar {
3181c688d042SShri Abhyankar   PetscFunctionBegin;
3182c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3183c688d042SShri Abhyankar   ts->postevaluate = func;
3184c688d042SShri Abhyankar   PetscFunctionReturn(0);
3185c688d042SShri Abhyankar }
3186c688d042SShri Abhyankar 
3187b8123daeSJed Brown /*@
3188b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3189b8123daeSJed Brown 
3190b8123daeSJed Brown   Collective on TS
3191b8123daeSJed Brown 
3192b8123daeSJed Brown   Input Parameters:
3193b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
31949be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3195b8123daeSJed Brown 
3196b8123daeSJed Brown   Notes:
3197b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3198b8123daeSJed Brown   most users would not generally call this routine themselves.
3199b8123daeSJed Brown 
3200b8123daeSJed Brown   Level: developer
3201b8123daeSJed Brown 
32029be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3203b8123daeSJed Brown @*/
3204b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3205b8123daeSJed Brown {
3206b8123daeSJed Brown   PetscFunctionBegin;
3207b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3208ae60f76fSBarry Smith   if (ts->prestage) {
3209ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3210b8123daeSJed Brown   }
3211b8123daeSJed Brown   PetscFunctionReturn(0);
3212b8123daeSJed Brown }
3213b8123daeSJed Brown 
32149be3e283SDebojyoti Ghosh /*@
32159be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
32169be3e283SDebojyoti Ghosh 
32179be3e283SDebojyoti Ghosh   Collective on TS
32189be3e283SDebojyoti Ghosh 
32199be3e283SDebojyoti Ghosh   Input Parameters:
32209be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
32219be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32229be3e283SDebojyoti Ghosh   stageindex  - Stage number
32239be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32249be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32259be3e283SDebojyoti Ghosh 
32269be3e283SDebojyoti Ghosh   Notes:
32279be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32289be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32299be3e283SDebojyoti Ghosh 
32309be3e283SDebojyoti Ghosh   Level: developer
32319be3e283SDebojyoti Ghosh 
32329be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
32339be3e283SDebojyoti Ghosh @*/
32349be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
32359be3e283SDebojyoti Ghosh {
32369be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32379be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32384beae5d8SLisandro Dalcin   if (ts->poststage) {
32399be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
32409be3e283SDebojyoti Ghosh   }
32419be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32429be3e283SDebojyoti Ghosh }
32439be3e283SDebojyoti Ghosh 
3244c688d042SShri Abhyankar /*@
3245c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3246c688d042SShri Abhyankar 
3247c688d042SShri Abhyankar   Collective on TS
3248c688d042SShri Abhyankar 
3249c688d042SShri Abhyankar   Input Parameters:
3250c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3251c688d042SShri Abhyankar 
3252c688d042SShri Abhyankar   Notes:
3253c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3254c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3255c688d042SShri Abhyankar 
3256c688d042SShri Abhyankar   Level: developer
3257c688d042SShri Abhyankar 
3258c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3259c688d042SShri Abhyankar @*/
3260c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3261c688d042SShri Abhyankar {
3262c688d042SShri Abhyankar   PetscErrorCode ierr;
3263c688d042SShri Abhyankar 
3264c688d042SShri Abhyankar   PetscFunctionBegin;
3265c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3266c688d042SShri Abhyankar   if (ts->postevaluate) {
3267dcb233daSLisandro Dalcin     Vec              U;
3268dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3269dcb233daSLisandro Dalcin 
3270dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3271dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3272c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3273dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3274dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3275c688d042SShri Abhyankar   }
3276c688d042SShri Abhyankar   PetscFunctionReturn(0);
3277c688d042SShri Abhyankar }
3278c688d042SShri Abhyankar 
3279ac226902SBarry Smith /*@C
3280000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
32813f2090d5SJed Brown   called once at the end of each time step.
3282000e7ae3SMatthew Knepley 
32833f9fe445SBarry Smith   Logically Collective on TS
3284000e7ae3SMatthew Knepley 
3285000e7ae3SMatthew Knepley   Input Parameters:
3286000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3287000e7ae3SMatthew Knepley - func - The function
3288000e7ae3SMatthew Knepley 
3289000e7ae3SMatthew Knepley   Calling sequence of func:
3290b8123daeSJed Brown $ func (TS ts);
3291000e7ae3SMatthew Knepley 
32921785ff2aSShri Abhyankar   Notes:
32931785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
32941785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
32951785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
32961785ff2aSShri Abhyankar 
3297000e7ae3SMatthew Knepley   Level: intermediate
3298000e7ae3SMatthew Knepley 
3299dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3300000e7ae3SMatthew Knepley @*/
33017087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3302000e7ae3SMatthew Knepley {
3303000e7ae3SMatthew Knepley   PetscFunctionBegin;
33040700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3305ae60f76fSBarry Smith   ts->poststep = func;
3306000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3307000e7ae3SMatthew Knepley }
3308000e7ae3SMatthew Knepley 
330909ee8438SJed Brown /*@
33103f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
33113f2090d5SJed Brown 
33123f2090d5SJed Brown   Collective on TS
33133f2090d5SJed Brown 
33143f2090d5SJed Brown   Input Parameters:
33153f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33163f2090d5SJed Brown 
33173f2090d5SJed Brown   Notes:
33183f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33193f2090d5SJed Brown   so most users would not generally call this routine themselves.
33203f2090d5SJed Brown 
33213f2090d5SJed Brown   Level: developer
33223f2090d5SJed Brown 
33233f2090d5SJed Brown @*/
33247087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33253f2090d5SJed Brown {
33263f2090d5SJed Brown   PetscErrorCode ierr;
33273f2090d5SJed Brown 
33283f2090d5SJed Brown   PetscFunctionBegin;
33290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3330ae60f76fSBarry Smith   if (ts->poststep) {
33315efd42a4SStefano Zampini     Vec              U;
33325efd42a4SStefano Zampini     PetscObjectState sprev,spost;
33335efd42a4SStefano Zampini 
33345efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
33355efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3336ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
33375efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3338dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
333972ac3e02SJed Brown   }
33403f2090d5SJed Brown   PetscFunctionReturn(0);
33413f2090d5SJed Brown }
33423f2090d5SJed Brown 
3343d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3344d763cef2SBarry Smith 
3345d763cef2SBarry Smith /*@C
3346a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3347d763cef2SBarry Smith    timestep to display the iteration's  progress.
3348d763cef2SBarry Smith 
33493f9fe445SBarry Smith    Logically Collective on TS
3350d763cef2SBarry Smith 
3351d763cef2SBarry Smith    Input Parameters:
3352d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3353e213d8f1SJed Brown .  monitor - monitoring routine
3354329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
33550298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3356b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
33570298fd71SBarry Smith           (may be NULL)
3358d763cef2SBarry Smith 
3359e213d8f1SJed Brown    Calling sequence of monitor:
3360dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3361d763cef2SBarry Smith 
3362d763cef2SBarry Smith +    ts - the TS context
336363e21af5SBarry 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)
33641f06c33eSBarry Smith .    time - current time
33650910c330SBarry Smith .    u - current iterate
3366d763cef2SBarry Smith -    mctx - [optional] monitoring context
3367d763cef2SBarry Smith 
3368d763cef2SBarry Smith    Notes:
3369d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3370d763cef2SBarry Smith    already has been loaded.
3371d763cef2SBarry Smith 
337295452b02SPatrick Sanan    Fortran Notes:
337395452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3374025f1a04SBarry Smith 
3375d763cef2SBarry Smith    Level: intermediate
3376d763cef2SBarry Smith 
3377a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3378d763cef2SBarry Smith @*/
3379c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3380d763cef2SBarry Smith {
338178064530SBarry Smith   PetscErrorCode ierr;
338278064530SBarry Smith   PetscInt       i;
338378064530SBarry Smith   PetscBool      identical;
338478064530SBarry Smith 
3385d763cef2SBarry Smith   PetscFunctionBegin;
33860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
338778064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
338878064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
338978064530SBarry Smith     if (identical) PetscFunctionReturn(0);
339078064530SBarry Smith   }
339117186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3392d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
33938704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3394d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3395d763cef2SBarry Smith   PetscFunctionReturn(0);
3396d763cef2SBarry Smith }
3397d763cef2SBarry Smith 
3398d763cef2SBarry Smith /*@C
3399a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3400d763cef2SBarry Smith 
34013f9fe445SBarry Smith    Logically Collective on TS
3402d763cef2SBarry Smith 
3403d763cef2SBarry Smith    Input Parameters:
3404d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3405d763cef2SBarry Smith 
3406d763cef2SBarry Smith    Notes:
3407d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3408d763cef2SBarry Smith 
3409d763cef2SBarry Smith    Level: intermediate
3410d763cef2SBarry Smith 
3411a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3412d763cef2SBarry Smith @*/
34137087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3414d763cef2SBarry Smith {
3415d952e501SBarry Smith   PetscErrorCode ierr;
3416d952e501SBarry Smith   PetscInt       i;
3417d952e501SBarry Smith 
3418d763cef2SBarry Smith   PetscFunctionBegin;
34190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3420d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34218704b422SBarry Smith     if (ts->monitordestroy[i]) {
34228704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3423d952e501SBarry Smith     }
3424d952e501SBarry Smith   }
3425d763cef2SBarry Smith   ts->numbermonitors = 0;
3426d763cef2SBarry Smith   PetscFunctionReturn(0);
3427d763cef2SBarry Smith }
3428d763cef2SBarry Smith 
3429721cd6eeSBarry Smith /*@C
3430721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
34315516499fSSatish Balay 
34325516499fSSatish Balay    Level: intermediate
343341251cbbSSatish Balay 
343463e21af5SBarry Smith .seealso:  TSMonitorSet()
343541251cbbSSatish Balay @*/
3436721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3437d763cef2SBarry Smith {
3438dfbe8321SBarry Smith   PetscErrorCode ierr;
3439721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
344041aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3441d132466eSBarry Smith 
3442d763cef2SBarry Smith   PetscFunctionBegin;
34434d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
344441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
344541aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3446721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
344741aca3d6SBarry Smith   if (iascii) {
3448649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
344963e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
345063e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
345163e21af5SBarry Smith     } else {
34528392e04aSShri 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);
345363e21af5SBarry Smith     }
3454649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
345541aca3d6SBarry Smith   } else if (ibinary) {
345641aca3d6SBarry Smith     PetscMPIInt rank;
345741aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
345841aca3d6SBarry Smith     if (!rank) {
3459450a797fSBarry Smith       PetscBool skipHeader;
3460450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3461450a797fSBarry Smith 
3462450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3463450a797fSBarry Smith       if (!skipHeader) {
3464450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3465450a797fSBarry Smith        }
346641aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
346741aca3d6SBarry Smith     } else {
346841aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
346941aca3d6SBarry Smith     }
347041aca3d6SBarry Smith   }
3471721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3472d763cef2SBarry Smith   PetscFunctionReturn(0);
3473d763cef2SBarry Smith }
3474d763cef2SBarry Smith 
3475cc9c3a59SBarry Smith /*@C
3476cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3477cc9c3a59SBarry Smith 
3478cc9c3a59SBarry Smith    Level: intermediate
3479cc9c3a59SBarry Smith 
3480cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3481cc9c3a59SBarry Smith @*/
3482cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3483cc9c3a59SBarry Smith {
3484cc9c3a59SBarry Smith   PetscErrorCode ierr;
3485cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3486cc9c3a59SBarry Smith   PetscBool      iascii;
3487cc9c3a59SBarry Smith   PetscReal      max,min;
3488cc9c3a59SBarry Smith 
3489cc9c3a59SBarry Smith 
3490cc9c3a59SBarry Smith   PetscFunctionBegin;
3491cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3492cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3493cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3494cc9c3a59SBarry Smith   if (iascii) {
3495cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3496cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3497cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
34985132926bSBarry 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);
3499cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3500cc9c3a59SBarry Smith   }
3501cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3502cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3503cc9c3a59SBarry Smith }
3504cc9c3a59SBarry Smith 
3505cd652676SJed Brown /*@
3506cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3507cd652676SJed Brown 
3508cd652676SJed Brown    Collective on TS
3509cd652676SJed Brown 
3510cd652676SJed Brown    Input Argument:
3511cd652676SJed Brown +  ts - time stepping context
3512cd652676SJed Brown -  t - time to interpolate to
3513cd652676SJed Brown 
3514cd652676SJed Brown    Output Argument:
35150910c330SBarry Smith .  U - state at given time
3516cd652676SJed Brown 
3517cd652676SJed Brown    Level: intermediate
3518cd652676SJed Brown 
3519cd652676SJed Brown    Developer Notes:
3520cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3521cd652676SJed Brown 
3522874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3523cd652676SJed Brown @*/
35240910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3525cd652676SJed Brown {
3526cd652676SJed Brown   PetscErrorCode ierr;
3527cd652676SJed Brown 
3528cd652676SJed Brown   PetscFunctionBegin;
3529cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3530b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3531be5899b3SLisandro 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);
3532ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35330910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3534cd652676SJed Brown   PetscFunctionReturn(0);
3535cd652676SJed Brown }
3536cd652676SJed Brown 
3537d763cef2SBarry Smith /*@
35386d9e5789SSean Farley    TSStep - Steps one time step
3539d763cef2SBarry Smith 
3540d763cef2SBarry Smith    Collective on TS
3541d763cef2SBarry Smith 
3542d763cef2SBarry Smith    Input Parameter:
3543d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3544d763cef2SBarry Smith 
354527829d71SBarry Smith    Level: developer
3546d763cef2SBarry Smith 
3547b8123daeSJed Brown    Notes:
354827829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
354927829d71SBarry Smith 
3550b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3551b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3552b8123daeSJed Brown 
355319eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
355419eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
355525cb2221SBarry Smith 
35569be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3557d763cef2SBarry Smith @*/
3558193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3559d763cef2SBarry Smith {
3560dfbe8321SBarry Smith   PetscErrorCode   ierr;
3561fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3562be5899b3SLisandro Dalcin   PetscReal        ptime;
3563d763cef2SBarry Smith 
3564d763cef2SBarry Smith   PetscFunctionBegin;
35650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3566fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3567fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3568fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3569fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3570fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3571fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3572302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3573fffbeea8SBarry Smith 
3574d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
357568bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3576d405a339SMatthew Knepley 
3577ef85077eSLisandro 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>");
3578a6772fa2SLisandro 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()");
3579be5899b3SLisandro 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");
3580a6772fa2SLisandro Dalcin 
3581be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3582be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
35832808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3584e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3585d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3586193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3587d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3588be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
35892808aa04SLisandro Dalcin   ts->steps++;
3590be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
359128d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3592362cd11cSLisandro Dalcin 
3593362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3594cef5090cSJed Brown     if (ts->errorifstepfailed) {
359508c7845fSBarry Smith       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
359608c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3597d2daff3dSHong Zhang     }
359808c7845fSBarry Smith   } else if (!ts->reason) {
359908c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
360008c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
360108c7845fSBarry Smith   }
360208c7845fSBarry Smith   PetscFunctionReturn(0);
360308c7845fSBarry Smith }
360408c7845fSBarry Smith 
360508c7845fSBarry Smith /*@
36067cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36077cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36087cbde773SLisandro Dalcin 
36097cbde773SLisandro Dalcin    Collective on TS
36107cbde773SLisandro Dalcin 
36117cbde773SLisandro Dalcin    Input Arguments:
36127cbde773SLisandro Dalcin +  ts - time stepping context
36137cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36147cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
36157cbde773SLisandro Dalcin 
36167cbde773SLisandro Dalcin    Output Arguments:
36177cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
36187cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
36197cbde773SLisandro Dalcin 
36207cbde773SLisandro Dalcin    Level: advanced
36217cbde773SLisandro Dalcin 
36227cbde773SLisandro Dalcin    Notes:
36237cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36247cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36257cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36267cbde773SLisandro Dalcin 
36277cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36287cbde773SLisandro Dalcin @*/
36297cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36307cbde773SLisandro Dalcin {
36317cbde773SLisandro Dalcin   PetscErrorCode ierr;
36327cbde773SLisandro Dalcin 
36337cbde773SLisandro Dalcin   PetscFunctionBegin;
36347cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36357cbde773SLisandro Dalcin   PetscValidType(ts,1);
36367cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
36377cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36387cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36397cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36407cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36417cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36427cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
36437cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36447cbde773SLisandro Dalcin }
36457cbde773SLisandro Dalcin 
364605175c85SJed Brown /*@
364705175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
364805175c85SJed Brown 
36491c3436cfSJed Brown    Collective on TS
365005175c85SJed Brown 
365105175c85SJed Brown    Input Arguments:
36521c3436cfSJed Brown +  ts - time stepping context
36531c3436cfSJed Brown .  order - desired order of accuracy
36540298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
365505175c85SJed Brown 
365605175c85SJed Brown    Output Arguments:
36570910c330SBarry Smith .  U - state at the end of the current step
365805175c85SJed Brown 
365905175c85SJed Brown    Level: advanced
366005175c85SJed Brown 
3661108c343cSJed Brown    Notes:
3662108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3663108c343cSJed 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.
3664108c343cSJed Brown 
36651c3436cfSJed Brown .seealso: TSStep(), TSAdapt
366605175c85SJed Brown @*/
36670910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
366805175c85SJed Brown {
366905175c85SJed Brown   PetscErrorCode ierr;
367005175c85SJed Brown 
367105175c85SJed Brown   PetscFunctionBegin;
367205175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
367305175c85SJed Brown   PetscValidType(ts,1);
36740910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3675ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
36760910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
367705175c85SJed Brown   PetscFunctionReturn(0);
367805175c85SJed Brown }
367905175c85SJed Brown 
3680b1cb36f3SHong Zhang /*@
36816a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
36826a4d4014SLisandro Dalcin 
36836a4d4014SLisandro Dalcin    Collective on TS
36846a4d4014SLisandro Dalcin 
36856a4d4014SLisandro Dalcin    Input Parameter:
36866a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
368763e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
368863e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
36895a3a76d0SJed Brown 
36906a4d4014SLisandro Dalcin    Level: beginner
36916a4d4014SLisandro Dalcin 
36925a3a76d0SJed Brown    Notes:
36935a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
36945a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
36955a3a76d0SJed Brown    stepped over the final time.
36965a3a76d0SJed Brown 
369763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
36986a4d4014SLisandro Dalcin @*/
3699cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
37006a4d4014SLisandro Dalcin {
3701b06615a5SLisandro Dalcin   Vec               solution;
37026a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3703f22f69f0SBarry Smith 
37046a4d4014SLisandro Dalcin   PetscFunctionBegin;
37050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3706f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3707feed9e9dSBarry Smith 
3708ee41a567SStefano 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 */
37090910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3710b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3711b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3712b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
37135a3a76d0SJed Brown     }
37140910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3715715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3716bbd56ea5SKarl Rupp   } else if (u) {
37170910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
37185a3a76d0SJed Brown   }
3719b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
372068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3721a6772fa2SLisandro Dalcin 
3722ef85077eSLisandro 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>");
3723a6772fa2SLisandro 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()");
3724a6772fa2SLisandro 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");
3725a6772fa2SLisandro Dalcin 
3726715f1b00SHong Zhang   if (ts->forward_solve) {
3727715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3728715f1b00SHong Zhang   }
3729715f1b00SHong Zhang 
3730e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3731715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3732e7069c78SShri      TSTrajectory to incorrectly save the output files
3733e7069c78SShri   */
3734715f1b00SHong Zhang   /* reset time step and iteration counters */
37352808aa04SLisandro Dalcin   if (!ts->steps) {
37365ef26d82SJed Brown     ts->ksp_its           = 0;
37375ef26d82SJed Brown     ts->snes_its          = 0;
3738c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3739c610991cSLisandro Dalcin     ts->reject            = 0;
37402808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
37412808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
37422808aa04SLisandro Dalcin   }
374310bc0e4dSStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime;
3744193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3745193ac0bcSJed Brown 
3746ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3747f05ece33SBarry Smith 
3748193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3749193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3750a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3751cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3752a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3753be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3754db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3755db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3756e7069c78SShri 
37572808aa04SLisandro Dalcin     if (!ts->steps) {
37582808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37596427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37602808aa04SLisandro Dalcin     }
37616427ac75SLisandro Dalcin 
3762e1a7a14fSJed Brown     while (!ts->reason) {
37632808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37649687d888SLisandro Dalcin       if (!ts->steprollback) {
37659687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
37669687d888SLisandro Dalcin       }
3767193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3768f3b1f45cSBarry Smith       if (ts->testjacobian) {
3769f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3770f3b1f45cSBarry Smith       }
3771f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3772f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3773f3b1f45cSBarry Smith       }
3774cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
3775b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3776b1cb36f3SHong Zhang       }
377758818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3778715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3779715f1b00SHong Zhang       }
378010b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3781e783b05fSHong 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. */
378258818c2dSLisandro Dalcin       if (ts->steprollback) {
378358818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
378458818c2dSLisandro Dalcin       }
3785e783b05fSHong Zhang       if (!ts->steprollback) {
37862808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37871eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3788aeb4809dSShri Abhyankar       }
3789193ac0bcSJed Brown     }
37902808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37916427ac75SLisandro Dalcin 
379249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
37930910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3794cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3795b06615a5SLisandro Dalcin       solution = u;
379663e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
37970574a7fbSJed Brown     } else {
3798ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3799cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3800b06615a5SLisandro Dalcin       solution = ts->vec_sol;
38010574a7fbSJed Brown     }
3802193ac0bcSJed Brown   }
3803d2daff3dSHong Zhang 
3804ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
380563e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
380656f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3807ef222394SBarry Smith   if (ts->adjoint_solve) {
3808ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
38092b0a91c0SBarry Smith   }
38106a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
38116a4d4014SLisandro Dalcin }
38126a4d4014SLisandro Dalcin 
38137db568b7SBarry Smith /*@C
3814228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3815228d79bcSJed Brown 
3816228d79bcSJed Brown    Collective on TS
3817228d79bcSJed Brown 
3818228d79bcSJed Brown    Input Parameters:
3819228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3820228d79bcSJed Brown .  step - step number that has just completed
3821228d79bcSJed Brown .  ptime - model time of the state
38220910c330SBarry Smith -  u - state at the current model time
3823228d79bcSJed Brown 
3824228d79bcSJed Brown    Notes:
38257db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
38267db568b7SBarry Smith    Users would almost never call this routine directly.
3827228d79bcSJed Brown 
382863e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
382963e21af5SBarry Smith 
38307db568b7SBarry Smith    Level: developer
3831228d79bcSJed Brown 
3832228d79bcSJed Brown @*/
38330910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3834d763cef2SBarry Smith {
3835d6152f81SLisandro Dalcin   DM             dm;
3836a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3837d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3838d763cef2SBarry Smith 
3839d763cef2SBarry Smith   PetscFunctionBegin;
3840b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3841b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3842d6152f81SLisandro Dalcin 
3843d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3844d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3845d6152f81SLisandro Dalcin 
38468860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
3847d763cef2SBarry Smith   for (i=0; i<n; i++) {
38480910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3849d763cef2SBarry Smith   }
38508860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
3851d763cef2SBarry Smith   PetscFunctionReturn(0);
3852d763cef2SBarry Smith }
3853d763cef2SBarry Smith 
3854d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3855d763cef2SBarry Smith /*@C
38567db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3857a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3858d763cef2SBarry Smith 
3859d763cef2SBarry Smith    Collective on TS
3860d763cef2SBarry Smith 
3861d763cef2SBarry Smith    Input Parameters:
3862d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3863d763cef2SBarry Smith .  label - the title to put in the title bar
38647c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3865a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3866a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3867d763cef2SBarry Smith 
3868d763cef2SBarry Smith    Output Parameter:
38690b039ecaSBarry Smith .  ctx - the context
3870d763cef2SBarry Smith 
3871d763cef2SBarry Smith    Options Database Key:
38724f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
38738b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
38747db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
38757db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
38767db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
38777db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3878b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3879d763cef2SBarry Smith 
3880d763cef2SBarry Smith    Notes:
3881a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3882d763cef2SBarry Smith 
38837db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
38847db568b7SBarry Smith 
38857db568b7SBarry 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
38867db568b7SBarry 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
38877db568b7SBarry Smith    as the first argument.
38887db568b7SBarry Smith 
38897db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
38907db568b7SBarry Smith 
3891d763cef2SBarry Smith    Level: intermediate
3892d763cef2SBarry Smith 
38937db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
38947db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
38957db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
38967db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
38977db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
38987c922b88SBarry Smith 
3899d763cef2SBarry Smith @*/
3900a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3901d763cef2SBarry Smith {
39027f52daa2SLisandro Dalcin   PetscDraw      draw;
3903dfbe8321SBarry Smith   PetscErrorCode ierr;
3904d763cef2SBarry Smith 
3905d763cef2SBarry Smith   PetscFunctionBegin;
3906b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
39077f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39087f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39097f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3910287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
39117f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3912a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3913d763cef2SBarry Smith   PetscFunctionReturn(0);
3914d763cef2SBarry Smith }
3915d763cef2SBarry Smith 
3916b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3917d763cef2SBarry Smith {
39180b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3919c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3920dfbe8321SBarry Smith   PetscErrorCode ierr;
3921d763cef2SBarry Smith 
3922d763cef2SBarry Smith   PetscFunctionBegin;
392363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
3924b06615a5SLisandro Dalcin   if (!step) {
3925a9f9c1f6SBarry Smith     PetscDrawAxis axis;
39268b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
3927a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
39288b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
3929a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3930a9f9c1f6SBarry Smith   }
3931c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
39328b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
39330b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3934b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
39350b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
39366934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
39373923b477SBarry Smith   }
3938d763cef2SBarry Smith   PetscFunctionReturn(0);
3939d763cef2SBarry Smith }
3940d763cef2SBarry Smith 
3941d763cef2SBarry Smith /*@C
3942a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3943a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3944d763cef2SBarry Smith 
39450b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3946d763cef2SBarry Smith 
3947d763cef2SBarry Smith    Input Parameter:
39480b039ecaSBarry Smith .  ctx - the monitor context
3949d763cef2SBarry Smith 
3950d763cef2SBarry Smith    Level: intermediate
3951d763cef2SBarry Smith 
39524f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3953d763cef2SBarry Smith @*/
3954a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3955d763cef2SBarry Smith {
3956dfbe8321SBarry Smith   PetscErrorCode ierr;
3957d763cef2SBarry Smith 
3958d763cef2SBarry Smith   PetscFunctionBegin;
39597684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
39607684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
39617684fa3eSBarry Smith   }
39620b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
396331152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3964387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3965387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3966387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
39670b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3968d763cef2SBarry Smith   PetscFunctionReturn(0);
3969d763cef2SBarry Smith }
3970d763cef2SBarry Smith 
39711b575b74SJoseph Pusztay /*
39721b575b74SJoseph Pusztay 
39731b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
39741b575b74SJoseph Pusztay 
39751b575b74SJoseph Pusztay */
39761b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
39771b575b74SJoseph Pusztay {
39781b575b74SJoseph Pusztay   PetscDraw      draw;
39791b575b74SJoseph Pusztay   PetscErrorCode ierr;
39801b575b74SJoseph Pusztay 
39811b575b74SJoseph Pusztay   PetscFunctionBegin;
39821b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
39831b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39841b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39851b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
39861b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
39871b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
39881b575b74SJoseph Pusztay   PetscFunctionReturn(0);
39891b575b74SJoseph Pusztay 
39901b575b74SJoseph Pusztay }
39911b575b74SJoseph Pusztay 
39921b575b74SJoseph Pusztay /*
39931b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
39941b575b74SJoseph Pusztay */
39951b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
39961b575b74SJoseph Pusztay {
39971b575b74SJoseph Pusztay   PetscErrorCode ierr;
39981b575b74SJoseph Pusztay 
39991b575b74SJoseph Pusztay   PetscFunctionBegin;
40001b575b74SJoseph Pusztay 
40011b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
40021b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
40031b575b74SJoseph Pusztay 
40041b575b74SJoseph Pusztay   PetscFunctionReturn(0);
40051b575b74SJoseph Pusztay 
40061b575b74SJoseph Pusztay }
40071b575b74SJoseph Pusztay 
4008d763cef2SBarry Smith /*@
4009b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4010d763cef2SBarry Smith 
4011d763cef2SBarry Smith    Not Collective
4012d763cef2SBarry Smith 
4013d763cef2SBarry Smith    Input Parameter:
4014d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4015d763cef2SBarry Smith 
4016d763cef2SBarry Smith    Output Parameter:
401719eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4018d763cef2SBarry Smith 
4019d763cef2SBarry Smith    Level: beginner
4020d763cef2SBarry Smith 
4021b8123daeSJed Brown    Note:
4022b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40239be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4024b8123daeSJed Brown 
40258f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4026d763cef2SBarry Smith 
4027d763cef2SBarry Smith @*/
40287087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4029d763cef2SBarry Smith {
4030d763cef2SBarry Smith   PetscFunctionBegin;
40310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4032f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4033d763cef2SBarry Smith   *t = ts->ptime;
4034d763cef2SBarry Smith   PetscFunctionReturn(0);
4035d763cef2SBarry Smith }
4036d763cef2SBarry Smith 
4037e5e524a1SHong Zhang /*@
4038e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4039e5e524a1SHong Zhang 
4040e5e524a1SHong Zhang    Not Collective
4041e5e524a1SHong Zhang 
4042e5e524a1SHong Zhang    Input Parameter:
4043e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4044e5e524a1SHong Zhang 
4045e5e524a1SHong Zhang    Output Parameter:
4046e5e524a1SHong Zhang .  t  - the previous time
4047e5e524a1SHong Zhang 
4048e5e524a1SHong Zhang    Level: beginner
4049e5e524a1SHong Zhang 
4050aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4051e5e524a1SHong Zhang 
4052e5e524a1SHong Zhang @*/
4053e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4054e5e524a1SHong Zhang {
4055e5e524a1SHong Zhang   PetscFunctionBegin;
4056e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4057e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4058e5e524a1SHong Zhang   *t = ts->ptime_prev;
4059e5e524a1SHong Zhang   PetscFunctionReturn(0);
4060e5e524a1SHong Zhang }
4061e5e524a1SHong Zhang 
40626a4d4014SLisandro Dalcin /*@
40636a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40646a4d4014SLisandro Dalcin 
40653f9fe445SBarry Smith    Logically Collective on TS
40666a4d4014SLisandro Dalcin 
40676a4d4014SLisandro Dalcin    Input Parameters:
40686a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40696a4d4014SLisandro Dalcin -  time - the time
40706a4d4014SLisandro Dalcin 
40716a4d4014SLisandro Dalcin    Level: intermediate
40726a4d4014SLisandro Dalcin 
407319eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
40746a4d4014SLisandro Dalcin 
40756a4d4014SLisandro Dalcin @*/
40767087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
40776a4d4014SLisandro Dalcin {
40786a4d4014SLisandro Dalcin   PetscFunctionBegin;
40790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4080c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
40816a4d4014SLisandro Dalcin   ts->ptime = t;
40826a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40836a4d4014SLisandro Dalcin }
40846a4d4014SLisandro Dalcin 
4085d763cef2SBarry Smith /*@C
4086d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4087d763cef2SBarry Smith    TS options in the database.
4088d763cef2SBarry Smith 
40893f9fe445SBarry Smith    Logically Collective on TS
4090d763cef2SBarry Smith 
4091d763cef2SBarry Smith    Input Parameter:
4092d763cef2SBarry Smith +  ts     - The TS context
4093d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4094d763cef2SBarry Smith 
4095d763cef2SBarry Smith    Notes:
4096d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4097d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4098d763cef2SBarry Smith    hyphen.
4099d763cef2SBarry Smith 
4100d763cef2SBarry Smith    Level: advanced
4101d763cef2SBarry Smith 
4102d763cef2SBarry Smith .seealso: TSSetFromOptions()
4103d763cef2SBarry Smith 
4104d763cef2SBarry Smith @*/
41057087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4106d763cef2SBarry Smith {
4107dfbe8321SBarry Smith   PetscErrorCode ierr;
4108089b2837SJed Brown   SNES           snes;
4109d763cef2SBarry Smith 
4110d763cef2SBarry Smith   PetscFunctionBegin;
41110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4112d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4113089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4114089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4115d763cef2SBarry Smith   PetscFunctionReturn(0);
4116d763cef2SBarry Smith }
4117d763cef2SBarry Smith 
4118d763cef2SBarry Smith /*@C
4119d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4120d763cef2SBarry Smith    TS options in the database.
4121d763cef2SBarry Smith 
41223f9fe445SBarry Smith    Logically Collective on TS
4123d763cef2SBarry Smith 
4124d763cef2SBarry Smith    Input Parameter:
4125d763cef2SBarry Smith +  ts     - The TS context
4126d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4127d763cef2SBarry Smith 
4128d763cef2SBarry Smith    Notes:
4129d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4130d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4131d763cef2SBarry Smith    hyphen.
4132d763cef2SBarry Smith 
4133d763cef2SBarry Smith    Level: advanced
4134d763cef2SBarry Smith 
4135d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4136d763cef2SBarry Smith 
4137d763cef2SBarry Smith @*/
41387087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4139d763cef2SBarry Smith {
4140dfbe8321SBarry Smith   PetscErrorCode ierr;
4141089b2837SJed Brown   SNES           snes;
4142d763cef2SBarry Smith 
4143d763cef2SBarry Smith   PetscFunctionBegin;
41440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4145d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4146089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4147089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4148d763cef2SBarry Smith   PetscFunctionReturn(0);
4149d763cef2SBarry Smith }
4150d763cef2SBarry Smith 
4151d763cef2SBarry Smith /*@C
4152d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4153d763cef2SBarry Smith    TS options in the database.
4154d763cef2SBarry Smith 
4155d763cef2SBarry Smith    Not Collective
4156d763cef2SBarry Smith 
4157d763cef2SBarry Smith    Input Parameter:
4158d763cef2SBarry Smith .  ts - The TS context
4159d763cef2SBarry Smith 
4160d763cef2SBarry Smith    Output Parameter:
4161d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4162d763cef2SBarry Smith 
416395452b02SPatrick Sanan    Notes:
416495452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4165d763cef2SBarry Smith    sufficient length to hold the prefix.
4166d763cef2SBarry Smith 
4167d763cef2SBarry Smith    Level: intermediate
4168d763cef2SBarry Smith 
4169d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4170d763cef2SBarry Smith @*/
41717087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4172d763cef2SBarry Smith {
4173dfbe8321SBarry Smith   PetscErrorCode ierr;
4174d763cef2SBarry Smith 
4175d763cef2SBarry Smith   PetscFunctionBegin;
41760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41774482741eSBarry Smith   PetscValidPointer(prefix,2);
4178d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4179d763cef2SBarry Smith   PetscFunctionReturn(0);
4180d763cef2SBarry Smith }
4181d763cef2SBarry Smith 
4182d763cef2SBarry Smith /*@C
4183d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4184d763cef2SBarry Smith 
4185d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4186d763cef2SBarry Smith 
4187d763cef2SBarry Smith    Input Parameter:
4188d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4189d763cef2SBarry Smith 
4190d763cef2SBarry Smith    Output Parameters:
4191e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4192e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4193e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4194e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4195d763cef2SBarry Smith 
419695452b02SPatrick Sanan    Notes:
419795452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4198d763cef2SBarry Smith 
4199d763cef2SBarry Smith    Level: intermediate
4200d763cef2SBarry Smith 
420180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4202d763cef2SBarry Smith 
4203d763cef2SBarry Smith @*/
4204e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4205d763cef2SBarry Smith {
4206089b2837SJed Brown   PetscErrorCode ierr;
420724989b8cSPeter Brune   DM             dm;
4208089b2837SJed Brown 
4209d763cef2SBarry Smith   PetscFunctionBegin;
421023a57915SBarry Smith   if (Amat || Pmat) {
421123a57915SBarry Smith     SNES snes;
4212089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
421323a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4214e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
421523a57915SBarry Smith   }
421624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
421724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4218d763cef2SBarry Smith   PetscFunctionReturn(0);
4219d763cef2SBarry Smith }
4220d763cef2SBarry Smith 
42212eca1d9cSJed Brown /*@C
42222eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42232eca1d9cSJed Brown 
42242eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42252eca1d9cSJed Brown 
42262eca1d9cSJed Brown    Input Parameter:
42272eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42282eca1d9cSJed Brown 
42292eca1d9cSJed Brown    Output Parameters:
4230e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4231e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42322eca1d9cSJed Brown .  f   - The function to compute the matrices
42332eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42342eca1d9cSJed Brown 
423595452b02SPatrick Sanan    Notes:
423695452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42372eca1d9cSJed Brown 
42382eca1d9cSJed Brown    Level: advanced
42392eca1d9cSJed Brown 
424080275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42412eca1d9cSJed Brown 
42422eca1d9cSJed Brown @*/
4243e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
42442eca1d9cSJed Brown {
4245089b2837SJed Brown   PetscErrorCode ierr;
424624989b8cSPeter Brune   DM             dm;
42470910c330SBarry Smith 
42482eca1d9cSJed Brown   PetscFunctionBegin;
4249c0aab802Sstefano_zampini   if (Amat || Pmat) {
4250c0aab802Sstefano_zampini     SNES snes;
4251089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4252f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4253e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4254c0aab802Sstefano_zampini   }
425524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
425624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
42572eca1d9cSJed Brown   PetscFunctionReturn(0);
42582eca1d9cSJed Brown }
42592eca1d9cSJed Brown 
42601713a123SBarry Smith /*@C
426183a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
42621713a123SBarry Smith    VecView() for the solution at each timestep
42631713a123SBarry Smith 
42641713a123SBarry Smith    Collective on TS
42651713a123SBarry Smith 
42661713a123SBarry Smith    Input Parameters:
42671713a123SBarry Smith +  ts - the TS context
42681713a123SBarry Smith .  step - current time-step
4269142b95e3SSatish Balay .  ptime - current time
42700298fd71SBarry Smith -  dummy - either a viewer or NULL
42711713a123SBarry Smith 
427299fdda47SBarry Smith    Options Database:
427399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
427499fdda47SBarry Smith 
427595452b02SPatrick Sanan    Notes:
427695452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
427799fdda47SBarry Smith        will look bad
427899fdda47SBarry Smith 
42791713a123SBarry Smith    Level: intermediate
42801713a123SBarry Smith 
4281a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42821713a123SBarry Smith @*/
42830910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42841713a123SBarry Smith {
4285dfbe8321SBarry Smith   PetscErrorCode   ierr;
428683a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4287473a3ab2SBarry Smith   PetscDraw        draw;
42881713a123SBarry Smith 
42891713a123SBarry Smith   PetscFunctionBegin;
42906083293cSBarry Smith   if (!step && ictx->showinitial) {
42916083293cSBarry Smith     if (!ictx->initialsolution) {
42920910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
42931713a123SBarry Smith     }
42940910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
42956083293cSBarry Smith   }
4296b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42970dcf80beSBarry Smith 
42986083293cSBarry Smith   if (ictx->showinitial) {
42996083293cSBarry Smith     PetscReal pause;
43006083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
43016083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
43026083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
43036083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
43046083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
43056083293cSBarry Smith   }
43060910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4307473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
430851fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4309473a3ab2SBarry Smith     char      time[32];
4310473a3ab2SBarry Smith 
4311473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
431285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4313473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4314473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
431551fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4316473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4317473a3ab2SBarry Smith   }
4318473a3ab2SBarry Smith 
43196083293cSBarry Smith   if (ictx->showinitial) {
43206083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
43216083293cSBarry Smith   }
43221713a123SBarry Smith   PetscFunctionReturn(0);
43231713a123SBarry Smith }
43241713a123SBarry Smith 
43259110b2e7SHong Zhang /*@C
43262d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43272d5ee99bSBarry Smith 
43282d5ee99bSBarry Smith    Collective on TS
43292d5ee99bSBarry Smith 
43302d5ee99bSBarry Smith    Input Parameters:
43312d5ee99bSBarry Smith +  ts - the TS context
43322d5ee99bSBarry Smith .  step - current time-step
43332d5ee99bSBarry Smith .  ptime - current time
43342d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43352d5ee99bSBarry Smith 
43362d5ee99bSBarry Smith    Level: intermediate
43372d5ee99bSBarry Smith 
43382d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43392d5ee99bSBarry Smith @*/
43402d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43412d5ee99bSBarry Smith {
43422d5ee99bSBarry Smith   PetscErrorCode    ierr;
43432d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
43442d5ee99bSBarry Smith   PetscDraw         draw;
43456934998bSLisandro Dalcin   PetscDrawAxis     axis;
43462d5ee99bSBarry Smith   PetscInt          n;
43472d5ee99bSBarry Smith   PetscMPIInt       size;
43486934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
43492d5ee99bSBarry Smith   char              time[32];
43502d5ee99bSBarry Smith   const PetscScalar *U;
43512d5ee99bSBarry Smith 
43522d5ee99bSBarry Smith   PetscFunctionBegin;
43536934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
43546934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
43552d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
43566934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
43572d5ee99bSBarry Smith 
43582d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43596934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
43606934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
43616934998bSLisandro Dalcin   if (!step) {
43626934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
43636934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
43646934998bSLisandro Dalcin   }
43652d5ee99bSBarry Smith 
43662d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
43676934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
43686934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4369a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
43706934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
43712d5ee99bSBarry Smith 
43726934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
43736934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
43742d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
43754b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
437685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43772d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
437851fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43792d5ee99bSBarry Smith   }
43806934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
43812d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
438256d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
43836934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
43842d5ee99bSBarry Smith   PetscFunctionReturn(0);
43852d5ee99bSBarry Smith }
43862d5ee99bSBarry Smith 
43876083293cSBarry Smith /*@C
438883a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
43896083293cSBarry Smith 
43906083293cSBarry Smith    Collective on TS
43916083293cSBarry Smith 
43926083293cSBarry Smith    Input Parameters:
43936083293cSBarry Smith .    ctx - the monitor context
43946083293cSBarry Smith 
43956083293cSBarry Smith    Level: intermediate
43966083293cSBarry Smith 
439783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
43986083293cSBarry Smith @*/
439983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
44006083293cSBarry Smith {
44016083293cSBarry Smith   PetscErrorCode ierr;
44026083293cSBarry Smith 
44036083293cSBarry Smith   PetscFunctionBegin;
440483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
440583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
440683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
44076083293cSBarry Smith   PetscFunctionReturn(0);
44086083293cSBarry Smith }
44096083293cSBarry Smith 
44106083293cSBarry Smith /*@C
441183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
44126083293cSBarry Smith 
44136083293cSBarry Smith    Collective on TS
44146083293cSBarry Smith 
44156083293cSBarry Smith    Input Parameter:
44166083293cSBarry Smith .    ts - time-step context
44176083293cSBarry Smith 
44186083293cSBarry Smith    Output Patameter:
44196083293cSBarry Smith .    ctx - the monitor context
44206083293cSBarry Smith 
442199fdda47SBarry Smith    Options Database:
442299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
442399fdda47SBarry Smith 
44246083293cSBarry Smith    Level: intermediate
44256083293cSBarry Smith 
442683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44276083293cSBarry Smith @*/
442883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44296083293cSBarry Smith {
44306083293cSBarry Smith   PetscErrorCode   ierr;
44316083293cSBarry Smith 
44326083293cSBarry Smith   PetscFunctionBegin;
4433b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
443483a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4435e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4436bbd56ea5SKarl Rupp 
443783a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4438473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4439c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4440473a3ab2SBarry Smith 
4441473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4442c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
44436083293cSBarry Smith   PetscFunctionReturn(0);
44446083293cSBarry Smith }
44456083293cSBarry Smith 
44463a471f94SBarry Smith /*@C
44470ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
44480ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
44490ed3bfb6SBarry Smith 
44500ed3bfb6SBarry Smith    Collective on TS
44510ed3bfb6SBarry Smith 
44520ed3bfb6SBarry Smith    Input Parameters:
44530ed3bfb6SBarry Smith +  ts - the TS context
44540ed3bfb6SBarry Smith .  step - current time-step
44550ed3bfb6SBarry Smith .  ptime - current time
44560ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
44570ed3bfb6SBarry Smith 
44580ed3bfb6SBarry Smith    Options Database:
44590ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44600ed3bfb6SBarry Smith 
44610ed3bfb6SBarry Smith    Level: intermediate
44620ed3bfb6SBarry Smith 
44630ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
44640ed3bfb6SBarry Smith @*/
44650ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44660ed3bfb6SBarry Smith {
44670ed3bfb6SBarry Smith   PetscErrorCode   ierr;
44680ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
44690ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
44700ed3bfb6SBarry Smith   Vec              work;
44710ed3bfb6SBarry Smith 
44720ed3bfb6SBarry Smith   PetscFunctionBegin;
44730ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44740ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44750ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
44760ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
44770ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
44780ed3bfb6SBarry Smith   PetscFunctionReturn(0);
44790ed3bfb6SBarry Smith }
44800ed3bfb6SBarry Smith 
44810ed3bfb6SBarry Smith /*@C
448283a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
44833a471f94SBarry Smith    VecView() for the error at each timestep
44843a471f94SBarry Smith 
44853a471f94SBarry Smith    Collective on TS
44863a471f94SBarry Smith 
44873a471f94SBarry Smith    Input Parameters:
44883a471f94SBarry Smith +  ts - the TS context
44893a471f94SBarry Smith .  step - current time-step
44903a471f94SBarry Smith .  ptime - current time
44910298fd71SBarry Smith -  dummy - either a viewer or NULL
44923a471f94SBarry Smith 
44930ed3bfb6SBarry Smith    Options Database:
44940ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44950ed3bfb6SBarry Smith 
44963a471f94SBarry Smith    Level: intermediate
44973a471f94SBarry Smith 
44980ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
44993a471f94SBarry Smith @*/
45000910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45013a471f94SBarry Smith {
45023a471f94SBarry Smith   PetscErrorCode   ierr;
450383a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
450483a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
45053a471f94SBarry Smith   Vec              work;
45063a471f94SBarry Smith 
45073a471f94SBarry Smith   PetscFunctionBegin;
4508b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45090910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45103a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45110910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
45123a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45133a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45143a471f94SBarry Smith   PetscFunctionReturn(0);
45153a471f94SBarry Smith }
45163a471f94SBarry Smith 
4517af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
45186c699258SBarry Smith /*@
45192a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
45206c699258SBarry Smith 
4521d083f849SBarry Smith    Logically Collective on ts
45226c699258SBarry Smith 
45236c699258SBarry Smith    Input Parameters:
45242a808120SBarry Smith +  ts - the ODE integrator object
45252a808120SBarry Smith -  dm - the dm, cannot be NULL
45266c699258SBarry Smith 
4527e03a659cSJed Brown    Notes:
4528e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4529e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4530e03a659cSJed Brown    different problems using the same function space.
4531e03a659cSJed Brown 
45326c699258SBarry Smith    Level: intermediate
45336c699258SBarry Smith 
45346c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
45356c699258SBarry Smith @*/
45367087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
45376c699258SBarry Smith {
45386c699258SBarry Smith   PetscErrorCode ierr;
4539089b2837SJed Brown   SNES           snes;
4540942e3340SBarry Smith   DMTS           tsdm;
45416c699258SBarry Smith 
45426c699258SBarry Smith   PetscFunctionBegin;
45430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45442a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
454570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4546942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
45472a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4548942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4549942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
455024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
455124989b8cSPeter Brune         tsdm->originaldm = dm;
455224989b8cSPeter Brune       }
455324989b8cSPeter Brune     }
45546bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
455524989b8cSPeter Brune   }
45566c699258SBarry Smith   ts->dm = dm;
4557bbd56ea5SKarl Rupp 
4558089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4559089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
45606c699258SBarry Smith   PetscFunctionReturn(0);
45616c699258SBarry Smith }
45626c699258SBarry Smith 
45636c699258SBarry Smith /*@
45646c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
45656c699258SBarry Smith 
45663f9fe445SBarry Smith    Not Collective
45676c699258SBarry Smith 
45686c699258SBarry Smith    Input Parameter:
45696c699258SBarry Smith . ts - the preconditioner context
45706c699258SBarry Smith 
45716c699258SBarry Smith    Output Parameter:
45726c699258SBarry Smith .  dm - the dm
45736c699258SBarry Smith 
45746c699258SBarry Smith    Level: intermediate
45756c699258SBarry Smith 
45766c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
45776c699258SBarry Smith @*/
45787087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
45796c699258SBarry Smith {
4580496e6a7aSJed Brown   PetscErrorCode ierr;
4581496e6a7aSJed Brown 
45826c699258SBarry Smith   PetscFunctionBegin;
45830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4584496e6a7aSJed Brown   if (!ts->dm) {
4585ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4586496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4587496e6a7aSJed Brown   }
45886c699258SBarry Smith   *dm = ts->dm;
45896c699258SBarry Smith   PetscFunctionReturn(0);
45906c699258SBarry Smith }
45911713a123SBarry Smith 
45920f5c6efeSJed Brown /*@
45930f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
45940f5c6efeSJed Brown 
45953f9fe445SBarry Smith    Logically Collective on SNES
45960f5c6efeSJed Brown 
45970f5c6efeSJed Brown    Input Parameter:
4598d42a1c89SJed Brown + snes - nonlinear solver
45990910c330SBarry Smith . U - the current state at which to evaluate the residual
4600d42a1c89SJed Brown - ctx - user context, must be a TS
46010f5c6efeSJed Brown 
46020f5c6efeSJed Brown    Output Parameter:
46030f5c6efeSJed Brown . F - the nonlinear residual
46040f5c6efeSJed Brown 
46050f5c6efeSJed Brown    Notes:
46060f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46070f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
46080f5c6efeSJed Brown 
46090f5c6efeSJed Brown    Level: advanced
46100f5c6efeSJed Brown 
46110f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
46120f5c6efeSJed Brown @*/
46130910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
46140f5c6efeSJed Brown {
46150f5c6efeSJed Brown   TS             ts = (TS)ctx;
46160f5c6efeSJed Brown   PetscErrorCode ierr;
46170f5c6efeSJed Brown 
46180f5c6efeSJed Brown   PetscFunctionBegin;
46190f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46200910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46210f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
46220f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
46230910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
46240f5c6efeSJed Brown   PetscFunctionReturn(0);
46250f5c6efeSJed Brown }
46260f5c6efeSJed Brown 
46270f5c6efeSJed Brown /*@
46280f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
46290f5c6efeSJed Brown 
46300f5c6efeSJed Brown    Collective on SNES
46310f5c6efeSJed Brown 
46320f5c6efeSJed Brown    Input Parameter:
46330f5c6efeSJed Brown + snes - nonlinear solver
46340910c330SBarry Smith . U - the current state at which to evaluate the residual
46350f5c6efeSJed Brown - ctx - user context, must be a TS
46360f5c6efeSJed Brown 
46370f5c6efeSJed Brown    Output Parameter:
46380f5c6efeSJed Brown + A - the Jacobian
46390f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
46400f5c6efeSJed Brown - flag - indicates any structure change in the matrix
46410f5c6efeSJed Brown 
46420f5c6efeSJed Brown    Notes:
46430f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46440f5c6efeSJed Brown 
46450f5c6efeSJed Brown    Level: developer
46460f5c6efeSJed Brown 
46470f5c6efeSJed Brown .seealso: SNESSetJacobian()
46480f5c6efeSJed Brown @*/
4649d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
46500f5c6efeSJed Brown {
46510f5c6efeSJed Brown   TS             ts = (TS)ctx;
46520f5c6efeSJed Brown   PetscErrorCode ierr;
46530f5c6efeSJed Brown 
46540f5c6efeSJed Brown   PetscFunctionBegin;
46550f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46560910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46570f5c6efeSJed Brown   PetscValidPointer(A,3);
465894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
46590f5c6efeSJed Brown   PetscValidPointer(B,4);
466094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
46610f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4662d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
46630f5c6efeSJed Brown   PetscFunctionReturn(0);
46640f5c6efeSJed Brown }
4665325fc9f4SBarry Smith 
46660e4ef248SJed Brown /*@C
46679ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
46680e4ef248SJed Brown 
46690e4ef248SJed Brown    Collective on TS
46700e4ef248SJed Brown 
46710e4ef248SJed Brown    Input Arguments:
46720e4ef248SJed Brown +  ts - time stepping context
46730e4ef248SJed Brown .  t - time at which to evaluate
46740910c330SBarry Smith .  U - state at which to evaluate
46750e4ef248SJed Brown -  ctx - context
46760e4ef248SJed Brown 
46770e4ef248SJed Brown    Output Arguments:
46780e4ef248SJed Brown .  F - right hand side
46790e4ef248SJed Brown 
46800e4ef248SJed Brown    Level: intermediate
46810e4ef248SJed Brown 
46820e4ef248SJed Brown    Notes:
46830e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
46840e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
46850e4ef248SJed Brown 
46860e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
46870e4ef248SJed Brown @*/
46880910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
46890e4ef248SJed Brown {
46900e4ef248SJed Brown   PetscErrorCode ierr;
46910e4ef248SJed Brown   Mat            Arhs,Brhs;
46920e4ef248SJed Brown 
46930e4ef248SJed Brown   PetscFunctionBegin;
46940e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4695d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
46960910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
46970e4ef248SJed Brown   PetscFunctionReturn(0);
46980e4ef248SJed Brown }
46990e4ef248SJed Brown 
47000e4ef248SJed Brown /*@C
47010e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
47020e4ef248SJed Brown 
47030e4ef248SJed Brown    Collective on TS
47040e4ef248SJed Brown 
47050e4ef248SJed Brown    Input Arguments:
47060e4ef248SJed Brown +  ts - time stepping context
47070e4ef248SJed Brown .  t - time at which to evaluate
47080910c330SBarry Smith .  U - state at which to evaluate
47090e4ef248SJed Brown -  ctx - context
47100e4ef248SJed Brown 
47110e4ef248SJed Brown    Output Arguments:
47120e4ef248SJed Brown +  A - pointer to operator
47130e4ef248SJed Brown .  B - pointer to preconditioning matrix
47140e4ef248SJed Brown -  flg - matrix structure flag
47150e4ef248SJed Brown 
47160e4ef248SJed Brown    Level: intermediate
47170e4ef248SJed Brown 
47180e4ef248SJed Brown    Notes:
47190e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
47200e4ef248SJed Brown 
47210e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
47220e4ef248SJed Brown @*/
4723d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
47240e4ef248SJed Brown {
47250e4ef248SJed Brown   PetscFunctionBegin;
47260e4ef248SJed Brown   PetscFunctionReturn(0);
47270e4ef248SJed Brown }
47280e4ef248SJed Brown 
47290026cea9SSean Farley /*@C
47300026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
47310026cea9SSean Farley 
47320026cea9SSean Farley    Collective on TS
47330026cea9SSean Farley 
47340026cea9SSean Farley    Input Arguments:
47350026cea9SSean Farley +  ts - time stepping context
47360026cea9SSean Farley .  t - time at which to evaluate
47370910c330SBarry Smith .  U - state at which to evaluate
47380910c330SBarry Smith .  Udot - time derivative of state vector
47390026cea9SSean Farley -  ctx - context
47400026cea9SSean Farley 
47410026cea9SSean Farley    Output Arguments:
47420026cea9SSean Farley .  F - left hand side
47430026cea9SSean Farley 
47440026cea9SSean Farley    Level: intermediate
47450026cea9SSean Farley 
47460026cea9SSean Farley    Notes:
47470910c330SBarry 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
47480026cea9SSean Farley    user is required to write their own TSComputeIFunction.
47490026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
47500026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
47510026cea9SSean Farley 
47529ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
47539ae8fd06SBarry Smith 
47549ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
47550026cea9SSean Farley @*/
47560910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
47570026cea9SSean Farley {
47580026cea9SSean Farley   PetscErrorCode ierr;
47590026cea9SSean Farley   Mat            A,B;
47600026cea9SSean Farley 
47610026cea9SSean Farley   PetscFunctionBegin;
47620298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4763d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
47640910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
47650026cea9SSean Farley   PetscFunctionReturn(0);
47660026cea9SSean Farley }
47670026cea9SSean Farley 
47680026cea9SSean Farley /*@C
4769b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
47700026cea9SSean Farley 
47710026cea9SSean Farley    Collective on TS
47720026cea9SSean Farley 
47730026cea9SSean Farley    Input Arguments:
47740026cea9SSean Farley +  ts - time stepping context
47750026cea9SSean Farley .  t - time at which to evaluate
47760910c330SBarry Smith .  U - state at which to evaluate
47770910c330SBarry Smith .  Udot - time derivative of state vector
47780026cea9SSean Farley .  shift - shift to apply
47790026cea9SSean Farley -  ctx - context
47800026cea9SSean Farley 
47810026cea9SSean Farley    Output Arguments:
47820026cea9SSean Farley +  A - pointer to operator
47830026cea9SSean Farley .  B - pointer to preconditioning matrix
47840026cea9SSean Farley -  flg - matrix structure flag
47850026cea9SSean Farley 
4786b41af12eSJed Brown    Level: advanced
47870026cea9SSean Farley 
47880026cea9SSean Farley    Notes:
47890026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
47900026cea9SSean Farley 
4791b41af12eSJed Brown    It is only appropriate for problems of the form
4792b41af12eSJed Brown 
4793b41af12eSJed Brown $     M Udot = F(U,t)
4794b41af12eSJed Brown 
4795b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4796b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4797b41af12eSJed Brown   an implicit operator of the form
4798b41af12eSJed Brown 
4799b41af12eSJed Brown $    shift*M + J
4800b41af12eSJed Brown 
4801b41af12eSJed 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
4802b41af12eSJed Brown   a copy of M or reassemble it when requested.
4803b41af12eSJed Brown 
48040026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
48050026cea9SSean Farley @*/
4806d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
48070026cea9SSean Farley {
4808b41af12eSJed Brown   PetscErrorCode ierr;
4809b41af12eSJed Brown 
48100026cea9SSean Farley   PetscFunctionBegin;
481194ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4812b41af12eSJed Brown   ts->ijacobian.shift = shift;
48130026cea9SSean Farley   PetscFunctionReturn(0);
48140026cea9SSean Farley }
4815b41af12eSJed Brown 
4816e817cc15SEmil Constantinescu /*@
4817e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4818e817cc15SEmil Constantinescu 
4819e817cc15SEmil Constantinescu    Not Collective
4820e817cc15SEmil Constantinescu 
4821e817cc15SEmil Constantinescu    Input Parameter:
4822e817cc15SEmil Constantinescu .  ts - the TS context
4823e817cc15SEmil Constantinescu 
4824e817cc15SEmil Constantinescu    Output Parameter:
48254e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4826e817cc15SEmil Constantinescu 
4827e817cc15SEmil Constantinescu    Level: beginner
4828e817cc15SEmil Constantinescu 
4829e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4830e817cc15SEmil Constantinescu @*/
4831e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4832e817cc15SEmil Constantinescu {
4833e817cc15SEmil Constantinescu   PetscFunctionBegin;
4834e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4835e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4836e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4837e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4838e817cc15SEmil Constantinescu }
4839e817cc15SEmil Constantinescu 
4840e817cc15SEmil Constantinescu /*@
4841e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4842e817cc15SEmil Constantinescu 
4843e817cc15SEmil Constantinescu    Not Collective
4844e817cc15SEmil Constantinescu 
4845e817cc15SEmil Constantinescu    Input Parameter:
4846e817cc15SEmil Constantinescu +  ts - the TS context
48471297b224SEmil Constantinescu -  equation_type - see TSEquationType
4848e817cc15SEmil Constantinescu 
4849e817cc15SEmil Constantinescu    Level: advanced
4850e817cc15SEmil Constantinescu 
4851e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4852e817cc15SEmil Constantinescu @*/
4853e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4854e817cc15SEmil Constantinescu {
4855e817cc15SEmil Constantinescu   PetscFunctionBegin;
4856e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4857e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4858e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4859e817cc15SEmil Constantinescu }
48600026cea9SSean Farley 
48614af1b03aSJed Brown /*@
48624af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
48634af1b03aSJed Brown 
48644af1b03aSJed Brown    Not Collective
48654af1b03aSJed Brown 
48664af1b03aSJed Brown    Input Parameter:
48674af1b03aSJed Brown .  ts - the TS context
48684af1b03aSJed Brown 
48694af1b03aSJed Brown    Output Parameter:
48704af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
48714af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
48724af1b03aSJed Brown 
4873487e0bb9SJed Brown    Level: beginner
48744af1b03aSJed Brown 
4875cd652676SJed Brown    Notes:
4876cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
48774af1b03aSJed Brown 
48784af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
48794af1b03aSJed Brown @*/
48804af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
48814af1b03aSJed Brown {
48824af1b03aSJed Brown   PetscFunctionBegin;
48834af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48844af1b03aSJed Brown   PetscValidPointer(reason,2);
48854af1b03aSJed Brown   *reason = ts->reason;
48864af1b03aSJed Brown   PetscFunctionReturn(0);
48874af1b03aSJed Brown }
48884af1b03aSJed Brown 
4889d6ad946cSShri Abhyankar /*@
4890d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4891d6ad946cSShri Abhyankar 
48926b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
4893d6ad946cSShri Abhyankar 
48946b221cbeSPatrick Sanan    Input Parameters:
4895d6ad946cSShri Abhyankar +  ts - the TS context
48966b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4897d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4898d6ad946cSShri Abhyankar 
4899f5abba47SShri Abhyankar    Level: advanced
4900d6ad946cSShri Abhyankar 
4901d6ad946cSShri Abhyankar    Notes:
49026b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
4903d6ad946cSShri Abhyankar 
4904d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4905d6ad946cSShri Abhyankar @*/
4906d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4907d6ad946cSShri Abhyankar {
4908d6ad946cSShri Abhyankar   PetscFunctionBegin;
4909d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4910d6ad946cSShri Abhyankar   ts->reason = reason;
4911d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4912d6ad946cSShri Abhyankar }
4913d6ad946cSShri Abhyankar 
4914cc708dedSBarry Smith /*@
4915cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4916cc708dedSBarry Smith 
4917cc708dedSBarry Smith    Not Collective
4918cc708dedSBarry Smith 
4919cc708dedSBarry Smith    Input Parameter:
4920cc708dedSBarry Smith .  ts - the TS context
4921cc708dedSBarry Smith 
4922cc708dedSBarry Smith    Output Parameter:
492319eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4924cc708dedSBarry Smith 
4925487e0bb9SJed Brown    Level: beginner
4926cc708dedSBarry Smith 
4927cc708dedSBarry Smith    Notes:
4928cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4929cc708dedSBarry Smith 
4930cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4931cc708dedSBarry Smith @*/
4932cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4933cc708dedSBarry Smith {
4934cc708dedSBarry Smith   PetscFunctionBegin;
4935cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4936cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4937cc708dedSBarry Smith   *ftime = ts->solvetime;
4938cc708dedSBarry Smith   PetscFunctionReturn(0);
4939cc708dedSBarry Smith }
4940cc708dedSBarry Smith 
49412c18e0fdSBarry Smith /*@
49425ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49439f67acb7SJed Brown    used by the time integrator.
49449f67acb7SJed Brown 
49459f67acb7SJed Brown    Not Collective
49469f67acb7SJed Brown 
49479f67acb7SJed Brown    Input Parameter:
49489f67acb7SJed Brown .  ts - TS context
49499f67acb7SJed Brown 
49509f67acb7SJed Brown    Output Parameter:
49519f67acb7SJed Brown .  nits - number of nonlinear iterations
49529f67acb7SJed Brown 
49539f67acb7SJed Brown    Notes:
49549f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49559f67acb7SJed Brown 
49569f67acb7SJed Brown    Level: intermediate
49579f67acb7SJed Brown 
49585ef26d82SJed Brown .seealso:  TSGetKSPIterations()
49599f67acb7SJed Brown @*/
49605ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
49619f67acb7SJed Brown {
49629f67acb7SJed Brown   PetscFunctionBegin;
49639f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49649f67acb7SJed Brown   PetscValidIntPointer(nits,2);
49655ef26d82SJed Brown   *nits = ts->snes_its;
49669f67acb7SJed Brown   PetscFunctionReturn(0);
49679f67acb7SJed Brown }
49689f67acb7SJed Brown 
49699f67acb7SJed Brown /*@
49705ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
49719f67acb7SJed Brown    used by the time integrator.
49729f67acb7SJed Brown 
49739f67acb7SJed Brown    Not Collective
49749f67acb7SJed Brown 
49759f67acb7SJed Brown    Input Parameter:
49769f67acb7SJed Brown .  ts - TS context
49779f67acb7SJed Brown 
49789f67acb7SJed Brown    Output Parameter:
49799f67acb7SJed Brown .  lits - number of linear iterations
49809f67acb7SJed Brown 
49819f67acb7SJed Brown    Notes:
49829f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49839f67acb7SJed Brown 
49849f67acb7SJed Brown    Level: intermediate
49859f67acb7SJed Brown 
49865ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
49879f67acb7SJed Brown @*/
49885ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
49899f67acb7SJed Brown {
49909f67acb7SJed Brown   PetscFunctionBegin;
49919f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49929f67acb7SJed Brown   PetscValidIntPointer(lits,2);
49935ef26d82SJed Brown   *lits = ts->ksp_its;
49949f67acb7SJed Brown   PetscFunctionReturn(0);
49959f67acb7SJed Brown }
49969f67acb7SJed Brown 
4997cef5090cSJed Brown /*@
4998cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4999cef5090cSJed Brown 
5000cef5090cSJed Brown    Not Collective
5001cef5090cSJed Brown 
5002cef5090cSJed Brown    Input Parameter:
5003cef5090cSJed Brown .  ts - TS context
5004cef5090cSJed Brown 
5005cef5090cSJed Brown    Output Parameter:
5006cef5090cSJed Brown .  rejects - number of steps rejected
5007cef5090cSJed Brown 
5008cef5090cSJed Brown    Notes:
5009cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5010cef5090cSJed Brown 
5011cef5090cSJed Brown    Level: intermediate
5012cef5090cSJed Brown 
50135ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5014cef5090cSJed Brown @*/
5015cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5016cef5090cSJed Brown {
5017cef5090cSJed Brown   PetscFunctionBegin;
5018cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5019cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5020cef5090cSJed Brown   *rejects = ts->reject;
5021cef5090cSJed Brown   PetscFunctionReturn(0);
5022cef5090cSJed Brown }
5023cef5090cSJed Brown 
5024cef5090cSJed Brown /*@
5025cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5026cef5090cSJed Brown 
5027cef5090cSJed Brown    Not Collective
5028cef5090cSJed Brown 
5029cef5090cSJed Brown    Input Parameter:
5030cef5090cSJed Brown .  ts - TS context
5031cef5090cSJed Brown 
5032cef5090cSJed Brown    Output Parameter:
5033cef5090cSJed Brown .  fails - number of failed nonlinear solves
5034cef5090cSJed Brown 
5035cef5090cSJed Brown    Notes:
5036cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5037cef5090cSJed Brown 
5038cef5090cSJed Brown    Level: intermediate
5039cef5090cSJed Brown 
50405ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5041cef5090cSJed Brown @*/
5042cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5043cef5090cSJed Brown {
5044cef5090cSJed Brown   PetscFunctionBegin;
5045cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5046cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5047cef5090cSJed Brown   *fails = ts->num_snes_failures;
5048cef5090cSJed Brown   PetscFunctionReturn(0);
5049cef5090cSJed Brown }
5050cef5090cSJed Brown 
5051cef5090cSJed Brown /*@
5052cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5053cef5090cSJed Brown 
5054cef5090cSJed Brown    Not Collective
5055cef5090cSJed Brown 
5056cef5090cSJed Brown    Input Parameter:
5057cef5090cSJed Brown +  ts - TS context
5058cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5059cef5090cSJed Brown 
5060cef5090cSJed Brown    Notes:
5061cef5090cSJed Brown    The counter is reset to zero for each step
5062cef5090cSJed Brown 
5063cef5090cSJed Brown    Options Database Key:
5064cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5065cef5090cSJed Brown 
5066cef5090cSJed Brown    Level: intermediate
5067cef5090cSJed Brown 
50685ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5069cef5090cSJed Brown @*/
5070cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5071cef5090cSJed Brown {
5072cef5090cSJed Brown   PetscFunctionBegin;
5073cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5074cef5090cSJed Brown   ts->max_reject = rejects;
5075cef5090cSJed Brown   PetscFunctionReturn(0);
5076cef5090cSJed Brown }
5077cef5090cSJed Brown 
5078cef5090cSJed Brown /*@
5079cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5080cef5090cSJed Brown 
5081cef5090cSJed Brown    Not Collective
5082cef5090cSJed Brown 
5083cef5090cSJed Brown    Input Parameter:
5084cef5090cSJed Brown +  ts - TS context
5085cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5086cef5090cSJed Brown 
5087cef5090cSJed Brown    Notes:
5088cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5089cef5090cSJed Brown 
5090cef5090cSJed Brown    Options Database Key:
5091cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5092cef5090cSJed Brown 
5093cef5090cSJed Brown    Level: intermediate
5094cef5090cSJed Brown 
50955ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5096cef5090cSJed Brown @*/
5097cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5098cef5090cSJed Brown {
5099cef5090cSJed Brown   PetscFunctionBegin;
5100cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5101cef5090cSJed Brown   ts->max_snes_failures = fails;
5102cef5090cSJed Brown   PetscFunctionReturn(0);
5103cef5090cSJed Brown }
5104cef5090cSJed Brown 
5105cef5090cSJed Brown /*@
5106cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5107cef5090cSJed Brown 
5108cef5090cSJed Brown    Not Collective
5109cef5090cSJed Brown 
5110cef5090cSJed Brown    Input Parameter:
5111cef5090cSJed Brown +  ts - TS context
5112cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5113cef5090cSJed Brown 
5114cef5090cSJed Brown    Options Database Key:
5115cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5116cef5090cSJed Brown 
5117cef5090cSJed Brown    Level: intermediate
5118cef5090cSJed Brown 
51195ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5120cef5090cSJed Brown @*/
5121cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5122cef5090cSJed Brown {
5123cef5090cSJed Brown   PetscFunctionBegin;
5124cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5125cef5090cSJed Brown   ts->errorifstepfailed = err;
5126cef5090cSJed Brown   PetscFunctionReturn(0);
5127cef5090cSJed Brown }
5128cef5090cSJed Brown 
5129fb1732b5SBarry Smith /*@C
5130fde5950dSBarry 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
5131fb1732b5SBarry Smith 
5132fb1732b5SBarry Smith    Collective on TS
5133fb1732b5SBarry Smith 
5134fb1732b5SBarry Smith    Input Parameters:
5135fb1732b5SBarry Smith +  ts - the TS context
5136fb1732b5SBarry Smith .  step - current time-step
5137fb1732b5SBarry Smith .  ptime - current time
51380910c330SBarry Smith .  u - current state
5139721cd6eeSBarry Smith -  vf - viewer and its format
5140fb1732b5SBarry Smith 
5141fb1732b5SBarry Smith    Level: intermediate
5142fb1732b5SBarry Smith 
5143fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5144fb1732b5SBarry Smith @*/
5145721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5146fb1732b5SBarry Smith {
5147fb1732b5SBarry Smith   PetscErrorCode ierr;
5148fb1732b5SBarry Smith 
5149fb1732b5SBarry Smith   PetscFunctionBegin;
5150721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5151721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5152721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5153ed81e22dSJed Brown   PetscFunctionReturn(0);
5154ed81e22dSJed Brown }
5155ed81e22dSJed Brown 
5156ed81e22dSJed Brown /*@C
5157ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5158ed81e22dSJed Brown 
5159ed81e22dSJed Brown    Collective on TS
5160ed81e22dSJed Brown 
5161ed81e22dSJed Brown    Input Parameters:
5162ed81e22dSJed Brown +  ts - the TS context
5163ed81e22dSJed Brown .  step - current time-step
5164ed81e22dSJed Brown .  ptime - current time
51650910c330SBarry Smith .  u - current state
5166ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5167ed81e22dSJed Brown 
5168ed81e22dSJed Brown    Level: intermediate
5169ed81e22dSJed Brown 
5170ed81e22dSJed Brown    Notes:
5171ed81e22dSJed 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.
5172ed81e22dSJed Brown    These are named according to the file name template.
5173ed81e22dSJed Brown 
5174ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5175ed81e22dSJed Brown 
5176ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5177ed81e22dSJed Brown @*/
51780910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5179ed81e22dSJed Brown {
5180ed81e22dSJed Brown   PetscErrorCode ierr;
5181ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5182ed81e22dSJed Brown   PetscViewer    viewer;
5183ed81e22dSJed Brown 
5184ed81e22dSJed Brown   PetscFunctionBegin;
518563e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
51868caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5187ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
51880910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5189ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5190ed81e22dSJed Brown   PetscFunctionReturn(0);
5191ed81e22dSJed Brown }
5192ed81e22dSJed Brown 
5193ed81e22dSJed Brown /*@C
5194ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5195ed81e22dSJed Brown 
5196ed81e22dSJed Brown    Collective on TS
5197ed81e22dSJed Brown 
5198ed81e22dSJed Brown    Input Parameters:
5199ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5200ed81e22dSJed Brown 
5201ed81e22dSJed Brown    Level: intermediate
5202ed81e22dSJed Brown 
5203ed81e22dSJed Brown    Note:
5204ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5205ed81e22dSJed Brown 
5206ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5207ed81e22dSJed Brown @*/
5208ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5209ed81e22dSJed Brown {
5210ed81e22dSJed Brown   PetscErrorCode ierr;
5211ed81e22dSJed Brown 
5212ed81e22dSJed Brown   PetscFunctionBegin;
5213ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5214fb1732b5SBarry Smith   PetscFunctionReturn(0);
5215fb1732b5SBarry Smith }
5216fb1732b5SBarry Smith 
521784df9cb4SJed Brown /*@
5218552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
521984df9cb4SJed Brown 
5220ed81e22dSJed Brown    Collective on TS if controller has not been created yet
522184df9cb4SJed Brown 
522284df9cb4SJed Brown    Input Arguments:
5223ed81e22dSJed Brown .  ts - time stepping context
522484df9cb4SJed Brown 
522584df9cb4SJed Brown    Output Arguments:
5226ed81e22dSJed Brown .  adapt - adaptive controller
522784df9cb4SJed Brown 
522884df9cb4SJed Brown    Level: intermediate
522984df9cb4SJed Brown 
5230ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
523184df9cb4SJed Brown @*/
5232552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
523384df9cb4SJed Brown {
523484df9cb4SJed Brown   PetscErrorCode ierr;
523584df9cb4SJed Brown 
523684df9cb4SJed Brown   PetscFunctionBegin;
523784df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5238bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
523984df9cb4SJed Brown   if (!ts->adapt) {
5240ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
52413bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
52421c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
524384df9cb4SJed Brown   }
5244bec58848SLisandro Dalcin   *adapt = ts->adapt;
524584df9cb4SJed Brown   PetscFunctionReturn(0);
524684df9cb4SJed Brown }
5247d6ebe24aSShri Abhyankar 
52481c3436cfSJed Brown /*@
52491c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
52501c3436cfSJed Brown 
52511c3436cfSJed Brown    Logically Collective
52521c3436cfSJed Brown 
52531c3436cfSJed Brown    Input Arguments:
52541c3436cfSJed Brown +  ts - time integration context
52551c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
52560298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
52571c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
52580298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
52591c3436cfSJed Brown 
5260a3cdaa26SBarry Smith    Options Database keys:
5261a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5262a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5263a3cdaa26SBarry Smith 
52643ff766beSShri Abhyankar    Notes:
52653ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
52663ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
52673ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
52683ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
52693ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
52703ff766beSShri Abhyankar    differential variables.
52713ff766beSShri Abhyankar 
52721c3436cfSJed Brown    Level: beginner
52731c3436cfSJed Brown 
5274c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
52751c3436cfSJed Brown @*/
52761c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
52771c3436cfSJed Brown {
52781c3436cfSJed Brown   PetscErrorCode ierr;
52791c3436cfSJed Brown 
52801c3436cfSJed Brown   PetscFunctionBegin;
5281c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
52821c3436cfSJed Brown   if (vatol) {
52831c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
52841c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
52851c3436cfSJed Brown     ts->vatol = vatol;
52861c3436cfSJed Brown   }
5287c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
52881c3436cfSJed Brown   if (vrtol) {
52891c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
52901c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
52911c3436cfSJed Brown     ts->vrtol = vrtol;
52921c3436cfSJed Brown   }
52931c3436cfSJed Brown   PetscFunctionReturn(0);
52941c3436cfSJed Brown }
52951c3436cfSJed Brown 
5296c5033834SJed Brown /*@
5297c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5298c5033834SJed Brown 
5299c5033834SJed Brown    Logically Collective
5300c5033834SJed Brown 
5301c5033834SJed Brown    Input Arguments:
5302c5033834SJed Brown .  ts - time integration context
5303c5033834SJed Brown 
5304c5033834SJed Brown    Output Arguments:
53050298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53060298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53070298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53080298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5309c5033834SJed Brown 
5310c5033834SJed Brown    Level: beginner
5311c5033834SJed Brown 
5312c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5313c5033834SJed Brown @*/
5314c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5315c5033834SJed Brown {
5316c5033834SJed Brown   PetscFunctionBegin;
5317c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5318c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5319c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5320c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5321c5033834SJed Brown   PetscFunctionReturn(0);
5322c5033834SJed Brown }
5323c5033834SJed Brown 
53249c6b16b5SShri Abhyankar /*@
5325a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
53269c6b16b5SShri Abhyankar 
53279c6b16b5SShri Abhyankar    Collective on TS
53289c6b16b5SShri Abhyankar 
53299c6b16b5SShri Abhyankar    Input Arguments:
53309c6b16b5SShri Abhyankar +  ts - time stepping context
5331a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5332a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
53339c6b16b5SShri Abhyankar 
53349c6b16b5SShri Abhyankar    Output Arguments:
5335a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
53367453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5337a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
53389c6b16b5SShri Abhyankar 
53399c6b16b5SShri Abhyankar    Level: developer
53409c6b16b5SShri Abhyankar 
5341deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
53429c6b16b5SShri Abhyankar @*/
53437453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53449c6b16b5SShri Abhyankar {
53459c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
53469c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
53477453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
53487453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
53499c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
53507453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
53517453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
53527453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
53539c6b16b5SShri Abhyankar 
53549c6b16b5SShri Abhyankar   PetscFunctionBegin;
53559c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5356a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5357a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5358a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5359a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5360a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5361a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
53628a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
53638a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5364a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
53659c6b16b5SShri Abhyankar 
53669c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
53679c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
53689c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
53699c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
53709c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
53717453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
53727453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
53737453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
53749c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
53759c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
53769c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53779c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53789c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
537976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
53807453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
53817453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
53827453f775SEmil Constantinescu       if(tola>0.){
53837453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
53847453f775SEmil Constantinescu         na_loc++;
53857453f775SEmil Constantinescu       }
53867453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53877453f775SEmil Constantinescu       if(tolr>0.){
53887453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
53897453f775SEmil Constantinescu         nr_loc++;
53907453f775SEmil Constantinescu       }
53917453f775SEmil Constantinescu       tol=tola+tolr;
53927453f775SEmil Constantinescu       if(tol>0.){
53937453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
53947453f775SEmil Constantinescu         n_loc++;
53957453f775SEmil Constantinescu       }
53969c6b16b5SShri Abhyankar     }
53979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53989c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53999c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54009c6b16b5SShri Abhyankar     const PetscScalar *atol;
54019c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54029c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
540376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54047453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54057453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
54067453f775SEmil Constantinescu       if(tola>0.){
54077453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54087453f775SEmil Constantinescu         na_loc++;
54097453f775SEmil Constantinescu       }
54107453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54117453f775SEmil Constantinescu       if(tolr>0.){
54127453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54137453f775SEmil Constantinescu         nr_loc++;
54147453f775SEmil Constantinescu       }
54157453f775SEmil Constantinescu       tol=tola+tolr;
54167453f775SEmil Constantinescu       if(tol>0.){
54177453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54187453f775SEmil Constantinescu         n_loc++;
54197453f775SEmil Constantinescu       }
54209c6b16b5SShri Abhyankar     }
54219c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54229c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54239c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54249c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54259c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
542676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54277453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54287453f775SEmil Constantinescu       tola = ts->atol;
54297453f775SEmil Constantinescu       if(tola>0.){
54307453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54317453f775SEmil Constantinescu         na_loc++;
54327453f775SEmil Constantinescu       }
54337453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54347453f775SEmil Constantinescu       if(tolr>0.){
54357453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54367453f775SEmil Constantinescu         nr_loc++;
54377453f775SEmil Constantinescu       }
54387453f775SEmil Constantinescu       tol=tola+tolr;
54397453f775SEmil Constantinescu       if(tol>0.){
54407453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54417453f775SEmil Constantinescu         n_loc++;
54427453f775SEmil Constantinescu       }
54439c6b16b5SShri Abhyankar     }
54449c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54459c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
54469c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
544776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54487453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54497453f775SEmil Constantinescu       tola = ts->atol;
54507453f775SEmil Constantinescu       if(tola>0.){
54517453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54527453f775SEmil Constantinescu         na_loc++;
54537453f775SEmil Constantinescu       }
54547453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54557453f775SEmil Constantinescu       if(tolr>0.){
54567453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54577453f775SEmil Constantinescu         nr_loc++;
54587453f775SEmil Constantinescu       }
54597453f775SEmil Constantinescu       tol=tola+tolr;
54607453f775SEmil Constantinescu       if(tol>0.){
54617453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54627453f775SEmil Constantinescu         n_loc++;
54637453f775SEmil Constantinescu       }
54649c6b16b5SShri Abhyankar     }
54659c6b16b5SShri Abhyankar   }
54669c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
54679c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
54689c6b16b5SShri Abhyankar 
54697453f775SEmil Constantinescu   err_loc[0] = sum;
54707453f775SEmil Constantinescu   err_loc[1] = suma;
54717453f775SEmil Constantinescu   err_loc[2] = sumr;
54727453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
54737453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
54747453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
54757453f775SEmil Constantinescu 
5476a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
54777453f775SEmil Constantinescu 
54787453f775SEmil Constantinescu   gsum   = err_glb[0];
54797453f775SEmil Constantinescu   gsuma  = err_glb[1];
54807453f775SEmil Constantinescu   gsumr  = err_glb[2];
54817453f775SEmil Constantinescu   n_glb  = err_glb[3];
54827453f775SEmil Constantinescu   na_glb = err_glb[4];
54837453f775SEmil Constantinescu   nr_glb = err_glb[5];
54847453f775SEmil Constantinescu 
5485b1316ef9SEmil Constantinescu   *norm  = 0.;
5486b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5487b1316ef9SEmil Constantinescu   *norma = 0.;
5488b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5489b1316ef9SEmil Constantinescu   *normr = 0.;
5490b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
54919c6b16b5SShri Abhyankar 
54929c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
54937453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
54947453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
54959c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
54969c6b16b5SShri Abhyankar }
54979c6b16b5SShri Abhyankar 
54989c6b16b5SShri Abhyankar /*@
5499a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
55009c6b16b5SShri Abhyankar 
55019c6b16b5SShri Abhyankar    Collective on TS
55029c6b16b5SShri Abhyankar 
55039c6b16b5SShri Abhyankar    Input Arguments:
55049c6b16b5SShri Abhyankar +  ts - time stepping context
5505a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5506a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55079c6b16b5SShri Abhyankar 
55089c6b16b5SShri Abhyankar    Output Arguments:
5509a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55107453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5511a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55129c6b16b5SShri Abhyankar 
55139c6b16b5SShri Abhyankar    Level: developer
55149c6b16b5SShri Abhyankar 
5515deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
55169c6b16b5SShri Abhyankar @*/
55177453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55189c6b16b5SShri Abhyankar {
55199c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
55207453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
55219c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
55227453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
55237453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
55247453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
55259c6b16b5SShri Abhyankar 
55269c6b16b5SShri Abhyankar   PetscFunctionBegin;
55279c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5528a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5529a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5530a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5531a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5532a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5533a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
55348a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
55358a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5536a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55379c6b16b5SShri Abhyankar 
55389c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55399c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55409c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55419c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55429c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55437453f775SEmil Constantinescu 
55447453f775SEmil Constantinescu   max=0.;
55457453f775SEmil Constantinescu   maxa=0.;
55467453f775SEmil Constantinescu   maxr=0.;
55477453f775SEmil Constantinescu 
55487453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
55499c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55519c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55527453f775SEmil Constantinescu 
55537453f775SEmil Constantinescu     for (i=0; i<n; i++) {
555476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55557453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55567453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55577453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55587453f775SEmil Constantinescu       tol  = tola+tolr;
55597453f775SEmil Constantinescu       if(tola>0.){
55607453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
55617453f775SEmil Constantinescu       }
55627453f775SEmil Constantinescu       if(tolr>0.){
55637453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
55647453f775SEmil Constantinescu       }
55657453f775SEmil Constantinescu       if(tol>0.){
55667453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
55677453f775SEmil Constantinescu       }
55689c6b16b5SShri Abhyankar     }
55699c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55709c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55719c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55729c6b16b5SShri Abhyankar     const PetscScalar *atol;
55739c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55747453f775SEmil Constantinescu     for (i=0; i<n; i++) {
557576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55767453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55777453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55787453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55797453f775SEmil Constantinescu       tol  = tola+tolr;
55807453f775SEmil Constantinescu       if(tola>0.){
55817453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
55827453f775SEmil Constantinescu       }
55837453f775SEmil Constantinescu       if(tolr>0.){
55847453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
55857453f775SEmil Constantinescu       }
55867453f775SEmil Constantinescu       if(tol>0.){
55877453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
55887453f775SEmil Constantinescu       }
55899c6b16b5SShri Abhyankar     }
55909c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55919c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55929c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55939c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55947453f775SEmil Constantinescu 
55957453f775SEmil Constantinescu     for (i=0; i<n; i++) {
559676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55977453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55987453f775SEmil Constantinescu       tola = ts->atol;
55997453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56007453f775SEmil Constantinescu       tol  = tola+tolr;
56017453f775SEmil Constantinescu       if(tola>0.){
56027453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56037453f775SEmil Constantinescu       }
56047453f775SEmil Constantinescu       if(tolr>0.){
56057453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56067453f775SEmil Constantinescu       }
56077453f775SEmil Constantinescu       if(tol>0.){
56087453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56097453f775SEmil Constantinescu       }
56109c6b16b5SShri Abhyankar     }
56119c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56129c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
56137453f775SEmil Constantinescu 
56147453f775SEmil Constantinescu     for (i=0; i<n; i++) {
561576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56167453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56177453f775SEmil Constantinescu       tola = ts->atol;
56187453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56197453f775SEmil Constantinescu       tol  = tola+tolr;
56207453f775SEmil Constantinescu       if(tola>0.){
56217453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56227453f775SEmil Constantinescu       }
56237453f775SEmil Constantinescu       if(tolr>0.){
56247453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56257453f775SEmil Constantinescu       }
56267453f775SEmil Constantinescu       if(tol>0.){
56277453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56287453f775SEmil Constantinescu       }
56299c6b16b5SShri Abhyankar     }
56309c6b16b5SShri Abhyankar   }
56319c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
56329c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
56337453f775SEmil Constantinescu   err_loc[0] = max;
56347453f775SEmil Constantinescu   err_loc[1] = maxa;
56357453f775SEmil Constantinescu   err_loc[2] = maxr;
5636a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56377453f775SEmil Constantinescu   gmax   = err_glb[0];
56387453f775SEmil Constantinescu   gmaxa  = err_glb[1];
56397453f775SEmil Constantinescu   gmaxr  = err_glb[2];
56409c6b16b5SShri Abhyankar 
56419c6b16b5SShri Abhyankar   *norm = gmax;
56427453f775SEmil Constantinescu   *norma = gmaxa;
56437453f775SEmil Constantinescu   *normr = gmaxr;
56449c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56457453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56467453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56479c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
56489c6b16b5SShri Abhyankar }
56499c6b16b5SShri Abhyankar 
56501c3436cfSJed Brown /*@
56518a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
56521c3436cfSJed Brown 
56531c3436cfSJed Brown    Collective on TS
56541c3436cfSJed Brown 
56551c3436cfSJed Brown    Input Arguments:
56561c3436cfSJed Brown +  ts - time stepping context
5657a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5658a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5659a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
56607619abb3SShri 
56611c3436cfSJed Brown    Output Arguments:
5662a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
56638a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5664a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5665a4868fbcSLisandro Dalcin 
5666a4868fbcSLisandro Dalcin    Options Database Keys:
5667a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5668a4868fbcSLisandro Dalcin 
56691c3436cfSJed Brown    Level: developer
56701c3436cfSJed Brown 
56718a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
56721c3436cfSJed Brown @*/
56737453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56741c3436cfSJed Brown {
56758beabaa1SBarry Smith   PetscErrorCode ierr;
56761c3436cfSJed Brown 
56771c3436cfSJed Brown   PetscFunctionBegin;
5678a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
56797453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5680a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
56817453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5682a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
56831c3436cfSJed Brown   PetscFunctionReturn(0);
56841c3436cfSJed Brown }
56851c3436cfSJed Brown 
56868a175baeSEmil Constantinescu 
56878a175baeSEmil Constantinescu /*@
56888a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
56898a175baeSEmil Constantinescu 
56908a175baeSEmil Constantinescu    Collective on TS
56918a175baeSEmil Constantinescu 
56928a175baeSEmil Constantinescu    Input Arguments:
56938a175baeSEmil Constantinescu +  ts - time stepping context
56948a175baeSEmil Constantinescu .  E - error vector
56958a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
56968a175baeSEmil Constantinescu -  Y - state vector, previous time step
56978a175baeSEmil Constantinescu 
56988a175baeSEmil Constantinescu    Output Arguments:
5699a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57008a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5701a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57028a175baeSEmil Constantinescu 
57038a175baeSEmil Constantinescu    Level: developer
57048a175baeSEmil Constantinescu 
57058a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
57068a175baeSEmil Constantinescu @*/
57078a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57088a175baeSEmil Constantinescu {
57098a175baeSEmil Constantinescu   PetscErrorCode    ierr;
57108a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
57118a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57128a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57138a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
57148a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
57158a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
57168a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57178a175baeSEmil Constantinescu 
57188a175baeSEmil Constantinescu   PetscFunctionBegin;
57198a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57208a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
57218a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
57228a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
57238a175baeSEmil Constantinescu   PetscValidType(E,2);
57248a175baeSEmil Constantinescu   PetscValidType(U,3);
57258a175baeSEmil Constantinescu   PetscValidType(Y,4);
57268a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
57278a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
57288a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
57298a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
57308a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
57318a175baeSEmil Constantinescu 
57328a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
57338a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
57348a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
57358a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
57368a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57378a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57388a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
57398a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
57408a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
57418a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
57428a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
57438a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57448a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57458a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
574676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57478a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57488a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57498a175baeSEmil Constantinescu       if(tola>0.){
57508a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57518a175baeSEmil Constantinescu         na_loc++;
57528a175baeSEmil Constantinescu       }
57538a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57548a175baeSEmil Constantinescu       if(tolr>0.){
57558a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
57568a175baeSEmil Constantinescu         nr_loc++;
57578a175baeSEmil Constantinescu       }
57588a175baeSEmil Constantinescu       tol=tola+tolr;
57598a175baeSEmil Constantinescu       if(tol>0.){
57608a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
57618a175baeSEmil Constantinescu         n_loc++;
57628a175baeSEmil Constantinescu       }
57638a175baeSEmil Constantinescu     }
57648a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57658a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57668a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57678a175baeSEmil Constantinescu     const PetscScalar *atol;
57688a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57698a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
577076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57718a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57728a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57738a175baeSEmil Constantinescu       if(tola>0.){
57748a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57758a175baeSEmil Constantinescu         na_loc++;
57768a175baeSEmil Constantinescu       }
57778a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57788a175baeSEmil Constantinescu       if(tolr>0.){
57798a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
57808a175baeSEmil Constantinescu         nr_loc++;
57818a175baeSEmil Constantinescu       }
57828a175baeSEmil Constantinescu       tol=tola+tolr;
57838a175baeSEmil Constantinescu       if(tol>0.){
57848a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
57858a175baeSEmil Constantinescu         n_loc++;
57868a175baeSEmil Constantinescu       }
57878a175baeSEmil Constantinescu     }
57888a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57898a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57908a175baeSEmil Constantinescu     const PetscScalar *rtol;
57918a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57928a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
579376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57948a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57958a175baeSEmil Constantinescu       tola = ts->atol;
57968a175baeSEmil Constantinescu       if(tola>0.){
57978a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57988a175baeSEmil Constantinescu         na_loc++;
57998a175baeSEmil Constantinescu       }
58008a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58018a175baeSEmil Constantinescu       if(tolr>0.){
58028a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58038a175baeSEmil Constantinescu         nr_loc++;
58048a175baeSEmil Constantinescu       }
58058a175baeSEmil Constantinescu       tol=tola+tolr;
58068a175baeSEmil Constantinescu       if(tol>0.){
58078a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58088a175baeSEmil Constantinescu         n_loc++;
58098a175baeSEmil Constantinescu       }
58108a175baeSEmil Constantinescu     }
58118a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58128a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
58138a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
581476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58158a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58168a175baeSEmil Constantinescu       tola = ts->atol;
58178a175baeSEmil Constantinescu       if(tola>0.){
58188a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58198a175baeSEmil Constantinescu         na_loc++;
58208a175baeSEmil Constantinescu       }
58218a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58228a175baeSEmil Constantinescu       if(tolr>0.){
58238a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58248a175baeSEmil Constantinescu         nr_loc++;
58258a175baeSEmil Constantinescu       }
58268a175baeSEmil Constantinescu       tol=tola+tolr;
58278a175baeSEmil Constantinescu       if(tol>0.){
58288a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58298a175baeSEmil Constantinescu         n_loc++;
58308a175baeSEmil Constantinescu       }
58318a175baeSEmil Constantinescu     }
58328a175baeSEmil Constantinescu   }
58338a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
58348a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58358a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58368a175baeSEmil Constantinescu 
58378a175baeSEmil Constantinescu   err_loc[0] = sum;
58388a175baeSEmil Constantinescu   err_loc[1] = suma;
58398a175baeSEmil Constantinescu   err_loc[2] = sumr;
58408a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58418a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58428a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58438a175baeSEmil Constantinescu 
5844a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58458a175baeSEmil Constantinescu 
58468a175baeSEmil Constantinescu   gsum   = err_glb[0];
58478a175baeSEmil Constantinescu   gsuma  = err_glb[1];
58488a175baeSEmil Constantinescu   gsumr  = err_glb[2];
58498a175baeSEmil Constantinescu   n_glb  = err_glb[3];
58508a175baeSEmil Constantinescu   na_glb = err_glb[4];
58518a175baeSEmil Constantinescu   nr_glb = err_glb[5];
58528a175baeSEmil Constantinescu 
58538a175baeSEmil Constantinescu   *norm  = 0.;
58548a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
58558a175baeSEmil Constantinescu   *norma = 0.;
58568a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
58578a175baeSEmil Constantinescu   *normr = 0.;
58588a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58598a175baeSEmil Constantinescu 
58608a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58618a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58628a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58638a175baeSEmil Constantinescu   PetscFunctionReturn(0);
58648a175baeSEmil Constantinescu }
58658a175baeSEmil Constantinescu 
58668a175baeSEmil Constantinescu /*@
58678a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
58688a175baeSEmil Constantinescu    Collective on TS
58698a175baeSEmil Constantinescu 
58708a175baeSEmil Constantinescu    Input Arguments:
58718a175baeSEmil Constantinescu +  ts - time stepping context
58728a175baeSEmil Constantinescu .  E - error vector
58738a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
58748a175baeSEmil Constantinescu -  Y - state vector, previous time step
58758a175baeSEmil Constantinescu 
58768a175baeSEmil Constantinescu    Output Arguments:
5877a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58788a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5879a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58808a175baeSEmil Constantinescu 
58818a175baeSEmil Constantinescu    Level: developer
58828a175baeSEmil Constantinescu 
58838a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
58848a175baeSEmil Constantinescu @*/
58858a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58868a175baeSEmil Constantinescu {
58878a175baeSEmil Constantinescu   PetscErrorCode    ierr;
58888a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
58898a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
58908a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
58918a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
58928a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
58938a175baeSEmil Constantinescu 
58948a175baeSEmil Constantinescu   PetscFunctionBegin;
58958a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58968a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
58978a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
58988a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
58998a175baeSEmil Constantinescu   PetscValidType(E,2);
59008a175baeSEmil Constantinescu   PetscValidType(U,3);
59018a175baeSEmil Constantinescu   PetscValidType(Y,4);
59028a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
59038a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
59048a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
59058a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
59068a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
59078a175baeSEmil Constantinescu 
59088a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
59098a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
59108a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
59118a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
59128a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59138a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59148a175baeSEmil Constantinescu 
59158a175baeSEmil Constantinescu   max=0.;
59168a175baeSEmil Constantinescu   maxa=0.;
59178a175baeSEmil Constantinescu   maxr=0.;
59188a175baeSEmil Constantinescu 
59198a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59208a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
59218a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59228a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59238a175baeSEmil Constantinescu 
59248a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
592576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59268a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59278a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59288a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59298a175baeSEmil Constantinescu       tol  = tola+tolr;
59308a175baeSEmil Constantinescu       if(tola>0.){
59318a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59328a175baeSEmil Constantinescu       }
59338a175baeSEmil Constantinescu       if(tolr>0.){
59348a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59358a175baeSEmil Constantinescu       }
59368a175baeSEmil Constantinescu       if(tol>0.){
59378a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59388a175baeSEmil Constantinescu       }
59398a175baeSEmil Constantinescu     }
59408a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59418a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59428a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59438a175baeSEmil Constantinescu     const PetscScalar *atol;
59448a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59458a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
594676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59478a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59488a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59498a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59508a175baeSEmil Constantinescu       tol  = tola+tolr;
59518a175baeSEmil Constantinescu       if(tola>0.){
59528a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59538a175baeSEmil Constantinescu       }
59548a175baeSEmil Constantinescu       if(tolr>0.){
59558a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59568a175baeSEmil Constantinescu       }
59578a175baeSEmil Constantinescu       if(tol>0.){
59588a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59598a175baeSEmil Constantinescu       }
59608a175baeSEmil Constantinescu     }
59618a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59628a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59638a175baeSEmil Constantinescu     const PetscScalar *rtol;
59648a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59658a175baeSEmil Constantinescu 
59668a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
596776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59688a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59698a175baeSEmil Constantinescu       tola = ts->atol;
59708a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59718a175baeSEmil Constantinescu       tol  = tola+tolr;
59728a175baeSEmil Constantinescu       if(tola>0.){
59738a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59748a175baeSEmil Constantinescu       }
59758a175baeSEmil Constantinescu       if(tolr>0.){
59768a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59778a175baeSEmil Constantinescu       }
59788a175baeSEmil Constantinescu       if(tol>0.){
59798a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59808a175baeSEmil Constantinescu       }
59818a175baeSEmil Constantinescu     }
59828a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59838a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
59848a175baeSEmil Constantinescu 
59858a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
598676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59878a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59888a175baeSEmil Constantinescu       tola = ts->atol;
59898a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59908a175baeSEmil Constantinescu       tol  = tola+tolr;
59918a175baeSEmil Constantinescu       if(tola>0.){
59928a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59938a175baeSEmil Constantinescu       }
59948a175baeSEmil Constantinescu       if(tolr>0.){
59958a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59968a175baeSEmil Constantinescu       }
59978a175baeSEmil Constantinescu       if(tol>0.){
59988a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59998a175baeSEmil Constantinescu       }
60008a175baeSEmil Constantinescu     }
60018a175baeSEmil Constantinescu   }
60028a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
60038a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60048a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60058a175baeSEmil Constantinescu   err_loc[0] = max;
60068a175baeSEmil Constantinescu   err_loc[1] = maxa;
60078a175baeSEmil Constantinescu   err_loc[2] = maxr;
6008a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60098a175baeSEmil Constantinescu   gmax   = err_glb[0];
60108a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
60118a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
60128a175baeSEmil Constantinescu 
60138a175baeSEmil Constantinescu   *norm = gmax;
60148a175baeSEmil Constantinescu   *norma = gmaxa;
60158a175baeSEmil Constantinescu   *normr = gmaxr;
60168a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60178a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60188a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60198a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60208a175baeSEmil Constantinescu }
60218a175baeSEmil Constantinescu 
60228a175baeSEmil Constantinescu /*@
60238a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
60248a175baeSEmil Constantinescu 
60258a175baeSEmil Constantinescu    Collective on TS
60268a175baeSEmil Constantinescu 
60278a175baeSEmil Constantinescu    Input Arguments:
60288a175baeSEmil Constantinescu +  ts - time stepping context
60298a175baeSEmil Constantinescu .  E - error vector
60308a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60318a175baeSEmil Constantinescu .  Y - state vector, previous time step
60328a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60338a175baeSEmil Constantinescu 
60348a175baeSEmil Constantinescu    Output Arguments:
6035a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60368a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6037a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
60388a175baeSEmil Constantinescu 
60398a175baeSEmil Constantinescu    Options Database Keys:
60408a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
60418a175baeSEmil Constantinescu 
60428a175baeSEmil Constantinescu    Level: developer
60438a175baeSEmil Constantinescu 
60448a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
60458a175baeSEmil Constantinescu @*/
60468a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60478a175baeSEmil Constantinescu {
60488a175baeSEmil Constantinescu   PetscErrorCode ierr;
60498a175baeSEmil Constantinescu 
60508a175baeSEmil Constantinescu   PetscFunctionBegin;
60518a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
60528a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60538a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
60548a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60558a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60568a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60578a175baeSEmil Constantinescu }
60588a175baeSEmil Constantinescu 
60598a175baeSEmil Constantinescu 
60608d59e960SJed Brown /*@
60618d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
60628d59e960SJed Brown 
60638d59e960SJed Brown    Logically Collective on TS
60648d59e960SJed Brown 
60658d59e960SJed Brown    Input Arguments:
60668d59e960SJed Brown +  ts - time stepping context
60678d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
60688d59e960SJed Brown 
60698d59e960SJed Brown    Note:
60708d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
60718d59e960SJed Brown 
60728d59e960SJed Brown    Level: intermediate
60738d59e960SJed Brown 
60748d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
60758d59e960SJed Brown @*/
60768d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
60778d59e960SJed Brown {
60788d59e960SJed Brown   PetscFunctionBegin;
60798d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60808d59e960SJed Brown   ts->cfltime_local = cfltime;
60818d59e960SJed Brown   ts->cfltime       = -1.;
60828d59e960SJed Brown   PetscFunctionReturn(0);
60838d59e960SJed Brown }
60848d59e960SJed Brown 
60858d59e960SJed Brown /*@
60868d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
60878d59e960SJed Brown 
60888d59e960SJed Brown    Collective on TS
60898d59e960SJed Brown 
60908d59e960SJed Brown    Input Arguments:
60918d59e960SJed Brown .  ts - time stepping context
60928d59e960SJed Brown 
60938d59e960SJed Brown    Output Arguments:
60948d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
60958d59e960SJed Brown 
60968d59e960SJed Brown    Level: advanced
60978d59e960SJed Brown 
60988d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
60998d59e960SJed Brown @*/
61008d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
61018d59e960SJed Brown {
61028d59e960SJed Brown   PetscErrorCode ierr;
61038d59e960SJed Brown 
61048d59e960SJed Brown   PetscFunctionBegin;
61058d59e960SJed Brown   if (ts->cfltime < 0) {
6106b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61078d59e960SJed Brown   }
61088d59e960SJed Brown   *cfltime = ts->cfltime;
61098d59e960SJed Brown   PetscFunctionReturn(0);
61108d59e960SJed Brown }
61118d59e960SJed Brown 
6112d6ebe24aSShri Abhyankar /*@
6113d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6114d6ebe24aSShri Abhyankar 
6115d6ebe24aSShri Abhyankar    Input Parameters:
6116a2b725a8SWilliam Gropp +  ts   - the TS context.
6117d6ebe24aSShri Abhyankar .  xl   - lower bound.
6118a2b725a8SWilliam Gropp -  xu   - upper bound.
6119d6ebe24aSShri Abhyankar 
6120d6ebe24aSShri Abhyankar    Notes:
6121d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6122e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6123d6ebe24aSShri Abhyankar 
61242bd2b0e6SSatish Balay    Level: advanced
61252bd2b0e6SSatish Balay 
6126d6ebe24aSShri Abhyankar @*/
6127d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6128d6ebe24aSShri Abhyankar {
6129d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6130d6ebe24aSShri Abhyankar   SNES           snes;
6131d6ebe24aSShri Abhyankar 
6132d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6133d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6134d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6135d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6136d6ebe24aSShri Abhyankar }
6137d6ebe24aSShri Abhyankar 
6138b3603a34SBarry Smith /*@C
61394f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6140b3603a34SBarry Smith        in a time based line graph
6141b3603a34SBarry Smith 
6142b3603a34SBarry Smith    Collective on TS
6143b3603a34SBarry Smith 
6144b3603a34SBarry Smith    Input Parameters:
6145b3603a34SBarry Smith +  ts - the TS context
6146b3603a34SBarry Smith .  step - current time-step
6147b3603a34SBarry Smith .  ptime - current time
61487db568b7SBarry Smith .  u - current solution
61497db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6150b3603a34SBarry Smith 
6151b3d3934dSBarry Smith    Options Database:
61529ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6153b3d3934dSBarry Smith 
6154b3603a34SBarry Smith    Level: intermediate
6155b3603a34SBarry Smith 
615695452b02SPatrick Sanan    Notes:
615795452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6158b3603a34SBarry Smith 
61597db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
61607db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
61617db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
61627db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6163b3603a34SBarry Smith @*/
61647db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6165b3603a34SBarry Smith {
6166b3603a34SBarry Smith   PetscErrorCode    ierr;
61677db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6168b3603a34SBarry Smith   const PetscScalar *yy;
616980666b62SBarry Smith   Vec               v;
6170b3603a34SBarry Smith 
6171b3603a34SBarry Smith   PetscFunctionBegin;
617263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
617358ff32f7SBarry Smith   if (!step) {
6174a9f9c1f6SBarry Smith     PetscDrawAxis axis;
61756934998bSLisandro Dalcin     PetscInt      dim;
6176a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
61770ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6178bab0a581SBarry Smith     if (!ctx->names) {
6179bab0a581SBarry Smith       PetscBool flg;
6180bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6181bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6182bab0a581SBarry Smith       if (flg) {
6183bab0a581SBarry Smith         PetscInt i,n;
6184bab0a581SBarry Smith         char     **names;
6185bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6186bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6187bab0a581SBarry Smith         for (i=0; i<n; i++) {
6188bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6189bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6190bab0a581SBarry Smith         }
6191bab0a581SBarry Smith         names[n] = NULL;
6192bab0a581SBarry Smith         ctx->names = names;
6193bab0a581SBarry Smith       }
6194bab0a581SBarry Smith     }
6195387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6196387f4636SBarry Smith       char      **displaynames;
6197387f4636SBarry Smith       PetscBool flg;
6198387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6199580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6200c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6201387f4636SBarry Smith       if (flg) {
6202a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6203387f4636SBarry Smith       }
6204387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6205387f4636SBarry Smith     }
6206387f4636SBarry Smith     if (ctx->displaynames) {
6207387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6208387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6209387f4636SBarry Smith     } else if (ctx->names) {
62100910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
62110b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6212387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6213b0bc92c6SBarry Smith     } else {
6214b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6215b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6216387f4636SBarry Smith     }
62170b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
621858ff32f7SBarry Smith   }
62196934998bSLisandro Dalcin 
62206934998bSLisandro Dalcin   if (!ctx->transform) v = u;
62216934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
622280666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
62236934998bSLisandro Dalcin   if (ctx->displaynames) {
62246934998bSLisandro Dalcin     PetscInt i;
62256934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
62266934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
62276934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
62286934998bSLisandro Dalcin   } else {
6229e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6230e3efe391SJed Brown     PetscInt  i,n;
62316934998bSLisandro Dalcin     PetscReal *yreal;
623280666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6233785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6234e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
62350b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6236e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6237e3efe391SJed Brown #else
62380b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6239e3efe391SJed Brown #endif
624080666b62SBarry Smith   }
62416934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
62426934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
62436934998bSLisandro Dalcin 
6244b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
62450b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
62466934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
62473923b477SBarry Smith   }
6248b3603a34SBarry Smith   PetscFunctionReturn(0);
6249b3603a34SBarry Smith }
6250b3603a34SBarry Smith 
6251b037adc7SBarry Smith /*@C
625231152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6253b037adc7SBarry Smith 
6254b037adc7SBarry Smith    Collective on TS
6255b037adc7SBarry Smith 
6256b037adc7SBarry Smith    Input Parameters:
6257b037adc7SBarry Smith +  ts - the TS context
6258b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6259b037adc7SBarry Smith 
6260b037adc7SBarry Smith    Level: intermediate
6261b037adc7SBarry Smith 
626295452b02SPatrick Sanan    Notes:
626395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
62647db568b7SBarry Smith 
6265a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6266b037adc7SBarry Smith @*/
626731152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6268b037adc7SBarry Smith {
6269b037adc7SBarry Smith   PetscErrorCode    ierr;
6270b037adc7SBarry Smith   PetscInt          i;
6271b037adc7SBarry Smith 
6272b037adc7SBarry Smith   PetscFunctionBegin;
6273b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6274b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
62755537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6276b3d3934dSBarry Smith       break;
6277b3d3934dSBarry Smith     }
6278b3d3934dSBarry Smith   }
6279b3d3934dSBarry Smith   PetscFunctionReturn(0);
6280b3d3934dSBarry Smith }
6281b3d3934dSBarry Smith 
6282e673d494SBarry Smith /*@C
6283e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6284e673d494SBarry Smith 
6285e673d494SBarry Smith    Collective on TS
6286e673d494SBarry Smith 
6287e673d494SBarry Smith    Input Parameters:
6288e673d494SBarry Smith +  ts - the TS context
6289e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6290e673d494SBarry Smith 
6291e673d494SBarry Smith    Level: intermediate
6292e673d494SBarry Smith 
6293a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6294e673d494SBarry Smith @*/
6295e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6296e673d494SBarry Smith {
6297e673d494SBarry Smith   PetscErrorCode    ierr;
6298e673d494SBarry Smith 
6299e673d494SBarry Smith   PetscFunctionBegin;
6300e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6301e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6302e673d494SBarry Smith   PetscFunctionReturn(0);
6303e673d494SBarry Smith }
6304e673d494SBarry Smith 
6305b3d3934dSBarry Smith /*@C
6306b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6307b3d3934dSBarry Smith 
6308b3d3934dSBarry Smith    Collective on TS
6309b3d3934dSBarry Smith 
6310b3d3934dSBarry Smith    Input Parameter:
6311b3d3934dSBarry Smith .  ts - the TS context
6312b3d3934dSBarry Smith 
6313b3d3934dSBarry Smith    Output Parameter:
6314b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6315b3d3934dSBarry Smith 
6316b3d3934dSBarry Smith    Level: intermediate
6317b3d3934dSBarry Smith 
631895452b02SPatrick Sanan    Notes:
631995452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
63207db568b7SBarry Smith 
6321b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6322b3d3934dSBarry Smith @*/
6323b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6324b3d3934dSBarry Smith {
6325b3d3934dSBarry Smith   PetscInt       i;
6326b3d3934dSBarry Smith 
6327b3d3934dSBarry Smith   PetscFunctionBegin;
6328b3d3934dSBarry Smith   *names = NULL;
6329b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6330b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
63315537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6332b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6333b3d3934dSBarry Smith       break;
6334387f4636SBarry Smith     }
6335387f4636SBarry Smith   }
6336387f4636SBarry Smith   PetscFunctionReturn(0);
6337387f4636SBarry Smith }
6338387f4636SBarry Smith 
6339a66092f1SBarry Smith /*@C
6340a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6341a66092f1SBarry Smith 
6342a66092f1SBarry Smith    Collective on TS
6343a66092f1SBarry Smith 
6344a66092f1SBarry Smith    Input Parameters:
6345a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6346a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6347a66092f1SBarry Smith 
6348a66092f1SBarry Smith    Level: intermediate
6349a66092f1SBarry Smith 
6350a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6351a66092f1SBarry Smith @*/
6352a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6353a66092f1SBarry Smith {
6354a66092f1SBarry Smith   PetscInt          j = 0,k;
6355a66092f1SBarry Smith   PetscErrorCode    ierr;
6356a66092f1SBarry Smith 
6357a66092f1SBarry Smith   PetscFunctionBegin;
6358a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6359a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6360a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6361a66092f1SBarry Smith   while (displaynames[j]) j++;
6362a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6363a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6364a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6365a66092f1SBarry Smith   j = 0;
6366a66092f1SBarry Smith   while (displaynames[j]) {
6367a66092f1SBarry Smith     k = 0;
6368a66092f1SBarry Smith     while (ctx->names[k]) {
6369a66092f1SBarry Smith       PetscBool flg;
6370a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6371a66092f1SBarry Smith       if (flg) {
6372a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6373a66092f1SBarry Smith         break;
6374a66092f1SBarry Smith       }
6375a66092f1SBarry Smith       k++;
6376a66092f1SBarry Smith     }
6377a66092f1SBarry Smith     j++;
6378a66092f1SBarry Smith   }
6379a66092f1SBarry Smith   PetscFunctionReturn(0);
6380a66092f1SBarry Smith }
6381a66092f1SBarry Smith 
6382387f4636SBarry Smith /*@C
6383387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6384387f4636SBarry Smith 
6385387f4636SBarry Smith    Collective on TS
6386387f4636SBarry Smith 
6387387f4636SBarry Smith    Input Parameters:
6388387f4636SBarry Smith +  ts - the TS context
6389a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6390387f4636SBarry Smith 
639195452b02SPatrick Sanan    Notes:
639295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
63937db568b7SBarry Smith 
6394387f4636SBarry Smith    Level: intermediate
6395387f4636SBarry Smith 
6396387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6397387f4636SBarry Smith @*/
6398387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6399387f4636SBarry Smith {
6400a66092f1SBarry Smith   PetscInt          i;
6401387f4636SBarry Smith   PetscErrorCode    ierr;
6402387f4636SBarry Smith 
6403387f4636SBarry Smith   PetscFunctionBegin;
6404387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6405387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
64065537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6407b3d3934dSBarry Smith       break;
6408b037adc7SBarry Smith     }
6409b037adc7SBarry Smith   }
6410b037adc7SBarry Smith   PetscFunctionReturn(0);
6411b037adc7SBarry Smith }
6412b037adc7SBarry Smith 
641380666b62SBarry Smith /*@C
641480666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
641580666b62SBarry Smith 
641680666b62SBarry Smith    Collective on TS
641780666b62SBarry Smith 
641880666b62SBarry Smith    Input Parameters:
641980666b62SBarry Smith +  ts - the TS context
642080666b62SBarry Smith .  transform - the transform function
64217684fa3eSBarry Smith .  destroy - function to destroy the optional context
642280666b62SBarry Smith -  ctx - optional context used by transform function
642380666b62SBarry Smith 
642495452b02SPatrick Sanan    Notes:
642595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
64267db568b7SBarry Smith 
642780666b62SBarry Smith    Level: intermediate
642880666b62SBarry Smith 
6429a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
643080666b62SBarry Smith @*/
64317684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
643280666b62SBarry Smith {
643380666b62SBarry Smith   PetscInt          i;
6434a66092f1SBarry Smith   PetscErrorCode    ierr;
643580666b62SBarry Smith 
643680666b62SBarry Smith   PetscFunctionBegin;
643780666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
643880666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
64395537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
644080666b62SBarry Smith     }
644180666b62SBarry Smith   }
644280666b62SBarry Smith   PetscFunctionReturn(0);
644380666b62SBarry Smith }
644480666b62SBarry Smith 
6445e673d494SBarry Smith /*@C
6446e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6447e673d494SBarry Smith 
6448e673d494SBarry Smith    Collective on TSLGCtx
6449e673d494SBarry Smith 
6450e673d494SBarry Smith    Input Parameters:
6451e673d494SBarry Smith +  ts - the TS context
6452e673d494SBarry Smith .  transform - the transform function
64537684fa3eSBarry Smith .  destroy - function to destroy the optional context
6454e673d494SBarry Smith -  ctx - optional context used by transform function
6455e673d494SBarry Smith 
6456e673d494SBarry Smith    Level: intermediate
6457e673d494SBarry Smith 
6458a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6459e673d494SBarry Smith @*/
64607684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6461e673d494SBarry Smith {
6462e673d494SBarry Smith   PetscFunctionBegin;
6463e673d494SBarry Smith   ctx->transform    = transform;
64647684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6465e673d494SBarry Smith   ctx->transformctx = tctx;
6466e673d494SBarry Smith   PetscFunctionReturn(0);
6467e673d494SBarry Smith }
6468e673d494SBarry Smith 
6469ef20d060SBarry Smith /*@C
64708b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6471ef20d060SBarry Smith        in a time based line graph
6472ef20d060SBarry Smith 
6473ef20d060SBarry Smith    Collective on TS
6474ef20d060SBarry Smith 
6475ef20d060SBarry Smith    Input Parameters:
6476ef20d060SBarry Smith +  ts - the TS context
6477ef20d060SBarry Smith .  step - current time-step
6478ef20d060SBarry Smith .  ptime - current time
64797db568b7SBarry Smith .  u - current solution
64807db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6481ef20d060SBarry Smith 
6482ef20d060SBarry Smith    Level: intermediate
6483ef20d060SBarry Smith 
648495452b02SPatrick Sanan    Notes:
648595452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6486abd5a294SJed Brown 
6487abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6488abd5a294SJed Brown 
6489abd5a294SJed Brown    Options Database Keys:
64904f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6491ef20d060SBarry Smith 
6492abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6493ef20d060SBarry Smith @*/
64940910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6495ef20d060SBarry Smith {
6496ef20d060SBarry Smith   PetscErrorCode    ierr;
64970b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6498ef20d060SBarry Smith   const PetscScalar *yy;
6499ef20d060SBarry Smith   Vec               y;
6500ef20d060SBarry Smith 
6501ef20d060SBarry Smith   PetscFunctionBegin;
6502a9f9c1f6SBarry Smith   if (!step) {
6503a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65046934998bSLisandro Dalcin     PetscInt      dim;
6505a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65068b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
65070910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6508a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6509a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6510a9f9c1f6SBarry Smith   }
65110910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6512ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
65130910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6514ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6515e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6516e3efe391SJed Brown   {
6517e3efe391SJed Brown     PetscReal *yreal;
6518e3efe391SJed Brown     PetscInt  i,n;
6519e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6520785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6521e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
65220b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6523e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6524e3efe391SJed Brown   }
6525e3efe391SJed Brown #else
65260b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6527e3efe391SJed Brown #endif
6528ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6529ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6530b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65310b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65326934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65333923b477SBarry Smith   }
6534ef20d060SBarry Smith   PetscFunctionReturn(0);
6535ef20d060SBarry Smith }
6536ef20d060SBarry Smith 
65375e3b7effSJoseph Pusztay /*@C
65385e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
65395e3b7effSJoseph Pusztay 
65405e3b7effSJoseph Pusztay    Input Parameters:
65415e3b7effSJoseph Pusztay +  ts - the TS context
65425e3b7effSJoseph Pusztay .  step - current time-step
65435e3b7effSJoseph Pusztay .  ptime - current time
65445e3b7effSJoseph Pusztay .  u - current solution
65455e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
65465e3b7effSJoseph Pusztay 
65475e3b7effSJoseph Pusztay    Options Database:
6548918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
65495e3b7effSJoseph Pusztay 
65505e3b7effSJoseph Pusztay    Level: intermediate
65515e3b7effSJoseph Pusztay 
65525e3b7effSJoseph Pusztay @*/
65530ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
65541b575b74SJoseph Pusztay {
65551b575b74SJoseph Pusztay   PetscErrorCode    ierr;
65561b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
65571b575b74SJoseph Pusztay   const PetscScalar *yy;
6558b1670a61SJoseph Pusztay   PetscReal       *y,*x;
65591b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
65601b575b74SJoseph Pusztay   DM                dm;
65611b575b74SJoseph Pusztay 
65621b575b74SJoseph Pusztay   PetscFunctionBegin;
65631b575b74SJoseph Pusztay 
65641b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
65651b575b74SJoseph Pusztay   if (!step) {
65661b575b74SJoseph Pusztay     PetscDrawAxis axis;
65671b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
65681b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6569895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6570895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
65711b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
65721b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
65731b575b74SJoseph Pusztay     if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
65741b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
65751b575b74SJoseph Pusztay     Np /= 2*dim;
65761b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
65771b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
65781b575b74SJoseph Pusztay   }
65791b575b74SJoseph Pusztay 
65801b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
65811b575b74SJoseph Pusztay   Np /= 2*dim;
65821b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6583895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
65841b575b74SJoseph Pusztay   /* get points from solution vector */
65851b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6586b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6587b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6588895f37d5SJoseph Pusztay   }
65891b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
65901b575b74SJoseph Pusztay 
65911b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65921b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
65931b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
65941b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
65951b575b74SJoseph Pusztay   }
65961b575b74SJoseph Pusztay 
6597918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6598918b1d10SJoseph Pusztay 
65991b575b74SJoseph Pusztay   PetscFunctionReturn(0);
66001b575b74SJoseph Pusztay }
66011b575b74SJoseph Pusztay 
66021b575b74SJoseph Pusztay 
66031b575b74SJoseph Pusztay 
66047cf37e64SBarry Smith /*@C
66057cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
66067cf37e64SBarry Smith 
66077cf37e64SBarry Smith    Collective on TS
66087cf37e64SBarry Smith 
66097cf37e64SBarry Smith    Input Parameters:
66107cf37e64SBarry Smith +  ts - the TS context
66117cf37e64SBarry Smith .  step - current time-step
66127cf37e64SBarry Smith .  ptime - current time
66137cf37e64SBarry Smith .  u - current solution
66147cf37e64SBarry Smith -  dctx - unused context
66157cf37e64SBarry Smith 
66167cf37e64SBarry Smith    Level: intermediate
66177cf37e64SBarry Smith 
66187cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
66197cf37e64SBarry Smith 
66207cf37e64SBarry Smith    Options Database Keys:
66217cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
66227cf37e64SBarry Smith 
66237cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
66247cf37e64SBarry Smith @*/
6625edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
66267cf37e64SBarry Smith {
66277cf37e64SBarry Smith   PetscErrorCode    ierr;
66287cf37e64SBarry Smith   Vec               y;
66297cf37e64SBarry Smith   PetscReal         nrm;
6630edbaebb3SBarry Smith   PetscBool         flg;
66317cf37e64SBarry Smith 
66327cf37e64SBarry Smith   PetscFunctionBegin;
66337cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
66347cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
66357cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6636edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6637edbaebb3SBarry Smith   if (flg) {
66387cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6639edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6640edbaebb3SBarry Smith   }
6641edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6642edbaebb3SBarry Smith   if (flg) {
6643edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6644edbaebb3SBarry Smith   }
6645edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
66467cf37e64SBarry Smith   PetscFunctionReturn(0);
66477cf37e64SBarry Smith }
66487cf37e64SBarry Smith 
6649201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6650201da799SBarry Smith {
6651201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6652201da799SBarry Smith   PetscReal      x   = ptime,y;
6653201da799SBarry Smith   PetscErrorCode ierr;
6654201da799SBarry Smith   PetscInt       its;
6655201da799SBarry Smith 
6656201da799SBarry Smith   PetscFunctionBegin;
665763e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6658201da799SBarry Smith   if (!n) {
6659201da799SBarry Smith     PetscDrawAxis axis;
6660201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6661201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6662201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6663201da799SBarry Smith     ctx->snes_its = 0;
6664201da799SBarry Smith   }
6665201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6666201da799SBarry Smith   y    = its - ctx->snes_its;
6667201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
66683fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6669201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66706934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6671201da799SBarry Smith   }
6672201da799SBarry Smith   ctx->snes_its = its;
6673201da799SBarry Smith   PetscFunctionReturn(0);
6674201da799SBarry Smith }
6675201da799SBarry Smith 
6676201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6677201da799SBarry Smith {
6678201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6679201da799SBarry Smith   PetscReal      x   = ptime,y;
6680201da799SBarry Smith   PetscErrorCode ierr;
6681201da799SBarry Smith   PetscInt       its;
6682201da799SBarry Smith 
6683201da799SBarry Smith   PetscFunctionBegin;
668463e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6685201da799SBarry Smith   if (!n) {
6686201da799SBarry Smith     PetscDrawAxis axis;
6687201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6688201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6689201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6690201da799SBarry Smith     ctx->ksp_its = 0;
6691201da799SBarry Smith   }
6692201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6693201da799SBarry Smith   y    = its - ctx->ksp_its;
6694201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
669599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6696201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66976934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6698201da799SBarry Smith   }
6699201da799SBarry Smith   ctx->ksp_its = its;
6700201da799SBarry Smith   PetscFunctionReturn(0);
6701201da799SBarry Smith }
6702f9c1d6abSBarry Smith 
6703f9c1d6abSBarry Smith /*@
6704f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6705f9c1d6abSBarry Smith 
6706d083f849SBarry Smith    Collective on TS
6707f9c1d6abSBarry Smith 
6708f9c1d6abSBarry Smith    Input Parameters:
6709f9c1d6abSBarry Smith +  ts - the TS context
6710f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6711f9c1d6abSBarry Smith 
6712f9c1d6abSBarry Smith    Output Parameters:
6713f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6714f9c1d6abSBarry Smith 
6715f9c1d6abSBarry Smith    Level: developer
6716f9c1d6abSBarry Smith 
6717f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6718f9c1d6abSBarry Smith @*/
6719f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6720f9c1d6abSBarry Smith {
6721f9c1d6abSBarry Smith   PetscErrorCode ierr;
6722f9c1d6abSBarry Smith 
6723f9c1d6abSBarry Smith   PetscFunctionBegin;
6724f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6725ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6726f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6727f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6728f9c1d6abSBarry Smith }
672924655328SShri 
6730b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6731b3d3934dSBarry Smith /*@C
6732b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6733b3d3934dSBarry Smith 
6734b3d3934dSBarry Smith    Collective on TS
6735b3d3934dSBarry Smith 
6736b3d3934dSBarry Smith    Input Parameters:
6737b3d3934dSBarry Smith .  ts  - the ODE solver object
6738b3d3934dSBarry Smith 
6739b3d3934dSBarry Smith    Output Parameter:
6740b3d3934dSBarry Smith .  ctx - the context
6741b3d3934dSBarry Smith 
6742b3d3934dSBarry Smith    Level: intermediate
6743b3d3934dSBarry Smith 
6744b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6745b3d3934dSBarry Smith 
6746b3d3934dSBarry Smith @*/
6747b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6748b3d3934dSBarry Smith {
6749b3d3934dSBarry Smith   PetscErrorCode ierr;
6750b3d3934dSBarry Smith 
6751b3d3934dSBarry Smith   PetscFunctionBegin;
6752a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6753b3d3934dSBarry Smith   PetscFunctionReturn(0);
6754b3d3934dSBarry Smith }
6755b3d3934dSBarry Smith 
6756b3d3934dSBarry Smith /*@C
6757b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6758b3d3934dSBarry Smith 
6759b3d3934dSBarry Smith    Collective on TS
6760b3d3934dSBarry Smith 
6761b3d3934dSBarry Smith    Input Parameters:
6762b3d3934dSBarry Smith +  ts - the TS context
6763b3d3934dSBarry Smith .  step - current time-step
6764b3d3934dSBarry Smith .  ptime - current time
67657db568b7SBarry Smith .  u  - current solution
67667db568b7SBarry Smith -  dctx - the envelope context
6767b3d3934dSBarry Smith 
6768b3d3934dSBarry Smith    Options Database:
6769b3d3934dSBarry Smith .  -ts_monitor_envelope
6770b3d3934dSBarry Smith 
6771b3d3934dSBarry Smith    Level: intermediate
6772b3d3934dSBarry Smith 
677395452b02SPatrick Sanan    Notes:
677495452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
6775b3d3934dSBarry Smith 
67767db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
6777b3d3934dSBarry Smith @*/
67787db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6779b3d3934dSBarry Smith {
6780b3d3934dSBarry Smith   PetscErrorCode       ierr;
67817db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
6782b3d3934dSBarry Smith 
6783b3d3934dSBarry Smith   PetscFunctionBegin;
6784b3d3934dSBarry Smith   if (!ctx->max) {
6785b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
6786b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
6787b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
6788b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
6789b3d3934dSBarry Smith   } else {
6790b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
6791b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
6792b3d3934dSBarry Smith   }
6793b3d3934dSBarry Smith   PetscFunctionReturn(0);
6794b3d3934dSBarry Smith }
6795b3d3934dSBarry Smith 
6796b3d3934dSBarry Smith /*@C
6797b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
6798b3d3934dSBarry Smith 
6799b3d3934dSBarry Smith    Collective on TS
6800b3d3934dSBarry Smith 
6801b3d3934dSBarry Smith    Input Parameter:
6802b3d3934dSBarry Smith .  ts - the TS context
6803b3d3934dSBarry Smith 
6804b3d3934dSBarry Smith    Output Parameter:
6805b3d3934dSBarry Smith +  max - the maximum values
6806b3d3934dSBarry Smith -  min - the minimum values
6807b3d3934dSBarry Smith 
680895452b02SPatrick Sanan    Notes:
680995452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
68107db568b7SBarry Smith 
6811b3d3934dSBarry Smith    Level: intermediate
6812b3d3934dSBarry Smith 
6813b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6814b3d3934dSBarry Smith @*/
6815b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
6816b3d3934dSBarry Smith {
6817b3d3934dSBarry Smith   PetscInt i;
6818b3d3934dSBarry Smith 
6819b3d3934dSBarry Smith   PetscFunctionBegin;
6820b3d3934dSBarry Smith   if (max) *max = NULL;
6821b3d3934dSBarry Smith   if (min) *min = NULL;
6822b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6823b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
68245537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
6825b3d3934dSBarry Smith       if (max) *max = ctx->max;
6826b3d3934dSBarry Smith       if (min) *min = ctx->min;
6827b3d3934dSBarry Smith       break;
6828b3d3934dSBarry Smith     }
6829b3d3934dSBarry Smith   }
6830b3d3934dSBarry Smith   PetscFunctionReturn(0);
6831b3d3934dSBarry Smith }
6832b3d3934dSBarry Smith 
6833b3d3934dSBarry Smith /*@C
6834b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
6835b3d3934dSBarry Smith 
6836b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
6837b3d3934dSBarry Smith 
6838b3d3934dSBarry Smith    Input Parameter:
6839b3d3934dSBarry Smith .  ctx - the monitor context
6840b3d3934dSBarry Smith 
6841b3d3934dSBarry Smith    Level: intermediate
6842b3d3934dSBarry Smith 
68437db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
6844b3d3934dSBarry Smith @*/
6845b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
6846b3d3934dSBarry Smith {
6847b3d3934dSBarry Smith   PetscErrorCode ierr;
6848b3d3934dSBarry Smith 
6849b3d3934dSBarry Smith   PetscFunctionBegin;
6850b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
6851b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
6852b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
6853b3d3934dSBarry Smith   PetscFunctionReturn(0);
6854b3d3934dSBarry Smith }
6855f2dee214SBarry Smith 
685624655328SShri /*@
6857dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
6858dcb233daSLisandro Dalcin 
6859dcb233daSLisandro Dalcin    Collective on TS
6860dcb233daSLisandro Dalcin 
6861dcb233daSLisandro Dalcin    Input Parameter:
6862dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
6863dcb233daSLisandro Dalcin 
6864dcb233daSLisandro Dalcin    Level: advanced
6865dcb233daSLisandro Dalcin 
6866dcb233daSLisandro Dalcin    Notes:
6867dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
6868dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
6869dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
6870dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
6871dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
6872dcb233daSLisandro Dalcin    discontinuous source terms).
6873dcb233daSLisandro Dalcin 
6874dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
6875dcb233daSLisandro Dalcin @*/
6876dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
6877dcb233daSLisandro Dalcin {
6878dcb233daSLisandro Dalcin   PetscFunctionBegin;
6879dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6880dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
6881dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
6882dcb233daSLisandro Dalcin }
6883dcb233daSLisandro Dalcin 
6884dcb233daSLisandro Dalcin /*@
688524655328SShri    TSRollBack - Rolls back one time step
688624655328SShri 
688724655328SShri    Collective on TS
688824655328SShri 
688924655328SShri    Input Parameter:
689024655328SShri .  ts - the TS context obtained from TSCreate()
689124655328SShri 
689224655328SShri    Level: advanced
689324655328SShri 
689424655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
689524655328SShri @*/
689624655328SShri PetscErrorCode  TSRollBack(TS ts)
689724655328SShri {
689824655328SShri   PetscErrorCode ierr;
689924655328SShri 
690024655328SShri   PetscFunctionBegin;
690124655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6902b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
690324655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
690424655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
690524655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
690624655328SShri   ts->ptime = ts->ptime_prev;
6907be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
69082808aa04SLisandro Dalcin   ts->steps--;
6909b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
691024655328SShri   PetscFunctionReturn(0);
691124655328SShri }
6912aeb4809dSShri Abhyankar 
6913ff22ae23SHong Zhang /*@
6914ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
6915ff22ae23SHong Zhang 
6916ff22ae23SHong Zhang    Input Parameter:
6917ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
6918ff22ae23SHong Zhang 
69190429704eSStefano Zampini    Output Parameters:
69200429704eSStefano Zampini +  ns - the number of stages
69210429704eSStefano Zampini -  Y - the current stage vectors
69220429704eSStefano Zampini 
6923ff22ae23SHong Zhang    Level: advanced
6924ff22ae23SHong Zhang 
69250429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
69260429704eSStefano Zampini 
6927ff22ae23SHong Zhang .seealso: TSCreate()
6928ff22ae23SHong Zhang @*/
6929ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
6930ff22ae23SHong Zhang {
6931ff22ae23SHong Zhang   PetscErrorCode ierr;
6932ff22ae23SHong Zhang 
6933ff22ae23SHong Zhang   PetscFunctionBegin;
6934ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
69350429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
69360429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
69370429704eSStefano Zampini   if (!ts->ops->getstages) {
69380429704eSStefano Zampini     if (ns) *ns = 0;
69390429704eSStefano Zampini     if (Y) *Y = NULL;
69400429704eSStefano Zampini   } else {
6941ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
6942ff22ae23SHong Zhang   }
6943ff22ae23SHong Zhang   PetscFunctionReturn(0);
6944ff22ae23SHong Zhang }
6945ff22ae23SHong Zhang 
6946847ff0e1SMatthew G. Knepley /*@C
6947847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
6948847ff0e1SMatthew G. Knepley 
6949847ff0e1SMatthew G. Knepley   Collective on SNES
6950847ff0e1SMatthew G. Knepley 
6951847ff0e1SMatthew G. Knepley   Input Parameters:
6952847ff0e1SMatthew G. Knepley + ts - the TS context
6953847ff0e1SMatthew G. Knepley . t - current timestep
6954847ff0e1SMatthew G. Knepley . U - state vector
6955847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
6956847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
6957847ff0e1SMatthew G. Knepley - ctx - an optional user context
6958847ff0e1SMatthew G. Knepley 
6959847ff0e1SMatthew G. Knepley   Output Parameters:
6960847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
6961847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
6962847ff0e1SMatthew G. Knepley 
6963847ff0e1SMatthew G. Knepley   Level: intermediate
6964847ff0e1SMatthew G. Knepley 
6965847ff0e1SMatthew G. Knepley   Notes:
6966847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
6967847ff0e1SMatthew G. Knepley 
6968847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
6969847ff0e1SMatthew G. Knepley 
6970847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
6971847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
6972847ff0e1SMatthew G. Knepley 
6973847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
6974847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
6975847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
6976847ff0e1SMatthew G. Knepley 
6977847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
6978847ff0e1SMatthew G. Knepley @*/
6979847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
6980847ff0e1SMatthew G. Knepley {
6981847ff0e1SMatthew G. Knepley   SNES           snes;
6982847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6983847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6984847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6985847ff0e1SMatthew G. Knepley 
6986847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6987c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6988847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6989847ff0e1SMatthew G. Knepley   if (!color) {
6990847ff0e1SMatthew G. Knepley     DM         dm;
6991847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6992847ff0e1SMatthew G. Knepley 
6993847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6994847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6995847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6996847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6997847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6998847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6999847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7000847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7001847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7002847ff0e1SMatthew G. Knepley     } else {
7003847ff0e1SMatthew G. Knepley       MatColoring mc;
7004847ff0e1SMatthew G. Knepley 
7005847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7006847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7007847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7008847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7009847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7010847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7011847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7012847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7013847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7014847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7015847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7016847ff0e1SMatthew G. Knepley     }
7017847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7018847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7019847ff0e1SMatthew G. Knepley   }
7020847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7021847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7022847ff0e1SMatthew G. Knepley   if (J != B) {
7023847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7024847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7025847ff0e1SMatthew G. Knepley   }
7026847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7027847ff0e1SMatthew G. Knepley }
702893b34091SDebojyoti Ghosh 
7029cb9d8021SPierre Barbier de Reuille /*@
70306bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7031cb9d8021SPierre Barbier de Reuille 
7032cb9d8021SPierre Barbier de Reuille     Input Parameters:
70336bc98fa9SBarry Smith +    ts - the TS context
70346bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
70356bc98fa9SBarry Smith 
70366bc98fa9SBarry Smith     Calling sequence of func:
70376bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
70386bc98fa9SBarry Smith 
70396bc98fa9SBarry Smith +   ts - the TS context
70406bc98fa9SBarry Smith .   time - the current time (of the stage)
70416bc98fa9SBarry Smith .   state - the state to check if it is valid
70426bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7043cb9d8021SPierre Barbier de Reuille 
7044cb9d8021SPierre Barbier de Reuille     Level: intermediate
7045cb9d8021SPierre Barbier de Reuille 
70466bc98fa9SBarry Smith     Notes:
70476bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
70486bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
70496bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
70506bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
70516bc98fa9SBarry Smith 
70526bc98fa9SBarry Smith     Developer Notes:
70536bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
70546bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
70556bc98fa9SBarry Smith 
70566bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7057cb9d8021SPierre Barbier de Reuille @*/
7058cb9d8021SPierre Barbier de Reuille 
7059d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7060cb9d8021SPierre Barbier de Reuille {
7061cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7062cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7063cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7064cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7065cb9d8021SPierre Barbier de Reuille }
7066cb9d8021SPierre Barbier de Reuille 
7067cb9d8021SPierre Barbier de Reuille /*@
70686bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7069cb9d8021SPierre Barbier de Reuille 
7070cb9d8021SPierre Barbier de Reuille     Input Parameters:
70716bc98fa9SBarry Smith +    ts - the TS context
70726bc98fa9SBarry Smith .    stagetime - time of the simulation
70736bc98fa9SBarry Smith -    Y - state vector to check.
7074cb9d8021SPierre Barbier de Reuille 
7075cb9d8021SPierre Barbier de Reuille     Output Parameter:
70766bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7077cb9d8021SPierre Barbier de Reuille 
7078cb9d8021SPierre Barbier de Reuille     Note:
70796bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
70806bc98fa9SBarry Smith     to check if the current state is valid.
708196a0c994SBarry Smith 
70826bc98fa9SBarry Smith     Level: developer
70836bc98fa9SBarry Smith 
70846bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7085cb9d8021SPierre Barbier de Reuille @*/
7086d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7087cb9d8021SPierre Barbier de Reuille {
7088cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7089cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7090cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7091cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7092d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7093cb9d8021SPierre Barbier de Reuille   }
7094cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7095cb9d8021SPierre Barbier de Reuille }
70961ceb14c0SBarry Smith 
709793b34091SDebojyoti Ghosh /*@C
7098e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
709993b34091SDebojyoti Ghosh 
7100d083f849SBarry Smith   Collective
710193b34091SDebojyoti Ghosh 
710293b34091SDebojyoti Ghosh   Input Parameter:
710393b34091SDebojyoti Ghosh . tsin    - The input TS
710493b34091SDebojyoti Ghosh 
710593b34091SDebojyoti Ghosh   Output Parameter:
7106e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
710793b34091SDebojyoti Ghosh 
71085eca1a21SEmil Constantinescu   Notes:
71095eca1a21SEmil 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.
71105eca1a21SEmil Constantinescu 
7111928bb9adSStefano 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);
71125eca1a21SEmil Constantinescu 
71135eca1a21SEmil Constantinescu   Level: developer
711493b34091SDebojyoti Ghosh 
7115e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
711693b34091SDebojyoti Ghosh @*/
7117baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
711893b34091SDebojyoti Ghosh {
711993b34091SDebojyoti Ghosh   TS             t;
712093b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7121dc846ba4SSatish Balay   SNES           snes_start;
7122dc846ba4SSatish Balay   DM             dm;
7123dc846ba4SSatish Balay   TSType         type;
712493b34091SDebojyoti Ghosh 
712593b34091SDebojyoti Ghosh   PetscFunctionBegin;
712693b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
712793b34091SDebojyoti Ghosh   *tsout = NULL;
712893b34091SDebojyoti Ghosh 
71297a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
713093b34091SDebojyoti Ghosh 
713193b34091SDebojyoti Ghosh   /* General TS description */
713293b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
713393b34091SDebojyoti Ghosh   t->setupcalled       = 0;
713493b34091SDebojyoti Ghosh   t->ksp_its           = 0;
713593b34091SDebojyoti Ghosh   t->snes_its          = 0;
713693b34091SDebojyoti Ghosh   t->nwork             = 0;
713793b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
713893b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
713993b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
714093b34091SDebojyoti Ghosh 
714134561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
714234561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7143d15a3a53SEmil Constantinescu 
714493b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
714593b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
714693b34091SDebojyoti Ghosh 
714793b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
714851699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
714993b34091SDebojyoti Ghosh 
7150e7069c78SShri   t->trajectory = tsin->trajectory;
7151e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7152e7069c78SShri 
7153e7069c78SShri   t->event = tsin->event;
71546b10a48eSSatish Balay   if (t->event) t->event->refct++;
7155e7069c78SShri 
715693b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
715793b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7158e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
715993b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
716093b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
716193b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
716293b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
716393b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
716493b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
716593b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
716693b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
716793b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
716893b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
716993b34091SDebojyoti Ghosh 
717093b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
717193b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
717293b34091SDebojyoti Ghosh 
717393b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
717493b34091SDebojyoti Ghosh 
717593b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
717693b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
717793b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
717893b34091SDebojyoti Ghosh 
717993b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
718093b34091SDebojyoti Ghosh 
71810d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
71820d4fed19SBarry Smith     PetscInt i;
71830d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
71840d4fed19SBarry Smith     for (i=0; i<10; i++) {
71850d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
71860d4fed19SBarry Smith     }
71870d4fed19SBarry Smith   }
718893b34091SDebojyoti Ghosh   *tsout = t;
718993b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
719093b34091SDebojyoti Ghosh }
7191f3b1f45cSBarry Smith 
7192f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7193f3b1f45cSBarry Smith {
7194f3b1f45cSBarry Smith   PetscErrorCode ierr;
7195f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7196f3b1f45cSBarry Smith 
7197f3b1f45cSBarry Smith   PetscFunctionBegin;
7198f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7199f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7200f3b1f45cSBarry Smith }
7201f3b1f45cSBarry Smith 
7202f3b1f45cSBarry Smith /*@
7203f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7204f3b1f45cSBarry Smith 
7205d083f849SBarry Smith    Logically Collective on TS
7206f3b1f45cSBarry Smith 
7207f3b1f45cSBarry Smith     Input Parameters:
7208f3b1f45cSBarry Smith     TS - the time stepping routine
7209f3b1f45cSBarry Smith 
7210f3b1f45cSBarry Smith    Output Parameter:
7211f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7212f3b1f45cSBarry Smith 
7213f3b1f45cSBarry Smith    Options Database:
7214f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7215f3b1f45cSBarry Smith 
7216f3b1f45cSBarry Smith    Level: advanced
7217f3b1f45cSBarry Smith 
721895452b02SPatrick Sanan    Notes:
721995452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7220f3b1f45cSBarry Smith 
7221f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7222f3b1f45cSBarry Smith @*/
7223f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7224f3b1f45cSBarry Smith {
7225f3b1f45cSBarry Smith   Mat            J,B;
7226f3b1f45cSBarry Smith   PetscErrorCode ierr;
7227f3b1f45cSBarry Smith   TSRHSJacobian  func;
7228f3b1f45cSBarry Smith   void*          ctx;
7229f3b1f45cSBarry Smith 
7230f3b1f45cSBarry Smith   PetscFunctionBegin;
7231f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7232f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7233f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7234f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7235f3b1f45cSBarry Smith }
7236f3b1f45cSBarry Smith 
7237f3b1f45cSBarry Smith /*@C
7238f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7239f3b1f45cSBarry Smith 
7240d083f849SBarry Smith    Logically Collective on TS
7241f3b1f45cSBarry Smith 
7242f3b1f45cSBarry Smith     Input Parameters:
7243f3b1f45cSBarry Smith     TS - the time stepping routine
7244f3b1f45cSBarry Smith 
7245f3b1f45cSBarry Smith    Output Parameter:
7246f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7247f3b1f45cSBarry Smith 
7248f3b1f45cSBarry Smith    Options Database:
7249f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7250f3b1f45cSBarry Smith 
725195452b02SPatrick Sanan    Notes:
725295452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7253f3b1f45cSBarry Smith 
7254f3b1f45cSBarry Smith    Level: advanced
7255f3b1f45cSBarry Smith 
7256f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7257f3b1f45cSBarry Smith @*/
7258f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7259f3b1f45cSBarry Smith {
7260f3b1f45cSBarry Smith   Mat            J,B;
7261f3b1f45cSBarry Smith   PetscErrorCode ierr;
7262f3b1f45cSBarry Smith   void           *ctx;
7263f3b1f45cSBarry Smith   TSRHSJacobian  func;
7264f3b1f45cSBarry Smith 
7265f3b1f45cSBarry Smith   PetscFunctionBegin;
7266f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7267f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7268f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7269f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7270f3b1f45cSBarry Smith }
72710fe4d17eSHong Zhang 
72720fe4d17eSHong Zhang /*@
72730fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
72740fe4d17eSHong Zhang 
72750fe4d17eSHong Zhang   Logically collective
72760fe4d17eSHong Zhang 
72770fe4d17eSHong Zhang   Input Parameter:
72780fe4d17eSHong Zhang +  ts - timestepping context
72790fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
72800fe4d17eSHong Zhang 
72810fe4d17eSHong Zhang   Options Database:
72820fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
72830fe4d17eSHong Zhang 
72840fe4d17eSHong Zhang   Notes:
72850fe4d17eSHong Zhang     This is only useful for multirate methods
72860fe4d17eSHong Zhang 
72870fe4d17eSHong Zhang   Level: intermediate
72880fe4d17eSHong Zhang 
72890fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
72900fe4d17eSHong Zhang @*/
72910fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
72920fe4d17eSHong Zhang {
72930fe4d17eSHong Zhang   PetscFunctionBegin;
72940fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
72950fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
72960fe4d17eSHong Zhang   PetscFunctionReturn(0);
72970fe4d17eSHong Zhang }
72980fe4d17eSHong Zhang 
72990fe4d17eSHong Zhang /*@
73000fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
73010fe4d17eSHong Zhang 
73020fe4d17eSHong Zhang   Not collective
73030fe4d17eSHong Zhang 
73040fe4d17eSHong Zhang   Input Parameter:
73050fe4d17eSHong Zhang .  ts - timestepping context
73060fe4d17eSHong Zhang 
73070fe4d17eSHong Zhang   Output Parameter:
73080fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
73090fe4d17eSHong Zhang 
73100fe4d17eSHong Zhang   Level: intermediate
73110fe4d17eSHong Zhang 
73120fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
73130fe4d17eSHong Zhang @*/
73140fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
73150fe4d17eSHong Zhang {
73160fe4d17eSHong Zhang   PetscFunctionBegin;
73170fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
73180fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
73190fe4d17eSHong Zhang   PetscFunctionReturn(0);
73200fe4d17eSHong Zhang }
7321