xref: /petsc/src/ts/interface/ts.c (revision aee7a9fbfae82b8c1518c378ea55a980a207c5a6)
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
101*aee7a9fbSMatthew G. Knepley .  -ts_monitor_cancel - Cancel all monitors
102de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
103de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1047cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1056934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1068b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
107de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
108de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
109de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
110de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1113e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1123e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
113fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
114e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
1159e336e28SPatrick Sanan -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11653ea634cSHong Zhang 
1173d5a8a6aSBarry Smith    Notes:
1183d5a8a6aSBarry Smith      See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper.
1193d5a8a6aSBarry Smith 
1203d5a8a6aSBarry Smith      Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order
1213d5a8a6aSBarry Smith      to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and
1223d5a8a6aSBarry Smith      -snes_lag_preconditioner_persists true
1233d5a8a6aSBarry Smith 
1249e336e28SPatrick Sanan    Developer Note:
1259e336e28SPatrick Sanan      We should unify all the -ts_monitor options in the way that -xxx_view has been unified
126bdad233fSMatthew Knepley 
127bdad233fSMatthew Knepley    Level: beginner
128bdad233fSMatthew Knepley 
129a313700dSBarry Smith .seealso: TSGetType()
130bdad233fSMatthew Knepley @*/
1317087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
132bdad233fSMatthew Knepley {
133bc952696SBarry Smith   PetscBool              opt,flg,tflg;
134dfbe8321SBarry Smith   PetscErrorCode         ierr;
135eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
13631748224SBarry Smith   PetscReal              time_step;
13749354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
138d1212d36SBarry Smith   char                   dir[16];
139cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1406991f827SBarry Smith   const char             *defaultType;
1416991f827SBarry Smith   char                   typeName[256];
142bdad233fSMatthew Knepley 
143bdad233fSMatthew Knepley   PetscFunctionBegin;
1440700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1456991f827SBarry Smith 
1466991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
147cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
148cd11d68dSLisandro Dalcin 
149cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1501ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1511ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1526991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1536991f827SBarry Smith   if (opt) {
1546991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1556991f827SBarry Smith   } else {
1566991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1576991f827SBarry Smith   }
158bdad233fSMatthew Knepley 
159bdad233fSMatthew Knepley   /* Handle generic TS options */
1604dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
161ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
16219eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1630298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
16431748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
165cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
16649354f04SShri 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);
16749354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1680298fd71SBarry 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);
1690298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1700298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1710298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1720298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
173bdad233fSMatthew Knepley 
174f3b1f45cSBarry 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);
175f3b1f45cSBarry 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);
1760fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr);
17756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
17856f85f32SBarry Smith   {
17956f85f32SBarry Smith     PetscBool set;
18056f85f32SBarry Smith     flg  = PETSC_FALSE;
18156f85f32SBarry Smith     ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
18256f85f32SBarry Smith     if (set) {
18356f85f32SBarry Smith       ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
18456f85f32SBarry Smith     }
18556f85f32SBarry Smith   }
18656f85f32SBarry Smith #endif
18756f85f32SBarry Smith 
188bdad233fSMatthew Knepley   /* Monitor options */
189cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
190cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
191fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
192fde5950dSBarry Smith 
193589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
1945180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1955180491cSLisandro Dalcin 
1964f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
197b3603a34SBarry Smith   if (opt) {
1983923b477SBarry Smith     PetscInt       howoften = 1;
199e669de00SBarry Smith     DM             dm;
200e669de00SBarry Smith     PetscBool      net;
201b3603a34SBarry Smith 
2020298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
203e669de00SBarry Smith     ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
204e669de00SBarry Smith     ierr = PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net);CHKERRQ(ierr);
205e669de00SBarry Smith     if (net) {
206e669de00SBarry Smith       TSMonitorLGCtxNetwork ctx;
207e669de00SBarry Smith       ierr = TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
208e669de00SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy);CHKERRQ(ierr);
209e669de00SBarry Smith       ierr = PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL);CHKERRQ(ierr);
210e669de00SBarry Smith     } else {
211e669de00SBarry Smith       TSMonitorLGCtx ctx;
212c793f718SLisandro Dalcin       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2134f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
214bdad233fSMatthew Knepley     }
215e669de00SBarry Smith   }
2166ba87a44SLisandro Dalcin 
2174f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
218ef20d060SBarry Smith   if (opt) {
2190b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2203923b477SBarry Smith     PetscInt       howoften = 1;
221ef20d060SBarry Smith 
2220298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
223c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2244f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
225ef20d060SBarry Smith   }
226edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2277cf37e64SBarry Smith 
2286934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2296934998bSLisandro Dalcin   if (opt) {
2306934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2316934998bSLisandro Dalcin     PetscInt       howoften = 1;
2326934998bSLisandro Dalcin 
2336934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2346ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2356934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2366934998bSLisandro Dalcin   }
2378b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2388b668821SLisandro Dalcin   if (opt) {
2398b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2408b668821SLisandro Dalcin     PetscInt       howoften = 1;
2418b668821SLisandro Dalcin 
2428b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2438b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2448b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2458b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2468b668821SLisandro Dalcin   }
2478b668821SLisandro Dalcin 
248201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
249201da799SBarry Smith   if (opt) {
250201da799SBarry Smith     TSMonitorLGCtx ctx;
251201da799SBarry Smith     PetscInt       howoften = 1;
252201da799SBarry Smith 
2530298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2546ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
255201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
256201da799SBarry Smith   }
257201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
258201da799SBarry Smith   if (opt) {
259201da799SBarry Smith     TSMonitorLGCtx ctx;
260201da799SBarry Smith     PetscInt       howoften = 1;
261201da799SBarry Smith 
2620298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2636ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
264201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
265201da799SBarry Smith   }
2668189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2678189c53fSBarry Smith   if (opt) {
2688189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2698189c53fSBarry Smith     PetscInt          howoften = 1;
2708189c53fSBarry Smith 
2710298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
272c793f718SLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2738189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2748189c53fSBarry Smith   }
2750ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2761b575b74SJoseph Pusztay   if (opt) {
2771b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2781b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2790ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2801b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2810ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2821b575b74SJoseph Pusztay   }
283ef20d060SBarry Smith   opt  = PETSC_FALSE;
2840dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
285a7cc72afSBarry Smith   if (opt) {
28683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28783a4ac43SBarry Smith     PetscInt         howoften = 1;
288a80ad3e0SBarry Smith 
2890298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
290c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
29183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
292bdad233fSMatthew Knepley   }
293fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2942d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2952d5ee99bSBarry Smith   if (opt) {
2962d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2972d5ee99bSBarry Smith     PetscReal        bounds[4];
2982d5ee99bSBarry Smith     PetscInt         n = 4;
2992d5ee99bSBarry Smith     PetscDraw        draw;
3006934998bSLisandro Dalcin     PetscDrawAxis    axis;
3012d5ee99bSBarry Smith 
3022d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3032d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
304c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3052d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3066934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3076934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3086934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3092d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3102d5ee99bSBarry Smith   }
3112d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3120dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3133a471f94SBarry Smith   if (opt) {
31483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
31583a4ac43SBarry Smith     PetscInt         howoften = 1;
3163a471f94SBarry Smith 
3170298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
318c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
31983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3203a471f94SBarry Smith   }
3210ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3220ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3230ed3bfb6SBarry Smith   if (opt) {
3240ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3250ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3260ed3bfb6SBarry Smith 
3270ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
328c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3290ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3300ed3bfb6SBarry Smith   }
331fde5950dSBarry Smith 
332ed81e22dSJed Brown   opt  = PETSC_FALSE;
333589a23caSBarry 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);
334ed81e22dSJed Brown   if (flg) {
335ed81e22dSJed Brown     const char *ptr,*ptr2;
336ed81e22dSJed Brown     char       *filetemplate;
337ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
338ed81e22dSJed Brown     /* Do some cursory validation of the input. */
339ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
340ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
341ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
342ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
343ce94432eSBarry 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");
344ed81e22dSJed Brown       if (ptr2) break;
345ed81e22dSJed Brown     }
346ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
347ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
348ed81e22dSJed Brown   }
349bdad233fSMatthew Knepley 
350589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
351d1212d36SBarry Smith   if (flg) {
352d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
353d1212d36SBarry Smith     int                  ray = 0;
3543ee9839eSMatthew G. Knepley     DMDirection          ddir;
355d1212d36SBarry Smith     DM                   da;
356d1212d36SBarry Smith     PetscMPIInt          rank;
357d1212d36SBarry Smith 
358ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3593ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3603ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
361ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
362d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
363d1212d36SBarry Smith 
3645bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr);
365b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
366d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
367d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
368ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRMPI(ierr);
369d1212d36SBarry Smith     if (!rank) {
370c793f718SLisandro Dalcin       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
371d1212d36SBarry Smith     }
37251b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
373d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
374d1212d36SBarry Smith   }
375589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
37651b4a12fSMatthew G. Knepley   if (flg) {
37751b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37851b4a12fSMatthew G. Knepley     int                 ray = 0;
3793ee9839eSMatthew G. Knepley     DMDirection         ddir;
38051b4a12fSMatthew G. Knepley     DM                  da;
38151b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
382d1212d36SBarry Smith 
38351b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3843ee9839eSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DM_X;
3853ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
38651b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
38751b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3881c3436cfSJed Brown 
3895bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr);
390b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
39151b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
39251b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
393c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
39451b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
39551b4a12fSMatthew G. Knepley   }
396a7a1495cSBarry Smith 
397b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
398b3d3934dSBarry Smith   if (opt) {
399b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
400b3d3934dSBarry Smith 
401b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
402b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
403b3d3934dSBarry Smith   }
404*aee7a9fbSMatthew G. Knepley   flg  = PETSC_FALSE;
405*aee7a9fbSMatthew G. Knepley   ierr = PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt);CHKERRQ(ierr);
406*aee7a9fbSMatthew G. Knepley   if (opt && flg) {ierr = TSMonitorCancel(ts);CHKERRQ(ierr);}
407b3d3934dSBarry Smith 
408847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
409847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
410847ff0e1SMatthew G. Knepley   if (flg) {
411847ff0e1SMatthew G. Knepley     DM   dm;
412847ff0e1SMatthew G. Knepley     DMTS tdm;
413847ff0e1SMatthew G. Knepley 
414847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
415847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
416847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
417c793f718SLisandro Dalcin     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL);CHKERRQ(ierr);
418847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
419847ff0e1SMatthew G. Knepley   }
420847ff0e1SMatthew G. Knepley 
421d763cef2SBarry Smith   /* Handle specific TS options */
422d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4236991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
424d763cef2SBarry Smith   }
425fbc52257SHong Zhang 
426a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
427a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
428a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
429a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
430a7bdc993SLisandro Dalcin 
43168bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4324f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
43368bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
43468bece0bSHong Zhang   if (tflg) {
43568bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43668bece0bSHong Zhang   }
437a05bf03eSHong Zhang 
438a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
439d763cef2SBarry Smith 
440d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4410633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
442fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
443d763cef2SBarry Smith 
4444f122a70SLisandro Dalcin   if (ts->trajectory) {
4454f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
446d763cef2SBarry Smith   }
44768bece0bSHong Zhang 
4481ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4494f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4501ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4511ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4521ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4531ef27442SStefano Zampini   }
4544f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
455d763cef2SBarry Smith   PetscFunctionReturn(0);
456d763cef2SBarry Smith }
457d763cef2SBarry Smith 
458d2daff3dSHong Zhang /*@
45978fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
46078fbdcc8SBarry Smith 
46178fbdcc8SBarry Smith    Collective on TS
46278fbdcc8SBarry Smith 
46378fbdcc8SBarry Smith    Input Parameters:
46478fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
46578fbdcc8SBarry Smith 
4667a7aea1fSJed Brown    Output Parameters:
46778fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
46878fbdcc8SBarry Smith 
46978fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
47078fbdcc8SBarry Smith 
47178fbdcc8SBarry Smith    Level: advanced
47278fbdcc8SBarry Smith 
47378fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
47478fbdcc8SBarry Smith 
47578fbdcc8SBarry Smith @*/
47678fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
47778fbdcc8SBarry Smith {
47878fbdcc8SBarry Smith   PetscFunctionBegin;
47978fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48078fbdcc8SBarry Smith   *tr = ts->trajectory;
48178fbdcc8SBarry Smith   PetscFunctionReturn(0);
48278fbdcc8SBarry Smith }
48378fbdcc8SBarry Smith 
48478fbdcc8SBarry Smith /*@
485bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
486d2daff3dSHong Zhang 
487d2daff3dSHong Zhang    Collective on TS
488d2daff3dSHong Zhang 
489d2daff3dSHong Zhang    Input Parameters:
490bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
491bc952696SBarry Smith 
49278fbdcc8SBarry Smith    Options Database:
49378fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
49478fbdcc8SBarry Smith -  -ts_trajectory_type type
49578fbdcc8SBarry Smith 
49668bece0bSHong Zhang Note: This routine should be called after all TS options have been set
497d2daff3dSHong Zhang 
498c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
49978fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
50078fbdcc8SBarry Smith 
501d2daff3dSHong Zhang    Level: intermediate
502d2daff3dSHong Zhang 
5032d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
504d2daff3dSHong Zhang 
505d2daff3dSHong Zhang @*/
506bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
507d2daff3dSHong Zhang {
508bc952696SBarry Smith   PetscErrorCode ierr;
509bc952696SBarry Smith 
510d2daff3dSHong Zhang   PetscFunctionBegin;
511d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
512bc952696SBarry Smith   if (!ts->trajectory) {
513bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
514bc952696SBarry Smith   }
515d2daff3dSHong Zhang   PetscFunctionReturn(0);
516d2daff3dSHong Zhang }
517d2daff3dSHong Zhang 
518a7a1495cSBarry Smith /*@
5192d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
5202d29f1f2SStefano Zampini 
5212d29f1f2SStefano Zampini    Collective on TS
5222d29f1f2SStefano Zampini 
5232d29f1f2SStefano Zampini    Input Parameters:
5242d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5252d29f1f2SStefano Zampini 
5262d29f1f2SStefano Zampini    Level: intermediate
5272d29f1f2SStefano Zampini 
5282d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5292d29f1f2SStefano Zampini 
5302d29f1f2SStefano Zampini @*/
5312d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5322d29f1f2SStefano Zampini {
5332d29f1f2SStefano Zampini   PetscErrorCode ierr;
5342d29f1f2SStefano Zampini 
5352d29f1f2SStefano Zampini   PetscFunctionBegin;
5362d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5372d29f1f2SStefano Zampini   if (ts->trajectory) {
5382d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5392d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5402d29f1f2SStefano Zampini   }
5412d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5422d29f1f2SStefano Zampini }
5432d29f1f2SStefano Zampini 
5442d29f1f2SStefano Zampini /*@
545a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
546a7a1495cSBarry Smith       set with TSSetRHSJacobian().
547a7a1495cSBarry Smith 
548d083f849SBarry Smith    Collective on TS
549a7a1495cSBarry Smith 
550a7a1495cSBarry Smith    Input Parameters:
551316643e7SJed Brown +  ts - the TS context
552a7a1495cSBarry Smith .  t - current timestep
5530910c330SBarry Smith -  U - input vector
554a7a1495cSBarry Smith 
555a7a1495cSBarry Smith    Output Parameters:
556a7a1495cSBarry Smith +  A - Jacobian matrix
557a7a1495cSBarry Smith .  B - optional preconditioning matrix
558a7a1495cSBarry Smith -  flag - flag indicating matrix structure
559a7a1495cSBarry Smith 
560a7a1495cSBarry Smith    Notes:
561a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
562a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
563a7a1495cSBarry Smith 
56494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
565a7a1495cSBarry Smith    flag parameter.
566a7a1495cSBarry Smith 
567a7a1495cSBarry Smith    Level: developer
568a7a1495cSBarry Smith 
56994b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
570a7a1495cSBarry Smith @*/
571d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
572a7a1495cSBarry Smith {
573dfbe8321SBarry Smith   PetscErrorCode   ierr;
574270bf2e7SJed Brown   PetscObjectState Ustate;
5756c1e1eecSBarry Smith   PetscObjectId    Uid;
57624989b8cSPeter Brune   DM               dm;
577942e3340SBarry Smith   DMTS             tsdm;
57824989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
57924989b8cSPeter Brune   void             *ctx;
580b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
581a7a1495cSBarry Smith 
582a7a1495cSBarry Smith   PetscFunctionBegin;
5830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5840910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5850910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
58624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
587942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
5886374d434SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
5892663174eSHong Zhang   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
59059e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5916c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
592971015bcSStefano Zampini 
5932663174eSHong Zhang   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0);
594d90be118SSean Farley 
595ae692cf2SHong 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);
59624989b8cSPeter Brune   if (rhsjacobianfunc) {
59794ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
598a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
599d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
600a7a1495cSBarry Smith     PetscStackPop;
601a6ab3590SBarry Smith     ts->rhsjacs++;
60294ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
603ef66eb69SBarry Smith   } else {
60494ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
605971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
606ef66eb69SBarry Smith   }
6070e4ef248SJed Brown   ts->rhsjacobian.time  = t;
608971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
609971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6107f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
61159e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
612a7a1495cSBarry Smith   PetscFunctionReturn(0);
613a7a1495cSBarry Smith }
614a7a1495cSBarry Smith 
615316643e7SJed Brown /*@
616d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
617d763cef2SBarry Smith 
618d083f849SBarry Smith    Collective on TS
619316643e7SJed Brown 
620316643e7SJed Brown    Input Parameters:
621316643e7SJed Brown +  ts - the TS context
622316643e7SJed Brown .  t - current time
6230910c330SBarry Smith -  U - state vector
624316643e7SJed Brown 
625316643e7SJed Brown    Output Parameter:
626316643e7SJed Brown .  y - right hand side
627316643e7SJed Brown 
628316643e7SJed Brown    Note:
629316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
630316643e7SJed Brown    is used internally within the nonlinear solvers.
631316643e7SJed Brown 
632316643e7SJed Brown    Level: developer
633316643e7SJed Brown 
634316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
635316643e7SJed Brown @*/
6360910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
637d763cef2SBarry Smith {
638dfbe8321SBarry Smith   PetscErrorCode ierr;
63924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
64024989b8cSPeter Brune   TSIFunction    ifunction;
64124989b8cSPeter Brune   void           *ctx;
64224989b8cSPeter Brune   DM             dm;
64324989b8cSPeter Brune 
644d763cef2SBarry Smith   PetscFunctionBegin;
6450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6470700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
64824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
64924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6500298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
651d763cef2SBarry Smith 
652ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
653d763cef2SBarry Smith 
65424989b8cSPeter Brune   if (rhsfunction) {
655a6ab3590SBarry Smith     ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
6561be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
657d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6580910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
659d763cef2SBarry Smith     PetscStackPop;
6601be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
661a6ab3590SBarry Smith     ts->rhsfuncs++;
662a6ab3590SBarry Smith     ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
663214bc6a2SJed Brown   } else {
664214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
665b2cd27e8SJed Brown   }
666d763cef2SBarry Smith   PetscFunctionReturn(0);
667d763cef2SBarry Smith }
668d763cef2SBarry Smith 
669ef20d060SBarry Smith /*@
670ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
671ef20d060SBarry Smith 
672d083f849SBarry Smith    Collective on TS
673ef20d060SBarry Smith 
674ef20d060SBarry Smith    Input Parameters:
675ef20d060SBarry Smith +  ts - the TS context
676ef20d060SBarry Smith -  t - current time
677ef20d060SBarry Smith 
678ef20d060SBarry Smith    Output Parameter:
6790910c330SBarry Smith .  U - the solution
680ef20d060SBarry Smith 
681ef20d060SBarry Smith    Note:
682ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
683ef20d060SBarry Smith    is used internally within the nonlinear solvers.
684ef20d060SBarry Smith 
685ef20d060SBarry Smith    Level: developer
686ef20d060SBarry Smith 
687abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
688ef20d060SBarry Smith @*/
6890910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
690ef20d060SBarry Smith {
691ef20d060SBarry Smith   PetscErrorCode     ierr;
692ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
693ef20d060SBarry Smith   void               *ctx;
694ef20d060SBarry Smith   DM                 dm;
695ef20d060SBarry Smith 
696ef20d060SBarry Smith   PetscFunctionBegin;
697ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6980910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
699ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
700ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
701ef20d060SBarry Smith 
702ef20d060SBarry Smith   if (solutionfunction) {
7039b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7040910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
705ef20d060SBarry Smith     PetscStackPop;
706ef20d060SBarry Smith   }
707ef20d060SBarry Smith   PetscFunctionReturn(0);
708ef20d060SBarry Smith }
7099b7cd975SBarry Smith /*@
7109b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7119b7cd975SBarry Smith 
712d083f849SBarry Smith    Collective on TS
7139b7cd975SBarry Smith 
7149b7cd975SBarry Smith    Input Parameters:
7159b7cd975SBarry Smith +  ts - the TS context
7169b7cd975SBarry Smith -  t - current time
7179b7cd975SBarry Smith 
7189b7cd975SBarry Smith    Output Parameter:
7199b7cd975SBarry Smith .  U - the function value
7209b7cd975SBarry Smith 
7219b7cd975SBarry Smith    Note:
7229b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7239b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7249b7cd975SBarry Smith 
7259b7cd975SBarry Smith    Level: developer
7269b7cd975SBarry Smith 
7279b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7289b7cd975SBarry Smith @*/
7299b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7309b7cd975SBarry Smith {
7319b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7329b7cd975SBarry Smith   void               *ctx;
7339b7cd975SBarry Smith   DM                 dm;
7349b7cd975SBarry Smith 
7359b7cd975SBarry Smith   PetscFunctionBegin;
7369b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7379b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7389b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7399b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7409b7cd975SBarry Smith 
7419b7cd975SBarry Smith   if (forcing) {
7429b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7439b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7449b7cd975SBarry Smith     PetscStackPop;
7459b7cd975SBarry Smith   }
7469b7cd975SBarry Smith   PetscFunctionReturn(0);
7479b7cd975SBarry Smith }
748ef20d060SBarry Smith 
749214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
750214bc6a2SJed Brown {
7512dd45cf8SJed Brown   Vec            F;
752214bc6a2SJed Brown   PetscErrorCode ierr;
753214bc6a2SJed Brown 
754214bc6a2SJed Brown   PetscFunctionBegin;
7550298fd71SBarry Smith   *Frhs = NULL;
7560298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
757214bc6a2SJed Brown   if (!ts->Frhs) {
7582dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
759214bc6a2SJed Brown   }
760214bc6a2SJed Brown   *Frhs = ts->Frhs;
761214bc6a2SJed Brown   PetscFunctionReturn(0);
762214bc6a2SJed Brown }
763214bc6a2SJed Brown 
764971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
765214bc6a2SJed Brown {
766214bc6a2SJed Brown   Mat            A,B;
7672dd45cf8SJed Brown   PetscErrorCode ierr;
76841a1d4d2SBarry Smith   TSIJacobian    ijacobian;
769214bc6a2SJed Brown 
770214bc6a2SJed Brown   PetscFunctionBegin;
771c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
772c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
77341a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
774214bc6a2SJed Brown   if (Arhs) {
775214bc6a2SJed Brown     if (!ts->Arhs) {
77641a1d4d2SBarry Smith       if (ijacobian) {
777214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
778d60b7d5cSBarry Smith         ierr = TSSetMatStructure(ts,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
77941a1d4d2SBarry Smith       } else {
78041a1d4d2SBarry Smith         ts->Arhs = A;
78141a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
78241a1d4d2SBarry Smith       }
7833565c898SBarry Smith     } else {
7843565c898SBarry Smith       PetscBool flg;
7853565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7863565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7873565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7883565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7893565c898SBarry Smith         ts->Arhs = A;
7903565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7913565c898SBarry Smith       }
792214bc6a2SJed Brown     }
793214bc6a2SJed Brown     *Arhs = ts->Arhs;
794214bc6a2SJed Brown   }
795214bc6a2SJed Brown   if (Brhs) {
796214bc6a2SJed Brown     if (!ts->Brhs) {
797bdb70873SJed Brown       if (A != B) {
79841a1d4d2SBarry Smith         if (ijacobian) {
799214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
800bdb70873SJed Brown         } else {
80141a1d4d2SBarry Smith           ts->Brhs = B;
80241a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
80341a1d4d2SBarry Smith         }
80441a1d4d2SBarry Smith       } else {
805bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
80651699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
807bdb70873SJed Brown       }
808214bc6a2SJed Brown     }
809214bc6a2SJed Brown     *Brhs = ts->Brhs;
810214bc6a2SJed Brown   }
811214bc6a2SJed Brown   PetscFunctionReturn(0);
812214bc6a2SJed Brown }
813214bc6a2SJed Brown 
814316643e7SJed Brown /*@
8150910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
816316643e7SJed Brown 
817d083f849SBarry Smith    Collective on TS
818316643e7SJed Brown 
819316643e7SJed Brown    Input Parameters:
820316643e7SJed Brown +  ts - the TS context
821316643e7SJed Brown .  t - current time
8220910c330SBarry Smith .  U - state vector
8230910c330SBarry Smith .  Udot - time derivative of state vector
824214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
825316643e7SJed Brown 
826316643e7SJed Brown    Output Parameter:
827316643e7SJed Brown .  Y - right hand side
828316643e7SJed Brown 
829316643e7SJed Brown    Note:
830316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
831316643e7SJed Brown    is used internally within the nonlinear solvers.
832316643e7SJed Brown 
833316643e7SJed Brown    If the user did did not write their equations in implicit form, this
834316643e7SJed Brown    function recasts them in implicit form.
835316643e7SJed Brown 
836316643e7SJed Brown    Level: developer
837316643e7SJed Brown 
838316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
839316643e7SJed Brown @*/
8400910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
841316643e7SJed Brown {
842316643e7SJed Brown   PetscErrorCode ierr;
84324989b8cSPeter Brune   TSIFunction    ifunction;
84424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
84524989b8cSPeter Brune   void           *ctx;
84624989b8cSPeter Brune   DM             dm;
847316643e7SJed Brown 
848316643e7SJed Brown   PetscFunctionBegin;
8490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8500910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8510910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8520700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
853316643e7SJed Brown 
85424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
85524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8560298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
85724989b8cSPeter Brune 
858ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
859d90be118SSean Farley 
8600910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
86124989b8cSPeter Brune   if (ifunction) {
862316643e7SJed Brown     PetscStackPush("TS user implicit function");
8630910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
864316643e7SJed Brown     PetscStackPop;
865a6ab3590SBarry Smith     ts->ifuncs++;
866214bc6a2SJed Brown   }
867214bc6a2SJed Brown   if (imex) {
86824989b8cSPeter Brune     if (!ifunction) {
8690910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8702dd45cf8SJed Brown     }
87124989b8cSPeter Brune   } else if (rhsfunction) {
87224989b8cSPeter Brune     if (ifunction) {
873214bc6a2SJed Brown       Vec Frhs;
874214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8750910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
876214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8772dd45cf8SJed Brown     } else {
8780910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8790910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
880316643e7SJed Brown     }
8814a6899ffSJed Brown   }
8820910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
883316643e7SJed Brown   PetscFunctionReturn(0);
884316643e7SJed Brown }
885316643e7SJed Brown 
886cfa8a9a2SHong Zhang /*
887cfa8a9a2SHong Zhang    TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it.
888cfa8a9a2SHong Zhang 
889cfa8a9a2SHong Zhang    Note:
890cfa8a9a2SHong Zhang    This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian().
891cfa8a9a2SHong Zhang 
892cfa8a9a2SHong Zhang */
893cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B)
894cfa8a9a2SHong Zhang {
895cfa8a9a2SHong Zhang   PetscErrorCode   ierr;
896cfa8a9a2SHong Zhang 
897cfa8a9a2SHong Zhang   PetscFunctionBegin;
898cfa8a9a2SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
89903c97d52SHong Zhang   if (A != ts->Arhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat");
90003c97d52SHong Zhang   if (B != ts->Brhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat");
901cfa8a9a2SHong Zhang 
902cfa8a9a2SHong Zhang   if (ts->rhsjacobian.shift) {
903cfa8a9a2SHong Zhang     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
904cfa8a9a2SHong Zhang   }
905cfa8a9a2SHong Zhang   if (ts->rhsjacobian.scale == -1.) {
906cfa8a9a2SHong Zhang     ierr = MatScale(A,-1);CHKERRQ(ierr);
907cfa8a9a2SHong Zhang   }
908cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
909cfa8a9a2SHong Zhang     if (ts->rhsjacobian.shift) {
910cfa8a9a2SHong Zhang       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
911cfa8a9a2SHong Zhang     }
912cfa8a9a2SHong Zhang     if (ts->rhsjacobian.scale == -1.) {
913cfa8a9a2SHong Zhang       ierr = MatScale(B,-1);CHKERRQ(ierr);
914cfa8a9a2SHong Zhang     }
915cfa8a9a2SHong Zhang   }
916cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
917cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
918cfa8a9a2SHong Zhang   PetscFunctionReturn(0);
919cfa8a9a2SHong Zhang }
920cfa8a9a2SHong Zhang 
921316643e7SJed Brown /*@
922316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
923316643e7SJed Brown 
924d083f849SBarry Smith    Collective on TS
925316643e7SJed Brown 
926316643e7SJed Brown    Input
927316643e7SJed Brown       Input Parameters:
928316643e7SJed Brown +  ts - the TS context
929316643e7SJed Brown .  t - current timestep
9300910c330SBarry Smith .  U - state vector
9310910c330SBarry Smith .  Udot - time derivative of state vector
932214bc6a2SJed Brown .  shift - shift to apply, see note below
933214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
934316643e7SJed Brown 
935316643e7SJed Brown    Output Parameters:
936316643e7SJed Brown +  A - Jacobian matrix
9373565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
938316643e7SJed Brown 
939316643e7SJed Brown    Notes:
9400910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
941316643e7SJed Brown 
9420910c330SBarry Smith    dF/dU + shift*dF/dUdot
943316643e7SJed Brown 
944316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
945316643e7SJed Brown    is used internally within the nonlinear solvers.
946316643e7SJed Brown 
947316643e7SJed Brown    Level: developer
948316643e7SJed Brown 
949316643e7SJed Brown .seealso:  TSSetIJacobian()
95063495f91SJed Brown @*/
951d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
952316643e7SJed Brown {
953316643e7SJed Brown   PetscErrorCode ierr;
95424989b8cSPeter Brune   TSIJacobian    ijacobian;
95524989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
95624989b8cSPeter Brune   DM             dm;
95724989b8cSPeter Brune   void           *ctx;
958316643e7SJed Brown 
959316643e7SJed Brown   PetscFunctionBegin;
9600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9610910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9620910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
963316643e7SJed Brown   PetscValidPointer(A,6);
96494ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
965316643e7SJed Brown   PetscValidPointer(B,7);
96694ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
96724989b8cSPeter Brune 
96824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9700298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
97124989b8cSPeter Brune 
972ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
973316643e7SJed Brown 
97494ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
97524989b8cSPeter Brune   if (ijacobian) {
976316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
977d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
978a6ab3590SBarry Smith     ts->ijacs++;
979316643e7SJed Brown     PetscStackPop;
9804a6899ffSJed Brown   }
981214bc6a2SJed Brown   if (imex) {
982b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9834c26be97Sstefano_zampini       PetscBool assembled;
984971015bcSStefano Zampini       if (rhsjacobian) {
985971015bcSStefano Zampini         Mat Arhs = NULL;
986971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
987971015bcSStefano Zampini         if (A == Arhs) {
988e8b1e424SHong 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 */
989971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
990971015bcSStefano Zampini         }
991971015bcSStefano Zampini       }
99294ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9934c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9944c26be97Sstefano_zampini       if (!assembled) {
9954c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9964c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9974c26be97Sstefano_zampini       }
99894ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99994ab13aaSBarry Smith       if (A != B) {
100094ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
10014c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
10024c26be97Sstefano_zampini         if (!assembled) {
10034c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10044c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10054c26be97Sstefano_zampini         }
100694ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1007214bc6a2SJed Brown       }
1008214bc6a2SJed Brown     }
1009214bc6a2SJed Brown   } else {
1010e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1011e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
1012214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1013e1244c69SJed Brown     }
1014e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
1015e8b1e424SHong Zhang       PetscObjectState Ustate;
1016e8b1e424SHong Zhang       PetscObjectId    Uid;
1017e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
1018e8b1e424SHong Zhang 
1019e8b1e424SHong Zhang       ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1020e8b1e424SHong Zhang       ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
1021e8b1e424SHong Zhang       ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
1022097a96a2SHong 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 */
1023e8b1e424SHong 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 */
102434d322a1SHong Zhang         if (A != B) {
102534d322a1SHong Zhang           ierr = MatShift(B,shift-ts->rhsjacobian.shift);CHKERRQ(ierr);
102634d322a1SHong Zhang         }
1027e8b1e424SHong Zhang       } else {
10283565c898SBarry Smith         PetscBool flg;
1029e8b1e424SHong Zhang 
1030e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1031e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1032e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1033e8b1e424SHong Zhang              and the matrices have not been assembled yet */
1034cfa8a9a2SHong Zhang           ierr = TSRecoverRHSJacobian(ts,A,B);CHKERRQ(ierr);
1035e8b1e424SHong Zhang         }
1036e8b1e424SHong Zhang         ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr);
10373565c898SBarry Smith         ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10383565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10393565c898SBarry Smith         if (!flg) {
104094ab13aaSBarry Smith           ierr = MatScale(A,-1);CHKERRQ(ierr);
104194ab13aaSBarry Smith           ierr = MatShift(A,shift);CHKERRQ(ierr);
10423565c898SBarry Smith         }
104394ab13aaSBarry Smith         if (A != B) {
104494ab13aaSBarry Smith           ierr = MatScale(B,-1);CHKERRQ(ierr);
104594ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1046316643e7SJed Brown         }
1047e8b1e424SHong Zhang       }
1048e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1049e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1050d60b7d5cSBarry Smith     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1051e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
105294ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
105394ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
105494ab13aaSBarry Smith         if (A != B) {
105594ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
105694ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1057214bc6a2SJed Brown         }
1058316643e7SJed Brown       }
1059e8b1e424SHong Zhang       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1060d60b7d5cSBarry Smith       ierr = MatAXPY(A,-1,Arhs,ts->axpy_pattern);CHKERRQ(ierr);
106194ab13aaSBarry Smith       if (A != B) {
1062d60b7d5cSBarry Smith         ierr = MatAXPY(B,-1,Brhs,ts->axpy_pattern);CHKERRQ(ierr);
1063316643e7SJed Brown       }
1064316643e7SJed Brown     }
1065316643e7SJed Brown   }
106694ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1067316643e7SJed Brown   PetscFunctionReturn(0);
1068316643e7SJed Brown }
1069316643e7SJed Brown 
1070d763cef2SBarry Smith /*@C
1071d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1072b5abc632SBarry Smith     where U_t = G(t,u).
1073d763cef2SBarry Smith 
10743f9fe445SBarry Smith     Logically Collective on TS
1075d763cef2SBarry Smith 
1076d763cef2SBarry Smith     Input Parameters:
1077d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10780298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1079d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1080d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10810298fd71SBarry Smith           function evaluation routine (may be NULL)
1082d763cef2SBarry Smith 
1083a96d6ef6SBarry Smith     Calling sequence of f:
1084a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1085d763cef2SBarry Smith 
1086a96d6ef6SBarry Smith +   ts - timestep context
1087a96d6ef6SBarry Smith .   t - current timestep
1088d763cef2SBarry Smith .   u - input vector
1089d763cef2SBarry Smith .   F - function vector
1090d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1091d763cef2SBarry Smith 
1092d763cef2SBarry Smith     Level: beginner
1093d763cef2SBarry Smith 
109495452b02SPatrick Sanan     Notes:
109595452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10962bbac0d3SBarry Smith 
1097ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1098d763cef2SBarry Smith @*/
1099089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1100d763cef2SBarry Smith {
1101089b2837SJed Brown   PetscErrorCode ierr;
1102089b2837SJed Brown   SNES           snes;
11030298fd71SBarry Smith   Vec            ralloc = NULL;
110424989b8cSPeter Brune   DM             dm;
1105d763cef2SBarry Smith 
1106089b2837SJed Brown   PetscFunctionBegin;
11070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1108ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
110924989b8cSPeter Brune 
111024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
111124989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1112089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1113e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1114e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1115e856ceecSJed Brown     r = ralloc;
1116e856ceecSJed Brown   }
1117089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1118e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1119d763cef2SBarry Smith   PetscFunctionReturn(0);
1120d763cef2SBarry Smith }
1121d763cef2SBarry Smith 
1122ef20d060SBarry Smith /*@C
1123abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1124ef20d060SBarry Smith 
1125ef20d060SBarry Smith     Logically Collective on TS
1126ef20d060SBarry Smith 
1127ef20d060SBarry Smith     Input Parameters:
1128ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1129ef20d060SBarry Smith .   f - routine for evaluating the solution
1130ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11310298fd71SBarry Smith           function evaluation routine (may be NULL)
1132ef20d060SBarry Smith 
1133a96d6ef6SBarry Smith     Calling sequence of f:
1134a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1135ef20d060SBarry Smith 
1136ef20d060SBarry Smith +   t - current timestep
1137ef20d060SBarry Smith .   u - output vector
1138ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1139ef20d060SBarry Smith 
11400ed3bfb6SBarry Smith     Options Database:
11410ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11420ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11430ed3bfb6SBarry Smith 
1144abd5a294SJed Brown     Notes:
1145abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1146abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1147abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1148abd5a294SJed Brown 
11494f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1150abd5a294SJed Brown 
1151ef20d060SBarry Smith     Level: beginner
1152ef20d060SBarry Smith 
11530ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1154ef20d060SBarry Smith @*/
1155ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1156ef20d060SBarry Smith {
1157ef20d060SBarry Smith   PetscErrorCode ierr;
1158ef20d060SBarry Smith   DM             dm;
1159ef20d060SBarry Smith 
1160ef20d060SBarry Smith   PetscFunctionBegin;
1161ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1162ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1163ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1164ef20d060SBarry Smith   PetscFunctionReturn(0);
1165ef20d060SBarry Smith }
1166ef20d060SBarry Smith 
11679b7cd975SBarry Smith /*@C
11689b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11699b7cd975SBarry Smith 
11709b7cd975SBarry Smith     Logically Collective on TS
11719b7cd975SBarry Smith 
11729b7cd975SBarry Smith     Input Parameters:
11739b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1174e162b725SBarry Smith .   func - routine for evaluating the forcing function
11759b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11760298fd71SBarry Smith           function evaluation routine (may be NULL)
11779b7cd975SBarry Smith 
11789b7cd975SBarry Smith     Calling sequence of func:
11796bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11809b7cd975SBarry Smith 
11819b7cd975SBarry Smith +   t - current timestep
1182e162b725SBarry Smith .   f - output vector
11839b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11849b7cd975SBarry Smith 
11859b7cd975SBarry Smith     Notes:
11869b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1187e162b725SBarry 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
1188e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1189e162b725SBarry Smith 
1190e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1191e162b725SBarry Smith 
1192e162b725SBarry 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
1193e162b725SBarry Smith     parameters can be passed in the ctx variable.
11949b7cd975SBarry Smith 
11959b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11969b7cd975SBarry Smith 
11979b7cd975SBarry Smith     Level: beginner
11989b7cd975SBarry Smith 
11999b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
12009b7cd975SBarry Smith @*/
1201e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
12029b7cd975SBarry Smith {
12039b7cd975SBarry Smith   PetscErrorCode ierr;
12049b7cd975SBarry Smith   DM             dm;
12059b7cd975SBarry Smith 
12069b7cd975SBarry Smith   PetscFunctionBegin;
12079b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12089b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1209e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
12109b7cd975SBarry Smith   PetscFunctionReturn(0);
12119b7cd975SBarry Smith }
12129b7cd975SBarry Smith 
1213d763cef2SBarry Smith /*@C
1214f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1215b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1216d763cef2SBarry Smith 
12173f9fe445SBarry Smith    Logically Collective on TS
1218d763cef2SBarry Smith 
1219d763cef2SBarry Smith    Input Parameters:
1220d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1221e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1222e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1223d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1224d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12250298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1226d763cef2SBarry Smith 
1227f7ab8db6SBarry Smith    Calling sequence of f:
1228a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1229d763cef2SBarry Smith 
1230d763cef2SBarry Smith +  t - current timestep
1231d763cef2SBarry Smith .  u - input vector
1232e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1233e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1234d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1235d763cef2SBarry Smith 
12366cd88445SBarry Smith    Notes:
12376cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12386cd88445SBarry Smith 
12396cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1240ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1241d763cef2SBarry Smith 
1242d763cef2SBarry Smith    Level: beginner
1243d763cef2SBarry Smith 
1244ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1245d763cef2SBarry Smith 
1246d763cef2SBarry Smith @*/
1247e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1248d763cef2SBarry Smith {
1249277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1250089b2837SJed Brown   SNES           snes;
125124989b8cSPeter Brune   DM             dm;
125224989b8cSPeter Brune   TSIJacobian    ijacobian;
1253277b19d0SLisandro Dalcin 
1254d763cef2SBarry Smith   PetscFunctionBegin;
12550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1256e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1257e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1258e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1259e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1260d763cef2SBarry Smith 
126124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
12630298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1264089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12655f659677SPeter Brune   if (!ijacobian) {
1266e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12670e4ef248SJed Brown   }
1268e5d3d808SBarry Smith   if (Amat) {
1269e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12700e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1271e5d3d808SBarry Smith     ts->Arhs = Amat;
12720e4ef248SJed Brown   }
1273e5d3d808SBarry Smith   if (Pmat) {
1274e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12750e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1276e5d3d808SBarry Smith     ts->Brhs = Pmat;
12770e4ef248SJed Brown   }
1278d763cef2SBarry Smith   PetscFunctionReturn(0);
1279d763cef2SBarry Smith }
1280d763cef2SBarry Smith 
1281316643e7SJed Brown /*@C
1282b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1283316643e7SJed Brown 
12843f9fe445SBarry Smith    Logically Collective on TS
1285316643e7SJed Brown 
1286316643e7SJed Brown    Input Parameters:
1287316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12880298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1289316643e7SJed Brown .  f   - the function evaluation routine
12900298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1291316643e7SJed Brown 
1292316643e7SJed Brown    Calling sequence of f:
12936bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1294316643e7SJed Brown 
1295316643e7SJed Brown +  t   - time at step/stage being solved
1296316643e7SJed Brown .  u   - state vector
1297316643e7SJed Brown .  u_t - time derivative of state vector
1298316643e7SJed Brown .  F   - function vector
1299316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1300316643e7SJed Brown 
1301316643e7SJed Brown    Important:
13022bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1303316643e7SJed Brown 
1304316643e7SJed Brown    Level: beginner
1305316643e7SJed Brown 
1306d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1307316643e7SJed Brown @*/
130851699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1309316643e7SJed Brown {
1310089b2837SJed Brown   PetscErrorCode ierr;
1311089b2837SJed Brown   SNES           snes;
131251699248SLisandro Dalcin   Vec            ralloc = NULL;
131324989b8cSPeter Brune   DM             dm;
1314316643e7SJed Brown 
1315316643e7SJed Brown   PetscFunctionBegin;
13160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
131751699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
131824989b8cSPeter Brune 
131924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
132024989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
132124989b8cSPeter Brune 
1322089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
132351699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
132451699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
132551699248SLisandro Dalcin     r  = ralloc;
1326e856ceecSJed Brown   }
132751699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
132851699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1329089b2837SJed Brown   PetscFunctionReturn(0);
1330089b2837SJed Brown }
1331089b2837SJed Brown 
1332089b2837SJed Brown /*@C
1333089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1334089b2837SJed Brown 
1335089b2837SJed Brown    Not Collective
1336089b2837SJed Brown 
1337089b2837SJed Brown    Input Parameter:
1338089b2837SJed Brown .  ts - the TS context
1339089b2837SJed Brown 
1340089b2837SJed Brown    Output Parameter:
13410298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13420298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13430298fd71SBarry Smith -  ctx - the function context (or NULL)
1344089b2837SJed Brown 
1345089b2837SJed Brown    Level: advanced
1346089b2837SJed Brown 
1347089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1348089b2837SJed Brown @*/
1349089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1350089b2837SJed Brown {
1351089b2837SJed Brown   PetscErrorCode ierr;
1352089b2837SJed Brown   SNES           snes;
135324989b8cSPeter Brune   DM             dm;
1354089b2837SJed Brown 
1355089b2837SJed Brown   PetscFunctionBegin;
1356089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1357089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13580298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
135924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
136024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1361089b2837SJed Brown   PetscFunctionReturn(0);
1362089b2837SJed Brown }
1363089b2837SJed Brown 
1364089b2837SJed Brown /*@C
1365089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1366089b2837SJed Brown 
1367089b2837SJed Brown    Not Collective
1368089b2837SJed Brown 
1369089b2837SJed Brown    Input Parameter:
1370089b2837SJed Brown .  ts - the TS context
1371089b2837SJed Brown 
1372089b2837SJed Brown    Output Parameter:
13730298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13740298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13750298fd71SBarry Smith -  ctx - the function context (or NULL)
1376089b2837SJed Brown 
1377089b2837SJed Brown    Level: advanced
1378089b2837SJed Brown 
13792bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1380089b2837SJed Brown @*/
1381089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1382089b2837SJed Brown {
1383089b2837SJed Brown   PetscErrorCode ierr;
1384089b2837SJed Brown   SNES           snes;
138524989b8cSPeter Brune   DM             dm;
1386089b2837SJed Brown 
1387089b2837SJed Brown   PetscFunctionBegin;
1388089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1389089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13900298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
139124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
139224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1393316643e7SJed Brown   PetscFunctionReturn(0);
1394316643e7SJed Brown }
1395316643e7SJed Brown 
1396316643e7SJed Brown /*@C
1397a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1398ae8867d6SBarry Smith         provided with TSSetIFunction().
1399316643e7SJed Brown 
14003f9fe445SBarry Smith    Logically Collective on TS
1401316643e7SJed Brown 
1402316643e7SJed Brown    Input Parameters:
1403316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1404e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1405e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1406316643e7SJed Brown .  f   - the Jacobian evaluation routine
14070298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1408316643e7SJed Brown 
1409316643e7SJed Brown    Calling sequence of f:
14106bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1411316643e7SJed Brown 
1412316643e7SJed Brown +  t    - time at step/stage being solved
14131b4a444bSJed Brown .  U    - state vector
14141b4a444bSJed Brown .  U_t  - time derivative of state vector
1415316643e7SJed Brown .  a    - shift
1416e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1417e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1418316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1419316643e7SJed Brown 
1420316643e7SJed Brown    Notes:
1421e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1422316643e7SJed Brown 
1423895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1424895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1425895c21f2SBarry Smith 
1426a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1427b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1428a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1429a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1430a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1431a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1432a4f0a591SBarry Smith 
14336cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14346cd88445SBarry Smith 
14356cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1436ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1437ca5f011dSBarry Smith 
1438316643e7SJed Brown    Level: beginner
1439316643e7SJed Brown 
1440ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1441316643e7SJed Brown 
1442316643e7SJed Brown @*/
1443e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1444316643e7SJed Brown {
1445316643e7SJed Brown   PetscErrorCode ierr;
1446089b2837SJed Brown   SNES           snes;
144724989b8cSPeter Brune   DM             dm;
1448316643e7SJed Brown 
1449316643e7SJed Brown   PetscFunctionBegin;
14500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1451e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1452e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1453e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1454e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
145524989b8cSPeter Brune 
145624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
145724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
145824989b8cSPeter Brune 
1459089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1460e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1461316643e7SJed Brown   PetscFunctionReturn(0);
1462316643e7SJed Brown }
1463316643e7SJed Brown 
1464e1244c69SJed Brown /*@
1465e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1466e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1467e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1468e1244c69SJed Brown    not been changed by the TS.
1469e1244c69SJed Brown 
1470e1244c69SJed Brown    Logically Collective
1471e1244c69SJed Brown 
1472e1244c69SJed Brown    Input Arguments:
1473e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1474e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1475e1244c69SJed Brown 
1476e1244c69SJed Brown    Level: intermediate
1477e1244c69SJed Brown 
1478e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1479e1244c69SJed Brown @*/
1480e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1481e1244c69SJed Brown {
1482e1244c69SJed Brown   PetscFunctionBegin;
1483e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1484e1244c69SJed Brown   PetscFunctionReturn(0);
1485e1244c69SJed Brown }
1486e1244c69SJed Brown 
1487efe9872eSLisandro Dalcin /*@C
1488efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin    Logically Collective on TS
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin    Input Parameters:
1493efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1494efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1495efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1496efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1497efe9872eSLisandro Dalcin 
1498efe9872eSLisandro Dalcin    Calling sequence of fun:
14996bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1500efe9872eSLisandro Dalcin 
1501efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1502efe9872eSLisandro Dalcin .  U    - state vector
1503efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1504efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1505efe9872eSLisandro Dalcin .  F    - function vector
1506efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin    Level: beginner
1509efe9872eSLisandro Dalcin 
1510a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1511efe9872eSLisandro Dalcin @*/
1512efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1513efe9872eSLisandro Dalcin {
1514efe9872eSLisandro Dalcin   DM             dm;
1515efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin   PetscFunctionBegin;
1518efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1519efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1520efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1521efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1522efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1523efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1524efe9872eSLisandro Dalcin }
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin /*@C
1527efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1528efe9872eSLisandro Dalcin 
1529efe9872eSLisandro Dalcin   Not Collective
1530efe9872eSLisandro Dalcin 
1531efe9872eSLisandro Dalcin   Input Parameter:
1532efe9872eSLisandro Dalcin . ts - the TS context
1533efe9872eSLisandro Dalcin 
1534efe9872eSLisandro Dalcin   Output Parameter:
1535efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1536efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1537efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin   Level: advanced
1540efe9872eSLisandro Dalcin 
1541a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1542efe9872eSLisandro Dalcin @*/
1543efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1544efe9872eSLisandro Dalcin {
1545efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1546efe9872eSLisandro Dalcin   SNES           snes;
1547efe9872eSLisandro Dalcin   DM             dm;
1548efe9872eSLisandro Dalcin 
1549efe9872eSLisandro Dalcin   PetscFunctionBegin;
1550efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1551efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1552efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1553efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1554efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1555efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1556efe9872eSLisandro Dalcin }
1557efe9872eSLisandro Dalcin 
1558efe9872eSLisandro Dalcin /*@C
1559bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1560efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1561efe9872eSLisandro Dalcin 
1562efe9872eSLisandro Dalcin    Logically Collective on TS
1563efe9872eSLisandro Dalcin 
1564efe9872eSLisandro Dalcin    Input Parameters:
1565efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1566efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1567efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1568efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1569efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1570efe9872eSLisandro Dalcin 
1571efe9872eSLisandro Dalcin    Calling sequence of jac:
15726bc98fa9SBarry 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);
1573efe9872eSLisandro Dalcin 
1574efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1575efe9872eSLisandro Dalcin .  U    - state vector
1576efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1577efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1578efe9872eSLisandro Dalcin .  v    - shift for U_t
1579efe9872eSLisandro Dalcin .  a    - shift for U_tt
1580efe9872eSLisandro 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
1581efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1582efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1583efe9872eSLisandro Dalcin 
1584efe9872eSLisandro Dalcin    Notes:
1585efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1588efe9872eSLisandro 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.
1589efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1590bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1591efe9872eSLisandro Dalcin 
1592efe9872eSLisandro Dalcin    Level: beginner
1593efe9872eSLisandro Dalcin 
1594a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1595efe9872eSLisandro Dalcin @*/
1596efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1597efe9872eSLisandro Dalcin {
1598efe9872eSLisandro Dalcin   DM             dm;
1599efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1600efe9872eSLisandro Dalcin 
1601efe9872eSLisandro Dalcin   PetscFunctionBegin;
1602efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1603efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1604efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1605efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1606efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1607efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1608efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1609efe9872eSLisandro Dalcin }
1610efe9872eSLisandro Dalcin 
1611efe9872eSLisandro Dalcin /*@C
1612efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1613efe9872eSLisandro Dalcin 
1614efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1615efe9872eSLisandro Dalcin 
1616efe9872eSLisandro Dalcin   Input Parameter:
1617efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1618efe9872eSLisandro Dalcin 
1619efe9872eSLisandro Dalcin   Output Parameters:
1620efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1621efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1622efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1623efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1624efe9872eSLisandro Dalcin 
162595452b02SPatrick Sanan   Notes:
162695452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1627efe9872eSLisandro Dalcin 
1628efe9872eSLisandro Dalcin   Level: advanced
1629efe9872eSLisandro Dalcin 
1630a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1631efe9872eSLisandro Dalcin 
1632efe9872eSLisandro Dalcin @*/
1633efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1634efe9872eSLisandro Dalcin {
1635efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1636efe9872eSLisandro Dalcin   SNES           snes;
1637efe9872eSLisandro Dalcin   DM             dm;
1638efe9872eSLisandro Dalcin 
1639efe9872eSLisandro Dalcin   PetscFunctionBegin;
1640efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1641efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1642efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1643efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1644efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1645efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1646efe9872eSLisandro Dalcin }
1647efe9872eSLisandro Dalcin 
1648efe9872eSLisandro Dalcin /*@
1649efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1650efe9872eSLisandro Dalcin 
1651d083f849SBarry Smith   Collective on TS
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   Input Parameters:
1654efe9872eSLisandro Dalcin + ts - the TS context
1655efe9872eSLisandro Dalcin . t - current time
1656efe9872eSLisandro Dalcin . U - state vector
1657efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1658efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1659efe9872eSLisandro Dalcin 
1660efe9872eSLisandro Dalcin   Output Parameter:
1661efe9872eSLisandro Dalcin . F - the residual vector
1662efe9872eSLisandro Dalcin 
1663efe9872eSLisandro Dalcin   Note:
1664efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1665efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1666efe9872eSLisandro Dalcin 
1667efe9872eSLisandro Dalcin   Level: developer
1668efe9872eSLisandro Dalcin 
1669a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1670efe9872eSLisandro Dalcin @*/
1671efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1672efe9872eSLisandro Dalcin {
1673efe9872eSLisandro Dalcin   DM             dm;
1674efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1675efe9872eSLisandro Dalcin   void           *ctx;
1676efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1677efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   PetscFunctionBegin;
1680efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1681efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1682efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1683efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1684efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1685efe9872eSLisandro Dalcin 
1686efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1687efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1688efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin   if (!I2Function) {
1691efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1692efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1693efe9872eSLisandro Dalcin   }
1694efe9872eSLisandro Dalcin 
1695efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1696efe9872eSLisandro Dalcin 
1697efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1698efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1699efe9872eSLisandro Dalcin   PetscStackPop;
1700efe9872eSLisandro Dalcin 
1701efe9872eSLisandro Dalcin   if (rhsfunction) {
1702efe9872eSLisandro Dalcin     Vec Frhs;
1703efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1704efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1705efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1706efe9872eSLisandro Dalcin   }
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1709efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1710efe9872eSLisandro Dalcin }
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin /*@
1713efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1714efe9872eSLisandro Dalcin 
1715d083f849SBarry Smith   Collective on TS
1716efe9872eSLisandro Dalcin 
1717efe9872eSLisandro Dalcin   Input Parameters:
1718efe9872eSLisandro Dalcin + ts - the TS context
1719efe9872eSLisandro Dalcin . t - current timestep
1720efe9872eSLisandro Dalcin . U - state vector
1721efe9872eSLisandro Dalcin . V - time derivative of state vector
1722efe9872eSLisandro Dalcin . A - second time derivative of state vector
1723efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1724efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin   Output Parameters:
1727efe9872eSLisandro Dalcin + J - Jacobian matrix
1728efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1729efe9872eSLisandro Dalcin 
1730efe9872eSLisandro Dalcin   Notes:
1731efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1732efe9872eSLisandro Dalcin 
1733efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1734efe9872eSLisandro Dalcin 
1735efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1736efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1737efe9872eSLisandro Dalcin 
1738efe9872eSLisandro Dalcin   Level: developer
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1741efe9872eSLisandro Dalcin @*/
1742efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1743efe9872eSLisandro Dalcin {
1744efe9872eSLisandro Dalcin   DM             dm;
1745efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1746efe9872eSLisandro Dalcin   void           *ctx;
1747efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1748efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1749efe9872eSLisandro Dalcin 
1750efe9872eSLisandro Dalcin   PetscFunctionBegin;
1751efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1752efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1753efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1754efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1755efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1756efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1757efe9872eSLisandro Dalcin 
1758efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1759efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1760efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1761efe9872eSLisandro Dalcin 
1762efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1763efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1764efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1765efe9872eSLisandro Dalcin   }
1766efe9872eSLisandro Dalcin 
1767efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1768efe9872eSLisandro Dalcin 
1769efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1770efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1771efe9872eSLisandro Dalcin   PetscStackPop;
1772efe9872eSLisandro Dalcin 
1773efe9872eSLisandro Dalcin   if (rhsjacobian) {
1774d60b7d5cSBarry Smith     Mat Jrhs,Prhs;
1775efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1776efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1777d60b7d5cSBarry Smith     ierr = MatAXPY(J,-1,Jrhs,ts->axpy_pattern);CHKERRQ(ierr);
1778d60b7d5cSBarry Smith     if (P != J) {ierr = MatAXPY(P,-1,Prhs,ts->axpy_pattern);CHKERRQ(ierr);}
1779efe9872eSLisandro Dalcin   }
1780efe9872eSLisandro Dalcin 
1781efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1782efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1783efe9872eSLisandro Dalcin }
1784efe9872eSLisandro Dalcin 
1785438f35afSJed Brown /*@C
1786438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1787438f35afSJed Brown 
1788438f35afSJed Brown    Logically Collective
1789438f35afSJed Brown 
1790438f35afSJed Brown    Input Arguments:
1791438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1792a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1793438f35afSJed Brown -  ctx - a context for tvar
1794438f35afSJed Brown 
1795a96d6ef6SBarry Smith     Calling sequence of tvar:
1796a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1797a96d6ef6SBarry Smith 
1798a96d6ef6SBarry Smith +   ts - timestep context
1799a96d6ef6SBarry Smith .   p - input vector (primative form)
1800a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1801a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1802a96d6ef6SBarry Smith 
1803438f35afSJed Brown    Level: advanced
1804438f35afSJed Brown 
1805438f35afSJed Brown    Notes:
1806438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1807438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1808438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1809438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1810438f35afSJed Brown    evaluated via the chain rule, as in
1811438f35afSJed Brown 
1812438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1813438f35afSJed Brown 
1814438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1815438f35afSJed Brown @*/
1816438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1817438f35afSJed Brown {
1818438f35afSJed Brown   PetscErrorCode ierr;
1819438f35afSJed Brown   DM             dm;
1820438f35afSJed Brown 
1821438f35afSJed Brown   PetscFunctionBegin;
1822438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1823438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1824438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1825438f35afSJed Brown   PetscFunctionReturn(0);
1826438f35afSJed Brown }
1827438f35afSJed Brown 
1828efe9872eSLisandro Dalcin /*@
1829e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1830e3c11fc1SJed Brown 
1831e3c11fc1SJed Brown    Logically Collective
1832e3c11fc1SJed Brown 
1833e3c11fc1SJed Brown    Input Parameters:
1834e3c11fc1SJed Brown +  ts - TS on which to compute
1835e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1836e3c11fc1SJed Brown 
1837e3c11fc1SJed Brown    Output Parameters:
1838e3c11fc1SJed Brown .  C - transient (conservative) variable
1839e3c11fc1SJed Brown 
1840e3c11fc1SJed Brown    Developer Notes:
1841e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1842e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1843e3c11fc1SJed Brown    being used.
1844e3c11fc1SJed Brown 
1845e3c11fc1SJed Brown    Level: developer
1846e3c11fc1SJed Brown 
1847e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1848e3c11fc1SJed Brown @*/
1849e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1850e3c11fc1SJed Brown {
1851e3c11fc1SJed Brown   PetscErrorCode ierr;
1852e3c11fc1SJed Brown   DM             dm;
1853e3c11fc1SJed Brown   DMTS           dmts;
1854e3c11fc1SJed Brown 
1855e3c11fc1SJed Brown   PetscFunctionBegin;
1856e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1857e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1858e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1859e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1860e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1861e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1862e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1863e3c11fc1SJed Brown   }
1864e3c11fc1SJed Brown   PetscFunctionReturn(0);
1865e3c11fc1SJed Brown }
1866e3c11fc1SJed Brown 
1867e3c11fc1SJed Brown /*@
1868e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1869e3c11fc1SJed Brown 
1870e3c11fc1SJed Brown    Logically Collective
1871e3c11fc1SJed Brown 
1872e3c11fc1SJed Brown    Input Parameters:
1873e3c11fc1SJed Brown .  ts - TS on which to compute
1874e3c11fc1SJed Brown 
1875e3c11fc1SJed Brown    Output Parameters:
1876e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1877e3c11fc1SJed Brown 
1878e3c11fc1SJed Brown    Level: developer
1879e3c11fc1SJed Brown 
1880e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1881e3c11fc1SJed Brown @*/
1882e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1883e3c11fc1SJed Brown {
1884e3c11fc1SJed Brown   PetscErrorCode ierr;
1885e3c11fc1SJed Brown   DM             dm;
1886e3c11fc1SJed Brown   DMTS           dmts;
1887e3c11fc1SJed Brown 
1888e3c11fc1SJed Brown   PetscFunctionBegin;
1889e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1890e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1891e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1892e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1893e3c11fc1SJed Brown   PetscFunctionReturn(0);
1894e3c11fc1SJed Brown }
1895e3c11fc1SJed Brown 
1896e3c11fc1SJed Brown /*@
1897efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1898efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1899efe9872eSLisandro Dalcin 
1900d083f849SBarry Smith    Logically Collective on TS
1901efe9872eSLisandro Dalcin 
1902efe9872eSLisandro Dalcin    Input Parameters:
1903efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1904efe9872eSLisandro Dalcin .  u - the solution vector
1905efe9872eSLisandro Dalcin -  v - the time derivative vector
1906efe9872eSLisandro Dalcin 
1907efe9872eSLisandro Dalcin    Level: beginner
1908efe9872eSLisandro Dalcin 
1909efe9872eSLisandro Dalcin @*/
1910efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1911efe9872eSLisandro Dalcin {
1912efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1913efe9872eSLisandro Dalcin 
1914efe9872eSLisandro Dalcin   PetscFunctionBegin;
1915efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1916efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1917efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1918efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1919efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1920efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1921efe9872eSLisandro Dalcin   ts->vec_dot = v;
1922efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1923efe9872eSLisandro Dalcin }
1924efe9872eSLisandro Dalcin 
1925efe9872eSLisandro Dalcin /*@
1926efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1927efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1928efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1929efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1930efe9872eSLisandro Dalcin 
1931efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1932efe9872eSLisandro Dalcin 
1933efe9872eSLisandro Dalcin    Input Parameter:
1934efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1935efe9872eSLisandro Dalcin 
1936efe9872eSLisandro Dalcin    Output Parameter:
1937efe9872eSLisandro Dalcin +  u - the vector containing the solution
1938efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1939efe9872eSLisandro Dalcin 
1940efe9872eSLisandro Dalcin    Level: intermediate
1941efe9872eSLisandro Dalcin 
1942efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1943efe9872eSLisandro Dalcin 
1944efe9872eSLisandro Dalcin @*/
1945efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1946efe9872eSLisandro Dalcin {
1947efe9872eSLisandro Dalcin   PetscFunctionBegin;
1948efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1949efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1950efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1951efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1952efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1953efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1954efe9872eSLisandro Dalcin }
1955efe9872eSLisandro Dalcin 
195655849f57SBarry Smith /*@C
195755849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
195855849f57SBarry Smith 
195955849f57SBarry Smith   Collective on PetscViewer
196055849f57SBarry Smith 
196155849f57SBarry Smith   Input Parameters:
196255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
196355849f57SBarry Smith            some related function before a call to TSLoad().
196455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
196555849f57SBarry Smith 
196655849f57SBarry Smith    Level: intermediate
196755849f57SBarry Smith 
196855849f57SBarry Smith   Notes:
196955849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
197055849f57SBarry Smith 
197155849f57SBarry Smith   Notes for advanced users:
197255849f57SBarry Smith   Most users should not need to know the details of the binary storage
197355849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
197455849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
197555849f57SBarry Smith   format is
197655849f57SBarry Smith .vb
197755849f57SBarry Smith      has not yet been determined
197855849f57SBarry Smith .ve
197955849f57SBarry Smith 
198055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
198155849f57SBarry Smith @*/
1982f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
198355849f57SBarry Smith {
198455849f57SBarry Smith   PetscErrorCode ierr;
198555849f57SBarry Smith   PetscBool      isbinary;
198655849f57SBarry Smith   PetscInt       classid;
198755849f57SBarry Smith   char           type[256];
19882d53ad75SBarry Smith   DMTS           sdm;
1989ad6bc421SBarry Smith   DM             dm;
199055849f57SBarry Smith 
199155849f57SBarry Smith   PetscFunctionBegin;
1992f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
199355849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
199455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
199555849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
199655849f57SBarry Smith 
1997060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1998ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1999060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
2000f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
2001f2c2a1b9SBarry Smith   if (ts->ops->load) {
2002f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
2003f2c2a1b9SBarry Smith   }
2004ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
2005ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
2006ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
2007f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
2008f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
20092d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20102d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
201155849f57SBarry Smith   PetscFunctionReturn(0);
201255849f57SBarry Smith }
201355849f57SBarry Smith 
20149804daf3SBarry Smith #include <petscdraw.h>
2015e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2016e04113cfSBarry Smith #include <petscviewersaws.h>
2017f05ece33SBarry Smith #endif
2018fe2efc57SMark 
2019fe2efc57SMark /*@C
2020fe2efc57SMark    TSViewFromOptions - View from Options
2021fe2efc57SMark 
2022fe2efc57SMark    Collective on TS
2023fe2efc57SMark 
2024fe2efc57SMark    Input Parameters:
2025fe2efc57SMark +  A - the application ordering context
2026736c3998SJose E. Roman .  obj - Optional object
2027736c3998SJose E. Roman -  name - command line option
2028fe2efc57SMark 
2029fe2efc57SMark    Level: intermediate
2030fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
2031fe2efc57SMark @*/
2032fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
2033fe2efc57SMark {
2034fe2efc57SMark   PetscErrorCode ierr;
2035fe2efc57SMark 
2036fe2efc57SMark   PetscFunctionBegin;
2037fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
2038fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
2039fe2efc57SMark   PetscFunctionReturn(0);
2040fe2efc57SMark }
2041fe2efc57SMark 
20427e2c5f70SBarry Smith /*@C
2043d763cef2SBarry Smith     TSView - Prints the TS data structure.
2044d763cef2SBarry Smith 
20454c49b128SBarry Smith     Collective on TS
2046d763cef2SBarry Smith 
2047d763cef2SBarry Smith     Input Parameters:
2048d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2049d763cef2SBarry Smith -   viewer - visualization context
2050d763cef2SBarry Smith 
2051d763cef2SBarry Smith     Options Database Key:
2052d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith     Notes:
2055d763cef2SBarry Smith     The available visualization contexts include
2056b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2057b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2058d763cef2SBarry Smith          output where only the first processor opens
2059d763cef2SBarry Smith          the file.  All other processors send their
2060d763cef2SBarry Smith          data to the first processor to print.
2061d763cef2SBarry Smith 
2062d763cef2SBarry Smith     The user can open an alternative visualization context with
2063b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2064d763cef2SBarry Smith 
2065595c91d4SBarry Smith     In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer).
2066595c91d4SBarry Smith 
2067d763cef2SBarry Smith     Level: beginner
2068d763cef2SBarry Smith 
2069b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2070d763cef2SBarry Smith @*/
20717087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2072d763cef2SBarry Smith {
2073dfbe8321SBarry Smith   PetscErrorCode ierr;
207419fd82e9SBarry Smith   TSType         type;
20752b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20762d53ad75SBarry Smith   DMTS           sdm;
2077e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2078536b137fSBarry Smith   PetscBool      issaws;
2079f05ece33SBarry Smith #endif
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith   PetscFunctionBegin;
20820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20833050cee2SBarry Smith   if (!viewer) {
2084ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20853050cee2SBarry Smith   }
20860700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2087c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2088fd16b177SBarry Smith 
2089251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2090251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
209155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20922b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2093e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2094536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2095f05ece33SBarry Smith #endif
209632077d6dSBarry Smith   if (iascii) {
2097dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2098efd4aadfSBarry Smith     if (ts->ops->view) {
2099efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2100efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2101efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2102efd4aadfSBarry Smith     }
2103ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
210477431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2105ef85077eSLisandro Dalcin     }
2106ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
21077c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2108ef85077eSLisandro Dalcin     }
2109a6ab3590SBarry Smith     if (ts->ifuncs) {
2110a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of I function evaluations=%D\n",ts->ifuncs);CHKERRQ(ierr);
2111a6ab3590SBarry Smith     }
2112a6ab3590SBarry Smith     if (ts->ijacs) {
2113a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of I Jacobian evaluations=%D\n",ts->ijacs);CHKERRQ(ierr);
2114a6ab3590SBarry Smith     }
2115a6ab3590SBarry Smith     if (ts->rhsfuncs) {
2116a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of RHS function evaluations=%D\n",ts->rhsfuncs);CHKERRQ(ierr);
2117a6ab3590SBarry Smith     }
2118a6ab3590SBarry Smith     if (ts->rhsjacs) {
2119a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of RHS Jacobian evaluations=%D\n",ts->rhsjacs);CHKERRQ(ierr);
2120a6ab3590SBarry Smith     }
2121efd4aadfSBarry Smith     if (ts->usessnes) {
2122efd4aadfSBarry Smith       PetscBool lin;
2123d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
21245ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2125d763cef2SBarry Smith       }
21265ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
21271ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2128efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2129efd4aadfSBarry Smith     }
2130193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2131a0af407cSBarry Smith     if (ts->vrtol) {
2132a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2133a0af407cSBarry Smith     } else {
2134a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2135a0af407cSBarry Smith     }
2136a0af407cSBarry Smith     if (ts->vatol) {
2137a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2138a0af407cSBarry Smith     } else {
2139a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2140a0af407cSBarry Smith     }
2141825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2142efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2143825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21440f5bd95cSBarry Smith   } else if (isstring) {
2145a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
214636a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
214736a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
214855849f57SBarry Smith   } else if (isbinary) {
214955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
215055849f57SBarry Smith     MPI_Comm    comm;
215155849f57SBarry Smith     PetscMPIInt rank;
215255849f57SBarry Smith     char        type[256];
215355849f57SBarry Smith 
215455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
2155ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
215655849f57SBarry Smith     if (!rank) {
2157f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
215855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2159f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
216055849f57SBarry Smith     }
216155849f57SBarry Smith     if (ts->ops->view) {
216255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
216355849f57SBarry Smith     }
2164efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2165f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2166f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21672d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21682d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21692b0a91c0SBarry Smith   } else if (isdraw) {
21702b0a91c0SBarry Smith     PetscDraw draw;
21712b0a91c0SBarry Smith     char      str[36];
217289fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21732b0a91c0SBarry Smith 
21742b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21752b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21762b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21772b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
217851fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
217989fd9fafSBarry Smith     bottom = y - h;
21802b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21812b0a91c0SBarry Smith     if (ts->ops->view) {
21822b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21832b0a91c0SBarry Smith     }
2184efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2185efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21862b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2187e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2188536b137fSBarry Smith   } else if (issaws) {
2189d45a07a7SBarry Smith     PetscMPIInt rank;
21902657e9d9SBarry Smith     const char  *name;
21912657e9d9SBarry Smith 
21922657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2193ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
2194d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2195d45a07a7SBarry Smith       char       dir[1024];
2196d45a07a7SBarry Smith 
2197e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2198a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21992657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
22002657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
22012657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2202d763cef2SBarry Smith     }
22030acecf5bSBarry Smith     if (ts->ops->view) {
22040acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
22050acecf5bSBarry Smith     }
2206f05ece33SBarry Smith #endif
2207f05ece33SBarry Smith   }
220836a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
220936a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
221036a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
221136a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
221236a9e3b9SBarry Smith   }
221336a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
221436a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2215f05ece33SBarry Smith 
2216b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2217251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2218b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2219d763cef2SBarry Smith   PetscFunctionReturn(0);
2220d763cef2SBarry Smith }
2221d763cef2SBarry Smith 
2222b07ff414SBarry Smith /*@
2223d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2224d763cef2SBarry Smith    the timesteppers.
2225d763cef2SBarry Smith 
22263f9fe445SBarry Smith    Logically Collective on TS
2227d763cef2SBarry Smith 
2228d763cef2SBarry Smith    Input Parameters:
2229d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2230d763cef2SBarry Smith -  usrP - optional user context
2231d763cef2SBarry Smith 
223295452b02SPatrick Sanan    Fortran Notes:
223395452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2234daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2235daf670e6SBarry Smith 
2236d763cef2SBarry Smith    Level: intermediate
2237d763cef2SBarry Smith 
2238d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2239d763cef2SBarry Smith @*/
22407087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2241d763cef2SBarry Smith {
2242d763cef2SBarry Smith   PetscFunctionBegin;
22430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2244d763cef2SBarry Smith   ts->user = usrP;
2245d763cef2SBarry Smith   PetscFunctionReturn(0);
2246d763cef2SBarry Smith }
2247d763cef2SBarry Smith 
2248b07ff414SBarry Smith /*@
2249d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2250d763cef2SBarry Smith     timestepper.
2251d763cef2SBarry Smith 
2252d763cef2SBarry Smith     Not Collective
2253d763cef2SBarry Smith 
2254d763cef2SBarry Smith     Input Parameter:
2255d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2256d763cef2SBarry Smith 
2257d763cef2SBarry Smith     Output Parameter:
2258d763cef2SBarry Smith .   usrP - user context
2259d763cef2SBarry Smith 
226095452b02SPatrick Sanan    Fortran Notes:
226195452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2262daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2263daf670e6SBarry Smith 
2264d763cef2SBarry Smith     Level: intermediate
2265d763cef2SBarry Smith 
2266d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2267d763cef2SBarry Smith @*/
2268e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2269d763cef2SBarry Smith {
2270d763cef2SBarry Smith   PetscFunctionBegin;
22710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2272e71120c6SJed Brown   *(void**)usrP = ts->user;
2273d763cef2SBarry Smith   PetscFunctionReturn(0);
2274d763cef2SBarry Smith }
2275d763cef2SBarry Smith 
2276d763cef2SBarry Smith /*@
227780275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2278d763cef2SBarry Smith 
2279d763cef2SBarry Smith    Not Collective
2280d763cef2SBarry Smith 
2281d763cef2SBarry Smith    Input Parameter:
2282d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2283d763cef2SBarry Smith 
2284d763cef2SBarry Smith    Output Parameter:
228580275a0aSLisandro Dalcin .  steps - number of steps completed so far
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Level: intermediate
2288d763cef2SBarry Smith 
22899be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2290d763cef2SBarry Smith @*/
229180275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2292d763cef2SBarry Smith {
2293d763cef2SBarry Smith   PetscFunctionBegin;
22940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
229580275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
229680275a0aSLisandro Dalcin   *steps = ts->steps;
229780275a0aSLisandro Dalcin   PetscFunctionReturn(0);
229880275a0aSLisandro Dalcin }
229980275a0aSLisandro Dalcin 
230080275a0aSLisandro Dalcin /*@
230180275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
230280275a0aSLisandro Dalcin 
230380275a0aSLisandro Dalcin    Logically Collective on TS
230480275a0aSLisandro Dalcin 
230580275a0aSLisandro Dalcin    Input Parameters:
230680275a0aSLisandro Dalcin +  ts - the TS context
230780275a0aSLisandro Dalcin -  steps - number of steps completed so far
230880275a0aSLisandro Dalcin 
230980275a0aSLisandro Dalcin    Notes:
231080275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
231180275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
231280275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
231380275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
231480275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
231580275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
231680275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
231780275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
231880275a0aSLisandro Dalcin 
231980275a0aSLisandro Dalcin    Level: advanced
232080275a0aSLisandro Dalcin 
232180275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
232280275a0aSLisandro Dalcin @*/
232380275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
232480275a0aSLisandro Dalcin {
232580275a0aSLisandro Dalcin   PetscFunctionBegin;
232680275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
232780275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
232880275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
232980275a0aSLisandro Dalcin   ts->steps = steps;
2330d763cef2SBarry Smith   PetscFunctionReturn(0);
2331d763cef2SBarry Smith }
2332d763cef2SBarry Smith 
2333d763cef2SBarry Smith /*@
2334d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2335d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2336d763cef2SBarry Smith 
23373f9fe445SBarry Smith    Logically Collective on TS
2338d763cef2SBarry Smith 
2339d763cef2SBarry Smith    Input Parameters:
2340d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2341d763cef2SBarry Smith -  time_step - the size of the timestep
2342d763cef2SBarry Smith 
2343d763cef2SBarry Smith    Level: intermediate
2344d763cef2SBarry Smith 
2345aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2346d763cef2SBarry Smith 
2347d763cef2SBarry Smith @*/
23487087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2349d763cef2SBarry Smith {
2350d763cef2SBarry Smith   PetscFunctionBegin;
23510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2352c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2353d763cef2SBarry Smith   ts->time_step = time_step;
2354d763cef2SBarry Smith   PetscFunctionReturn(0);
2355d763cef2SBarry Smith }
2356d763cef2SBarry Smith 
2357a43b19c4SJed Brown /*@
235849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
235949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
236049354f04SShri Abhyankar      or just return at the final time TS computed.
2361a43b19c4SJed Brown 
2362a43b19c4SJed Brown   Logically Collective on TS
2363a43b19c4SJed Brown 
2364a43b19c4SJed Brown    Input Parameter:
2365a43b19c4SJed Brown +   ts - the time-step context
236649354f04SShri Abhyankar -   eftopt - exact final time option
2367a43b19c4SJed Brown 
2368feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2369feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2370feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2371feed9e9dSBarry Smith 
2372feed9e9dSBarry Smith    Options Database:
2373feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2374feed9e9dSBarry Smith 
2375ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2376ee346746SBarry Smith     then the final time you selected.
2377ee346746SBarry Smith 
2378a43b19c4SJed Brown    Level: beginner
2379a43b19c4SJed Brown 
2380f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2381a43b19c4SJed Brown @*/
238249354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2383a43b19c4SJed Brown {
2384a43b19c4SJed Brown   PetscFunctionBegin;
2385a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
238649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
238749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2388a43b19c4SJed Brown   PetscFunctionReturn(0);
2389a43b19c4SJed Brown }
2390a43b19c4SJed Brown 
2391d763cef2SBarry Smith /*@
2392f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2393f6953c82SLisandro Dalcin 
2394f6953c82SLisandro Dalcin    Not Collective
2395f6953c82SLisandro Dalcin 
2396f6953c82SLisandro Dalcin    Input Parameter:
2397f6953c82SLisandro Dalcin .  ts - the TS context
2398f6953c82SLisandro Dalcin 
2399f6953c82SLisandro Dalcin    Output Parameter:
2400f6953c82SLisandro Dalcin .  eftopt - exact final time option
2401f6953c82SLisandro Dalcin 
2402f6953c82SLisandro Dalcin    Level: beginner
2403f6953c82SLisandro Dalcin 
2404f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2405f6953c82SLisandro Dalcin @*/
2406f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2407f6953c82SLisandro Dalcin {
2408f6953c82SLisandro Dalcin   PetscFunctionBegin;
2409f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2410f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2411f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2412f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2413f6953c82SLisandro Dalcin }
2414f6953c82SLisandro Dalcin 
2415f6953c82SLisandro Dalcin /*@
2416d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2417d763cef2SBarry Smith 
2418d763cef2SBarry Smith    Not Collective
2419d763cef2SBarry Smith 
2420d763cef2SBarry Smith    Input Parameter:
2421d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2422d763cef2SBarry Smith 
2423d763cef2SBarry Smith    Output Parameter:
2424d763cef2SBarry Smith .  dt - the current timestep size
2425d763cef2SBarry Smith 
2426d763cef2SBarry Smith    Level: intermediate
2427d763cef2SBarry Smith 
2428aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2429d763cef2SBarry Smith 
2430d763cef2SBarry Smith @*/
24317087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2432d763cef2SBarry Smith {
2433d763cef2SBarry Smith   PetscFunctionBegin;
24340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2435f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2436d763cef2SBarry Smith   *dt = ts->time_step;
2437d763cef2SBarry Smith   PetscFunctionReturn(0);
2438d763cef2SBarry Smith }
2439d763cef2SBarry Smith 
2440d8e5e3e6SSatish Balay /*@
2441d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2442d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2443d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2444d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2445d763cef2SBarry Smith 
2446d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2447d763cef2SBarry Smith 
2448d763cef2SBarry Smith    Input Parameter:
2449d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2450d763cef2SBarry Smith 
2451d763cef2SBarry Smith    Output Parameter:
2452d763cef2SBarry Smith .  v - the vector containing the solution
2453d763cef2SBarry Smith 
245463e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
245563e21af5SBarry Smith    final time. It returns the solution at the next timestep.
245663e21af5SBarry Smith 
2457d763cef2SBarry Smith    Level: intermediate
2458d763cef2SBarry Smith 
24590ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2460d763cef2SBarry Smith 
2461d763cef2SBarry Smith @*/
24627087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2463d763cef2SBarry Smith {
2464d763cef2SBarry Smith   PetscFunctionBegin;
24650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24664482741eSBarry Smith   PetscValidPointer(v,2);
24678737fe31SLisandro Dalcin   *v = ts->vec_sol;
2468d763cef2SBarry Smith   PetscFunctionReturn(0);
2469d763cef2SBarry Smith }
2470d763cef2SBarry Smith 
247103fe5f5eSDebojyoti Ghosh /*@
2472b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
247303fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2474b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
247503fe5f5eSDebojyoti Ghosh    structure as the solution vector.
247603fe5f5eSDebojyoti Ghosh 
247703fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
247803fe5f5eSDebojyoti Ghosh 
247903fe5f5eSDebojyoti Ghosh    Parameters :
2480a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2481b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2482b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
248303fe5f5eSDebojyoti Ghosh        returned in v.
2484a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
248503fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2486b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
248703fe5f5eSDebojyoti Ghosh 
24884cdd57e5SDebojyoti Ghosh    Level: advanced
248903fe5f5eSDebojyoti Ghosh 
249003fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
249103fe5f5eSDebojyoti Ghosh 
249203fe5f5eSDebojyoti Ghosh @*/
2493b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
249403fe5f5eSDebojyoti Ghosh {
249503fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
249603fe5f5eSDebojyoti Ghosh 
249703fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
249803fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2499b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
250003fe5f5eSDebojyoti Ghosh   else {
2501b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
250203fe5f5eSDebojyoti Ghosh   }
250303fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
250403fe5f5eSDebojyoti Ghosh }
250503fe5f5eSDebojyoti Ghosh 
25064cdd57e5SDebojyoti Ghosh /*@
25074cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
25084cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
25094cdd57e5SDebojyoti Ghosh 
25104cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25114cdd57e5SDebojyoti Ghosh 
25124cdd57e5SDebojyoti Ghosh    Parameters :
2513a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2514a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
25154cdd57e5SDebojyoti Ghosh 
25164cdd57e5SDebojyoti Ghosh    Level: intermediate
25174cdd57e5SDebojyoti Ghosh 
25184cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
25194cdd57e5SDebojyoti Ghosh 
25204cdd57e5SDebojyoti Ghosh @*/
25214cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
25224cdd57e5SDebojyoti Ghosh {
25234cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25244cdd57e5SDebojyoti Ghosh 
25254cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25264cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2527f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
25284cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2529f6356ec7SDebojyoti Ghosh   } else {
2530f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2531f6356ec7SDebojyoti Ghosh   }
25324cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25334cdd57e5SDebojyoti Ghosh }
25344cdd57e5SDebojyoti Ghosh 
25354cdd57e5SDebojyoti Ghosh /*@
25364cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
25374cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
25384cdd57e5SDebojyoti Ghosh 
25394cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25404cdd57e5SDebojyoti Ghosh 
25419657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
25429657682dSDebojyoti Ghosh 
25434cdd57e5SDebojyoti Ghosh    Parameters :
2544a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2545657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2546a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
25474cdd57e5SDebojyoti Ghosh 
25484cdd57e5SDebojyoti Ghosh    Level: intermediate
25494cdd57e5SDebojyoti Ghosh 
255057df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25514cdd57e5SDebojyoti Ghosh 
25524cdd57e5SDebojyoti Ghosh @*/
25530a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25544cdd57e5SDebojyoti Ghosh {
25554cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25564cdd57e5SDebojyoti Ghosh 
25574cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25584cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2559f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25600a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2561f6356ec7SDebojyoti Ghosh   } else {
2562f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2563f6356ec7SDebojyoti Ghosh   }
25644cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25654cdd57e5SDebojyoti Ghosh }
25664cdd57e5SDebojyoti Ghosh 
256757df6a1bSDebojyoti Ghosh /*@
256857df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
256957df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
257057df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
257157df6a1bSDebojyoti Ghosh 
257257df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
257357df6a1bSDebojyoti Ghosh 
257457df6a1bSDebojyoti Ghosh    Parameters :
2575a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2576a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
257757df6a1bSDebojyoti Ghosh 
257857df6a1bSDebojyoti Ghosh    Level: intermediate
257957df6a1bSDebojyoti Ghosh 
258057df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
258157df6a1bSDebojyoti Ghosh 
258257df6a1bSDebojyoti Ghosh @*/
258357df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
258457df6a1bSDebojyoti Ghosh {
258557df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
258657df6a1bSDebojyoti Ghosh 
258757df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
258857df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25899657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
259057df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
259157df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
259257df6a1bSDebojyoti Ghosh   }
259357df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
259457df6a1bSDebojyoti Ghosh }
259557df6a1bSDebojyoti Ghosh 
2596bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2597d8e5e3e6SSatish Balay /*@
2598bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2599d763cef2SBarry Smith 
2600bdad233fSMatthew Knepley   Not collective
2601d763cef2SBarry Smith 
2602bdad233fSMatthew Knepley   Input Parameters:
2603bdad233fSMatthew Knepley + ts   - The TS
2604bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2605d763cef2SBarry Smith .vb
26060910c330SBarry Smith          U_t - A U = 0      (linear)
26070910c330SBarry Smith          U_t - A(t) U = 0   (linear)
26080910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2609d763cef2SBarry Smith .ve
2610d763cef2SBarry Smith 
2611d763cef2SBarry Smith    Level: beginner
2612d763cef2SBarry Smith 
2613bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2614d763cef2SBarry Smith @*/
26157087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2616a7cc72afSBarry Smith {
26179e2a6581SJed Brown   PetscErrorCode ierr;
26189e2a6581SJed Brown 
2619d763cef2SBarry Smith   PetscFunctionBegin;
26200700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2621bdad233fSMatthew Knepley   ts->problem_type = type;
26229e2a6581SJed Brown   if (type == TS_LINEAR) {
26239e2a6581SJed Brown     SNES snes;
26249e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
26259e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
26269e2a6581SJed Brown   }
2627d763cef2SBarry Smith   PetscFunctionReturn(0);
2628d763cef2SBarry Smith }
2629d763cef2SBarry Smith 
2630bdad233fSMatthew Knepley /*@C
2631bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2632bdad233fSMatthew Knepley 
2633bdad233fSMatthew Knepley   Not collective
2634bdad233fSMatthew Knepley 
2635bdad233fSMatthew Knepley   Input Parameter:
2636bdad233fSMatthew Knepley . ts   - The TS
2637bdad233fSMatthew Knepley 
2638bdad233fSMatthew Knepley   Output Parameter:
2639bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2640bdad233fSMatthew Knepley .vb
2641089b2837SJed Brown          M U_t = A U
2642089b2837SJed Brown          M(t) U_t = A(t) U
2643b5abc632SBarry Smith          F(t,U,U_t)
2644bdad233fSMatthew Knepley .ve
2645bdad233fSMatthew Knepley 
2646bdad233fSMatthew Knepley    Level: beginner
2647bdad233fSMatthew Knepley 
2648bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2649bdad233fSMatthew Knepley @*/
26507087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2651a7cc72afSBarry Smith {
2652bdad233fSMatthew Knepley   PetscFunctionBegin;
26530700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26544482741eSBarry Smith   PetscValidIntPointer(type,2);
2655bdad233fSMatthew Knepley   *type = ts->problem_type;
2656bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2657bdad233fSMatthew Knepley }
2658d763cef2SBarry Smith 
2659303a5415SBarry Smith /*
2660303a5415SBarry 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()
2661303a5415SBarry Smith */
2662303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2663303a5415SBarry Smith {
2664303a5415SBarry Smith   PetscErrorCode ierr;
2665303a5415SBarry Smith   PetscBool      isnone;
2666303a5415SBarry Smith 
2667303a5415SBarry Smith   PetscFunctionBegin;
2668303a5415SBarry Smith   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
2669303a5415SBarry Smith   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
2670303a5415SBarry Smith 
2671303a5415SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
2672303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2673303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2674303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2675303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2676303a5415SBarry Smith   }
2677303a5415SBarry Smith   PetscFunctionReturn(0);
2678303a5415SBarry Smith }
2679303a5415SBarry Smith 
2680303a5415SBarry Smith 
2681d763cef2SBarry Smith /*@
2682303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2683d763cef2SBarry Smith 
2684d763cef2SBarry Smith    Collective on TS
2685d763cef2SBarry Smith 
2686d763cef2SBarry Smith    Input Parameter:
2687d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2688d763cef2SBarry Smith 
2689d763cef2SBarry Smith    Notes:
2690d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2691d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2692141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2693d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2694141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2695d763cef2SBarry Smith 
2696d763cef2SBarry Smith    Level: advanced
2697d763cef2SBarry Smith 
2698141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2699d763cef2SBarry Smith @*/
27007087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2701d763cef2SBarry Smith {
2702dfbe8321SBarry Smith   PetscErrorCode ierr;
27036c6b9e74SPeter Brune   DM             dm;
27046c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2705d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2706cd11d68dSLisandro Dalcin   TSIFunction    ifun;
27076c6b9e74SPeter Brune   TSIJacobian    ijac;
2708efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
27096c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2710d763cef2SBarry Smith 
2711d763cef2SBarry Smith   PetscFunctionBegin;
27120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2713277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2714277b19d0SLisandro Dalcin 
27157adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2716cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2717cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2718d763cef2SBarry Smith   }
2719277b19d0SLisandro Dalcin 
27201a638600SStefano Zampini   if (!ts->vec_sol) {
27211a638600SStefano Zampini     if (ts->dm) {
27221a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
27231a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
27241a638600SStefano Zampini   }
2725277b19d0SLisandro Dalcin 
2726298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2727298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2728298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2729298bade4SHong Zhang   }
2730298bade4SHong Zhang 
2731cd4cee2dSHong Zhang   if (ts->quadraturets) {
2732cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2733ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2734cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2735cd4cee2dSHong Zhang   }
2736cd4cee2dSHong Zhang 
2737971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2738f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2739e1244c69SJed Brown     Mat Amat,Pmat;
2740e1244c69SJed Brown     SNES snes;
2741e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2742e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2743e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2744e1244c69SJed Brown      * have displaced the RHS matrix */
2745971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2746abc0d4abSBarry 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 */
2747abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2748e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2749e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2750e1244c69SJed Brown     }
2751971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2752abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2753e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2754e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2755e1244c69SJed Brown     }
2756e1244c69SJed Brown   }
27572ffb9264SLisandro Dalcin 
27582ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27592ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27602ffb9264SLisandro Dalcin 
2761277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2762000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2763277b19d0SLisandro Dalcin   }
2764277b19d0SLisandro Dalcin 
2765303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
27662ffb9264SLisandro Dalcin 
2767a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27686c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27696c6b9e74SPeter Brune    */
27706c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27710298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27726c6b9e74SPeter Brune   if (!func) {
27736c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27746c6b9e74SPeter Brune   }
2775a6772fa2SLisandro 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.
27766c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27776c6b9e74SPeter Brune    */
27780298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27790298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2780efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27810298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2782efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27836c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27846c6b9e74SPeter Brune   }
2785c0517034SDebojyoti Ghosh 
2786c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2787c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2788c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2789c0517034SDebojyoti Ghosh   }
2790c0517034SDebojyoti Ghosh 
2791277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2792277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2793277b19d0SLisandro Dalcin }
2794277b19d0SLisandro Dalcin 
2795f6a906c0SBarry Smith /*@
2796277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2797277b19d0SLisandro Dalcin 
2798277b19d0SLisandro Dalcin    Collective on TS
2799277b19d0SLisandro Dalcin 
2800277b19d0SLisandro Dalcin    Input Parameter:
2801277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2802277b19d0SLisandro Dalcin 
2803277b19d0SLisandro Dalcin    Level: beginner
2804277b19d0SLisandro Dalcin 
2805277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2806277b19d0SLisandro Dalcin @*/
2807277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2808277b19d0SLisandro Dalcin {
28091d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2810277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2811277b19d0SLisandro Dalcin 
2812277b19d0SLisandro Dalcin   PetscFunctionBegin;
2813277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2814b18ea86cSHong Zhang 
2815277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2816277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2817277b19d0SLisandro Dalcin   }
2818277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2819e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2820bbd56ea5SKarl Rupp 
28214e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
28224e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2823214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
28246bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2825efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2826e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2827e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
282838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2829bbd56ea5SKarl Rupp 
2830cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2831ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2832ecf68647SHong Zhang   if (ts->forward_solve) {
2833ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2834ecf68647SHong Zhang   }
2835cd4cee2dSHong Zhang   if (ts->quadraturets) {
2836cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
283736eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2838cd4cee2dSHong Zhang   }
28391d06f6b3SHong Zhang   while (ilink) {
28401d06f6b3SHong Zhang     next = ilink->next;
28411d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
28421d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
28431d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
28441d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
28451d06f6b3SHong Zhang     ilink = next;
284687f4e208SHong Zhang   }
2847545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2848277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2849d763cef2SBarry Smith   PetscFunctionReturn(0);
2850d763cef2SBarry Smith }
2851d763cef2SBarry Smith 
2852d8e5e3e6SSatish Balay /*@
2853d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2854d763cef2SBarry Smith    with TSCreate().
2855d763cef2SBarry Smith 
2856d763cef2SBarry Smith    Collective on TS
2857d763cef2SBarry Smith 
2858d763cef2SBarry Smith    Input Parameter:
2859d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2860d763cef2SBarry Smith 
2861d763cef2SBarry Smith    Level: beginner
2862d763cef2SBarry Smith 
2863d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2864d763cef2SBarry Smith @*/
28656bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2866d763cef2SBarry Smith {
28676849ba73SBarry Smith   PetscErrorCode ierr;
2868d763cef2SBarry Smith 
2869d763cef2SBarry Smith   PetscFunctionBegin;
28706bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2871ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2872c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2873d763cef2SBarry Smith 
2874ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2875ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2876ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2877ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2878ecf68647SHong Zhang   }
2879e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2880e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28816bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28826d4c513bSLisandro Dalcin 
2883bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2884bc952696SBarry Smith 
288584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28866427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28876427ac75SLisandro Dalcin 
28886bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28896bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28906bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28910dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28926d4c513bSLisandro Dalcin 
2893cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2894a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2895d763cef2SBarry Smith   PetscFunctionReturn(0);
2896d763cef2SBarry Smith }
2897d763cef2SBarry Smith 
2898d8e5e3e6SSatish Balay /*@
2899d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2900d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2901d763cef2SBarry Smith 
2902d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2903d763cef2SBarry Smith 
2904d763cef2SBarry Smith    Input Parameter:
2905d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2906d763cef2SBarry Smith 
2907d763cef2SBarry Smith    Output Parameter:
2908d763cef2SBarry Smith .  snes - the nonlinear solver context
2909d763cef2SBarry Smith 
2910d763cef2SBarry Smith    Notes:
2911d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2912d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
291394b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2914d763cef2SBarry Smith 
2915d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
29160298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2917d763cef2SBarry Smith 
2918d763cef2SBarry Smith    Level: beginner
2919d763cef2SBarry Smith 
2920d763cef2SBarry Smith @*/
29217087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2922d763cef2SBarry Smith {
2923d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2924d372ba47SLisandro Dalcin 
2925d763cef2SBarry Smith   PetscFunctionBegin;
29260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29274482741eSBarry Smith   PetscValidPointer(snes,2);
2928d372ba47SLisandro Dalcin   if (!ts->snes) {
2929ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
293016413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
29310298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29323bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2933d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2934496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
29359e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
29369e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
29379e2a6581SJed Brown     }
2938d372ba47SLisandro Dalcin   }
2939d763cef2SBarry Smith   *snes = ts->snes;
2940d763cef2SBarry Smith   PetscFunctionReturn(0);
2941d763cef2SBarry Smith }
2942d763cef2SBarry Smith 
2943deb2cd25SJed Brown /*@
2944deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2945deb2cd25SJed Brown 
2946deb2cd25SJed Brown    Collective
2947deb2cd25SJed Brown 
2948deb2cd25SJed Brown    Input Parameter:
2949deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2950deb2cd25SJed Brown -  snes - the nonlinear solver context
2951deb2cd25SJed Brown 
2952deb2cd25SJed Brown    Notes:
2953deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2954deb2cd25SJed Brown 
2955deb2cd25SJed Brown    Level: developer
2956deb2cd25SJed Brown 
2957deb2cd25SJed Brown @*/
2958deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2959deb2cd25SJed Brown {
2960deb2cd25SJed Brown   PetscErrorCode ierr;
2961d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2962deb2cd25SJed Brown 
2963deb2cd25SJed Brown   PetscFunctionBegin;
2964deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2965deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2966deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2967deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2968bbd56ea5SKarl Rupp 
2969deb2cd25SJed Brown   ts->snes = snes;
2970bbd56ea5SKarl Rupp 
29710298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29720298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2973740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29740298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2975740132f1SEmil Constantinescu   }
2976deb2cd25SJed Brown   PetscFunctionReturn(0);
2977deb2cd25SJed Brown }
2978deb2cd25SJed Brown 
2979d8e5e3e6SSatish Balay /*@
298094b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2981d763cef2SBarry Smith    a TS (timestepper) context.
2982d763cef2SBarry Smith 
298394b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2984d763cef2SBarry Smith 
2985d763cef2SBarry Smith    Input Parameter:
2986d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2987d763cef2SBarry Smith 
2988d763cef2SBarry Smith    Output Parameter:
298994b7f48cSBarry Smith .  ksp - the nonlinear solver context
2990d763cef2SBarry Smith 
2991d763cef2SBarry Smith    Notes:
299294b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2993d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2994d763cef2SBarry Smith    KSP and PC contexts as well.
2995d763cef2SBarry Smith 
299694b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29970298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2998d763cef2SBarry Smith 
2999d763cef2SBarry Smith    Level: beginner
3000d763cef2SBarry Smith 
3001d763cef2SBarry Smith @*/
30027087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
3003d763cef2SBarry Smith {
3004d372ba47SLisandro Dalcin   PetscErrorCode ierr;
3005089b2837SJed Brown   SNES           snes;
3006d372ba47SLisandro Dalcin 
3007d763cef2SBarry Smith   PetscFunctionBegin;
30080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30094482741eSBarry Smith   PetscValidPointer(ksp,2);
301017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
3011e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
3012089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3013089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
3014d763cef2SBarry Smith   PetscFunctionReturn(0);
3015d763cef2SBarry Smith }
3016d763cef2SBarry Smith 
3017d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
3018d763cef2SBarry Smith 
3019adb62b0dSMatthew Knepley /*@
3020618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
3021618ce8baSLisandro Dalcin 
3022618ce8baSLisandro Dalcin    Logically Collective on TS
3023618ce8baSLisandro Dalcin 
3024618ce8baSLisandro Dalcin    Input Parameters:
3025618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3026618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
3027618ce8baSLisandro Dalcin 
3028618ce8baSLisandro Dalcin    Options Database Keys:
3029618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
3030618ce8baSLisandro Dalcin 
3031618ce8baSLisandro Dalcin    Notes:
3032618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
3033618ce8baSLisandro Dalcin 
3034618ce8baSLisandro Dalcin    Level: intermediate
3035618ce8baSLisandro Dalcin 
3036618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
3037618ce8baSLisandro Dalcin @*/
3038618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
3039618ce8baSLisandro Dalcin {
3040618ce8baSLisandro Dalcin   PetscFunctionBegin;
3041618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3042618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3043618ce8baSLisandro Dalcin   if (maxsteps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
3044618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
3045618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3046618ce8baSLisandro Dalcin }
3047618ce8baSLisandro Dalcin 
3048618ce8baSLisandro Dalcin /*@
3049618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
3050618ce8baSLisandro Dalcin 
3051618ce8baSLisandro Dalcin    Not Collective
3052618ce8baSLisandro Dalcin 
3053618ce8baSLisandro Dalcin    Input Parameters:
3054618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3055618ce8baSLisandro Dalcin 
3056618ce8baSLisandro Dalcin    Output Parameter:
3057618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3058618ce8baSLisandro Dalcin 
3059618ce8baSLisandro Dalcin    Level: advanced
3060618ce8baSLisandro Dalcin 
3061618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3062618ce8baSLisandro Dalcin @*/
3063618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3064618ce8baSLisandro Dalcin {
3065618ce8baSLisandro Dalcin   PetscFunctionBegin;
3066618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3067618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3068618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3069618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3070618ce8baSLisandro Dalcin }
3071618ce8baSLisandro Dalcin 
3072618ce8baSLisandro Dalcin /*@
3073618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3074618ce8baSLisandro Dalcin 
3075618ce8baSLisandro Dalcin    Logically Collective on TS
3076618ce8baSLisandro Dalcin 
3077618ce8baSLisandro Dalcin    Input Parameters:
3078618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3079618ce8baSLisandro Dalcin -  maxtime - final time to step to
3080618ce8baSLisandro Dalcin 
3081618ce8baSLisandro Dalcin    Options Database Keys:
3082ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3083618ce8baSLisandro Dalcin 
3084618ce8baSLisandro Dalcin    Notes:
3085618ce8baSLisandro Dalcin    The default maximum time is 5.0
3086618ce8baSLisandro Dalcin 
3087618ce8baSLisandro Dalcin    Level: intermediate
3088618ce8baSLisandro Dalcin 
3089618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3090618ce8baSLisandro Dalcin @*/
3091618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3092618ce8baSLisandro Dalcin {
3093618ce8baSLisandro Dalcin   PetscFunctionBegin;
3094618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3095618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3096618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3097618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3098618ce8baSLisandro Dalcin }
3099618ce8baSLisandro Dalcin 
3100618ce8baSLisandro Dalcin /*@
3101618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3102618ce8baSLisandro Dalcin 
3103618ce8baSLisandro Dalcin    Not Collective
3104618ce8baSLisandro Dalcin 
3105618ce8baSLisandro Dalcin    Input Parameters:
3106618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3107618ce8baSLisandro Dalcin 
3108618ce8baSLisandro Dalcin    Output Parameter:
3109618ce8baSLisandro Dalcin .  maxtime - final time to step to
3110618ce8baSLisandro Dalcin 
3111618ce8baSLisandro Dalcin    Level: advanced
3112618ce8baSLisandro Dalcin 
3113618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3114618ce8baSLisandro Dalcin @*/
3115618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3116618ce8baSLisandro Dalcin {
3117618ce8baSLisandro Dalcin   PetscFunctionBegin;
3118618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3119618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3120618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3121618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3122618ce8baSLisandro Dalcin }
3123618ce8baSLisandro Dalcin 
3124618ce8baSLisandro Dalcin /*@
3125aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3126edc382c3SSatish Balay 
3127edc382c3SSatish Balay    Level: deprecated
3128edc382c3SSatish Balay 
3129aaa6c58dSLisandro Dalcin @*/
3130aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3131aaa6c58dSLisandro Dalcin {
3132aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3133aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3134aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3135aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3136aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3137aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3138aaa6c58dSLisandro Dalcin }
3139aaa6c58dSLisandro Dalcin 
3140aaa6c58dSLisandro Dalcin /*@
314119eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3142edc382c3SSatish Balay 
3143edc382c3SSatish Balay    Level: deprecated
3144edc382c3SSatish Balay 
3145adb62b0dSMatthew Knepley @*/
31467087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3147adb62b0dSMatthew Knepley {
3148adb62b0dSMatthew Knepley   PetscFunctionBegin;
31490700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3150abc0a331SBarry Smith   if (maxsteps) {
31514482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3152adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3153adb62b0dSMatthew Knepley   }
3154abc0a331SBarry Smith   if (maxtime) {
31554482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3156adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3157adb62b0dSMatthew Knepley   }
3158adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3159adb62b0dSMatthew Knepley }
3160adb62b0dSMatthew Knepley 
3161d763cef2SBarry Smith /*@
316219eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3163edc382c3SSatish Balay 
3164edc382c3SSatish Balay    Level: deprecated
3165edc382c3SSatish Balay 
3166d763cef2SBarry Smith @*/
31677087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3168d763cef2SBarry Smith {
3169d763cef2SBarry Smith   PetscFunctionBegin;
31700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3171c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3172c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
317339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
317439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3175d763cef2SBarry Smith   PetscFunctionReturn(0);
3176d763cef2SBarry Smith }
3177d763cef2SBarry Smith 
3178d763cef2SBarry Smith /*@
31795c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3180edc382c3SSatish Balay 
3181edc382c3SSatish Balay    Level: deprecated
3182edc382c3SSatish Balay 
31835c5f5948SLisandro Dalcin @*/
3184e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31855c5f5948SLisandro Dalcin 
318619eac22cSLisandro Dalcin /*@
31874f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3188edc382c3SSatish Balay 
3189edc382c3SSatish Balay    Level: deprecated
3190edc382c3SSatish Balay 
31914f4e0956SLisandro Dalcin @*/
31924f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31934f4e0956SLisandro Dalcin 
31944f4e0956SLisandro Dalcin /*@
3195d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3196d763cef2SBarry Smith    for use by the TS routines.
3197d763cef2SBarry Smith 
3198d083f849SBarry Smith    Logically Collective on TS
3199d763cef2SBarry Smith 
3200d763cef2SBarry Smith    Input Parameters:
3201d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
32020910c330SBarry Smith -  u - the solution vector
3203d763cef2SBarry Smith 
3204d763cef2SBarry Smith    Level: beginner
3205d763cef2SBarry Smith 
32060ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3207d763cef2SBarry Smith @*/
32080910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3209d763cef2SBarry Smith {
32108737fe31SLisandro Dalcin   PetscErrorCode ierr;
3211496e6a7aSJed Brown   DM             dm;
32128737fe31SLisandro Dalcin 
3213d763cef2SBarry Smith   PetscFunctionBegin;
32140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32150910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32160910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
32176bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
32180910c330SBarry Smith   ts->vec_sol = u;
3219bbd56ea5SKarl Rupp 
3220496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
32210910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3222d763cef2SBarry Smith   PetscFunctionReturn(0);
3223d763cef2SBarry Smith }
3224d763cef2SBarry Smith 
3225ac226902SBarry Smith /*@C
3226000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32273f2090d5SJed Brown   called once at the beginning of each time step.
3228000e7ae3SMatthew Knepley 
32293f9fe445SBarry Smith   Logically Collective on TS
3230000e7ae3SMatthew Knepley 
3231000e7ae3SMatthew Knepley   Input Parameters:
3232000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3233000e7ae3SMatthew Knepley - func - The function
3234000e7ae3SMatthew Knepley 
3235000e7ae3SMatthew Knepley   Calling sequence of func:
32366bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3237000e7ae3SMatthew Knepley 
3238000e7ae3SMatthew Knepley   Level: intermediate
3239000e7ae3SMatthew Knepley 
3240dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3241000e7ae3SMatthew Knepley @*/
32427087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3243000e7ae3SMatthew Knepley {
3244000e7ae3SMatthew Knepley   PetscFunctionBegin;
32450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3246ae60f76fSBarry Smith   ts->prestep = func;
3247000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3248000e7ae3SMatthew Knepley }
3249000e7ae3SMatthew Knepley 
325009ee8438SJed Brown /*@
32513f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32523f2090d5SJed Brown 
32533f2090d5SJed Brown   Collective on TS
32543f2090d5SJed Brown 
32553f2090d5SJed Brown   Input Parameters:
32563f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32573f2090d5SJed Brown 
32583f2090d5SJed Brown   Notes:
32593f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32603f2090d5SJed Brown   so most users would not generally call this routine themselves.
32613f2090d5SJed Brown 
32623f2090d5SJed Brown   Level: developer
32633f2090d5SJed Brown 
32649be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32653f2090d5SJed Brown @*/
32667087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32673f2090d5SJed Brown {
32683f2090d5SJed Brown   PetscErrorCode ierr;
32693f2090d5SJed Brown 
32703f2090d5SJed Brown   PetscFunctionBegin;
32710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3272ae60f76fSBarry Smith   if (ts->prestep) {
32735efd42a4SStefano Zampini     Vec              U;
32745efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32755efd42a4SStefano Zampini 
32765efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32775efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3278ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32795efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3280dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3281312ce896SJed Brown   }
32823f2090d5SJed Brown   PetscFunctionReturn(0);
32833f2090d5SJed Brown }
32843f2090d5SJed Brown 
3285b8123daeSJed Brown /*@C
3286b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3287b8123daeSJed Brown   called once at the beginning of each stage.
3288b8123daeSJed Brown 
3289b8123daeSJed Brown   Logically Collective on TS
3290b8123daeSJed Brown 
3291b8123daeSJed Brown   Input Parameters:
3292b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3293b8123daeSJed Brown - func - The function
3294b8123daeSJed Brown 
3295b8123daeSJed Brown   Calling sequence of func:
3296b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3297b8123daeSJed Brown 
3298b8123daeSJed Brown   Level: intermediate
3299b8123daeSJed Brown 
3300b8123daeSJed Brown   Note:
3301b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
330280275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3303b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3304b8123daeSJed Brown 
33059be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3306b8123daeSJed Brown @*/
3307b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3308b8123daeSJed Brown {
3309b8123daeSJed Brown   PetscFunctionBegin;
3310b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3311ae60f76fSBarry Smith   ts->prestage = func;
3312b8123daeSJed Brown   PetscFunctionReturn(0);
3313b8123daeSJed Brown }
3314b8123daeSJed Brown 
33159be3e283SDebojyoti Ghosh /*@C
33169be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33179be3e283SDebojyoti Ghosh   called once at the end of each stage.
33189be3e283SDebojyoti Ghosh 
33199be3e283SDebojyoti Ghosh   Logically Collective on TS
33209be3e283SDebojyoti Ghosh 
33219be3e283SDebojyoti Ghosh   Input Parameters:
33229be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33239be3e283SDebojyoti Ghosh - func - The function
33249be3e283SDebojyoti Ghosh 
33259be3e283SDebojyoti Ghosh   Calling sequence of func:
33269be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33279be3e283SDebojyoti Ghosh 
33289be3e283SDebojyoti Ghosh   Level: intermediate
33299be3e283SDebojyoti Ghosh 
33309be3e283SDebojyoti Ghosh   Note:
33319be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
333280275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
33339be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33349be3e283SDebojyoti Ghosh 
33359be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33369be3e283SDebojyoti Ghosh @*/
33379be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33389be3e283SDebojyoti Ghosh {
33399be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33409be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33419be3e283SDebojyoti Ghosh   ts->poststage = func;
33429be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33439be3e283SDebojyoti Ghosh }
33449be3e283SDebojyoti Ghosh 
3345c688d042SShri Abhyankar /*@C
3346c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3347c688d042SShri Abhyankar   called once at the end of each step evaluation.
3348c688d042SShri Abhyankar 
3349c688d042SShri Abhyankar   Logically Collective on TS
3350c688d042SShri Abhyankar 
3351c688d042SShri Abhyankar   Input Parameters:
3352c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3353c688d042SShri Abhyankar - func - The function
3354c688d042SShri Abhyankar 
3355c688d042SShri Abhyankar   Calling sequence of func:
3356c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3357c688d042SShri Abhyankar 
3358c688d042SShri Abhyankar   Level: intermediate
3359c688d042SShri Abhyankar 
3360c688d042SShri Abhyankar   Note:
33611785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33621785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3363e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3364e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3365e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3366c688d042SShri Abhyankar 
3367c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3368c688d042SShri Abhyankar @*/
3369c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3370c688d042SShri Abhyankar {
3371c688d042SShri Abhyankar   PetscFunctionBegin;
3372c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3373c688d042SShri Abhyankar   ts->postevaluate = func;
3374c688d042SShri Abhyankar   PetscFunctionReturn(0);
3375c688d042SShri Abhyankar }
3376c688d042SShri Abhyankar 
3377b8123daeSJed Brown /*@
3378b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3379b8123daeSJed Brown 
3380b8123daeSJed Brown   Collective on TS
3381b8123daeSJed Brown 
3382b8123daeSJed Brown   Input Parameters:
3383b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33849be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3385b8123daeSJed Brown 
3386b8123daeSJed Brown   Notes:
3387b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3388b8123daeSJed Brown   most users would not generally call this routine themselves.
3389b8123daeSJed Brown 
3390b8123daeSJed Brown   Level: developer
3391b8123daeSJed Brown 
33929be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3393b8123daeSJed Brown @*/
3394b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3395b8123daeSJed Brown {
3396b8123daeSJed Brown   PetscFunctionBegin;
3397b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3398ae60f76fSBarry Smith   if (ts->prestage) {
3399ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3400b8123daeSJed Brown   }
3401b8123daeSJed Brown   PetscFunctionReturn(0);
3402b8123daeSJed Brown }
3403b8123daeSJed Brown 
34049be3e283SDebojyoti Ghosh /*@
34059be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34069be3e283SDebojyoti Ghosh 
34079be3e283SDebojyoti Ghosh   Collective on TS
34089be3e283SDebojyoti Ghosh 
34099be3e283SDebojyoti Ghosh   Input Parameters:
34109be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34119be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34129be3e283SDebojyoti Ghosh   stageindex  - Stage number
34139be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34149be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34159be3e283SDebojyoti Ghosh 
34169be3e283SDebojyoti Ghosh   Notes:
34179be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34189be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34199be3e283SDebojyoti Ghosh 
34209be3e283SDebojyoti Ghosh   Level: developer
34219be3e283SDebojyoti Ghosh 
34229be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34239be3e283SDebojyoti Ghosh @*/
34249be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34259be3e283SDebojyoti Ghosh {
34269be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34279be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34284beae5d8SLisandro Dalcin   if (ts->poststage) {
34299be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34309be3e283SDebojyoti Ghosh   }
34319be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34329be3e283SDebojyoti Ghosh }
34339be3e283SDebojyoti Ghosh 
3434c688d042SShri Abhyankar /*@
3435c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3436c688d042SShri Abhyankar 
3437c688d042SShri Abhyankar   Collective on TS
3438c688d042SShri Abhyankar 
3439c688d042SShri Abhyankar   Input Parameters:
3440c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3441c688d042SShri Abhyankar 
3442c688d042SShri Abhyankar   Notes:
3443c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3444c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3445c688d042SShri Abhyankar 
3446c688d042SShri Abhyankar   Level: developer
3447c688d042SShri Abhyankar 
3448c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3449c688d042SShri Abhyankar @*/
3450c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3451c688d042SShri Abhyankar {
3452c688d042SShri Abhyankar   PetscErrorCode ierr;
3453c688d042SShri Abhyankar 
3454c688d042SShri Abhyankar   PetscFunctionBegin;
3455c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3456c688d042SShri Abhyankar   if (ts->postevaluate) {
3457dcb233daSLisandro Dalcin     Vec              U;
3458dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3459dcb233daSLisandro Dalcin 
3460dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3461dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3462c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3463dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3464dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3465c688d042SShri Abhyankar   }
3466c688d042SShri Abhyankar   PetscFunctionReturn(0);
3467c688d042SShri Abhyankar }
3468c688d042SShri Abhyankar 
3469ac226902SBarry Smith /*@C
3470000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34713f2090d5SJed Brown   called once at the end of each time step.
3472000e7ae3SMatthew Knepley 
34733f9fe445SBarry Smith   Logically Collective on TS
3474000e7ae3SMatthew Knepley 
3475000e7ae3SMatthew Knepley   Input Parameters:
3476000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3477000e7ae3SMatthew Knepley - func - The function
3478000e7ae3SMatthew Knepley 
3479000e7ae3SMatthew Knepley   Calling sequence of func:
3480b8123daeSJed Brown $ func (TS ts);
3481000e7ae3SMatthew Knepley 
34821785ff2aSShri Abhyankar   Notes:
34831785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34841785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34851785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34861785ff2aSShri Abhyankar 
3487000e7ae3SMatthew Knepley   Level: intermediate
3488000e7ae3SMatthew Knepley 
3489dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3490000e7ae3SMatthew Knepley @*/
34917087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3492000e7ae3SMatthew Knepley {
3493000e7ae3SMatthew Knepley   PetscFunctionBegin;
34940700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3495ae60f76fSBarry Smith   ts->poststep = func;
3496000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3497000e7ae3SMatthew Knepley }
3498000e7ae3SMatthew Knepley 
349909ee8438SJed Brown /*@
35003f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
35013f2090d5SJed Brown 
35023f2090d5SJed Brown   Collective on TS
35033f2090d5SJed Brown 
35043f2090d5SJed Brown   Input Parameters:
35053f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35063f2090d5SJed Brown 
35073f2090d5SJed Brown   Notes:
35083f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35093f2090d5SJed Brown   so most users would not generally call this routine themselves.
35103f2090d5SJed Brown 
35113f2090d5SJed Brown   Level: developer
35123f2090d5SJed Brown 
35133f2090d5SJed Brown @*/
35147087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35153f2090d5SJed Brown {
35163f2090d5SJed Brown   PetscErrorCode ierr;
35173f2090d5SJed Brown 
35183f2090d5SJed Brown   PetscFunctionBegin;
35190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3520ae60f76fSBarry Smith   if (ts->poststep) {
35215efd42a4SStefano Zampini     Vec              U;
35225efd42a4SStefano Zampini     PetscObjectState sprev,spost;
35235efd42a4SStefano Zampini 
35245efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
35255efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3526ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
35275efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3528dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
352972ac3e02SJed Brown   }
35303f2090d5SJed Brown   PetscFunctionReturn(0);
35313f2090d5SJed Brown }
35323f2090d5SJed Brown 
3533d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3534d763cef2SBarry Smith 
3535d763cef2SBarry Smith /*@C
3536a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3537d763cef2SBarry Smith    timestep to display the iteration's  progress.
3538d763cef2SBarry Smith 
35393f9fe445SBarry Smith    Logically Collective on TS
3540d763cef2SBarry Smith 
3541d763cef2SBarry Smith    Input Parameters:
3542d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3543e213d8f1SJed Brown .  monitor - monitoring routine
3544329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35450298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3546b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35470298fd71SBarry Smith           (may be NULL)
3548d763cef2SBarry Smith 
3549e213d8f1SJed Brown    Calling sequence of monitor:
3550dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3551d763cef2SBarry Smith 
3552d763cef2SBarry Smith +    ts - the TS context
355363e21af5SBarry 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)
35541f06c33eSBarry Smith .    time - current time
35550910c330SBarry Smith .    u - current iterate
3556d763cef2SBarry Smith -    mctx - [optional] monitoring context
3557d763cef2SBarry Smith 
3558d763cef2SBarry Smith    Notes:
3559d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3560d763cef2SBarry Smith    already has been loaded.
3561d763cef2SBarry Smith 
356295452b02SPatrick Sanan    Fortran Notes:
356395452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3564025f1a04SBarry Smith 
3565d763cef2SBarry Smith    Level: intermediate
3566d763cef2SBarry Smith 
3567a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3568d763cef2SBarry Smith @*/
3569c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3570d763cef2SBarry Smith {
357178064530SBarry Smith   PetscErrorCode ierr;
357278064530SBarry Smith   PetscInt       i;
357378064530SBarry Smith   PetscBool      identical;
357478064530SBarry Smith 
3575d763cef2SBarry Smith   PetscFunctionBegin;
35760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
357778064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
357878064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
357978064530SBarry Smith     if (identical) PetscFunctionReturn(0);
358078064530SBarry Smith   }
358117186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3582d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35838704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3584d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3585d763cef2SBarry Smith   PetscFunctionReturn(0);
3586d763cef2SBarry Smith }
3587d763cef2SBarry Smith 
3588d763cef2SBarry Smith /*@C
3589a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3590d763cef2SBarry Smith 
35913f9fe445SBarry Smith    Logically Collective on TS
3592d763cef2SBarry Smith 
3593d763cef2SBarry Smith    Input Parameters:
3594d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3595d763cef2SBarry Smith 
3596d763cef2SBarry Smith    Notes:
3597d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3598d763cef2SBarry Smith 
3599d763cef2SBarry Smith    Level: intermediate
3600d763cef2SBarry Smith 
3601a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3602d763cef2SBarry Smith @*/
36037087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3604d763cef2SBarry Smith {
3605d952e501SBarry Smith   PetscErrorCode ierr;
3606d952e501SBarry Smith   PetscInt       i;
3607d952e501SBarry Smith 
3608d763cef2SBarry Smith   PetscFunctionBegin;
36090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3610d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36118704b422SBarry Smith     if (ts->monitordestroy[i]) {
36128704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3613d952e501SBarry Smith     }
3614d952e501SBarry Smith   }
3615d763cef2SBarry Smith   ts->numbermonitors = 0;
3616d763cef2SBarry Smith   PetscFunctionReturn(0);
3617d763cef2SBarry Smith }
3618d763cef2SBarry Smith 
3619721cd6eeSBarry Smith /*@C
3620721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36215516499fSSatish Balay 
36225516499fSSatish Balay    Level: intermediate
362341251cbbSSatish Balay 
362463e21af5SBarry Smith .seealso:  TSMonitorSet()
362541251cbbSSatish Balay @*/
3626721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3627d763cef2SBarry Smith {
3628dfbe8321SBarry Smith   PetscErrorCode ierr;
3629721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
363041aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3631d132466eSBarry Smith 
3632d763cef2SBarry Smith   PetscFunctionBegin;
36334d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
363441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
363541aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3636721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
363741aca3d6SBarry Smith   if (iascii) {
3638649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
363963e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
364063e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
364163e21af5SBarry Smith     } else {
36428392e04aSShri 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);
364363e21af5SBarry Smith     }
3644649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
364541aca3d6SBarry Smith   } else if (ibinary) {
364641aca3d6SBarry Smith     PetscMPIInt rank;
3647ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRMPI(ierr);
364841aca3d6SBarry Smith     if (!rank) {
3649450a797fSBarry Smith       PetscBool skipHeader;
3650450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3651450a797fSBarry Smith 
3652450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3653450a797fSBarry Smith       if (!skipHeader) {
3654f253e43cSLisandro Dalcin          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
3655450a797fSBarry Smith        }
365641aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
365741aca3d6SBarry Smith     } else {
365841aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
365941aca3d6SBarry Smith     }
366041aca3d6SBarry Smith   }
3661721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3662d763cef2SBarry Smith   PetscFunctionReturn(0);
3663d763cef2SBarry Smith }
3664d763cef2SBarry Smith 
3665cc9c3a59SBarry Smith /*@C
3666cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3667cc9c3a59SBarry Smith 
3668cc9c3a59SBarry Smith    Level: intermediate
3669cc9c3a59SBarry Smith 
3670cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3671cc9c3a59SBarry Smith @*/
3672cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3673cc9c3a59SBarry Smith {
3674cc9c3a59SBarry Smith   PetscErrorCode ierr;
3675cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3676cc9c3a59SBarry Smith   PetscBool      iascii;
3677cc9c3a59SBarry Smith   PetscReal      max,min;
3678cc9c3a59SBarry Smith 
3679cc9c3a59SBarry Smith 
3680cc9c3a59SBarry Smith   PetscFunctionBegin;
3681cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3682cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3683cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3684cc9c3a59SBarry Smith   if (iascii) {
3685cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3686cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3687cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
36885132926bSBarry 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);
3689cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3690cc9c3a59SBarry Smith   }
3691cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3692cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3693cc9c3a59SBarry Smith }
3694cc9c3a59SBarry Smith 
3695cd652676SJed Brown /*@
3696cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3697cd652676SJed Brown 
3698cd652676SJed Brown    Collective on TS
3699cd652676SJed Brown 
3700cd652676SJed Brown    Input Argument:
3701cd652676SJed Brown +  ts - time stepping context
3702cd652676SJed Brown -  t - time to interpolate to
3703cd652676SJed Brown 
3704cd652676SJed Brown    Output Argument:
37050910c330SBarry Smith .  U - state at given time
3706cd652676SJed Brown 
3707cd652676SJed Brown    Level: intermediate
3708cd652676SJed Brown 
3709cd652676SJed Brown    Developer Notes:
3710cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3711cd652676SJed Brown 
3712874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3713cd652676SJed Brown @*/
37140910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3715cd652676SJed Brown {
3716cd652676SJed Brown   PetscErrorCode ierr;
3717cd652676SJed Brown 
3718cd652676SJed Brown   PetscFunctionBegin;
3719cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3720b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3721be5899b3SLisandro 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);
3722ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
37230910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3724cd652676SJed Brown   PetscFunctionReturn(0);
3725cd652676SJed Brown }
3726cd652676SJed Brown 
3727d763cef2SBarry Smith /*@
37286d9e5789SSean Farley    TSStep - Steps one time step
3729d763cef2SBarry Smith 
3730d763cef2SBarry Smith    Collective on TS
3731d763cef2SBarry Smith 
3732d763cef2SBarry Smith    Input Parameter:
3733d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3734d763cef2SBarry Smith 
373527829d71SBarry Smith    Level: developer
3736d763cef2SBarry Smith 
3737b8123daeSJed Brown    Notes:
373827829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
373927829d71SBarry Smith 
3740b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3741b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3742b8123daeSJed Brown 
374319eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
374419eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
374525cb2221SBarry Smith 
37469be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3747d763cef2SBarry Smith @*/
3748193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3749d763cef2SBarry Smith {
3750dfbe8321SBarry Smith   PetscErrorCode   ierr;
3751fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3752be5899b3SLisandro Dalcin   PetscReal        ptime;
3753d763cef2SBarry Smith 
3754d763cef2SBarry Smith   PetscFunctionBegin;
37550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3756f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3757fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3758f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3759f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3760f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3761f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3762f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3763fffbeea8SBarry Smith 
3764d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
376568bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3766d405a339SMatthew Knepley 
3767fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3768ef85077eSLisandro 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>");
3769a6772fa2SLisandro 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()");
3770be5899b3SLisandro 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");
3771a6772fa2SLisandro Dalcin 
3772be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3773be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37742808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3775fc8dbba5SLisandro Dalcin 
3776d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3777193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3778d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3779fc8dbba5SLisandro Dalcin 
3780fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3781be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
37822808aa04SLisandro Dalcin     ts->steps++;
3783be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
378428d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3785d2daff3dSHong Zhang   }
3786fc8dbba5SLisandro Dalcin 
3787fc8dbba5SLisandro Dalcin   if (!ts->reason) {
378808c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
378908c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
379008c7845fSBarry Smith   }
3791fc8dbba5SLisandro Dalcin 
3792fc8dbba5SLisandro 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]);
3793fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
379408c7845fSBarry Smith   PetscFunctionReturn(0);
379508c7845fSBarry Smith }
379608c7845fSBarry Smith 
379708c7845fSBarry Smith /*@
37987cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37997cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
38007cbde773SLisandro Dalcin 
38017cbde773SLisandro Dalcin    Collective on TS
38027cbde773SLisandro Dalcin 
38037cbde773SLisandro Dalcin    Input Arguments:
38047cbde773SLisandro Dalcin +  ts - time stepping context
38057cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
38067cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
38077cbde773SLisandro Dalcin 
38087cbde773SLisandro Dalcin    Output Arguments:
38097cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
38107cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
38117cbde773SLisandro Dalcin 
38127cbde773SLisandro Dalcin    Level: advanced
38137cbde773SLisandro Dalcin 
38147cbde773SLisandro Dalcin    Notes:
38157cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
38167cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
38177cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
38187cbde773SLisandro Dalcin 
38197cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
38207cbde773SLisandro Dalcin @*/
38217cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
38227cbde773SLisandro Dalcin {
38237cbde773SLisandro Dalcin   PetscErrorCode ierr;
38247cbde773SLisandro Dalcin 
38257cbde773SLisandro Dalcin   PetscFunctionBegin;
38267cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38277cbde773SLisandro Dalcin   PetscValidType(ts,1);
38287cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
38297cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
38307cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
38317cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
38327cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
38337cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
38347cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
38357cbde773SLisandro Dalcin   PetscFunctionReturn(0);
38367cbde773SLisandro Dalcin }
38377cbde773SLisandro Dalcin 
383805175c85SJed Brown /*@
383905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
384005175c85SJed Brown 
38411c3436cfSJed Brown    Collective on TS
384205175c85SJed Brown 
384305175c85SJed Brown    Input Arguments:
38441c3436cfSJed Brown +  ts - time stepping context
38451c3436cfSJed Brown .  order - desired order of accuracy
38460298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
384705175c85SJed Brown 
384805175c85SJed Brown    Output Arguments:
38490910c330SBarry Smith .  U - state at the end of the current step
385005175c85SJed Brown 
385105175c85SJed Brown    Level: advanced
385205175c85SJed Brown 
3853108c343cSJed Brown    Notes:
3854108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3855108c343cSJed 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.
3856108c343cSJed Brown 
38571c3436cfSJed Brown .seealso: TSStep(), TSAdapt
385805175c85SJed Brown @*/
38590910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
386005175c85SJed Brown {
386105175c85SJed Brown   PetscErrorCode ierr;
386205175c85SJed Brown 
386305175c85SJed Brown   PetscFunctionBegin;
386405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
386505175c85SJed Brown   PetscValidType(ts,1);
38660910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3867ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38680910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
386905175c85SJed Brown   PetscFunctionReturn(0);
387005175c85SJed Brown }
387105175c85SJed Brown 
3872aad739acSMatthew G. Knepley /*@C
38732e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3874aad739acSMatthew G. Knepley 
3875aad739acSMatthew G. Knepley   Not collective
3876aad739acSMatthew G. Knepley 
3877aad739acSMatthew G. Knepley   Input Argument:
3878aad739acSMatthew G. Knepley . ts        - time stepping context
3879aad739acSMatthew G. Knepley 
3880aad739acSMatthew G. Knepley   Output Argument:
38812e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3882aad739acSMatthew G. Knepley 
3883aad739acSMatthew G. Knepley    Level: advanced
3884aad739acSMatthew G. Knepley 
3885aad739acSMatthew G. Knepley    Notes:
3886aad739acSMatthew G. Knepley    The calling sequence for the function is
38872e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3888aad739acSMatthew G. Knepley $ ts - The timestepping context
38892e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3890aad739acSMatthew G. Knepley 
38912e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3892aad739acSMatthew G. Knepley @*/
38932e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3894aad739acSMatthew G. Knepley {
3895aad739acSMatthew G. Knepley   PetscFunctionBegin;
3896aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38972e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38982e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3899aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3900aad739acSMatthew G. Knepley }
3901aad739acSMatthew G. Knepley 
3902aad739acSMatthew G. Knepley /*@C
39032e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3904aad739acSMatthew G. Knepley 
3905aad739acSMatthew G. Knepley   Logically collective on ts
3906aad739acSMatthew G. Knepley 
3907aad739acSMatthew G. Knepley   Input Arguments:
3908aad739acSMatthew G. Knepley + ts        - time stepping context
39092e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3910aad739acSMatthew G. Knepley 
3911aad739acSMatthew G. Knepley   Level: advanced
3912aad739acSMatthew G. Knepley 
3913a96d6ef6SBarry Smith   Calling sequence for initCondition:
3914a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3915a96d6ef6SBarry Smith 
3916a96d6ef6SBarry Smith + ts - The timestepping context
3917a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3918aad739acSMatthew G. Knepley 
39192e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3920aad739acSMatthew G. Knepley @*/
39212e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3922aad739acSMatthew G. Knepley {
3923aad739acSMatthew G. Knepley   PetscFunctionBegin;
3924aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
39252e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
39262e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3927aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3928aad739acSMatthew G. Knepley }
3929aad739acSMatthew G. Knepley 
3930aad739acSMatthew G. Knepley /*@
39312e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3932aad739acSMatthew G. Knepley 
3933aad739acSMatthew G. Knepley   Collective on ts
3934aad739acSMatthew G. Knepley 
3935aad739acSMatthew G. Knepley   Input Arguments:
3936aad739acSMatthew G. Knepley + ts - time stepping context
39372e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3938aad739acSMatthew G. Knepley 
3939aad739acSMatthew G. Knepley   Level: advanced
3940aad739acSMatthew G. Knepley 
39412e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3942aad739acSMatthew G. Knepley @*/
39432e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3944aad739acSMatthew G. Knepley {
3945aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3946aad739acSMatthew G. Knepley 
3947aad739acSMatthew G. Knepley   PetscFunctionBegin;
3948aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3949aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
39502e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3951aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3952aad739acSMatthew G. Knepley }
3953aad739acSMatthew G. Knepley 
3954aad739acSMatthew G. Knepley /*@C
3955aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3956aad739acSMatthew G. Knepley 
3957aad739acSMatthew G. Knepley   Not collective
3958aad739acSMatthew G. Knepley 
3959aad739acSMatthew G. Knepley   Input Argument:
3960aad739acSMatthew G. Knepley . ts         - time stepping context
3961aad739acSMatthew G. Knepley 
3962aad739acSMatthew G. Knepley   Output Argument:
3963aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3964aad739acSMatthew G. Knepley 
3965aad739acSMatthew G. Knepley   Level: advanced
3966aad739acSMatthew G. Knepley 
3967a96d6ef6SBarry Smith   Calling sequence for exactError:
3968a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3969a96d6ef6SBarry Smith 
3970a96d6ef6SBarry Smith + ts - The timestepping context
3971a96d6ef6SBarry Smith . u  - The approximate solution vector
3972a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3973aad739acSMatthew G. Knepley 
3974aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3975aad739acSMatthew G. Knepley @*/
3976aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3977aad739acSMatthew G. Knepley {
3978aad739acSMatthew G. Knepley   PetscFunctionBegin;
3979aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3980aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3981aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3982aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3983aad739acSMatthew G. Knepley }
3984aad739acSMatthew G. Knepley 
3985aad739acSMatthew G. Knepley /*@C
3986aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3987aad739acSMatthew G. Knepley 
3988aad739acSMatthew G. Knepley   Logically collective on ts
3989aad739acSMatthew G. Knepley 
3990aad739acSMatthew G. Knepley   Input Arguments:
3991aad739acSMatthew G. Knepley + ts         - time stepping context
3992aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3993aad739acSMatthew G. Knepley 
3994aad739acSMatthew G. Knepley   Level: advanced
3995aad739acSMatthew G. Knepley 
3996a96d6ef6SBarry Smith   Calling sequence for exactError:
3997a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3998a96d6ef6SBarry Smith 
3999a96d6ef6SBarry Smith + ts - The timestepping context
4000a96d6ef6SBarry Smith . u  - The approximate solution vector
4001a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
4002aad739acSMatthew G. Knepley 
4003aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
4004aad739acSMatthew G. Knepley @*/
4005aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
4006aad739acSMatthew G. Knepley {
4007aad739acSMatthew G. Knepley   PetscFunctionBegin;
4008aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4009f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
4010aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
4011aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4012aad739acSMatthew G. Knepley }
4013aad739acSMatthew G. Knepley 
4014aad739acSMatthew G. Knepley /*@
4015aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
4016aad739acSMatthew G. Knepley 
4017aad739acSMatthew G. Knepley   Collective on ts
4018aad739acSMatthew G. Knepley 
4019aad739acSMatthew G. Knepley   Input Arguments:
4020aad739acSMatthew G. Knepley + ts - time stepping context
4021aad739acSMatthew G. Knepley . u  - The approximate solution
4022aad739acSMatthew G. Knepley - e  - The Vec used to store the error
4023aad739acSMatthew G. Knepley 
4024aad739acSMatthew G. Knepley   Level: advanced
4025aad739acSMatthew G. Knepley 
40262e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
4027aad739acSMatthew G. Knepley @*/
4028aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
4029aad739acSMatthew G. Knepley {
4030aad739acSMatthew G. Knepley   PetscErrorCode ierr;
4031aad739acSMatthew G. Knepley 
4032aad739acSMatthew G. Knepley   PetscFunctionBegin;
4033aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4034aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
4035aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
4036aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
4037aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4038aad739acSMatthew G. Knepley }
4039aad739acSMatthew G. Knepley 
4040b1cb36f3SHong Zhang /*@
40416a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
40426a4d4014SLisandro Dalcin 
40436a4d4014SLisandro Dalcin    Collective on TS
40446a4d4014SLisandro Dalcin 
40456a4d4014SLisandro Dalcin    Input Parameter:
40466a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
404763e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
404863e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
40495a3a76d0SJed Brown 
40506a4d4014SLisandro Dalcin    Level: beginner
40516a4d4014SLisandro Dalcin 
40525a3a76d0SJed Brown    Notes:
40535a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
40545a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40555a3a76d0SJed Brown    stepped over the final time.
40565a3a76d0SJed Brown 
405763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
40586a4d4014SLisandro Dalcin @*/
4059cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
40606a4d4014SLisandro Dalcin {
4061b06615a5SLisandro Dalcin   Vec               solution;
40626a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
4063f22f69f0SBarry Smith 
40646a4d4014SLisandro Dalcin   PetscFunctionBegin;
40650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4066f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4067303a5415SBarry Smith 
4068303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
4069ee41a567SStefano 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 */
40700910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4071b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4072b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4073b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40745a3a76d0SJed Brown     }
40750910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4076715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4077bbd56ea5SKarl Rupp   } else if (u) {
40780910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40795a3a76d0SJed Brown   }
4080b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
408168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4082a6772fa2SLisandro Dalcin 
4083ef85077eSLisandro 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>");
4084a6772fa2SLisandro 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()");
4085a6772fa2SLisandro 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");
4086a6772fa2SLisandro Dalcin 
4087715f1b00SHong Zhang   if (ts->forward_solve) {
4088715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4089715f1b00SHong Zhang   }
4090715f1b00SHong Zhang 
4091e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4092715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4093e7069c78SShri      TSTrajectory to incorrectly save the output files
4094e7069c78SShri   */
4095715f1b00SHong Zhang   /* reset time step and iteration counters */
40962808aa04SLisandro Dalcin   if (!ts->steps) {
40975ef26d82SJed Brown     ts->ksp_its           = 0;
40985ef26d82SJed Brown     ts->snes_its          = 0;
4099c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4100c610991cSLisandro Dalcin     ts->reject            = 0;
41012808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
41022808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
41037d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
41042808aa04SLisandro Dalcin   }
4105e97c63d7SStefano Zampini 
4106e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4107e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4108e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4109e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4110e97c63d7SStefano Zampini 
4111e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4112e97c63d7SStefano Zampini   }
4113193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4114193ac0bcSJed Brown 
4115900f6b5bSMatthew G. Knepley   {
4116900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4117900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4118900f6b5bSMatthew G. Knepley     PetscBool         flg;
4119900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4120900f6b5bSMatthew G. Knepley 
4121900f6b5bSMatthew G. Knepley     if (!incall) {
4122900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4123900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4124900f6b5bSMatthew G. Knepley       if (flg) {
4125900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4126900f6b5bSMatthew G. Knepley         DM           dm;
4127900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4128900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4129f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4130900f6b5bSMatthew G. Knepley 
4131900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4132f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
4133900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4134900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4135900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4136900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4137f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
4138900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4139900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4140900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4141900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4142900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4143900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4144900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4145900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4146900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4147900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4148900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4149900f6b5bSMatthew G. Knepley       }
4150900f6b5bSMatthew G. Knepley     }
4151900f6b5bSMatthew G. Knepley   }
4152900f6b5bSMatthew G. Knepley 
4153ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4154f05ece33SBarry Smith 
4155193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4156193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4157a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4158cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4159a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4160be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4161db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4162db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4163e7069c78SShri 
41642808aa04SLisandro Dalcin     if (!ts->steps) {
41652808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41666427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41672808aa04SLisandro Dalcin     }
41686427ac75SLisandro Dalcin 
4169e1a7a14fSJed Brown     while (!ts->reason) {
41702808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41719687d888SLisandro Dalcin       if (!ts->steprollback) {
41729687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41739687d888SLisandro Dalcin       }
4174193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4175f3b1f45cSBarry Smith       if (ts->testjacobian) {
4176f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4177f3b1f45cSBarry Smith       }
4178f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4179f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4180f3b1f45cSBarry Smith       }
4181cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
41827b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4183b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
41847b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4185b1cb36f3SHong Zhang       }
418658818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41877b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4188715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
41897b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4190715f1b00SHong Zhang       }
419110b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4192e783b05fSHong 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. */
419358818c2dSLisandro Dalcin       if (ts->steprollback) {
419458818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
419558818c2dSLisandro Dalcin       }
4196e783b05fSHong Zhang       if (!ts->steprollback) {
41972808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41981eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4199aeb4809dSShri Abhyankar       }
4200193ac0bcSJed Brown     }
42012808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
42026427ac75SLisandro Dalcin 
420349354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
42040910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4205cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4206b06615a5SLisandro Dalcin       solution = u;
420763e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
42080574a7fbSJed Brown     } else {
4209ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4210cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4211b06615a5SLisandro Dalcin       solution = ts->vec_sol;
42120574a7fbSJed Brown     }
4213193ac0bcSJed Brown   }
4214d2daff3dSHong Zhang 
4215ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
4216*aee7a9fbSMatthew G. Knepley   ierr = VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
421756f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4218ef222394SBarry Smith   if (ts->adjoint_solve) {
4219ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
42202b0a91c0SBarry Smith   }
42216a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
42226a4d4014SLisandro Dalcin }
42236a4d4014SLisandro Dalcin 
42247db568b7SBarry Smith /*@C
4225228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4226228d79bcSJed Brown 
4227228d79bcSJed Brown    Collective on TS
4228228d79bcSJed Brown 
4229228d79bcSJed Brown    Input Parameters:
4230228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4231228d79bcSJed Brown .  step - step number that has just completed
4232228d79bcSJed Brown .  ptime - model time of the state
42330910c330SBarry Smith -  u - state at the current model time
4234228d79bcSJed Brown 
4235228d79bcSJed Brown    Notes:
42367db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
42377db568b7SBarry Smith    Users would almost never call this routine directly.
4238228d79bcSJed Brown 
423963e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
424063e21af5SBarry Smith 
42417db568b7SBarry Smith    Level: developer
4242228d79bcSJed Brown 
4243228d79bcSJed Brown @*/
42440910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4245d763cef2SBarry Smith {
4246d6152f81SLisandro Dalcin   DM             dm;
4247a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4248d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4249d763cef2SBarry Smith 
4250d763cef2SBarry Smith   PetscFunctionBegin;
4251b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4252b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4253d6152f81SLisandro Dalcin 
4254d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4255d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4256d6152f81SLisandro Dalcin 
42578860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4258d763cef2SBarry Smith   for (i=0; i<n; i++) {
42590910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4260d763cef2SBarry Smith   }
42618860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4262d763cef2SBarry Smith   PetscFunctionReturn(0);
4263d763cef2SBarry Smith }
4264d763cef2SBarry Smith 
4265d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4266d763cef2SBarry Smith /*@C
42677db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4268a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4269d763cef2SBarry Smith 
4270d763cef2SBarry Smith    Collective on TS
4271d763cef2SBarry Smith 
4272d763cef2SBarry Smith    Input Parameters:
4273d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4274d763cef2SBarry Smith .  label - the title to put in the title bar
42757c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4276a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4277a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4278d763cef2SBarry Smith 
4279d763cef2SBarry Smith    Output Parameter:
42800b039ecaSBarry Smith .  ctx - the context
4281d763cef2SBarry Smith 
4282d763cef2SBarry Smith    Options Database Key:
42834f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42848b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
42857db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42867db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42877db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42887db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4289b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4290d763cef2SBarry Smith 
4291d763cef2SBarry Smith    Notes:
4292a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4293d763cef2SBarry Smith 
42947db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42957db568b7SBarry Smith 
42967db568b7SBarry 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
42977db568b7SBarry 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
42987db568b7SBarry Smith    as the first argument.
42997db568b7SBarry Smith 
43007db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
43017db568b7SBarry Smith 
4302d763cef2SBarry Smith    Level: intermediate
4303d763cef2SBarry Smith 
43047db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
43057db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
43067db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
43077db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
43087db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
43097c922b88SBarry Smith 
4310d763cef2SBarry Smith @*/
4311a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4312d763cef2SBarry Smith {
43137f52daa2SLisandro Dalcin   PetscDraw      draw;
4314dfbe8321SBarry Smith   PetscErrorCode ierr;
4315d763cef2SBarry Smith 
4316d763cef2SBarry Smith   PetscFunctionBegin;
4317b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
43187f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43197f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43207f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4321287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43227f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4323a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4324d763cef2SBarry Smith   PetscFunctionReturn(0);
4325d763cef2SBarry Smith }
4326d763cef2SBarry Smith 
4327b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4328d763cef2SBarry Smith {
43290b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4330c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4331dfbe8321SBarry Smith   PetscErrorCode ierr;
4332d763cef2SBarry Smith 
4333d763cef2SBarry Smith   PetscFunctionBegin;
433463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4335b06615a5SLisandro Dalcin   if (!step) {
4336a9f9c1f6SBarry Smith     PetscDrawAxis axis;
43378b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4338a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43398b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4340a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4341a9f9c1f6SBarry Smith   }
4342c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43438b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
43440b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4345b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43460b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43476934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
43483923b477SBarry Smith   }
4349d763cef2SBarry Smith   PetscFunctionReturn(0);
4350d763cef2SBarry Smith }
4351d763cef2SBarry Smith 
4352d763cef2SBarry Smith /*@C
4353a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4354a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4355d763cef2SBarry Smith 
43560b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4357d763cef2SBarry Smith 
4358d763cef2SBarry Smith    Input Parameter:
43590b039ecaSBarry Smith .  ctx - the monitor context
4360d763cef2SBarry Smith 
4361d763cef2SBarry Smith    Level: intermediate
4362d763cef2SBarry Smith 
43634f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4364d763cef2SBarry Smith @*/
4365a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4366d763cef2SBarry Smith {
4367dfbe8321SBarry Smith   PetscErrorCode ierr;
4368d763cef2SBarry Smith 
4369d763cef2SBarry Smith   PetscFunctionBegin;
43707684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43717684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43727684fa3eSBarry Smith   }
43730b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
437431152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4375387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4376387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4377387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43780b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4379d763cef2SBarry Smith   PetscFunctionReturn(0);
4380d763cef2SBarry Smith }
4381d763cef2SBarry Smith 
43821b575b74SJoseph Pusztay /*
43831b575b74SJoseph Pusztay 
43841b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
43851b575b74SJoseph Pusztay 
43861b575b74SJoseph Pusztay */
43871b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43881b575b74SJoseph Pusztay {
43891b575b74SJoseph Pusztay   PetscDraw      draw;
43901b575b74SJoseph Pusztay   PetscErrorCode ierr;
43911b575b74SJoseph Pusztay 
43921b575b74SJoseph Pusztay   PetscFunctionBegin;
43931b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43941b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43951b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43961b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43971b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43981b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43991b575b74SJoseph Pusztay   PetscFunctionReturn(0);
44001b575b74SJoseph Pusztay 
44011b575b74SJoseph Pusztay }
44021b575b74SJoseph Pusztay 
44031b575b74SJoseph Pusztay /*
44041b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
44051b575b74SJoseph Pusztay */
44061b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
44071b575b74SJoseph Pusztay {
44081b575b74SJoseph Pusztay   PetscErrorCode ierr;
44091b575b74SJoseph Pusztay 
44101b575b74SJoseph Pusztay   PetscFunctionBegin;
44111b575b74SJoseph Pusztay 
44121b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
44131b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
44141b575b74SJoseph Pusztay 
44151b575b74SJoseph Pusztay   PetscFunctionReturn(0);
44161b575b74SJoseph Pusztay 
44171b575b74SJoseph Pusztay }
44181b575b74SJoseph Pusztay 
4419d763cef2SBarry Smith /*@
4420b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4421d763cef2SBarry Smith 
4422d763cef2SBarry Smith    Not Collective
4423d763cef2SBarry Smith 
4424d763cef2SBarry Smith    Input Parameter:
4425d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4426d763cef2SBarry Smith 
4427d763cef2SBarry Smith    Output Parameter:
442819eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4429d763cef2SBarry Smith 
4430d763cef2SBarry Smith    Level: beginner
4431d763cef2SBarry Smith 
4432b8123daeSJed Brown    Note:
4433b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
44349be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4435b8123daeSJed Brown 
44368f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4437d763cef2SBarry Smith 
4438d763cef2SBarry Smith @*/
44397087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4440d763cef2SBarry Smith {
4441d763cef2SBarry Smith   PetscFunctionBegin;
44420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4443f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4444d763cef2SBarry Smith   *t = ts->ptime;
4445d763cef2SBarry Smith   PetscFunctionReturn(0);
4446d763cef2SBarry Smith }
4447d763cef2SBarry Smith 
4448e5e524a1SHong Zhang /*@
4449e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4450e5e524a1SHong Zhang 
4451e5e524a1SHong Zhang    Not Collective
4452e5e524a1SHong Zhang 
4453e5e524a1SHong Zhang    Input Parameter:
4454e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4455e5e524a1SHong Zhang 
4456e5e524a1SHong Zhang    Output Parameter:
4457e5e524a1SHong Zhang .  t  - the previous time
4458e5e524a1SHong Zhang 
4459e5e524a1SHong Zhang    Level: beginner
4460e5e524a1SHong Zhang 
4461aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4462e5e524a1SHong Zhang 
4463e5e524a1SHong Zhang @*/
4464e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4465e5e524a1SHong Zhang {
4466e5e524a1SHong Zhang   PetscFunctionBegin;
4467e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4468e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4469e5e524a1SHong Zhang   *t = ts->ptime_prev;
4470e5e524a1SHong Zhang   PetscFunctionReturn(0);
4471e5e524a1SHong Zhang }
4472e5e524a1SHong Zhang 
44736a4d4014SLisandro Dalcin /*@
44746a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44756a4d4014SLisandro Dalcin 
44763f9fe445SBarry Smith    Logically Collective on TS
44776a4d4014SLisandro Dalcin 
44786a4d4014SLisandro Dalcin    Input Parameters:
44796a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44806a4d4014SLisandro Dalcin -  time - the time
44816a4d4014SLisandro Dalcin 
44826a4d4014SLisandro Dalcin    Level: intermediate
44836a4d4014SLisandro Dalcin 
448419eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
44856a4d4014SLisandro Dalcin 
44866a4d4014SLisandro Dalcin @*/
44877087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44886a4d4014SLisandro Dalcin {
44896a4d4014SLisandro Dalcin   PetscFunctionBegin;
44900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4491c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44926a4d4014SLisandro Dalcin   ts->ptime = t;
44936a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44946a4d4014SLisandro Dalcin }
44956a4d4014SLisandro Dalcin 
4496d763cef2SBarry Smith /*@C
4497d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4498d763cef2SBarry Smith    TS options in the database.
4499d763cef2SBarry Smith 
45003f9fe445SBarry Smith    Logically Collective on TS
4501d763cef2SBarry Smith 
4502d763cef2SBarry Smith    Input Parameter:
4503d763cef2SBarry Smith +  ts     - The TS context
4504d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4505d763cef2SBarry Smith 
4506d763cef2SBarry Smith    Notes:
4507d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4508d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4509d763cef2SBarry Smith    hyphen.
4510d763cef2SBarry Smith 
4511d763cef2SBarry Smith    Level: advanced
4512d763cef2SBarry Smith 
4513d763cef2SBarry Smith .seealso: TSSetFromOptions()
4514d763cef2SBarry Smith 
4515d763cef2SBarry Smith @*/
45167087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4517d763cef2SBarry Smith {
4518dfbe8321SBarry Smith   PetscErrorCode ierr;
4519089b2837SJed Brown   SNES           snes;
4520d763cef2SBarry Smith 
4521d763cef2SBarry Smith   PetscFunctionBegin;
45220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4523d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4524089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4525089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4526d763cef2SBarry Smith   PetscFunctionReturn(0);
4527d763cef2SBarry Smith }
4528d763cef2SBarry Smith 
4529d763cef2SBarry Smith /*@C
4530d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4531d763cef2SBarry Smith    TS options in the database.
4532d763cef2SBarry Smith 
45333f9fe445SBarry Smith    Logically Collective on TS
4534d763cef2SBarry Smith 
4535d763cef2SBarry Smith    Input Parameter:
4536d763cef2SBarry Smith +  ts     - The TS context
4537d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4538d763cef2SBarry Smith 
4539d763cef2SBarry Smith    Notes:
4540d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4541d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4542d763cef2SBarry Smith    hyphen.
4543d763cef2SBarry Smith 
4544d763cef2SBarry Smith    Level: advanced
4545d763cef2SBarry Smith 
4546d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4547d763cef2SBarry Smith 
4548d763cef2SBarry Smith @*/
45497087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4550d763cef2SBarry Smith {
4551dfbe8321SBarry Smith   PetscErrorCode ierr;
4552089b2837SJed Brown   SNES           snes;
4553d763cef2SBarry Smith 
4554d763cef2SBarry Smith   PetscFunctionBegin;
45550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4556d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4557089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4558089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4559d763cef2SBarry Smith   PetscFunctionReturn(0);
4560d763cef2SBarry Smith }
4561d763cef2SBarry Smith 
4562d763cef2SBarry Smith /*@C
4563d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4564d763cef2SBarry Smith    TS options in the database.
4565d763cef2SBarry Smith 
4566d763cef2SBarry Smith    Not Collective
4567d763cef2SBarry Smith 
4568d763cef2SBarry Smith    Input Parameter:
4569d763cef2SBarry Smith .  ts - The TS context
4570d763cef2SBarry Smith 
4571d763cef2SBarry Smith    Output Parameter:
4572d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4573d763cef2SBarry Smith 
457495452b02SPatrick Sanan    Notes:
457595452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4576d763cef2SBarry Smith    sufficient length to hold the prefix.
4577d763cef2SBarry Smith 
4578d763cef2SBarry Smith    Level: intermediate
4579d763cef2SBarry Smith 
4580d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4581d763cef2SBarry Smith @*/
45827087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4583d763cef2SBarry Smith {
4584dfbe8321SBarry Smith   PetscErrorCode ierr;
4585d763cef2SBarry Smith 
4586d763cef2SBarry Smith   PetscFunctionBegin;
45870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45884482741eSBarry Smith   PetscValidPointer(prefix,2);
4589d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4590d763cef2SBarry Smith   PetscFunctionReturn(0);
4591d763cef2SBarry Smith }
4592d763cef2SBarry Smith 
4593d763cef2SBarry Smith /*@C
4594d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4595d763cef2SBarry Smith 
4596d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4597d763cef2SBarry Smith 
4598d763cef2SBarry Smith    Input Parameter:
4599d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4600d763cef2SBarry Smith 
4601d763cef2SBarry Smith    Output Parameters:
4602e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4603e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4604e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4605e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4606d763cef2SBarry Smith 
460795452b02SPatrick Sanan    Notes:
460895452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4609d763cef2SBarry Smith 
4610d763cef2SBarry Smith    Level: intermediate
4611d763cef2SBarry Smith 
461280275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4613d763cef2SBarry Smith 
4614d763cef2SBarry Smith @*/
4615e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4616d763cef2SBarry Smith {
4617089b2837SJed Brown   PetscErrorCode ierr;
461824989b8cSPeter Brune   DM             dm;
4619089b2837SJed Brown 
4620d763cef2SBarry Smith   PetscFunctionBegin;
462123a57915SBarry Smith   if (Amat || Pmat) {
462223a57915SBarry Smith     SNES snes;
4623089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
462423a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4625e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
462623a57915SBarry Smith   }
462724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
462824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4629d763cef2SBarry Smith   PetscFunctionReturn(0);
4630d763cef2SBarry Smith }
4631d763cef2SBarry Smith 
46322eca1d9cSJed Brown /*@C
46332eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
46342eca1d9cSJed Brown 
46352eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46362eca1d9cSJed Brown 
46372eca1d9cSJed Brown    Input Parameter:
46382eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46392eca1d9cSJed Brown 
46402eca1d9cSJed Brown    Output Parameters:
4641e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4642e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46432eca1d9cSJed Brown .  f   - The function to compute the matrices
46442eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46452eca1d9cSJed Brown 
464695452b02SPatrick Sanan    Notes:
464795452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
46482eca1d9cSJed Brown 
46492eca1d9cSJed Brown    Level: advanced
46502eca1d9cSJed Brown 
465180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
46522eca1d9cSJed Brown 
46532eca1d9cSJed Brown @*/
4654e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46552eca1d9cSJed Brown {
4656089b2837SJed Brown   PetscErrorCode ierr;
465724989b8cSPeter Brune   DM             dm;
46580910c330SBarry Smith 
46592eca1d9cSJed Brown   PetscFunctionBegin;
4660c0aab802Sstefano_zampini   if (Amat || Pmat) {
4661c0aab802Sstefano_zampini     SNES snes;
4662089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4663f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4664e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4665c0aab802Sstefano_zampini   }
466624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
466724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46682eca1d9cSJed Brown   PetscFunctionReturn(0);
46692eca1d9cSJed Brown }
46702eca1d9cSJed Brown 
46711713a123SBarry Smith /*@C
467283a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46731713a123SBarry Smith    VecView() for the solution at each timestep
46741713a123SBarry Smith 
46751713a123SBarry Smith    Collective on TS
46761713a123SBarry Smith 
46771713a123SBarry Smith    Input Parameters:
46781713a123SBarry Smith +  ts - the TS context
46791713a123SBarry Smith .  step - current time-step
4680142b95e3SSatish Balay .  ptime - current time
46810298fd71SBarry Smith -  dummy - either a viewer or NULL
46821713a123SBarry Smith 
468399fdda47SBarry Smith    Options Database:
468499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
468599fdda47SBarry Smith 
468695452b02SPatrick Sanan    Notes:
468795452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
468899fdda47SBarry Smith        will look bad
468999fdda47SBarry Smith 
46901713a123SBarry Smith    Level: intermediate
46911713a123SBarry Smith 
4692a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46931713a123SBarry Smith @*/
46940910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46951713a123SBarry Smith {
4696dfbe8321SBarry Smith   PetscErrorCode   ierr;
469783a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4698473a3ab2SBarry Smith   PetscDraw        draw;
46991713a123SBarry Smith 
47001713a123SBarry Smith   PetscFunctionBegin;
47016083293cSBarry Smith   if (!step && ictx->showinitial) {
47026083293cSBarry Smith     if (!ictx->initialsolution) {
47030910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
47041713a123SBarry Smith     }
47050910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
47066083293cSBarry Smith   }
4707b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47080dcf80beSBarry Smith 
47096083293cSBarry Smith   if (ictx->showinitial) {
47106083293cSBarry Smith     PetscReal pause;
47116083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
47126083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
47136083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
47146083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
47156083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
47166083293cSBarry Smith   }
47170910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4718473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
471951fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4720473a3ab2SBarry Smith     char      time[32];
4721473a3ab2SBarry Smith 
4722473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
472385b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4724473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4725473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
472651fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4727473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4728473a3ab2SBarry Smith   }
4729473a3ab2SBarry Smith 
47306083293cSBarry Smith   if (ictx->showinitial) {
47316083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
47326083293cSBarry Smith   }
47331713a123SBarry Smith   PetscFunctionReturn(0);
47341713a123SBarry Smith }
47351713a123SBarry Smith 
47369110b2e7SHong Zhang /*@C
47372d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
47382d5ee99bSBarry Smith 
47392d5ee99bSBarry Smith    Collective on TS
47402d5ee99bSBarry Smith 
47412d5ee99bSBarry Smith    Input Parameters:
47422d5ee99bSBarry Smith +  ts - the TS context
47432d5ee99bSBarry Smith .  step - current time-step
47442d5ee99bSBarry Smith .  ptime - current time
47452d5ee99bSBarry Smith -  dummy - either a viewer or NULL
47462d5ee99bSBarry Smith 
47472d5ee99bSBarry Smith    Level: intermediate
47482d5ee99bSBarry Smith 
47492d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47502d5ee99bSBarry Smith @*/
47512d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47522d5ee99bSBarry Smith {
47532d5ee99bSBarry Smith   PetscErrorCode    ierr;
47542d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47552d5ee99bSBarry Smith   PetscDraw         draw;
47566934998bSLisandro Dalcin   PetscDrawAxis     axis;
47572d5ee99bSBarry Smith   PetscInt          n;
47582d5ee99bSBarry Smith   PetscMPIInt       size;
47596934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47602d5ee99bSBarry Smith   char              time[32];
47612d5ee99bSBarry Smith   const PetscScalar *U;
47622d5ee99bSBarry Smith 
47632d5ee99bSBarry Smith   PetscFunctionBegin;
4764ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRMPI(ierr);
47656934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47662d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47676934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47682d5ee99bSBarry Smith 
47692d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47706934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47716934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47726934998bSLisandro Dalcin   if (!step) {
47736934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47746934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47756934998bSLisandro Dalcin   }
47762d5ee99bSBarry Smith 
47772d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47786934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47796934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4780a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47816934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47822d5ee99bSBarry Smith 
47836934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47846934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47852d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47864b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
478785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47882d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
478951fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47902d5ee99bSBarry Smith   }
47916934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47922d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
479356d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47946934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47952d5ee99bSBarry Smith   PetscFunctionReturn(0);
47962d5ee99bSBarry Smith }
47972d5ee99bSBarry Smith 
47986083293cSBarry Smith /*@C
479983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
48006083293cSBarry Smith 
48016083293cSBarry Smith    Collective on TS
48026083293cSBarry Smith 
48036083293cSBarry Smith    Input Parameters:
48046083293cSBarry Smith .    ctx - the monitor context
48056083293cSBarry Smith 
48066083293cSBarry Smith    Level: intermediate
48076083293cSBarry Smith 
480883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
48096083293cSBarry Smith @*/
481083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
48116083293cSBarry Smith {
48126083293cSBarry Smith   PetscErrorCode ierr;
48136083293cSBarry Smith 
48146083293cSBarry Smith   PetscFunctionBegin;
481583a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
481683a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
481783a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
48186083293cSBarry Smith   PetscFunctionReturn(0);
48196083293cSBarry Smith }
48206083293cSBarry Smith 
48216083293cSBarry Smith /*@C
482283a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
48236083293cSBarry Smith 
48246083293cSBarry Smith    Collective on TS
48256083293cSBarry Smith 
48266083293cSBarry Smith    Input Parameter:
48276083293cSBarry Smith .    ts - time-step context
48286083293cSBarry Smith 
48296083293cSBarry Smith    Output Patameter:
48306083293cSBarry Smith .    ctx - the monitor context
48316083293cSBarry Smith 
483299fdda47SBarry Smith    Options Database:
483399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
483499fdda47SBarry Smith 
48356083293cSBarry Smith    Level: intermediate
48366083293cSBarry Smith 
483783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
48386083293cSBarry Smith @*/
483983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
48406083293cSBarry Smith {
48416083293cSBarry Smith   PetscErrorCode   ierr;
48426083293cSBarry Smith 
48436083293cSBarry Smith   PetscFunctionBegin;
4844b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
484583a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4846e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4847bbd56ea5SKarl Rupp 
484883a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4849473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4850c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4851473a3ab2SBarry Smith 
4852473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4853c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
48546083293cSBarry Smith   PetscFunctionReturn(0);
48556083293cSBarry Smith }
48566083293cSBarry Smith 
48573a471f94SBarry Smith /*@C
48580ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
48590ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
48600ed3bfb6SBarry Smith 
48610ed3bfb6SBarry Smith    Collective on TS
48620ed3bfb6SBarry Smith 
48630ed3bfb6SBarry Smith    Input Parameters:
48640ed3bfb6SBarry Smith +  ts - the TS context
48650ed3bfb6SBarry Smith .  step - current time-step
48660ed3bfb6SBarry Smith .  ptime - current time
48670ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
48680ed3bfb6SBarry Smith 
48690ed3bfb6SBarry Smith    Options Database:
48700ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48710ed3bfb6SBarry Smith 
48720ed3bfb6SBarry Smith    Level: intermediate
48730ed3bfb6SBarry Smith 
48740ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48750ed3bfb6SBarry Smith @*/
48760ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48770ed3bfb6SBarry Smith {
48780ed3bfb6SBarry Smith   PetscErrorCode   ierr;
48790ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
48800ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
48810ed3bfb6SBarry Smith   Vec              work;
48820ed3bfb6SBarry Smith 
48830ed3bfb6SBarry Smith   PetscFunctionBegin;
48840ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48850ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48860ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48870ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48880ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48890ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48900ed3bfb6SBarry Smith }
48910ed3bfb6SBarry Smith 
48920ed3bfb6SBarry Smith /*@C
489383a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48943a471f94SBarry Smith    VecView() for the error at each timestep
48953a471f94SBarry Smith 
48963a471f94SBarry Smith    Collective on TS
48973a471f94SBarry Smith 
48983a471f94SBarry Smith    Input Parameters:
48993a471f94SBarry Smith +  ts - the TS context
49003a471f94SBarry Smith .  step - current time-step
49013a471f94SBarry Smith .  ptime - current time
49020298fd71SBarry Smith -  dummy - either a viewer or NULL
49033a471f94SBarry Smith 
49040ed3bfb6SBarry Smith    Options Database:
49050ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
49060ed3bfb6SBarry Smith 
49073a471f94SBarry Smith    Level: intermediate
49083a471f94SBarry Smith 
49090ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
49103a471f94SBarry Smith @*/
49110910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49123a471f94SBarry Smith {
49133a471f94SBarry Smith   PetscErrorCode   ierr;
491483a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
491583a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
49163a471f94SBarry Smith   Vec              work;
49173a471f94SBarry Smith 
49183a471f94SBarry Smith   PetscFunctionBegin;
4919b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49200910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
49213a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
49220910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
49233a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
49243a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
49253a471f94SBarry Smith   PetscFunctionReturn(0);
49263a471f94SBarry Smith }
49273a471f94SBarry Smith 
4928af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49296c699258SBarry Smith /*@
49302a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49316c699258SBarry Smith 
4932d083f849SBarry Smith    Logically Collective on ts
49336c699258SBarry Smith 
49346c699258SBarry Smith    Input Parameters:
49352a808120SBarry Smith +  ts - the ODE integrator object
49362a808120SBarry Smith -  dm - the dm, cannot be NULL
49376c699258SBarry Smith 
4938e03a659cSJed Brown    Notes:
4939e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4940e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4941e03a659cSJed Brown    different problems using the same function space.
4942e03a659cSJed Brown 
49436c699258SBarry Smith    Level: intermediate
49446c699258SBarry Smith 
49456c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49466c699258SBarry Smith @*/
49477087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49486c699258SBarry Smith {
49496c699258SBarry Smith   PetscErrorCode ierr;
4950089b2837SJed Brown   SNES           snes;
4951942e3340SBarry Smith   DMTS           tsdm;
49526c699258SBarry Smith 
49536c699258SBarry Smith   PetscFunctionBegin;
49540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49552a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
495670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4957942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49582a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4959942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4960942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
496124989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
496224989b8cSPeter Brune         tsdm->originaldm = dm;
496324989b8cSPeter Brune       }
496424989b8cSPeter Brune     }
49656bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
496624989b8cSPeter Brune   }
49676c699258SBarry Smith   ts->dm = dm;
4968bbd56ea5SKarl Rupp 
4969089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4970089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49716c699258SBarry Smith   PetscFunctionReturn(0);
49726c699258SBarry Smith }
49736c699258SBarry Smith 
49746c699258SBarry Smith /*@
49756c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49766c699258SBarry Smith 
49773f9fe445SBarry Smith    Not Collective
49786c699258SBarry Smith 
49796c699258SBarry Smith    Input Parameter:
49806c699258SBarry Smith . ts - the preconditioner context
49816c699258SBarry Smith 
49826c699258SBarry Smith    Output Parameter:
49836c699258SBarry Smith .  dm - the dm
49846c699258SBarry Smith 
49856c699258SBarry Smith    Level: intermediate
49866c699258SBarry Smith 
49876c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49886c699258SBarry Smith @*/
49897087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49906c699258SBarry Smith {
4991496e6a7aSJed Brown   PetscErrorCode ierr;
4992496e6a7aSJed Brown 
49936c699258SBarry Smith   PetscFunctionBegin;
49940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4995496e6a7aSJed Brown   if (!ts->dm) {
4996ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4997496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4998496e6a7aSJed Brown   }
49996c699258SBarry Smith   *dm = ts->dm;
50006c699258SBarry Smith   PetscFunctionReturn(0);
50016c699258SBarry Smith }
50021713a123SBarry Smith 
50030f5c6efeSJed Brown /*@
50040f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
50050f5c6efeSJed Brown 
50063f9fe445SBarry Smith    Logically Collective on SNES
50070f5c6efeSJed Brown 
50080f5c6efeSJed Brown    Input Parameter:
5009d42a1c89SJed Brown + snes - nonlinear solver
50100910c330SBarry Smith . U - the current state at which to evaluate the residual
5011d42a1c89SJed Brown - ctx - user context, must be a TS
50120f5c6efeSJed Brown 
50130f5c6efeSJed Brown    Output Parameter:
50140f5c6efeSJed Brown . F - the nonlinear residual
50150f5c6efeSJed Brown 
50160f5c6efeSJed Brown    Notes:
50170f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50180f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
50190f5c6efeSJed Brown 
50200f5c6efeSJed Brown    Level: advanced
50210f5c6efeSJed Brown 
50220f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
50230f5c6efeSJed Brown @*/
50240910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
50250f5c6efeSJed Brown {
50260f5c6efeSJed Brown   TS             ts = (TS)ctx;
50270f5c6efeSJed Brown   PetscErrorCode ierr;
50280f5c6efeSJed Brown 
50290f5c6efeSJed Brown   PetscFunctionBegin;
50300f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50310910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50320f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50330f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50340910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50350f5c6efeSJed Brown   PetscFunctionReturn(0);
50360f5c6efeSJed Brown }
50370f5c6efeSJed Brown 
50380f5c6efeSJed Brown /*@
50390f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50400f5c6efeSJed Brown 
50410f5c6efeSJed Brown    Collective on SNES
50420f5c6efeSJed Brown 
50430f5c6efeSJed Brown    Input Parameter:
50440f5c6efeSJed Brown + snes - nonlinear solver
50450910c330SBarry Smith . U - the current state at which to evaluate the residual
50460f5c6efeSJed Brown - ctx - user context, must be a TS
50470f5c6efeSJed Brown 
50480f5c6efeSJed Brown    Output Parameter:
50490f5c6efeSJed Brown + A - the Jacobian
50500f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50510f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50520f5c6efeSJed Brown 
50530f5c6efeSJed Brown    Notes:
50540f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50550f5c6efeSJed Brown 
50560f5c6efeSJed Brown    Level: developer
50570f5c6efeSJed Brown 
50580f5c6efeSJed Brown .seealso: SNESSetJacobian()
50590f5c6efeSJed Brown @*/
5060d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50610f5c6efeSJed Brown {
50620f5c6efeSJed Brown   TS             ts = (TS)ctx;
50630f5c6efeSJed Brown   PetscErrorCode ierr;
50640f5c6efeSJed Brown 
50650f5c6efeSJed Brown   PetscFunctionBegin;
50660f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50670910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50680f5c6efeSJed Brown   PetscValidPointer(A,3);
506994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50700f5c6efeSJed Brown   PetscValidPointer(B,4);
507194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50720f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5073d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50740f5c6efeSJed Brown   PetscFunctionReturn(0);
50750f5c6efeSJed Brown }
5076325fc9f4SBarry Smith 
50770e4ef248SJed Brown /*@C
50789ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50790e4ef248SJed Brown 
50800e4ef248SJed Brown    Collective on TS
50810e4ef248SJed Brown 
50820e4ef248SJed Brown    Input Arguments:
50830e4ef248SJed Brown +  ts - time stepping context
50840e4ef248SJed Brown .  t - time at which to evaluate
50850910c330SBarry Smith .  U - state at which to evaluate
50860e4ef248SJed Brown -  ctx - context
50870e4ef248SJed Brown 
50880e4ef248SJed Brown    Output Arguments:
50890e4ef248SJed Brown .  F - right hand side
50900e4ef248SJed Brown 
50910e4ef248SJed Brown    Level: intermediate
50920e4ef248SJed Brown 
50930e4ef248SJed Brown    Notes:
50940e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50950e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50960e4ef248SJed Brown 
50970e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50980e4ef248SJed Brown @*/
50990910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
51000e4ef248SJed Brown {
51010e4ef248SJed Brown   PetscErrorCode ierr;
51020e4ef248SJed Brown   Mat            Arhs,Brhs;
51030e4ef248SJed Brown 
51040e4ef248SJed Brown   PetscFunctionBegin;
51050e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
51062663174eSHong Zhang   /* undo the damage caused by shifting */
5107cfa8a9a2SHong Zhang   ierr = TSRecoverRHSJacobian(ts,Arhs,Brhs);CHKERRQ(ierr);
5108d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
51090910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
51100e4ef248SJed Brown   PetscFunctionReturn(0);
51110e4ef248SJed Brown }
51120e4ef248SJed Brown 
51130e4ef248SJed Brown /*@C
51140e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
51150e4ef248SJed Brown 
51160e4ef248SJed Brown    Collective on TS
51170e4ef248SJed Brown 
51180e4ef248SJed Brown    Input Arguments:
51190e4ef248SJed Brown +  ts - time stepping context
51200e4ef248SJed Brown .  t - time at which to evaluate
51210910c330SBarry Smith .  U - state at which to evaluate
51220e4ef248SJed Brown -  ctx - context
51230e4ef248SJed Brown 
51240e4ef248SJed Brown    Output Arguments:
51250e4ef248SJed Brown +  A - pointer to operator
51260e4ef248SJed Brown .  B - pointer to preconditioning matrix
51270e4ef248SJed Brown -  flg - matrix structure flag
51280e4ef248SJed Brown 
51290e4ef248SJed Brown    Level: intermediate
51300e4ef248SJed Brown 
51310e4ef248SJed Brown    Notes:
51320e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51330e4ef248SJed Brown 
51340e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51350e4ef248SJed Brown @*/
5136d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51370e4ef248SJed Brown {
51380e4ef248SJed Brown   PetscFunctionBegin;
51390e4ef248SJed Brown   PetscFunctionReturn(0);
51400e4ef248SJed Brown }
51410e4ef248SJed Brown 
51420026cea9SSean Farley /*@C
51430026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51440026cea9SSean Farley 
51450026cea9SSean Farley    Collective on TS
51460026cea9SSean Farley 
51470026cea9SSean Farley    Input Arguments:
51480026cea9SSean Farley +  ts - time stepping context
51490026cea9SSean Farley .  t - time at which to evaluate
51500910c330SBarry Smith .  U - state at which to evaluate
51510910c330SBarry Smith .  Udot - time derivative of state vector
51520026cea9SSean Farley -  ctx - context
51530026cea9SSean Farley 
51540026cea9SSean Farley    Output Arguments:
51550026cea9SSean Farley .  F - left hand side
51560026cea9SSean Farley 
51570026cea9SSean Farley    Level: intermediate
51580026cea9SSean Farley 
51590026cea9SSean Farley    Notes:
51600910c330SBarry 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
51610026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51620026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51630026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51640026cea9SSean Farley 
51659ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51669ae8fd06SBarry Smith 
51679ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51680026cea9SSean Farley @*/
51690910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51700026cea9SSean Farley {
51710026cea9SSean Farley   PetscErrorCode ierr;
51720026cea9SSean Farley   Mat            A,B;
51730026cea9SSean Farley 
51740026cea9SSean Farley   PetscFunctionBegin;
51750298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5176d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51770910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51780026cea9SSean Farley   PetscFunctionReturn(0);
51790026cea9SSean Farley }
51800026cea9SSean Farley 
51810026cea9SSean Farley /*@C
5182b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51830026cea9SSean Farley 
51840026cea9SSean Farley    Collective on TS
51850026cea9SSean Farley 
51860026cea9SSean Farley    Input Arguments:
51870026cea9SSean Farley +  ts - time stepping context
51880026cea9SSean Farley .  t - time at which to evaluate
51890910c330SBarry Smith .  U - state at which to evaluate
51900910c330SBarry Smith .  Udot - time derivative of state vector
51910026cea9SSean Farley .  shift - shift to apply
51920026cea9SSean Farley -  ctx - context
51930026cea9SSean Farley 
51940026cea9SSean Farley    Output Arguments:
51950026cea9SSean Farley +  A - pointer to operator
51960026cea9SSean Farley .  B - pointer to preconditioning matrix
51970026cea9SSean Farley -  flg - matrix structure flag
51980026cea9SSean Farley 
5199b41af12eSJed Brown    Level: advanced
52000026cea9SSean Farley 
52010026cea9SSean Farley    Notes:
52020026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
52030026cea9SSean Farley 
5204b41af12eSJed Brown    It is only appropriate for problems of the form
5205b41af12eSJed Brown 
5206b41af12eSJed Brown $     M Udot = F(U,t)
5207b41af12eSJed Brown 
5208b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5209b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5210b41af12eSJed Brown   an implicit operator of the form
5211b41af12eSJed Brown 
5212b41af12eSJed Brown $    shift*M + J
5213b41af12eSJed Brown 
5214b41af12eSJed 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
5215b41af12eSJed Brown   a copy of M or reassemble it when requested.
5216b41af12eSJed Brown 
52170026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
52180026cea9SSean Farley @*/
5219d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
52200026cea9SSean Farley {
5221b41af12eSJed Brown   PetscErrorCode ierr;
5222b41af12eSJed Brown 
52230026cea9SSean Farley   PetscFunctionBegin;
522494ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5225b41af12eSJed Brown   ts->ijacobian.shift = shift;
52260026cea9SSean Farley   PetscFunctionReturn(0);
52270026cea9SSean Farley }
5228b41af12eSJed Brown 
5229e817cc15SEmil Constantinescu /*@
5230e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5231e817cc15SEmil Constantinescu 
5232e817cc15SEmil Constantinescu    Not Collective
5233e817cc15SEmil Constantinescu 
5234e817cc15SEmil Constantinescu    Input Parameter:
5235e817cc15SEmil Constantinescu .  ts - the TS context
5236e817cc15SEmil Constantinescu 
5237e817cc15SEmil Constantinescu    Output Parameter:
52384e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5239e817cc15SEmil Constantinescu 
5240e817cc15SEmil Constantinescu    Level: beginner
5241e817cc15SEmil Constantinescu 
5242e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5243e817cc15SEmil Constantinescu @*/
5244e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5245e817cc15SEmil Constantinescu {
5246e817cc15SEmil Constantinescu   PetscFunctionBegin;
5247e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5248e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5249e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5250e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5251e817cc15SEmil Constantinescu }
5252e817cc15SEmil Constantinescu 
5253e817cc15SEmil Constantinescu /*@
5254e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5255e817cc15SEmil Constantinescu 
5256e817cc15SEmil Constantinescu    Not Collective
5257e817cc15SEmil Constantinescu 
5258e817cc15SEmil Constantinescu    Input Parameter:
5259e817cc15SEmil Constantinescu +  ts - the TS context
52601297b224SEmil Constantinescu -  equation_type - see TSEquationType
5261e817cc15SEmil Constantinescu 
5262e817cc15SEmil Constantinescu    Level: advanced
5263e817cc15SEmil Constantinescu 
5264e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5265e817cc15SEmil Constantinescu @*/
5266e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5267e817cc15SEmil Constantinescu {
5268e817cc15SEmil Constantinescu   PetscFunctionBegin;
5269e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5270e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5271e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5272e817cc15SEmil Constantinescu }
52730026cea9SSean Farley 
52744af1b03aSJed Brown /*@
52754af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52764af1b03aSJed Brown 
52774af1b03aSJed Brown    Not Collective
52784af1b03aSJed Brown 
52794af1b03aSJed Brown    Input Parameter:
52804af1b03aSJed Brown .  ts - the TS context
52814af1b03aSJed Brown 
52824af1b03aSJed Brown    Output Parameter:
52834af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52844af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52854af1b03aSJed Brown 
5286487e0bb9SJed Brown    Level: beginner
52874af1b03aSJed Brown 
5288cd652676SJed Brown    Notes:
5289cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52904af1b03aSJed Brown 
52914af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52924af1b03aSJed Brown @*/
52934af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52944af1b03aSJed Brown {
52954af1b03aSJed Brown   PetscFunctionBegin;
52964af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52974af1b03aSJed Brown   PetscValidPointer(reason,2);
52984af1b03aSJed Brown   *reason = ts->reason;
52994af1b03aSJed Brown   PetscFunctionReturn(0);
53004af1b03aSJed Brown }
53014af1b03aSJed Brown 
5302d6ad946cSShri Abhyankar /*@
5303d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5304d6ad946cSShri Abhyankar 
53056b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5306d6ad946cSShri Abhyankar 
53076b221cbeSPatrick Sanan    Input Parameters:
5308d6ad946cSShri Abhyankar +  ts - the TS context
53096b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5310d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5311d6ad946cSShri Abhyankar 
5312f5abba47SShri Abhyankar    Level: advanced
5313d6ad946cSShri Abhyankar 
5314d6ad946cSShri Abhyankar    Notes:
53156b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5316d6ad946cSShri Abhyankar 
5317d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5318d6ad946cSShri Abhyankar @*/
5319d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5320d6ad946cSShri Abhyankar {
5321d6ad946cSShri Abhyankar   PetscFunctionBegin;
5322d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5323d6ad946cSShri Abhyankar   ts->reason = reason;
5324d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5325d6ad946cSShri Abhyankar }
5326d6ad946cSShri Abhyankar 
5327cc708dedSBarry Smith /*@
5328cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5329cc708dedSBarry Smith 
5330cc708dedSBarry Smith    Not Collective
5331cc708dedSBarry Smith 
5332cc708dedSBarry Smith    Input Parameter:
5333cc708dedSBarry Smith .  ts - the TS context
5334cc708dedSBarry Smith 
5335cc708dedSBarry Smith    Output Parameter:
533619eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5337cc708dedSBarry Smith 
5338487e0bb9SJed Brown    Level: beginner
5339cc708dedSBarry Smith 
5340cc708dedSBarry Smith    Notes:
5341cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5342cc708dedSBarry Smith 
5343cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5344cc708dedSBarry Smith @*/
5345cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5346cc708dedSBarry Smith {
5347cc708dedSBarry Smith   PetscFunctionBegin;
5348cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5349cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5350cc708dedSBarry Smith   *ftime = ts->solvetime;
5351cc708dedSBarry Smith   PetscFunctionReturn(0);
5352cc708dedSBarry Smith }
5353cc708dedSBarry Smith 
53542c18e0fdSBarry Smith /*@
53555ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53569f67acb7SJed Brown    used by the time integrator.
53579f67acb7SJed Brown 
53589f67acb7SJed Brown    Not Collective
53599f67acb7SJed Brown 
53609f67acb7SJed Brown    Input Parameter:
53619f67acb7SJed Brown .  ts - TS context
53629f67acb7SJed Brown 
53639f67acb7SJed Brown    Output Parameter:
53649f67acb7SJed Brown .  nits - number of nonlinear iterations
53659f67acb7SJed Brown 
53669f67acb7SJed Brown    Notes:
53679f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53689f67acb7SJed Brown 
53699f67acb7SJed Brown    Level: intermediate
53709f67acb7SJed Brown 
53715ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53729f67acb7SJed Brown @*/
53735ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53749f67acb7SJed Brown {
53759f67acb7SJed Brown   PetscFunctionBegin;
53769f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53779f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53785ef26d82SJed Brown   *nits = ts->snes_its;
53799f67acb7SJed Brown   PetscFunctionReturn(0);
53809f67acb7SJed Brown }
53819f67acb7SJed Brown 
53829f67acb7SJed Brown /*@
53835ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53849f67acb7SJed Brown    used by the time integrator.
53859f67acb7SJed Brown 
53869f67acb7SJed Brown    Not Collective
53879f67acb7SJed Brown 
53889f67acb7SJed Brown    Input Parameter:
53899f67acb7SJed Brown .  ts - TS context
53909f67acb7SJed Brown 
53919f67acb7SJed Brown    Output Parameter:
53929f67acb7SJed Brown .  lits - number of linear iterations
53939f67acb7SJed Brown 
53949f67acb7SJed Brown    Notes:
53959f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53969f67acb7SJed Brown 
53979f67acb7SJed Brown    Level: intermediate
53989f67acb7SJed Brown 
53995ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
54009f67acb7SJed Brown @*/
54015ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
54029f67acb7SJed Brown {
54039f67acb7SJed Brown   PetscFunctionBegin;
54049f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54059f67acb7SJed Brown   PetscValidIntPointer(lits,2);
54065ef26d82SJed Brown   *lits = ts->ksp_its;
54079f67acb7SJed Brown   PetscFunctionReturn(0);
54089f67acb7SJed Brown }
54099f67acb7SJed Brown 
5410cef5090cSJed Brown /*@
5411cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5412cef5090cSJed Brown 
5413cef5090cSJed Brown    Not Collective
5414cef5090cSJed Brown 
5415cef5090cSJed Brown    Input Parameter:
5416cef5090cSJed Brown .  ts - TS context
5417cef5090cSJed Brown 
5418cef5090cSJed Brown    Output Parameter:
5419cef5090cSJed Brown .  rejects - number of steps rejected
5420cef5090cSJed Brown 
5421cef5090cSJed Brown    Notes:
5422cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5423cef5090cSJed Brown 
5424cef5090cSJed Brown    Level: intermediate
5425cef5090cSJed Brown 
54265ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5427cef5090cSJed Brown @*/
5428cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5429cef5090cSJed Brown {
5430cef5090cSJed Brown   PetscFunctionBegin;
5431cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5432cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5433cef5090cSJed Brown   *rejects = ts->reject;
5434cef5090cSJed Brown   PetscFunctionReturn(0);
5435cef5090cSJed Brown }
5436cef5090cSJed Brown 
5437cef5090cSJed Brown /*@
5438cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5439cef5090cSJed Brown 
5440cef5090cSJed Brown    Not Collective
5441cef5090cSJed Brown 
5442cef5090cSJed Brown    Input Parameter:
5443cef5090cSJed Brown .  ts - TS context
5444cef5090cSJed Brown 
5445cef5090cSJed Brown    Output Parameter:
5446cef5090cSJed Brown .  fails - number of failed nonlinear solves
5447cef5090cSJed Brown 
5448cef5090cSJed Brown    Notes:
5449cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5450cef5090cSJed Brown 
5451cef5090cSJed Brown    Level: intermediate
5452cef5090cSJed Brown 
54535ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5454cef5090cSJed Brown @*/
5455cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5456cef5090cSJed Brown {
5457cef5090cSJed Brown   PetscFunctionBegin;
5458cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5459cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5460cef5090cSJed Brown   *fails = ts->num_snes_failures;
5461cef5090cSJed Brown   PetscFunctionReturn(0);
5462cef5090cSJed Brown }
5463cef5090cSJed Brown 
5464cef5090cSJed Brown /*@
5465cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5466cef5090cSJed Brown 
5467cef5090cSJed Brown    Not Collective
5468cef5090cSJed Brown 
5469cef5090cSJed Brown    Input Parameter:
5470cef5090cSJed Brown +  ts - TS context
5471cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5472cef5090cSJed Brown 
5473cef5090cSJed Brown    Notes:
5474cef5090cSJed Brown    The counter is reset to zero for each step
5475cef5090cSJed Brown 
5476cef5090cSJed Brown    Options Database Key:
5477cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5478cef5090cSJed Brown 
5479cef5090cSJed Brown    Level: intermediate
5480cef5090cSJed Brown 
54815ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5482cef5090cSJed Brown @*/
5483cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5484cef5090cSJed Brown {
5485cef5090cSJed Brown   PetscFunctionBegin;
5486cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5487cef5090cSJed Brown   ts->max_reject = rejects;
5488cef5090cSJed Brown   PetscFunctionReturn(0);
5489cef5090cSJed Brown }
5490cef5090cSJed Brown 
5491cef5090cSJed Brown /*@
5492cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5493cef5090cSJed Brown 
5494cef5090cSJed Brown    Not Collective
5495cef5090cSJed Brown 
5496cef5090cSJed Brown    Input Parameter:
5497cef5090cSJed Brown +  ts - TS context
5498cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5499cef5090cSJed Brown 
5500cef5090cSJed Brown    Notes:
5501cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5502cef5090cSJed Brown 
5503cef5090cSJed Brown    Options Database Key:
5504cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5505cef5090cSJed Brown 
5506cef5090cSJed Brown    Level: intermediate
5507cef5090cSJed Brown 
55085ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5509cef5090cSJed Brown @*/
5510cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5511cef5090cSJed Brown {
5512cef5090cSJed Brown   PetscFunctionBegin;
5513cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5514cef5090cSJed Brown   ts->max_snes_failures = fails;
5515cef5090cSJed Brown   PetscFunctionReturn(0);
5516cef5090cSJed Brown }
5517cef5090cSJed Brown 
5518cef5090cSJed Brown /*@
5519cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5520cef5090cSJed Brown 
5521cef5090cSJed Brown    Not Collective
5522cef5090cSJed Brown 
5523cef5090cSJed Brown    Input Parameter:
5524cef5090cSJed Brown +  ts - TS context
5525cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5526cef5090cSJed Brown 
5527cef5090cSJed Brown    Options Database Key:
5528cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5529cef5090cSJed Brown 
5530cef5090cSJed Brown    Level: intermediate
5531cef5090cSJed Brown 
55325ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5533cef5090cSJed Brown @*/
5534cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5535cef5090cSJed Brown {
5536cef5090cSJed Brown   PetscFunctionBegin;
5537cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5538cef5090cSJed Brown   ts->errorifstepfailed = err;
5539cef5090cSJed Brown   PetscFunctionReturn(0);
5540cef5090cSJed Brown }
5541cef5090cSJed Brown 
5542fb1732b5SBarry Smith /*@C
5543fde5950dSBarry 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
5544fb1732b5SBarry Smith 
5545fb1732b5SBarry Smith    Collective on TS
5546fb1732b5SBarry Smith 
5547fb1732b5SBarry Smith    Input Parameters:
5548fb1732b5SBarry Smith +  ts - the TS context
5549fb1732b5SBarry Smith .  step - current time-step
5550fb1732b5SBarry Smith .  ptime - current time
55510910c330SBarry Smith .  u - current state
5552721cd6eeSBarry Smith -  vf - viewer and its format
5553fb1732b5SBarry Smith 
5554fb1732b5SBarry Smith    Level: intermediate
5555fb1732b5SBarry Smith 
5556fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5557fb1732b5SBarry Smith @*/
5558721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5559fb1732b5SBarry Smith {
5560fb1732b5SBarry Smith   PetscErrorCode ierr;
5561fb1732b5SBarry Smith 
5562fb1732b5SBarry Smith   PetscFunctionBegin;
5563721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5564721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5565721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5566ed81e22dSJed Brown   PetscFunctionReturn(0);
5567ed81e22dSJed Brown }
5568ed81e22dSJed Brown 
5569ed81e22dSJed Brown /*@C
5570ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5571ed81e22dSJed Brown 
5572ed81e22dSJed Brown    Collective on TS
5573ed81e22dSJed Brown 
5574ed81e22dSJed Brown    Input Parameters:
5575ed81e22dSJed Brown +  ts - the TS context
5576ed81e22dSJed Brown .  step - current time-step
5577ed81e22dSJed Brown .  ptime - current time
55780910c330SBarry Smith .  u - current state
5579ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5580ed81e22dSJed Brown 
5581ed81e22dSJed Brown    Level: intermediate
5582ed81e22dSJed Brown 
5583ed81e22dSJed Brown    Notes:
5584ed81e22dSJed 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.
5585ed81e22dSJed Brown    These are named according to the file name template.
5586ed81e22dSJed Brown 
5587ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5588ed81e22dSJed Brown 
5589ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5590ed81e22dSJed Brown @*/
55910910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5592ed81e22dSJed Brown {
5593ed81e22dSJed Brown   PetscErrorCode ierr;
5594ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5595ed81e22dSJed Brown   PetscViewer    viewer;
5596ed81e22dSJed Brown 
5597ed81e22dSJed Brown   PetscFunctionBegin;
559863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55998caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5600ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
56010910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5602ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5603ed81e22dSJed Brown   PetscFunctionReturn(0);
5604ed81e22dSJed Brown }
5605ed81e22dSJed Brown 
5606ed81e22dSJed Brown /*@C
5607ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5608ed81e22dSJed Brown 
5609ed81e22dSJed Brown    Collective on TS
5610ed81e22dSJed Brown 
5611ed81e22dSJed Brown    Input Parameters:
5612ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5613ed81e22dSJed Brown 
5614ed81e22dSJed Brown    Level: intermediate
5615ed81e22dSJed Brown 
5616ed81e22dSJed Brown    Note:
5617ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5618ed81e22dSJed Brown 
5619ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5620ed81e22dSJed Brown @*/
5621ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5622ed81e22dSJed Brown {
5623ed81e22dSJed Brown   PetscErrorCode ierr;
5624ed81e22dSJed Brown 
5625ed81e22dSJed Brown   PetscFunctionBegin;
5626ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5627fb1732b5SBarry Smith   PetscFunctionReturn(0);
5628fb1732b5SBarry Smith }
5629fb1732b5SBarry Smith 
563084df9cb4SJed Brown /*@
5631552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
563284df9cb4SJed Brown 
5633ed81e22dSJed Brown    Collective on TS if controller has not been created yet
563484df9cb4SJed Brown 
563584df9cb4SJed Brown    Input Arguments:
5636ed81e22dSJed Brown .  ts - time stepping context
563784df9cb4SJed Brown 
563884df9cb4SJed Brown    Output Arguments:
5639ed81e22dSJed Brown .  adapt - adaptive controller
564084df9cb4SJed Brown 
564184df9cb4SJed Brown    Level: intermediate
564284df9cb4SJed Brown 
5643ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
564484df9cb4SJed Brown @*/
5645552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
564684df9cb4SJed Brown {
564784df9cb4SJed Brown   PetscErrorCode ierr;
564884df9cb4SJed Brown 
564984df9cb4SJed Brown   PetscFunctionBegin;
565084df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5651bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
565284df9cb4SJed Brown   if (!ts->adapt) {
5653ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56543bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56551c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
565684df9cb4SJed Brown   }
5657bec58848SLisandro Dalcin   *adapt = ts->adapt;
565884df9cb4SJed Brown   PetscFunctionReturn(0);
565984df9cb4SJed Brown }
5660d6ebe24aSShri Abhyankar 
56611c3436cfSJed Brown /*@
56621c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
56631c3436cfSJed Brown 
56641c3436cfSJed Brown    Logically Collective
56651c3436cfSJed Brown 
56661c3436cfSJed Brown    Input Arguments:
56671c3436cfSJed Brown +  ts - time integration context
56681c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56690298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56701c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56710298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
56721c3436cfSJed Brown 
5673a3cdaa26SBarry Smith    Options Database keys:
5674a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5675a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5676a3cdaa26SBarry Smith 
56773ff766beSShri Abhyankar    Notes:
56783ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
56793ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
56803ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
56813ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
56823ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
56833ff766beSShri Abhyankar    differential variables.
56843ff766beSShri Abhyankar 
56851c3436cfSJed Brown    Level: beginner
56861c3436cfSJed Brown 
56875c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
56881c3436cfSJed Brown @*/
56891c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56901c3436cfSJed Brown {
56911c3436cfSJed Brown   PetscErrorCode ierr;
56921c3436cfSJed Brown 
56931c3436cfSJed Brown   PetscFunctionBegin;
5694c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56951c3436cfSJed Brown   if (vatol) {
56961c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56971c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56981c3436cfSJed Brown     ts->vatol = vatol;
56991c3436cfSJed Brown   }
5700c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
57011c3436cfSJed Brown   if (vrtol) {
57021c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
57031c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
57041c3436cfSJed Brown     ts->vrtol = vrtol;
57051c3436cfSJed Brown   }
57061c3436cfSJed Brown   PetscFunctionReturn(0);
57071c3436cfSJed Brown }
57081c3436cfSJed Brown 
5709c5033834SJed Brown /*@
5710c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5711c5033834SJed Brown 
5712c5033834SJed Brown    Logically Collective
5713c5033834SJed Brown 
5714c5033834SJed Brown    Input Arguments:
5715c5033834SJed Brown .  ts - time integration context
5716c5033834SJed Brown 
5717c5033834SJed Brown    Output Arguments:
57180298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
57190298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
57200298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
57210298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5722c5033834SJed Brown 
5723c5033834SJed Brown    Level: beginner
5724c5033834SJed Brown 
57255c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5726c5033834SJed Brown @*/
5727c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5728c5033834SJed Brown {
5729c5033834SJed Brown   PetscFunctionBegin;
5730c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5731c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5732c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5733c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5734c5033834SJed Brown   PetscFunctionReturn(0);
5735c5033834SJed Brown }
5736c5033834SJed Brown 
57379c6b16b5SShri Abhyankar /*@
5738a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57399c6b16b5SShri Abhyankar 
57409c6b16b5SShri Abhyankar    Collective on TS
57419c6b16b5SShri Abhyankar 
57429c6b16b5SShri Abhyankar    Input Arguments:
57439c6b16b5SShri Abhyankar +  ts - time stepping context
5744a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5745a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57469c6b16b5SShri Abhyankar 
57479c6b16b5SShri Abhyankar    Output Arguments:
5748a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57497453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5750a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57519c6b16b5SShri Abhyankar 
57529c6b16b5SShri Abhyankar    Level: developer
57539c6b16b5SShri Abhyankar 
5754deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57559c6b16b5SShri Abhyankar @*/
57567453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57579c6b16b5SShri Abhyankar {
57589c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57599c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57607453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57617453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57629c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57637453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
57647453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
57657453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57669c6b16b5SShri Abhyankar 
57679c6b16b5SShri Abhyankar   PetscFunctionBegin;
57689c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5769a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5770a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5771a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5772a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5773a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5774a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57758a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57768a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5777a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57789c6b16b5SShri Abhyankar 
57799c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57809c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57819c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57829c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57839c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57847453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
57857453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
57867453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
57879c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57889c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57899c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57909c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57919c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
579276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57937453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57947453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57957453f775SEmil Constantinescu       if (tola>0.){
57967453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57977453f775SEmil Constantinescu         na_loc++;
57987453f775SEmil Constantinescu       }
57997453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58007453f775SEmil Constantinescu       if (tolr>0.){
58017453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58027453f775SEmil Constantinescu         nr_loc++;
58037453f775SEmil Constantinescu       }
58047453f775SEmil Constantinescu       tol=tola+tolr;
58057453f775SEmil Constantinescu       if (tol>0.){
58067453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58077453f775SEmil Constantinescu         n_loc++;
58087453f775SEmil Constantinescu       }
58099c6b16b5SShri Abhyankar     }
58109c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58119c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58129c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58139c6b16b5SShri Abhyankar     const PetscScalar *atol;
58149c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58159c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
581676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58177453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58187453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58197453f775SEmil Constantinescu       if (tola>0.){
58207453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58217453f775SEmil Constantinescu         na_loc++;
58227453f775SEmil Constantinescu       }
58237453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58247453f775SEmil Constantinescu       if (tolr>0.){
58257453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58267453f775SEmil Constantinescu         nr_loc++;
58277453f775SEmil Constantinescu       }
58287453f775SEmil Constantinescu       tol=tola+tolr;
58297453f775SEmil Constantinescu       if (tol>0.){
58307453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58317453f775SEmil Constantinescu         n_loc++;
58327453f775SEmil Constantinescu       }
58339c6b16b5SShri Abhyankar     }
58349c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58359c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58369c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58379c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58389c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
583976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58407453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58417453f775SEmil Constantinescu       tola = ts->atol;
58427453f775SEmil Constantinescu       if (tola>0.){
58437453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58447453f775SEmil Constantinescu         na_loc++;
58457453f775SEmil Constantinescu       }
58467453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58477453f775SEmil Constantinescu       if (tolr>0.){
58487453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58497453f775SEmil Constantinescu         nr_loc++;
58507453f775SEmil Constantinescu       }
58517453f775SEmil Constantinescu       tol=tola+tolr;
58527453f775SEmil Constantinescu       if (tol>0.){
58537453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58547453f775SEmil Constantinescu         n_loc++;
58557453f775SEmil Constantinescu       }
58569c6b16b5SShri Abhyankar     }
58579c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58589c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58599c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
586076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58617453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58627453f775SEmil Constantinescu       tola = ts->atol;
58637453f775SEmil Constantinescu       if (tola>0.){
58647453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58657453f775SEmil Constantinescu         na_loc++;
58667453f775SEmil Constantinescu       }
58677453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58687453f775SEmil Constantinescu       if (tolr>0.){
58697453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58707453f775SEmil Constantinescu         nr_loc++;
58717453f775SEmil Constantinescu       }
58727453f775SEmil Constantinescu       tol=tola+tolr;
58737453f775SEmil Constantinescu       if (tol>0.){
58747453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58757453f775SEmil Constantinescu         n_loc++;
58767453f775SEmil Constantinescu       }
58779c6b16b5SShri Abhyankar     }
58789c6b16b5SShri Abhyankar   }
58799c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58809c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58819c6b16b5SShri Abhyankar 
58827453f775SEmil Constantinescu   err_loc[0] = sum;
58837453f775SEmil Constantinescu   err_loc[1] = suma;
58847453f775SEmil Constantinescu   err_loc[2] = sumr;
58857453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58867453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58877453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58887453f775SEmil Constantinescu 
5889a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58907453f775SEmil Constantinescu 
58917453f775SEmil Constantinescu   gsum   = err_glb[0];
58927453f775SEmil Constantinescu   gsuma  = err_glb[1];
58937453f775SEmil Constantinescu   gsumr  = err_glb[2];
58947453f775SEmil Constantinescu   n_glb  = err_glb[3];
58957453f775SEmil Constantinescu   na_glb = err_glb[4];
58967453f775SEmil Constantinescu   nr_glb = err_glb[5];
58977453f775SEmil Constantinescu 
5898b1316ef9SEmil Constantinescu   *norm  = 0.;
5899b1316ef9SEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
5900b1316ef9SEmil Constantinescu   *norma = 0.;
5901b1316ef9SEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5902b1316ef9SEmil Constantinescu   *normr = 0.;
5903b1316ef9SEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
59049c6b16b5SShri Abhyankar 
59059c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59067453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59077453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59089c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59099c6b16b5SShri Abhyankar }
59109c6b16b5SShri Abhyankar 
59119c6b16b5SShri Abhyankar /*@
5912a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
59139c6b16b5SShri Abhyankar 
59149c6b16b5SShri Abhyankar    Collective on TS
59159c6b16b5SShri Abhyankar 
59169c6b16b5SShri Abhyankar    Input Arguments:
59179c6b16b5SShri Abhyankar +  ts - time stepping context
5918a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5919a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
59209c6b16b5SShri Abhyankar 
59219c6b16b5SShri Abhyankar    Output Arguments:
5922a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59237453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5924a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59259c6b16b5SShri Abhyankar 
59269c6b16b5SShri Abhyankar    Level: developer
59279c6b16b5SShri Abhyankar 
5928deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
59299c6b16b5SShri Abhyankar @*/
59307453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59319c6b16b5SShri Abhyankar {
59329c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
59337453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
59349c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
59357453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
59367453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
59377453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59389c6b16b5SShri Abhyankar 
59399c6b16b5SShri Abhyankar   PetscFunctionBegin;
59409c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5941a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5942a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5943a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5944a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5945a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5946a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59478a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59488a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5949a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59509c6b16b5SShri Abhyankar 
59519c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59529c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59539c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59549c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59559c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59567453f775SEmil Constantinescu 
59577453f775SEmil Constantinescu   max=0.;
59587453f775SEmil Constantinescu   maxa=0.;
59597453f775SEmil Constantinescu   maxr=0.;
59607453f775SEmil Constantinescu 
59617453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59629c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59639c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59649c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59657453f775SEmil Constantinescu 
59667453f775SEmil Constantinescu     for (i=0; i<n; i++) {
596776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59687453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59697453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59707453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59717453f775SEmil Constantinescu       tol  = tola+tolr;
59727453f775SEmil Constantinescu       if (tola>0.){
59737453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59747453f775SEmil Constantinescu       }
59757453f775SEmil Constantinescu       if (tolr>0.){
59767453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59777453f775SEmil Constantinescu       }
59787453f775SEmil Constantinescu       if (tol>0.){
59797453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59807453f775SEmil Constantinescu       }
59819c6b16b5SShri Abhyankar     }
59829c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59839c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59849c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59859c6b16b5SShri Abhyankar     const PetscScalar *atol;
59869c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59877453f775SEmil Constantinescu     for (i=0; i<n; i++) {
598876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59897453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59907453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59917453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59927453f775SEmil Constantinescu       tol  = tola+tolr;
59937453f775SEmil Constantinescu       if (tola>0.){
59947453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59957453f775SEmil Constantinescu       }
59967453f775SEmil Constantinescu       if (tolr>0.){
59977453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59987453f775SEmil Constantinescu       }
59997453f775SEmil Constantinescu       if (tol>0.){
60007453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60017453f775SEmil Constantinescu       }
60029c6b16b5SShri Abhyankar     }
60039c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60049c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60059c6b16b5SShri Abhyankar     const PetscScalar *rtol;
60069c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60077453f775SEmil Constantinescu 
60087453f775SEmil Constantinescu     for (i=0; i<n; i++) {
600976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60107453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60117453f775SEmil Constantinescu       tola = ts->atol;
60127453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60137453f775SEmil Constantinescu       tol  = tola+tolr;
60147453f775SEmil Constantinescu       if (tola>0.){
60157453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60167453f775SEmil Constantinescu       }
60177453f775SEmil Constantinescu       if (tolr>0.){
60187453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60197453f775SEmil Constantinescu       }
60207453f775SEmil Constantinescu       if (tol>0.){
60217453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60227453f775SEmil Constantinescu       }
60239c6b16b5SShri Abhyankar     }
60249c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60259c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
60267453f775SEmil Constantinescu 
60277453f775SEmil Constantinescu     for (i=0; i<n; i++) {
602876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60297453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60307453f775SEmil Constantinescu       tola = ts->atol;
60317453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60327453f775SEmil Constantinescu       tol  = tola+tolr;
60337453f775SEmil Constantinescu       if (tola>0.){
60347453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60357453f775SEmil Constantinescu       }
60367453f775SEmil Constantinescu       if (tolr>0.){
60377453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60387453f775SEmil Constantinescu       }
60397453f775SEmil Constantinescu       if (tol>0.){
60407453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60417453f775SEmil Constantinescu       }
60429c6b16b5SShri Abhyankar     }
60439c6b16b5SShri Abhyankar   }
60449c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60459c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60467453f775SEmil Constantinescu   err_loc[0] = max;
60477453f775SEmil Constantinescu   err_loc[1] = maxa;
60487453f775SEmil Constantinescu   err_loc[2] = maxr;
6049a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60507453f775SEmil Constantinescu   gmax   = err_glb[0];
60517453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60527453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60539c6b16b5SShri Abhyankar 
60549c6b16b5SShri Abhyankar   *norm = gmax;
60557453f775SEmil Constantinescu   *norma = gmaxa;
60567453f775SEmil Constantinescu   *normr = gmaxr;
60579c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60587453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60597453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60609c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60619c6b16b5SShri Abhyankar }
60629c6b16b5SShri Abhyankar 
60631c3436cfSJed Brown /*@
60648a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60651c3436cfSJed Brown 
60661c3436cfSJed Brown    Collective on TS
60671c3436cfSJed Brown 
60681c3436cfSJed Brown    Input Arguments:
60691c3436cfSJed Brown +  ts - time stepping context
6070a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6071a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6072a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60737619abb3SShri 
60741c3436cfSJed Brown    Output Arguments:
6075a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60768a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6077a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6078a4868fbcSLisandro Dalcin 
6079a4868fbcSLisandro Dalcin    Options Database Keys:
6080a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6081a4868fbcSLisandro Dalcin 
60821c3436cfSJed Brown    Level: developer
60831c3436cfSJed Brown 
60848a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
60851c3436cfSJed Brown @*/
60867453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60871c3436cfSJed Brown {
60888beabaa1SBarry Smith   PetscErrorCode ierr;
60891c3436cfSJed Brown 
60901c3436cfSJed Brown   PetscFunctionBegin;
6091a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60927453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6093a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
60947453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6095a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60961c3436cfSJed Brown   PetscFunctionReturn(0);
60971c3436cfSJed Brown }
60981c3436cfSJed Brown 
60998a175baeSEmil Constantinescu 
61008a175baeSEmil Constantinescu /*@
61018a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
61028a175baeSEmil Constantinescu 
61038a175baeSEmil Constantinescu    Collective on TS
61048a175baeSEmil Constantinescu 
61058a175baeSEmil Constantinescu    Input Arguments:
61068a175baeSEmil Constantinescu +  ts - time stepping context
61078a175baeSEmil Constantinescu .  E - error vector
61088a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61098a175baeSEmil Constantinescu -  Y - state vector, previous time step
61108a175baeSEmil Constantinescu 
61118a175baeSEmil Constantinescu    Output Arguments:
6112a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61138a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6114a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61158a175baeSEmil Constantinescu 
61168a175baeSEmil Constantinescu    Level: developer
61178a175baeSEmil Constantinescu 
61188a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
61198a175baeSEmil Constantinescu @*/
61208a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61218a175baeSEmil Constantinescu {
61228a175baeSEmil Constantinescu   PetscErrorCode    ierr;
61238a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
61248a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
61258a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
61268a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
61278a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
61288a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
61298a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
61308a175baeSEmil Constantinescu 
61318a175baeSEmil Constantinescu   PetscFunctionBegin;
61328a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61338a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
61348a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
61358a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61368a175baeSEmil Constantinescu   PetscValidType(E,2);
61378a175baeSEmil Constantinescu   PetscValidType(U,3);
61388a175baeSEmil Constantinescu   PetscValidType(Y,4);
61398a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61408a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61418a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61428a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61438a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61448a175baeSEmil Constantinescu 
61458a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61468a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61478a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61488a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61498a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61508a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61518a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61528a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61538a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61548a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61558a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61568a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61578a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61588a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
615976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61608a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61618a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61628a175baeSEmil Constantinescu       if (tola>0.){
61638a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61648a175baeSEmil Constantinescu         na_loc++;
61658a175baeSEmil Constantinescu       }
61668a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61678a175baeSEmil Constantinescu       if (tolr>0.){
61688a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61698a175baeSEmil Constantinescu         nr_loc++;
61708a175baeSEmil Constantinescu       }
61718a175baeSEmil Constantinescu       tol=tola+tolr;
61728a175baeSEmil Constantinescu       if (tol>0.){
61738a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61748a175baeSEmil Constantinescu         n_loc++;
61758a175baeSEmil Constantinescu       }
61768a175baeSEmil Constantinescu     }
61778a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61788a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61798a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61808a175baeSEmil Constantinescu     const PetscScalar *atol;
61818a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61828a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
618376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61848a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61858a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61868a175baeSEmil Constantinescu       if (tola>0.){
61878a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61888a175baeSEmil Constantinescu         na_loc++;
61898a175baeSEmil Constantinescu       }
61908a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61918a175baeSEmil Constantinescu       if (tolr>0.){
61928a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61938a175baeSEmil Constantinescu         nr_loc++;
61948a175baeSEmil Constantinescu       }
61958a175baeSEmil Constantinescu       tol=tola+tolr;
61968a175baeSEmil Constantinescu       if (tol>0.){
61978a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61988a175baeSEmil Constantinescu         n_loc++;
61998a175baeSEmil Constantinescu       }
62008a175baeSEmil Constantinescu     }
62018a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62028a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62038a175baeSEmil Constantinescu     const PetscScalar *rtol;
62048a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62058a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
620676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62078a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62088a175baeSEmil Constantinescu       tola = ts->atol;
62098a175baeSEmil Constantinescu       if (tola>0.){
62108a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62118a175baeSEmil Constantinescu         na_loc++;
62128a175baeSEmil Constantinescu       }
62138a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62148a175baeSEmil Constantinescu       if (tolr>0.){
62158a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62168a175baeSEmil Constantinescu         nr_loc++;
62178a175baeSEmil Constantinescu       }
62188a175baeSEmil Constantinescu       tol=tola+tolr;
62198a175baeSEmil Constantinescu       if (tol>0.){
62208a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62218a175baeSEmil Constantinescu         n_loc++;
62228a175baeSEmil Constantinescu       }
62238a175baeSEmil Constantinescu     }
62248a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62258a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
62268a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
622776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62288a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62298a175baeSEmil Constantinescu       tola = ts->atol;
62308a175baeSEmil Constantinescu       if (tola>0.){
62318a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62328a175baeSEmil Constantinescu         na_loc++;
62338a175baeSEmil Constantinescu       }
62348a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62358a175baeSEmil Constantinescu       if (tolr>0.){
62368a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62378a175baeSEmil Constantinescu         nr_loc++;
62388a175baeSEmil Constantinescu       }
62398a175baeSEmil Constantinescu       tol=tola+tolr;
62408a175baeSEmil Constantinescu       if (tol>0.){
62418a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62428a175baeSEmil Constantinescu         n_loc++;
62438a175baeSEmil Constantinescu       }
62448a175baeSEmil Constantinescu     }
62458a175baeSEmil Constantinescu   }
62468a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62478a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62488a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62498a175baeSEmil Constantinescu 
62508a175baeSEmil Constantinescu   err_loc[0] = sum;
62518a175baeSEmil Constantinescu   err_loc[1] = suma;
62528a175baeSEmil Constantinescu   err_loc[2] = sumr;
62538a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62548a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62558a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62568a175baeSEmil Constantinescu 
6257a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62588a175baeSEmil Constantinescu 
62598a175baeSEmil Constantinescu   gsum   = err_glb[0];
62608a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62618a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62628a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62638a175baeSEmil Constantinescu   na_glb = err_glb[4];
62648a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62658a175baeSEmil Constantinescu 
62668a175baeSEmil Constantinescu   *norm  = 0.;
62678a175baeSEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
62688a175baeSEmil Constantinescu   *norma = 0.;
62698a175baeSEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62708a175baeSEmil Constantinescu   *normr = 0.;
62718a175baeSEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62728a175baeSEmil Constantinescu 
62738a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62748a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62758a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62768a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62778a175baeSEmil Constantinescu }
62788a175baeSEmil Constantinescu 
62798a175baeSEmil Constantinescu /*@
62808a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62818a175baeSEmil Constantinescu    Collective on TS
62828a175baeSEmil Constantinescu 
62838a175baeSEmil Constantinescu    Input Arguments:
62848a175baeSEmil Constantinescu +  ts - time stepping context
62858a175baeSEmil Constantinescu .  E - error vector
62868a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62878a175baeSEmil Constantinescu -  Y - state vector, previous time step
62888a175baeSEmil Constantinescu 
62898a175baeSEmil Constantinescu    Output Arguments:
6290a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62918a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6292a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62938a175baeSEmil Constantinescu 
62948a175baeSEmil Constantinescu    Level: developer
62958a175baeSEmil Constantinescu 
62968a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62978a175baeSEmil Constantinescu @*/
62988a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62998a175baeSEmil Constantinescu {
63008a175baeSEmil Constantinescu   PetscErrorCode    ierr;
63018a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
63028a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
63038a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
63048a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
63058a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
63068a175baeSEmil Constantinescu 
63078a175baeSEmil Constantinescu   PetscFunctionBegin;
63088a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63098a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
63108a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
63118a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
63128a175baeSEmil Constantinescu   PetscValidType(E,2);
63138a175baeSEmil Constantinescu   PetscValidType(U,3);
63148a175baeSEmil Constantinescu   PetscValidType(Y,4);
63158a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
63168a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
63178a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
63188a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
63198a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
63208a175baeSEmil Constantinescu 
63218a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
63228a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
63238a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
63248a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
63258a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
63268a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
63278a175baeSEmil Constantinescu 
63288a175baeSEmil Constantinescu   max=0.;
63298a175baeSEmil Constantinescu   maxa=0.;
63308a175baeSEmil Constantinescu   maxr=0.;
63318a175baeSEmil Constantinescu 
63328a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
63338a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
63348a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63358a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63368a175baeSEmil Constantinescu 
63378a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
633876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63398a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63408a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63418a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63428a175baeSEmil Constantinescu       tol  = tola+tolr;
63438a175baeSEmil Constantinescu       if (tola>0.){
63448a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63458a175baeSEmil Constantinescu       }
63468a175baeSEmil Constantinescu       if (tolr>0.){
63478a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63488a175baeSEmil Constantinescu       }
63498a175baeSEmil Constantinescu       if (tol>0.){
63508a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63518a175baeSEmil Constantinescu       }
63528a175baeSEmil Constantinescu     }
63538a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63548a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63558a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63568a175baeSEmil Constantinescu     const PetscScalar *atol;
63578a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63588a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
635976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63608a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63618a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63628a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63638a175baeSEmil Constantinescu       tol  = tola+tolr;
63648a175baeSEmil Constantinescu       if (tola>0.){
63658a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63668a175baeSEmil Constantinescu       }
63678a175baeSEmil Constantinescu       if (tolr>0.){
63688a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63698a175baeSEmil Constantinescu       }
63708a175baeSEmil Constantinescu       if (tol>0.){
63718a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63728a175baeSEmil Constantinescu       }
63738a175baeSEmil Constantinescu     }
63748a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63758a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63768a175baeSEmil Constantinescu     const PetscScalar *rtol;
63778a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63788a175baeSEmil Constantinescu 
63798a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
638076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63818a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63828a175baeSEmil Constantinescu       tola = ts->atol;
63838a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63848a175baeSEmil Constantinescu       tol  = tola+tolr;
63858a175baeSEmil Constantinescu       if (tola>0.){
63868a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63878a175baeSEmil Constantinescu       }
63888a175baeSEmil Constantinescu       if (tolr>0.){
63898a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63908a175baeSEmil Constantinescu       }
63918a175baeSEmil Constantinescu       if (tol>0.){
63928a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63938a175baeSEmil Constantinescu       }
63948a175baeSEmil Constantinescu     }
63958a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63968a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63978a175baeSEmil Constantinescu 
63988a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
639976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
64008a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64018a175baeSEmil Constantinescu       tola = ts->atol;
64028a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64038a175baeSEmil Constantinescu       tol  = tola+tolr;
64048a175baeSEmil Constantinescu       if (tola>0.){
64058a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64068a175baeSEmil Constantinescu       }
64078a175baeSEmil Constantinescu       if (tolr>0.){
64088a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64098a175baeSEmil Constantinescu       }
64108a175baeSEmil Constantinescu       if (tol>0.){
64118a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64128a175baeSEmil Constantinescu       }
64138a175baeSEmil Constantinescu     }
64148a175baeSEmil Constantinescu   }
64158a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
64168a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
64178a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
64188a175baeSEmil Constantinescu   err_loc[0] = max;
64198a175baeSEmil Constantinescu   err_loc[1] = maxa;
64208a175baeSEmil Constantinescu   err_loc[2] = maxr;
6421a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64228a175baeSEmil Constantinescu   gmax   = err_glb[0];
64238a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
64248a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
64258a175baeSEmil Constantinescu 
64268a175baeSEmil Constantinescu   *norm = gmax;
64278a175baeSEmil Constantinescu   *norma = gmaxa;
64288a175baeSEmil Constantinescu   *normr = gmaxr;
64298a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
64308a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
64318a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
64328a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64338a175baeSEmil Constantinescu }
64348a175baeSEmil Constantinescu 
64358a175baeSEmil Constantinescu /*@
64368a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
64378a175baeSEmil Constantinescu 
64388a175baeSEmil Constantinescu    Collective on TS
64398a175baeSEmil Constantinescu 
64408a175baeSEmil Constantinescu    Input Arguments:
64418a175baeSEmil Constantinescu +  ts - time stepping context
64428a175baeSEmil Constantinescu .  E - error vector
64438a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64448a175baeSEmil Constantinescu .  Y - state vector, previous time step
64458a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64468a175baeSEmil Constantinescu 
64478a175baeSEmil Constantinescu    Output Arguments:
6448a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64498a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6450a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64518a175baeSEmil Constantinescu 
64528a175baeSEmil Constantinescu    Options Database Keys:
64538a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64548a175baeSEmil Constantinescu 
64558a175baeSEmil Constantinescu    Level: developer
64568a175baeSEmil Constantinescu 
64578a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64588a175baeSEmil Constantinescu @*/
64598a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64608a175baeSEmil Constantinescu {
64618a175baeSEmil Constantinescu   PetscErrorCode ierr;
64628a175baeSEmil Constantinescu 
64638a175baeSEmil Constantinescu   PetscFunctionBegin;
64648a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64658a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64668a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
64678a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64688a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64698a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64708a175baeSEmil Constantinescu }
64718a175baeSEmil Constantinescu 
64728a175baeSEmil Constantinescu 
64738d59e960SJed Brown /*@
64748d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64758d59e960SJed Brown 
64768d59e960SJed Brown    Logically Collective on TS
64778d59e960SJed Brown 
64788d59e960SJed Brown    Input Arguments:
64798d59e960SJed Brown +  ts - time stepping context
64808d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64818d59e960SJed Brown 
64828d59e960SJed Brown    Note:
64838d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64848d59e960SJed Brown 
64858d59e960SJed Brown    Level: intermediate
64868d59e960SJed Brown 
64878d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64888d59e960SJed Brown @*/
64898d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64908d59e960SJed Brown {
64918d59e960SJed Brown   PetscFunctionBegin;
64928d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64938d59e960SJed Brown   ts->cfltime_local = cfltime;
64948d59e960SJed Brown   ts->cfltime       = -1.;
64958d59e960SJed Brown   PetscFunctionReturn(0);
64968d59e960SJed Brown }
64978d59e960SJed Brown 
64988d59e960SJed Brown /*@
64998d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
65008d59e960SJed Brown 
65018d59e960SJed Brown    Collective on TS
65028d59e960SJed Brown 
65038d59e960SJed Brown    Input Arguments:
65048d59e960SJed Brown .  ts - time stepping context
65058d59e960SJed Brown 
65068d59e960SJed Brown    Output Arguments:
65078d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
65088d59e960SJed Brown 
65098d59e960SJed Brown    Level: advanced
65108d59e960SJed Brown 
65118d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
65128d59e960SJed Brown @*/
65138d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
65148d59e960SJed Brown {
65158d59e960SJed Brown   PetscErrorCode ierr;
65168d59e960SJed Brown 
65178d59e960SJed Brown   PetscFunctionBegin;
65188d59e960SJed Brown   if (ts->cfltime < 0) {
6519b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65208d59e960SJed Brown   }
65218d59e960SJed Brown   *cfltime = ts->cfltime;
65228d59e960SJed Brown   PetscFunctionReturn(0);
65238d59e960SJed Brown }
65248d59e960SJed Brown 
6525d6ebe24aSShri Abhyankar /*@
6526d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6527d6ebe24aSShri Abhyankar 
6528d6ebe24aSShri Abhyankar    Input Parameters:
6529a2b725a8SWilliam Gropp +  ts   - the TS context.
6530d6ebe24aSShri Abhyankar .  xl   - lower bound.
6531a2b725a8SWilliam Gropp -  xu   - upper bound.
6532d6ebe24aSShri Abhyankar 
6533d6ebe24aSShri Abhyankar    Notes:
6534d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6535e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6536d6ebe24aSShri Abhyankar 
65372bd2b0e6SSatish Balay    Level: advanced
65382bd2b0e6SSatish Balay 
6539d6ebe24aSShri Abhyankar @*/
6540d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6541d6ebe24aSShri Abhyankar {
6542d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6543d6ebe24aSShri Abhyankar   SNES           snes;
6544d6ebe24aSShri Abhyankar 
6545d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6546d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6547d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6548d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6549d6ebe24aSShri Abhyankar }
6550d6ebe24aSShri Abhyankar 
6551b3603a34SBarry Smith /*@C
65524f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6553b3603a34SBarry Smith        in a time based line graph
6554b3603a34SBarry Smith 
6555b3603a34SBarry Smith    Collective on TS
6556b3603a34SBarry Smith 
6557b3603a34SBarry Smith    Input Parameters:
6558b3603a34SBarry Smith +  ts - the TS context
6559b3603a34SBarry Smith .  step - current time-step
6560b3603a34SBarry Smith .  ptime - current time
65617db568b7SBarry Smith .  u - current solution
65627db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6563b3603a34SBarry Smith 
6564b3d3934dSBarry Smith    Options Database:
65659ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6566b3d3934dSBarry Smith 
6567b3603a34SBarry Smith    Level: intermediate
6568b3603a34SBarry Smith 
656995452b02SPatrick Sanan    Notes:
657095452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6571b3603a34SBarry Smith 
65727db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65737db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65747db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65757db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6576b3603a34SBarry Smith @*/
65777db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6578b3603a34SBarry Smith {
6579b3603a34SBarry Smith   PetscErrorCode    ierr;
65807db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6581b3603a34SBarry Smith   const PetscScalar *yy;
658280666b62SBarry Smith   Vec               v;
6583b3603a34SBarry Smith 
6584b3603a34SBarry Smith   PetscFunctionBegin;
658563e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
658658ff32f7SBarry Smith   if (!step) {
6587a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65886934998bSLisandro Dalcin     PetscInt      dim;
6589a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65900ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6591bab0a581SBarry Smith     if (!ctx->names) {
6592bab0a581SBarry Smith       PetscBool flg;
6593bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6594bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6595bab0a581SBarry Smith       if (flg) {
6596bab0a581SBarry Smith         PetscInt i,n;
6597bab0a581SBarry Smith         char     **names;
6598bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6599bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6600bab0a581SBarry Smith         for (i=0; i<n; i++) {
6601bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6602bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6603bab0a581SBarry Smith         }
6604bab0a581SBarry Smith         names[n] = NULL;
6605bab0a581SBarry Smith         ctx->names = names;
6606bab0a581SBarry Smith       }
6607bab0a581SBarry Smith     }
6608387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6609387f4636SBarry Smith       char      **displaynames;
6610387f4636SBarry Smith       PetscBool flg;
6611387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6612580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6613c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6614387f4636SBarry Smith       if (flg) {
6615a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6616387f4636SBarry Smith       }
6617387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6618387f4636SBarry Smith     }
6619387f4636SBarry Smith     if (ctx->displaynames) {
6620387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6621387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6622387f4636SBarry Smith     } else if (ctx->names) {
66230910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
66240b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6625387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6626b0bc92c6SBarry Smith     } else {
6627b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6628b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6629387f4636SBarry Smith     }
66300b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
663158ff32f7SBarry Smith   }
66326934998bSLisandro Dalcin 
66336934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66346934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
663580666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66366934998bSLisandro Dalcin   if (ctx->displaynames) {
66376934998bSLisandro Dalcin     PetscInt i;
66386934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66396934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66406934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66416934998bSLisandro Dalcin   } else {
6642e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6643e3efe391SJed Brown     PetscInt  i,n;
66446934998bSLisandro Dalcin     PetscReal *yreal;
664580666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6646785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6647e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66480b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6649e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6650e3efe391SJed Brown #else
66510b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6652e3efe391SJed Brown #endif
665380666b62SBarry Smith   }
66546934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66556934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66566934998bSLisandro Dalcin 
6657b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66580b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66596934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66603923b477SBarry Smith   }
6661b3603a34SBarry Smith   PetscFunctionReturn(0);
6662b3603a34SBarry Smith }
6663b3603a34SBarry Smith 
6664b037adc7SBarry Smith /*@C
666531152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6666b037adc7SBarry Smith 
6667b037adc7SBarry Smith    Collective on TS
6668b037adc7SBarry Smith 
6669b037adc7SBarry Smith    Input Parameters:
6670b037adc7SBarry Smith +  ts - the TS context
6671b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6672b037adc7SBarry Smith 
6673b037adc7SBarry Smith    Level: intermediate
6674b037adc7SBarry Smith 
667595452b02SPatrick Sanan    Notes:
667695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66777db568b7SBarry Smith 
6678a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6679b037adc7SBarry Smith @*/
668031152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6681b037adc7SBarry Smith {
6682b037adc7SBarry Smith   PetscErrorCode    ierr;
6683b037adc7SBarry Smith   PetscInt          i;
6684b037adc7SBarry Smith 
6685b037adc7SBarry Smith   PetscFunctionBegin;
6686b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6687b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66885537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6689b3d3934dSBarry Smith       break;
6690b3d3934dSBarry Smith     }
6691b3d3934dSBarry Smith   }
6692b3d3934dSBarry Smith   PetscFunctionReturn(0);
6693b3d3934dSBarry Smith }
6694b3d3934dSBarry Smith 
6695e673d494SBarry Smith /*@C
6696e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6697e673d494SBarry Smith 
6698e673d494SBarry Smith    Collective on TS
6699e673d494SBarry Smith 
6700e673d494SBarry Smith    Input Parameters:
6701e673d494SBarry Smith +  ts - the TS context
6702e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6703e673d494SBarry Smith 
6704e673d494SBarry Smith    Level: intermediate
6705e673d494SBarry Smith 
6706a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6707e673d494SBarry Smith @*/
6708e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6709e673d494SBarry Smith {
6710e673d494SBarry Smith   PetscErrorCode    ierr;
6711e673d494SBarry Smith 
6712e673d494SBarry Smith   PetscFunctionBegin;
6713e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6714e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6715e673d494SBarry Smith   PetscFunctionReturn(0);
6716e673d494SBarry Smith }
6717e673d494SBarry Smith 
6718b3d3934dSBarry Smith /*@C
6719b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6720b3d3934dSBarry Smith 
6721b3d3934dSBarry Smith    Collective on TS
6722b3d3934dSBarry Smith 
6723b3d3934dSBarry Smith    Input Parameter:
6724b3d3934dSBarry Smith .  ts - the TS context
6725b3d3934dSBarry Smith 
6726b3d3934dSBarry Smith    Output Parameter:
6727b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6728b3d3934dSBarry Smith 
6729b3d3934dSBarry Smith    Level: intermediate
6730b3d3934dSBarry Smith 
673195452b02SPatrick Sanan    Notes:
673295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67337db568b7SBarry Smith 
6734b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6735b3d3934dSBarry Smith @*/
6736b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6737b3d3934dSBarry Smith {
6738b3d3934dSBarry Smith   PetscInt       i;
6739b3d3934dSBarry Smith 
6740b3d3934dSBarry Smith   PetscFunctionBegin;
6741b3d3934dSBarry Smith   *names = NULL;
6742b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6743b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67445537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6745b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6746b3d3934dSBarry Smith       break;
6747387f4636SBarry Smith     }
6748387f4636SBarry Smith   }
6749387f4636SBarry Smith   PetscFunctionReturn(0);
6750387f4636SBarry Smith }
6751387f4636SBarry Smith 
6752a66092f1SBarry Smith /*@C
6753a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6754a66092f1SBarry Smith 
6755a66092f1SBarry Smith    Collective on TS
6756a66092f1SBarry Smith 
6757a66092f1SBarry Smith    Input Parameters:
6758a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6759a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6760a66092f1SBarry Smith 
6761a66092f1SBarry Smith    Level: intermediate
6762a66092f1SBarry Smith 
6763a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6764a66092f1SBarry Smith @*/
6765a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6766a66092f1SBarry Smith {
6767a66092f1SBarry Smith   PetscInt          j = 0,k;
6768a66092f1SBarry Smith   PetscErrorCode    ierr;
6769a66092f1SBarry Smith 
6770a66092f1SBarry Smith   PetscFunctionBegin;
6771a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6772a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6773a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6774a66092f1SBarry Smith   while (displaynames[j]) j++;
6775a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6776a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6777a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6778a66092f1SBarry Smith   j = 0;
6779a66092f1SBarry Smith   while (displaynames[j]) {
6780a66092f1SBarry Smith     k = 0;
6781a66092f1SBarry Smith     while (ctx->names[k]) {
6782a66092f1SBarry Smith       PetscBool flg;
6783a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6784a66092f1SBarry Smith       if (flg) {
6785a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6786a66092f1SBarry Smith         break;
6787a66092f1SBarry Smith       }
6788a66092f1SBarry Smith       k++;
6789a66092f1SBarry Smith     }
6790a66092f1SBarry Smith     j++;
6791a66092f1SBarry Smith   }
6792a66092f1SBarry Smith   PetscFunctionReturn(0);
6793a66092f1SBarry Smith }
6794a66092f1SBarry Smith 
6795387f4636SBarry Smith /*@C
6796387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6797387f4636SBarry Smith 
6798387f4636SBarry Smith    Collective on TS
6799387f4636SBarry Smith 
6800387f4636SBarry Smith    Input Parameters:
6801387f4636SBarry Smith +  ts - the TS context
6802a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6803387f4636SBarry Smith 
680495452b02SPatrick Sanan    Notes:
680595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68067db568b7SBarry Smith 
6807387f4636SBarry Smith    Level: intermediate
6808387f4636SBarry Smith 
6809387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6810387f4636SBarry Smith @*/
6811387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6812387f4636SBarry Smith {
6813a66092f1SBarry Smith   PetscInt          i;
6814387f4636SBarry Smith   PetscErrorCode    ierr;
6815387f4636SBarry Smith 
6816387f4636SBarry Smith   PetscFunctionBegin;
6817387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6818387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68195537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6820b3d3934dSBarry Smith       break;
6821b037adc7SBarry Smith     }
6822b037adc7SBarry Smith   }
6823b037adc7SBarry Smith   PetscFunctionReturn(0);
6824b037adc7SBarry Smith }
6825b037adc7SBarry Smith 
682680666b62SBarry Smith /*@C
682780666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
682880666b62SBarry Smith 
682980666b62SBarry Smith    Collective on TS
683080666b62SBarry Smith 
683180666b62SBarry Smith    Input Parameters:
683280666b62SBarry Smith +  ts - the TS context
683380666b62SBarry Smith .  transform - the transform function
68347684fa3eSBarry Smith .  destroy - function to destroy the optional context
683580666b62SBarry Smith -  ctx - optional context used by transform function
683680666b62SBarry Smith 
683795452b02SPatrick Sanan    Notes:
683895452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68397db568b7SBarry Smith 
684080666b62SBarry Smith    Level: intermediate
684180666b62SBarry Smith 
6842a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
684380666b62SBarry Smith @*/
68447684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
684580666b62SBarry Smith {
684680666b62SBarry Smith   PetscInt          i;
6847a66092f1SBarry Smith   PetscErrorCode    ierr;
684880666b62SBarry Smith 
684980666b62SBarry Smith   PetscFunctionBegin;
685080666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
685180666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68525537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
685380666b62SBarry Smith     }
685480666b62SBarry Smith   }
685580666b62SBarry Smith   PetscFunctionReturn(0);
685680666b62SBarry Smith }
685780666b62SBarry Smith 
6858e673d494SBarry Smith /*@C
6859e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6860e673d494SBarry Smith 
6861e673d494SBarry Smith    Collective on TSLGCtx
6862e673d494SBarry Smith 
6863e673d494SBarry Smith    Input Parameters:
6864e673d494SBarry Smith +  ts - the TS context
6865e673d494SBarry Smith .  transform - the transform function
68667684fa3eSBarry Smith .  destroy - function to destroy the optional context
6867e673d494SBarry Smith -  ctx - optional context used by transform function
6868e673d494SBarry Smith 
6869e673d494SBarry Smith    Level: intermediate
6870e673d494SBarry Smith 
6871a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6872e673d494SBarry Smith @*/
68737684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6874e673d494SBarry Smith {
6875e673d494SBarry Smith   PetscFunctionBegin;
6876e673d494SBarry Smith   ctx->transform    = transform;
68777684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6878e673d494SBarry Smith   ctx->transformctx = tctx;
6879e673d494SBarry Smith   PetscFunctionReturn(0);
6880e673d494SBarry Smith }
6881e673d494SBarry Smith 
6882ef20d060SBarry Smith /*@C
68838b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6884ef20d060SBarry Smith        in a time based line graph
6885ef20d060SBarry Smith 
6886ef20d060SBarry Smith    Collective on TS
6887ef20d060SBarry Smith 
6888ef20d060SBarry Smith    Input Parameters:
6889ef20d060SBarry Smith +  ts - the TS context
6890ef20d060SBarry Smith .  step - current time-step
6891ef20d060SBarry Smith .  ptime - current time
68927db568b7SBarry Smith .  u - current solution
68937db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6894ef20d060SBarry Smith 
6895ef20d060SBarry Smith    Level: intermediate
6896ef20d060SBarry Smith 
689795452b02SPatrick Sanan    Notes:
689895452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6899abd5a294SJed Brown 
6900abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6901abd5a294SJed Brown 
6902abd5a294SJed Brown    Options Database Keys:
69034f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6904ef20d060SBarry Smith 
6905abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6906ef20d060SBarry Smith @*/
69070910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6908ef20d060SBarry Smith {
6909ef20d060SBarry Smith   PetscErrorCode    ierr;
69100b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6911ef20d060SBarry Smith   const PetscScalar *yy;
6912ef20d060SBarry Smith   Vec               y;
6913ef20d060SBarry Smith 
6914ef20d060SBarry Smith   PetscFunctionBegin;
6915a9f9c1f6SBarry Smith   if (!step) {
6916a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69176934998bSLisandro Dalcin     PetscInt      dim;
6918a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
69198b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
69200910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6921a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6922a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6923a9f9c1f6SBarry Smith   }
69240910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6925ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69260910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6927ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6928e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6929e3efe391SJed Brown   {
6930e3efe391SJed Brown     PetscReal *yreal;
6931e3efe391SJed Brown     PetscInt  i,n;
6932e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6933785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6934e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69350b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6936e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6937e3efe391SJed Brown   }
6938e3efe391SJed Brown #else
69390b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6940e3efe391SJed Brown #endif
6941ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6942ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6943b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69440b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69456934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69463923b477SBarry Smith   }
6947ef20d060SBarry Smith   PetscFunctionReturn(0);
6948ef20d060SBarry Smith }
6949ef20d060SBarry Smith 
69505e3b7effSJoseph Pusztay /*@C
69515e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
69525e3b7effSJoseph Pusztay 
69535e3b7effSJoseph Pusztay    Input Parameters:
69545e3b7effSJoseph Pusztay +  ts - the TS context
69555e3b7effSJoseph Pusztay .  step - current time-step
69565e3b7effSJoseph Pusztay .  ptime - current time
69575e3b7effSJoseph Pusztay .  u - current solution
69585e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
69595e3b7effSJoseph Pusztay 
69605e3b7effSJoseph Pusztay    Options Database:
6961918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
69625e3b7effSJoseph Pusztay 
69635e3b7effSJoseph Pusztay    Level: intermediate
69645e3b7effSJoseph Pusztay 
69655e3b7effSJoseph Pusztay @*/
69660ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
69671b575b74SJoseph Pusztay {
69681b575b74SJoseph Pusztay   PetscErrorCode    ierr;
69691b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
69701b575b74SJoseph Pusztay   const PetscScalar *yy;
6971b1670a61SJoseph Pusztay   PetscReal       *y,*x;
69721b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
69731b575b74SJoseph Pusztay   DM                dm;
69741b575b74SJoseph Pusztay 
69751b575b74SJoseph Pusztay   PetscFunctionBegin;
69761b575b74SJoseph Pusztay 
69771b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
69781b575b74SJoseph Pusztay   if (!step) {
69791b575b74SJoseph Pusztay     PetscDrawAxis axis;
69801b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
69811b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6982895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6983895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
69841b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
69851b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
69861b575b74SJoseph Pusztay     if (dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
69871b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69881b575b74SJoseph Pusztay     Np /= 2*dim;
69891b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69901b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69911b575b74SJoseph Pusztay   }
69921b575b74SJoseph Pusztay 
69931b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69941b575b74SJoseph Pusztay   Np /= 2*dim;
69951b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6996895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69971b575b74SJoseph Pusztay   /* get points from solution vector */
69981b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6999b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
7000b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
7001895f37d5SJoseph Pusztay   }
70021b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
70031b575b74SJoseph Pusztay 
70041b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
70051b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
70061b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
70071b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
70081b575b74SJoseph Pusztay   }
70091b575b74SJoseph Pusztay 
7010918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
7011918b1d10SJoseph Pusztay 
70121b575b74SJoseph Pusztay   PetscFunctionReturn(0);
70131b575b74SJoseph Pusztay }
70141b575b74SJoseph Pusztay 
70151b575b74SJoseph Pusztay 
70161b575b74SJoseph Pusztay 
70177cf37e64SBarry Smith /*@C
70187cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
70197cf37e64SBarry Smith 
70207cf37e64SBarry Smith    Collective on TS
70217cf37e64SBarry Smith 
70227cf37e64SBarry Smith    Input Parameters:
70237cf37e64SBarry Smith +  ts - the TS context
70247cf37e64SBarry Smith .  step - current time-step
70257cf37e64SBarry Smith .  ptime - current time
70267cf37e64SBarry Smith .  u - current solution
70277cf37e64SBarry Smith -  dctx - unused context
70287cf37e64SBarry Smith 
70297cf37e64SBarry Smith    Level: intermediate
70307cf37e64SBarry Smith 
70317cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
70327cf37e64SBarry Smith 
70337cf37e64SBarry Smith    Options Database Keys:
70347cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
70357cf37e64SBarry Smith 
70367cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
70377cf37e64SBarry Smith @*/
7038edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70397cf37e64SBarry Smith {
70407cf37e64SBarry Smith   PetscErrorCode    ierr;
70417cf37e64SBarry Smith   Vec               y;
70427cf37e64SBarry Smith   PetscReal         nrm;
7043edbaebb3SBarry Smith   PetscBool         flg;
70447cf37e64SBarry Smith 
70457cf37e64SBarry Smith   PetscFunctionBegin;
70467cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70477cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70487cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7049edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7050edbaebb3SBarry Smith   if (flg) {
70517cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7052edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7053edbaebb3SBarry Smith   }
7054edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7055edbaebb3SBarry Smith   if (flg) {
7056edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7057edbaebb3SBarry Smith   }
7058edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70597cf37e64SBarry Smith   PetscFunctionReturn(0);
70607cf37e64SBarry Smith }
70617cf37e64SBarry Smith 
7062201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7063201da799SBarry Smith {
7064201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7065201da799SBarry Smith   PetscReal      x   = ptime,y;
7066201da799SBarry Smith   PetscErrorCode ierr;
7067201da799SBarry Smith   PetscInt       its;
7068201da799SBarry Smith 
7069201da799SBarry Smith   PetscFunctionBegin;
707063e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7071201da799SBarry Smith   if (!n) {
7072201da799SBarry Smith     PetscDrawAxis axis;
7073201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7074201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7075201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7076201da799SBarry Smith     ctx->snes_its = 0;
7077201da799SBarry Smith   }
7078201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7079201da799SBarry Smith   y    = its - ctx->snes_its;
7080201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70813fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7082201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70836934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7084201da799SBarry Smith   }
7085201da799SBarry Smith   ctx->snes_its = its;
7086201da799SBarry Smith   PetscFunctionReturn(0);
7087201da799SBarry Smith }
7088201da799SBarry Smith 
7089201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7090201da799SBarry Smith {
7091201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7092201da799SBarry Smith   PetscReal      x   = ptime,y;
7093201da799SBarry Smith   PetscErrorCode ierr;
7094201da799SBarry Smith   PetscInt       its;
7095201da799SBarry Smith 
7096201da799SBarry Smith   PetscFunctionBegin;
709763e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7098201da799SBarry Smith   if (!n) {
7099201da799SBarry Smith     PetscDrawAxis axis;
7100201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7101201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7102201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7103201da799SBarry Smith     ctx->ksp_its = 0;
7104201da799SBarry Smith   }
7105201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7106201da799SBarry Smith   y    = its - ctx->ksp_its;
7107201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
710899fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7109201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
71106934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7111201da799SBarry Smith   }
7112201da799SBarry Smith   ctx->ksp_its = its;
7113201da799SBarry Smith   PetscFunctionReturn(0);
7114201da799SBarry Smith }
7115f9c1d6abSBarry Smith 
7116f9c1d6abSBarry Smith /*@
7117f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7118f9c1d6abSBarry Smith 
7119d083f849SBarry Smith    Collective on TS
7120f9c1d6abSBarry Smith 
7121f9c1d6abSBarry Smith    Input Parameters:
7122f9c1d6abSBarry Smith +  ts - the TS context
7123f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7124f9c1d6abSBarry Smith 
7125f9c1d6abSBarry Smith    Output Parameters:
7126f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7127f9c1d6abSBarry Smith 
7128f9c1d6abSBarry Smith    Level: developer
7129f9c1d6abSBarry Smith 
7130f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7131f9c1d6abSBarry Smith @*/
7132f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7133f9c1d6abSBarry Smith {
7134f9c1d6abSBarry Smith   PetscErrorCode ierr;
7135f9c1d6abSBarry Smith 
7136f9c1d6abSBarry Smith   PetscFunctionBegin;
7137f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7138ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7139f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7140f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7141f9c1d6abSBarry Smith }
714224655328SShri 
7143b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7144b3d3934dSBarry Smith /*@C
7145b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7146b3d3934dSBarry Smith 
7147b3d3934dSBarry Smith    Collective on TS
7148b3d3934dSBarry Smith 
7149b3d3934dSBarry Smith    Input Parameters:
7150b3d3934dSBarry Smith .  ts  - the ODE solver object
7151b3d3934dSBarry Smith 
7152b3d3934dSBarry Smith    Output Parameter:
7153b3d3934dSBarry Smith .  ctx - the context
7154b3d3934dSBarry Smith 
7155b3d3934dSBarry Smith    Level: intermediate
7156b3d3934dSBarry Smith 
7157b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7158b3d3934dSBarry Smith 
7159b3d3934dSBarry Smith @*/
7160b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7161b3d3934dSBarry Smith {
7162b3d3934dSBarry Smith   PetscErrorCode ierr;
7163b3d3934dSBarry Smith 
7164b3d3934dSBarry Smith   PetscFunctionBegin;
7165a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7166b3d3934dSBarry Smith   PetscFunctionReturn(0);
7167b3d3934dSBarry Smith }
7168b3d3934dSBarry Smith 
7169b3d3934dSBarry Smith /*@C
7170b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7171b3d3934dSBarry Smith 
7172b3d3934dSBarry Smith    Collective on TS
7173b3d3934dSBarry Smith 
7174b3d3934dSBarry Smith    Input Parameters:
7175b3d3934dSBarry Smith +  ts - the TS context
7176b3d3934dSBarry Smith .  step - current time-step
7177b3d3934dSBarry Smith .  ptime - current time
71787db568b7SBarry Smith .  u  - current solution
71797db568b7SBarry Smith -  dctx - the envelope context
7180b3d3934dSBarry Smith 
7181b3d3934dSBarry Smith    Options Database:
7182b3d3934dSBarry Smith .  -ts_monitor_envelope
7183b3d3934dSBarry Smith 
7184b3d3934dSBarry Smith    Level: intermediate
7185b3d3934dSBarry Smith 
718695452b02SPatrick Sanan    Notes:
718795452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7188b3d3934dSBarry Smith 
71897db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7190b3d3934dSBarry Smith @*/
71917db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7192b3d3934dSBarry Smith {
7193b3d3934dSBarry Smith   PetscErrorCode       ierr;
71947db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7195b3d3934dSBarry Smith 
7196b3d3934dSBarry Smith   PetscFunctionBegin;
7197b3d3934dSBarry Smith   if (!ctx->max) {
7198b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7199b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7200b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7201b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7202b3d3934dSBarry Smith   } else {
7203b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7204b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7205b3d3934dSBarry Smith   }
7206b3d3934dSBarry Smith   PetscFunctionReturn(0);
7207b3d3934dSBarry Smith }
7208b3d3934dSBarry Smith 
7209b3d3934dSBarry Smith /*@C
7210b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7211b3d3934dSBarry Smith 
7212b3d3934dSBarry Smith    Collective on TS
7213b3d3934dSBarry Smith 
7214b3d3934dSBarry Smith    Input Parameter:
7215b3d3934dSBarry Smith .  ts - the TS context
7216b3d3934dSBarry Smith 
7217b3d3934dSBarry Smith    Output Parameter:
7218b3d3934dSBarry Smith +  max - the maximum values
7219b3d3934dSBarry Smith -  min - the minimum values
7220b3d3934dSBarry Smith 
722195452b02SPatrick Sanan    Notes:
722295452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
72237db568b7SBarry Smith 
7224b3d3934dSBarry Smith    Level: intermediate
7225b3d3934dSBarry Smith 
7226b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7227b3d3934dSBarry Smith @*/
7228b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7229b3d3934dSBarry Smith {
7230b3d3934dSBarry Smith   PetscInt i;
7231b3d3934dSBarry Smith 
7232b3d3934dSBarry Smith   PetscFunctionBegin;
7233b3d3934dSBarry Smith   if (max) *max = NULL;
7234b3d3934dSBarry Smith   if (min) *min = NULL;
7235b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7236b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
72375537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7238b3d3934dSBarry Smith       if (max) *max = ctx->max;
7239b3d3934dSBarry Smith       if (min) *min = ctx->min;
7240b3d3934dSBarry Smith       break;
7241b3d3934dSBarry Smith     }
7242b3d3934dSBarry Smith   }
7243b3d3934dSBarry Smith   PetscFunctionReturn(0);
7244b3d3934dSBarry Smith }
7245b3d3934dSBarry Smith 
7246b3d3934dSBarry Smith /*@C
7247b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7248b3d3934dSBarry Smith 
7249b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7250b3d3934dSBarry Smith 
7251b3d3934dSBarry Smith    Input Parameter:
7252b3d3934dSBarry Smith .  ctx - the monitor context
7253b3d3934dSBarry Smith 
7254b3d3934dSBarry Smith    Level: intermediate
7255b3d3934dSBarry Smith 
72567db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7257b3d3934dSBarry Smith @*/
7258b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7259b3d3934dSBarry Smith {
7260b3d3934dSBarry Smith   PetscErrorCode ierr;
7261b3d3934dSBarry Smith 
7262b3d3934dSBarry Smith   PetscFunctionBegin;
7263b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7264b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7265b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7266b3d3934dSBarry Smith   PetscFunctionReturn(0);
7267b3d3934dSBarry Smith }
7268f2dee214SBarry Smith 
726924655328SShri /*@
7270dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7271dcb233daSLisandro Dalcin 
7272dcb233daSLisandro Dalcin    Collective on TS
7273dcb233daSLisandro Dalcin 
7274dcb233daSLisandro Dalcin    Input Parameter:
7275dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7276dcb233daSLisandro Dalcin 
7277dcb233daSLisandro Dalcin    Level: advanced
7278dcb233daSLisandro Dalcin 
7279dcb233daSLisandro Dalcin    Notes:
7280dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7281dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7282dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7283dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7284dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7285dcb233daSLisandro Dalcin    discontinuous source terms).
7286dcb233daSLisandro Dalcin 
7287dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7288dcb233daSLisandro Dalcin @*/
7289dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7290dcb233daSLisandro Dalcin {
7291dcb233daSLisandro Dalcin   PetscFunctionBegin;
7292dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7293dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7294dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7295dcb233daSLisandro Dalcin }
7296dcb233daSLisandro Dalcin 
7297dcb233daSLisandro Dalcin /*@
729824655328SShri    TSRollBack - Rolls back one time step
729924655328SShri 
730024655328SShri    Collective on TS
730124655328SShri 
730224655328SShri    Input Parameter:
730324655328SShri .  ts - the TS context obtained from TSCreate()
730424655328SShri 
730524655328SShri    Level: advanced
730624655328SShri 
730724655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
730824655328SShri @*/
730924655328SShri PetscErrorCode  TSRollBack(TS ts)
731024655328SShri {
731124655328SShri   PetscErrorCode ierr;
731224655328SShri 
731324655328SShri   PetscFunctionBegin;
731424655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7315b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
731624655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
731724655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
731824655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
731924655328SShri   ts->ptime = ts->ptime_prev;
7320be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
73212808aa04SLisandro Dalcin   ts->steps--;
7322b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
732324655328SShri   PetscFunctionReturn(0);
732424655328SShri }
7325aeb4809dSShri Abhyankar 
7326ff22ae23SHong Zhang /*@
7327ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7328ff22ae23SHong Zhang 
7329ff22ae23SHong Zhang    Input Parameter:
7330ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7331ff22ae23SHong Zhang 
73320429704eSStefano Zampini    Output Parameters:
73330429704eSStefano Zampini +  ns - the number of stages
73340429704eSStefano Zampini -  Y - the current stage vectors
73350429704eSStefano Zampini 
7336ff22ae23SHong Zhang    Level: advanced
7337ff22ae23SHong Zhang 
73380429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
73390429704eSStefano Zampini 
7340ff22ae23SHong Zhang .seealso: TSCreate()
7341ff22ae23SHong Zhang @*/
7342ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7343ff22ae23SHong Zhang {
7344ff22ae23SHong Zhang   PetscErrorCode ierr;
7345ff22ae23SHong Zhang 
7346ff22ae23SHong Zhang   PetscFunctionBegin;
7347ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
73480429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
73490429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
73500429704eSStefano Zampini   if (!ts->ops->getstages) {
73510429704eSStefano Zampini     if (ns) *ns = 0;
73520429704eSStefano Zampini     if (Y) *Y = NULL;
73530429704eSStefano Zampini   } else {
7354ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7355ff22ae23SHong Zhang   }
7356ff22ae23SHong Zhang   PetscFunctionReturn(0);
7357ff22ae23SHong Zhang }
7358ff22ae23SHong Zhang 
7359847ff0e1SMatthew G. Knepley /*@C
7360847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7361847ff0e1SMatthew G. Knepley 
7362847ff0e1SMatthew G. Knepley   Collective on SNES
7363847ff0e1SMatthew G. Knepley 
7364847ff0e1SMatthew G. Knepley   Input Parameters:
7365847ff0e1SMatthew G. Knepley + ts - the TS context
7366847ff0e1SMatthew G. Knepley . t - current timestep
7367847ff0e1SMatthew G. Knepley . U - state vector
7368847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7369847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7370847ff0e1SMatthew G. Knepley - ctx - an optional user context
7371847ff0e1SMatthew G. Knepley 
7372847ff0e1SMatthew G. Knepley   Output Parameters:
7373847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7374847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7375847ff0e1SMatthew G. Knepley 
7376847ff0e1SMatthew G. Knepley   Level: intermediate
7377847ff0e1SMatthew G. Knepley 
7378847ff0e1SMatthew G. Knepley   Notes:
7379847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7380847ff0e1SMatthew G. Knepley 
7381847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7382847ff0e1SMatthew G. Knepley 
7383847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7384847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7385847ff0e1SMatthew G. Knepley 
7386847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7387847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7388847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7389847ff0e1SMatthew G. Knepley 
7390847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7391847ff0e1SMatthew G. Knepley @*/
7392847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7393847ff0e1SMatthew G. Knepley {
7394847ff0e1SMatthew G. Knepley   SNES           snes;
7395847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7396847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7397847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7398847ff0e1SMatthew G. Knepley 
7399847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7400c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7401847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7402847ff0e1SMatthew G. Knepley   if (!color) {
7403847ff0e1SMatthew G. Knepley     DM         dm;
7404847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7405847ff0e1SMatthew G. Knepley 
7406847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7407847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7408847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7409847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7410847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7411847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7412847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7413847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7414847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7415847ff0e1SMatthew G. Knepley     } else {
7416847ff0e1SMatthew G. Knepley       MatColoring mc;
7417847ff0e1SMatthew G. Knepley 
7418847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7419847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7420847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7421847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7422847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7423847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7424847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7425847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7426847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7427847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7428847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7429847ff0e1SMatthew G. Knepley     }
7430847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7431847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7432847ff0e1SMatthew G. Knepley   }
7433847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7434847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7435847ff0e1SMatthew G. Knepley   if (J != B) {
7436847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7437847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7438847ff0e1SMatthew G. Knepley   }
7439847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7440847ff0e1SMatthew G. Knepley }
744193b34091SDebojyoti Ghosh 
7442cb9d8021SPierre Barbier de Reuille /*@
74436bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7444cb9d8021SPierre Barbier de Reuille 
7445cb9d8021SPierre Barbier de Reuille     Input Parameters:
74466bc98fa9SBarry Smith +    ts - the TS context
74476bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
74486bc98fa9SBarry Smith 
74496bc98fa9SBarry Smith     Calling sequence of func:
74506bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
74516bc98fa9SBarry Smith 
74526bc98fa9SBarry Smith +   ts - the TS context
74536bc98fa9SBarry Smith .   time - the current time (of the stage)
74546bc98fa9SBarry Smith .   state - the state to check if it is valid
74556bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7456cb9d8021SPierre Barbier de Reuille 
7457cb9d8021SPierre Barbier de Reuille     Level: intermediate
7458cb9d8021SPierre Barbier de Reuille 
74596bc98fa9SBarry Smith     Notes:
74606bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
74616bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
74626bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
74636bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
74646bc98fa9SBarry Smith 
74656bc98fa9SBarry Smith     Developer Notes:
74666bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
74676bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
74686bc98fa9SBarry Smith 
74696bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7470cb9d8021SPierre Barbier de Reuille @*/
7471cb9d8021SPierre Barbier de Reuille 
7472d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7473cb9d8021SPierre Barbier de Reuille {
7474cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7475cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7476cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7477cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7478cb9d8021SPierre Barbier de Reuille }
7479cb9d8021SPierre Barbier de Reuille 
7480cb9d8021SPierre Barbier de Reuille /*@
74816bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7482cb9d8021SPierre Barbier de Reuille 
7483cb9d8021SPierre Barbier de Reuille     Input Parameters:
74846bc98fa9SBarry Smith +    ts - the TS context
74856bc98fa9SBarry Smith .    stagetime - time of the simulation
74866bc98fa9SBarry Smith -    Y - state vector to check.
7487cb9d8021SPierre Barbier de Reuille 
7488cb9d8021SPierre Barbier de Reuille     Output Parameter:
74896bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7490cb9d8021SPierre Barbier de Reuille 
7491cb9d8021SPierre Barbier de Reuille     Note:
74926bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74936bc98fa9SBarry Smith     to check if the current state is valid.
749496a0c994SBarry Smith 
74956bc98fa9SBarry Smith     Level: developer
74966bc98fa9SBarry Smith 
74976bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7498cb9d8021SPierre Barbier de Reuille @*/
7499d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7500cb9d8021SPierre Barbier de Reuille {
7501cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7502cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7503cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7504cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7505d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7506cb9d8021SPierre Barbier de Reuille   }
7507cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7508cb9d8021SPierre Barbier de Reuille }
75091ceb14c0SBarry Smith 
751093b34091SDebojyoti Ghosh /*@C
7511e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
751293b34091SDebojyoti Ghosh 
7513d083f849SBarry Smith   Collective
751493b34091SDebojyoti Ghosh 
751593b34091SDebojyoti Ghosh   Input Parameter:
751693b34091SDebojyoti Ghosh . tsin    - The input TS
751793b34091SDebojyoti Ghosh 
751893b34091SDebojyoti Ghosh   Output Parameter:
7519e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
752093b34091SDebojyoti Ghosh 
75215eca1a21SEmil Constantinescu   Notes:
75225eca1a21SEmil 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.
75235eca1a21SEmil Constantinescu 
7524928bb9adSStefano 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);
75255eca1a21SEmil Constantinescu 
75265eca1a21SEmil Constantinescu   Level: developer
752793b34091SDebojyoti Ghosh 
7528e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
752993b34091SDebojyoti Ghosh @*/
7530baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
753193b34091SDebojyoti Ghosh {
753293b34091SDebojyoti Ghosh   TS             t;
753393b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7534dc846ba4SSatish Balay   SNES           snes_start;
7535dc846ba4SSatish Balay   DM             dm;
7536dc846ba4SSatish Balay   TSType         type;
753793b34091SDebojyoti Ghosh 
753893b34091SDebojyoti Ghosh   PetscFunctionBegin;
753993b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
754093b34091SDebojyoti Ghosh   *tsout = NULL;
754193b34091SDebojyoti Ghosh 
75427a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
754393b34091SDebojyoti Ghosh 
754493b34091SDebojyoti Ghosh   /* General TS description */
754593b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
754693b34091SDebojyoti Ghosh   t->setupcalled       = 0;
754793b34091SDebojyoti Ghosh   t->ksp_its           = 0;
754893b34091SDebojyoti Ghosh   t->snes_its          = 0;
754993b34091SDebojyoti Ghosh   t->nwork             = 0;
75507d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
755193b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
755293b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
755393b34091SDebojyoti Ghosh 
755434561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
755534561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7556d15a3a53SEmil Constantinescu 
755793b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
755893b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
755993b34091SDebojyoti Ghosh 
756093b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
756151699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
756293b34091SDebojyoti Ghosh 
7563e7069c78SShri   t->trajectory = tsin->trajectory;
7564e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7565e7069c78SShri 
7566e7069c78SShri   t->event = tsin->event;
75676b10a48eSSatish Balay   if (t->event) t->event->refct++;
7568e7069c78SShri 
756993b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
757093b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7571e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
757293b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
757393b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
757493b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
757593b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
757693b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
757793b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
757893b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
757993b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
758093b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
758193b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
758293b34091SDebojyoti Ghosh 
758393b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
758493b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
758593b34091SDebojyoti Ghosh 
758693b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
758793b34091SDebojyoti Ghosh 
758893b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
758993b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
759093b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
759193b34091SDebojyoti Ghosh 
759293b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
759393b34091SDebojyoti Ghosh 
75940d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75950d4fed19SBarry Smith     PetscInt i;
75960d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75970d4fed19SBarry Smith     for (i=0; i<10; i++) {
75980d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75990d4fed19SBarry Smith     }
76000d4fed19SBarry Smith   }
760193b34091SDebojyoti Ghosh   *tsout = t;
760293b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
760393b34091SDebojyoti Ghosh }
7604f3b1f45cSBarry Smith 
7605f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7606f3b1f45cSBarry Smith {
7607f3b1f45cSBarry Smith   PetscErrorCode ierr;
7608f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7609f3b1f45cSBarry Smith 
7610f3b1f45cSBarry Smith   PetscFunctionBegin;
7611f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7612f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7613f3b1f45cSBarry Smith }
7614f3b1f45cSBarry Smith 
7615f3b1f45cSBarry Smith /*@
7616f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7617f3b1f45cSBarry Smith 
7618d083f849SBarry Smith    Logically Collective on TS
7619f3b1f45cSBarry Smith 
7620f3b1f45cSBarry Smith     Input Parameters:
7621f3b1f45cSBarry Smith     TS - the time stepping routine
7622f3b1f45cSBarry Smith 
7623f3b1f45cSBarry Smith    Output Parameter:
7624f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7625f3b1f45cSBarry Smith 
7626f3b1f45cSBarry Smith    Options Database:
7627f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7628f3b1f45cSBarry Smith 
7629f3b1f45cSBarry Smith    Level: advanced
7630f3b1f45cSBarry Smith 
763195452b02SPatrick Sanan    Notes:
763295452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7633f3b1f45cSBarry Smith 
7634f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7635f3b1f45cSBarry Smith @*/
7636f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7637f3b1f45cSBarry Smith {
7638f3b1f45cSBarry Smith   Mat            J,B;
7639f3b1f45cSBarry Smith   PetscErrorCode ierr;
7640f3b1f45cSBarry Smith   TSRHSJacobian  func;
7641f3b1f45cSBarry Smith   void*          ctx;
7642f3b1f45cSBarry Smith 
7643f3b1f45cSBarry Smith   PetscFunctionBegin;
7644f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7645f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7646f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7647f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7648f3b1f45cSBarry Smith }
7649f3b1f45cSBarry Smith 
7650f3b1f45cSBarry Smith /*@C
7651f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7652f3b1f45cSBarry Smith 
7653d083f849SBarry Smith    Logically Collective on TS
7654f3b1f45cSBarry Smith 
7655f3b1f45cSBarry Smith     Input Parameters:
7656f3b1f45cSBarry Smith     TS - the time stepping routine
7657f3b1f45cSBarry Smith 
7658f3b1f45cSBarry Smith    Output Parameter:
7659f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7660f3b1f45cSBarry Smith 
7661f3b1f45cSBarry Smith    Options Database:
7662f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7663f3b1f45cSBarry Smith 
766495452b02SPatrick Sanan    Notes:
766595452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7666f3b1f45cSBarry Smith 
7667f3b1f45cSBarry Smith    Level: advanced
7668f3b1f45cSBarry Smith 
7669f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7670f3b1f45cSBarry Smith @*/
7671f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7672f3b1f45cSBarry Smith {
7673f3b1f45cSBarry Smith   Mat            J,B;
7674f3b1f45cSBarry Smith   PetscErrorCode ierr;
7675f3b1f45cSBarry Smith   void           *ctx;
7676f3b1f45cSBarry Smith   TSRHSJacobian  func;
7677f3b1f45cSBarry Smith 
7678f3b1f45cSBarry Smith   PetscFunctionBegin;
7679f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7680f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7681f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7682f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7683f3b1f45cSBarry Smith }
76840fe4d17eSHong Zhang 
76850fe4d17eSHong Zhang /*@
76860fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
76870fe4d17eSHong Zhang 
76880fe4d17eSHong Zhang   Logically collective
76890fe4d17eSHong Zhang 
76900fe4d17eSHong Zhang   Input Parameter:
76910fe4d17eSHong Zhang +  ts - timestepping context
76920fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76930fe4d17eSHong Zhang 
76940fe4d17eSHong Zhang   Options Database:
76950fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76960fe4d17eSHong Zhang 
76970fe4d17eSHong Zhang   Notes:
76980fe4d17eSHong Zhang     This is only useful for multirate methods
76990fe4d17eSHong Zhang 
77000fe4d17eSHong Zhang   Level: intermediate
77010fe4d17eSHong Zhang 
77020fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
77030fe4d17eSHong Zhang @*/
77040fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
77050fe4d17eSHong Zhang {
77060fe4d17eSHong Zhang   PetscFunctionBegin;
77070fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77080fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
77090fe4d17eSHong Zhang   PetscFunctionReturn(0);
77100fe4d17eSHong Zhang }
77110fe4d17eSHong Zhang 
77120fe4d17eSHong Zhang /*@
77130fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
77140fe4d17eSHong Zhang 
77150fe4d17eSHong Zhang   Not collective
77160fe4d17eSHong Zhang 
77170fe4d17eSHong Zhang   Input Parameter:
77180fe4d17eSHong Zhang .  ts - timestepping context
77190fe4d17eSHong Zhang 
77200fe4d17eSHong Zhang   Output Parameter:
77210fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
77220fe4d17eSHong Zhang 
77230fe4d17eSHong Zhang   Level: intermediate
77240fe4d17eSHong Zhang 
77250fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
77260fe4d17eSHong Zhang @*/
77270fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
77280fe4d17eSHong Zhang {
77290fe4d17eSHong Zhang   PetscFunctionBegin;
77300fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77310fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
77320fe4d17eSHong Zhang   PetscFunctionReturn(0);
77330fe4d17eSHong Zhang }
7734d60b7d5cSBarry Smith 
7735d60b7d5cSBarry Smith /*@
7736d60b7d5cSBarry Smith     TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix.
7737d60b7d5cSBarry Smith 
7738d60b7d5cSBarry Smith    Logically  Collective on ts
7739d60b7d5cSBarry Smith 
7740d60b7d5cSBarry Smith    Input Parameters:
7741d60b7d5cSBarry Smith +  ts - the time-stepper
7742d60b7d5cSBarry Smith -  str - the structure (the default is UNKNOWN_NONZERO_PATTERN)
7743d60b7d5cSBarry Smith 
7744d60b7d5cSBarry Smith    Level: intermediate
7745d60b7d5cSBarry Smith 
7746d60b7d5cSBarry Smith    Notes:
7747d60b7d5cSBarry Smith      When the relationship between the nonzero structures is known and supplied the solution process can be much faster
7748d60b7d5cSBarry Smith 
7749d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure
7750d60b7d5cSBarry Smith  @*/
7751d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str)
7752d60b7d5cSBarry Smith {
7753d60b7d5cSBarry Smith   PetscFunctionBegin;
7754d60b7d5cSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7755d60b7d5cSBarry Smith   ts->axpy_pattern = str;
7756d60b7d5cSBarry Smith   PetscFunctionReturn(0);
7757d60b7d5cSBarry Smith }
7758