xref: /petsc/src/ts/interface/ts.c (revision ffc4695bcb29f4b022f59a5fd6bc99fc280ff6d8)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6900f6b5bSMatthew G. Knepley #include <petscconvest.h>
7d763cef2SBarry Smith 
81c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if (PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;
91c167fc2SEmil Constantinescu 
10d5ba7fb7SMatthew Knepley /* Logging support */
11d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
13d405a339SMatthew Knepley 
14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",NULL};
1549354f04SShri Abhyankar 
161c167fc2SEmil Constantinescu 
17fde5950dSBarry Smith /*@C
18fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
19fde5950dSBarry Smith 
20fde5950dSBarry Smith    Collective on TS
21fde5950dSBarry Smith 
22fde5950dSBarry Smith    Input Parameters:
23fde5950dSBarry Smith +  ts - TS object you wish to monitor
24fde5950dSBarry Smith .  name - the monitor type one is seeking
25fde5950dSBarry Smith .  help - message indicating what monitoring is done
26fde5950dSBarry Smith .  manual - manual page for the monitor
27fde5950dSBarry Smith .  monitor - the monitor function
28fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
29fde5950dSBarry Smith 
30fde5950dSBarry Smith    Level: developer
31fde5950dSBarry Smith 
32fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
33fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
34fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
35fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
36fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
37fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
38fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
39fde5950dSBarry Smith @*/
40721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
41fde5950dSBarry Smith {
42fde5950dSBarry Smith   PetscErrorCode    ierr;
43fde5950dSBarry Smith   PetscViewer       viewer;
44fde5950dSBarry Smith   PetscViewerFormat format;
45fde5950dSBarry Smith   PetscBool         flg;
46fde5950dSBarry Smith 
47fde5950dSBarry Smith   PetscFunctionBegin;
4816413a6aSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
49fde5950dSBarry Smith   if (flg) {
50721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
51721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
52721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
53fde5950dSBarry Smith     if (monitorsetup) {
54721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
55fde5950dSBarry Smith     }
56721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
57fde5950dSBarry Smith   }
58fde5950dSBarry Smith   PetscFunctionReturn(0);
59fde5950dSBarry Smith }
60fde5950dSBarry Smith 
612ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
622ffb9264SLisandro Dalcin {
632ffb9264SLisandro Dalcin   PetscErrorCode ierr;
642ffb9264SLisandro Dalcin 
652ffb9264SLisandro Dalcin   PetscFunctionBegin;
66b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
67b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
682ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
692ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
702ffb9264SLisandro Dalcin   }
712ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
722ffb9264SLisandro Dalcin }
732ffb9264SLisandro Dalcin 
74bdad233fSMatthew Knepley /*@
75bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
76bdad233fSMatthew Knepley 
77bdad233fSMatthew Knepley    Collective on TS
78bdad233fSMatthew Knepley 
79bdad233fSMatthew Knepley    Input Parameter:
80bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
81bdad233fSMatthew Knepley 
82bdad233fSMatthew Knepley    Options Database Keys:
83e49d4f37SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
84ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
85ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
86ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
87ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
884dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
893e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
906c51df0eSBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> - whether to stop at the exact given final time and how to compute the solution at that ti,e
91a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
92a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
93a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
94a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
95a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
96f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
97f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
98ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
99847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
100bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
101de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
102de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1037cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1046934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1058b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
106de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
107de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
108de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
109de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1103e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1113e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
112fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
113e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
1149e336e28SPatrick Sanan -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11553ea634cSHong Zhang 
1163d5a8a6aSBarry Smith    Notes:
1173d5a8a6aSBarry Smith      See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper.
1183d5a8a6aSBarry Smith 
1193d5a8a6aSBarry Smith      Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order
1203d5a8a6aSBarry Smith      to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and
1213d5a8a6aSBarry Smith      -snes_lag_preconditioner_persists true
1223d5a8a6aSBarry Smith 
1239e336e28SPatrick Sanan    Developer Note:
1249e336e28SPatrick Sanan      We should unify all the -ts_monitor options in the way that -xxx_view has been unified
125bdad233fSMatthew Knepley 
126bdad233fSMatthew Knepley    Level: beginner
127bdad233fSMatthew Knepley 
128a313700dSBarry Smith .seealso: TSGetType()
129bdad233fSMatthew Knepley @*/
1307087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
131bdad233fSMatthew Knepley {
132bc952696SBarry Smith   PetscBool              opt,flg,tflg;
133dfbe8321SBarry Smith   PetscErrorCode         ierr;
134eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
13531748224SBarry Smith   PetscReal              time_step;
13649354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
137d1212d36SBarry Smith   char                   dir[16];
138cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1396991f827SBarry Smith   const char             *defaultType;
1406991f827SBarry Smith   char                   typeName[256];
141bdad233fSMatthew Knepley 
142bdad233fSMatthew Knepley   PetscFunctionBegin;
1430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1446991f827SBarry Smith 
1456991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
146cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
147cd11d68dSLisandro Dalcin 
148cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1491ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1501ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1516991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1526991f827SBarry Smith   if (opt) {
1536991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1546991f827SBarry Smith   } else {
1556991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1566991f827SBarry Smith   }
157bdad233fSMatthew Knepley 
158bdad233fSMatthew Knepley   /* Handle generic TS options */
1594dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
160ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
16119eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
16331748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
164cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
16549354f04SShri 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);
16649354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1670298fd71SBarry 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);
1680298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1690298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1700298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1710298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
172bdad233fSMatthew Knepley 
173f3b1f45cSBarry 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);
174f3b1f45cSBarry 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);
1750fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr);
17656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
17756f85f32SBarry Smith   {
17856f85f32SBarry Smith     PetscBool set;
17956f85f32SBarry Smith     flg  = PETSC_FALSE;
18056f85f32SBarry Smith     ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
18156f85f32SBarry Smith     if (set) {
18256f85f32SBarry Smith       ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
18356f85f32SBarry Smith     }
18456f85f32SBarry Smith   }
18556f85f32SBarry Smith #endif
18656f85f32SBarry Smith 
187bdad233fSMatthew Knepley   /* Monitor options */
188cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
189cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
190fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
191fde5950dSBarry Smith 
192589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
1935180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1945180491cSLisandro Dalcin 
1954f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
196b3603a34SBarry Smith   if (opt) {
1973923b477SBarry Smith     PetscInt       howoften = 1;
198e669de00SBarry Smith     DM             dm;
199e669de00SBarry Smith     PetscBool      net;
200b3603a34SBarry Smith 
2010298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
202e669de00SBarry Smith     ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
203e669de00SBarry Smith     ierr = PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net);CHKERRQ(ierr);
204e669de00SBarry Smith     if (net) {
205e669de00SBarry Smith       TSMonitorLGCtxNetwork ctx;
206e669de00SBarry Smith       ierr = TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
207e669de00SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy);CHKERRQ(ierr);
208e669de00SBarry Smith       ierr = PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL);CHKERRQ(ierr);
209e669de00SBarry Smith     } else {
210e669de00SBarry Smith       TSMonitorLGCtx ctx;
211c793f718SLisandro Dalcin       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2124f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
213bdad233fSMatthew Knepley     }
214e669de00SBarry Smith   }
2156ba87a44SLisandro Dalcin 
2164f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
217ef20d060SBarry Smith   if (opt) {
2180b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2193923b477SBarry Smith     PetscInt       howoften = 1;
220ef20d060SBarry Smith 
2210298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
222c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2234f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
224ef20d060SBarry Smith   }
225edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2267cf37e64SBarry Smith 
2276934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2286934998bSLisandro Dalcin   if (opt) {
2296934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2306934998bSLisandro Dalcin     PetscInt       howoften = 1;
2316934998bSLisandro Dalcin 
2326934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2336ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2346934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2356934998bSLisandro Dalcin   }
2368b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2378b668821SLisandro Dalcin   if (opt) {
2388b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2398b668821SLisandro Dalcin     PetscInt       howoften = 1;
2408b668821SLisandro Dalcin 
2418b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2428b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2438b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2448b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2458b668821SLisandro Dalcin   }
2468b668821SLisandro Dalcin 
247201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
248201da799SBarry Smith   if (opt) {
249201da799SBarry Smith     TSMonitorLGCtx ctx;
250201da799SBarry Smith     PetscInt       howoften = 1;
251201da799SBarry Smith 
2520298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2536ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
254201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
255201da799SBarry Smith   }
256201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
257201da799SBarry Smith   if (opt) {
258201da799SBarry Smith     TSMonitorLGCtx ctx;
259201da799SBarry Smith     PetscInt       howoften = 1;
260201da799SBarry Smith 
2610298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2626ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
263201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
264201da799SBarry Smith   }
2658189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2668189c53fSBarry Smith   if (opt) {
2678189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2688189c53fSBarry Smith     PetscInt          howoften = 1;
2698189c53fSBarry Smith 
2700298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
271c793f718SLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2728189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2738189c53fSBarry Smith   }
2740ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2751b575b74SJoseph Pusztay   if (opt) {
2761b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2771b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2780ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2791b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2800ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2811b575b74SJoseph Pusztay   }
282ef20d060SBarry Smith   opt  = PETSC_FALSE;
2830dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
284a7cc72afSBarry Smith   if (opt) {
28583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28683a4ac43SBarry Smith     PetscInt         howoften = 1;
287a80ad3e0SBarry Smith 
2880298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
289c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
29083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
291bdad233fSMatthew Knepley   }
292fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2932d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2942d5ee99bSBarry Smith   if (opt) {
2952d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2962d5ee99bSBarry Smith     PetscReal        bounds[4];
2972d5ee99bSBarry Smith     PetscInt         n = 4;
2982d5ee99bSBarry Smith     PetscDraw        draw;
2996934998bSLisandro Dalcin     PetscDrawAxis    axis;
3002d5ee99bSBarry Smith 
3012d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3022d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
303c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3042d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3056934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3066934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3076934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3082d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3092d5ee99bSBarry Smith   }
3102d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3110dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3123a471f94SBarry Smith   if (opt) {
31383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
31483a4ac43SBarry Smith     PetscInt         howoften = 1;
3153a471f94SBarry Smith 
3160298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
317c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
31883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3193a471f94SBarry Smith   }
3200ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3210ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3220ed3bfb6SBarry Smith   if (opt) {
3230ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3240ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3250ed3bfb6SBarry Smith 
3260ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
327c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3280ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3290ed3bfb6SBarry Smith   }
330fde5950dSBarry Smith 
331ed81e22dSJed Brown   opt  = PETSC_FALSE;
332589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
333ed81e22dSJed Brown   if (flg) {
334ed81e22dSJed Brown     const char *ptr,*ptr2;
335ed81e22dSJed Brown     char       *filetemplate;
336ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
337ed81e22dSJed Brown     /* Do some cursory validation of the input. */
338ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
339ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
340ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
341ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
342ce94432eSBarry 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");
343ed81e22dSJed Brown       if (ptr2) break;
344ed81e22dSJed Brown     }
345ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
346ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
347ed81e22dSJed Brown   }
348bdad233fSMatthew Knepley 
349589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
350d1212d36SBarry Smith   if (flg) {
351d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
352d1212d36SBarry Smith     int                  ray = 0;
3533ee9839eSMatthew G. Knepley     DMDirection          ddir;
354d1212d36SBarry Smith     DM                   da;
355d1212d36SBarry Smith     PetscMPIInt          rank;
356d1212d36SBarry Smith 
357ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3583ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3593ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
360ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
361d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
362d1212d36SBarry Smith 
3635bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr);
364b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
365d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
366d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
367*ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRMPI(ierr);
368d1212d36SBarry Smith     if (!rank) {
369c793f718SLisandro Dalcin       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
370d1212d36SBarry Smith     }
37151b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
372d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
373d1212d36SBarry Smith   }
374589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
37551b4a12fSMatthew G. Knepley   if (flg) {
37651b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37751b4a12fSMatthew G. Knepley     int                 ray = 0;
3783ee9839eSMatthew G. Knepley     DMDirection         ddir;
37951b4a12fSMatthew G. Knepley     DM                  da;
38051b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
381d1212d36SBarry Smith 
38251b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3833ee9839eSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DM_X;
3843ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
38551b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
38651b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3871c3436cfSJed Brown 
3885bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr);
389b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
39051b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
39151b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
392c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
39351b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
39451b4a12fSMatthew G. Knepley   }
395a7a1495cSBarry Smith 
396b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
397b3d3934dSBarry Smith   if (opt) {
398b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
399b3d3934dSBarry Smith 
400b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
401b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
402b3d3934dSBarry Smith   }
403b3d3934dSBarry Smith 
404847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
405847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
406847ff0e1SMatthew G. Knepley   if (flg) {
407847ff0e1SMatthew G. Knepley     DM   dm;
408847ff0e1SMatthew G. Knepley     DMTS tdm;
409847ff0e1SMatthew G. Knepley 
410847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
411847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
412847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
413c793f718SLisandro Dalcin     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL);CHKERRQ(ierr);
414847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
415847ff0e1SMatthew G. Knepley   }
416847ff0e1SMatthew G. Knepley 
417d763cef2SBarry Smith   /* Handle specific TS options */
418d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4196991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
420d763cef2SBarry Smith   }
421fbc52257SHong Zhang 
422a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
423a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
424a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
425a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
426a7bdc993SLisandro Dalcin 
42768bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4284f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
42968bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
43068bece0bSHong Zhang   if (tflg) {
43168bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43268bece0bSHong Zhang   }
433a05bf03eSHong Zhang 
434a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
435d763cef2SBarry Smith 
436d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4370633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
438fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
439d763cef2SBarry Smith 
4404f122a70SLisandro Dalcin   if (ts->trajectory) {
4414f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
442d763cef2SBarry Smith   }
44368bece0bSHong Zhang 
4441ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4454f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4461ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4471ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4481ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4491ef27442SStefano Zampini   }
4504f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
451d763cef2SBarry Smith   PetscFunctionReturn(0);
452d763cef2SBarry Smith }
453d763cef2SBarry Smith 
454d2daff3dSHong Zhang /*@
45578fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
45678fbdcc8SBarry Smith 
45778fbdcc8SBarry Smith    Collective on TS
45878fbdcc8SBarry Smith 
45978fbdcc8SBarry Smith    Input Parameters:
46078fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
46178fbdcc8SBarry Smith 
4627a7aea1fSJed Brown    Output Parameters:
46378fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
46478fbdcc8SBarry Smith 
46578fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
46678fbdcc8SBarry Smith 
46778fbdcc8SBarry Smith    Level: advanced
46878fbdcc8SBarry Smith 
46978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
47078fbdcc8SBarry Smith 
47178fbdcc8SBarry Smith @*/
47278fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
47378fbdcc8SBarry Smith {
47478fbdcc8SBarry Smith   PetscFunctionBegin;
47578fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47678fbdcc8SBarry Smith   *tr = ts->trajectory;
47778fbdcc8SBarry Smith   PetscFunctionReturn(0);
47878fbdcc8SBarry Smith }
47978fbdcc8SBarry Smith 
48078fbdcc8SBarry Smith /*@
481bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
482d2daff3dSHong Zhang 
483d2daff3dSHong Zhang    Collective on TS
484d2daff3dSHong Zhang 
485d2daff3dSHong Zhang    Input Parameters:
486bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
487bc952696SBarry Smith 
48878fbdcc8SBarry Smith    Options Database:
48978fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
49078fbdcc8SBarry Smith -  -ts_trajectory_type type
49178fbdcc8SBarry Smith 
49268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
493d2daff3dSHong Zhang 
494c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
49578fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
49678fbdcc8SBarry Smith 
497d2daff3dSHong Zhang    Level: intermediate
498d2daff3dSHong Zhang 
4992d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
500d2daff3dSHong Zhang 
501d2daff3dSHong Zhang @*/
502bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
503d2daff3dSHong Zhang {
504bc952696SBarry Smith   PetscErrorCode ierr;
505bc952696SBarry Smith 
506d2daff3dSHong Zhang   PetscFunctionBegin;
507d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
508bc952696SBarry Smith   if (!ts->trajectory) {
509bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
510bc952696SBarry Smith   }
511d2daff3dSHong Zhang   PetscFunctionReturn(0);
512d2daff3dSHong Zhang }
513d2daff3dSHong Zhang 
514a7a1495cSBarry Smith /*@
5152d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
5162d29f1f2SStefano Zampini 
5172d29f1f2SStefano Zampini    Collective on TS
5182d29f1f2SStefano Zampini 
5192d29f1f2SStefano Zampini    Input Parameters:
5202d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5212d29f1f2SStefano Zampini 
5222d29f1f2SStefano Zampini    Level: intermediate
5232d29f1f2SStefano Zampini 
5242d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5252d29f1f2SStefano Zampini 
5262d29f1f2SStefano Zampini @*/
5272d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5282d29f1f2SStefano Zampini {
5292d29f1f2SStefano Zampini   PetscErrorCode ierr;
5302d29f1f2SStefano Zampini 
5312d29f1f2SStefano Zampini   PetscFunctionBegin;
5322d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5332d29f1f2SStefano Zampini   if (ts->trajectory) {
5342d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5352d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5362d29f1f2SStefano Zampini   }
5372d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5382d29f1f2SStefano Zampini }
5392d29f1f2SStefano Zampini 
5402d29f1f2SStefano Zampini /*@
541a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
542a7a1495cSBarry Smith       set with TSSetRHSJacobian().
543a7a1495cSBarry Smith 
544d083f849SBarry Smith    Collective on TS
545a7a1495cSBarry Smith 
546a7a1495cSBarry Smith    Input Parameters:
547316643e7SJed Brown +  ts - the TS context
548a7a1495cSBarry Smith .  t - current timestep
5490910c330SBarry Smith -  U - input vector
550a7a1495cSBarry Smith 
551a7a1495cSBarry Smith    Output Parameters:
552a7a1495cSBarry Smith +  A - Jacobian matrix
553a7a1495cSBarry Smith .  B - optional preconditioning matrix
554a7a1495cSBarry Smith -  flag - flag indicating matrix structure
555a7a1495cSBarry Smith 
556a7a1495cSBarry Smith    Notes:
557a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
558a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
559a7a1495cSBarry Smith 
56094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
561a7a1495cSBarry Smith    flag parameter.
562a7a1495cSBarry Smith 
563a7a1495cSBarry Smith    Level: developer
564a7a1495cSBarry Smith 
56594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
566a7a1495cSBarry Smith @*/
567d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
568a7a1495cSBarry Smith {
569dfbe8321SBarry Smith   PetscErrorCode   ierr;
570270bf2e7SJed Brown   PetscObjectState Ustate;
5716c1e1eecSBarry Smith   PetscObjectId    Uid;
57224989b8cSPeter Brune   DM               dm;
573942e3340SBarry Smith   DMTS             tsdm;
57424989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
57524989b8cSPeter Brune   void             *ctx;
576b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
577a7a1495cSBarry Smith 
578a7a1495cSBarry Smith   PetscFunctionBegin;
5790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5800910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5810910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
58224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
583942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
5846374d434SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
5852663174eSHong Zhang   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
58659e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5876c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
588971015bcSStefano Zampini 
5892663174eSHong Zhang   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0);
590d90be118SSean Farley 
591ae692cf2SHong Zhang   if (ts->rhsjacobian.shift && ts->rhsjacobian.reuse) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Should not call TSComputeRHSJacobian() on a shifted matrix (shift=%lf) when RHSJacobian is reusable.",ts->rhsjacobian.shift);
59224989b8cSPeter Brune   if (rhsjacobianfunc) {
59394ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
594a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
595d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
596a7a1495cSBarry Smith     PetscStackPop;
597a6ab3590SBarry Smith     ts->rhsjacs++;
59894ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
599ef66eb69SBarry Smith   } else {
60094ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
601971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
602ef66eb69SBarry Smith   }
6030e4ef248SJed Brown   ts->rhsjacobian.time  = t;
604971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
605971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6067f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
60759e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
608a7a1495cSBarry Smith   PetscFunctionReturn(0);
609a7a1495cSBarry Smith }
610a7a1495cSBarry Smith 
611316643e7SJed Brown /*@
612d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
613d763cef2SBarry Smith 
614d083f849SBarry Smith    Collective on TS
615316643e7SJed Brown 
616316643e7SJed Brown    Input Parameters:
617316643e7SJed Brown +  ts - the TS context
618316643e7SJed Brown .  t - current time
6190910c330SBarry Smith -  U - state vector
620316643e7SJed Brown 
621316643e7SJed Brown    Output Parameter:
622316643e7SJed Brown .  y - right hand side
623316643e7SJed Brown 
624316643e7SJed Brown    Note:
625316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
626316643e7SJed Brown    is used internally within the nonlinear solvers.
627316643e7SJed Brown 
628316643e7SJed Brown    Level: developer
629316643e7SJed Brown 
630316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
631316643e7SJed Brown @*/
6320910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
633d763cef2SBarry Smith {
634dfbe8321SBarry Smith   PetscErrorCode ierr;
63524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
63624989b8cSPeter Brune   TSIFunction    ifunction;
63724989b8cSPeter Brune   void           *ctx;
63824989b8cSPeter Brune   DM             dm;
63924989b8cSPeter Brune 
640d763cef2SBarry Smith   PetscFunctionBegin;
6410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6420910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6430700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
64424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
64524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6460298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
647d763cef2SBarry Smith 
648ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
649d763cef2SBarry Smith 
65024989b8cSPeter Brune   if (rhsfunction) {
651a6ab3590SBarry Smith     ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
6521be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
653d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6540910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
655d763cef2SBarry Smith     PetscStackPop;
6561be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
657a6ab3590SBarry Smith     ts->rhsfuncs++;
658a6ab3590SBarry Smith     ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
659214bc6a2SJed Brown   } else {
660214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
661b2cd27e8SJed Brown   }
662d763cef2SBarry Smith   PetscFunctionReturn(0);
663d763cef2SBarry Smith }
664d763cef2SBarry Smith 
665ef20d060SBarry Smith /*@
666ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
667ef20d060SBarry Smith 
668d083f849SBarry Smith    Collective on TS
669ef20d060SBarry Smith 
670ef20d060SBarry Smith    Input Parameters:
671ef20d060SBarry Smith +  ts - the TS context
672ef20d060SBarry Smith -  t - current time
673ef20d060SBarry Smith 
674ef20d060SBarry Smith    Output Parameter:
6750910c330SBarry Smith .  U - the solution
676ef20d060SBarry Smith 
677ef20d060SBarry Smith    Note:
678ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
679ef20d060SBarry Smith    is used internally within the nonlinear solvers.
680ef20d060SBarry Smith 
681ef20d060SBarry Smith    Level: developer
682ef20d060SBarry Smith 
683abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
684ef20d060SBarry Smith @*/
6850910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
686ef20d060SBarry Smith {
687ef20d060SBarry Smith   PetscErrorCode     ierr;
688ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
689ef20d060SBarry Smith   void               *ctx;
690ef20d060SBarry Smith   DM                 dm;
691ef20d060SBarry Smith 
692ef20d060SBarry Smith   PetscFunctionBegin;
693ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6940910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
695ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
696ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
697ef20d060SBarry Smith 
698ef20d060SBarry Smith   if (solutionfunction) {
6999b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7000910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
701ef20d060SBarry Smith     PetscStackPop;
702ef20d060SBarry Smith   }
703ef20d060SBarry Smith   PetscFunctionReturn(0);
704ef20d060SBarry Smith }
7059b7cd975SBarry Smith /*@
7069b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7079b7cd975SBarry Smith 
708d083f849SBarry Smith    Collective on TS
7099b7cd975SBarry Smith 
7109b7cd975SBarry Smith    Input Parameters:
7119b7cd975SBarry Smith +  ts - the TS context
7129b7cd975SBarry Smith -  t - current time
7139b7cd975SBarry Smith 
7149b7cd975SBarry Smith    Output Parameter:
7159b7cd975SBarry Smith .  U - the function value
7169b7cd975SBarry Smith 
7179b7cd975SBarry Smith    Note:
7189b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7199b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7209b7cd975SBarry Smith 
7219b7cd975SBarry Smith    Level: developer
7229b7cd975SBarry Smith 
7239b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7249b7cd975SBarry Smith @*/
7259b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7269b7cd975SBarry Smith {
7279b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7289b7cd975SBarry Smith   void               *ctx;
7299b7cd975SBarry Smith   DM                 dm;
7309b7cd975SBarry Smith 
7319b7cd975SBarry Smith   PetscFunctionBegin;
7329b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7339b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7349b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7359b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7369b7cd975SBarry Smith 
7379b7cd975SBarry Smith   if (forcing) {
7389b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7399b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7409b7cd975SBarry Smith     PetscStackPop;
7419b7cd975SBarry Smith   }
7429b7cd975SBarry Smith   PetscFunctionReturn(0);
7439b7cd975SBarry Smith }
744ef20d060SBarry Smith 
745214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
746214bc6a2SJed Brown {
7472dd45cf8SJed Brown   Vec            F;
748214bc6a2SJed Brown   PetscErrorCode ierr;
749214bc6a2SJed Brown 
750214bc6a2SJed Brown   PetscFunctionBegin;
7510298fd71SBarry Smith   *Frhs = NULL;
7520298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
753214bc6a2SJed Brown   if (!ts->Frhs) {
7542dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
755214bc6a2SJed Brown   }
756214bc6a2SJed Brown   *Frhs = ts->Frhs;
757214bc6a2SJed Brown   PetscFunctionReturn(0);
758214bc6a2SJed Brown }
759214bc6a2SJed Brown 
760971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
761214bc6a2SJed Brown {
762214bc6a2SJed Brown   Mat            A,B;
7632dd45cf8SJed Brown   PetscErrorCode ierr;
76441a1d4d2SBarry Smith   TSIJacobian    ijacobian;
765214bc6a2SJed Brown 
766214bc6a2SJed Brown   PetscFunctionBegin;
767c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
768c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
76941a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
770214bc6a2SJed Brown   if (Arhs) {
771214bc6a2SJed Brown     if (!ts->Arhs) {
77241a1d4d2SBarry Smith       if (ijacobian) {
773214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
774d60b7d5cSBarry Smith         ierr = TSSetMatStructure(ts,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
77541a1d4d2SBarry Smith       } else {
77641a1d4d2SBarry Smith         ts->Arhs = A;
77741a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
77841a1d4d2SBarry Smith       }
7793565c898SBarry Smith     } else {
7803565c898SBarry Smith       PetscBool flg;
7813565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7823565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7833565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7843565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7853565c898SBarry Smith         ts->Arhs = A;
7863565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7873565c898SBarry Smith       }
788214bc6a2SJed Brown     }
789214bc6a2SJed Brown     *Arhs = ts->Arhs;
790214bc6a2SJed Brown   }
791214bc6a2SJed Brown   if (Brhs) {
792214bc6a2SJed Brown     if (!ts->Brhs) {
793bdb70873SJed Brown       if (A != B) {
79441a1d4d2SBarry Smith         if (ijacobian) {
795214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
796bdb70873SJed Brown         } else {
79741a1d4d2SBarry Smith           ts->Brhs = B;
79841a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
79941a1d4d2SBarry Smith         }
80041a1d4d2SBarry Smith       } else {
801bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
80251699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
803bdb70873SJed Brown       }
804214bc6a2SJed Brown     }
805214bc6a2SJed Brown     *Brhs = ts->Brhs;
806214bc6a2SJed Brown   }
807214bc6a2SJed Brown   PetscFunctionReturn(0);
808214bc6a2SJed Brown }
809214bc6a2SJed Brown 
810316643e7SJed Brown /*@
8110910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
812316643e7SJed Brown 
813d083f849SBarry Smith    Collective on TS
814316643e7SJed Brown 
815316643e7SJed Brown    Input Parameters:
816316643e7SJed Brown +  ts - the TS context
817316643e7SJed Brown .  t - current time
8180910c330SBarry Smith .  U - state vector
8190910c330SBarry Smith .  Udot - time derivative of state vector
820214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
821316643e7SJed Brown 
822316643e7SJed Brown    Output Parameter:
823316643e7SJed Brown .  Y - right hand side
824316643e7SJed Brown 
825316643e7SJed Brown    Note:
826316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
827316643e7SJed Brown    is used internally within the nonlinear solvers.
828316643e7SJed Brown 
829316643e7SJed Brown    If the user did did not write their equations in implicit form, this
830316643e7SJed Brown    function recasts them in implicit form.
831316643e7SJed Brown 
832316643e7SJed Brown    Level: developer
833316643e7SJed Brown 
834316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
835316643e7SJed Brown @*/
8360910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
837316643e7SJed Brown {
838316643e7SJed Brown   PetscErrorCode ierr;
83924989b8cSPeter Brune   TSIFunction    ifunction;
84024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
84124989b8cSPeter Brune   void           *ctx;
84224989b8cSPeter Brune   DM             dm;
843316643e7SJed Brown 
844316643e7SJed Brown   PetscFunctionBegin;
8450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8470910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8480700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
849316643e7SJed Brown 
85024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
85124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8520298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
85324989b8cSPeter Brune 
854ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
855d90be118SSean Farley 
8560910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
85724989b8cSPeter Brune   if (ifunction) {
858316643e7SJed Brown     PetscStackPush("TS user implicit function");
8590910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
860316643e7SJed Brown     PetscStackPop;
861a6ab3590SBarry Smith     ts->ifuncs++;
862214bc6a2SJed Brown   }
863214bc6a2SJed Brown   if (imex) {
86424989b8cSPeter Brune     if (!ifunction) {
8650910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8662dd45cf8SJed Brown     }
86724989b8cSPeter Brune   } else if (rhsfunction) {
86824989b8cSPeter Brune     if (ifunction) {
869214bc6a2SJed Brown       Vec Frhs;
870214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8710910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
872214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8732dd45cf8SJed Brown     } else {
8740910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8750910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
876316643e7SJed Brown     }
8774a6899ffSJed Brown   }
8780910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
879316643e7SJed Brown   PetscFunctionReturn(0);
880316643e7SJed Brown }
881316643e7SJed Brown 
882cfa8a9a2SHong Zhang /*
883cfa8a9a2SHong Zhang    TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it.
884cfa8a9a2SHong Zhang 
885cfa8a9a2SHong Zhang    Note:
886cfa8a9a2SHong Zhang    This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian().
887cfa8a9a2SHong Zhang 
888cfa8a9a2SHong Zhang */
889cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B)
890cfa8a9a2SHong Zhang {
891cfa8a9a2SHong Zhang   PetscErrorCode   ierr;
892cfa8a9a2SHong Zhang 
893cfa8a9a2SHong Zhang   PetscFunctionBegin;
894cfa8a9a2SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
89503c97d52SHong Zhang   if (A != ts->Arhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat");
89603c97d52SHong Zhang   if (B != ts->Brhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat");
897cfa8a9a2SHong Zhang 
898cfa8a9a2SHong Zhang   if (ts->rhsjacobian.shift) {
899cfa8a9a2SHong Zhang     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
900cfa8a9a2SHong Zhang   }
901cfa8a9a2SHong Zhang   if (ts->rhsjacobian.scale == -1.) {
902cfa8a9a2SHong Zhang     ierr = MatScale(A,-1);CHKERRQ(ierr);
903cfa8a9a2SHong Zhang   }
904cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
905cfa8a9a2SHong Zhang     if (ts->rhsjacobian.shift) {
906cfa8a9a2SHong Zhang       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
907cfa8a9a2SHong Zhang     }
908cfa8a9a2SHong Zhang     if (ts->rhsjacobian.scale == -1.) {
909cfa8a9a2SHong Zhang       ierr = MatScale(B,-1);CHKERRQ(ierr);
910cfa8a9a2SHong Zhang     }
911cfa8a9a2SHong Zhang   }
912cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
913cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
914cfa8a9a2SHong Zhang   PetscFunctionReturn(0);
915cfa8a9a2SHong Zhang }
916cfa8a9a2SHong Zhang 
917316643e7SJed Brown /*@
918316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
919316643e7SJed Brown 
920d083f849SBarry Smith    Collective on TS
921316643e7SJed Brown 
922316643e7SJed Brown    Input
923316643e7SJed Brown       Input Parameters:
924316643e7SJed Brown +  ts - the TS context
925316643e7SJed Brown .  t - current timestep
9260910c330SBarry Smith .  U - state vector
9270910c330SBarry Smith .  Udot - time derivative of state vector
928214bc6a2SJed Brown .  shift - shift to apply, see note below
929214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
930316643e7SJed Brown 
931316643e7SJed Brown    Output Parameters:
932316643e7SJed Brown +  A - Jacobian matrix
9333565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
934316643e7SJed Brown 
935316643e7SJed Brown    Notes:
9360910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
937316643e7SJed Brown 
9380910c330SBarry Smith    dF/dU + shift*dF/dUdot
939316643e7SJed Brown 
940316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
941316643e7SJed Brown    is used internally within the nonlinear solvers.
942316643e7SJed Brown 
943316643e7SJed Brown    Level: developer
944316643e7SJed Brown 
945316643e7SJed Brown .seealso:  TSSetIJacobian()
94663495f91SJed Brown @*/
947d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
948316643e7SJed Brown {
949316643e7SJed Brown   PetscErrorCode ierr;
95024989b8cSPeter Brune   TSIJacobian    ijacobian;
95124989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
95224989b8cSPeter Brune   DM             dm;
95324989b8cSPeter Brune   void           *ctx;
954316643e7SJed Brown 
955316643e7SJed Brown   PetscFunctionBegin;
9560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9570910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9580910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
959316643e7SJed Brown   PetscValidPointer(A,6);
96094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
961316643e7SJed Brown   PetscValidPointer(B,7);
96294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
96324989b8cSPeter Brune 
96424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9660298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96724989b8cSPeter Brune 
968ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
969316643e7SJed Brown 
97094ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
97124989b8cSPeter Brune   if (ijacobian) {
972316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
973d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
974a6ab3590SBarry Smith     ts->ijacs++;
975316643e7SJed Brown     PetscStackPop;
9764a6899ffSJed Brown   }
977214bc6a2SJed Brown   if (imex) {
978b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9794c26be97Sstefano_zampini       PetscBool assembled;
980971015bcSStefano Zampini       if (rhsjacobian) {
981971015bcSStefano Zampini         Mat Arhs = NULL;
982971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
983971015bcSStefano Zampini         if (A == Arhs) {
984e8b1e424SHong Zhang           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant"); /* there is no way to reconstruct shift*M-J since J cannot be reevaluated */
985971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
986971015bcSStefano Zampini         }
987971015bcSStefano Zampini       }
98894ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9894c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9904c26be97Sstefano_zampini       if (!assembled) {
9914c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9924c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9934c26be97Sstefano_zampini       }
99494ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99594ab13aaSBarry Smith       if (A != B) {
99694ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9974c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9984c26be97Sstefano_zampini         if (!assembled) {
9994c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10004c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10014c26be97Sstefano_zampini         }
100294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1003214bc6a2SJed Brown       }
1004214bc6a2SJed Brown     }
1005214bc6a2SJed Brown   } else {
1006e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1007e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
1008214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1009e1244c69SJed Brown     }
1010e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
1011e8b1e424SHong Zhang       PetscObjectState Ustate;
1012e8b1e424SHong Zhang       PetscObjectId    Uid;
1013e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
1014e8b1e424SHong Zhang 
1015e8b1e424SHong Zhang       ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1016e8b1e424SHong Zhang       ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
1017e8b1e424SHong Zhang       ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
1018097a96a2SHong Zhang       if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */
1019e8b1e424SHong Zhang         ierr = MatShift(A,shift-ts->rhsjacobian.shift);CHKERRQ(ierr); /* revert the old shift and add the new shift with a single call to MatShift */
102034d322a1SHong Zhang         if (A != B) {
102134d322a1SHong Zhang           ierr = MatShift(B,shift-ts->rhsjacobian.shift);CHKERRQ(ierr);
102234d322a1SHong Zhang         }
1023e8b1e424SHong Zhang       } else {
10243565c898SBarry Smith         PetscBool flg;
1025e8b1e424SHong Zhang 
1026e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1027e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1028e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1029e8b1e424SHong Zhang              and the matrices have not been assembled yet */
1030cfa8a9a2SHong Zhang           ierr = TSRecoverRHSJacobian(ts,A,B);CHKERRQ(ierr);
1031e8b1e424SHong Zhang         }
1032e8b1e424SHong Zhang         ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr);
10333565c898SBarry Smith         ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10343565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10353565c898SBarry Smith         if (!flg) {
103694ab13aaSBarry Smith           ierr = MatScale(A,-1);CHKERRQ(ierr);
103794ab13aaSBarry Smith           ierr = MatShift(A,shift);CHKERRQ(ierr);
10383565c898SBarry Smith         }
103994ab13aaSBarry Smith         if (A != B) {
104094ab13aaSBarry Smith           ierr = MatScale(B,-1);CHKERRQ(ierr);
104194ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1042316643e7SJed Brown         }
1043e8b1e424SHong Zhang       }
1044e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1045e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1046d60b7d5cSBarry Smith     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1047e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
104894ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
104994ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
105094ab13aaSBarry Smith         if (A != B) {
105194ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
105294ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1053214bc6a2SJed Brown         }
1054316643e7SJed Brown       }
1055e8b1e424SHong Zhang       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1056d60b7d5cSBarry Smith       ierr = MatAXPY(A,-1,Arhs,ts->axpy_pattern);CHKERRQ(ierr);
105794ab13aaSBarry Smith       if (A != B) {
1058d60b7d5cSBarry Smith         ierr = MatAXPY(B,-1,Brhs,ts->axpy_pattern);CHKERRQ(ierr);
1059316643e7SJed Brown       }
1060316643e7SJed Brown     }
1061316643e7SJed Brown   }
106294ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1063316643e7SJed Brown   PetscFunctionReturn(0);
1064316643e7SJed Brown }
1065316643e7SJed Brown 
1066d763cef2SBarry Smith /*@C
1067d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1068b5abc632SBarry Smith     where U_t = G(t,u).
1069d763cef2SBarry Smith 
10703f9fe445SBarry Smith     Logically Collective on TS
1071d763cef2SBarry Smith 
1072d763cef2SBarry Smith     Input Parameters:
1073d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10740298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1075d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1076d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10770298fd71SBarry Smith           function evaluation routine (may be NULL)
1078d763cef2SBarry Smith 
1079a96d6ef6SBarry Smith     Calling sequence of f:
1080a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1081d763cef2SBarry Smith 
1082a96d6ef6SBarry Smith +   ts - timestep context
1083a96d6ef6SBarry Smith .   t - current timestep
1084d763cef2SBarry Smith .   u - input vector
1085d763cef2SBarry Smith .   F - function vector
1086d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1087d763cef2SBarry Smith 
1088d763cef2SBarry Smith     Level: beginner
1089d763cef2SBarry Smith 
109095452b02SPatrick Sanan     Notes:
109195452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10922bbac0d3SBarry Smith 
1093ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1094d763cef2SBarry Smith @*/
1095089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1096d763cef2SBarry Smith {
1097089b2837SJed Brown   PetscErrorCode ierr;
1098089b2837SJed Brown   SNES           snes;
10990298fd71SBarry Smith   Vec            ralloc = NULL;
110024989b8cSPeter Brune   DM             dm;
1101d763cef2SBarry Smith 
1102089b2837SJed Brown   PetscFunctionBegin;
11030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1104ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
110524989b8cSPeter Brune 
110624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1108089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1109e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1110e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1111e856ceecSJed Brown     r = ralloc;
1112e856ceecSJed Brown   }
1113089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1114e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1115d763cef2SBarry Smith   PetscFunctionReturn(0);
1116d763cef2SBarry Smith }
1117d763cef2SBarry Smith 
1118ef20d060SBarry Smith /*@C
1119abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1120ef20d060SBarry Smith 
1121ef20d060SBarry Smith     Logically Collective on TS
1122ef20d060SBarry Smith 
1123ef20d060SBarry Smith     Input Parameters:
1124ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1125ef20d060SBarry Smith .   f - routine for evaluating the solution
1126ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11270298fd71SBarry Smith           function evaluation routine (may be NULL)
1128ef20d060SBarry Smith 
1129a96d6ef6SBarry Smith     Calling sequence of f:
1130a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1131ef20d060SBarry Smith 
1132ef20d060SBarry Smith +   t - current timestep
1133ef20d060SBarry Smith .   u - output vector
1134ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1135ef20d060SBarry Smith 
11360ed3bfb6SBarry Smith     Options Database:
11370ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11380ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11390ed3bfb6SBarry Smith 
1140abd5a294SJed Brown     Notes:
1141abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1142abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1143abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1144abd5a294SJed Brown 
11454f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1146abd5a294SJed Brown 
1147ef20d060SBarry Smith     Level: beginner
1148ef20d060SBarry Smith 
11490ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1150ef20d060SBarry Smith @*/
1151ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1152ef20d060SBarry Smith {
1153ef20d060SBarry Smith   PetscErrorCode ierr;
1154ef20d060SBarry Smith   DM             dm;
1155ef20d060SBarry Smith 
1156ef20d060SBarry Smith   PetscFunctionBegin;
1157ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1158ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1159ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1160ef20d060SBarry Smith   PetscFunctionReturn(0);
1161ef20d060SBarry Smith }
1162ef20d060SBarry Smith 
11639b7cd975SBarry Smith /*@C
11649b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11659b7cd975SBarry Smith 
11669b7cd975SBarry Smith     Logically Collective on TS
11679b7cd975SBarry Smith 
11689b7cd975SBarry Smith     Input Parameters:
11699b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1170e162b725SBarry Smith .   func - routine for evaluating the forcing function
11719b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11720298fd71SBarry Smith           function evaluation routine (may be NULL)
11739b7cd975SBarry Smith 
11749b7cd975SBarry Smith     Calling sequence of func:
11756bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11769b7cd975SBarry Smith 
11779b7cd975SBarry Smith +   t - current timestep
1178e162b725SBarry Smith .   f - output vector
11799b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11809b7cd975SBarry Smith 
11819b7cd975SBarry Smith     Notes:
11829b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1183e162b725SBarry 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
1184e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1185e162b725SBarry Smith 
1186e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1187e162b725SBarry Smith 
1188e162b725SBarry 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
1189e162b725SBarry Smith     parameters can be passed in the ctx variable.
11909b7cd975SBarry Smith 
11919b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11929b7cd975SBarry Smith 
11939b7cd975SBarry Smith     Level: beginner
11949b7cd975SBarry Smith 
11959b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11969b7cd975SBarry Smith @*/
1197e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11989b7cd975SBarry Smith {
11999b7cd975SBarry Smith   PetscErrorCode ierr;
12009b7cd975SBarry Smith   DM             dm;
12019b7cd975SBarry Smith 
12029b7cd975SBarry Smith   PetscFunctionBegin;
12039b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12049b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1205e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
12069b7cd975SBarry Smith   PetscFunctionReturn(0);
12079b7cd975SBarry Smith }
12089b7cd975SBarry Smith 
1209d763cef2SBarry Smith /*@C
1210f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1211b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1212d763cef2SBarry Smith 
12133f9fe445SBarry Smith    Logically Collective on TS
1214d763cef2SBarry Smith 
1215d763cef2SBarry Smith    Input Parameters:
1216d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1217e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1218e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1219d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1220d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12210298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1222d763cef2SBarry Smith 
1223f7ab8db6SBarry Smith    Calling sequence of f:
1224a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1225d763cef2SBarry Smith 
1226d763cef2SBarry Smith +  t - current timestep
1227d763cef2SBarry Smith .  u - input vector
1228e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1229e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1230d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1231d763cef2SBarry Smith 
12326cd88445SBarry Smith    Notes:
12336cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12346cd88445SBarry Smith 
12356cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1236ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1237d763cef2SBarry Smith 
1238d763cef2SBarry Smith    Level: beginner
1239d763cef2SBarry Smith 
1240ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1241d763cef2SBarry Smith 
1242d763cef2SBarry Smith @*/
1243e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1244d763cef2SBarry Smith {
1245277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1246089b2837SJed Brown   SNES           snes;
124724989b8cSPeter Brune   DM             dm;
124824989b8cSPeter Brune   TSIJacobian    ijacobian;
1249277b19d0SLisandro Dalcin 
1250d763cef2SBarry Smith   PetscFunctionBegin;
12510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1252e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1253e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1254e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1255e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1256d763cef2SBarry Smith 
125724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
125824989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
12590298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1260089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12615f659677SPeter Brune   if (!ijacobian) {
1262e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12630e4ef248SJed Brown   }
1264e5d3d808SBarry Smith   if (Amat) {
1265e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12660e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1267e5d3d808SBarry Smith     ts->Arhs = Amat;
12680e4ef248SJed Brown   }
1269e5d3d808SBarry Smith   if (Pmat) {
1270e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12710e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1272e5d3d808SBarry Smith     ts->Brhs = Pmat;
12730e4ef248SJed Brown   }
1274d763cef2SBarry Smith   PetscFunctionReturn(0);
1275d763cef2SBarry Smith }
1276d763cef2SBarry Smith 
1277316643e7SJed Brown /*@C
1278b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1279316643e7SJed Brown 
12803f9fe445SBarry Smith    Logically Collective on TS
1281316643e7SJed Brown 
1282316643e7SJed Brown    Input Parameters:
1283316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12840298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1285316643e7SJed Brown .  f   - the function evaluation routine
12860298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1287316643e7SJed Brown 
1288316643e7SJed Brown    Calling sequence of f:
12896bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1290316643e7SJed Brown 
1291316643e7SJed Brown +  t   - time at step/stage being solved
1292316643e7SJed Brown .  u   - state vector
1293316643e7SJed Brown .  u_t - time derivative of state vector
1294316643e7SJed Brown .  F   - function vector
1295316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1296316643e7SJed Brown 
1297316643e7SJed Brown    Important:
12982bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1299316643e7SJed Brown 
1300316643e7SJed Brown    Level: beginner
1301316643e7SJed Brown 
1302d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1303316643e7SJed Brown @*/
130451699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1305316643e7SJed Brown {
1306089b2837SJed Brown   PetscErrorCode ierr;
1307089b2837SJed Brown   SNES           snes;
130851699248SLisandro Dalcin   Vec            ralloc = NULL;
130924989b8cSPeter Brune   DM             dm;
1310316643e7SJed Brown 
1311316643e7SJed Brown   PetscFunctionBegin;
13120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
131351699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
131424989b8cSPeter Brune 
131524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
131624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
131724989b8cSPeter Brune 
1318089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
131951699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
132051699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
132151699248SLisandro Dalcin     r  = ralloc;
1322e856ceecSJed Brown   }
132351699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
132451699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1325089b2837SJed Brown   PetscFunctionReturn(0);
1326089b2837SJed Brown }
1327089b2837SJed Brown 
1328089b2837SJed Brown /*@C
1329089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1330089b2837SJed Brown 
1331089b2837SJed Brown    Not Collective
1332089b2837SJed Brown 
1333089b2837SJed Brown    Input Parameter:
1334089b2837SJed Brown .  ts - the TS context
1335089b2837SJed Brown 
1336089b2837SJed Brown    Output Parameter:
13370298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13380298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13390298fd71SBarry Smith -  ctx - the function context (or NULL)
1340089b2837SJed Brown 
1341089b2837SJed Brown    Level: advanced
1342089b2837SJed Brown 
1343089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1344089b2837SJed Brown @*/
1345089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1346089b2837SJed Brown {
1347089b2837SJed Brown   PetscErrorCode ierr;
1348089b2837SJed Brown   SNES           snes;
134924989b8cSPeter Brune   DM             dm;
1350089b2837SJed Brown 
1351089b2837SJed Brown   PetscFunctionBegin;
1352089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1353089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13540298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
135524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
135624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1357089b2837SJed Brown   PetscFunctionReturn(0);
1358089b2837SJed Brown }
1359089b2837SJed Brown 
1360089b2837SJed Brown /*@C
1361089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1362089b2837SJed Brown 
1363089b2837SJed Brown    Not Collective
1364089b2837SJed Brown 
1365089b2837SJed Brown    Input Parameter:
1366089b2837SJed Brown .  ts - the TS context
1367089b2837SJed Brown 
1368089b2837SJed Brown    Output Parameter:
13690298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13700298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13710298fd71SBarry Smith -  ctx - the function context (or NULL)
1372089b2837SJed Brown 
1373089b2837SJed Brown    Level: advanced
1374089b2837SJed Brown 
13752bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1376089b2837SJed Brown @*/
1377089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1378089b2837SJed Brown {
1379089b2837SJed Brown   PetscErrorCode ierr;
1380089b2837SJed Brown   SNES           snes;
138124989b8cSPeter Brune   DM             dm;
1382089b2837SJed Brown 
1383089b2837SJed Brown   PetscFunctionBegin;
1384089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1385089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13860298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
138724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
138824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1389316643e7SJed Brown   PetscFunctionReturn(0);
1390316643e7SJed Brown }
1391316643e7SJed Brown 
1392316643e7SJed Brown /*@C
1393a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1394ae8867d6SBarry Smith         provided with TSSetIFunction().
1395316643e7SJed Brown 
13963f9fe445SBarry Smith    Logically Collective on TS
1397316643e7SJed Brown 
1398316643e7SJed Brown    Input Parameters:
1399316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1400e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1401e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1402316643e7SJed Brown .  f   - the Jacobian evaluation routine
14030298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1404316643e7SJed Brown 
1405316643e7SJed Brown    Calling sequence of f:
14066bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1407316643e7SJed Brown 
1408316643e7SJed Brown +  t    - time at step/stage being solved
14091b4a444bSJed Brown .  U    - state vector
14101b4a444bSJed Brown .  U_t  - time derivative of state vector
1411316643e7SJed Brown .  a    - shift
1412e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1413e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1414316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1415316643e7SJed Brown 
1416316643e7SJed Brown    Notes:
1417e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1418316643e7SJed Brown 
1419895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1420895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1421895c21f2SBarry Smith 
1422a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1423b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1424a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1425a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1426a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1427a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1428a4f0a591SBarry Smith 
14296cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14306cd88445SBarry Smith 
14316cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1432ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1433ca5f011dSBarry Smith 
1434316643e7SJed Brown    Level: beginner
1435316643e7SJed Brown 
1436ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1437316643e7SJed Brown 
1438316643e7SJed Brown @*/
1439e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1440316643e7SJed Brown {
1441316643e7SJed Brown   PetscErrorCode ierr;
1442089b2837SJed Brown   SNES           snes;
144324989b8cSPeter Brune   DM             dm;
1444316643e7SJed Brown 
1445316643e7SJed Brown   PetscFunctionBegin;
14460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1447e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1448e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1449e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1450e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
145124989b8cSPeter Brune 
145224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
145324989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
145424989b8cSPeter Brune 
1455089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1456e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1457316643e7SJed Brown   PetscFunctionReturn(0);
1458316643e7SJed Brown }
1459316643e7SJed Brown 
1460e1244c69SJed Brown /*@
1461e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1462e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1463e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1464e1244c69SJed Brown    not been changed by the TS.
1465e1244c69SJed Brown 
1466e1244c69SJed Brown    Logically Collective
1467e1244c69SJed Brown 
1468e1244c69SJed Brown    Input Arguments:
1469e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1470e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1471e1244c69SJed Brown 
1472e1244c69SJed Brown    Level: intermediate
1473e1244c69SJed Brown 
1474e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1475e1244c69SJed Brown @*/
1476e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1477e1244c69SJed Brown {
1478e1244c69SJed Brown   PetscFunctionBegin;
1479e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1480e1244c69SJed Brown   PetscFunctionReturn(0);
1481e1244c69SJed Brown }
1482e1244c69SJed Brown 
1483efe9872eSLisandro Dalcin /*@C
1484efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1485efe9872eSLisandro Dalcin 
1486efe9872eSLisandro Dalcin    Logically Collective on TS
1487efe9872eSLisandro Dalcin 
1488efe9872eSLisandro Dalcin    Input Parameters:
1489efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1490efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1491efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1492efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1493efe9872eSLisandro Dalcin 
1494efe9872eSLisandro Dalcin    Calling sequence of fun:
14956bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1496efe9872eSLisandro Dalcin 
1497efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1498efe9872eSLisandro Dalcin .  U    - state vector
1499efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1500efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1501efe9872eSLisandro Dalcin .  F    - function vector
1502efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1503efe9872eSLisandro Dalcin 
1504efe9872eSLisandro Dalcin    Level: beginner
1505efe9872eSLisandro Dalcin 
1506a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1507efe9872eSLisandro Dalcin @*/
1508efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1509efe9872eSLisandro Dalcin {
1510efe9872eSLisandro Dalcin   DM             dm;
1511efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1512efe9872eSLisandro Dalcin 
1513efe9872eSLisandro Dalcin   PetscFunctionBegin;
1514efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1515efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1516efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1517efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1518efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1519efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1520efe9872eSLisandro Dalcin }
1521efe9872eSLisandro Dalcin 
1522efe9872eSLisandro Dalcin /*@C
1523efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1524efe9872eSLisandro Dalcin 
1525efe9872eSLisandro Dalcin   Not Collective
1526efe9872eSLisandro Dalcin 
1527efe9872eSLisandro Dalcin   Input Parameter:
1528efe9872eSLisandro Dalcin . ts - the TS context
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin   Output Parameter:
1531efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1532efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1533efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1534efe9872eSLisandro Dalcin 
1535efe9872eSLisandro Dalcin   Level: advanced
1536efe9872eSLisandro Dalcin 
1537a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1538efe9872eSLisandro Dalcin @*/
1539efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1540efe9872eSLisandro Dalcin {
1541efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1542efe9872eSLisandro Dalcin   SNES           snes;
1543efe9872eSLisandro Dalcin   DM             dm;
1544efe9872eSLisandro Dalcin 
1545efe9872eSLisandro Dalcin   PetscFunctionBegin;
1546efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1547efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1548efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1549efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1550efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1551efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1552efe9872eSLisandro Dalcin }
1553efe9872eSLisandro Dalcin 
1554efe9872eSLisandro Dalcin /*@C
1555bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1556efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1557efe9872eSLisandro Dalcin 
1558efe9872eSLisandro Dalcin    Logically Collective on TS
1559efe9872eSLisandro Dalcin 
1560efe9872eSLisandro Dalcin    Input Parameters:
1561efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1562efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1563efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1564efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1565efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1566efe9872eSLisandro Dalcin 
1567efe9872eSLisandro Dalcin    Calling sequence of jac:
15686bc98fa9SBarry 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);
1569efe9872eSLisandro Dalcin 
1570efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1571efe9872eSLisandro Dalcin .  U    - state vector
1572efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1573efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1574efe9872eSLisandro Dalcin .  v    - shift for U_t
1575efe9872eSLisandro Dalcin .  a    - shift for U_tt
1576efe9872eSLisandro 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
1577efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1578efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin    Notes:
1581efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1582efe9872eSLisandro Dalcin 
1583efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1584efe9872eSLisandro 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.
1585efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1586bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1587efe9872eSLisandro Dalcin 
1588efe9872eSLisandro Dalcin    Level: beginner
1589efe9872eSLisandro Dalcin 
1590a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1591efe9872eSLisandro Dalcin @*/
1592efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1593efe9872eSLisandro Dalcin {
1594efe9872eSLisandro Dalcin   DM             dm;
1595efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin   PetscFunctionBegin;
1598efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1599efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1600efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1601efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1602efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1603efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1604efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1605efe9872eSLisandro Dalcin }
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin /*@C
1608efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1609efe9872eSLisandro Dalcin 
1610efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1611efe9872eSLisandro Dalcin 
1612efe9872eSLisandro Dalcin   Input Parameter:
1613efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1614efe9872eSLisandro Dalcin 
1615efe9872eSLisandro Dalcin   Output Parameters:
1616efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1617efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1618efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1619efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1620efe9872eSLisandro Dalcin 
162195452b02SPatrick Sanan   Notes:
162295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1623efe9872eSLisandro Dalcin 
1624efe9872eSLisandro Dalcin   Level: advanced
1625efe9872eSLisandro Dalcin 
1626a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1627efe9872eSLisandro Dalcin 
1628efe9872eSLisandro Dalcin @*/
1629efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1630efe9872eSLisandro Dalcin {
1631efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1632efe9872eSLisandro Dalcin   SNES           snes;
1633efe9872eSLisandro Dalcin   DM             dm;
1634efe9872eSLisandro Dalcin 
1635efe9872eSLisandro Dalcin   PetscFunctionBegin;
1636efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1637efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1638efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1639efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1640efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1641efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1642efe9872eSLisandro Dalcin }
1643efe9872eSLisandro Dalcin 
1644efe9872eSLisandro Dalcin /*@
1645efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1646efe9872eSLisandro Dalcin 
1647d083f849SBarry Smith   Collective on TS
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin   Input Parameters:
1650efe9872eSLisandro Dalcin + ts - the TS context
1651efe9872eSLisandro Dalcin . t - current time
1652efe9872eSLisandro Dalcin . U - state vector
1653efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1654efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1655efe9872eSLisandro Dalcin 
1656efe9872eSLisandro Dalcin   Output Parameter:
1657efe9872eSLisandro Dalcin . F - the residual vector
1658efe9872eSLisandro Dalcin 
1659efe9872eSLisandro Dalcin   Note:
1660efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1661efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1662efe9872eSLisandro Dalcin 
1663efe9872eSLisandro Dalcin   Level: developer
1664efe9872eSLisandro Dalcin 
1665a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1666efe9872eSLisandro Dalcin @*/
1667efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1668efe9872eSLisandro Dalcin {
1669efe9872eSLisandro Dalcin   DM             dm;
1670efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1671efe9872eSLisandro Dalcin   void           *ctx;
1672efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1673efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1674efe9872eSLisandro Dalcin 
1675efe9872eSLisandro Dalcin   PetscFunctionBegin;
1676efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1677efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1678efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1679efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1680efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1681efe9872eSLisandro Dalcin 
1682efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1683efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1684efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1685efe9872eSLisandro Dalcin 
1686efe9872eSLisandro Dalcin   if (!I2Function) {
1687efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1688efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1689efe9872eSLisandro Dalcin   }
1690efe9872eSLisandro Dalcin 
1691efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1692efe9872eSLisandro Dalcin 
1693efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1694efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1695efe9872eSLisandro Dalcin   PetscStackPop;
1696efe9872eSLisandro Dalcin 
1697efe9872eSLisandro Dalcin   if (rhsfunction) {
1698efe9872eSLisandro Dalcin     Vec Frhs;
1699efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1700efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1701efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1702efe9872eSLisandro Dalcin   }
1703efe9872eSLisandro Dalcin 
1704efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1705efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1706efe9872eSLisandro Dalcin }
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin /*@
1709efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1710efe9872eSLisandro Dalcin 
1711d083f849SBarry Smith   Collective on TS
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   Input Parameters:
1714efe9872eSLisandro Dalcin + ts - the TS context
1715efe9872eSLisandro Dalcin . t - current timestep
1716efe9872eSLisandro Dalcin . U - state vector
1717efe9872eSLisandro Dalcin . V - time derivative of state vector
1718efe9872eSLisandro Dalcin . A - second time derivative of state vector
1719efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1720efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1721efe9872eSLisandro Dalcin 
1722efe9872eSLisandro Dalcin   Output Parameters:
1723efe9872eSLisandro Dalcin + J - Jacobian matrix
1724efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin   Notes:
1727efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1728efe9872eSLisandro Dalcin 
1729efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1730efe9872eSLisandro Dalcin 
1731efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1732efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1733efe9872eSLisandro Dalcin 
1734efe9872eSLisandro Dalcin   Level: developer
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1737efe9872eSLisandro Dalcin @*/
1738efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1739efe9872eSLisandro Dalcin {
1740efe9872eSLisandro Dalcin   DM             dm;
1741efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1742efe9872eSLisandro Dalcin   void           *ctx;
1743efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1744efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1745efe9872eSLisandro Dalcin 
1746efe9872eSLisandro Dalcin   PetscFunctionBegin;
1747efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1748efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1749efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1750efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1751efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1752efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1753efe9872eSLisandro Dalcin 
1754efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1755efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1756efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1757efe9872eSLisandro Dalcin 
1758efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1759efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1760efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1761efe9872eSLisandro Dalcin   }
1762efe9872eSLisandro Dalcin 
1763efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1764efe9872eSLisandro Dalcin 
1765efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1766efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1767efe9872eSLisandro Dalcin   PetscStackPop;
1768efe9872eSLisandro Dalcin 
1769efe9872eSLisandro Dalcin   if (rhsjacobian) {
1770d60b7d5cSBarry Smith     Mat Jrhs,Prhs;
1771efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1772efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1773d60b7d5cSBarry Smith     ierr = MatAXPY(J,-1,Jrhs,ts->axpy_pattern);CHKERRQ(ierr);
1774d60b7d5cSBarry Smith     if (P != J) {ierr = MatAXPY(P,-1,Prhs,ts->axpy_pattern);CHKERRQ(ierr);}
1775efe9872eSLisandro Dalcin   }
1776efe9872eSLisandro Dalcin 
1777efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1778efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1779efe9872eSLisandro Dalcin }
1780efe9872eSLisandro Dalcin 
1781438f35afSJed Brown /*@C
1782438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1783438f35afSJed Brown 
1784438f35afSJed Brown    Logically Collective
1785438f35afSJed Brown 
1786438f35afSJed Brown    Input Arguments:
1787438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1788a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1789438f35afSJed Brown -  ctx - a context for tvar
1790438f35afSJed Brown 
1791a96d6ef6SBarry Smith     Calling sequence of tvar:
1792a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1793a96d6ef6SBarry Smith 
1794a96d6ef6SBarry Smith +   ts - timestep context
1795a96d6ef6SBarry Smith .   p - input vector (primative form)
1796a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1797a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1798a96d6ef6SBarry Smith 
1799438f35afSJed Brown    Level: advanced
1800438f35afSJed Brown 
1801438f35afSJed Brown    Notes:
1802438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1803438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1804438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1805438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1806438f35afSJed Brown    evaluated via the chain rule, as in
1807438f35afSJed Brown 
1808438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1809438f35afSJed Brown 
1810438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1811438f35afSJed Brown @*/
1812438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1813438f35afSJed Brown {
1814438f35afSJed Brown   PetscErrorCode ierr;
1815438f35afSJed Brown   DM             dm;
1816438f35afSJed Brown 
1817438f35afSJed Brown   PetscFunctionBegin;
1818438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1819438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1820438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1821438f35afSJed Brown   PetscFunctionReturn(0);
1822438f35afSJed Brown }
1823438f35afSJed Brown 
1824efe9872eSLisandro Dalcin /*@
1825e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1826e3c11fc1SJed Brown 
1827e3c11fc1SJed Brown    Logically Collective
1828e3c11fc1SJed Brown 
1829e3c11fc1SJed Brown    Input Parameters:
1830e3c11fc1SJed Brown +  ts - TS on which to compute
1831e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1832e3c11fc1SJed Brown 
1833e3c11fc1SJed Brown    Output Parameters:
1834e3c11fc1SJed Brown .  C - transient (conservative) variable
1835e3c11fc1SJed Brown 
1836e3c11fc1SJed Brown    Developer Notes:
1837e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1838e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1839e3c11fc1SJed Brown    being used.
1840e3c11fc1SJed Brown 
1841e3c11fc1SJed Brown    Level: developer
1842e3c11fc1SJed Brown 
1843e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1844e3c11fc1SJed Brown @*/
1845e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1846e3c11fc1SJed Brown {
1847e3c11fc1SJed Brown   PetscErrorCode ierr;
1848e3c11fc1SJed Brown   DM             dm;
1849e3c11fc1SJed Brown   DMTS           dmts;
1850e3c11fc1SJed Brown 
1851e3c11fc1SJed Brown   PetscFunctionBegin;
1852e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1853e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1854e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1855e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1856e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1857e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1858e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1859e3c11fc1SJed Brown   }
1860e3c11fc1SJed Brown   PetscFunctionReturn(0);
1861e3c11fc1SJed Brown }
1862e3c11fc1SJed Brown 
1863e3c11fc1SJed Brown /*@
1864e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1865e3c11fc1SJed Brown 
1866e3c11fc1SJed Brown    Logically Collective
1867e3c11fc1SJed Brown 
1868e3c11fc1SJed Brown    Input Parameters:
1869e3c11fc1SJed Brown .  ts - TS on which to compute
1870e3c11fc1SJed Brown 
1871e3c11fc1SJed Brown    Output Parameters:
1872e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1873e3c11fc1SJed Brown 
1874e3c11fc1SJed Brown    Level: developer
1875e3c11fc1SJed Brown 
1876e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1877e3c11fc1SJed Brown @*/
1878e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1879e3c11fc1SJed Brown {
1880e3c11fc1SJed Brown   PetscErrorCode ierr;
1881e3c11fc1SJed Brown   DM             dm;
1882e3c11fc1SJed Brown   DMTS           dmts;
1883e3c11fc1SJed Brown 
1884e3c11fc1SJed Brown   PetscFunctionBegin;
1885e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1886e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1887e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1888e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1889e3c11fc1SJed Brown   PetscFunctionReturn(0);
1890e3c11fc1SJed Brown }
1891e3c11fc1SJed Brown 
1892e3c11fc1SJed Brown /*@
1893efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1894efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1895efe9872eSLisandro Dalcin 
1896d083f849SBarry Smith    Logically Collective on TS
1897efe9872eSLisandro Dalcin 
1898efe9872eSLisandro Dalcin    Input Parameters:
1899efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1900efe9872eSLisandro Dalcin .  u - the solution vector
1901efe9872eSLisandro Dalcin -  v - the time derivative vector
1902efe9872eSLisandro Dalcin 
1903efe9872eSLisandro Dalcin    Level: beginner
1904efe9872eSLisandro Dalcin 
1905efe9872eSLisandro Dalcin @*/
1906efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1907efe9872eSLisandro Dalcin {
1908efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1909efe9872eSLisandro Dalcin 
1910efe9872eSLisandro Dalcin   PetscFunctionBegin;
1911efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1912efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1913efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1914efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1915efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1916efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1917efe9872eSLisandro Dalcin   ts->vec_dot = v;
1918efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1919efe9872eSLisandro Dalcin }
1920efe9872eSLisandro Dalcin 
1921efe9872eSLisandro Dalcin /*@
1922efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1923efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1924efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1925efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1926efe9872eSLisandro Dalcin 
1927efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1928efe9872eSLisandro Dalcin 
1929efe9872eSLisandro Dalcin    Input Parameter:
1930efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1931efe9872eSLisandro Dalcin 
1932efe9872eSLisandro Dalcin    Output Parameter:
1933efe9872eSLisandro Dalcin +  u - the vector containing the solution
1934efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1935efe9872eSLisandro Dalcin 
1936efe9872eSLisandro Dalcin    Level: intermediate
1937efe9872eSLisandro Dalcin 
1938efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1939efe9872eSLisandro Dalcin 
1940efe9872eSLisandro Dalcin @*/
1941efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1942efe9872eSLisandro Dalcin {
1943efe9872eSLisandro Dalcin   PetscFunctionBegin;
1944efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1945efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1946efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1947efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1948efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1949efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1950efe9872eSLisandro Dalcin }
1951efe9872eSLisandro Dalcin 
195255849f57SBarry Smith /*@C
195355849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
195455849f57SBarry Smith 
195555849f57SBarry Smith   Collective on PetscViewer
195655849f57SBarry Smith 
195755849f57SBarry Smith   Input Parameters:
195855849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
195955849f57SBarry Smith            some related function before a call to TSLoad().
196055849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
196155849f57SBarry Smith 
196255849f57SBarry Smith    Level: intermediate
196355849f57SBarry Smith 
196455849f57SBarry Smith   Notes:
196555849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
196655849f57SBarry Smith 
196755849f57SBarry Smith   Notes for advanced users:
196855849f57SBarry Smith   Most users should not need to know the details of the binary storage
196955849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
197055849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
197155849f57SBarry Smith   format is
197255849f57SBarry Smith .vb
197355849f57SBarry Smith      has not yet been determined
197455849f57SBarry Smith .ve
197555849f57SBarry Smith 
197655849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
197755849f57SBarry Smith @*/
1978f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
197955849f57SBarry Smith {
198055849f57SBarry Smith   PetscErrorCode ierr;
198155849f57SBarry Smith   PetscBool      isbinary;
198255849f57SBarry Smith   PetscInt       classid;
198355849f57SBarry Smith   char           type[256];
19842d53ad75SBarry Smith   DMTS           sdm;
1985ad6bc421SBarry Smith   DM             dm;
198655849f57SBarry Smith 
198755849f57SBarry Smith   PetscFunctionBegin;
1988f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
198955849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
199055849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
199155849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
199255849f57SBarry Smith 
1993060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1994ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1995060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1996f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1997f2c2a1b9SBarry Smith   if (ts->ops->load) {
1998f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1999f2c2a1b9SBarry Smith   }
2000ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
2001ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
2002ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
2003f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
2004f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
20052d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20062d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
200755849f57SBarry Smith   PetscFunctionReturn(0);
200855849f57SBarry Smith }
200955849f57SBarry Smith 
20109804daf3SBarry Smith #include <petscdraw.h>
2011e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2012e04113cfSBarry Smith #include <petscviewersaws.h>
2013f05ece33SBarry Smith #endif
2014fe2efc57SMark 
2015fe2efc57SMark /*@C
2016fe2efc57SMark    TSViewFromOptions - View from Options
2017fe2efc57SMark 
2018fe2efc57SMark    Collective on TS
2019fe2efc57SMark 
2020fe2efc57SMark    Input Parameters:
2021fe2efc57SMark +  A - the application ordering context
2022736c3998SJose E. Roman .  obj - Optional object
2023736c3998SJose E. Roman -  name - command line option
2024fe2efc57SMark 
2025fe2efc57SMark    Level: intermediate
2026fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
2027fe2efc57SMark @*/
2028fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
2029fe2efc57SMark {
2030fe2efc57SMark   PetscErrorCode ierr;
2031fe2efc57SMark 
2032fe2efc57SMark   PetscFunctionBegin;
2033fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
2034fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
2035fe2efc57SMark   PetscFunctionReturn(0);
2036fe2efc57SMark }
2037fe2efc57SMark 
20387e2c5f70SBarry Smith /*@C
2039d763cef2SBarry Smith     TSView - Prints the TS data structure.
2040d763cef2SBarry Smith 
20414c49b128SBarry Smith     Collective on TS
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith     Input Parameters:
2044d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2045d763cef2SBarry Smith -   viewer - visualization context
2046d763cef2SBarry Smith 
2047d763cef2SBarry Smith     Options Database Key:
2048d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2049d763cef2SBarry Smith 
2050d763cef2SBarry Smith     Notes:
2051d763cef2SBarry Smith     The available visualization contexts include
2052b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2053b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2054d763cef2SBarry Smith          output where only the first processor opens
2055d763cef2SBarry Smith          the file.  All other processors send their
2056d763cef2SBarry Smith          data to the first processor to print.
2057d763cef2SBarry Smith 
2058d763cef2SBarry Smith     The user can open an alternative visualization context with
2059b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2060d763cef2SBarry Smith 
2061d763cef2SBarry Smith     Level: beginner
2062d763cef2SBarry Smith 
2063b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2064d763cef2SBarry Smith @*/
20657087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2066d763cef2SBarry Smith {
2067dfbe8321SBarry Smith   PetscErrorCode ierr;
206819fd82e9SBarry Smith   TSType         type;
20692b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20702d53ad75SBarry Smith   DMTS           sdm;
2071e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2072536b137fSBarry Smith   PetscBool      issaws;
2073f05ece33SBarry Smith #endif
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith   PetscFunctionBegin;
20760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20773050cee2SBarry Smith   if (!viewer) {
2078ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20793050cee2SBarry Smith   }
20800700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2081c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2082fd16b177SBarry Smith 
2083251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2084251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
208555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20862b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2087e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2088536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2089f05ece33SBarry Smith #endif
209032077d6dSBarry Smith   if (iascii) {
2091dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2092efd4aadfSBarry Smith     if (ts->ops->view) {
2093efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2094efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2095efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2096efd4aadfSBarry Smith     }
2097ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
209877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2099ef85077eSLisandro Dalcin     }
2100ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
21017c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2102ef85077eSLisandro Dalcin     }
2103a6ab3590SBarry Smith     if (ts->ifuncs) {
2104a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of I function evaluations=%D\n",ts->ifuncs);CHKERRQ(ierr);
2105a6ab3590SBarry Smith     }
2106a6ab3590SBarry Smith     if (ts->ijacs) {
2107a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of I Jacobian evaluations=%D\n",ts->ijacs);CHKERRQ(ierr);
2108a6ab3590SBarry Smith     }
2109a6ab3590SBarry Smith     if (ts->rhsfuncs) {
2110a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of RHS function evaluations=%D\n",ts->rhsfuncs);CHKERRQ(ierr);
2111a6ab3590SBarry Smith     }
2112a6ab3590SBarry Smith     if (ts->rhsjacs) {
2113a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of RHS Jacobian evaluations=%D\n",ts->rhsjacs);CHKERRQ(ierr);
2114a6ab3590SBarry Smith     }
2115efd4aadfSBarry Smith     if (ts->usessnes) {
2116efd4aadfSBarry Smith       PetscBool lin;
2117d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
21185ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2119d763cef2SBarry Smith       }
21205ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
21211ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2122efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2123efd4aadfSBarry Smith     }
2124193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2125a0af407cSBarry Smith     if (ts->vrtol) {
2126a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2127a0af407cSBarry Smith     } else {
2128a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2129a0af407cSBarry Smith     }
2130a0af407cSBarry Smith     if (ts->vatol) {
2131a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2132a0af407cSBarry Smith     } else {
2133a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2134a0af407cSBarry Smith     }
2135825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2136efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2137825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21380f5bd95cSBarry Smith   } else if (isstring) {
2139a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
214036a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
214136a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
214255849f57SBarry Smith   } else if (isbinary) {
214355849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
214455849f57SBarry Smith     MPI_Comm    comm;
214555849f57SBarry Smith     PetscMPIInt rank;
214655849f57SBarry Smith     char        type[256];
214755849f57SBarry Smith 
214855849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
2149*ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
215055849f57SBarry Smith     if (!rank) {
2151f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
215255849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2153f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
215455849f57SBarry Smith     }
215555849f57SBarry Smith     if (ts->ops->view) {
215655849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
215755849f57SBarry Smith     }
2158efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2159f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2160f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21612d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21622d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21632b0a91c0SBarry Smith   } else if (isdraw) {
21642b0a91c0SBarry Smith     PetscDraw draw;
21652b0a91c0SBarry Smith     char      str[36];
216689fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21672b0a91c0SBarry Smith 
21682b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21692b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21702b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21712b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
217251fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
217389fd9fafSBarry Smith     bottom = y - h;
21742b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21752b0a91c0SBarry Smith     if (ts->ops->view) {
21762b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21772b0a91c0SBarry Smith     }
2178efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2179efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21802b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2181e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2182536b137fSBarry Smith   } else if (issaws) {
2183d45a07a7SBarry Smith     PetscMPIInt rank;
21842657e9d9SBarry Smith     const char  *name;
21852657e9d9SBarry Smith 
21862657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2187*ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
2188d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2189d45a07a7SBarry Smith       char       dir[1024];
2190d45a07a7SBarry Smith 
2191e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2192a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21932657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21942657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21952657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2196d763cef2SBarry Smith     }
21970acecf5bSBarry Smith     if (ts->ops->view) {
21980acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21990acecf5bSBarry Smith     }
2200f05ece33SBarry Smith #endif
2201f05ece33SBarry Smith   }
220236a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
220336a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
220436a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
220536a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
220636a9e3b9SBarry Smith   }
220736a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
220836a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2209f05ece33SBarry Smith 
2210b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2211251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2212b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2213d763cef2SBarry Smith   PetscFunctionReturn(0);
2214d763cef2SBarry Smith }
2215d763cef2SBarry Smith 
2216b07ff414SBarry Smith /*@
2217d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2218d763cef2SBarry Smith    the timesteppers.
2219d763cef2SBarry Smith 
22203f9fe445SBarry Smith    Logically Collective on TS
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Input Parameters:
2223d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2224d763cef2SBarry Smith -  usrP - optional user context
2225d763cef2SBarry Smith 
222695452b02SPatrick Sanan    Fortran Notes:
222795452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2228daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2229daf670e6SBarry Smith 
2230d763cef2SBarry Smith    Level: intermediate
2231d763cef2SBarry Smith 
2232d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2233d763cef2SBarry Smith @*/
22347087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2235d763cef2SBarry Smith {
2236d763cef2SBarry Smith   PetscFunctionBegin;
22370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2238d763cef2SBarry Smith   ts->user = usrP;
2239d763cef2SBarry Smith   PetscFunctionReturn(0);
2240d763cef2SBarry Smith }
2241d763cef2SBarry Smith 
2242b07ff414SBarry Smith /*@
2243d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2244d763cef2SBarry Smith     timestepper.
2245d763cef2SBarry Smith 
2246d763cef2SBarry Smith     Not Collective
2247d763cef2SBarry Smith 
2248d763cef2SBarry Smith     Input Parameter:
2249d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2250d763cef2SBarry Smith 
2251d763cef2SBarry Smith     Output Parameter:
2252d763cef2SBarry Smith .   usrP - user context
2253d763cef2SBarry Smith 
225495452b02SPatrick Sanan    Fortran Notes:
225595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2256daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2257daf670e6SBarry Smith 
2258d763cef2SBarry Smith     Level: intermediate
2259d763cef2SBarry Smith 
2260d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2261d763cef2SBarry Smith @*/
2262e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2263d763cef2SBarry Smith {
2264d763cef2SBarry Smith   PetscFunctionBegin;
22650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2266e71120c6SJed Brown   *(void**)usrP = ts->user;
2267d763cef2SBarry Smith   PetscFunctionReturn(0);
2268d763cef2SBarry Smith }
2269d763cef2SBarry Smith 
2270d763cef2SBarry Smith /*@
227180275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2272d763cef2SBarry Smith 
2273d763cef2SBarry Smith    Not Collective
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith    Input Parameter:
2276d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Output Parameter:
227980275a0aSLisandro Dalcin .  steps - number of steps completed so far
2280d763cef2SBarry Smith 
2281d763cef2SBarry Smith    Level: intermediate
2282d763cef2SBarry Smith 
22839be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2284d763cef2SBarry Smith @*/
228580275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2286d763cef2SBarry Smith {
2287d763cef2SBarry Smith   PetscFunctionBegin;
22880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
228980275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
229080275a0aSLisandro Dalcin   *steps = ts->steps;
229180275a0aSLisandro Dalcin   PetscFunctionReturn(0);
229280275a0aSLisandro Dalcin }
229380275a0aSLisandro Dalcin 
229480275a0aSLisandro Dalcin /*@
229580275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
229680275a0aSLisandro Dalcin 
229780275a0aSLisandro Dalcin    Logically Collective on TS
229880275a0aSLisandro Dalcin 
229980275a0aSLisandro Dalcin    Input Parameters:
230080275a0aSLisandro Dalcin +  ts - the TS context
230180275a0aSLisandro Dalcin -  steps - number of steps completed so far
230280275a0aSLisandro Dalcin 
230380275a0aSLisandro Dalcin    Notes:
230480275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
230580275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
230680275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
230780275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
230880275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
230980275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
231080275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
231180275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
231280275a0aSLisandro Dalcin 
231380275a0aSLisandro Dalcin    Level: advanced
231480275a0aSLisandro Dalcin 
231580275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
231680275a0aSLisandro Dalcin @*/
231780275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
231880275a0aSLisandro Dalcin {
231980275a0aSLisandro Dalcin   PetscFunctionBegin;
232080275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
232180275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
232280275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
232380275a0aSLisandro Dalcin   ts->steps = steps;
2324d763cef2SBarry Smith   PetscFunctionReturn(0);
2325d763cef2SBarry Smith }
2326d763cef2SBarry Smith 
2327d763cef2SBarry Smith /*@
2328d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2329d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2330d763cef2SBarry Smith 
23313f9fe445SBarry Smith    Logically Collective on TS
2332d763cef2SBarry Smith 
2333d763cef2SBarry Smith    Input Parameters:
2334d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2335d763cef2SBarry Smith -  time_step - the size of the timestep
2336d763cef2SBarry Smith 
2337d763cef2SBarry Smith    Level: intermediate
2338d763cef2SBarry Smith 
2339aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2340d763cef2SBarry Smith 
2341d763cef2SBarry Smith @*/
23427087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2343d763cef2SBarry Smith {
2344d763cef2SBarry Smith   PetscFunctionBegin;
23450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2346c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2347d763cef2SBarry Smith   ts->time_step = time_step;
2348d763cef2SBarry Smith   PetscFunctionReturn(0);
2349d763cef2SBarry Smith }
2350d763cef2SBarry Smith 
2351a43b19c4SJed Brown /*@
235249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
235349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
235449354f04SShri Abhyankar      or just return at the final time TS computed.
2355a43b19c4SJed Brown 
2356a43b19c4SJed Brown   Logically Collective on TS
2357a43b19c4SJed Brown 
2358a43b19c4SJed Brown    Input Parameter:
2359a43b19c4SJed Brown +   ts - the time-step context
236049354f04SShri Abhyankar -   eftopt - exact final time option
2361a43b19c4SJed Brown 
2362feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2363feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2364feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2365feed9e9dSBarry Smith 
2366feed9e9dSBarry Smith    Options Database:
2367feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2368feed9e9dSBarry Smith 
2369ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2370ee346746SBarry Smith     then the final time you selected.
2371ee346746SBarry Smith 
2372a43b19c4SJed Brown    Level: beginner
2373a43b19c4SJed Brown 
2374f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2375a43b19c4SJed Brown @*/
237649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2377a43b19c4SJed Brown {
2378a43b19c4SJed Brown   PetscFunctionBegin;
2379a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
238049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
238149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2382a43b19c4SJed Brown   PetscFunctionReturn(0);
2383a43b19c4SJed Brown }
2384a43b19c4SJed Brown 
2385d763cef2SBarry Smith /*@
2386f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2387f6953c82SLisandro Dalcin 
2388f6953c82SLisandro Dalcin    Not Collective
2389f6953c82SLisandro Dalcin 
2390f6953c82SLisandro Dalcin    Input Parameter:
2391f6953c82SLisandro Dalcin .  ts - the TS context
2392f6953c82SLisandro Dalcin 
2393f6953c82SLisandro Dalcin    Output Parameter:
2394f6953c82SLisandro Dalcin .  eftopt - exact final time option
2395f6953c82SLisandro Dalcin 
2396f6953c82SLisandro Dalcin    Level: beginner
2397f6953c82SLisandro Dalcin 
2398f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2399f6953c82SLisandro Dalcin @*/
2400f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2401f6953c82SLisandro Dalcin {
2402f6953c82SLisandro Dalcin   PetscFunctionBegin;
2403f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2404f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2405f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2406f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2407f6953c82SLisandro Dalcin }
2408f6953c82SLisandro Dalcin 
2409f6953c82SLisandro Dalcin /*@
2410d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2411d763cef2SBarry Smith 
2412d763cef2SBarry Smith    Not Collective
2413d763cef2SBarry Smith 
2414d763cef2SBarry Smith    Input Parameter:
2415d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2416d763cef2SBarry Smith 
2417d763cef2SBarry Smith    Output Parameter:
2418d763cef2SBarry Smith .  dt - the current timestep size
2419d763cef2SBarry Smith 
2420d763cef2SBarry Smith    Level: intermediate
2421d763cef2SBarry Smith 
2422aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2423d763cef2SBarry Smith 
2424d763cef2SBarry Smith @*/
24257087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2426d763cef2SBarry Smith {
2427d763cef2SBarry Smith   PetscFunctionBegin;
24280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2429f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2430d763cef2SBarry Smith   *dt = ts->time_step;
2431d763cef2SBarry Smith   PetscFunctionReturn(0);
2432d763cef2SBarry Smith }
2433d763cef2SBarry Smith 
2434d8e5e3e6SSatish Balay /*@
2435d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2436d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2437d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2438d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2439d763cef2SBarry Smith 
2440d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2441d763cef2SBarry Smith 
2442d763cef2SBarry Smith    Input Parameter:
2443d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2444d763cef2SBarry Smith 
2445d763cef2SBarry Smith    Output Parameter:
2446d763cef2SBarry Smith .  v - the vector containing the solution
2447d763cef2SBarry Smith 
244863e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
244963e21af5SBarry Smith    final time. It returns the solution at the next timestep.
245063e21af5SBarry Smith 
2451d763cef2SBarry Smith    Level: intermediate
2452d763cef2SBarry Smith 
24530ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2454d763cef2SBarry Smith 
2455d763cef2SBarry Smith @*/
24567087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2457d763cef2SBarry Smith {
2458d763cef2SBarry Smith   PetscFunctionBegin;
24590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24604482741eSBarry Smith   PetscValidPointer(v,2);
24618737fe31SLisandro Dalcin   *v = ts->vec_sol;
2462d763cef2SBarry Smith   PetscFunctionReturn(0);
2463d763cef2SBarry Smith }
2464d763cef2SBarry Smith 
246503fe5f5eSDebojyoti Ghosh /*@
2466b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
246703fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2468b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
246903fe5f5eSDebojyoti Ghosh    structure as the solution vector.
247003fe5f5eSDebojyoti Ghosh 
247103fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
247203fe5f5eSDebojyoti Ghosh 
247303fe5f5eSDebojyoti Ghosh    Parameters :
2474a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2475b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2476b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
247703fe5f5eSDebojyoti Ghosh        returned in v.
2478a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
247903fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2480b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
248103fe5f5eSDebojyoti Ghosh 
24824cdd57e5SDebojyoti Ghosh    Level: advanced
248303fe5f5eSDebojyoti Ghosh 
248403fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
248503fe5f5eSDebojyoti Ghosh 
248603fe5f5eSDebojyoti Ghosh @*/
2487b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
248803fe5f5eSDebojyoti Ghosh {
248903fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
249003fe5f5eSDebojyoti Ghosh 
249103fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
249203fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2493b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
249403fe5f5eSDebojyoti Ghosh   else {
2495b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
249603fe5f5eSDebojyoti Ghosh   }
249703fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
249803fe5f5eSDebojyoti Ghosh }
249903fe5f5eSDebojyoti Ghosh 
25004cdd57e5SDebojyoti Ghosh /*@
25014cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
25024cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
25034cdd57e5SDebojyoti Ghosh 
25044cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25054cdd57e5SDebojyoti Ghosh 
25064cdd57e5SDebojyoti Ghosh    Parameters :
2507a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2508a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
25094cdd57e5SDebojyoti Ghosh 
25104cdd57e5SDebojyoti Ghosh    Level: intermediate
25114cdd57e5SDebojyoti Ghosh 
25124cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
25134cdd57e5SDebojyoti Ghosh 
25144cdd57e5SDebojyoti Ghosh @*/
25154cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
25164cdd57e5SDebojyoti Ghosh {
25174cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25184cdd57e5SDebojyoti Ghosh 
25194cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25204cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2521f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
25224cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2523f6356ec7SDebojyoti Ghosh   } else {
2524f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2525f6356ec7SDebojyoti Ghosh   }
25264cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25274cdd57e5SDebojyoti Ghosh }
25284cdd57e5SDebojyoti Ghosh 
25294cdd57e5SDebojyoti Ghosh /*@
25304cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
25314cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
25324cdd57e5SDebojyoti Ghosh 
25334cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25344cdd57e5SDebojyoti Ghosh 
25359657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
25369657682dSDebojyoti Ghosh 
25374cdd57e5SDebojyoti Ghosh    Parameters :
2538a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2539657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2540a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
25414cdd57e5SDebojyoti Ghosh 
25424cdd57e5SDebojyoti Ghosh    Level: intermediate
25434cdd57e5SDebojyoti Ghosh 
254457df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25454cdd57e5SDebojyoti Ghosh 
25464cdd57e5SDebojyoti Ghosh @*/
25470a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25484cdd57e5SDebojyoti Ghosh {
25494cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25504cdd57e5SDebojyoti Ghosh 
25514cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25524cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2553f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25540a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2555f6356ec7SDebojyoti Ghosh   } else {
2556f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2557f6356ec7SDebojyoti Ghosh   }
25584cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25594cdd57e5SDebojyoti Ghosh }
25604cdd57e5SDebojyoti Ghosh 
256157df6a1bSDebojyoti Ghosh /*@
256257df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
256357df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
256457df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
256557df6a1bSDebojyoti Ghosh 
256657df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
256757df6a1bSDebojyoti Ghosh 
256857df6a1bSDebojyoti Ghosh    Parameters :
2569a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2570a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
257157df6a1bSDebojyoti Ghosh 
257257df6a1bSDebojyoti Ghosh    Level: intermediate
257357df6a1bSDebojyoti Ghosh 
257457df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
257557df6a1bSDebojyoti Ghosh 
257657df6a1bSDebojyoti Ghosh @*/
257757df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
257857df6a1bSDebojyoti Ghosh {
257957df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
258057df6a1bSDebojyoti Ghosh 
258157df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
258257df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25839657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
258457df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
258557df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
258657df6a1bSDebojyoti Ghosh   }
258757df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
258857df6a1bSDebojyoti Ghosh }
258957df6a1bSDebojyoti Ghosh 
2590bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2591d8e5e3e6SSatish Balay /*@
2592bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2593d763cef2SBarry Smith 
2594bdad233fSMatthew Knepley   Not collective
2595d763cef2SBarry Smith 
2596bdad233fSMatthew Knepley   Input Parameters:
2597bdad233fSMatthew Knepley + ts   - The TS
2598bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2599d763cef2SBarry Smith .vb
26000910c330SBarry Smith          U_t - A U = 0      (linear)
26010910c330SBarry Smith          U_t - A(t) U = 0   (linear)
26020910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2603d763cef2SBarry Smith .ve
2604d763cef2SBarry Smith 
2605d763cef2SBarry Smith    Level: beginner
2606d763cef2SBarry Smith 
2607bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2608d763cef2SBarry Smith @*/
26097087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2610a7cc72afSBarry Smith {
26119e2a6581SJed Brown   PetscErrorCode ierr;
26129e2a6581SJed Brown 
2613d763cef2SBarry Smith   PetscFunctionBegin;
26140700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2615bdad233fSMatthew Knepley   ts->problem_type = type;
26169e2a6581SJed Brown   if (type == TS_LINEAR) {
26179e2a6581SJed Brown     SNES snes;
26189e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
26199e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
26209e2a6581SJed Brown   }
2621d763cef2SBarry Smith   PetscFunctionReturn(0);
2622d763cef2SBarry Smith }
2623d763cef2SBarry Smith 
2624bdad233fSMatthew Knepley /*@C
2625bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2626bdad233fSMatthew Knepley 
2627bdad233fSMatthew Knepley   Not collective
2628bdad233fSMatthew Knepley 
2629bdad233fSMatthew Knepley   Input Parameter:
2630bdad233fSMatthew Knepley . ts   - The TS
2631bdad233fSMatthew Knepley 
2632bdad233fSMatthew Knepley   Output Parameter:
2633bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2634bdad233fSMatthew Knepley .vb
2635089b2837SJed Brown          M U_t = A U
2636089b2837SJed Brown          M(t) U_t = A(t) U
2637b5abc632SBarry Smith          F(t,U,U_t)
2638bdad233fSMatthew Knepley .ve
2639bdad233fSMatthew Knepley 
2640bdad233fSMatthew Knepley    Level: beginner
2641bdad233fSMatthew Knepley 
2642bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2643bdad233fSMatthew Knepley @*/
26447087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2645a7cc72afSBarry Smith {
2646bdad233fSMatthew Knepley   PetscFunctionBegin;
26470700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26484482741eSBarry Smith   PetscValidIntPointer(type,2);
2649bdad233fSMatthew Knepley   *type = ts->problem_type;
2650bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2651bdad233fSMatthew Knepley }
2652d763cef2SBarry Smith 
2653303a5415SBarry Smith /*
2654303a5415SBarry Smith     Attempt to check/preset a default value for the exact final time option. This is needed at the beginning of TSSolve() and in TSSetUp()
2655303a5415SBarry Smith */
2656303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2657303a5415SBarry Smith {
2658303a5415SBarry Smith   PetscErrorCode ierr;
2659303a5415SBarry Smith   PetscBool      isnone;
2660303a5415SBarry Smith 
2661303a5415SBarry Smith   PetscFunctionBegin;
2662303a5415SBarry Smith   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
2663303a5415SBarry Smith   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
2664303a5415SBarry Smith 
2665303a5415SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
2666303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2667303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2668303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2669303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2670303a5415SBarry Smith   }
2671303a5415SBarry Smith   PetscFunctionReturn(0);
2672303a5415SBarry Smith }
2673303a5415SBarry Smith 
2674303a5415SBarry Smith 
2675d763cef2SBarry Smith /*@
2676303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith    Collective on TS
2679d763cef2SBarry Smith 
2680d763cef2SBarry Smith    Input Parameter:
2681d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2682d763cef2SBarry Smith 
2683d763cef2SBarry Smith    Notes:
2684d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2685d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2686141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2687d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2688141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2689d763cef2SBarry Smith 
2690d763cef2SBarry Smith    Level: advanced
2691d763cef2SBarry Smith 
2692141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2693d763cef2SBarry Smith @*/
26947087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2695d763cef2SBarry Smith {
2696dfbe8321SBarry Smith   PetscErrorCode ierr;
26976c6b9e74SPeter Brune   DM             dm;
26986c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2699d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2700cd11d68dSLisandro Dalcin   TSIFunction    ifun;
27016c6b9e74SPeter Brune   TSIJacobian    ijac;
2702efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
27036c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2704d763cef2SBarry Smith 
2705d763cef2SBarry Smith   PetscFunctionBegin;
27060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2707277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2708277b19d0SLisandro Dalcin 
27097adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2710cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2711cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2712d763cef2SBarry Smith   }
2713277b19d0SLisandro Dalcin 
27141a638600SStefano Zampini   if (!ts->vec_sol) {
27151a638600SStefano Zampini     if (ts->dm) {
27161a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
27171a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
27181a638600SStefano Zampini   }
2719277b19d0SLisandro Dalcin 
2720298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2721298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2722298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2723298bade4SHong Zhang   }
2724298bade4SHong Zhang 
2725cd4cee2dSHong Zhang   if (ts->quadraturets) {
2726cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2727ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2728cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2729cd4cee2dSHong Zhang   }
2730cd4cee2dSHong Zhang 
2731971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2732f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2733e1244c69SJed Brown     Mat Amat,Pmat;
2734e1244c69SJed Brown     SNES snes;
2735e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2736e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2737e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2738e1244c69SJed Brown      * have displaced the RHS matrix */
2739971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2740abc0d4abSBarry 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 */
2741abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2742e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2743e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2744e1244c69SJed Brown     }
2745971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2746abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2747e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2748e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2749e1244c69SJed Brown     }
2750e1244c69SJed Brown   }
27512ffb9264SLisandro Dalcin 
27522ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27532ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27542ffb9264SLisandro Dalcin 
2755277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2756000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2757277b19d0SLisandro Dalcin   }
2758277b19d0SLisandro Dalcin 
2759303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
27602ffb9264SLisandro Dalcin 
2761a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27626c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27636c6b9e74SPeter Brune    */
27646c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27650298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27666c6b9e74SPeter Brune   if (!func) {
27676c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27686c6b9e74SPeter Brune   }
2769a6772fa2SLisandro 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.
27706c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27716c6b9e74SPeter Brune    */
27720298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27730298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2774efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27750298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2776efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27776c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27786c6b9e74SPeter Brune   }
2779c0517034SDebojyoti Ghosh 
2780c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2781c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2782c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2783c0517034SDebojyoti Ghosh   }
2784c0517034SDebojyoti Ghosh 
2785277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2786277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2787277b19d0SLisandro Dalcin }
2788277b19d0SLisandro Dalcin 
2789f6a906c0SBarry Smith /*@
2790277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2791277b19d0SLisandro Dalcin 
2792277b19d0SLisandro Dalcin    Collective on TS
2793277b19d0SLisandro Dalcin 
2794277b19d0SLisandro Dalcin    Input Parameter:
2795277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2796277b19d0SLisandro Dalcin 
2797277b19d0SLisandro Dalcin    Level: beginner
2798277b19d0SLisandro Dalcin 
2799277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2800277b19d0SLisandro Dalcin @*/
2801277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2802277b19d0SLisandro Dalcin {
28031d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2804277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2805277b19d0SLisandro Dalcin 
2806277b19d0SLisandro Dalcin   PetscFunctionBegin;
2807277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2808b18ea86cSHong Zhang 
2809277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2810277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2811277b19d0SLisandro Dalcin   }
2812277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2813e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2814bbd56ea5SKarl Rupp 
28154e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
28164e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2817214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
28186bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2819efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2820e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2821e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
282238637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2823bbd56ea5SKarl Rupp 
2824cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2825ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2826ecf68647SHong Zhang   if (ts->forward_solve) {
2827ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2828ecf68647SHong Zhang   }
2829cd4cee2dSHong Zhang   if (ts->quadraturets) {
2830cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
283136eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2832cd4cee2dSHong Zhang   }
28331d06f6b3SHong Zhang   while (ilink) {
28341d06f6b3SHong Zhang     next = ilink->next;
28351d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
28361d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
28371d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
28381d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
28391d06f6b3SHong Zhang     ilink = next;
284087f4e208SHong Zhang   }
2841545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2842277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2843d763cef2SBarry Smith   PetscFunctionReturn(0);
2844d763cef2SBarry Smith }
2845d763cef2SBarry Smith 
2846d8e5e3e6SSatish Balay /*@
2847d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2848d763cef2SBarry Smith    with TSCreate().
2849d763cef2SBarry Smith 
2850d763cef2SBarry Smith    Collective on TS
2851d763cef2SBarry Smith 
2852d763cef2SBarry Smith    Input Parameter:
2853d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2854d763cef2SBarry Smith 
2855d763cef2SBarry Smith    Level: beginner
2856d763cef2SBarry Smith 
2857d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2858d763cef2SBarry Smith @*/
28596bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2860d763cef2SBarry Smith {
28616849ba73SBarry Smith   PetscErrorCode ierr;
2862d763cef2SBarry Smith 
2863d763cef2SBarry Smith   PetscFunctionBegin;
28646bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2865ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2866c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2867d763cef2SBarry Smith 
2868ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2869ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2870ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2871ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2872ecf68647SHong Zhang   }
2873e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2874e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28756bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28766d4c513bSLisandro Dalcin 
2877bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2878bc952696SBarry Smith 
287984df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28806427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28816427ac75SLisandro Dalcin 
28826bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28836bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28846bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28850dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28866d4c513bSLisandro Dalcin 
2887cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2888a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2889d763cef2SBarry Smith   PetscFunctionReturn(0);
2890d763cef2SBarry Smith }
2891d763cef2SBarry Smith 
2892d8e5e3e6SSatish Balay /*@
2893d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2894d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2895d763cef2SBarry Smith 
2896d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2897d763cef2SBarry Smith 
2898d763cef2SBarry Smith    Input Parameter:
2899d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2900d763cef2SBarry Smith 
2901d763cef2SBarry Smith    Output Parameter:
2902d763cef2SBarry Smith .  snes - the nonlinear solver context
2903d763cef2SBarry Smith 
2904d763cef2SBarry Smith    Notes:
2905d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2906d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
290794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2908d763cef2SBarry Smith 
2909d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
29100298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2911d763cef2SBarry Smith 
2912d763cef2SBarry Smith    Level: beginner
2913d763cef2SBarry Smith 
2914d763cef2SBarry Smith @*/
29157087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2916d763cef2SBarry Smith {
2917d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2918d372ba47SLisandro Dalcin 
2919d763cef2SBarry Smith   PetscFunctionBegin;
29200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29214482741eSBarry Smith   PetscValidPointer(snes,2);
2922d372ba47SLisandro Dalcin   if (!ts->snes) {
2923ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
292416413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
29250298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29263bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2927d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2928496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
29299e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
29309e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
29319e2a6581SJed Brown     }
2932d372ba47SLisandro Dalcin   }
2933d763cef2SBarry Smith   *snes = ts->snes;
2934d763cef2SBarry Smith   PetscFunctionReturn(0);
2935d763cef2SBarry Smith }
2936d763cef2SBarry Smith 
2937deb2cd25SJed Brown /*@
2938deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2939deb2cd25SJed Brown 
2940deb2cd25SJed Brown    Collective
2941deb2cd25SJed Brown 
2942deb2cd25SJed Brown    Input Parameter:
2943deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2944deb2cd25SJed Brown -  snes - the nonlinear solver context
2945deb2cd25SJed Brown 
2946deb2cd25SJed Brown    Notes:
2947deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2948deb2cd25SJed Brown 
2949deb2cd25SJed Brown    Level: developer
2950deb2cd25SJed Brown 
2951deb2cd25SJed Brown @*/
2952deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2953deb2cd25SJed Brown {
2954deb2cd25SJed Brown   PetscErrorCode ierr;
2955d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2956deb2cd25SJed Brown 
2957deb2cd25SJed Brown   PetscFunctionBegin;
2958deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2959deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2960deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2961deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2962bbd56ea5SKarl Rupp 
2963deb2cd25SJed Brown   ts->snes = snes;
2964bbd56ea5SKarl Rupp 
29650298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29660298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2967740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29680298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2969740132f1SEmil Constantinescu   }
2970deb2cd25SJed Brown   PetscFunctionReturn(0);
2971deb2cd25SJed Brown }
2972deb2cd25SJed Brown 
2973d8e5e3e6SSatish Balay /*@
297494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2975d763cef2SBarry Smith    a TS (timestepper) context.
2976d763cef2SBarry Smith 
297794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2978d763cef2SBarry Smith 
2979d763cef2SBarry Smith    Input Parameter:
2980d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2981d763cef2SBarry Smith 
2982d763cef2SBarry Smith    Output Parameter:
298394b7f48cSBarry Smith .  ksp - the nonlinear solver context
2984d763cef2SBarry Smith 
2985d763cef2SBarry Smith    Notes:
298694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2987d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2988d763cef2SBarry Smith    KSP and PC contexts as well.
2989d763cef2SBarry Smith 
299094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29910298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2992d763cef2SBarry Smith 
2993d763cef2SBarry Smith    Level: beginner
2994d763cef2SBarry Smith 
2995d763cef2SBarry Smith @*/
29967087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2997d763cef2SBarry Smith {
2998d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2999089b2837SJed Brown   SNES           snes;
3000d372ba47SLisandro Dalcin 
3001d763cef2SBarry Smith   PetscFunctionBegin;
30020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30034482741eSBarry Smith   PetscValidPointer(ksp,2);
300417186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
3005e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
3006089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3007089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
3008d763cef2SBarry Smith   PetscFunctionReturn(0);
3009d763cef2SBarry Smith }
3010d763cef2SBarry Smith 
3011d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
3012d763cef2SBarry Smith 
3013adb62b0dSMatthew Knepley /*@
3014618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
3015618ce8baSLisandro Dalcin 
3016618ce8baSLisandro Dalcin    Logically Collective on TS
3017618ce8baSLisandro Dalcin 
3018618ce8baSLisandro Dalcin    Input Parameters:
3019618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3020618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
3021618ce8baSLisandro Dalcin 
3022618ce8baSLisandro Dalcin    Options Database Keys:
3023618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
3024618ce8baSLisandro Dalcin 
3025618ce8baSLisandro Dalcin    Notes:
3026618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
3027618ce8baSLisandro Dalcin 
3028618ce8baSLisandro Dalcin    Level: intermediate
3029618ce8baSLisandro Dalcin 
3030618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
3031618ce8baSLisandro Dalcin @*/
3032618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
3033618ce8baSLisandro Dalcin {
3034618ce8baSLisandro Dalcin   PetscFunctionBegin;
3035618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3036618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3037618ce8baSLisandro Dalcin   if (maxsteps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
3038618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
3039618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3040618ce8baSLisandro Dalcin }
3041618ce8baSLisandro Dalcin 
3042618ce8baSLisandro Dalcin /*@
3043618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
3044618ce8baSLisandro Dalcin 
3045618ce8baSLisandro Dalcin    Not Collective
3046618ce8baSLisandro Dalcin 
3047618ce8baSLisandro Dalcin    Input Parameters:
3048618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3049618ce8baSLisandro Dalcin 
3050618ce8baSLisandro Dalcin    Output Parameter:
3051618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3052618ce8baSLisandro Dalcin 
3053618ce8baSLisandro Dalcin    Level: advanced
3054618ce8baSLisandro Dalcin 
3055618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3056618ce8baSLisandro Dalcin @*/
3057618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3058618ce8baSLisandro Dalcin {
3059618ce8baSLisandro Dalcin   PetscFunctionBegin;
3060618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3061618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3062618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3063618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3064618ce8baSLisandro Dalcin }
3065618ce8baSLisandro Dalcin 
3066618ce8baSLisandro Dalcin /*@
3067618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3068618ce8baSLisandro Dalcin 
3069618ce8baSLisandro Dalcin    Logically Collective on TS
3070618ce8baSLisandro Dalcin 
3071618ce8baSLisandro Dalcin    Input Parameters:
3072618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3073618ce8baSLisandro Dalcin -  maxtime - final time to step to
3074618ce8baSLisandro Dalcin 
3075618ce8baSLisandro Dalcin    Options Database Keys:
3076ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3077618ce8baSLisandro Dalcin 
3078618ce8baSLisandro Dalcin    Notes:
3079618ce8baSLisandro Dalcin    The default maximum time is 5.0
3080618ce8baSLisandro Dalcin 
3081618ce8baSLisandro Dalcin    Level: intermediate
3082618ce8baSLisandro Dalcin 
3083618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3084618ce8baSLisandro Dalcin @*/
3085618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3086618ce8baSLisandro Dalcin {
3087618ce8baSLisandro Dalcin   PetscFunctionBegin;
3088618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3089618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3090618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3091618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3092618ce8baSLisandro Dalcin }
3093618ce8baSLisandro Dalcin 
3094618ce8baSLisandro Dalcin /*@
3095618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3096618ce8baSLisandro Dalcin 
3097618ce8baSLisandro Dalcin    Not Collective
3098618ce8baSLisandro Dalcin 
3099618ce8baSLisandro Dalcin    Input Parameters:
3100618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3101618ce8baSLisandro Dalcin 
3102618ce8baSLisandro Dalcin    Output Parameter:
3103618ce8baSLisandro Dalcin .  maxtime - final time to step to
3104618ce8baSLisandro Dalcin 
3105618ce8baSLisandro Dalcin    Level: advanced
3106618ce8baSLisandro Dalcin 
3107618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3108618ce8baSLisandro Dalcin @*/
3109618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3110618ce8baSLisandro Dalcin {
3111618ce8baSLisandro Dalcin   PetscFunctionBegin;
3112618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3113618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3114618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3115618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3116618ce8baSLisandro Dalcin }
3117618ce8baSLisandro Dalcin 
3118618ce8baSLisandro Dalcin /*@
3119aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3120edc382c3SSatish Balay 
3121edc382c3SSatish Balay    Level: deprecated
3122edc382c3SSatish Balay 
3123aaa6c58dSLisandro Dalcin @*/
3124aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3125aaa6c58dSLisandro Dalcin {
3126aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3127aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3128aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3129aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3130aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3131aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3132aaa6c58dSLisandro Dalcin }
3133aaa6c58dSLisandro Dalcin 
3134aaa6c58dSLisandro Dalcin /*@
313519eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3136edc382c3SSatish Balay 
3137edc382c3SSatish Balay    Level: deprecated
3138edc382c3SSatish Balay 
3139adb62b0dSMatthew Knepley @*/
31407087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3141adb62b0dSMatthew Knepley {
3142adb62b0dSMatthew Knepley   PetscFunctionBegin;
31430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3144abc0a331SBarry Smith   if (maxsteps) {
31454482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3146adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3147adb62b0dSMatthew Knepley   }
3148abc0a331SBarry Smith   if (maxtime) {
31494482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3150adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3151adb62b0dSMatthew Knepley   }
3152adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3153adb62b0dSMatthew Knepley }
3154adb62b0dSMatthew Knepley 
3155d763cef2SBarry Smith /*@
315619eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3157edc382c3SSatish Balay 
3158edc382c3SSatish Balay    Level: deprecated
3159edc382c3SSatish Balay 
3160d763cef2SBarry Smith @*/
31617087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3162d763cef2SBarry Smith {
3163d763cef2SBarry Smith   PetscFunctionBegin;
31640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3165c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3166c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
316739b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
316839b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3169d763cef2SBarry Smith   PetscFunctionReturn(0);
3170d763cef2SBarry Smith }
3171d763cef2SBarry Smith 
3172d763cef2SBarry Smith /*@
31735c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3174edc382c3SSatish Balay 
3175edc382c3SSatish Balay    Level: deprecated
3176edc382c3SSatish Balay 
31775c5f5948SLisandro Dalcin @*/
3178e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31795c5f5948SLisandro Dalcin 
318019eac22cSLisandro Dalcin /*@
31814f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3182edc382c3SSatish Balay 
3183edc382c3SSatish Balay    Level: deprecated
3184edc382c3SSatish Balay 
31854f4e0956SLisandro Dalcin @*/
31864f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31874f4e0956SLisandro Dalcin 
31884f4e0956SLisandro Dalcin /*@
3189d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3190d763cef2SBarry Smith    for use by the TS routines.
3191d763cef2SBarry Smith 
3192d083f849SBarry Smith    Logically Collective on TS
3193d763cef2SBarry Smith 
3194d763cef2SBarry Smith    Input Parameters:
3195d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31960910c330SBarry Smith -  u - the solution vector
3197d763cef2SBarry Smith 
3198d763cef2SBarry Smith    Level: beginner
3199d763cef2SBarry Smith 
32000ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3201d763cef2SBarry Smith @*/
32020910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3203d763cef2SBarry Smith {
32048737fe31SLisandro Dalcin   PetscErrorCode ierr;
3205496e6a7aSJed Brown   DM             dm;
32068737fe31SLisandro Dalcin 
3207d763cef2SBarry Smith   PetscFunctionBegin;
32080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32090910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32100910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
32116bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
32120910c330SBarry Smith   ts->vec_sol = u;
3213bbd56ea5SKarl Rupp 
3214496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
32150910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3216d763cef2SBarry Smith   PetscFunctionReturn(0);
3217d763cef2SBarry Smith }
3218d763cef2SBarry Smith 
3219ac226902SBarry Smith /*@C
3220000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32213f2090d5SJed Brown   called once at the beginning of each time step.
3222000e7ae3SMatthew Knepley 
32233f9fe445SBarry Smith   Logically Collective on TS
3224000e7ae3SMatthew Knepley 
3225000e7ae3SMatthew Knepley   Input Parameters:
3226000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3227000e7ae3SMatthew Knepley - func - The function
3228000e7ae3SMatthew Knepley 
3229000e7ae3SMatthew Knepley   Calling sequence of func:
32306bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3231000e7ae3SMatthew Knepley 
3232000e7ae3SMatthew Knepley   Level: intermediate
3233000e7ae3SMatthew Knepley 
3234dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3235000e7ae3SMatthew Knepley @*/
32367087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3237000e7ae3SMatthew Knepley {
3238000e7ae3SMatthew Knepley   PetscFunctionBegin;
32390700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3240ae60f76fSBarry Smith   ts->prestep = func;
3241000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3242000e7ae3SMatthew Knepley }
3243000e7ae3SMatthew Knepley 
324409ee8438SJed Brown /*@
32453f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32463f2090d5SJed Brown 
32473f2090d5SJed Brown   Collective on TS
32483f2090d5SJed Brown 
32493f2090d5SJed Brown   Input Parameters:
32503f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32513f2090d5SJed Brown 
32523f2090d5SJed Brown   Notes:
32533f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32543f2090d5SJed Brown   so most users would not generally call this routine themselves.
32553f2090d5SJed Brown 
32563f2090d5SJed Brown   Level: developer
32573f2090d5SJed Brown 
32589be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32593f2090d5SJed Brown @*/
32607087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32613f2090d5SJed Brown {
32623f2090d5SJed Brown   PetscErrorCode ierr;
32633f2090d5SJed Brown 
32643f2090d5SJed Brown   PetscFunctionBegin;
32650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3266ae60f76fSBarry Smith   if (ts->prestep) {
32675efd42a4SStefano Zampini     Vec              U;
32685efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32695efd42a4SStefano Zampini 
32705efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32715efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3272ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32735efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3274dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3275312ce896SJed Brown   }
32763f2090d5SJed Brown   PetscFunctionReturn(0);
32773f2090d5SJed Brown }
32783f2090d5SJed Brown 
3279b8123daeSJed Brown /*@C
3280b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3281b8123daeSJed Brown   called once at the beginning of each stage.
3282b8123daeSJed Brown 
3283b8123daeSJed Brown   Logically Collective on TS
3284b8123daeSJed Brown 
3285b8123daeSJed Brown   Input Parameters:
3286b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3287b8123daeSJed Brown - func - The function
3288b8123daeSJed Brown 
3289b8123daeSJed Brown   Calling sequence of func:
3290b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3291b8123daeSJed Brown 
3292b8123daeSJed Brown   Level: intermediate
3293b8123daeSJed Brown 
3294b8123daeSJed Brown   Note:
3295b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
329680275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3297b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3298b8123daeSJed Brown 
32999be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3300b8123daeSJed Brown @*/
3301b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3302b8123daeSJed Brown {
3303b8123daeSJed Brown   PetscFunctionBegin;
3304b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3305ae60f76fSBarry Smith   ts->prestage = func;
3306b8123daeSJed Brown   PetscFunctionReturn(0);
3307b8123daeSJed Brown }
3308b8123daeSJed Brown 
33099be3e283SDebojyoti Ghosh /*@C
33109be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33119be3e283SDebojyoti Ghosh   called once at the end of each stage.
33129be3e283SDebojyoti Ghosh 
33139be3e283SDebojyoti Ghosh   Logically Collective on TS
33149be3e283SDebojyoti Ghosh 
33159be3e283SDebojyoti Ghosh   Input Parameters:
33169be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33179be3e283SDebojyoti Ghosh - func - The function
33189be3e283SDebojyoti Ghosh 
33199be3e283SDebojyoti Ghosh   Calling sequence of func:
33209be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33219be3e283SDebojyoti Ghosh 
33229be3e283SDebojyoti Ghosh   Level: intermediate
33239be3e283SDebojyoti Ghosh 
33249be3e283SDebojyoti Ghosh   Note:
33259be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
332680275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
33279be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33289be3e283SDebojyoti Ghosh 
33299be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33309be3e283SDebojyoti Ghosh @*/
33319be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33329be3e283SDebojyoti Ghosh {
33339be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33349be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33359be3e283SDebojyoti Ghosh   ts->poststage = func;
33369be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33379be3e283SDebojyoti Ghosh }
33389be3e283SDebojyoti Ghosh 
3339c688d042SShri Abhyankar /*@C
3340c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3341c688d042SShri Abhyankar   called once at the end of each step evaluation.
3342c688d042SShri Abhyankar 
3343c688d042SShri Abhyankar   Logically Collective on TS
3344c688d042SShri Abhyankar 
3345c688d042SShri Abhyankar   Input Parameters:
3346c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3347c688d042SShri Abhyankar - func - The function
3348c688d042SShri Abhyankar 
3349c688d042SShri Abhyankar   Calling sequence of func:
3350c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3351c688d042SShri Abhyankar 
3352c688d042SShri Abhyankar   Level: intermediate
3353c688d042SShri Abhyankar 
3354c688d042SShri Abhyankar   Note:
33551785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33561785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3357e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3358e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3359e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3360c688d042SShri Abhyankar 
3361c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3362c688d042SShri Abhyankar @*/
3363c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3364c688d042SShri Abhyankar {
3365c688d042SShri Abhyankar   PetscFunctionBegin;
3366c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3367c688d042SShri Abhyankar   ts->postevaluate = func;
3368c688d042SShri Abhyankar   PetscFunctionReturn(0);
3369c688d042SShri Abhyankar }
3370c688d042SShri Abhyankar 
3371b8123daeSJed Brown /*@
3372b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3373b8123daeSJed Brown 
3374b8123daeSJed Brown   Collective on TS
3375b8123daeSJed Brown 
3376b8123daeSJed Brown   Input Parameters:
3377b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33789be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3379b8123daeSJed Brown 
3380b8123daeSJed Brown   Notes:
3381b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3382b8123daeSJed Brown   most users would not generally call this routine themselves.
3383b8123daeSJed Brown 
3384b8123daeSJed Brown   Level: developer
3385b8123daeSJed Brown 
33869be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3387b8123daeSJed Brown @*/
3388b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3389b8123daeSJed Brown {
3390b8123daeSJed Brown   PetscFunctionBegin;
3391b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3392ae60f76fSBarry Smith   if (ts->prestage) {
3393ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3394b8123daeSJed Brown   }
3395b8123daeSJed Brown   PetscFunctionReturn(0);
3396b8123daeSJed Brown }
3397b8123daeSJed Brown 
33989be3e283SDebojyoti Ghosh /*@
33999be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34009be3e283SDebojyoti Ghosh 
34019be3e283SDebojyoti Ghosh   Collective on TS
34029be3e283SDebojyoti Ghosh 
34039be3e283SDebojyoti Ghosh   Input Parameters:
34049be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34059be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34069be3e283SDebojyoti Ghosh   stageindex  - Stage number
34079be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34089be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34099be3e283SDebojyoti Ghosh 
34109be3e283SDebojyoti Ghosh   Notes:
34119be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34129be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34139be3e283SDebojyoti Ghosh 
34149be3e283SDebojyoti Ghosh   Level: developer
34159be3e283SDebojyoti Ghosh 
34169be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34179be3e283SDebojyoti Ghosh @*/
34189be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34199be3e283SDebojyoti Ghosh {
34209be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34219be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34224beae5d8SLisandro Dalcin   if (ts->poststage) {
34239be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34249be3e283SDebojyoti Ghosh   }
34259be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34269be3e283SDebojyoti Ghosh }
34279be3e283SDebojyoti Ghosh 
3428c688d042SShri Abhyankar /*@
3429c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3430c688d042SShri Abhyankar 
3431c688d042SShri Abhyankar   Collective on TS
3432c688d042SShri Abhyankar 
3433c688d042SShri Abhyankar   Input Parameters:
3434c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3435c688d042SShri Abhyankar 
3436c688d042SShri Abhyankar   Notes:
3437c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3438c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3439c688d042SShri Abhyankar 
3440c688d042SShri Abhyankar   Level: developer
3441c688d042SShri Abhyankar 
3442c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3443c688d042SShri Abhyankar @*/
3444c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3445c688d042SShri Abhyankar {
3446c688d042SShri Abhyankar   PetscErrorCode ierr;
3447c688d042SShri Abhyankar 
3448c688d042SShri Abhyankar   PetscFunctionBegin;
3449c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3450c688d042SShri Abhyankar   if (ts->postevaluate) {
3451dcb233daSLisandro Dalcin     Vec              U;
3452dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3453dcb233daSLisandro Dalcin 
3454dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3455dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3456c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3457dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3458dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3459c688d042SShri Abhyankar   }
3460c688d042SShri Abhyankar   PetscFunctionReturn(0);
3461c688d042SShri Abhyankar }
3462c688d042SShri Abhyankar 
3463ac226902SBarry Smith /*@C
3464000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34653f2090d5SJed Brown   called once at the end of each time step.
3466000e7ae3SMatthew Knepley 
34673f9fe445SBarry Smith   Logically Collective on TS
3468000e7ae3SMatthew Knepley 
3469000e7ae3SMatthew Knepley   Input Parameters:
3470000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3471000e7ae3SMatthew Knepley - func - The function
3472000e7ae3SMatthew Knepley 
3473000e7ae3SMatthew Knepley   Calling sequence of func:
3474b8123daeSJed Brown $ func (TS ts);
3475000e7ae3SMatthew Knepley 
34761785ff2aSShri Abhyankar   Notes:
34771785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34781785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34791785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34801785ff2aSShri Abhyankar 
3481000e7ae3SMatthew Knepley   Level: intermediate
3482000e7ae3SMatthew Knepley 
3483dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3484000e7ae3SMatthew Knepley @*/
34857087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3486000e7ae3SMatthew Knepley {
3487000e7ae3SMatthew Knepley   PetscFunctionBegin;
34880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3489ae60f76fSBarry Smith   ts->poststep = func;
3490000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3491000e7ae3SMatthew Knepley }
3492000e7ae3SMatthew Knepley 
349309ee8438SJed Brown /*@
34943f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34953f2090d5SJed Brown 
34963f2090d5SJed Brown   Collective on TS
34973f2090d5SJed Brown 
34983f2090d5SJed Brown   Input Parameters:
34993f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35003f2090d5SJed Brown 
35013f2090d5SJed Brown   Notes:
35023f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35033f2090d5SJed Brown   so most users would not generally call this routine themselves.
35043f2090d5SJed Brown 
35053f2090d5SJed Brown   Level: developer
35063f2090d5SJed Brown 
35073f2090d5SJed Brown @*/
35087087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35093f2090d5SJed Brown {
35103f2090d5SJed Brown   PetscErrorCode ierr;
35113f2090d5SJed Brown 
35123f2090d5SJed Brown   PetscFunctionBegin;
35130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3514ae60f76fSBarry Smith   if (ts->poststep) {
35155efd42a4SStefano Zampini     Vec              U;
35165efd42a4SStefano Zampini     PetscObjectState sprev,spost;
35175efd42a4SStefano Zampini 
35185efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
35195efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3520ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
35215efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3522dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
352372ac3e02SJed Brown   }
35243f2090d5SJed Brown   PetscFunctionReturn(0);
35253f2090d5SJed Brown }
35263f2090d5SJed Brown 
3527d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3528d763cef2SBarry Smith 
3529d763cef2SBarry Smith /*@C
3530a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3531d763cef2SBarry Smith    timestep to display the iteration's  progress.
3532d763cef2SBarry Smith 
35333f9fe445SBarry Smith    Logically Collective on TS
3534d763cef2SBarry Smith 
3535d763cef2SBarry Smith    Input Parameters:
3536d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3537e213d8f1SJed Brown .  monitor - monitoring routine
3538329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35390298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3540b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35410298fd71SBarry Smith           (may be NULL)
3542d763cef2SBarry Smith 
3543e213d8f1SJed Brown    Calling sequence of monitor:
3544dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3545d763cef2SBarry Smith 
3546d763cef2SBarry Smith +    ts - the TS context
354763e21af5SBarry 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)
35481f06c33eSBarry Smith .    time - current time
35490910c330SBarry Smith .    u - current iterate
3550d763cef2SBarry Smith -    mctx - [optional] monitoring context
3551d763cef2SBarry Smith 
3552d763cef2SBarry Smith    Notes:
3553d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3554d763cef2SBarry Smith    already has been loaded.
3555d763cef2SBarry Smith 
355695452b02SPatrick Sanan    Fortran Notes:
355795452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3558025f1a04SBarry Smith 
3559d763cef2SBarry Smith    Level: intermediate
3560d763cef2SBarry Smith 
3561a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3562d763cef2SBarry Smith @*/
3563c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3564d763cef2SBarry Smith {
356578064530SBarry Smith   PetscErrorCode ierr;
356678064530SBarry Smith   PetscInt       i;
356778064530SBarry Smith   PetscBool      identical;
356878064530SBarry Smith 
3569d763cef2SBarry Smith   PetscFunctionBegin;
35700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
357178064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
357278064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
357378064530SBarry Smith     if (identical) PetscFunctionReturn(0);
357478064530SBarry Smith   }
357517186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3576d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35778704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3578d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3579d763cef2SBarry Smith   PetscFunctionReturn(0);
3580d763cef2SBarry Smith }
3581d763cef2SBarry Smith 
3582d763cef2SBarry Smith /*@C
3583a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3584d763cef2SBarry Smith 
35853f9fe445SBarry Smith    Logically Collective on TS
3586d763cef2SBarry Smith 
3587d763cef2SBarry Smith    Input Parameters:
3588d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3589d763cef2SBarry Smith 
3590d763cef2SBarry Smith    Notes:
3591d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3592d763cef2SBarry Smith 
3593d763cef2SBarry Smith    Level: intermediate
3594d763cef2SBarry Smith 
3595a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3596d763cef2SBarry Smith @*/
35977087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3598d763cef2SBarry Smith {
3599d952e501SBarry Smith   PetscErrorCode ierr;
3600d952e501SBarry Smith   PetscInt       i;
3601d952e501SBarry Smith 
3602d763cef2SBarry Smith   PetscFunctionBegin;
36030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3604d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36058704b422SBarry Smith     if (ts->monitordestroy[i]) {
36068704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3607d952e501SBarry Smith     }
3608d952e501SBarry Smith   }
3609d763cef2SBarry Smith   ts->numbermonitors = 0;
3610d763cef2SBarry Smith   PetscFunctionReturn(0);
3611d763cef2SBarry Smith }
3612d763cef2SBarry Smith 
3613721cd6eeSBarry Smith /*@C
3614721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36155516499fSSatish Balay 
36165516499fSSatish Balay    Level: intermediate
361741251cbbSSatish Balay 
361863e21af5SBarry Smith .seealso:  TSMonitorSet()
361941251cbbSSatish Balay @*/
3620721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3621d763cef2SBarry Smith {
3622dfbe8321SBarry Smith   PetscErrorCode ierr;
3623721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
362441aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3625d132466eSBarry Smith 
3626d763cef2SBarry Smith   PetscFunctionBegin;
36274d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
362841aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
362941aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3630721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
363141aca3d6SBarry Smith   if (iascii) {
3632649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
363363e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
363463e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
363563e21af5SBarry Smith     } else {
36368392e04aSShri 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);
363763e21af5SBarry Smith     }
3638649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
363941aca3d6SBarry Smith   } else if (ibinary) {
364041aca3d6SBarry Smith     PetscMPIInt rank;
3641*ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRMPI(ierr);
364241aca3d6SBarry Smith     if (!rank) {
3643450a797fSBarry Smith       PetscBool skipHeader;
3644450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3645450a797fSBarry Smith 
3646450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3647450a797fSBarry Smith       if (!skipHeader) {
3648f253e43cSLisandro Dalcin          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
3649450a797fSBarry Smith        }
365041aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
365141aca3d6SBarry Smith     } else {
365241aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
365341aca3d6SBarry Smith     }
365441aca3d6SBarry Smith   }
3655721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3656d763cef2SBarry Smith   PetscFunctionReturn(0);
3657d763cef2SBarry Smith }
3658d763cef2SBarry Smith 
3659cc9c3a59SBarry Smith /*@C
3660cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3661cc9c3a59SBarry Smith 
3662cc9c3a59SBarry Smith    Level: intermediate
3663cc9c3a59SBarry Smith 
3664cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3665cc9c3a59SBarry Smith @*/
3666cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3667cc9c3a59SBarry Smith {
3668cc9c3a59SBarry Smith   PetscErrorCode ierr;
3669cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3670cc9c3a59SBarry Smith   PetscBool      iascii;
3671cc9c3a59SBarry Smith   PetscReal      max,min;
3672cc9c3a59SBarry Smith 
3673cc9c3a59SBarry Smith 
3674cc9c3a59SBarry Smith   PetscFunctionBegin;
3675cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3676cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3677cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3678cc9c3a59SBarry Smith   if (iascii) {
3679cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3680cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3681cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
36825132926bSBarry 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);
3683cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3684cc9c3a59SBarry Smith   }
3685cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3686cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3687cc9c3a59SBarry Smith }
3688cc9c3a59SBarry Smith 
3689cd652676SJed Brown /*@
3690cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3691cd652676SJed Brown 
3692cd652676SJed Brown    Collective on TS
3693cd652676SJed Brown 
3694cd652676SJed Brown    Input Argument:
3695cd652676SJed Brown +  ts - time stepping context
3696cd652676SJed Brown -  t - time to interpolate to
3697cd652676SJed Brown 
3698cd652676SJed Brown    Output Argument:
36990910c330SBarry Smith .  U - state at given time
3700cd652676SJed Brown 
3701cd652676SJed Brown    Level: intermediate
3702cd652676SJed Brown 
3703cd652676SJed Brown    Developer Notes:
3704cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3705cd652676SJed Brown 
3706874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3707cd652676SJed Brown @*/
37080910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3709cd652676SJed Brown {
3710cd652676SJed Brown   PetscErrorCode ierr;
3711cd652676SJed Brown 
3712cd652676SJed Brown   PetscFunctionBegin;
3713cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3714b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3715be5899b3SLisandro 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);
3716ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
37170910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3718cd652676SJed Brown   PetscFunctionReturn(0);
3719cd652676SJed Brown }
3720cd652676SJed Brown 
3721d763cef2SBarry Smith /*@
37226d9e5789SSean Farley    TSStep - Steps one time step
3723d763cef2SBarry Smith 
3724d763cef2SBarry Smith    Collective on TS
3725d763cef2SBarry Smith 
3726d763cef2SBarry Smith    Input Parameter:
3727d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3728d763cef2SBarry Smith 
372927829d71SBarry Smith    Level: developer
3730d763cef2SBarry Smith 
3731b8123daeSJed Brown    Notes:
373227829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
373327829d71SBarry Smith 
3734b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3735b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3736b8123daeSJed Brown 
373719eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
373819eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
373925cb2221SBarry Smith 
37409be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3741d763cef2SBarry Smith @*/
3742193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3743d763cef2SBarry Smith {
3744dfbe8321SBarry Smith   PetscErrorCode   ierr;
3745fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3746be5899b3SLisandro Dalcin   PetscReal        ptime;
3747d763cef2SBarry Smith 
3748d763cef2SBarry Smith   PetscFunctionBegin;
37490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3750f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3751fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3752f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3753f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3754f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3755f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3756f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3757fffbeea8SBarry Smith 
3758d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
375968bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3760d405a339SMatthew Knepley 
3761fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3762ef85077eSLisandro 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>");
3763a6772fa2SLisandro 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()");
3764be5899b3SLisandro 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");
3765a6772fa2SLisandro Dalcin 
3766be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3767be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37682808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3769fc8dbba5SLisandro Dalcin 
3770d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3771193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3772d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3773fc8dbba5SLisandro Dalcin 
3774fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3775be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
37762808aa04SLisandro Dalcin     ts->steps++;
3777be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
377828d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3779d2daff3dSHong Zhang   }
3780fc8dbba5SLisandro Dalcin 
3781fc8dbba5SLisandro Dalcin   if (!ts->reason) {
378208c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
378308c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
378408c7845fSBarry Smith   }
3785fc8dbba5SLisandro Dalcin 
3786fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed && ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
3787fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
378808c7845fSBarry Smith   PetscFunctionReturn(0);
378908c7845fSBarry Smith }
379008c7845fSBarry Smith 
379108c7845fSBarry Smith /*@
37927cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37937cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37947cbde773SLisandro Dalcin 
37957cbde773SLisandro Dalcin    Collective on TS
37967cbde773SLisandro Dalcin 
37977cbde773SLisandro Dalcin    Input Arguments:
37987cbde773SLisandro Dalcin +  ts - time stepping context
37997cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
38007cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
38017cbde773SLisandro Dalcin 
38027cbde773SLisandro Dalcin    Output Arguments:
38037cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
38047cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
38057cbde773SLisandro Dalcin 
38067cbde773SLisandro Dalcin    Level: advanced
38077cbde773SLisandro Dalcin 
38087cbde773SLisandro Dalcin    Notes:
38097cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
38107cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
38117cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
38127cbde773SLisandro Dalcin 
38137cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
38147cbde773SLisandro Dalcin @*/
38157cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
38167cbde773SLisandro Dalcin {
38177cbde773SLisandro Dalcin   PetscErrorCode ierr;
38187cbde773SLisandro Dalcin 
38197cbde773SLisandro Dalcin   PetscFunctionBegin;
38207cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38217cbde773SLisandro Dalcin   PetscValidType(ts,1);
38227cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
38237cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
38247cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
38257cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
38267cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
38277cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
38287cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
38297cbde773SLisandro Dalcin   PetscFunctionReturn(0);
38307cbde773SLisandro Dalcin }
38317cbde773SLisandro Dalcin 
383205175c85SJed Brown /*@
383305175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
383405175c85SJed Brown 
38351c3436cfSJed Brown    Collective on TS
383605175c85SJed Brown 
383705175c85SJed Brown    Input Arguments:
38381c3436cfSJed Brown +  ts - time stepping context
38391c3436cfSJed Brown .  order - desired order of accuracy
38400298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
384105175c85SJed Brown 
384205175c85SJed Brown    Output Arguments:
38430910c330SBarry Smith .  U - state at the end of the current step
384405175c85SJed Brown 
384505175c85SJed Brown    Level: advanced
384605175c85SJed Brown 
3847108c343cSJed Brown    Notes:
3848108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3849108c343cSJed 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.
3850108c343cSJed Brown 
38511c3436cfSJed Brown .seealso: TSStep(), TSAdapt
385205175c85SJed Brown @*/
38530910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
385405175c85SJed Brown {
385505175c85SJed Brown   PetscErrorCode ierr;
385605175c85SJed Brown 
385705175c85SJed Brown   PetscFunctionBegin;
385805175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
385905175c85SJed Brown   PetscValidType(ts,1);
38600910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3861ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38620910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
386305175c85SJed Brown   PetscFunctionReturn(0);
386405175c85SJed Brown }
386505175c85SJed Brown 
3866aad739acSMatthew G. Knepley /*@C
38672e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3868aad739acSMatthew G. Knepley 
3869aad739acSMatthew G. Knepley   Not collective
3870aad739acSMatthew G. Knepley 
3871aad739acSMatthew G. Knepley   Input Argument:
3872aad739acSMatthew G. Knepley . ts        - time stepping context
3873aad739acSMatthew G. Knepley 
3874aad739acSMatthew G. Knepley   Output Argument:
38752e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3876aad739acSMatthew G. Knepley 
3877aad739acSMatthew G. Knepley    Level: advanced
3878aad739acSMatthew G. Knepley 
3879aad739acSMatthew G. Knepley    Notes:
3880aad739acSMatthew G. Knepley    The calling sequence for the function is
38812e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3882aad739acSMatthew G. Knepley $ ts - The timestepping context
38832e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3884aad739acSMatthew G. Knepley 
38852e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3886aad739acSMatthew G. Knepley @*/
38872e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3888aad739acSMatthew G. Knepley {
3889aad739acSMatthew G. Knepley   PetscFunctionBegin;
3890aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38912e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38922e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3893aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3894aad739acSMatthew G. Knepley }
3895aad739acSMatthew G. Knepley 
3896aad739acSMatthew G. Knepley /*@C
38972e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3898aad739acSMatthew G. Knepley 
3899aad739acSMatthew G. Knepley   Logically collective on ts
3900aad739acSMatthew G. Knepley 
3901aad739acSMatthew G. Knepley   Input Arguments:
3902aad739acSMatthew G. Knepley + ts        - time stepping context
39032e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3904aad739acSMatthew G. Knepley 
3905aad739acSMatthew G. Knepley   Level: advanced
3906aad739acSMatthew G. Knepley 
3907a96d6ef6SBarry Smith   Calling sequence for initCondition:
3908a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3909a96d6ef6SBarry Smith 
3910a96d6ef6SBarry Smith + ts - The timestepping context
3911a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3912aad739acSMatthew G. Knepley 
39132e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3914aad739acSMatthew G. Knepley @*/
39152e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3916aad739acSMatthew G. Knepley {
3917aad739acSMatthew G. Knepley   PetscFunctionBegin;
3918aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
39192e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
39202e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3921aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3922aad739acSMatthew G. Knepley }
3923aad739acSMatthew G. Knepley 
3924aad739acSMatthew G. Knepley /*@
39252e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3926aad739acSMatthew G. Knepley 
3927aad739acSMatthew G. Knepley   Collective on ts
3928aad739acSMatthew G. Knepley 
3929aad739acSMatthew G. Knepley   Input Arguments:
3930aad739acSMatthew G. Knepley + ts - time stepping context
39312e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3932aad739acSMatthew G. Knepley 
3933aad739acSMatthew G. Knepley   Level: advanced
3934aad739acSMatthew G. Knepley 
39352e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3936aad739acSMatthew G. Knepley @*/
39372e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3938aad739acSMatthew G. Knepley {
3939aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3940aad739acSMatthew G. Knepley 
3941aad739acSMatthew G. Knepley   PetscFunctionBegin;
3942aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3943aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
39442e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3945aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3946aad739acSMatthew G. Knepley }
3947aad739acSMatthew G. Knepley 
3948aad739acSMatthew G. Knepley /*@C
3949aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3950aad739acSMatthew G. Knepley 
3951aad739acSMatthew G. Knepley   Not collective
3952aad739acSMatthew G. Knepley 
3953aad739acSMatthew G. Knepley   Input Argument:
3954aad739acSMatthew G. Knepley . ts         - time stepping context
3955aad739acSMatthew G. Knepley 
3956aad739acSMatthew G. Knepley   Output Argument:
3957aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3958aad739acSMatthew G. Knepley 
3959aad739acSMatthew G. Knepley   Level: advanced
3960aad739acSMatthew G. Knepley 
3961a96d6ef6SBarry Smith   Calling sequence for exactError:
3962a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3963a96d6ef6SBarry Smith 
3964a96d6ef6SBarry Smith + ts - The timestepping context
3965a96d6ef6SBarry Smith . u  - The approximate solution vector
3966a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3967aad739acSMatthew G. Knepley 
3968aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3969aad739acSMatthew G. Knepley @*/
3970aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3971aad739acSMatthew G. Knepley {
3972aad739acSMatthew G. Knepley   PetscFunctionBegin;
3973aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3974aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3975aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3976aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3977aad739acSMatthew G. Knepley }
3978aad739acSMatthew G. Knepley 
3979aad739acSMatthew G. Knepley /*@C
3980aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3981aad739acSMatthew G. Knepley 
3982aad739acSMatthew G. Knepley   Logically collective on ts
3983aad739acSMatthew G. Knepley 
3984aad739acSMatthew G. Knepley   Input Arguments:
3985aad739acSMatthew G. Knepley + ts         - time stepping context
3986aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3987aad739acSMatthew G. Knepley 
3988aad739acSMatthew G. Knepley   Level: advanced
3989aad739acSMatthew G. Knepley 
3990a96d6ef6SBarry Smith   Calling sequence for exactError:
3991a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3992a96d6ef6SBarry Smith 
3993a96d6ef6SBarry Smith + ts - The timestepping context
3994a96d6ef6SBarry Smith . u  - The approximate solution vector
3995a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3996aad739acSMatthew G. Knepley 
3997aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3998aad739acSMatthew G. Knepley @*/
3999aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
4000aad739acSMatthew G. Knepley {
4001aad739acSMatthew G. Knepley   PetscFunctionBegin;
4002aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4003f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
4004aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
4005aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4006aad739acSMatthew G. Knepley }
4007aad739acSMatthew G. Knepley 
4008aad739acSMatthew G. Knepley /*@
4009aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
4010aad739acSMatthew G. Knepley 
4011aad739acSMatthew G. Knepley   Collective on ts
4012aad739acSMatthew G. Knepley 
4013aad739acSMatthew G. Knepley   Input Arguments:
4014aad739acSMatthew G. Knepley + ts - time stepping context
4015aad739acSMatthew G. Knepley . u  - The approximate solution
4016aad739acSMatthew G. Knepley - e  - The Vec used to store the error
4017aad739acSMatthew G. Knepley 
4018aad739acSMatthew G. Knepley   Level: advanced
4019aad739acSMatthew G. Knepley 
40202e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
4021aad739acSMatthew G. Knepley @*/
4022aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
4023aad739acSMatthew G. Knepley {
4024aad739acSMatthew G. Knepley   PetscErrorCode ierr;
4025aad739acSMatthew G. Knepley 
4026aad739acSMatthew G. Knepley   PetscFunctionBegin;
4027aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4028aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
4029aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
4030aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
4031aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4032aad739acSMatthew G. Knepley }
4033aad739acSMatthew G. Knepley 
4034b1cb36f3SHong Zhang /*@
40356a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
40366a4d4014SLisandro Dalcin 
40376a4d4014SLisandro Dalcin    Collective on TS
40386a4d4014SLisandro Dalcin 
40396a4d4014SLisandro Dalcin    Input Parameter:
40406a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
404163e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
404263e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
40435a3a76d0SJed Brown 
40446a4d4014SLisandro Dalcin    Level: beginner
40456a4d4014SLisandro Dalcin 
40465a3a76d0SJed Brown    Notes:
40475a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
40485a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40495a3a76d0SJed Brown    stepped over the final time.
40505a3a76d0SJed Brown 
405163e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
40526a4d4014SLisandro Dalcin @*/
4053cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
40546a4d4014SLisandro Dalcin {
4055b06615a5SLisandro Dalcin   Vec               solution;
40566a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
4057f22f69f0SBarry Smith 
40586a4d4014SLisandro Dalcin   PetscFunctionBegin;
40590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4060f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4061303a5415SBarry Smith 
4062303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
4063ee41a567SStefano 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 */
40640910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4065b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4066b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4067b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40685a3a76d0SJed Brown     }
40690910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4070715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4071bbd56ea5SKarl Rupp   } else if (u) {
40720910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40735a3a76d0SJed Brown   }
4074b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
407568bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4076a6772fa2SLisandro Dalcin 
4077ef85077eSLisandro 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>");
4078a6772fa2SLisandro 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()");
4079a6772fa2SLisandro 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");
4080a6772fa2SLisandro Dalcin 
4081715f1b00SHong Zhang   if (ts->forward_solve) {
4082715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4083715f1b00SHong Zhang   }
4084715f1b00SHong Zhang 
4085e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4086715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4087e7069c78SShri      TSTrajectory to incorrectly save the output files
4088e7069c78SShri   */
4089715f1b00SHong Zhang   /* reset time step and iteration counters */
40902808aa04SLisandro Dalcin   if (!ts->steps) {
40915ef26d82SJed Brown     ts->ksp_its           = 0;
40925ef26d82SJed Brown     ts->snes_its          = 0;
4093c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4094c610991cSLisandro Dalcin     ts->reject            = 0;
40952808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40962808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40977d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
40982808aa04SLisandro Dalcin   }
4099e97c63d7SStefano Zampini 
4100e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4101e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4102e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4103e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4104e97c63d7SStefano Zampini 
4105e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4106e97c63d7SStefano Zampini   }
4107193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4108193ac0bcSJed Brown 
4109900f6b5bSMatthew G. Knepley   {
4110900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4111900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4112900f6b5bSMatthew G. Knepley     PetscBool         flg;
4113900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4114900f6b5bSMatthew G. Knepley 
4115900f6b5bSMatthew G. Knepley     if (!incall) {
4116900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4117900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4118900f6b5bSMatthew G. Knepley       if (flg) {
4119900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4120900f6b5bSMatthew G. Knepley         DM           dm;
4121900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4122900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4123f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4124900f6b5bSMatthew G. Knepley 
4125900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4126f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
4127900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4128900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4129900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4130900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4131f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
4132900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4133900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4134900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4135900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4136900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4137900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4138900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4139900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4140900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4141900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4142900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4143900f6b5bSMatthew G. Knepley       }
4144900f6b5bSMatthew G. Knepley     }
4145900f6b5bSMatthew G. Knepley   }
4146900f6b5bSMatthew G. Knepley 
4147ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4148f05ece33SBarry Smith 
4149193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4150193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4151a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4152cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4153a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4154be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4155db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4156db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4157e7069c78SShri 
41582808aa04SLisandro Dalcin     if (!ts->steps) {
41592808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41606427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41612808aa04SLisandro Dalcin     }
41626427ac75SLisandro Dalcin 
4163e1a7a14fSJed Brown     while (!ts->reason) {
41642808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41659687d888SLisandro Dalcin       if (!ts->steprollback) {
41669687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41679687d888SLisandro Dalcin       }
4168193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4169f3b1f45cSBarry Smith       if (ts->testjacobian) {
4170f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4171f3b1f45cSBarry Smith       }
4172f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4173f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4174f3b1f45cSBarry Smith       }
4175cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
41767b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4177b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
41787b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4179b1cb36f3SHong Zhang       }
418058818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41817b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4182715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
41837b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4184715f1b00SHong Zhang       }
418510b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4186e783b05fSHong 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. */
418758818c2dSLisandro Dalcin       if (ts->steprollback) {
418858818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
418958818c2dSLisandro Dalcin       }
4190e783b05fSHong Zhang       if (!ts->steprollback) {
41912808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41921eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4193aeb4809dSShri Abhyankar       }
4194193ac0bcSJed Brown     }
41952808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41966427ac75SLisandro Dalcin 
419749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41980910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4199cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4200b06615a5SLisandro Dalcin       solution = u;
420163e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
42020574a7fbSJed Brown     } else {
4203ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4204cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4205b06615a5SLisandro Dalcin       solution = ts->vec_sol;
42060574a7fbSJed Brown     }
4207193ac0bcSJed Brown   }
4208d2daff3dSHong Zhang 
4209ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
421063e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
421156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4212ef222394SBarry Smith   if (ts->adjoint_solve) {
4213ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
42142b0a91c0SBarry Smith   }
42156a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
42166a4d4014SLisandro Dalcin }
42176a4d4014SLisandro Dalcin 
42187db568b7SBarry Smith /*@C
4219228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4220228d79bcSJed Brown 
4221228d79bcSJed Brown    Collective on TS
4222228d79bcSJed Brown 
4223228d79bcSJed Brown    Input Parameters:
4224228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4225228d79bcSJed Brown .  step - step number that has just completed
4226228d79bcSJed Brown .  ptime - model time of the state
42270910c330SBarry Smith -  u - state at the current model time
4228228d79bcSJed Brown 
4229228d79bcSJed Brown    Notes:
42307db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
42317db568b7SBarry Smith    Users would almost never call this routine directly.
4232228d79bcSJed Brown 
423363e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
423463e21af5SBarry Smith 
42357db568b7SBarry Smith    Level: developer
4236228d79bcSJed Brown 
4237228d79bcSJed Brown @*/
42380910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4239d763cef2SBarry Smith {
4240d6152f81SLisandro Dalcin   DM             dm;
4241a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4242d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4243d763cef2SBarry Smith 
4244d763cef2SBarry Smith   PetscFunctionBegin;
4245b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4246b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4247d6152f81SLisandro Dalcin 
4248d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4249d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4250d6152f81SLisandro Dalcin 
42518860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4252d763cef2SBarry Smith   for (i=0; i<n; i++) {
42530910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4254d763cef2SBarry Smith   }
42558860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4256d763cef2SBarry Smith   PetscFunctionReturn(0);
4257d763cef2SBarry Smith }
4258d763cef2SBarry Smith 
4259d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4260d763cef2SBarry Smith /*@C
42617db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4262a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4263d763cef2SBarry Smith 
4264d763cef2SBarry Smith    Collective on TS
4265d763cef2SBarry Smith 
4266d763cef2SBarry Smith    Input Parameters:
4267d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4268d763cef2SBarry Smith .  label - the title to put in the title bar
42697c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4270a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4271a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4272d763cef2SBarry Smith 
4273d763cef2SBarry Smith    Output Parameter:
42740b039ecaSBarry Smith .  ctx - the context
4275d763cef2SBarry Smith 
4276d763cef2SBarry Smith    Options Database Key:
42774f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42788b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
42797db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42807db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42817db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42827db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4283b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4284d763cef2SBarry Smith 
4285d763cef2SBarry Smith    Notes:
4286a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4287d763cef2SBarry Smith 
42887db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42897db568b7SBarry Smith 
42907db568b7SBarry 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
42917db568b7SBarry 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
42927db568b7SBarry Smith    as the first argument.
42937db568b7SBarry Smith 
42947db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42957db568b7SBarry Smith 
4296d763cef2SBarry Smith    Level: intermediate
4297d763cef2SBarry Smith 
42987db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
42997db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
43007db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
43017db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
43027db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
43037c922b88SBarry Smith 
4304d763cef2SBarry Smith @*/
4305a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4306d763cef2SBarry Smith {
43077f52daa2SLisandro Dalcin   PetscDraw      draw;
4308dfbe8321SBarry Smith   PetscErrorCode ierr;
4309d763cef2SBarry Smith 
4310d763cef2SBarry Smith   PetscFunctionBegin;
4311b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
43127f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43137f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43147f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4315287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43167f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4317a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4318d763cef2SBarry Smith   PetscFunctionReturn(0);
4319d763cef2SBarry Smith }
4320d763cef2SBarry Smith 
4321b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4322d763cef2SBarry Smith {
43230b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4324c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4325dfbe8321SBarry Smith   PetscErrorCode ierr;
4326d763cef2SBarry Smith 
4327d763cef2SBarry Smith   PetscFunctionBegin;
432863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4329b06615a5SLisandro Dalcin   if (!step) {
4330a9f9c1f6SBarry Smith     PetscDrawAxis axis;
43318b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4332a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43338b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4334a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4335a9f9c1f6SBarry Smith   }
4336c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43378b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
43380b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4339b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43400b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43416934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
43423923b477SBarry Smith   }
4343d763cef2SBarry Smith   PetscFunctionReturn(0);
4344d763cef2SBarry Smith }
4345d763cef2SBarry Smith 
4346d763cef2SBarry Smith /*@C
4347a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4348a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4349d763cef2SBarry Smith 
43500b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4351d763cef2SBarry Smith 
4352d763cef2SBarry Smith    Input Parameter:
43530b039ecaSBarry Smith .  ctx - the monitor context
4354d763cef2SBarry Smith 
4355d763cef2SBarry Smith    Level: intermediate
4356d763cef2SBarry Smith 
43574f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4358d763cef2SBarry Smith @*/
4359a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4360d763cef2SBarry Smith {
4361dfbe8321SBarry Smith   PetscErrorCode ierr;
4362d763cef2SBarry Smith 
4363d763cef2SBarry Smith   PetscFunctionBegin;
43647684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43657684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43667684fa3eSBarry Smith   }
43670b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
436831152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4369387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4370387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4371387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43720b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4373d763cef2SBarry Smith   PetscFunctionReturn(0);
4374d763cef2SBarry Smith }
4375d763cef2SBarry Smith 
43761b575b74SJoseph Pusztay /*
43771b575b74SJoseph Pusztay 
43781b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
43791b575b74SJoseph Pusztay 
43801b575b74SJoseph Pusztay */
43811b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43821b575b74SJoseph Pusztay {
43831b575b74SJoseph Pusztay   PetscDraw      draw;
43841b575b74SJoseph Pusztay   PetscErrorCode ierr;
43851b575b74SJoseph Pusztay 
43861b575b74SJoseph Pusztay   PetscFunctionBegin;
43871b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43881b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43891b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43901b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43911b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43921b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43931b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43941b575b74SJoseph Pusztay 
43951b575b74SJoseph Pusztay }
43961b575b74SJoseph Pusztay 
43971b575b74SJoseph Pusztay /*
43981b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
43991b575b74SJoseph Pusztay */
44001b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
44011b575b74SJoseph Pusztay {
44021b575b74SJoseph Pusztay   PetscErrorCode ierr;
44031b575b74SJoseph Pusztay 
44041b575b74SJoseph Pusztay   PetscFunctionBegin;
44051b575b74SJoseph Pusztay 
44061b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
44071b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
44081b575b74SJoseph Pusztay 
44091b575b74SJoseph Pusztay   PetscFunctionReturn(0);
44101b575b74SJoseph Pusztay 
44111b575b74SJoseph Pusztay }
44121b575b74SJoseph Pusztay 
4413d763cef2SBarry Smith /*@
4414b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4415d763cef2SBarry Smith 
4416d763cef2SBarry Smith    Not Collective
4417d763cef2SBarry Smith 
4418d763cef2SBarry Smith    Input Parameter:
4419d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4420d763cef2SBarry Smith 
4421d763cef2SBarry Smith    Output Parameter:
442219eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4423d763cef2SBarry Smith 
4424d763cef2SBarry Smith    Level: beginner
4425d763cef2SBarry Smith 
4426b8123daeSJed Brown    Note:
4427b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
44289be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4429b8123daeSJed Brown 
44308f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4431d763cef2SBarry Smith 
4432d763cef2SBarry Smith @*/
44337087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4434d763cef2SBarry Smith {
4435d763cef2SBarry Smith   PetscFunctionBegin;
44360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4437f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4438d763cef2SBarry Smith   *t = ts->ptime;
4439d763cef2SBarry Smith   PetscFunctionReturn(0);
4440d763cef2SBarry Smith }
4441d763cef2SBarry Smith 
4442e5e524a1SHong Zhang /*@
4443e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4444e5e524a1SHong Zhang 
4445e5e524a1SHong Zhang    Not Collective
4446e5e524a1SHong Zhang 
4447e5e524a1SHong Zhang    Input Parameter:
4448e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4449e5e524a1SHong Zhang 
4450e5e524a1SHong Zhang    Output Parameter:
4451e5e524a1SHong Zhang .  t  - the previous time
4452e5e524a1SHong Zhang 
4453e5e524a1SHong Zhang    Level: beginner
4454e5e524a1SHong Zhang 
4455aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4456e5e524a1SHong Zhang 
4457e5e524a1SHong Zhang @*/
4458e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4459e5e524a1SHong Zhang {
4460e5e524a1SHong Zhang   PetscFunctionBegin;
4461e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4462e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4463e5e524a1SHong Zhang   *t = ts->ptime_prev;
4464e5e524a1SHong Zhang   PetscFunctionReturn(0);
4465e5e524a1SHong Zhang }
4466e5e524a1SHong Zhang 
44676a4d4014SLisandro Dalcin /*@
44686a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44696a4d4014SLisandro Dalcin 
44703f9fe445SBarry Smith    Logically Collective on TS
44716a4d4014SLisandro Dalcin 
44726a4d4014SLisandro Dalcin    Input Parameters:
44736a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44746a4d4014SLisandro Dalcin -  time - the time
44756a4d4014SLisandro Dalcin 
44766a4d4014SLisandro Dalcin    Level: intermediate
44776a4d4014SLisandro Dalcin 
447819eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
44796a4d4014SLisandro Dalcin 
44806a4d4014SLisandro Dalcin @*/
44817087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44826a4d4014SLisandro Dalcin {
44836a4d4014SLisandro Dalcin   PetscFunctionBegin;
44840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4485c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44866a4d4014SLisandro Dalcin   ts->ptime = t;
44876a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44886a4d4014SLisandro Dalcin }
44896a4d4014SLisandro Dalcin 
4490d763cef2SBarry Smith /*@C
4491d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4492d763cef2SBarry Smith    TS options in the database.
4493d763cef2SBarry Smith 
44943f9fe445SBarry Smith    Logically Collective on TS
4495d763cef2SBarry Smith 
4496d763cef2SBarry Smith    Input Parameter:
4497d763cef2SBarry Smith +  ts     - The TS context
4498d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4499d763cef2SBarry Smith 
4500d763cef2SBarry Smith    Notes:
4501d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4502d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4503d763cef2SBarry Smith    hyphen.
4504d763cef2SBarry Smith 
4505d763cef2SBarry Smith    Level: advanced
4506d763cef2SBarry Smith 
4507d763cef2SBarry Smith .seealso: TSSetFromOptions()
4508d763cef2SBarry Smith 
4509d763cef2SBarry Smith @*/
45107087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4511d763cef2SBarry Smith {
4512dfbe8321SBarry Smith   PetscErrorCode ierr;
4513089b2837SJed Brown   SNES           snes;
4514d763cef2SBarry Smith 
4515d763cef2SBarry Smith   PetscFunctionBegin;
45160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4517d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4518089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4519089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4520d763cef2SBarry Smith   PetscFunctionReturn(0);
4521d763cef2SBarry Smith }
4522d763cef2SBarry Smith 
4523d763cef2SBarry Smith /*@C
4524d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4525d763cef2SBarry Smith    TS options in the database.
4526d763cef2SBarry Smith 
45273f9fe445SBarry Smith    Logically Collective on TS
4528d763cef2SBarry Smith 
4529d763cef2SBarry Smith    Input Parameter:
4530d763cef2SBarry Smith +  ts     - The TS context
4531d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4532d763cef2SBarry Smith 
4533d763cef2SBarry Smith    Notes:
4534d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4535d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4536d763cef2SBarry Smith    hyphen.
4537d763cef2SBarry Smith 
4538d763cef2SBarry Smith    Level: advanced
4539d763cef2SBarry Smith 
4540d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4541d763cef2SBarry Smith 
4542d763cef2SBarry Smith @*/
45437087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4544d763cef2SBarry Smith {
4545dfbe8321SBarry Smith   PetscErrorCode ierr;
4546089b2837SJed Brown   SNES           snes;
4547d763cef2SBarry Smith 
4548d763cef2SBarry Smith   PetscFunctionBegin;
45490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4550d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4551089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4552089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4553d763cef2SBarry Smith   PetscFunctionReturn(0);
4554d763cef2SBarry Smith }
4555d763cef2SBarry Smith 
4556d763cef2SBarry Smith /*@C
4557d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4558d763cef2SBarry Smith    TS options in the database.
4559d763cef2SBarry Smith 
4560d763cef2SBarry Smith    Not Collective
4561d763cef2SBarry Smith 
4562d763cef2SBarry Smith    Input Parameter:
4563d763cef2SBarry Smith .  ts - The TS context
4564d763cef2SBarry Smith 
4565d763cef2SBarry Smith    Output Parameter:
4566d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4567d763cef2SBarry Smith 
456895452b02SPatrick Sanan    Notes:
456995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4570d763cef2SBarry Smith    sufficient length to hold the prefix.
4571d763cef2SBarry Smith 
4572d763cef2SBarry Smith    Level: intermediate
4573d763cef2SBarry Smith 
4574d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4575d763cef2SBarry Smith @*/
45767087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4577d763cef2SBarry Smith {
4578dfbe8321SBarry Smith   PetscErrorCode ierr;
4579d763cef2SBarry Smith 
4580d763cef2SBarry Smith   PetscFunctionBegin;
45810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45824482741eSBarry Smith   PetscValidPointer(prefix,2);
4583d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4584d763cef2SBarry Smith   PetscFunctionReturn(0);
4585d763cef2SBarry Smith }
4586d763cef2SBarry Smith 
4587d763cef2SBarry Smith /*@C
4588d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4589d763cef2SBarry Smith 
4590d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4591d763cef2SBarry Smith 
4592d763cef2SBarry Smith    Input Parameter:
4593d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4594d763cef2SBarry Smith 
4595d763cef2SBarry Smith    Output Parameters:
4596e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4597e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4598e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4599e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4600d763cef2SBarry Smith 
460195452b02SPatrick Sanan    Notes:
460295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4603d763cef2SBarry Smith 
4604d763cef2SBarry Smith    Level: intermediate
4605d763cef2SBarry Smith 
460680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4607d763cef2SBarry Smith 
4608d763cef2SBarry Smith @*/
4609e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4610d763cef2SBarry Smith {
4611089b2837SJed Brown   PetscErrorCode ierr;
461224989b8cSPeter Brune   DM             dm;
4613089b2837SJed Brown 
4614d763cef2SBarry Smith   PetscFunctionBegin;
461523a57915SBarry Smith   if (Amat || Pmat) {
461623a57915SBarry Smith     SNES snes;
4617089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
461823a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4619e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
462023a57915SBarry Smith   }
462124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
462224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4623d763cef2SBarry Smith   PetscFunctionReturn(0);
4624d763cef2SBarry Smith }
4625d763cef2SBarry Smith 
46262eca1d9cSJed Brown /*@C
46272eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
46282eca1d9cSJed Brown 
46292eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46302eca1d9cSJed Brown 
46312eca1d9cSJed Brown    Input Parameter:
46322eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46332eca1d9cSJed Brown 
46342eca1d9cSJed Brown    Output Parameters:
4635e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4636e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46372eca1d9cSJed Brown .  f   - The function to compute the matrices
46382eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46392eca1d9cSJed Brown 
464095452b02SPatrick Sanan    Notes:
464195452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
46422eca1d9cSJed Brown 
46432eca1d9cSJed Brown    Level: advanced
46442eca1d9cSJed Brown 
464580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
46462eca1d9cSJed Brown 
46472eca1d9cSJed Brown @*/
4648e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46492eca1d9cSJed Brown {
4650089b2837SJed Brown   PetscErrorCode ierr;
465124989b8cSPeter Brune   DM             dm;
46520910c330SBarry Smith 
46532eca1d9cSJed Brown   PetscFunctionBegin;
4654c0aab802Sstefano_zampini   if (Amat || Pmat) {
4655c0aab802Sstefano_zampini     SNES snes;
4656089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4657f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4658e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4659c0aab802Sstefano_zampini   }
466024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
466124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46622eca1d9cSJed Brown   PetscFunctionReturn(0);
46632eca1d9cSJed Brown }
46642eca1d9cSJed Brown 
46651713a123SBarry Smith /*@C
466683a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46671713a123SBarry Smith    VecView() for the solution at each timestep
46681713a123SBarry Smith 
46691713a123SBarry Smith    Collective on TS
46701713a123SBarry Smith 
46711713a123SBarry Smith    Input Parameters:
46721713a123SBarry Smith +  ts - the TS context
46731713a123SBarry Smith .  step - current time-step
4674142b95e3SSatish Balay .  ptime - current time
46750298fd71SBarry Smith -  dummy - either a viewer or NULL
46761713a123SBarry Smith 
467799fdda47SBarry Smith    Options Database:
467899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
467999fdda47SBarry Smith 
468095452b02SPatrick Sanan    Notes:
468195452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
468299fdda47SBarry Smith        will look bad
468399fdda47SBarry Smith 
46841713a123SBarry Smith    Level: intermediate
46851713a123SBarry Smith 
4686a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46871713a123SBarry Smith @*/
46880910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46891713a123SBarry Smith {
4690dfbe8321SBarry Smith   PetscErrorCode   ierr;
469183a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4692473a3ab2SBarry Smith   PetscDraw        draw;
46931713a123SBarry Smith 
46941713a123SBarry Smith   PetscFunctionBegin;
46956083293cSBarry Smith   if (!step && ictx->showinitial) {
46966083293cSBarry Smith     if (!ictx->initialsolution) {
46970910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46981713a123SBarry Smith     }
46990910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
47006083293cSBarry Smith   }
4701b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47020dcf80beSBarry Smith 
47036083293cSBarry Smith   if (ictx->showinitial) {
47046083293cSBarry Smith     PetscReal pause;
47056083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
47066083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
47076083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
47086083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
47096083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
47106083293cSBarry Smith   }
47110910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4712473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
471351fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4714473a3ab2SBarry Smith     char      time[32];
4715473a3ab2SBarry Smith 
4716473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
471785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4718473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4719473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
472051fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4721473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4722473a3ab2SBarry Smith   }
4723473a3ab2SBarry Smith 
47246083293cSBarry Smith   if (ictx->showinitial) {
47256083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
47266083293cSBarry Smith   }
47271713a123SBarry Smith   PetscFunctionReturn(0);
47281713a123SBarry Smith }
47291713a123SBarry Smith 
47309110b2e7SHong Zhang /*@C
47312d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
47322d5ee99bSBarry Smith 
47332d5ee99bSBarry Smith    Collective on TS
47342d5ee99bSBarry Smith 
47352d5ee99bSBarry Smith    Input Parameters:
47362d5ee99bSBarry Smith +  ts - the TS context
47372d5ee99bSBarry Smith .  step - current time-step
47382d5ee99bSBarry Smith .  ptime - current time
47392d5ee99bSBarry Smith -  dummy - either a viewer or NULL
47402d5ee99bSBarry Smith 
47412d5ee99bSBarry Smith    Level: intermediate
47422d5ee99bSBarry Smith 
47432d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47442d5ee99bSBarry Smith @*/
47452d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47462d5ee99bSBarry Smith {
47472d5ee99bSBarry Smith   PetscErrorCode    ierr;
47482d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47492d5ee99bSBarry Smith   PetscDraw         draw;
47506934998bSLisandro Dalcin   PetscDrawAxis     axis;
47512d5ee99bSBarry Smith   PetscInt          n;
47522d5ee99bSBarry Smith   PetscMPIInt       size;
47536934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47542d5ee99bSBarry Smith   char              time[32];
47552d5ee99bSBarry Smith   const PetscScalar *U;
47562d5ee99bSBarry Smith 
47572d5ee99bSBarry Smith   PetscFunctionBegin;
4758*ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRMPI(ierr);
47596934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47602d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47616934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47622d5ee99bSBarry Smith 
47632d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47646934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47656934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47666934998bSLisandro Dalcin   if (!step) {
47676934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47686934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47696934998bSLisandro Dalcin   }
47702d5ee99bSBarry Smith 
47712d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47726934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47736934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4774a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47756934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47762d5ee99bSBarry Smith 
47776934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47786934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47792d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47804b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
478185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47822d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
478351fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47842d5ee99bSBarry Smith   }
47856934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47862d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
478756d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47886934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47892d5ee99bSBarry Smith   PetscFunctionReturn(0);
47902d5ee99bSBarry Smith }
47912d5ee99bSBarry Smith 
47926083293cSBarry Smith /*@C
479383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
47946083293cSBarry Smith 
47956083293cSBarry Smith    Collective on TS
47966083293cSBarry Smith 
47976083293cSBarry Smith    Input Parameters:
47986083293cSBarry Smith .    ctx - the monitor context
47996083293cSBarry Smith 
48006083293cSBarry Smith    Level: intermediate
48016083293cSBarry Smith 
480283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
48036083293cSBarry Smith @*/
480483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
48056083293cSBarry Smith {
48066083293cSBarry Smith   PetscErrorCode ierr;
48076083293cSBarry Smith 
48086083293cSBarry Smith   PetscFunctionBegin;
480983a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
481083a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
481183a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
48126083293cSBarry Smith   PetscFunctionReturn(0);
48136083293cSBarry Smith }
48146083293cSBarry Smith 
48156083293cSBarry Smith /*@C
481683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
48176083293cSBarry Smith 
48186083293cSBarry Smith    Collective on TS
48196083293cSBarry Smith 
48206083293cSBarry Smith    Input Parameter:
48216083293cSBarry Smith .    ts - time-step context
48226083293cSBarry Smith 
48236083293cSBarry Smith    Output Patameter:
48246083293cSBarry Smith .    ctx - the monitor context
48256083293cSBarry Smith 
482699fdda47SBarry Smith    Options Database:
482799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
482899fdda47SBarry Smith 
48296083293cSBarry Smith    Level: intermediate
48306083293cSBarry Smith 
483183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
48326083293cSBarry Smith @*/
483383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
48346083293cSBarry Smith {
48356083293cSBarry Smith   PetscErrorCode   ierr;
48366083293cSBarry Smith 
48376083293cSBarry Smith   PetscFunctionBegin;
4838b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
483983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4840e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4841bbd56ea5SKarl Rupp 
484283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4843473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4844c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4845473a3ab2SBarry Smith 
4846473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4847c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
48486083293cSBarry Smith   PetscFunctionReturn(0);
48496083293cSBarry Smith }
48506083293cSBarry Smith 
48513a471f94SBarry Smith /*@C
48520ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
48530ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
48540ed3bfb6SBarry Smith 
48550ed3bfb6SBarry Smith    Collective on TS
48560ed3bfb6SBarry Smith 
48570ed3bfb6SBarry Smith    Input Parameters:
48580ed3bfb6SBarry Smith +  ts - the TS context
48590ed3bfb6SBarry Smith .  step - current time-step
48600ed3bfb6SBarry Smith .  ptime - current time
48610ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
48620ed3bfb6SBarry Smith 
48630ed3bfb6SBarry Smith    Options Database:
48640ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48650ed3bfb6SBarry Smith 
48660ed3bfb6SBarry Smith    Level: intermediate
48670ed3bfb6SBarry Smith 
48680ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48690ed3bfb6SBarry Smith @*/
48700ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48710ed3bfb6SBarry Smith {
48720ed3bfb6SBarry Smith   PetscErrorCode   ierr;
48730ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
48740ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
48750ed3bfb6SBarry Smith   Vec              work;
48760ed3bfb6SBarry Smith 
48770ed3bfb6SBarry Smith   PetscFunctionBegin;
48780ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48790ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48800ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48810ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48820ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48830ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48840ed3bfb6SBarry Smith }
48850ed3bfb6SBarry Smith 
48860ed3bfb6SBarry Smith /*@C
488783a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48883a471f94SBarry Smith    VecView() for the error at each timestep
48893a471f94SBarry Smith 
48903a471f94SBarry Smith    Collective on TS
48913a471f94SBarry Smith 
48923a471f94SBarry Smith    Input Parameters:
48933a471f94SBarry Smith +  ts - the TS context
48943a471f94SBarry Smith .  step - current time-step
48953a471f94SBarry Smith .  ptime - current time
48960298fd71SBarry Smith -  dummy - either a viewer or NULL
48973a471f94SBarry Smith 
48980ed3bfb6SBarry Smith    Options Database:
48990ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
49000ed3bfb6SBarry Smith 
49013a471f94SBarry Smith    Level: intermediate
49023a471f94SBarry Smith 
49030ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
49043a471f94SBarry Smith @*/
49050910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49063a471f94SBarry Smith {
49073a471f94SBarry Smith   PetscErrorCode   ierr;
490883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
490983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
49103a471f94SBarry Smith   Vec              work;
49113a471f94SBarry Smith 
49123a471f94SBarry Smith   PetscFunctionBegin;
4913b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49140910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
49153a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
49160910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
49173a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
49183a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
49193a471f94SBarry Smith   PetscFunctionReturn(0);
49203a471f94SBarry Smith }
49213a471f94SBarry Smith 
4922af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49236c699258SBarry Smith /*@
49242a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49256c699258SBarry Smith 
4926d083f849SBarry Smith    Logically Collective on ts
49276c699258SBarry Smith 
49286c699258SBarry Smith    Input Parameters:
49292a808120SBarry Smith +  ts - the ODE integrator object
49302a808120SBarry Smith -  dm - the dm, cannot be NULL
49316c699258SBarry Smith 
4932e03a659cSJed Brown    Notes:
4933e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4934e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4935e03a659cSJed Brown    different problems using the same function space.
4936e03a659cSJed Brown 
49376c699258SBarry Smith    Level: intermediate
49386c699258SBarry Smith 
49396c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49406c699258SBarry Smith @*/
49417087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49426c699258SBarry Smith {
49436c699258SBarry Smith   PetscErrorCode ierr;
4944089b2837SJed Brown   SNES           snes;
4945942e3340SBarry Smith   DMTS           tsdm;
49466c699258SBarry Smith 
49476c699258SBarry Smith   PetscFunctionBegin;
49480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49492a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
495070663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4951942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49522a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4953942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4954942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
495524989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
495624989b8cSPeter Brune         tsdm->originaldm = dm;
495724989b8cSPeter Brune       }
495824989b8cSPeter Brune     }
49596bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
496024989b8cSPeter Brune   }
49616c699258SBarry Smith   ts->dm = dm;
4962bbd56ea5SKarl Rupp 
4963089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4964089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49656c699258SBarry Smith   PetscFunctionReturn(0);
49666c699258SBarry Smith }
49676c699258SBarry Smith 
49686c699258SBarry Smith /*@
49696c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49706c699258SBarry Smith 
49713f9fe445SBarry Smith    Not Collective
49726c699258SBarry Smith 
49736c699258SBarry Smith    Input Parameter:
49746c699258SBarry Smith . ts - the preconditioner context
49756c699258SBarry Smith 
49766c699258SBarry Smith    Output Parameter:
49776c699258SBarry Smith .  dm - the dm
49786c699258SBarry Smith 
49796c699258SBarry Smith    Level: intermediate
49806c699258SBarry Smith 
49816c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49826c699258SBarry Smith @*/
49837087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49846c699258SBarry Smith {
4985496e6a7aSJed Brown   PetscErrorCode ierr;
4986496e6a7aSJed Brown 
49876c699258SBarry Smith   PetscFunctionBegin;
49880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4989496e6a7aSJed Brown   if (!ts->dm) {
4990ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4991496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4992496e6a7aSJed Brown   }
49936c699258SBarry Smith   *dm = ts->dm;
49946c699258SBarry Smith   PetscFunctionReturn(0);
49956c699258SBarry Smith }
49961713a123SBarry Smith 
49970f5c6efeSJed Brown /*@
49980f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49990f5c6efeSJed Brown 
50003f9fe445SBarry Smith    Logically Collective on SNES
50010f5c6efeSJed Brown 
50020f5c6efeSJed Brown    Input Parameter:
5003d42a1c89SJed Brown + snes - nonlinear solver
50040910c330SBarry Smith . U - the current state at which to evaluate the residual
5005d42a1c89SJed Brown - ctx - user context, must be a TS
50060f5c6efeSJed Brown 
50070f5c6efeSJed Brown    Output Parameter:
50080f5c6efeSJed Brown . F - the nonlinear residual
50090f5c6efeSJed Brown 
50100f5c6efeSJed Brown    Notes:
50110f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50120f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
50130f5c6efeSJed Brown 
50140f5c6efeSJed Brown    Level: advanced
50150f5c6efeSJed Brown 
50160f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
50170f5c6efeSJed Brown @*/
50180910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
50190f5c6efeSJed Brown {
50200f5c6efeSJed Brown   TS             ts = (TS)ctx;
50210f5c6efeSJed Brown   PetscErrorCode ierr;
50220f5c6efeSJed Brown 
50230f5c6efeSJed Brown   PetscFunctionBegin;
50240f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50250910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50260f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50270f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50280910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50290f5c6efeSJed Brown   PetscFunctionReturn(0);
50300f5c6efeSJed Brown }
50310f5c6efeSJed Brown 
50320f5c6efeSJed Brown /*@
50330f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50340f5c6efeSJed Brown 
50350f5c6efeSJed Brown    Collective on SNES
50360f5c6efeSJed Brown 
50370f5c6efeSJed Brown    Input Parameter:
50380f5c6efeSJed Brown + snes - nonlinear solver
50390910c330SBarry Smith . U - the current state at which to evaluate the residual
50400f5c6efeSJed Brown - ctx - user context, must be a TS
50410f5c6efeSJed Brown 
50420f5c6efeSJed Brown    Output Parameter:
50430f5c6efeSJed Brown + A - the Jacobian
50440f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50450f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50460f5c6efeSJed Brown 
50470f5c6efeSJed Brown    Notes:
50480f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50490f5c6efeSJed Brown 
50500f5c6efeSJed Brown    Level: developer
50510f5c6efeSJed Brown 
50520f5c6efeSJed Brown .seealso: SNESSetJacobian()
50530f5c6efeSJed Brown @*/
5054d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50550f5c6efeSJed Brown {
50560f5c6efeSJed Brown   TS             ts = (TS)ctx;
50570f5c6efeSJed Brown   PetscErrorCode ierr;
50580f5c6efeSJed Brown 
50590f5c6efeSJed Brown   PetscFunctionBegin;
50600f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50610910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50620f5c6efeSJed Brown   PetscValidPointer(A,3);
506394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50640f5c6efeSJed Brown   PetscValidPointer(B,4);
506594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50660f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5067d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50680f5c6efeSJed Brown   PetscFunctionReturn(0);
50690f5c6efeSJed Brown }
5070325fc9f4SBarry Smith 
50710e4ef248SJed Brown /*@C
50729ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50730e4ef248SJed Brown 
50740e4ef248SJed Brown    Collective on TS
50750e4ef248SJed Brown 
50760e4ef248SJed Brown    Input Arguments:
50770e4ef248SJed Brown +  ts - time stepping context
50780e4ef248SJed Brown .  t - time at which to evaluate
50790910c330SBarry Smith .  U - state at which to evaluate
50800e4ef248SJed Brown -  ctx - context
50810e4ef248SJed Brown 
50820e4ef248SJed Brown    Output Arguments:
50830e4ef248SJed Brown .  F - right hand side
50840e4ef248SJed Brown 
50850e4ef248SJed Brown    Level: intermediate
50860e4ef248SJed Brown 
50870e4ef248SJed Brown    Notes:
50880e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50890e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50900e4ef248SJed Brown 
50910e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50920e4ef248SJed Brown @*/
50930910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50940e4ef248SJed Brown {
50950e4ef248SJed Brown   PetscErrorCode ierr;
50960e4ef248SJed Brown   Mat            Arhs,Brhs;
50970e4ef248SJed Brown 
50980e4ef248SJed Brown   PetscFunctionBegin;
50990e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
51002663174eSHong Zhang   /* undo the damage caused by shifting */
5101cfa8a9a2SHong Zhang   ierr = TSRecoverRHSJacobian(ts,Arhs,Brhs);CHKERRQ(ierr);
5102d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
51030910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
51040e4ef248SJed Brown   PetscFunctionReturn(0);
51050e4ef248SJed Brown }
51060e4ef248SJed Brown 
51070e4ef248SJed Brown /*@C
51080e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
51090e4ef248SJed Brown 
51100e4ef248SJed Brown    Collective on TS
51110e4ef248SJed Brown 
51120e4ef248SJed Brown    Input Arguments:
51130e4ef248SJed Brown +  ts - time stepping context
51140e4ef248SJed Brown .  t - time at which to evaluate
51150910c330SBarry Smith .  U - state at which to evaluate
51160e4ef248SJed Brown -  ctx - context
51170e4ef248SJed Brown 
51180e4ef248SJed Brown    Output Arguments:
51190e4ef248SJed Brown +  A - pointer to operator
51200e4ef248SJed Brown .  B - pointer to preconditioning matrix
51210e4ef248SJed Brown -  flg - matrix structure flag
51220e4ef248SJed Brown 
51230e4ef248SJed Brown    Level: intermediate
51240e4ef248SJed Brown 
51250e4ef248SJed Brown    Notes:
51260e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51270e4ef248SJed Brown 
51280e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51290e4ef248SJed Brown @*/
5130d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51310e4ef248SJed Brown {
51320e4ef248SJed Brown   PetscFunctionBegin;
51330e4ef248SJed Brown   PetscFunctionReturn(0);
51340e4ef248SJed Brown }
51350e4ef248SJed Brown 
51360026cea9SSean Farley /*@C
51370026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51380026cea9SSean Farley 
51390026cea9SSean Farley    Collective on TS
51400026cea9SSean Farley 
51410026cea9SSean Farley    Input Arguments:
51420026cea9SSean Farley +  ts - time stepping context
51430026cea9SSean Farley .  t - time at which to evaluate
51440910c330SBarry Smith .  U - state at which to evaluate
51450910c330SBarry Smith .  Udot - time derivative of state vector
51460026cea9SSean Farley -  ctx - context
51470026cea9SSean Farley 
51480026cea9SSean Farley    Output Arguments:
51490026cea9SSean Farley .  F - left hand side
51500026cea9SSean Farley 
51510026cea9SSean Farley    Level: intermediate
51520026cea9SSean Farley 
51530026cea9SSean Farley    Notes:
51540910c330SBarry 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
51550026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51560026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51570026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51580026cea9SSean Farley 
51599ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51609ae8fd06SBarry Smith 
51619ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51620026cea9SSean Farley @*/
51630910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51640026cea9SSean Farley {
51650026cea9SSean Farley   PetscErrorCode ierr;
51660026cea9SSean Farley   Mat            A,B;
51670026cea9SSean Farley 
51680026cea9SSean Farley   PetscFunctionBegin;
51690298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5170d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51710910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51720026cea9SSean Farley   PetscFunctionReturn(0);
51730026cea9SSean Farley }
51740026cea9SSean Farley 
51750026cea9SSean Farley /*@C
5176b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51770026cea9SSean Farley 
51780026cea9SSean Farley    Collective on TS
51790026cea9SSean Farley 
51800026cea9SSean Farley    Input Arguments:
51810026cea9SSean Farley +  ts - time stepping context
51820026cea9SSean Farley .  t - time at which to evaluate
51830910c330SBarry Smith .  U - state at which to evaluate
51840910c330SBarry Smith .  Udot - time derivative of state vector
51850026cea9SSean Farley .  shift - shift to apply
51860026cea9SSean Farley -  ctx - context
51870026cea9SSean Farley 
51880026cea9SSean Farley    Output Arguments:
51890026cea9SSean Farley +  A - pointer to operator
51900026cea9SSean Farley .  B - pointer to preconditioning matrix
51910026cea9SSean Farley -  flg - matrix structure flag
51920026cea9SSean Farley 
5193b41af12eSJed Brown    Level: advanced
51940026cea9SSean Farley 
51950026cea9SSean Farley    Notes:
51960026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51970026cea9SSean Farley 
5198b41af12eSJed Brown    It is only appropriate for problems of the form
5199b41af12eSJed Brown 
5200b41af12eSJed Brown $     M Udot = F(U,t)
5201b41af12eSJed Brown 
5202b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5203b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5204b41af12eSJed Brown   an implicit operator of the form
5205b41af12eSJed Brown 
5206b41af12eSJed Brown $    shift*M + J
5207b41af12eSJed Brown 
5208b41af12eSJed 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
5209b41af12eSJed Brown   a copy of M or reassemble it when requested.
5210b41af12eSJed Brown 
52110026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
52120026cea9SSean Farley @*/
5213d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
52140026cea9SSean Farley {
5215b41af12eSJed Brown   PetscErrorCode ierr;
5216b41af12eSJed Brown 
52170026cea9SSean Farley   PetscFunctionBegin;
521894ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5219b41af12eSJed Brown   ts->ijacobian.shift = shift;
52200026cea9SSean Farley   PetscFunctionReturn(0);
52210026cea9SSean Farley }
5222b41af12eSJed Brown 
5223e817cc15SEmil Constantinescu /*@
5224e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5225e817cc15SEmil Constantinescu 
5226e817cc15SEmil Constantinescu    Not Collective
5227e817cc15SEmil Constantinescu 
5228e817cc15SEmil Constantinescu    Input Parameter:
5229e817cc15SEmil Constantinescu .  ts - the TS context
5230e817cc15SEmil Constantinescu 
5231e817cc15SEmil Constantinescu    Output Parameter:
52324e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5233e817cc15SEmil Constantinescu 
5234e817cc15SEmil Constantinescu    Level: beginner
5235e817cc15SEmil Constantinescu 
5236e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5237e817cc15SEmil Constantinescu @*/
5238e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5239e817cc15SEmil Constantinescu {
5240e817cc15SEmil Constantinescu   PetscFunctionBegin;
5241e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5242e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5243e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5244e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5245e817cc15SEmil Constantinescu }
5246e817cc15SEmil Constantinescu 
5247e817cc15SEmil Constantinescu /*@
5248e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5249e817cc15SEmil Constantinescu 
5250e817cc15SEmil Constantinescu    Not Collective
5251e817cc15SEmil Constantinescu 
5252e817cc15SEmil Constantinescu    Input Parameter:
5253e817cc15SEmil Constantinescu +  ts - the TS context
52541297b224SEmil Constantinescu -  equation_type - see TSEquationType
5255e817cc15SEmil Constantinescu 
5256e817cc15SEmil Constantinescu    Level: advanced
5257e817cc15SEmil Constantinescu 
5258e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5259e817cc15SEmil Constantinescu @*/
5260e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5261e817cc15SEmil Constantinescu {
5262e817cc15SEmil Constantinescu   PetscFunctionBegin;
5263e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5264e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5265e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5266e817cc15SEmil Constantinescu }
52670026cea9SSean Farley 
52684af1b03aSJed Brown /*@
52694af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52704af1b03aSJed Brown 
52714af1b03aSJed Brown    Not Collective
52724af1b03aSJed Brown 
52734af1b03aSJed Brown    Input Parameter:
52744af1b03aSJed Brown .  ts - the TS context
52754af1b03aSJed Brown 
52764af1b03aSJed Brown    Output Parameter:
52774af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52784af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52794af1b03aSJed Brown 
5280487e0bb9SJed Brown    Level: beginner
52814af1b03aSJed Brown 
5282cd652676SJed Brown    Notes:
5283cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52844af1b03aSJed Brown 
52854af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52864af1b03aSJed Brown @*/
52874af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52884af1b03aSJed Brown {
52894af1b03aSJed Brown   PetscFunctionBegin;
52904af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52914af1b03aSJed Brown   PetscValidPointer(reason,2);
52924af1b03aSJed Brown   *reason = ts->reason;
52934af1b03aSJed Brown   PetscFunctionReturn(0);
52944af1b03aSJed Brown }
52954af1b03aSJed Brown 
5296d6ad946cSShri Abhyankar /*@
5297d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5298d6ad946cSShri Abhyankar 
52996b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5300d6ad946cSShri Abhyankar 
53016b221cbeSPatrick Sanan    Input Parameters:
5302d6ad946cSShri Abhyankar +  ts - the TS context
53036b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5304d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5305d6ad946cSShri Abhyankar 
5306f5abba47SShri Abhyankar    Level: advanced
5307d6ad946cSShri Abhyankar 
5308d6ad946cSShri Abhyankar    Notes:
53096b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5310d6ad946cSShri Abhyankar 
5311d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5312d6ad946cSShri Abhyankar @*/
5313d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5314d6ad946cSShri Abhyankar {
5315d6ad946cSShri Abhyankar   PetscFunctionBegin;
5316d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5317d6ad946cSShri Abhyankar   ts->reason = reason;
5318d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5319d6ad946cSShri Abhyankar }
5320d6ad946cSShri Abhyankar 
5321cc708dedSBarry Smith /*@
5322cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5323cc708dedSBarry Smith 
5324cc708dedSBarry Smith    Not Collective
5325cc708dedSBarry Smith 
5326cc708dedSBarry Smith    Input Parameter:
5327cc708dedSBarry Smith .  ts - the TS context
5328cc708dedSBarry Smith 
5329cc708dedSBarry Smith    Output Parameter:
533019eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5331cc708dedSBarry Smith 
5332487e0bb9SJed Brown    Level: beginner
5333cc708dedSBarry Smith 
5334cc708dedSBarry Smith    Notes:
5335cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5336cc708dedSBarry Smith 
5337cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5338cc708dedSBarry Smith @*/
5339cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5340cc708dedSBarry Smith {
5341cc708dedSBarry Smith   PetscFunctionBegin;
5342cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5343cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5344cc708dedSBarry Smith   *ftime = ts->solvetime;
5345cc708dedSBarry Smith   PetscFunctionReturn(0);
5346cc708dedSBarry Smith }
5347cc708dedSBarry Smith 
53482c18e0fdSBarry Smith /*@
53495ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53509f67acb7SJed Brown    used by the time integrator.
53519f67acb7SJed Brown 
53529f67acb7SJed Brown    Not Collective
53539f67acb7SJed Brown 
53549f67acb7SJed Brown    Input Parameter:
53559f67acb7SJed Brown .  ts - TS context
53569f67acb7SJed Brown 
53579f67acb7SJed Brown    Output Parameter:
53589f67acb7SJed Brown .  nits - number of nonlinear iterations
53599f67acb7SJed Brown 
53609f67acb7SJed Brown    Notes:
53619f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53629f67acb7SJed Brown 
53639f67acb7SJed Brown    Level: intermediate
53649f67acb7SJed Brown 
53655ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53669f67acb7SJed Brown @*/
53675ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53689f67acb7SJed Brown {
53699f67acb7SJed Brown   PetscFunctionBegin;
53709f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53719f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53725ef26d82SJed Brown   *nits = ts->snes_its;
53739f67acb7SJed Brown   PetscFunctionReturn(0);
53749f67acb7SJed Brown }
53759f67acb7SJed Brown 
53769f67acb7SJed Brown /*@
53775ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53789f67acb7SJed Brown    used by the time integrator.
53799f67acb7SJed Brown 
53809f67acb7SJed Brown    Not Collective
53819f67acb7SJed Brown 
53829f67acb7SJed Brown    Input Parameter:
53839f67acb7SJed Brown .  ts - TS context
53849f67acb7SJed Brown 
53859f67acb7SJed Brown    Output Parameter:
53869f67acb7SJed Brown .  lits - number of linear iterations
53879f67acb7SJed Brown 
53889f67acb7SJed Brown    Notes:
53899f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53909f67acb7SJed Brown 
53919f67acb7SJed Brown    Level: intermediate
53929f67acb7SJed Brown 
53935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
53949f67acb7SJed Brown @*/
53955ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
53969f67acb7SJed Brown {
53979f67acb7SJed Brown   PetscFunctionBegin;
53989f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53999f67acb7SJed Brown   PetscValidIntPointer(lits,2);
54005ef26d82SJed Brown   *lits = ts->ksp_its;
54019f67acb7SJed Brown   PetscFunctionReturn(0);
54029f67acb7SJed Brown }
54039f67acb7SJed Brown 
5404cef5090cSJed Brown /*@
5405cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5406cef5090cSJed Brown 
5407cef5090cSJed Brown    Not Collective
5408cef5090cSJed Brown 
5409cef5090cSJed Brown    Input Parameter:
5410cef5090cSJed Brown .  ts - TS context
5411cef5090cSJed Brown 
5412cef5090cSJed Brown    Output Parameter:
5413cef5090cSJed Brown .  rejects - number of steps rejected
5414cef5090cSJed Brown 
5415cef5090cSJed Brown    Notes:
5416cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5417cef5090cSJed Brown 
5418cef5090cSJed Brown    Level: intermediate
5419cef5090cSJed Brown 
54205ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5421cef5090cSJed Brown @*/
5422cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5423cef5090cSJed Brown {
5424cef5090cSJed Brown   PetscFunctionBegin;
5425cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5426cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5427cef5090cSJed Brown   *rejects = ts->reject;
5428cef5090cSJed Brown   PetscFunctionReturn(0);
5429cef5090cSJed Brown }
5430cef5090cSJed Brown 
5431cef5090cSJed Brown /*@
5432cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5433cef5090cSJed Brown 
5434cef5090cSJed Brown    Not Collective
5435cef5090cSJed Brown 
5436cef5090cSJed Brown    Input Parameter:
5437cef5090cSJed Brown .  ts - TS context
5438cef5090cSJed Brown 
5439cef5090cSJed Brown    Output Parameter:
5440cef5090cSJed Brown .  fails - number of failed nonlinear solves
5441cef5090cSJed Brown 
5442cef5090cSJed Brown    Notes:
5443cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5444cef5090cSJed Brown 
5445cef5090cSJed Brown    Level: intermediate
5446cef5090cSJed Brown 
54475ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5448cef5090cSJed Brown @*/
5449cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5450cef5090cSJed Brown {
5451cef5090cSJed Brown   PetscFunctionBegin;
5452cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5453cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5454cef5090cSJed Brown   *fails = ts->num_snes_failures;
5455cef5090cSJed Brown   PetscFunctionReturn(0);
5456cef5090cSJed Brown }
5457cef5090cSJed Brown 
5458cef5090cSJed Brown /*@
5459cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5460cef5090cSJed Brown 
5461cef5090cSJed Brown    Not Collective
5462cef5090cSJed Brown 
5463cef5090cSJed Brown    Input Parameter:
5464cef5090cSJed Brown +  ts - TS context
5465cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5466cef5090cSJed Brown 
5467cef5090cSJed Brown    Notes:
5468cef5090cSJed Brown    The counter is reset to zero for each step
5469cef5090cSJed Brown 
5470cef5090cSJed Brown    Options Database Key:
5471cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5472cef5090cSJed Brown 
5473cef5090cSJed Brown    Level: intermediate
5474cef5090cSJed Brown 
54755ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5476cef5090cSJed Brown @*/
5477cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5478cef5090cSJed Brown {
5479cef5090cSJed Brown   PetscFunctionBegin;
5480cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5481cef5090cSJed Brown   ts->max_reject = rejects;
5482cef5090cSJed Brown   PetscFunctionReturn(0);
5483cef5090cSJed Brown }
5484cef5090cSJed Brown 
5485cef5090cSJed Brown /*@
5486cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5487cef5090cSJed Brown 
5488cef5090cSJed Brown    Not Collective
5489cef5090cSJed Brown 
5490cef5090cSJed Brown    Input Parameter:
5491cef5090cSJed Brown +  ts - TS context
5492cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5493cef5090cSJed Brown 
5494cef5090cSJed Brown    Notes:
5495cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5496cef5090cSJed Brown 
5497cef5090cSJed Brown    Options Database Key:
5498cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5499cef5090cSJed Brown 
5500cef5090cSJed Brown    Level: intermediate
5501cef5090cSJed Brown 
55025ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5503cef5090cSJed Brown @*/
5504cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5505cef5090cSJed Brown {
5506cef5090cSJed Brown   PetscFunctionBegin;
5507cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5508cef5090cSJed Brown   ts->max_snes_failures = fails;
5509cef5090cSJed Brown   PetscFunctionReturn(0);
5510cef5090cSJed Brown }
5511cef5090cSJed Brown 
5512cef5090cSJed Brown /*@
5513cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5514cef5090cSJed Brown 
5515cef5090cSJed Brown    Not Collective
5516cef5090cSJed Brown 
5517cef5090cSJed Brown    Input Parameter:
5518cef5090cSJed Brown +  ts - TS context
5519cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5520cef5090cSJed Brown 
5521cef5090cSJed Brown    Options Database Key:
5522cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5523cef5090cSJed Brown 
5524cef5090cSJed Brown    Level: intermediate
5525cef5090cSJed Brown 
55265ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5527cef5090cSJed Brown @*/
5528cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5529cef5090cSJed Brown {
5530cef5090cSJed Brown   PetscFunctionBegin;
5531cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5532cef5090cSJed Brown   ts->errorifstepfailed = err;
5533cef5090cSJed Brown   PetscFunctionReturn(0);
5534cef5090cSJed Brown }
5535cef5090cSJed Brown 
5536fb1732b5SBarry Smith /*@C
5537fde5950dSBarry 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
5538fb1732b5SBarry Smith 
5539fb1732b5SBarry Smith    Collective on TS
5540fb1732b5SBarry Smith 
5541fb1732b5SBarry Smith    Input Parameters:
5542fb1732b5SBarry Smith +  ts - the TS context
5543fb1732b5SBarry Smith .  step - current time-step
5544fb1732b5SBarry Smith .  ptime - current time
55450910c330SBarry Smith .  u - current state
5546721cd6eeSBarry Smith -  vf - viewer and its format
5547fb1732b5SBarry Smith 
5548fb1732b5SBarry Smith    Level: intermediate
5549fb1732b5SBarry Smith 
5550fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5551fb1732b5SBarry Smith @*/
5552721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5553fb1732b5SBarry Smith {
5554fb1732b5SBarry Smith   PetscErrorCode ierr;
5555fb1732b5SBarry Smith 
5556fb1732b5SBarry Smith   PetscFunctionBegin;
5557721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5558721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5559721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5560ed81e22dSJed Brown   PetscFunctionReturn(0);
5561ed81e22dSJed Brown }
5562ed81e22dSJed Brown 
5563ed81e22dSJed Brown /*@C
5564ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5565ed81e22dSJed Brown 
5566ed81e22dSJed Brown    Collective on TS
5567ed81e22dSJed Brown 
5568ed81e22dSJed Brown    Input Parameters:
5569ed81e22dSJed Brown +  ts - the TS context
5570ed81e22dSJed Brown .  step - current time-step
5571ed81e22dSJed Brown .  ptime - current time
55720910c330SBarry Smith .  u - current state
5573ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5574ed81e22dSJed Brown 
5575ed81e22dSJed Brown    Level: intermediate
5576ed81e22dSJed Brown 
5577ed81e22dSJed Brown    Notes:
5578ed81e22dSJed 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.
5579ed81e22dSJed Brown    These are named according to the file name template.
5580ed81e22dSJed Brown 
5581ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5582ed81e22dSJed Brown 
5583ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5584ed81e22dSJed Brown @*/
55850910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5586ed81e22dSJed Brown {
5587ed81e22dSJed Brown   PetscErrorCode ierr;
5588ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5589ed81e22dSJed Brown   PetscViewer    viewer;
5590ed81e22dSJed Brown 
5591ed81e22dSJed Brown   PetscFunctionBegin;
559263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55938caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5594ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
55950910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5596ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5597ed81e22dSJed Brown   PetscFunctionReturn(0);
5598ed81e22dSJed Brown }
5599ed81e22dSJed Brown 
5600ed81e22dSJed Brown /*@C
5601ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5602ed81e22dSJed Brown 
5603ed81e22dSJed Brown    Collective on TS
5604ed81e22dSJed Brown 
5605ed81e22dSJed Brown    Input Parameters:
5606ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5607ed81e22dSJed Brown 
5608ed81e22dSJed Brown    Level: intermediate
5609ed81e22dSJed Brown 
5610ed81e22dSJed Brown    Note:
5611ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5612ed81e22dSJed Brown 
5613ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5614ed81e22dSJed Brown @*/
5615ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5616ed81e22dSJed Brown {
5617ed81e22dSJed Brown   PetscErrorCode ierr;
5618ed81e22dSJed Brown 
5619ed81e22dSJed Brown   PetscFunctionBegin;
5620ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5621fb1732b5SBarry Smith   PetscFunctionReturn(0);
5622fb1732b5SBarry Smith }
5623fb1732b5SBarry Smith 
562484df9cb4SJed Brown /*@
5625552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
562684df9cb4SJed Brown 
5627ed81e22dSJed Brown    Collective on TS if controller has not been created yet
562884df9cb4SJed Brown 
562984df9cb4SJed Brown    Input Arguments:
5630ed81e22dSJed Brown .  ts - time stepping context
563184df9cb4SJed Brown 
563284df9cb4SJed Brown    Output Arguments:
5633ed81e22dSJed Brown .  adapt - adaptive controller
563484df9cb4SJed Brown 
563584df9cb4SJed Brown    Level: intermediate
563684df9cb4SJed Brown 
5637ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
563884df9cb4SJed Brown @*/
5639552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
564084df9cb4SJed Brown {
564184df9cb4SJed Brown   PetscErrorCode ierr;
564284df9cb4SJed Brown 
564384df9cb4SJed Brown   PetscFunctionBegin;
564484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5645bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
564684df9cb4SJed Brown   if (!ts->adapt) {
5647ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56483bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56491c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
565084df9cb4SJed Brown   }
5651bec58848SLisandro Dalcin   *adapt = ts->adapt;
565284df9cb4SJed Brown   PetscFunctionReturn(0);
565384df9cb4SJed Brown }
5654d6ebe24aSShri Abhyankar 
56551c3436cfSJed Brown /*@
56561c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
56571c3436cfSJed Brown 
56581c3436cfSJed Brown    Logically Collective
56591c3436cfSJed Brown 
56601c3436cfSJed Brown    Input Arguments:
56611c3436cfSJed Brown +  ts - time integration context
56621c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56630298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56641c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56650298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
56661c3436cfSJed Brown 
5667a3cdaa26SBarry Smith    Options Database keys:
5668a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5669a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5670a3cdaa26SBarry Smith 
56713ff766beSShri Abhyankar    Notes:
56723ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
56733ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
56743ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
56753ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
56763ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
56773ff766beSShri Abhyankar    differential variables.
56783ff766beSShri Abhyankar 
56791c3436cfSJed Brown    Level: beginner
56801c3436cfSJed Brown 
56815c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
56821c3436cfSJed Brown @*/
56831c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56841c3436cfSJed Brown {
56851c3436cfSJed Brown   PetscErrorCode ierr;
56861c3436cfSJed Brown 
56871c3436cfSJed Brown   PetscFunctionBegin;
5688c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56891c3436cfSJed Brown   if (vatol) {
56901c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56911c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56921c3436cfSJed Brown     ts->vatol = vatol;
56931c3436cfSJed Brown   }
5694c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
56951c3436cfSJed Brown   if (vrtol) {
56961c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
56971c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
56981c3436cfSJed Brown     ts->vrtol = vrtol;
56991c3436cfSJed Brown   }
57001c3436cfSJed Brown   PetscFunctionReturn(0);
57011c3436cfSJed Brown }
57021c3436cfSJed Brown 
5703c5033834SJed Brown /*@
5704c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5705c5033834SJed Brown 
5706c5033834SJed Brown    Logically Collective
5707c5033834SJed Brown 
5708c5033834SJed Brown    Input Arguments:
5709c5033834SJed Brown .  ts - time integration context
5710c5033834SJed Brown 
5711c5033834SJed Brown    Output Arguments:
57120298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
57130298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
57140298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
57150298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5716c5033834SJed Brown 
5717c5033834SJed Brown    Level: beginner
5718c5033834SJed Brown 
57195c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5720c5033834SJed Brown @*/
5721c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5722c5033834SJed Brown {
5723c5033834SJed Brown   PetscFunctionBegin;
5724c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5725c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5726c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5727c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5728c5033834SJed Brown   PetscFunctionReturn(0);
5729c5033834SJed Brown }
5730c5033834SJed Brown 
57319c6b16b5SShri Abhyankar /*@
5732a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57339c6b16b5SShri Abhyankar 
57349c6b16b5SShri Abhyankar    Collective on TS
57359c6b16b5SShri Abhyankar 
57369c6b16b5SShri Abhyankar    Input Arguments:
57379c6b16b5SShri Abhyankar +  ts - time stepping context
5738a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5739a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57409c6b16b5SShri Abhyankar 
57419c6b16b5SShri Abhyankar    Output Arguments:
5742a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57437453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5744a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57459c6b16b5SShri Abhyankar 
57469c6b16b5SShri Abhyankar    Level: developer
57479c6b16b5SShri Abhyankar 
5748deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57499c6b16b5SShri Abhyankar @*/
57507453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57519c6b16b5SShri Abhyankar {
57529c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57539c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57547453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57557453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57569c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57577453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
57587453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
57597453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57609c6b16b5SShri Abhyankar 
57619c6b16b5SShri Abhyankar   PetscFunctionBegin;
57629c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5763a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5764a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5765a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5766a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5767a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5768a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57698a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57708a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5771a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57729c6b16b5SShri Abhyankar 
57739c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57749c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57759c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57769c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57779c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57787453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
57797453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
57807453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
57819c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57829c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57839c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57849c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57859c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
578676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57877453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57887453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57897453f775SEmil Constantinescu       if (tola>0.){
57907453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57917453f775SEmil Constantinescu         na_loc++;
57927453f775SEmil Constantinescu       }
57937453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57947453f775SEmil Constantinescu       if (tolr>0.){
57957453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57967453f775SEmil Constantinescu         nr_loc++;
57977453f775SEmil Constantinescu       }
57987453f775SEmil Constantinescu       tol=tola+tolr;
57997453f775SEmil Constantinescu       if (tol>0.){
58007453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58017453f775SEmil Constantinescu         n_loc++;
58027453f775SEmil Constantinescu       }
58039c6b16b5SShri Abhyankar     }
58049c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58059c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58069c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58079c6b16b5SShri Abhyankar     const PetscScalar *atol;
58089c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58099c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
581076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58117453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58127453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58137453f775SEmil Constantinescu       if (tola>0.){
58147453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58157453f775SEmil Constantinescu         na_loc++;
58167453f775SEmil Constantinescu       }
58177453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58187453f775SEmil Constantinescu       if (tolr>0.){
58197453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58207453f775SEmil Constantinescu         nr_loc++;
58217453f775SEmil Constantinescu       }
58227453f775SEmil Constantinescu       tol=tola+tolr;
58237453f775SEmil Constantinescu       if (tol>0.){
58247453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58257453f775SEmil Constantinescu         n_loc++;
58267453f775SEmil Constantinescu       }
58279c6b16b5SShri Abhyankar     }
58289c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58299c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58309c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58319c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58329c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
583376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58347453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58357453f775SEmil Constantinescu       tola = ts->atol;
58367453f775SEmil Constantinescu       if (tola>0.){
58377453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58387453f775SEmil Constantinescu         na_loc++;
58397453f775SEmil Constantinescu       }
58407453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58417453f775SEmil Constantinescu       if (tolr>0.){
58427453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58437453f775SEmil Constantinescu         nr_loc++;
58447453f775SEmil Constantinescu       }
58457453f775SEmil Constantinescu       tol=tola+tolr;
58467453f775SEmil Constantinescu       if (tol>0.){
58477453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58487453f775SEmil Constantinescu         n_loc++;
58497453f775SEmil Constantinescu       }
58509c6b16b5SShri Abhyankar     }
58519c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58529c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58539c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
585476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58557453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58567453f775SEmil Constantinescu       tola = ts->atol;
58577453f775SEmil Constantinescu       if (tola>0.){
58587453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58597453f775SEmil Constantinescu         na_loc++;
58607453f775SEmil Constantinescu       }
58617453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58627453f775SEmil Constantinescu       if (tolr>0.){
58637453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58647453f775SEmil Constantinescu         nr_loc++;
58657453f775SEmil Constantinescu       }
58667453f775SEmil Constantinescu       tol=tola+tolr;
58677453f775SEmil Constantinescu       if (tol>0.){
58687453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58697453f775SEmil Constantinescu         n_loc++;
58707453f775SEmil Constantinescu       }
58719c6b16b5SShri Abhyankar     }
58729c6b16b5SShri Abhyankar   }
58739c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58749c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58759c6b16b5SShri Abhyankar 
58767453f775SEmil Constantinescu   err_loc[0] = sum;
58777453f775SEmil Constantinescu   err_loc[1] = suma;
58787453f775SEmil Constantinescu   err_loc[2] = sumr;
58797453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58807453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58817453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58827453f775SEmil Constantinescu 
5883a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58847453f775SEmil Constantinescu 
58857453f775SEmil Constantinescu   gsum   = err_glb[0];
58867453f775SEmil Constantinescu   gsuma  = err_glb[1];
58877453f775SEmil Constantinescu   gsumr  = err_glb[2];
58887453f775SEmil Constantinescu   n_glb  = err_glb[3];
58897453f775SEmil Constantinescu   na_glb = err_glb[4];
58907453f775SEmil Constantinescu   nr_glb = err_glb[5];
58917453f775SEmil Constantinescu 
5892b1316ef9SEmil Constantinescu   *norm  = 0.;
5893b1316ef9SEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
5894b1316ef9SEmil Constantinescu   *norma = 0.;
5895b1316ef9SEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5896b1316ef9SEmil Constantinescu   *normr = 0.;
5897b1316ef9SEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58989c6b16b5SShri Abhyankar 
58999c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59007453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59017453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59029c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59039c6b16b5SShri Abhyankar }
59049c6b16b5SShri Abhyankar 
59059c6b16b5SShri Abhyankar /*@
5906a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
59079c6b16b5SShri Abhyankar 
59089c6b16b5SShri Abhyankar    Collective on TS
59099c6b16b5SShri Abhyankar 
59109c6b16b5SShri Abhyankar    Input Arguments:
59119c6b16b5SShri Abhyankar +  ts - time stepping context
5912a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5913a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
59149c6b16b5SShri Abhyankar 
59159c6b16b5SShri Abhyankar    Output Arguments:
5916a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59177453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5918a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59199c6b16b5SShri Abhyankar 
59209c6b16b5SShri Abhyankar    Level: developer
59219c6b16b5SShri Abhyankar 
5922deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
59239c6b16b5SShri Abhyankar @*/
59247453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59259c6b16b5SShri Abhyankar {
59269c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
59277453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
59289c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
59297453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
59307453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
59317453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59329c6b16b5SShri Abhyankar 
59339c6b16b5SShri Abhyankar   PetscFunctionBegin;
59349c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5935a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5936a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5937a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5938a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5939a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5940a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59418a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59428a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5943a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59449c6b16b5SShri Abhyankar 
59459c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59469c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59479c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59489c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59499c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59507453f775SEmil Constantinescu 
59517453f775SEmil Constantinescu   max=0.;
59527453f775SEmil Constantinescu   maxa=0.;
59537453f775SEmil Constantinescu   maxr=0.;
59547453f775SEmil Constantinescu 
59557453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59569c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59579c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59589c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59597453f775SEmil Constantinescu 
59607453f775SEmil Constantinescu     for (i=0; i<n; i++) {
596176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59627453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59637453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59647453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59657453f775SEmil Constantinescu       tol  = tola+tolr;
59667453f775SEmil Constantinescu       if (tola>0.){
59677453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59687453f775SEmil Constantinescu       }
59697453f775SEmil Constantinescu       if (tolr>0.){
59707453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59717453f775SEmil Constantinescu       }
59727453f775SEmil Constantinescu       if (tol>0.){
59737453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59747453f775SEmil Constantinescu       }
59759c6b16b5SShri Abhyankar     }
59769c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59779c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59789c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59799c6b16b5SShri Abhyankar     const PetscScalar *atol;
59809c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59817453f775SEmil Constantinescu     for (i=0; i<n; i++) {
598276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59837453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59847453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59857453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59867453f775SEmil Constantinescu       tol  = tola+tolr;
59877453f775SEmil Constantinescu       if (tola>0.){
59887453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59897453f775SEmil Constantinescu       }
59907453f775SEmil Constantinescu       if (tolr>0.){
59917453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59927453f775SEmil Constantinescu       }
59937453f775SEmil Constantinescu       if (tol>0.){
59947453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59957453f775SEmil Constantinescu       }
59969c6b16b5SShri Abhyankar     }
59979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59989c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59999c6b16b5SShri Abhyankar     const PetscScalar *rtol;
60009c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60017453f775SEmil Constantinescu 
60027453f775SEmil Constantinescu     for (i=0; i<n; i++) {
600376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60047453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60057453f775SEmil Constantinescu       tola = ts->atol;
60067453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60077453f775SEmil Constantinescu       tol  = tola+tolr;
60087453f775SEmil Constantinescu       if (tola>0.){
60097453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60107453f775SEmil Constantinescu       }
60117453f775SEmil Constantinescu       if (tolr>0.){
60127453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60137453f775SEmil Constantinescu       }
60147453f775SEmil Constantinescu       if (tol>0.){
60157453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60167453f775SEmil Constantinescu       }
60179c6b16b5SShri Abhyankar     }
60189c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60199c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
60207453f775SEmil Constantinescu 
60217453f775SEmil Constantinescu     for (i=0; i<n; i++) {
602276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60237453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60247453f775SEmil Constantinescu       tola = ts->atol;
60257453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60267453f775SEmil Constantinescu       tol  = tola+tolr;
60277453f775SEmil Constantinescu       if (tola>0.){
60287453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60297453f775SEmil Constantinescu       }
60307453f775SEmil Constantinescu       if (tolr>0.){
60317453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60327453f775SEmil Constantinescu       }
60337453f775SEmil Constantinescu       if (tol>0.){
60347453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60357453f775SEmil Constantinescu       }
60369c6b16b5SShri Abhyankar     }
60379c6b16b5SShri Abhyankar   }
60389c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60399c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60407453f775SEmil Constantinescu   err_loc[0] = max;
60417453f775SEmil Constantinescu   err_loc[1] = maxa;
60427453f775SEmil Constantinescu   err_loc[2] = maxr;
6043a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60447453f775SEmil Constantinescu   gmax   = err_glb[0];
60457453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60467453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60479c6b16b5SShri Abhyankar 
60489c6b16b5SShri Abhyankar   *norm = gmax;
60497453f775SEmil Constantinescu   *norma = gmaxa;
60507453f775SEmil Constantinescu   *normr = gmaxr;
60519c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60527453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60537453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60549c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60559c6b16b5SShri Abhyankar }
60569c6b16b5SShri Abhyankar 
60571c3436cfSJed Brown /*@
60588a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60591c3436cfSJed Brown 
60601c3436cfSJed Brown    Collective on TS
60611c3436cfSJed Brown 
60621c3436cfSJed Brown    Input Arguments:
60631c3436cfSJed Brown +  ts - time stepping context
6064a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6065a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6066a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60677619abb3SShri 
60681c3436cfSJed Brown    Output Arguments:
6069a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60708a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6071a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6072a4868fbcSLisandro Dalcin 
6073a4868fbcSLisandro Dalcin    Options Database Keys:
6074a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6075a4868fbcSLisandro Dalcin 
60761c3436cfSJed Brown    Level: developer
60771c3436cfSJed Brown 
60788a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
60791c3436cfSJed Brown @*/
60807453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60811c3436cfSJed Brown {
60828beabaa1SBarry Smith   PetscErrorCode ierr;
60831c3436cfSJed Brown 
60841c3436cfSJed Brown   PetscFunctionBegin;
6085a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60867453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6087a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
60887453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6089a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60901c3436cfSJed Brown   PetscFunctionReturn(0);
60911c3436cfSJed Brown }
60921c3436cfSJed Brown 
60938a175baeSEmil Constantinescu 
60948a175baeSEmil Constantinescu /*@
60958a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
60968a175baeSEmil Constantinescu 
60978a175baeSEmil Constantinescu    Collective on TS
60988a175baeSEmil Constantinescu 
60998a175baeSEmil Constantinescu    Input Arguments:
61008a175baeSEmil Constantinescu +  ts - time stepping context
61018a175baeSEmil Constantinescu .  E - error vector
61028a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61038a175baeSEmil Constantinescu -  Y - state vector, previous time step
61048a175baeSEmil Constantinescu 
61058a175baeSEmil Constantinescu    Output Arguments:
6106a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61078a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6108a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61098a175baeSEmil Constantinescu 
61108a175baeSEmil Constantinescu    Level: developer
61118a175baeSEmil Constantinescu 
61128a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
61138a175baeSEmil Constantinescu @*/
61148a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61158a175baeSEmil Constantinescu {
61168a175baeSEmil Constantinescu   PetscErrorCode    ierr;
61178a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
61188a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
61198a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
61208a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
61218a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
61228a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
61238a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
61248a175baeSEmil Constantinescu 
61258a175baeSEmil Constantinescu   PetscFunctionBegin;
61268a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61278a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
61288a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
61298a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61308a175baeSEmil Constantinescu   PetscValidType(E,2);
61318a175baeSEmil Constantinescu   PetscValidType(U,3);
61328a175baeSEmil Constantinescu   PetscValidType(Y,4);
61338a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61348a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61358a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61368a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61378a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61388a175baeSEmil Constantinescu 
61398a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61408a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61418a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61428a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61438a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61448a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61458a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61468a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61478a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61488a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61498a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61508a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61518a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61528a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
615376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61548a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61558a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61568a175baeSEmil Constantinescu       if (tola>0.){
61578a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61588a175baeSEmil Constantinescu         na_loc++;
61598a175baeSEmil Constantinescu       }
61608a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61618a175baeSEmil Constantinescu       if (tolr>0.){
61628a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61638a175baeSEmil Constantinescu         nr_loc++;
61648a175baeSEmil Constantinescu       }
61658a175baeSEmil Constantinescu       tol=tola+tolr;
61668a175baeSEmil Constantinescu       if (tol>0.){
61678a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61688a175baeSEmil Constantinescu         n_loc++;
61698a175baeSEmil Constantinescu       }
61708a175baeSEmil Constantinescu     }
61718a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61728a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61738a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61748a175baeSEmil Constantinescu     const PetscScalar *atol;
61758a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61768a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
617776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61788a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61798a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61808a175baeSEmil Constantinescu       if (tola>0.){
61818a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61828a175baeSEmil Constantinescu         na_loc++;
61838a175baeSEmil Constantinescu       }
61848a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61858a175baeSEmil Constantinescu       if (tolr>0.){
61868a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61878a175baeSEmil Constantinescu         nr_loc++;
61888a175baeSEmil Constantinescu       }
61898a175baeSEmil Constantinescu       tol=tola+tolr;
61908a175baeSEmil Constantinescu       if (tol>0.){
61918a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61928a175baeSEmil Constantinescu         n_loc++;
61938a175baeSEmil Constantinescu       }
61948a175baeSEmil Constantinescu     }
61958a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61968a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61978a175baeSEmil Constantinescu     const PetscScalar *rtol;
61988a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61998a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
620076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62018a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62028a175baeSEmil Constantinescu       tola = ts->atol;
62038a175baeSEmil Constantinescu       if (tola>0.){
62048a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62058a175baeSEmil Constantinescu         na_loc++;
62068a175baeSEmil Constantinescu       }
62078a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62088a175baeSEmil Constantinescu       if (tolr>0.){
62098a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62108a175baeSEmil Constantinescu         nr_loc++;
62118a175baeSEmil Constantinescu       }
62128a175baeSEmil Constantinescu       tol=tola+tolr;
62138a175baeSEmil Constantinescu       if (tol>0.){
62148a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62158a175baeSEmil Constantinescu         n_loc++;
62168a175baeSEmil Constantinescu       }
62178a175baeSEmil Constantinescu     }
62188a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62198a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
62208a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
622176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62228a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62238a175baeSEmil Constantinescu       tola = ts->atol;
62248a175baeSEmil Constantinescu       if (tola>0.){
62258a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62268a175baeSEmil Constantinescu         na_loc++;
62278a175baeSEmil Constantinescu       }
62288a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62298a175baeSEmil Constantinescu       if (tolr>0.){
62308a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62318a175baeSEmil Constantinescu         nr_loc++;
62328a175baeSEmil Constantinescu       }
62338a175baeSEmil Constantinescu       tol=tola+tolr;
62348a175baeSEmil Constantinescu       if (tol>0.){
62358a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62368a175baeSEmil Constantinescu         n_loc++;
62378a175baeSEmil Constantinescu       }
62388a175baeSEmil Constantinescu     }
62398a175baeSEmil Constantinescu   }
62408a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62418a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62428a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62438a175baeSEmil Constantinescu 
62448a175baeSEmil Constantinescu   err_loc[0] = sum;
62458a175baeSEmil Constantinescu   err_loc[1] = suma;
62468a175baeSEmil Constantinescu   err_loc[2] = sumr;
62478a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62488a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62498a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62508a175baeSEmil Constantinescu 
6251a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62528a175baeSEmil Constantinescu 
62538a175baeSEmil Constantinescu   gsum   = err_glb[0];
62548a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62558a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62568a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62578a175baeSEmil Constantinescu   na_glb = err_glb[4];
62588a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62598a175baeSEmil Constantinescu 
62608a175baeSEmil Constantinescu   *norm  = 0.;
62618a175baeSEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
62628a175baeSEmil Constantinescu   *norma = 0.;
62638a175baeSEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62648a175baeSEmil Constantinescu   *normr = 0.;
62658a175baeSEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62668a175baeSEmil Constantinescu 
62678a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62688a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62698a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62708a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62718a175baeSEmil Constantinescu }
62728a175baeSEmil Constantinescu 
62738a175baeSEmil Constantinescu /*@
62748a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62758a175baeSEmil Constantinescu    Collective on TS
62768a175baeSEmil Constantinescu 
62778a175baeSEmil Constantinescu    Input Arguments:
62788a175baeSEmil Constantinescu +  ts - time stepping context
62798a175baeSEmil Constantinescu .  E - error vector
62808a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62818a175baeSEmil Constantinescu -  Y - state vector, previous time step
62828a175baeSEmil Constantinescu 
62838a175baeSEmil Constantinescu    Output Arguments:
6284a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62858a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6286a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62878a175baeSEmil Constantinescu 
62888a175baeSEmil Constantinescu    Level: developer
62898a175baeSEmil Constantinescu 
62908a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62918a175baeSEmil Constantinescu @*/
62928a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62938a175baeSEmil Constantinescu {
62948a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62958a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62968a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62978a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
62988a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62998a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
63008a175baeSEmil Constantinescu 
63018a175baeSEmil Constantinescu   PetscFunctionBegin;
63028a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63038a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
63048a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
63058a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
63068a175baeSEmil Constantinescu   PetscValidType(E,2);
63078a175baeSEmil Constantinescu   PetscValidType(U,3);
63088a175baeSEmil Constantinescu   PetscValidType(Y,4);
63098a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
63108a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
63118a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
63128a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
63138a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
63148a175baeSEmil Constantinescu 
63158a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
63168a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
63178a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
63188a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
63198a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
63208a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
63218a175baeSEmil Constantinescu 
63228a175baeSEmil Constantinescu   max=0.;
63238a175baeSEmil Constantinescu   maxa=0.;
63248a175baeSEmil Constantinescu   maxr=0.;
63258a175baeSEmil Constantinescu 
63268a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
63278a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
63288a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63298a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63308a175baeSEmil Constantinescu 
63318a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
633276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63338a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63348a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63358a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63368a175baeSEmil Constantinescu       tol  = tola+tolr;
63378a175baeSEmil Constantinescu       if (tola>0.){
63388a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63398a175baeSEmil Constantinescu       }
63408a175baeSEmil Constantinescu       if (tolr>0.){
63418a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63428a175baeSEmil Constantinescu       }
63438a175baeSEmil Constantinescu       if (tol>0.){
63448a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63458a175baeSEmil Constantinescu       }
63468a175baeSEmil Constantinescu     }
63478a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63488a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63498a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63508a175baeSEmil Constantinescu     const PetscScalar *atol;
63518a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63528a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
635376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63548a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63558a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63568a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63578a175baeSEmil Constantinescu       tol  = tola+tolr;
63588a175baeSEmil Constantinescu       if (tola>0.){
63598a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63608a175baeSEmil Constantinescu       }
63618a175baeSEmil Constantinescu       if (tolr>0.){
63628a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63638a175baeSEmil Constantinescu       }
63648a175baeSEmil Constantinescu       if (tol>0.){
63658a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63668a175baeSEmil Constantinescu       }
63678a175baeSEmil Constantinescu     }
63688a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63698a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63708a175baeSEmil Constantinescu     const PetscScalar *rtol;
63718a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63728a175baeSEmil Constantinescu 
63738a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
637476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63758a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63768a175baeSEmil Constantinescu       tola = ts->atol;
63778a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63788a175baeSEmil Constantinescu       tol  = tola+tolr;
63798a175baeSEmil Constantinescu       if (tola>0.){
63808a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63818a175baeSEmil Constantinescu       }
63828a175baeSEmil Constantinescu       if (tolr>0.){
63838a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63848a175baeSEmil Constantinescu       }
63858a175baeSEmil Constantinescu       if (tol>0.){
63868a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63878a175baeSEmil Constantinescu       }
63888a175baeSEmil Constantinescu     }
63898a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63908a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63918a175baeSEmil Constantinescu 
63928a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
639376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63948a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63958a175baeSEmil Constantinescu       tola = ts->atol;
63968a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63978a175baeSEmil Constantinescu       tol  = tola+tolr;
63988a175baeSEmil Constantinescu       if (tola>0.){
63998a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64008a175baeSEmil Constantinescu       }
64018a175baeSEmil Constantinescu       if (tolr>0.){
64028a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64038a175baeSEmil Constantinescu       }
64048a175baeSEmil Constantinescu       if (tol>0.){
64058a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64068a175baeSEmil Constantinescu       }
64078a175baeSEmil Constantinescu     }
64088a175baeSEmil Constantinescu   }
64098a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
64108a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
64118a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
64128a175baeSEmil Constantinescu   err_loc[0] = max;
64138a175baeSEmil Constantinescu   err_loc[1] = maxa;
64148a175baeSEmil Constantinescu   err_loc[2] = maxr;
6415a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64168a175baeSEmil Constantinescu   gmax   = err_glb[0];
64178a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
64188a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
64198a175baeSEmil Constantinescu 
64208a175baeSEmil Constantinescu   *norm = gmax;
64218a175baeSEmil Constantinescu   *norma = gmaxa;
64228a175baeSEmil Constantinescu   *normr = gmaxr;
64238a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
64248a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
64258a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
64268a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64278a175baeSEmil Constantinescu }
64288a175baeSEmil Constantinescu 
64298a175baeSEmil Constantinescu /*@
64308a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
64318a175baeSEmil Constantinescu 
64328a175baeSEmil Constantinescu    Collective on TS
64338a175baeSEmil Constantinescu 
64348a175baeSEmil Constantinescu    Input Arguments:
64358a175baeSEmil Constantinescu +  ts - time stepping context
64368a175baeSEmil Constantinescu .  E - error vector
64378a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64388a175baeSEmil Constantinescu .  Y - state vector, previous time step
64398a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64408a175baeSEmil Constantinescu 
64418a175baeSEmil Constantinescu    Output Arguments:
6442a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64438a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6444a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64458a175baeSEmil Constantinescu 
64468a175baeSEmil Constantinescu    Options Database Keys:
64478a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64488a175baeSEmil Constantinescu 
64498a175baeSEmil Constantinescu    Level: developer
64508a175baeSEmil Constantinescu 
64518a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64528a175baeSEmil Constantinescu @*/
64538a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64548a175baeSEmil Constantinescu {
64558a175baeSEmil Constantinescu   PetscErrorCode ierr;
64568a175baeSEmil Constantinescu 
64578a175baeSEmil Constantinescu   PetscFunctionBegin;
64588a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64598a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64608a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
64618a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64628a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64638a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64648a175baeSEmil Constantinescu }
64658a175baeSEmil Constantinescu 
64668a175baeSEmil Constantinescu 
64678d59e960SJed Brown /*@
64688d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64698d59e960SJed Brown 
64708d59e960SJed Brown    Logically Collective on TS
64718d59e960SJed Brown 
64728d59e960SJed Brown    Input Arguments:
64738d59e960SJed Brown +  ts - time stepping context
64748d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64758d59e960SJed Brown 
64768d59e960SJed Brown    Note:
64778d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64788d59e960SJed Brown 
64798d59e960SJed Brown    Level: intermediate
64808d59e960SJed Brown 
64818d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64828d59e960SJed Brown @*/
64838d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64848d59e960SJed Brown {
64858d59e960SJed Brown   PetscFunctionBegin;
64868d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64878d59e960SJed Brown   ts->cfltime_local = cfltime;
64888d59e960SJed Brown   ts->cfltime       = -1.;
64898d59e960SJed Brown   PetscFunctionReturn(0);
64908d59e960SJed Brown }
64918d59e960SJed Brown 
64928d59e960SJed Brown /*@
64938d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
64948d59e960SJed Brown 
64958d59e960SJed Brown    Collective on TS
64968d59e960SJed Brown 
64978d59e960SJed Brown    Input Arguments:
64988d59e960SJed Brown .  ts - time stepping context
64998d59e960SJed Brown 
65008d59e960SJed Brown    Output Arguments:
65018d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
65028d59e960SJed Brown 
65038d59e960SJed Brown    Level: advanced
65048d59e960SJed Brown 
65058d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
65068d59e960SJed Brown @*/
65078d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
65088d59e960SJed Brown {
65098d59e960SJed Brown   PetscErrorCode ierr;
65108d59e960SJed Brown 
65118d59e960SJed Brown   PetscFunctionBegin;
65128d59e960SJed Brown   if (ts->cfltime < 0) {
6513b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65148d59e960SJed Brown   }
65158d59e960SJed Brown   *cfltime = ts->cfltime;
65168d59e960SJed Brown   PetscFunctionReturn(0);
65178d59e960SJed Brown }
65188d59e960SJed Brown 
6519d6ebe24aSShri Abhyankar /*@
6520d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6521d6ebe24aSShri Abhyankar 
6522d6ebe24aSShri Abhyankar    Input Parameters:
6523a2b725a8SWilliam Gropp +  ts   - the TS context.
6524d6ebe24aSShri Abhyankar .  xl   - lower bound.
6525a2b725a8SWilliam Gropp -  xu   - upper bound.
6526d6ebe24aSShri Abhyankar 
6527d6ebe24aSShri Abhyankar    Notes:
6528d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6529e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6530d6ebe24aSShri Abhyankar 
65312bd2b0e6SSatish Balay    Level: advanced
65322bd2b0e6SSatish Balay 
6533d6ebe24aSShri Abhyankar @*/
6534d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6535d6ebe24aSShri Abhyankar {
6536d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6537d6ebe24aSShri Abhyankar   SNES           snes;
6538d6ebe24aSShri Abhyankar 
6539d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6540d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6541d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6542d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6543d6ebe24aSShri Abhyankar }
6544d6ebe24aSShri Abhyankar 
6545b3603a34SBarry Smith /*@C
65464f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6547b3603a34SBarry Smith        in a time based line graph
6548b3603a34SBarry Smith 
6549b3603a34SBarry Smith    Collective on TS
6550b3603a34SBarry Smith 
6551b3603a34SBarry Smith    Input Parameters:
6552b3603a34SBarry Smith +  ts - the TS context
6553b3603a34SBarry Smith .  step - current time-step
6554b3603a34SBarry Smith .  ptime - current time
65557db568b7SBarry Smith .  u - current solution
65567db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6557b3603a34SBarry Smith 
6558b3d3934dSBarry Smith    Options Database:
65599ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6560b3d3934dSBarry Smith 
6561b3603a34SBarry Smith    Level: intermediate
6562b3603a34SBarry Smith 
656395452b02SPatrick Sanan    Notes:
656495452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6565b3603a34SBarry Smith 
65667db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65677db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65687db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65697db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6570b3603a34SBarry Smith @*/
65717db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6572b3603a34SBarry Smith {
6573b3603a34SBarry Smith   PetscErrorCode    ierr;
65747db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6575b3603a34SBarry Smith   const PetscScalar *yy;
657680666b62SBarry Smith   Vec               v;
6577b3603a34SBarry Smith 
6578b3603a34SBarry Smith   PetscFunctionBegin;
657963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
658058ff32f7SBarry Smith   if (!step) {
6581a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65826934998bSLisandro Dalcin     PetscInt      dim;
6583a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65840ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6585bab0a581SBarry Smith     if (!ctx->names) {
6586bab0a581SBarry Smith       PetscBool flg;
6587bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6588bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6589bab0a581SBarry Smith       if (flg) {
6590bab0a581SBarry Smith         PetscInt i,n;
6591bab0a581SBarry Smith         char     **names;
6592bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6593bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6594bab0a581SBarry Smith         for (i=0; i<n; i++) {
6595bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6596bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6597bab0a581SBarry Smith         }
6598bab0a581SBarry Smith         names[n] = NULL;
6599bab0a581SBarry Smith         ctx->names = names;
6600bab0a581SBarry Smith       }
6601bab0a581SBarry Smith     }
6602387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6603387f4636SBarry Smith       char      **displaynames;
6604387f4636SBarry Smith       PetscBool flg;
6605387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6606580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6607c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6608387f4636SBarry Smith       if (flg) {
6609a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6610387f4636SBarry Smith       }
6611387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6612387f4636SBarry Smith     }
6613387f4636SBarry Smith     if (ctx->displaynames) {
6614387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6615387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6616387f4636SBarry Smith     } else if (ctx->names) {
66170910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
66180b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6619387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6620b0bc92c6SBarry Smith     } else {
6621b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6622b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6623387f4636SBarry Smith     }
66240b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
662558ff32f7SBarry Smith   }
66266934998bSLisandro Dalcin 
66276934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66286934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
662980666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66306934998bSLisandro Dalcin   if (ctx->displaynames) {
66316934998bSLisandro Dalcin     PetscInt i;
66326934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66336934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66346934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66356934998bSLisandro Dalcin   } else {
6636e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6637e3efe391SJed Brown     PetscInt  i,n;
66386934998bSLisandro Dalcin     PetscReal *yreal;
663980666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6640785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6641e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66420b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6643e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6644e3efe391SJed Brown #else
66450b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6646e3efe391SJed Brown #endif
664780666b62SBarry Smith   }
66486934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66496934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66506934998bSLisandro Dalcin 
6651b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66520b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66536934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66543923b477SBarry Smith   }
6655b3603a34SBarry Smith   PetscFunctionReturn(0);
6656b3603a34SBarry Smith }
6657b3603a34SBarry Smith 
6658b037adc7SBarry Smith /*@C
665931152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6660b037adc7SBarry Smith 
6661b037adc7SBarry Smith    Collective on TS
6662b037adc7SBarry Smith 
6663b037adc7SBarry Smith    Input Parameters:
6664b037adc7SBarry Smith +  ts - the TS context
6665b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6666b037adc7SBarry Smith 
6667b037adc7SBarry Smith    Level: intermediate
6668b037adc7SBarry Smith 
666995452b02SPatrick Sanan    Notes:
667095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66717db568b7SBarry Smith 
6672a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6673b037adc7SBarry Smith @*/
667431152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6675b037adc7SBarry Smith {
6676b037adc7SBarry Smith   PetscErrorCode    ierr;
6677b037adc7SBarry Smith   PetscInt          i;
6678b037adc7SBarry Smith 
6679b037adc7SBarry Smith   PetscFunctionBegin;
6680b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6681b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66825537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6683b3d3934dSBarry Smith       break;
6684b3d3934dSBarry Smith     }
6685b3d3934dSBarry Smith   }
6686b3d3934dSBarry Smith   PetscFunctionReturn(0);
6687b3d3934dSBarry Smith }
6688b3d3934dSBarry Smith 
6689e673d494SBarry Smith /*@C
6690e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6691e673d494SBarry Smith 
6692e673d494SBarry Smith    Collective on TS
6693e673d494SBarry Smith 
6694e673d494SBarry Smith    Input Parameters:
6695e673d494SBarry Smith +  ts - the TS context
6696e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6697e673d494SBarry Smith 
6698e673d494SBarry Smith    Level: intermediate
6699e673d494SBarry Smith 
6700a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6701e673d494SBarry Smith @*/
6702e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6703e673d494SBarry Smith {
6704e673d494SBarry Smith   PetscErrorCode    ierr;
6705e673d494SBarry Smith 
6706e673d494SBarry Smith   PetscFunctionBegin;
6707e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6708e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6709e673d494SBarry Smith   PetscFunctionReturn(0);
6710e673d494SBarry Smith }
6711e673d494SBarry Smith 
6712b3d3934dSBarry Smith /*@C
6713b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6714b3d3934dSBarry Smith 
6715b3d3934dSBarry Smith    Collective on TS
6716b3d3934dSBarry Smith 
6717b3d3934dSBarry Smith    Input Parameter:
6718b3d3934dSBarry Smith .  ts - the TS context
6719b3d3934dSBarry Smith 
6720b3d3934dSBarry Smith    Output Parameter:
6721b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6722b3d3934dSBarry Smith 
6723b3d3934dSBarry Smith    Level: intermediate
6724b3d3934dSBarry Smith 
672595452b02SPatrick Sanan    Notes:
672695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67277db568b7SBarry Smith 
6728b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6729b3d3934dSBarry Smith @*/
6730b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6731b3d3934dSBarry Smith {
6732b3d3934dSBarry Smith   PetscInt       i;
6733b3d3934dSBarry Smith 
6734b3d3934dSBarry Smith   PetscFunctionBegin;
6735b3d3934dSBarry Smith   *names = NULL;
6736b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6737b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67385537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6739b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6740b3d3934dSBarry Smith       break;
6741387f4636SBarry Smith     }
6742387f4636SBarry Smith   }
6743387f4636SBarry Smith   PetscFunctionReturn(0);
6744387f4636SBarry Smith }
6745387f4636SBarry Smith 
6746a66092f1SBarry Smith /*@C
6747a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6748a66092f1SBarry Smith 
6749a66092f1SBarry Smith    Collective on TS
6750a66092f1SBarry Smith 
6751a66092f1SBarry Smith    Input Parameters:
6752a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6753a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6754a66092f1SBarry Smith 
6755a66092f1SBarry Smith    Level: intermediate
6756a66092f1SBarry Smith 
6757a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6758a66092f1SBarry Smith @*/
6759a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6760a66092f1SBarry Smith {
6761a66092f1SBarry Smith   PetscInt          j = 0,k;
6762a66092f1SBarry Smith   PetscErrorCode    ierr;
6763a66092f1SBarry Smith 
6764a66092f1SBarry Smith   PetscFunctionBegin;
6765a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6766a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6767a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6768a66092f1SBarry Smith   while (displaynames[j]) j++;
6769a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6770a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6771a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6772a66092f1SBarry Smith   j = 0;
6773a66092f1SBarry Smith   while (displaynames[j]) {
6774a66092f1SBarry Smith     k = 0;
6775a66092f1SBarry Smith     while (ctx->names[k]) {
6776a66092f1SBarry Smith       PetscBool flg;
6777a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6778a66092f1SBarry Smith       if (flg) {
6779a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6780a66092f1SBarry Smith         break;
6781a66092f1SBarry Smith       }
6782a66092f1SBarry Smith       k++;
6783a66092f1SBarry Smith     }
6784a66092f1SBarry Smith     j++;
6785a66092f1SBarry Smith   }
6786a66092f1SBarry Smith   PetscFunctionReturn(0);
6787a66092f1SBarry Smith }
6788a66092f1SBarry Smith 
6789387f4636SBarry Smith /*@C
6790387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6791387f4636SBarry Smith 
6792387f4636SBarry Smith    Collective on TS
6793387f4636SBarry Smith 
6794387f4636SBarry Smith    Input Parameters:
6795387f4636SBarry Smith +  ts - the TS context
6796a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6797387f4636SBarry Smith 
679895452b02SPatrick Sanan    Notes:
679995452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68007db568b7SBarry Smith 
6801387f4636SBarry Smith    Level: intermediate
6802387f4636SBarry Smith 
6803387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6804387f4636SBarry Smith @*/
6805387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6806387f4636SBarry Smith {
6807a66092f1SBarry Smith   PetscInt          i;
6808387f4636SBarry Smith   PetscErrorCode    ierr;
6809387f4636SBarry Smith 
6810387f4636SBarry Smith   PetscFunctionBegin;
6811387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6812387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68135537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6814b3d3934dSBarry Smith       break;
6815b037adc7SBarry Smith     }
6816b037adc7SBarry Smith   }
6817b037adc7SBarry Smith   PetscFunctionReturn(0);
6818b037adc7SBarry Smith }
6819b037adc7SBarry Smith 
682080666b62SBarry Smith /*@C
682180666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
682280666b62SBarry Smith 
682380666b62SBarry Smith    Collective on TS
682480666b62SBarry Smith 
682580666b62SBarry Smith    Input Parameters:
682680666b62SBarry Smith +  ts - the TS context
682780666b62SBarry Smith .  transform - the transform function
68287684fa3eSBarry Smith .  destroy - function to destroy the optional context
682980666b62SBarry Smith -  ctx - optional context used by transform function
683080666b62SBarry Smith 
683195452b02SPatrick Sanan    Notes:
683295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68337db568b7SBarry Smith 
683480666b62SBarry Smith    Level: intermediate
683580666b62SBarry Smith 
6836a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
683780666b62SBarry Smith @*/
68387684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
683980666b62SBarry Smith {
684080666b62SBarry Smith   PetscInt          i;
6841a66092f1SBarry Smith   PetscErrorCode    ierr;
684280666b62SBarry Smith 
684380666b62SBarry Smith   PetscFunctionBegin;
684480666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
684580666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68465537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
684780666b62SBarry Smith     }
684880666b62SBarry Smith   }
684980666b62SBarry Smith   PetscFunctionReturn(0);
685080666b62SBarry Smith }
685180666b62SBarry Smith 
6852e673d494SBarry Smith /*@C
6853e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6854e673d494SBarry Smith 
6855e673d494SBarry Smith    Collective on TSLGCtx
6856e673d494SBarry Smith 
6857e673d494SBarry Smith    Input Parameters:
6858e673d494SBarry Smith +  ts - the TS context
6859e673d494SBarry Smith .  transform - the transform function
68607684fa3eSBarry Smith .  destroy - function to destroy the optional context
6861e673d494SBarry Smith -  ctx - optional context used by transform function
6862e673d494SBarry Smith 
6863e673d494SBarry Smith    Level: intermediate
6864e673d494SBarry Smith 
6865a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6866e673d494SBarry Smith @*/
68677684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6868e673d494SBarry Smith {
6869e673d494SBarry Smith   PetscFunctionBegin;
6870e673d494SBarry Smith   ctx->transform    = transform;
68717684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6872e673d494SBarry Smith   ctx->transformctx = tctx;
6873e673d494SBarry Smith   PetscFunctionReturn(0);
6874e673d494SBarry Smith }
6875e673d494SBarry Smith 
6876ef20d060SBarry Smith /*@C
68778b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6878ef20d060SBarry Smith        in a time based line graph
6879ef20d060SBarry Smith 
6880ef20d060SBarry Smith    Collective on TS
6881ef20d060SBarry Smith 
6882ef20d060SBarry Smith    Input Parameters:
6883ef20d060SBarry Smith +  ts - the TS context
6884ef20d060SBarry Smith .  step - current time-step
6885ef20d060SBarry Smith .  ptime - current time
68867db568b7SBarry Smith .  u - current solution
68877db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6888ef20d060SBarry Smith 
6889ef20d060SBarry Smith    Level: intermediate
6890ef20d060SBarry Smith 
689195452b02SPatrick Sanan    Notes:
689295452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6893abd5a294SJed Brown 
6894abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6895abd5a294SJed Brown 
6896abd5a294SJed Brown    Options Database Keys:
68974f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6898ef20d060SBarry Smith 
6899abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6900ef20d060SBarry Smith @*/
69010910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6902ef20d060SBarry Smith {
6903ef20d060SBarry Smith   PetscErrorCode    ierr;
69040b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6905ef20d060SBarry Smith   const PetscScalar *yy;
6906ef20d060SBarry Smith   Vec               y;
6907ef20d060SBarry Smith 
6908ef20d060SBarry Smith   PetscFunctionBegin;
6909a9f9c1f6SBarry Smith   if (!step) {
6910a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69116934998bSLisandro Dalcin     PetscInt      dim;
6912a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
69138b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
69140910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6915a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6916a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6917a9f9c1f6SBarry Smith   }
69180910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6919ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69200910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6921ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6922e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6923e3efe391SJed Brown   {
6924e3efe391SJed Brown     PetscReal *yreal;
6925e3efe391SJed Brown     PetscInt  i,n;
6926e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6927785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6928e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69290b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6930e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6931e3efe391SJed Brown   }
6932e3efe391SJed Brown #else
69330b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6934e3efe391SJed Brown #endif
6935ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6936ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6937b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69380b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69396934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69403923b477SBarry Smith   }
6941ef20d060SBarry Smith   PetscFunctionReturn(0);
6942ef20d060SBarry Smith }
6943ef20d060SBarry Smith 
69445e3b7effSJoseph Pusztay /*@C
69455e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
69465e3b7effSJoseph Pusztay 
69475e3b7effSJoseph Pusztay    Input Parameters:
69485e3b7effSJoseph Pusztay +  ts - the TS context
69495e3b7effSJoseph Pusztay .  step - current time-step
69505e3b7effSJoseph Pusztay .  ptime - current time
69515e3b7effSJoseph Pusztay .  u - current solution
69525e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
69535e3b7effSJoseph Pusztay 
69545e3b7effSJoseph Pusztay    Options Database:
6955918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
69565e3b7effSJoseph Pusztay 
69575e3b7effSJoseph Pusztay    Level: intermediate
69585e3b7effSJoseph Pusztay 
69595e3b7effSJoseph Pusztay @*/
69600ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
69611b575b74SJoseph Pusztay {
69621b575b74SJoseph Pusztay   PetscErrorCode    ierr;
69631b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
69641b575b74SJoseph Pusztay   const PetscScalar *yy;
6965b1670a61SJoseph Pusztay   PetscReal       *y,*x;
69661b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
69671b575b74SJoseph Pusztay   DM                dm;
69681b575b74SJoseph Pusztay 
69691b575b74SJoseph Pusztay   PetscFunctionBegin;
69701b575b74SJoseph Pusztay 
69711b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
69721b575b74SJoseph Pusztay   if (!step) {
69731b575b74SJoseph Pusztay     PetscDrawAxis axis;
69741b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
69751b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6976895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6977895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
69781b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
69791b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
69801b575b74SJoseph Pusztay     if (dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
69811b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69821b575b74SJoseph Pusztay     Np /= 2*dim;
69831b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69841b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69851b575b74SJoseph Pusztay   }
69861b575b74SJoseph Pusztay 
69871b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69881b575b74SJoseph Pusztay   Np /= 2*dim;
69891b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6990895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69911b575b74SJoseph Pusztay   /* get points from solution vector */
69921b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6993b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6994b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6995895f37d5SJoseph Pusztay   }
69961b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
69971b575b74SJoseph Pusztay 
69981b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69991b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
70001b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
70011b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
70021b575b74SJoseph Pusztay   }
70031b575b74SJoseph Pusztay 
7004918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
7005918b1d10SJoseph Pusztay 
70061b575b74SJoseph Pusztay   PetscFunctionReturn(0);
70071b575b74SJoseph Pusztay }
70081b575b74SJoseph Pusztay 
70091b575b74SJoseph Pusztay 
70101b575b74SJoseph Pusztay 
70117cf37e64SBarry Smith /*@C
70127cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
70137cf37e64SBarry Smith 
70147cf37e64SBarry Smith    Collective on TS
70157cf37e64SBarry Smith 
70167cf37e64SBarry Smith    Input Parameters:
70177cf37e64SBarry Smith +  ts - the TS context
70187cf37e64SBarry Smith .  step - current time-step
70197cf37e64SBarry Smith .  ptime - current time
70207cf37e64SBarry Smith .  u - current solution
70217cf37e64SBarry Smith -  dctx - unused context
70227cf37e64SBarry Smith 
70237cf37e64SBarry Smith    Level: intermediate
70247cf37e64SBarry Smith 
70257cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
70267cf37e64SBarry Smith 
70277cf37e64SBarry Smith    Options Database Keys:
70287cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
70297cf37e64SBarry Smith 
70307cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
70317cf37e64SBarry Smith @*/
7032edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70337cf37e64SBarry Smith {
70347cf37e64SBarry Smith   PetscErrorCode    ierr;
70357cf37e64SBarry Smith   Vec               y;
70367cf37e64SBarry Smith   PetscReal         nrm;
7037edbaebb3SBarry Smith   PetscBool         flg;
70387cf37e64SBarry Smith 
70397cf37e64SBarry Smith   PetscFunctionBegin;
70407cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70417cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70427cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7043edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7044edbaebb3SBarry Smith   if (flg) {
70457cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7046edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7047edbaebb3SBarry Smith   }
7048edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7049edbaebb3SBarry Smith   if (flg) {
7050edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7051edbaebb3SBarry Smith   }
7052edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70537cf37e64SBarry Smith   PetscFunctionReturn(0);
70547cf37e64SBarry Smith }
70557cf37e64SBarry Smith 
7056201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7057201da799SBarry Smith {
7058201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7059201da799SBarry Smith   PetscReal      x   = ptime,y;
7060201da799SBarry Smith   PetscErrorCode ierr;
7061201da799SBarry Smith   PetscInt       its;
7062201da799SBarry Smith 
7063201da799SBarry Smith   PetscFunctionBegin;
706463e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7065201da799SBarry Smith   if (!n) {
7066201da799SBarry Smith     PetscDrawAxis axis;
7067201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7068201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7069201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7070201da799SBarry Smith     ctx->snes_its = 0;
7071201da799SBarry Smith   }
7072201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7073201da799SBarry Smith   y    = its - ctx->snes_its;
7074201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70753fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7076201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70776934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7078201da799SBarry Smith   }
7079201da799SBarry Smith   ctx->snes_its = its;
7080201da799SBarry Smith   PetscFunctionReturn(0);
7081201da799SBarry Smith }
7082201da799SBarry Smith 
7083201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7084201da799SBarry Smith {
7085201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7086201da799SBarry Smith   PetscReal      x   = ptime,y;
7087201da799SBarry Smith   PetscErrorCode ierr;
7088201da799SBarry Smith   PetscInt       its;
7089201da799SBarry Smith 
7090201da799SBarry Smith   PetscFunctionBegin;
709163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7092201da799SBarry Smith   if (!n) {
7093201da799SBarry Smith     PetscDrawAxis axis;
7094201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7095201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7096201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7097201da799SBarry Smith     ctx->ksp_its = 0;
7098201da799SBarry Smith   }
7099201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7100201da799SBarry Smith   y    = its - ctx->ksp_its;
7101201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
710299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7103201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
71046934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7105201da799SBarry Smith   }
7106201da799SBarry Smith   ctx->ksp_its = its;
7107201da799SBarry Smith   PetscFunctionReturn(0);
7108201da799SBarry Smith }
7109f9c1d6abSBarry Smith 
7110f9c1d6abSBarry Smith /*@
7111f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7112f9c1d6abSBarry Smith 
7113d083f849SBarry Smith    Collective on TS
7114f9c1d6abSBarry Smith 
7115f9c1d6abSBarry Smith    Input Parameters:
7116f9c1d6abSBarry Smith +  ts - the TS context
7117f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7118f9c1d6abSBarry Smith 
7119f9c1d6abSBarry Smith    Output Parameters:
7120f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7121f9c1d6abSBarry Smith 
7122f9c1d6abSBarry Smith    Level: developer
7123f9c1d6abSBarry Smith 
7124f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7125f9c1d6abSBarry Smith @*/
7126f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7127f9c1d6abSBarry Smith {
7128f9c1d6abSBarry Smith   PetscErrorCode ierr;
7129f9c1d6abSBarry Smith 
7130f9c1d6abSBarry Smith   PetscFunctionBegin;
7131f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7132ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7133f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7134f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7135f9c1d6abSBarry Smith }
713624655328SShri 
7137b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7138b3d3934dSBarry Smith /*@C
7139b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7140b3d3934dSBarry Smith 
7141b3d3934dSBarry Smith    Collective on TS
7142b3d3934dSBarry Smith 
7143b3d3934dSBarry Smith    Input Parameters:
7144b3d3934dSBarry Smith .  ts  - the ODE solver object
7145b3d3934dSBarry Smith 
7146b3d3934dSBarry Smith    Output Parameter:
7147b3d3934dSBarry Smith .  ctx - the context
7148b3d3934dSBarry Smith 
7149b3d3934dSBarry Smith    Level: intermediate
7150b3d3934dSBarry Smith 
7151b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7152b3d3934dSBarry Smith 
7153b3d3934dSBarry Smith @*/
7154b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7155b3d3934dSBarry Smith {
7156b3d3934dSBarry Smith   PetscErrorCode ierr;
7157b3d3934dSBarry Smith 
7158b3d3934dSBarry Smith   PetscFunctionBegin;
7159a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7160b3d3934dSBarry Smith   PetscFunctionReturn(0);
7161b3d3934dSBarry Smith }
7162b3d3934dSBarry Smith 
7163b3d3934dSBarry Smith /*@C
7164b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7165b3d3934dSBarry Smith 
7166b3d3934dSBarry Smith    Collective on TS
7167b3d3934dSBarry Smith 
7168b3d3934dSBarry Smith    Input Parameters:
7169b3d3934dSBarry Smith +  ts - the TS context
7170b3d3934dSBarry Smith .  step - current time-step
7171b3d3934dSBarry Smith .  ptime - current time
71727db568b7SBarry Smith .  u  - current solution
71737db568b7SBarry Smith -  dctx - the envelope context
7174b3d3934dSBarry Smith 
7175b3d3934dSBarry Smith    Options Database:
7176b3d3934dSBarry Smith .  -ts_monitor_envelope
7177b3d3934dSBarry Smith 
7178b3d3934dSBarry Smith    Level: intermediate
7179b3d3934dSBarry Smith 
718095452b02SPatrick Sanan    Notes:
718195452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7182b3d3934dSBarry Smith 
71837db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7184b3d3934dSBarry Smith @*/
71857db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7186b3d3934dSBarry Smith {
7187b3d3934dSBarry Smith   PetscErrorCode       ierr;
71887db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7189b3d3934dSBarry Smith 
7190b3d3934dSBarry Smith   PetscFunctionBegin;
7191b3d3934dSBarry Smith   if (!ctx->max) {
7192b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7193b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7194b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7195b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7196b3d3934dSBarry Smith   } else {
7197b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7198b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7199b3d3934dSBarry Smith   }
7200b3d3934dSBarry Smith   PetscFunctionReturn(0);
7201b3d3934dSBarry Smith }
7202b3d3934dSBarry Smith 
7203b3d3934dSBarry Smith /*@C
7204b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7205b3d3934dSBarry Smith 
7206b3d3934dSBarry Smith    Collective on TS
7207b3d3934dSBarry Smith 
7208b3d3934dSBarry Smith    Input Parameter:
7209b3d3934dSBarry Smith .  ts - the TS context
7210b3d3934dSBarry Smith 
7211b3d3934dSBarry Smith    Output Parameter:
7212b3d3934dSBarry Smith +  max - the maximum values
7213b3d3934dSBarry Smith -  min - the minimum values
7214b3d3934dSBarry Smith 
721595452b02SPatrick Sanan    Notes:
721695452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
72177db568b7SBarry Smith 
7218b3d3934dSBarry Smith    Level: intermediate
7219b3d3934dSBarry Smith 
7220b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7221b3d3934dSBarry Smith @*/
7222b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7223b3d3934dSBarry Smith {
7224b3d3934dSBarry Smith   PetscInt i;
7225b3d3934dSBarry Smith 
7226b3d3934dSBarry Smith   PetscFunctionBegin;
7227b3d3934dSBarry Smith   if (max) *max = NULL;
7228b3d3934dSBarry Smith   if (min) *min = NULL;
7229b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7230b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
72315537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7232b3d3934dSBarry Smith       if (max) *max = ctx->max;
7233b3d3934dSBarry Smith       if (min) *min = ctx->min;
7234b3d3934dSBarry Smith       break;
7235b3d3934dSBarry Smith     }
7236b3d3934dSBarry Smith   }
7237b3d3934dSBarry Smith   PetscFunctionReturn(0);
7238b3d3934dSBarry Smith }
7239b3d3934dSBarry Smith 
7240b3d3934dSBarry Smith /*@C
7241b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7242b3d3934dSBarry Smith 
7243b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7244b3d3934dSBarry Smith 
7245b3d3934dSBarry Smith    Input Parameter:
7246b3d3934dSBarry Smith .  ctx - the monitor context
7247b3d3934dSBarry Smith 
7248b3d3934dSBarry Smith    Level: intermediate
7249b3d3934dSBarry Smith 
72507db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7251b3d3934dSBarry Smith @*/
7252b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7253b3d3934dSBarry Smith {
7254b3d3934dSBarry Smith   PetscErrorCode ierr;
7255b3d3934dSBarry Smith 
7256b3d3934dSBarry Smith   PetscFunctionBegin;
7257b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7258b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7259b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7260b3d3934dSBarry Smith   PetscFunctionReturn(0);
7261b3d3934dSBarry Smith }
7262f2dee214SBarry Smith 
726324655328SShri /*@
7264dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7265dcb233daSLisandro Dalcin 
7266dcb233daSLisandro Dalcin    Collective on TS
7267dcb233daSLisandro Dalcin 
7268dcb233daSLisandro Dalcin    Input Parameter:
7269dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7270dcb233daSLisandro Dalcin 
7271dcb233daSLisandro Dalcin    Level: advanced
7272dcb233daSLisandro Dalcin 
7273dcb233daSLisandro Dalcin    Notes:
7274dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7275dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7276dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7277dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7278dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7279dcb233daSLisandro Dalcin    discontinuous source terms).
7280dcb233daSLisandro Dalcin 
7281dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7282dcb233daSLisandro Dalcin @*/
7283dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7284dcb233daSLisandro Dalcin {
7285dcb233daSLisandro Dalcin   PetscFunctionBegin;
7286dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7287dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7288dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7289dcb233daSLisandro Dalcin }
7290dcb233daSLisandro Dalcin 
7291dcb233daSLisandro Dalcin /*@
729224655328SShri    TSRollBack - Rolls back one time step
729324655328SShri 
729424655328SShri    Collective on TS
729524655328SShri 
729624655328SShri    Input Parameter:
729724655328SShri .  ts - the TS context obtained from TSCreate()
729824655328SShri 
729924655328SShri    Level: advanced
730024655328SShri 
730124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
730224655328SShri @*/
730324655328SShri PetscErrorCode  TSRollBack(TS ts)
730424655328SShri {
730524655328SShri   PetscErrorCode ierr;
730624655328SShri 
730724655328SShri   PetscFunctionBegin;
730824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7309b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
731024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
731124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
731224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
731324655328SShri   ts->ptime = ts->ptime_prev;
7314be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
73152808aa04SLisandro Dalcin   ts->steps--;
7316b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
731724655328SShri   PetscFunctionReturn(0);
731824655328SShri }
7319aeb4809dSShri Abhyankar 
7320ff22ae23SHong Zhang /*@
7321ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7322ff22ae23SHong Zhang 
7323ff22ae23SHong Zhang    Input Parameter:
7324ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7325ff22ae23SHong Zhang 
73260429704eSStefano Zampini    Output Parameters:
73270429704eSStefano Zampini +  ns - the number of stages
73280429704eSStefano Zampini -  Y - the current stage vectors
73290429704eSStefano Zampini 
7330ff22ae23SHong Zhang    Level: advanced
7331ff22ae23SHong Zhang 
73320429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
73330429704eSStefano Zampini 
7334ff22ae23SHong Zhang .seealso: TSCreate()
7335ff22ae23SHong Zhang @*/
7336ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7337ff22ae23SHong Zhang {
7338ff22ae23SHong Zhang   PetscErrorCode ierr;
7339ff22ae23SHong Zhang 
7340ff22ae23SHong Zhang   PetscFunctionBegin;
7341ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
73420429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
73430429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
73440429704eSStefano Zampini   if (!ts->ops->getstages) {
73450429704eSStefano Zampini     if (ns) *ns = 0;
73460429704eSStefano Zampini     if (Y) *Y = NULL;
73470429704eSStefano Zampini   } else {
7348ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7349ff22ae23SHong Zhang   }
7350ff22ae23SHong Zhang   PetscFunctionReturn(0);
7351ff22ae23SHong Zhang }
7352ff22ae23SHong Zhang 
7353847ff0e1SMatthew G. Knepley /*@C
7354847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7355847ff0e1SMatthew G. Knepley 
7356847ff0e1SMatthew G. Knepley   Collective on SNES
7357847ff0e1SMatthew G. Knepley 
7358847ff0e1SMatthew G. Knepley   Input Parameters:
7359847ff0e1SMatthew G. Knepley + ts - the TS context
7360847ff0e1SMatthew G. Knepley . t - current timestep
7361847ff0e1SMatthew G. Knepley . U - state vector
7362847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7363847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7364847ff0e1SMatthew G. Knepley - ctx - an optional user context
7365847ff0e1SMatthew G. Knepley 
7366847ff0e1SMatthew G. Knepley   Output Parameters:
7367847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7368847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7369847ff0e1SMatthew G. Knepley 
7370847ff0e1SMatthew G. Knepley   Level: intermediate
7371847ff0e1SMatthew G. Knepley 
7372847ff0e1SMatthew G. Knepley   Notes:
7373847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7374847ff0e1SMatthew G. Knepley 
7375847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7376847ff0e1SMatthew G. Knepley 
7377847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7378847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7379847ff0e1SMatthew G. Knepley 
7380847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7381847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7382847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7383847ff0e1SMatthew G. Knepley 
7384847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7385847ff0e1SMatthew G. Knepley @*/
7386847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7387847ff0e1SMatthew G. Knepley {
7388847ff0e1SMatthew G. Knepley   SNES           snes;
7389847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7390847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7391847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7392847ff0e1SMatthew G. Knepley 
7393847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7394c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7395847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7396847ff0e1SMatthew G. Knepley   if (!color) {
7397847ff0e1SMatthew G. Knepley     DM         dm;
7398847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7399847ff0e1SMatthew G. Knepley 
7400847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7401847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7402847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7403847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7404847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7405847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7406847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7407847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7408847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7409847ff0e1SMatthew G. Knepley     } else {
7410847ff0e1SMatthew G. Knepley       MatColoring mc;
7411847ff0e1SMatthew G. Knepley 
7412847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7413847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7414847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7415847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7416847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7417847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7418847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7419847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7420847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7421847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7422847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7423847ff0e1SMatthew G. Knepley     }
7424847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7425847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7426847ff0e1SMatthew G. Knepley   }
7427847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7428847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7429847ff0e1SMatthew G. Knepley   if (J != B) {
7430847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7431847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7432847ff0e1SMatthew G. Knepley   }
7433847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7434847ff0e1SMatthew G. Knepley }
743593b34091SDebojyoti Ghosh 
7436cb9d8021SPierre Barbier de Reuille /*@
74376bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7438cb9d8021SPierre Barbier de Reuille 
7439cb9d8021SPierre Barbier de Reuille     Input Parameters:
74406bc98fa9SBarry Smith +    ts - the TS context
74416bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
74426bc98fa9SBarry Smith 
74436bc98fa9SBarry Smith     Calling sequence of func:
74446bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
74456bc98fa9SBarry Smith 
74466bc98fa9SBarry Smith +   ts - the TS context
74476bc98fa9SBarry Smith .   time - the current time (of the stage)
74486bc98fa9SBarry Smith .   state - the state to check if it is valid
74496bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7450cb9d8021SPierre Barbier de Reuille 
7451cb9d8021SPierre Barbier de Reuille     Level: intermediate
7452cb9d8021SPierre Barbier de Reuille 
74536bc98fa9SBarry Smith     Notes:
74546bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
74556bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
74566bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
74576bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
74586bc98fa9SBarry Smith 
74596bc98fa9SBarry Smith     Developer Notes:
74606bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
74616bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
74626bc98fa9SBarry Smith 
74636bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7464cb9d8021SPierre Barbier de Reuille @*/
7465cb9d8021SPierre Barbier de Reuille 
7466d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7467cb9d8021SPierre Barbier de Reuille {
7468cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7469cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7470cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7471cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7472cb9d8021SPierre Barbier de Reuille }
7473cb9d8021SPierre Barbier de Reuille 
7474cb9d8021SPierre Barbier de Reuille /*@
74756bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7476cb9d8021SPierre Barbier de Reuille 
7477cb9d8021SPierre Barbier de Reuille     Input Parameters:
74786bc98fa9SBarry Smith +    ts - the TS context
74796bc98fa9SBarry Smith .    stagetime - time of the simulation
74806bc98fa9SBarry Smith -    Y - state vector to check.
7481cb9d8021SPierre Barbier de Reuille 
7482cb9d8021SPierre Barbier de Reuille     Output Parameter:
74836bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7484cb9d8021SPierre Barbier de Reuille 
7485cb9d8021SPierre Barbier de Reuille     Note:
74866bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74876bc98fa9SBarry Smith     to check if the current state is valid.
748896a0c994SBarry Smith 
74896bc98fa9SBarry Smith     Level: developer
74906bc98fa9SBarry Smith 
74916bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7492cb9d8021SPierre Barbier de Reuille @*/
7493d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7494cb9d8021SPierre Barbier de Reuille {
7495cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7496cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7497cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7498cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7499d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7500cb9d8021SPierre Barbier de Reuille   }
7501cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7502cb9d8021SPierre Barbier de Reuille }
75031ceb14c0SBarry Smith 
750493b34091SDebojyoti Ghosh /*@C
7505e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
750693b34091SDebojyoti Ghosh 
7507d083f849SBarry Smith   Collective
750893b34091SDebojyoti Ghosh 
750993b34091SDebojyoti Ghosh   Input Parameter:
751093b34091SDebojyoti Ghosh . tsin    - The input TS
751193b34091SDebojyoti Ghosh 
751293b34091SDebojyoti Ghosh   Output Parameter:
7513e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
751493b34091SDebojyoti Ghosh 
75155eca1a21SEmil Constantinescu   Notes:
75165eca1a21SEmil 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.
75175eca1a21SEmil Constantinescu 
7518928bb9adSStefano 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);
75195eca1a21SEmil Constantinescu 
75205eca1a21SEmil Constantinescu   Level: developer
752193b34091SDebojyoti Ghosh 
7522e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
752393b34091SDebojyoti Ghosh @*/
7524baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
752593b34091SDebojyoti Ghosh {
752693b34091SDebojyoti Ghosh   TS             t;
752793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7528dc846ba4SSatish Balay   SNES           snes_start;
7529dc846ba4SSatish Balay   DM             dm;
7530dc846ba4SSatish Balay   TSType         type;
753193b34091SDebojyoti Ghosh 
753293b34091SDebojyoti Ghosh   PetscFunctionBegin;
753393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
753493b34091SDebojyoti Ghosh   *tsout = NULL;
753593b34091SDebojyoti Ghosh 
75367a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
753793b34091SDebojyoti Ghosh 
753893b34091SDebojyoti Ghosh   /* General TS description */
753993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
754093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
754193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
754293b34091SDebojyoti Ghosh   t->snes_its          = 0;
754393b34091SDebojyoti Ghosh   t->nwork             = 0;
75447d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
754593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
754693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
754793b34091SDebojyoti Ghosh 
754834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
754934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7550d15a3a53SEmil Constantinescu 
755193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
755293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
755393b34091SDebojyoti Ghosh 
755493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
755551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
755693b34091SDebojyoti Ghosh 
7557e7069c78SShri   t->trajectory = tsin->trajectory;
7558e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7559e7069c78SShri 
7560e7069c78SShri   t->event = tsin->event;
75616b10a48eSSatish Balay   if (t->event) t->event->refct++;
7562e7069c78SShri 
756393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
756493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7565e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
756693b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
756793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
756893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
756993b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
757093b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
757193b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
757293b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
757393b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
757493b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
757593b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
757693b34091SDebojyoti Ghosh 
757793b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
757893b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
757993b34091SDebojyoti Ghosh 
758093b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
758193b34091SDebojyoti Ghosh 
758293b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
758393b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
758493b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
758593b34091SDebojyoti Ghosh 
758693b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
758793b34091SDebojyoti Ghosh 
75880d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75890d4fed19SBarry Smith     PetscInt i;
75900d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75910d4fed19SBarry Smith     for (i=0; i<10; i++) {
75920d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75930d4fed19SBarry Smith     }
75940d4fed19SBarry Smith   }
759593b34091SDebojyoti Ghosh   *tsout = t;
759693b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
759793b34091SDebojyoti Ghosh }
7598f3b1f45cSBarry Smith 
7599f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7600f3b1f45cSBarry Smith {
7601f3b1f45cSBarry Smith   PetscErrorCode ierr;
7602f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7603f3b1f45cSBarry Smith 
7604f3b1f45cSBarry Smith   PetscFunctionBegin;
7605f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7606f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7607f3b1f45cSBarry Smith }
7608f3b1f45cSBarry Smith 
7609f3b1f45cSBarry Smith /*@
7610f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7611f3b1f45cSBarry Smith 
7612d083f849SBarry Smith    Logically Collective on TS
7613f3b1f45cSBarry Smith 
7614f3b1f45cSBarry Smith     Input Parameters:
7615f3b1f45cSBarry Smith     TS - the time stepping routine
7616f3b1f45cSBarry Smith 
7617f3b1f45cSBarry Smith    Output Parameter:
7618f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7619f3b1f45cSBarry Smith 
7620f3b1f45cSBarry Smith    Options Database:
7621f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7622f3b1f45cSBarry Smith 
7623f3b1f45cSBarry Smith    Level: advanced
7624f3b1f45cSBarry Smith 
762595452b02SPatrick Sanan    Notes:
762695452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7627f3b1f45cSBarry Smith 
7628f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7629f3b1f45cSBarry Smith @*/
7630f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7631f3b1f45cSBarry Smith {
7632f3b1f45cSBarry Smith   Mat            J,B;
7633f3b1f45cSBarry Smith   PetscErrorCode ierr;
7634f3b1f45cSBarry Smith   TSRHSJacobian  func;
7635f3b1f45cSBarry Smith   void*          ctx;
7636f3b1f45cSBarry Smith 
7637f3b1f45cSBarry Smith   PetscFunctionBegin;
7638f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7639f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7640f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7641f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7642f3b1f45cSBarry Smith }
7643f3b1f45cSBarry Smith 
7644f3b1f45cSBarry Smith /*@C
7645f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7646f3b1f45cSBarry Smith 
7647d083f849SBarry Smith    Logically Collective on TS
7648f3b1f45cSBarry Smith 
7649f3b1f45cSBarry Smith     Input Parameters:
7650f3b1f45cSBarry Smith     TS - the time stepping routine
7651f3b1f45cSBarry Smith 
7652f3b1f45cSBarry Smith    Output Parameter:
7653f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7654f3b1f45cSBarry Smith 
7655f3b1f45cSBarry Smith    Options Database:
7656f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7657f3b1f45cSBarry Smith 
765895452b02SPatrick Sanan    Notes:
765995452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7660f3b1f45cSBarry Smith 
7661f3b1f45cSBarry Smith    Level: advanced
7662f3b1f45cSBarry Smith 
7663f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7664f3b1f45cSBarry Smith @*/
7665f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7666f3b1f45cSBarry Smith {
7667f3b1f45cSBarry Smith   Mat            J,B;
7668f3b1f45cSBarry Smith   PetscErrorCode ierr;
7669f3b1f45cSBarry Smith   void           *ctx;
7670f3b1f45cSBarry Smith   TSRHSJacobian  func;
7671f3b1f45cSBarry Smith 
7672f3b1f45cSBarry Smith   PetscFunctionBegin;
7673f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7674f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7675f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7676f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7677f3b1f45cSBarry Smith }
76780fe4d17eSHong Zhang 
76790fe4d17eSHong Zhang /*@
76800fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
76810fe4d17eSHong Zhang 
76820fe4d17eSHong Zhang   Logically collective
76830fe4d17eSHong Zhang 
76840fe4d17eSHong Zhang   Input Parameter:
76850fe4d17eSHong Zhang +  ts - timestepping context
76860fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76870fe4d17eSHong Zhang 
76880fe4d17eSHong Zhang   Options Database:
76890fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76900fe4d17eSHong Zhang 
76910fe4d17eSHong Zhang   Notes:
76920fe4d17eSHong Zhang     This is only useful for multirate methods
76930fe4d17eSHong Zhang 
76940fe4d17eSHong Zhang   Level: intermediate
76950fe4d17eSHong Zhang 
76960fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
76970fe4d17eSHong Zhang @*/
76980fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
76990fe4d17eSHong Zhang {
77000fe4d17eSHong Zhang   PetscFunctionBegin;
77010fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77020fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
77030fe4d17eSHong Zhang   PetscFunctionReturn(0);
77040fe4d17eSHong Zhang }
77050fe4d17eSHong Zhang 
77060fe4d17eSHong Zhang /*@
77070fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
77080fe4d17eSHong Zhang 
77090fe4d17eSHong Zhang   Not collective
77100fe4d17eSHong Zhang 
77110fe4d17eSHong Zhang   Input Parameter:
77120fe4d17eSHong Zhang .  ts - timestepping context
77130fe4d17eSHong Zhang 
77140fe4d17eSHong Zhang   Output Parameter:
77150fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
77160fe4d17eSHong Zhang 
77170fe4d17eSHong Zhang   Level: intermediate
77180fe4d17eSHong Zhang 
77190fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
77200fe4d17eSHong Zhang @*/
77210fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
77220fe4d17eSHong Zhang {
77230fe4d17eSHong Zhang   PetscFunctionBegin;
77240fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77250fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
77260fe4d17eSHong Zhang   PetscFunctionReturn(0);
77270fe4d17eSHong Zhang }
7728d60b7d5cSBarry Smith 
7729d60b7d5cSBarry Smith /*@
7730d60b7d5cSBarry Smith     TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix.
7731d60b7d5cSBarry Smith 
7732d60b7d5cSBarry Smith    Logically  Collective on ts
7733d60b7d5cSBarry Smith 
7734d60b7d5cSBarry Smith    Input Parameters:
7735d60b7d5cSBarry Smith +  ts - the time-stepper
7736d60b7d5cSBarry Smith -  str - the structure (the default is UNKNOWN_NONZERO_PATTERN)
7737d60b7d5cSBarry Smith 
7738d60b7d5cSBarry Smith    Level: intermediate
7739d60b7d5cSBarry Smith 
7740d60b7d5cSBarry Smith    Notes:
7741d60b7d5cSBarry Smith      When the relationship between the nonzero structures is known and supplied the solution process can be much faster
7742d60b7d5cSBarry Smith 
7743d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure
7744d60b7d5cSBarry Smith  @*/
7745d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str)
7746d60b7d5cSBarry Smith {
7747d60b7d5cSBarry Smith   PetscFunctionBegin;
7748d60b7d5cSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7749d60b7d5cSBarry Smith   ts->axpy_pattern = str;
7750d60b7d5cSBarry Smith   PetscFunctionReturn(0);
7751d60b7d5cSBarry Smith }
7752