xref: /petsc/src/ts/interface/ts.c (revision 6bd3a4fd3ada34738ab3c62a7d3b0fa06ce8b47c)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h"  I*/
21e25c274SJed Brown #include <petscdmda.h>
360e16b1bSMatthew G. Knepley #include <petscdmshell.h>
460e16b1bSMatthew G. Knepley #include <petscdmplex.h>  // For TSSetFromOptions()
560e16b1bSMatthew G. Knepley #include <petscdmswarm.h> // For TSSetFromOptions()
62d5ee99bSBarry Smith #include <petscviewer.h>
72d5ee99bSBarry Smith #include <petscdraw.h>
8900f6b5bSMatthew G. Knepley #include <petscconvest.h>
9d763cef2SBarry Smith 
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 
16d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt, TSAdaptType default_type)
17d71ae5a4SJacob Faibussowitsch {
182ffb9264SLisandro Dalcin   PetscFunctionBegin;
19b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt, TSADAPT_CLASSID, 1);
20b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type, 2);
211e66621cSBarry Smith   if (!((PetscObject)adapt)->type_name) PetscCall(TSAdaptSetType(adapt, default_type));
223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
232ffb9264SLisandro Dalcin }
242ffb9264SLisandro Dalcin 
25bdad233fSMatthew Knepley /*@
26195e9b02SBarry Smith   TSSetFromOptions - Sets various `TS` parameters from the options database
27bdad233fSMatthew Knepley 
28c3339decSBarry Smith   Collective
29bdad233fSMatthew Knepley 
30bdad233fSMatthew Knepley   Input Parameter:
31bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
32bdad233fSMatthew Knepley 
33bdad233fSMatthew Knepley   Options Database Keys:
34195e9b02SBarry Smith + -ts_type <type>                                                    - EULER, BEULER, SUNDIALS, PSEUDO, CN, RK, THETA, ALPHA, GLLE,  SSP, GLEE, BSYMP, IRK, see `TSType`
35ef222394SBarry Smith . -ts_save_trajectory                                                - checkpoint the solution at each time-step
36ef85077eSLisandro Dalcin . -ts_max_time <time>                                                - maximum time to compute to
374a658b32SHong Zhang . -ts_time_span <t0,...tf>                                           - sets the time span, solutions are computed and stored for each indicated time
38ef85077eSLisandro Dalcin . -ts_max_steps <steps>                                              - maximum number of time-steps to take
39ef85077eSLisandro Dalcin . -ts_init_time <time>                                               - initial time to start computation
40195e9b02SBarry Smith . -ts_final_time <time>                                              - final time to compute to (deprecated: use `-ts_max_time`)
413e4cdcaaSBarry Smith . -ts_dt <dt>                                                        - initial time step
421628793fSSatish Balay . -ts_exact_final_time <stepover,interpolate,matchstep>              - whether to stop at the exact given final time and how to compute the solution at that time
43a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures>                                - Maximum number of nonlinear solve failures allowed
44a3cdaa26SBarry Smith . -ts_max_reject <maxrejects>                                        - Maximum number of step rejections before step fails
45a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false>                               - Error if no step succeeds
46a3cdaa26SBarry Smith . -ts_rtol <rtol>                                                    - relative tolerance for local truncation error
4767b8a455SSatish Balay . -ts_atol <atol>                                                    - Absolute tolerance for local truncation error
48f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view               - test the Jacobian at each iteration against finite difference with RHS function
49b43aa488SJacob Faibussowitsch . -ts_rhs_jacobian_test_mult_transpose                               - test the Jacobian at each iteration against finite difference with RHS function
50195e9b02SBarry Smith . -ts_adjoint_solve <yes,no>                                         - After solving the ODE/DAE solve the adjoint problem (requires `-ts_save_trajectory`)
51847ff0e1SMatthew G. Knepley . -ts_fd_color                                                       - Use finite differences with coloring to compute IJacobian
52bdad233fSMatthew Knepley . -ts_monitor                                                        - print information at each timestep
53aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel                                                 - Cancel all monitors
54de06c3feSJed Brown . -ts_monitor_lg_solution                                            - Monitor solution graphically
55de06c3feSJed Brown . -ts_monitor_lg_error                                               - Monitor error graphically
567cf37e64SBarry Smith . -ts_monitor_error                                                  - Monitors norm of error
576934998bSLisandro Dalcin . -ts_monitor_lg_timestep                                            - Monitor timestep size graphically
588b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log                                        - Monitor log timestep size graphically
59de06c3feSJed Brown . -ts_monitor_lg_snes_iterations                                     - Monitor number nonlinear iterations for each timestep graphically
60de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations                                      - Monitor number nonlinear iterations for each timestep graphically
61de06c3feSJed Brown . -ts_monitor_sp_eig                                                 - Monitor eigenvalues of linearized operator graphically
62de06c3feSJed Brown . -ts_monitor_draw_solution                                          - Monitor solution graphically
633e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright>       - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
643e4cdcaaSBarry Smith . -ts_monitor_draw_error                                             - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
65fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
66b43aa488SJacob Faibussowitsch . -ts_monitor_solution_interval <interval>                           - output once every interval (default=1) time steps
6763a3b9bcSJacob Faibussowitsch . -ts_monitor_solution_vtk <filename.vts,filename.vtu>               - Save each time step to a binary file, use filename-%%03" PetscInt_FMT ".vts (filename-%%03" PetscInt_FMT ".vtu)
689e336e28SPatrick Sanan - -ts_monitor_envelope                                               - determine maximum and minimum value of each component of the solution over the solution time
6953ea634cSHong Zhang 
70bcf0153eSBarry Smith   Level: beginner
713d5a8a6aSBarry Smith 
72bcf0153eSBarry Smith   Notes:
73bcf0153eSBarry Smith   See `SNESSetFromOptions()` and `KSPSetFromOptions()` for how to control the nonlinear and linear solves used by the time-stepper.
74bcf0153eSBarry Smith 
75195e9b02SBarry Smith   Certain `SNES` options get reset for each new nonlinear solver, for example `-snes_lag_jacobian its` and `-snes_lag_preconditioner its`, in order
76195e9b02SBarry Smith   to retain them over the multiple nonlinear solves that `TS` uses you mush also provide `-snes_lag_jacobian_persists true` and
77195e9b02SBarry Smith   `-snes_lag_preconditioner_persists true`
783d5a8a6aSBarry Smith 
79b43aa488SJacob Faibussowitsch   Developer Notes:
809e336e28SPatrick Sanan   We should unify all the -ts_monitor options in the way that -xxx_view has been unified
81bdad233fSMatthew Knepley 
821cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetType()`
83bdad233fSMatthew Knepley @*/
84d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFromOptions(TS ts)
85d71ae5a4SJacob Faibussowitsch {
86bc952696SBarry Smith   PetscBool              opt, flg, tflg;
87eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
884a658b32SHong Zhang   PetscReal              time_step, tspan[100];
894a658b32SHong Zhang   PetscInt               nt = PETSC_STATIC_ARRAY_LENGTH(tspan);
9049354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
91d1212d36SBarry Smith   char                   dir[16];
92cd11d68dSLisandro Dalcin   TSIFunction            ifun;
936991f827SBarry Smith   const char            *defaultType;
946991f827SBarry Smith   char                   typeName[256];
95bdad233fSMatthew Knepley 
96bdad233fSMatthew Knepley   PetscFunctionBegin;
970700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
986991f827SBarry Smith 
999566063dSJacob Faibussowitsch   PetscCall(TSRegisterAll());
1009566063dSJacob Faibussowitsch   PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL));
101cd11d68dSLisandro Dalcin 
102d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)ts);
1031ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1041ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-ts_type", "TS method", "TSSetType", TSList, defaultType, typeName, 256, &opt));
1061e66621cSBarry Smith   if (opt) PetscCall(TSSetType(ts, typeName));
1071e66621cSBarry Smith   else PetscCall(TSSetType(ts, defaultType));
108bdad233fSMatthew Knepley 
109bdad233fSMatthew Knepley   /* Handle generic TS options */
1109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsDeprecated("-ts_final_time", "-ts_max_time", "3.10", NULL));
1119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_max_time", "Maximum time to run to", "TSSetMaxTime", ts->max_time, &ts->max_time, NULL));
1124a658b32SHong Zhang   PetscCall(PetscOptionsRealArray("-ts_time_span", "Time span", "TSSetTimeSpan", tspan, &nt, &flg));
1134a658b32SHong Zhang   if (flg) PetscCall(TSSetTimeSpan(ts, nt, tspan));
1149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_max_steps", "Maximum number of time steps", "TSSetMaxSteps", ts->max_steps, &ts->max_steps, NULL));
1159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_init_time", "Initial time", "TSSetTime", ts->ptime, &ts->ptime, NULL));
1169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_dt", "Initial time step", "TSSetTimeStep", ts->time_step, &time_step, &flg));
1179566063dSJacob Faibussowitsch   if (flg) PetscCall(TSSetTimeStep(ts, time_step));
1189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-ts_exact_final_time", "Option for handling of final time step", "TSSetExactFinalTime", TSExactFinalTimeOptions, (PetscEnum)ts->exact_final_time, (PetscEnum *)&eftopt, &flg));
1199566063dSJacob Faibussowitsch   if (flg) PetscCall(TSSetExactFinalTime(ts, eftopt));
1209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_max_snes_failures", "Maximum number of nonlinear solve failures", "TSSetMaxSNESFailures", ts->max_snes_failures, &ts->max_snes_failures, NULL));
1219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_max_reject", "Maximum number of step rejections before step fails", "TSSetMaxStepRejections", ts->max_reject, &ts->max_reject, NULL));
1229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_error_if_step_fails", "Error if no step succeeds", "TSSetErrorIfStepFails", ts->errorifstepfailed, &ts->errorifstepfailed, NULL));
1239566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_rtol", "Relative tolerance for local truncation error", "TSSetTolerances", ts->rtol, &ts->rtol, NULL));
1249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_atol", "Absolute tolerance for local truncation error", "TSSetTolerances", ts->atol, &ts->atol, NULL));
125bdad233fSMatthew Knepley 
1269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_rhs_jacobian_test_mult", "Test the RHS Jacobian for consistency with RHS at each solve ", "None", ts->testjacobian, &ts->testjacobian, NULL));
1279566063dSJacob Faibussowitsch   PetscCall(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));
1289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction", "Use the split RHS function for multirate solvers ", "TSSetUseSplitRHSFunction", ts->use_splitrhsfunction, &ts->use_splitrhsfunction, NULL));
12956f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
13056f85f32SBarry Smith   {
13156f85f32SBarry Smith     PetscBool set;
13256f85f32SBarry Smith     flg = PETSC_FALSE;
1339566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-ts_saws_block", "Block for SAWs memory snooper at end of TSSolve", "PetscObjectSAWsBlock", ((PetscObject)ts)->amspublishblock, &flg, &set));
1341baa6e33SBarry Smith     if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts, flg));
13556f85f32SBarry Smith   }
13656f85f32SBarry Smith #endif
13756f85f32SBarry Smith 
138bdad233fSMatthew Knepley   /* Monitor options */
1399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL));
1409566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor", "Monitor time and timestep size", "TSMonitorDefault", TSMonitorDefault, NULL));
1419566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_extreme", "Monitor extreme values of the solution", "TSMonitorExtreme", TSMonitorExtreme, NULL));
1429566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_solution", "View the solution at each timestep", "TSMonitorSolution", TSMonitorSolution, NULL));
1439566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts, "-ts_dmswarm_monitor_moments", "Monitor moments of particle distribution", "TSDMSwarmMonitorMoments", TSDMSwarmMonitorMoments, NULL));
144fde5950dSBarry Smith 
1459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_python", "Use Python function", "TSMonitorSet", NULL, monfilename, sizeof(monfilename), &flg));
1469566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts, monfilename));
1475180491cSLisandro Dalcin 
1489566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", &opt));
149b3603a34SBarry Smith   if (opt) {
1503923b477SBarry Smith     PetscInt  howoften = 1;
151e669de00SBarry Smith     DM        dm;
152e669de00SBarry Smith     PetscBool net;
153b3603a34SBarry Smith 
1549566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", howoften, &howoften, NULL));
1559566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &dm));
1569566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMNETWORK, &net));
157e669de00SBarry Smith     if (net) {
158e669de00SBarry Smith       TSMonitorLGCtxNetwork ctx;
1599566063dSJacob Faibussowitsch       PetscCall(TSMonitorLGCtxNetworkCreate(ts, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &ctx));
1609566063dSJacob Faibussowitsch       PetscCall(TSMonitorSet(ts, TSMonitorLGCtxNetworkSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxNetworkDestroy));
1619566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy", "Plot the solution with a semi-log axis", "", ctx->semilogy, &ctx->semilogy, NULL));
162e669de00SBarry Smith     } else {
163e669de00SBarry Smith       TSMonitorLGCtx ctx;
1649566063dSJacob Faibussowitsch       PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx));
1659566063dSJacob Faibussowitsch       PetscCall(TSMonitorSet(ts, TSMonitorLGSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy));
166bdad233fSMatthew Knepley     }
167e669de00SBarry Smith   }
1686ba87a44SLisandro Dalcin 
1699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", &opt));
170ef20d060SBarry Smith   if (opt) {
1710b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1723923b477SBarry Smith     PetscInt       howoften = 1;
173ef20d060SBarry Smith 
1749566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", howoften, &howoften, NULL));
1759566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx));
1769566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGError, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy));
177ef20d060SBarry Smith   }
1789566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_error", "View the error at each timestep", "TSMonitorError", TSMonitorError, NULL));
1797cf37e64SBarry Smith 
1809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", &opt));
1816934998bSLisandro Dalcin   if (opt) {
1826934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
1836934998bSLisandro Dalcin     PetscInt       howoften = 1;
1846934998bSLisandro Dalcin 
1859566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL));
1869566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx));
1879566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy));
1886934998bSLisandro Dalcin   }
1899566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", &opt));
1908b668821SLisandro Dalcin   if (opt) {
1918b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
1928b668821SLisandro Dalcin     PetscInt       howoften = 1;
1938b668821SLisandro Dalcin 
1949566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL));
1959566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx));
1969566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy));
1978b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
1988b668821SLisandro Dalcin   }
1998b668821SLisandro Dalcin 
2009566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", &opt));
201201da799SBarry Smith   if (opt) {
202201da799SBarry Smith     TSMonitorLGCtx ctx;
203201da799SBarry Smith     PetscInt       howoften = 1;
204201da799SBarry Smith 
2059566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", howoften, &howoften, NULL));
2069566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx));
2079566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGSNESIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy));
208201da799SBarry Smith   }
2099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", &opt));
210201da799SBarry Smith   if (opt) {
211201da799SBarry Smith     TSMonitorLGCtx ctx;
212201da799SBarry Smith     PetscInt       howoften = 1;
213201da799SBarry Smith 
2149566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", howoften, &howoften, NULL));
2159566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx));
2169566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGKSPIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy));
217201da799SBarry Smith   }
2189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", &opt));
2198189c53fSBarry Smith   if (opt) {
2208189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2218189c53fSBarry Smith     PetscInt          howoften = 1;
2228189c53fSBarry Smith 
2239566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", howoften, &howoften, NULL));
2249566063dSJacob Faibussowitsch     PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx));
2259566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorSPEig, ctx, (PetscErrorCode(*)(void **))TSMonitorSPEigCtxDestroy));
2268189c53fSBarry Smith   }
22760e16b1bSMatthew G. Knepley   PetscCall(PetscOptionsName("-ts_monitor_sp_swarm", "Display particle phase space from the DMSwarm", "TSMonitorSPSwarm", &opt));
2281b575b74SJoseph Pusztay   if (opt) {
2291b575b74SJoseph Pusztay     TSMonitorSPCtx ctx;
230d7462660SMatthew Knepley     PetscInt       howoften = 1, retain = 0;
23160e16b1bSMatthew G. Knepley     PetscBool      phase = PETSC_TRUE, create = PETSC_TRUE, multispecies = PETSC_FALSE;
232d7462660SMatthew Knepley 
2339371c9d4SSatish Balay     for (PetscInt i = 0; i < ts->numbermonitors; ++i)
2349371c9d4SSatish Balay       if (ts->monitor[i] == TSMonitorSPSwarmSolution) {
2359371c9d4SSatish Balay         create = PETSC_FALSE;
2369371c9d4SSatish Balay         break;
2379371c9d4SSatish Balay       }
2386a5217c0SMatthew G. Knepley     if (create) {
23960e16b1bSMatthew G. Knepley       PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm", "Display particles phase space from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL));
2409566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL));
2419566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL));
24260e16b1bSMatthew G. Knepley       PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_multi_species", "Color particles by particle species", "TSMonitorSPSwarm", multispecies, &multispecies, NULL));
24360e16b1bSMatthew G. Knepley       PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, multispecies, &ctx));
2449566063dSJacob Faibussowitsch       PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorSPCtxDestroy));
2451b575b74SJoseph Pusztay     }
2466a5217c0SMatthew G. Knepley   }
24760e16b1bSMatthew G. Knepley   PetscCall(PetscOptionsName("-ts_monitor_hg_swarm", "Display particle histogram from the DMSwarm", "TSMonitorHGSwarm", &opt));
24860e16b1bSMatthew G. Knepley   if (opt) {
24960e16b1bSMatthew G. Knepley     TSMonitorHGCtx ctx;
25060e16b1bSMatthew G. Knepley     PetscInt       howoften = 1, Ns = 1;
25160e16b1bSMatthew G. Knepley     PetscBool      velocity = PETSC_FALSE, create = PETSC_TRUE;
25260e16b1bSMatthew G. Knepley 
25360e16b1bSMatthew G. Knepley     for (PetscInt i = 0; i < ts->numbermonitors; ++i)
25460e16b1bSMatthew G. Knepley       if (ts->monitor[i] == TSMonitorHGSwarmSolution) {
25560e16b1bSMatthew G. Knepley         create = PETSC_FALSE;
25660e16b1bSMatthew G. Knepley         break;
25760e16b1bSMatthew G. Knepley       }
25860e16b1bSMatthew G. Knepley     if (create) {
25960e16b1bSMatthew G. Knepley       DM       sw, dm;
26060e16b1bSMatthew G. Knepley       PetscInt Nc, Nb;
26160e16b1bSMatthew G. Knepley 
26260e16b1bSMatthew G. Knepley       PetscCall(TSGetDM(ts, &sw));
26360e16b1bSMatthew G. Knepley       PetscCall(DMSwarmGetCellDM(sw, &dm));
26460e16b1bSMatthew G. Knepley       PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &Nc));
26560e16b1bSMatthew G. Knepley       Nb = PetscMin(20, PetscMax(10, Nc));
26660e16b1bSMatthew G. Knepley       PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm", "Display particles histogram from the DMSwarm", "TSMonitorHGSwarm", howoften, &howoften, NULL));
26760e16b1bSMatthew G. Knepley       PetscCall(PetscOptionsBool("-ts_monitor_hg_swarm_velocity", "Plot in velocity space rather than coordinate space", "TSMonitorHGSwarm", velocity, &velocity, NULL));
26860e16b1bSMatthew G. Knepley       PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_species", "Number of species to histogram", "TSMonitorHGSwarm", Ns, &Ns, NULL));
26960e16b1bSMatthew G. Knepley       PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_bins", "Number of histogram bins", "TSMonitorHGSwarm", Nb, &Nb, NULL));
27060e16b1bSMatthew G. Knepley       PetscCall(TSMonitorHGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, Ns, Nb, velocity, &ctx));
27160e16b1bSMatthew G. Knepley       PetscCall(TSMonitorSet(ts, TSMonitorHGSwarmSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorHGCtxDestroy));
27260e16b1bSMatthew G. Knepley     }
27360e16b1bSMatthew G. Knepley   }
274ef20d060SBarry Smith   opt = PETSC_FALSE;
2759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", &opt));
276a7cc72afSBarry Smith   if (opt) {
27783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
27883a4ac43SBarry Smith     PetscInt         howoften = 1;
279a80ad3e0SBarry Smith 
2809566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", howoften, &howoften, NULL));
2819566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Computed Solution", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx));
2829566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorDrawSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy));
283bdad233fSMatthew Knepley   }
284fb1732b5SBarry Smith   opt = PETSC_FALSE;
2859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", &opt));
2862d5ee99bSBarry Smith   if (opt) {
2872d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2882d5ee99bSBarry Smith     PetscReal        bounds[4];
2892d5ee99bSBarry Smith     PetscInt         n = 4;
2902d5ee99bSBarry Smith     PetscDraw        draw;
2916934998bSLisandro Dalcin     PetscDrawAxis    axis;
2922d5ee99bSBarry Smith 
2939566063dSJacob Faibussowitsch     PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", bounds, &n, NULL));
2943c633725SBarry Smith     PetscCheck(n == 4, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Must provide bounding box of phase field");
2959566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, 1, &ctx));
2969566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(ctx->viewer, 0, &draw));
2979566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer, 0, &axis));
2989566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisSetLimits(axis, bounds[0], bounds[2], bounds[1], bounds[3]));
2999566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisSetLabels(axis, "Phase Diagram", "Variable 1", "Variable 2"));
3009566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionPhase, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy));
3012d5ee99bSBarry Smith   }
3022d5ee99bSBarry Smith   opt = PETSC_FALSE;
3039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", &opt));
3043a471f94SBarry Smith   if (opt) {
30583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
30683a4ac43SBarry Smith     PetscInt         howoften = 1;
3073a471f94SBarry Smith 
3089566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", howoften, &howoften, NULL));
3099566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Error", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx));
3109566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorDrawError, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy));
3113a471f94SBarry Smith   }
3120ed3bfb6SBarry Smith   opt = PETSC_FALSE;
3139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", &opt));
3140ed3bfb6SBarry Smith   if (opt) {
3150ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3160ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3170ed3bfb6SBarry Smith 
3189566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", howoften, &howoften, NULL));
3199566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Solution provided by user function", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx));
3209566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionFunction, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy));
3210ed3bfb6SBarry Smith   }
322fde5950dSBarry Smith 
323ed81e22dSJed Brown   opt = PETSC_FALSE;
32463a3b9bcSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_solution_vtk", "Save each time step to a binary file, use filename-%%03" PetscInt_FMT ".vts", "TSMonitorSolutionVTK", NULL, monfilename, sizeof(monfilename), &flg));
325ed81e22dSJed Brown   if (flg) {
326bbcf679cSJacob Faibussowitsch     const char *ptr = NULL, *ptr2 = NULL;
327ed81e22dSJed Brown     char       *filetemplate;
32863a3b9bcSJacob Faibussowitsch     PetscCheck(monfilename[0], PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts");
329ed81e22dSJed Brown     /* Do some cursory validation of the input. */
3309566063dSJacob Faibussowitsch     PetscCall(PetscStrstr(monfilename, "%", (char **)&ptr));
33163a3b9bcSJacob Faibussowitsch     PetscCheck(ptr, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts");
332ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
3339566063dSJacob Faibussowitsch       PetscCall(PetscStrchr("DdiouxX", *ptr, (char **)&ptr2));
33463a3b9bcSJacob Faibussowitsch       PetscCheck(ptr2 || (*ptr >= '0' && *ptr <= '9'), PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03" PetscInt_FMT ".vts");
335ed81e22dSJed Brown       if (ptr2) break;
336ed81e22dSJed Brown     }
3379566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(monfilename, &filetemplate));
3389566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorSolutionVTK, filetemplate, (PetscErrorCode(*)(void **))TSMonitorSolutionVTKDestroy));
339ed81e22dSJed Brown   }
340bdad233fSMatthew Knepley 
3419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_dmda_ray", "Display a ray of the solution", "None", "y=0", dir, sizeof(dir), &flg));
342d1212d36SBarry Smith   if (flg) {
343d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
344d1212d36SBarry Smith     int                  ray = 0;
3453ee9839eSMatthew G. Knepley     DMDirection          ddir;
346d1212d36SBarry Smith     DM                   da;
347d1212d36SBarry Smith     PetscMPIInt          rank;
348d1212d36SBarry Smith 
3493c633725SBarry Smith     PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir);
3503ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3513ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
35298921bdaSJacob Faibussowitsch     else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir);
353d1212d36SBarry Smith     sscanf(dir + 2, "%d", &ray);
354d1212d36SBarry Smith 
3559566063dSJacob Faibussowitsch     PetscCall(PetscInfo(((PetscObject)ts), "Displaying DMDA ray %c = %d\n", dir[0], ray));
3569566063dSJacob Faibussowitsch     PetscCall(PetscNew(&rayctx));
3579566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &da));
3589566063dSJacob Faibussowitsch     PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter));
3599566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts), &rank));
3601e66621cSBarry Smith     if (rank == 0) PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF, NULL, NULL, 0, 0, 600, 300, &rayctx->viewer));
36151b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
3629566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorDMDARay, rayctx, TSMonitorDMDARayDestroy));
363d1212d36SBarry Smith   }
3649566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray", "Display a ray of the solution", "None", "x=0", dir, sizeof(dir), &flg));
36551b4a12fSMatthew G. Knepley   if (flg) {
36651b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
36751b4a12fSMatthew G. Knepley     int                  ray = 0;
3683ee9839eSMatthew G. Knepley     DMDirection          ddir;
36951b4a12fSMatthew G. Knepley     DM                   da;
37051b4a12fSMatthew G. Knepley     PetscInt             howoften = 1;
371d1212d36SBarry Smith 
3723c633725SBarry Smith     PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3733ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3743ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
37598921bdaSJacob Faibussowitsch     else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
37651b4a12fSMatthew G. Knepley     sscanf(dir + 2, "%d", &ray);
3771c3436cfSJed Brown 
3789566063dSJacob Faibussowitsch     PetscCall(PetscInfo(((PetscObject)ts), "Displaying LG DMDA ray %c = %d\n", dir[0], ray));
3799566063dSJacob Faibussowitsch     PetscCall(PetscNew(&rayctx));
3809566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &da));
3819566063dSJacob Faibussowitsch     PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter));
3829566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &rayctx->lgctx));
3839566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy));
38451b4a12fSMatthew G. Knepley   }
385a7a1495cSBarry Smith 
3869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_envelope", "Monitor maximum and minimum value of each component of the solution", "TSMonitorEnvelope", &opt));
387b3d3934dSBarry Smith   if (opt) {
388b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
389b3d3934dSBarry Smith 
3909566063dSJacob Faibussowitsch     PetscCall(TSMonitorEnvelopeCtxCreate(ts, &ctx));
3919566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorEnvelope, ctx, (PetscErrorCode(*)(void **))TSMonitorEnvelopeCtxDestroy));
392b3d3934dSBarry Smith   }
393aee7a9fbSMatthew G. Knepley   flg = PETSC_FALSE;
3949566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_monitor_cancel", "Remove all monitors", "TSMonitorCancel", flg, &flg, &opt));
3959566063dSJacob Faibussowitsch   if (opt && flg) PetscCall(TSMonitorCancel(ts));
396b3d3934dSBarry Smith 
397847ff0e1SMatthew G. Knepley   flg = PETSC_FALSE;
398d7cfae9bSHong Zhang   PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeIJacobianDefaultColor", flg, &flg, NULL));
399847ff0e1SMatthew G. Knepley   if (flg) {
400847ff0e1SMatthew G. Knepley     DM dm;
401847ff0e1SMatthew G. Knepley 
4029371c9d4SSatish Balay     PetscCall(TSGetDM(ts, &dm));
4039371c9d4SSatish Balay     PetscCall(DMTSUnsetIJacobianContext_Internal(dm));
4049566063dSJacob Faibussowitsch     PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL));
4059566063dSJacob Faibussowitsch     PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n"));
406847ff0e1SMatthew G. Knepley   }
407847ff0e1SMatthew G. Knepley 
408d763cef2SBarry Smith   /* Handle specific TS options */
409dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, setfromoptions, PetscOptionsObject);
410fbc52257SHong Zhang 
411a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
4129566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts, &ts->adapt));
4139566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type));
414dbbe0bcdSBarry Smith   PetscCall(TSAdaptSetFromOptions(ts->adapt, PetscOptionsObject));
415a7bdc993SLisandro Dalcin 
41668bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4174f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
4189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_save_trajectory", "Save the solution at each timestep", "TSSetSaveTrajectory", tflg, &tflg, NULL));
4191baa6e33SBarry Smith   if (tflg) PetscCall(TSSetSaveTrajectory(ts));
420a05bf03eSHong Zhang 
421dbbe0bcdSBarry Smith   PetscCall(TSAdjointSetFromOptions(ts, PetscOptionsObject));
422d763cef2SBarry Smith 
423d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
424dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)ts, PetscOptionsObject));
425d0609cedSBarry Smith   PetscOptionsEnd();
426d763cef2SBarry Smith 
4271baa6e33SBarry Smith   if (ts->trajectory) PetscCall(TSTrajectorySetFromOptions(ts->trajectory, ts));
42868bece0bSHong Zhang 
4291ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4309566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &ts->snes));
4311ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4329566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &flg, SNESKSPONLY, SNESKSPTRANSPOSEONLY, ""));
4339566063dSJacob Faibussowitsch     if (!flg) PetscCall(SNESSetType(ts->snes, SNESKSPONLY));
4341ef27442SStefano Zampini   }
4359566063dSJacob Faibussowitsch   PetscCall(SNESSetFromOptions(ts->snes));
4363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
437d763cef2SBarry Smith }
438d763cef2SBarry Smith 
439d2daff3dSHong Zhang /*@
440bcf0153eSBarry Smith   TSGetTrajectory - Gets the trajectory from a `TS` if it exists
44178fbdcc8SBarry Smith 
442c3339decSBarry Smith   Collective
44378fbdcc8SBarry Smith 
4442fe279fdSBarry Smith   Input Parameter:
445bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
44678fbdcc8SBarry Smith 
4472fe279fdSBarry Smith   Output Parameter:
448bcf0153eSBarry Smith . tr - the `TSTrajectory` object, if it exists
44978fbdcc8SBarry Smith 
45078fbdcc8SBarry Smith   Level: advanced
45178fbdcc8SBarry Smith 
452bcf0153eSBarry Smith   Note:
453bcf0153eSBarry Smith   This routine should be called after all `TS` options have been set
45478fbdcc8SBarry Smith 
455b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSAdjointSolve()`, `TSTrajectoryCreate()`
45678fbdcc8SBarry Smith @*/
457d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTrajectory(TS ts, TSTrajectory *tr)
458d71ae5a4SJacob Faibussowitsch {
45978fbdcc8SBarry Smith   PetscFunctionBegin;
46078fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
46178fbdcc8SBarry Smith   *tr = ts->trajectory;
4623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46378fbdcc8SBarry Smith }
46478fbdcc8SBarry Smith 
46578fbdcc8SBarry Smith /*@
466bcf0153eSBarry Smith   TSSetSaveTrajectory - Causes the `TS` to save its solutions as it iterates forward in time in a `TSTrajectory` object
467d2daff3dSHong Zhang 
468c3339decSBarry Smith   Collective
469d2daff3dSHong Zhang 
470f899ff85SJose E. Roman   Input Parameter:
471bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
472bc952696SBarry Smith 
473bcf0153eSBarry Smith   Options Database Keys:
47478fbdcc8SBarry Smith + -ts_save_trajectory      - saves the trajectory to a file
47567b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type
47678fbdcc8SBarry Smith 
477d2daff3dSHong Zhang   Level: intermediate
478d2daff3dSHong Zhang 
479bcf0153eSBarry Smith   Notes:
480bcf0153eSBarry Smith   This routine should be called after all `TS` options have been set
481d2daff3dSHong Zhang 
482bcf0153eSBarry Smith   The `TSTRAJECTORYVISUALIZATION` files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
483bcf0153eSBarry Smith   MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
484bcf0153eSBarry Smith 
4851cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()`
486d2daff3dSHong Zhang @*/
487d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSaveTrajectory(TS ts)
488d71ae5a4SJacob Faibussowitsch {
489d2daff3dSHong Zhang   PetscFunctionBegin;
490d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
49163a3b9bcSJacob Faibussowitsch   if (!ts->trajectory) PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory));
4923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
493d2daff3dSHong Zhang }
494d2daff3dSHong Zhang 
495a7a1495cSBarry Smith /*@
496bcf0153eSBarry Smith   TSResetTrajectory - Destroys and recreates the internal `TSTrajectory` object
4972d29f1f2SStefano Zampini 
498c3339decSBarry Smith   Collective
4992d29f1f2SStefano Zampini 
5002fe279fdSBarry Smith   Input Parameter:
501bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
5022d29f1f2SStefano Zampini 
5032d29f1f2SStefano Zampini   Level: intermediate
5042d29f1f2SStefano Zampini 
5051cc06b55SBarry Smith .seealso: [](ch_ts), `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()`, `TSRemoveTrajectory()`
5062d29f1f2SStefano Zampini @*/
507d71ae5a4SJacob Faibussowitsch PetscErrorCode TSResetTrajectory(TS ts)
508d71ae5a4SJacob Faibussowitsch {
5092d29f1f2SStefano Zampini   PetscFunctionBegin;
5102d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5112d29f1f2SStefano Zampini   if (ts->trajectory) {
5129566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryDestroy(&ts->trajectory));
5139566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory));
5142d29f1f2SStefano Zampini   }
5153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5162d29f1f2SStefano Zampini }
5172d29f1f2SStefano Zampini 
5182d29f1f2SStefano Zampini /*@
519195e9b02SBarry Smith   TSRemoveTrajectory - Destroys and removes the internal `TSTrajectory` object from a `TS`
52067a3cfb0SHong Zhang 
521c3339decSBarry Smith   Collective
52267a3cfb0SHong Zhang 
5232fe279fdSBarry Smith   Input Parameter:
524bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
52567a3cfb0SHong Zhang 
52667a3cfb0SHong Zhang   Level: intermediate
52767a3cfb0SHong Zhang 
5281cc06b55SBarry Smith .seealso: [](ch_ts), `TSTrajectory`, `TSResetTrajectory()`, `TSAdjointSolve()`
52967a3cfb0SHong Zhang @*/
530d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRemoveTrajectory(TS ts)
531d71ae5a4SJacob Faibussowitsch {
53267a3cfb0SHong Zhang   PetscFunctionBegin;
53367a3cfb0SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5341e66621cSBarry Smith   if (ts->trajectory) PetscCall(TSTrajectoryDestroy(&ts->trajectory));
5353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
53667a3cfb0SHong Zhang }
53767a3cfb0SHong Zhang 
53867a3cfb0SHong Zhang /*@
539a7a1495cSBarry Smith   TSComputeRHSJacobian - Computes the Jacobian matrix that has been
540bcf0153eSBarry Smith   set with `TSSetRHSJacobian()`.
541a7a1495cSBarry Smith 
542c3339decSBarry Smith   Collective
543a7a1495cSBarry Smith 
544a7a1495cSBarry Smith   Input Parameters:
545bcf0153eSBarry Smith + ts - the `TS` context
546a7a1495cSBarry Smith . t  - current timestep
5470910c330SBarry Smith - U  - input vector
548a7a1495cSBarry Smith 
549a7a1495cSBarry Smith   Output Parameters:
550a7a1495cSBarry Smith + A - Jacobian matrix
5516b867d5aSJose E. Roman - B - optional preconditioning matrix
552a7a1495cSBarry Smith 
553bcf0153eSBarry Smith   Level: developer
554bcf0153eSBarry Smith 
555bcf0153eSBarry Smith   Note:
556a7a1495cSBarry Smith   Most users should not need to explicitly call this routine, as it
557a7a1495cSBarry Smith   is used internally within the nonlinear solvers.
558a7a1495cSBarry Smith 
5591cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `KSPSetOperators()`
560a7a1495cSBarry Smith @*/
561d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobian(TS ts, PetscReal t, Vec U, Mat A, Mat B)
562d71ae5a4SJacob Faibussowitsch {
563270bf2e7SJed Brown   PetscObjectState Ustate;
5646c1e1eecSBarry Smith   PetscObjectId    Uid;
56524989b8cSPeter Brune   DM               dm;
566942e3340SBarry Smith   DMTS             tsdm;
56724989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
56824989b8cSPeter Brune   void            *ctx;
569b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
570a7a1495cSBarry Smith 
571a7a1495cSBarry Smith   PetscFunctionBegin;
5720700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5730910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
5740910c330SBarry Smith   PetscCheckSameComm(ts, 1, U, 3);
5759566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
5769566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm, &tsdm));
5779566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL));
5789566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobianfunc, &ctx));
5799566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate));
5809566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetId((PetscObject)U, &Uid));
581971015bcSStefano Zampini 
5823ba16761SJacob Faibussowitsch   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(PETSC_SUCCESS);
583d90be118SSean Farley 
58463a3b9bcSJacob Faibussowitsch   PetscCheck(ts->rhsjacobian.shift == 0.0 || !ts->rhsjacobian.reuse, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Should not call TSComputeRHSJacobian() on a shifted matrix (shift=%lf) when RHSJacobian is reusable.", (double)ts->rhsjacobian.shift);
58524989b8cSPeter Brune   if (rhsjacobianfunc) {
5869566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, A, B));
587792fecdfSBarry Smith     PetscCallBack("TS callback Jacobian", (*rhsjacobianfunc)(ts, t, U, A, B, ctx));
588a6ab3590SBarry Smith     ts->rhsjacs++;
5899566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, A, B));
590ef66eb69SBarry Smith   } else {
5919566063dSJacob Faibussowitsch     PetscCall(MatZeroEntries(A));
5929566063dSJacob Faibussowitsch     if (B && A != B) PetscCall(MatZeroEntries(B));
593ef66eb69SBarry Smith   }
5940e4ef248SJed Brown   ts->rhsjacobian.time  = t;
595971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
596971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
5979566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetId((PetscObject)U, &ts->rhsjacobian.Xid));
5989566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)U, &ts->rhsjacobian.Xstate));
5993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
600a7a1495cSBarry Smith }
601a7a1495cSBarry Smith 
602316643e7SJed Brown /*@
603bcf0153eSBarry Smith   TSComputeRHSFunction - Evaluates the right-hand-side function for a `TS`
604d763cef2SBarry Smith 
605c3339decSBarry Smith   Collective
606316643e7SJed Brown 
607316643e7SJed Brown   Input Parameters:
608bcf0153eSBarry Smith + ts - the `TS` context
609316643e7SJed Brown . t  - current time
6100910c330SBarry Smith - U  - state vector
611316643e7SJed Brown 
612316643e7SJed Brown   Output Parameter:
613316643e7SJed Brown . y - right hand side
614316643e7SJed Brown 
615bcf0153eSBarry Smith   Level: developer
616bcf0153eSBarry Smith 
617316643e7SJed Brown   Note:
618316643e7SJed Brown   Most users should not need to explicitly call this routine, as it
619316643e7SJed Brown   is used internally within the nonlinear solvers.
620316643e7SJed Brown 
6211cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()`
622316643e7SJed Brown @*/
623d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunction(TS ts, PetscReal t, Vec U, Vec y)
624d71ae5a4SJacob Faibussowitsch {
62524989b8cSPeter Brune   TSRHSFunction rhsfunction;
62624989b8cSPeter Brune   TSIFunction   ifunction;
62724989b8cSPeter Brune   void         *ctx;
62824989b8cSPeter Brune   DM            dm;
62924989b8cSPeter Brune 
630d763cef2SBarry Smith   PetscFunctionBegin;
6310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
6320910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
6330700a824SBarry Smith   PetscValidHeaderSpecific(y, VEC_CLASSID, 4);
6349566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
6359566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, &ctx));
6369566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm, &ifunction, NULL));
637d763cef2SBarry Smith 
6383c633725SBarry Smith   PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()");
639d763cef2SBarry Smith 
64024989b8cSPeter Brune   if (rhsfunction) {
6419566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, y, 0));
6429566063dSJacob Faibussowitsch     PetscCall(VecLockReadPush(U));
643792fecdfSBarry Smith     PetscCallBack("TS callback right-hand-side", (*rhsfunction)(ts, t, U, y, ctx));
6449566063dSJacob Faibussowitsch     PetscCall(VecLockReadPop(U));
645a6ab3590SBarry Smith     ts->rhsfuncs++;
6469566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, y, 0));
6471e66621cSBarry Smith   } else PetscCall(VecZeroEntries(y));
6483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
649d763cef2SBarry Smith }
650d763cef2SBarry Smith 
651ef20d060SBarry Smith /*@
652ef20d060SBarry Smith   TSComputeSolutionFunction - Evaluates the solution function.
653ef20d060SBarry Smith 
654c3339decSBarry Smith   Collective
655ef20d060SBarry Smith 
656ef20d060SBarry Smith   Input Parameters:
657bcf0153eSBarry Smith + ts - the `TS` context
658ef20d060SBarry Smith - t  - current time
659ef20d060SBarry Smith 
660ef20d060SBarry Smith   Output Parameter:
6610910c330SBarry Smith . U - the solution
662ef20d060SBarry Smith 
663ef20d060SBarry Smith   Level: developer
664ef20d060SBarry Smith 
6651cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()`
666ef20d060SBarry Smith @*/
667d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeSolutionFunction(TS ts, PetscReal t, Vec U)
668d71ae5a4SJacob Faibussowitsch {
669ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
670ef20d060SBarry Smith   void              *ctx;
671ef20d060SBarry Smith   DM                 dm;
672ef20d060SBarry Smith 
673ef20d060SBarry Smith   PetscFunctionBegin;
674ef20d060SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
6750910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
6769566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
6779566063dSJacob Faibussowitsch   PetscCall(DMTSGetSolutionFunction(dm, &solutionfunction, &ctx));
678792fecdfSBarry Smith   if (solutionfunction) PetscCallBack("TS callback solution", (*solutionfunction)(ts, t, U, ctx));
6793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
680ef20d060SBarry Smith }
6819b7cd975SBarry Smith /*@
6829b7cd975SBarry Smith   TSComputeForcingFunction - Evaluates the forcing function.
6839b7cd975SBarry Smith 
684c3339decSBarry Smith   Collective
6859b7cd975SBarry Smith 
6869b7cd975SBarry Smith   Input Parameters:
687bcf0153eSBarry Smith + ts - the `TS` context
6889b7cd975SBarry Smith - t  - current time
6899b7cd975SBarry Smith 
6909b7cd975SBarry Smith   Output Parameter:
6919b7cd975SBarry Smith . U - the function value
6929b7cd975SBarry Smith 
6939b7cd975SBarry Smith   Level: developer
6949b7cd975SBarry Smith 
6951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()`
6969b7cd975SBarry Smith @*/
697d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeForcingFunction(TS ts, PetscReal t, Vec U)
698d71ae5a4SJacob Faibussowitsch {
6999b7cd975SBarry Smith   void             *ctx;
7009b7cd975SBarry Smith   DM                dm;
7015f80ce2aSJacob Faibussowitsch   TSForcingFunction forcing;
7029b7cd975SBarry Smith 
7039b7cd975SBarry Smith   PetscFunctionBegin;
7049b7cd975SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
7059b7cd975SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
7069566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
7079566063dSJacob Faibussowitsch   PetscCall(DMTSGetForcingFunction(dm, &forcing, &ctx));
7089b7cd975SBarry Smith 
709792fecdfSBarry Smith   if (forcing) PetscCallBack("TS callback forcing function", (*forcing)(ts, t, U, ctx));
7103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7119b7cd975SBarry Smith }
712ef20d060SBarry Smith 
713d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSGetRHSVec_Private(TS ts, Vec *Frhs)
714d71ae5a4SJacob Faibussowitsch {
7152dd45cf8SJed Brown   Vec F;
716214bc6a2SJed Brown 
717214bc6a2SJed Brown   PetscFunctionBegin;
7180298fd71SBarry Smith   *Frhs = NULL;
7199566063dSJacob Faibussowitsch   PetscCall(TSGetIFunction(ts, &F, NULL, NULL));
7201e66621cSBarry Smith   if (!ts->Frhs) PetscCall(VecDuplicate(F, &ts->Frhs));
721214bc6a2SJed Brown   *Frhs = ts->Frhs;
7223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
723214bc6a2SJed Brown }
724214bc6a2SJed Brown 
725d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSMats_Private(TS ts, Mat *Arhs, Mat *Brhs)
726d71ae5a4SJacob Faibussowitsch {
727214bc6a2SJed Brown   Mat         A, B;
72841a1d4d2SBarry Smith   TSIJacobian ijacobian;
729214bc6a2SJed Brown 
730214bc6a2SJed Brown   PetscFunctionBegin;
731c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
732c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7339566063dSJacob Faibussowitsch   PetscCall(TSGetIJacobian(ts, &A, &B, &ijacobian, NULL));
734214bc6a2SJed Brown   if (Arhs) {
735214bc6a2SJed Brown     if (!ts->Arhs) {
73641a1d4d2SBarry Smith       if (ijacobian) {
7379566063dSJacob Faibussowitsch         PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &ts->Arhs));
7389566063dSJacob Faibussowitsch         PetscCall(TSSetMatStructure(ts, SAME_NONZERO_PATTERN));
73941a1d4d2SBarry Smith       } else {
74041a1d4d2SBarry Smith         ts->Arhs = A;
7419566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)A));
74241a1d4d2SBarry Smith       }
7433565c898SBarry Smith     } else {
7443565c898SBarry Smith       PetscBool flg;
7459566063dSJacob Faibussowitsch       PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg));
7463565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7473565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs) {
7489566063dSJacob Faibussowitsch         PetscCall(PetscObjectDereference((PetscObject)ts->Arhs));
7493565c898SBarry Smith         ts->Arhs = A;
7509566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)A));
7513565c898SBarry Smith       }
752214bc6a2SJed Brown     }
753214bc6a2SJed Brown     *Arhs = ts->Arhs;
754214bc6a2SJed Brown   }
755214bc6a2SJed Brown   if (Brhs) {
756214bc6a2SJed Brown     if (!ts->Brhs) {
757bdb70873SJed Brown       if (A != B) {
75841a1d4d2SBarry Smith         if (ijacobian) {
7599566063dSJacob Faibussowitsch           PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &ts->Brhs));
760bdb70873SJed Brown         } else {
76141a1d4d2SBarry Smith           ts->Brhs = B;
7629566063dSJacob Faibussowitsch           PetscCall(PetscObjectReference((PetscObject)B));
76341a1d4d2SBarry Smith         }
76441a1d4d2SBarry Smith       } else {
7659566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)ts->Arhs));
76651699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
767bdb70873SJed Brown       }
768214bc6a2SJed Brown     }
769214bc6a2SJed Brown     *Brhs = ts->Brhs;
770214bc6a2SJed Brown   }
7713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
772214bc6a2SJed Brown }
773214bc6a2SJed Brown 
774316643e7SJed Brown /*@
775195e9b02SBarry Smith   TSComputeIFunction - Evaluates the DAE residual written in the implicit form F(t,U,Udot)=0
776316643e7SJed Brown 
777c3339decSBarry Smith   Collective
778316643e7SJed Brown 
779316643e7SJed Brown   Input Parameters:
780bcf0153eSBarry Smith + ts   - the `TS` context
781316643e7SJed Brown . t    - current time
7820910c330SBarry Smith . U    - state vector
7830910c330SBarry Smith . Udot - time derivative of state vector
784bcf0153eSBarry Smith - imex - flag indicates if the method is `TSIMEX` so that the RHSFunction should be kept separate
785316643e7SJed Brown 
786316643e7SJed Brown   Output Parameter:
787316643e7SJed Brown . Y - right hand side
788316643e7SJed Brown 
789bcf0153eSBarry Smith   Level: developer
790bcf0153eSBarry Smith 
791316643e7SJed Brown   Note:
792316643e7SJed Brown   Most users should not need to explicitly call this routine, as it
793316643e7SJed Brown   is used internally within the nonlinear solvers.
794316643e7SJed Brown 
795316643e7SJed Brown   If the user did did not write their equations in implicit form, this
796316643e7SJed Brown   function recasts them in implicit form.
797316643e7SJed Brown 
7981cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSComputeRHSFunction()`
799316643e7SJed Brown @*/
800d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunction(TS ts, PetscReal t, Vec U, Vec Udot, Vec Y, PetscBool imex)
801d71ae5a4SJacob Faibussowitsch {
80224989b8cSPeter Brune   TSIFunction   ifunction;
80324989b8cSPeter Brune   TSRHSFunction rhsfunction;
80424989b8cSPeter Brune   void         *ctx;
80524989b8cSPeter Brune   DM            dm;
806316643e7SJed Brown 
807316643e7SJed Brown   PetscFunctionBegin;
8080700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
8090910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
8100910c330SBarry Smith   PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4);
8110700a824SBarry Smith   PetscValidHeaderSpecific(Y, VEC_CLASSID, 5);
812316643e7SJed Brown 
8139566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
8149566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm, &ifunction, &ctx));
8159566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL));
81624989b8cSPeter Brune 
8173c633725SBarry Smith   PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()");
818d90be118SSean Farley 
8199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, Udot, Y));
82024989b8cSPeter Brune   if (ifunction) {
821792fecdfSBarry Smith     PetscCallBack("TS callback implicit function", (*ifunction)(ts, t, U, Udot, Y, ctx));
822a6ab3590SBarry Smith     ts->ifuncs++;
823214bc6a2SJed Brown   }
824214bc6a2SJed Brown   if (imex) {
8251e66621cSBarry Smith     if (!ifunction) PetscCall(VecCopy(Udot, Y));
82624989b8cSPeter Brune   } else if (rhsfunction) {
82724989b8cSPeter Brune     if (ifunction) {
828214bc6a2SJed Brown       Vec Frhs;
8299566063dSJacob Faibussowitsch       PetscCall(TSGetRHSVec_Private(ts, &Frhs));
8309566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSFunction(ts, t, U, Frhs));
8319566063dSJacob Faibussowitsch       PetscCall(VecAXPY(Y, -1, Frhs));
8322dd45cf8SJed Brown     } else {
8339566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSFunction(ts, t, U, Y));
8349566063dSJacob Faibussowitsch       PetscCall(VecAYPX(Y, -1, Udot));
835316643e7SJed Brown     }
8364a6899ffSJed Brown   }
8379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, Udot, Y));
8383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
839316643e7SJed Brown }
840316643e7SJed Brown 
841cfa8a9a2SHong Zhang /*
842195e9b02SBarry Smith    TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call `TSComputeRHSJacobian()` on it.
843cfa8a9a2SHong Zhang 
844cfa8a9a2SHong Zhang    Note:
845195e9b02SBarry Smith    This routine is needed when one switches from `TSComputeIJacobian()` to `TSComputeRHSJacobian()` because the Jacobian matrix may be shifted or scaled in `TSComputeIJacobian()`.
846cfa8a9a2SHong Zhang 
847cfa8a9a2SHong Zhang */
848d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSRecoverRHSJacobian(TS ts, Mat A, Mat B)
849d71ae5a4SJacob Faibussowitsch {
850cfa8a9a2SHong Zhang   PetscFunctionBegin;
851cfa8a9a2SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
8523c633725SBarry Smith   PetscCheck(A == ts->Arhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Amat");
8533c633725SBarry Smith   PetscCheck(B == ts->Brhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Bmat");
854cfa8a9a2SHong Zhang 
8551baa6e33SBarry Smith   if (ts->rhsjacobian.shift) PetscCall(MatShift(A, -ts->rhsjacobian.shift));
85648a46eb9SPierre Jolivet   if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(A, -1));
857cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
8581baa6e33SBarry Smith     if (ts->rhsjacobian.shift) PetscCall(MatShift(B, -ts->rhsjacobian.shift));
8591e66621cSBarry Smith     if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(B, -1));
860cfa8a9a2SHong Zhang   }
861cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
862cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
8633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
864cfa8a9a2SHong Zhang }
865cfa8a9a2SHong Zhang 
866316643e7SJed Brown /*@
867316643e7SJed Brown   TSComputeIJacobian - Evaluates the Jacobian of the DAE
868316643e7SJed Brown 
869c3339decSBarry Smith   Collective
870316643e7SJed Brown 
871316643e7SJed Brown   Input Parameters:
872bcf0153eSBarry Smith + ts    - the `TS` context
873316643e7SJed Brown . t     - current timestep
8740910c330SBarry Smith . U     - state vector
8750910c330SBarry Smith . Udot  - time derivative of state vector
876214bc6a2SJed Brown . shift - shift to apply, see note below
877bcf0153eSBarry Smith - imex  - flag indicates if the method is `TSIMEX` so that the RHSJacobian should be kept separate
878316643e7SJed Brown 
879316643e7SJed Brown   Output Parameters:
880316643e7SJed Brown + A - Jacobian matrix
881195e9b02SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as `A`
882316643e7SJed Brown 
883bcf0153eSBarry Smith   Level: developer
884bcf0153eSBarry Smith 
885316643e7SJed Brown   Notes:
8860910c330SBarry Smith   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
887195e9b02SBarry Smith .vb
8880910c330SBarry Smith    dF/dU + shift*dF/dUdot
889195e9b02SBarry Smith .ve
890316643e7SJed Brown   Most users should not need to explicitly call this routine, as it
891316643e7SJed Brown   is used internally within the nonlinear solvers.
892316643e7SJed Brown 
8931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIJacobian()`
89463495f91SJed Brown @*/
895d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobian(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, PetscBool imex)
896d71ae5a4SJacob Faibussowitsch {
89724989b8cSPeter Brune   TSIJacobian   ijacobian;
89824989b8cSPeter Brune   TSRHSJacobian rhsjacobian;
89924989b8cSPeter Brune   DM            dm;
90024989b8cSPeter Brune   void         *ctx;
901316643e7SJed Brown 
902316643e7SJed Brown   PetscFunctionBegin;
9030700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
9040910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
9050910c330SBarry Smith   PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4);
906316643e7SJed Brown   PetscValidPointer(A, 6);
90794ab13aaSBarry Smith   PetscValidHeaderSpecific(A, MAT_CLASSID, 6);
908316643e7SJed Brown   PetscValidPointer(B, 7);
90994ab13aaSBarry Smith   PetscValidHeaderSpecific(B, MAT_CLASSID, 7);
91024989b8cSPeter Brune 
9119566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
9129566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm, &ijacobian, &ctx));
9139566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL));
91424989b8cSPeter Brune 
9153c633725SBarry Smith   PetscCheck(rhsjacobian || ijacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
916316643e7SJed Brown 
9179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, A, B));
91824989b8cSPeter Brune   if (ijacobian) {
919792fecdfSBarry Smith     PetscCallBack("TS callback implicit Jacobian", (*ijacobian)(ts, t, U, Udot, shift, A, B, ctx));
920a6ab3590SBarry Smith     ts->ijacs++;
9214a6899ffSJed Brown   }
922214bc6a2SJed Brown   if (imex) {
923b5abc632SBarry Smith     if (!ijacobian) { /* system was written as Udot = G(t,U) */
9244c26be97Sstefano_zampini       PetscBool assembled;
925971015bcSStefano Zampini       if (rhsjacobian) {
926971015bcSStefano Zampini         Mat Arhs = NULL;
9279566063dSJacob Faibussowitsch         PetscCall(TSGetRHSMats_Private(ts, &Arhs, NULL));
928971015bcSStefano Zampini         if (A == Arhs) {
9293c633725SBarry Smith           PetscCheck(rhsjacobian != TSComputeRHSJacobianConstant, 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 */
930971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
931971015bcSStefano Zampini         }
932971015bcSStefano Zampini       }
9339566063dSJacob Faibussowitsch       PetscCall(MatZeroEntries(A));
9349566063dSJacob Faibussowitsch       PetscCall(MatAssembled(A, &assembled));
9354c26be97Sstefano_zampini       if (!assembled) {
9369566063dSJacob Faibussowitsch         PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
9379566063dSJacob Faibussowitsch         PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
9384c26be97Sstefano_zampini       }
9399566063dSJacob Faibussowitsch       PetscCall(MatShift(A, shift));
94094ab13aaSBarry Smith       if (A != B) {
9419566063dSJacob Faibussowitsch         PetscCall(MatZeroEntries(B));
9429566063dSJacob Faibussowitsch         PetscCall(MatAssembled(B, &assembled));
9434c26be97Sstefano_zampini         if (!assembled) {
9449566063dSJacob Faibussowitsch           PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
9459566063dSJacob Faibussowitsch           PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
9464c26be97Sstefano_zampini         }
9479566063dSJacob Faibussowitsch         PetscCall(MatShift(B, shift));
948214bc6a2SJed Brown       }
949214bc6a2SJed Brown     }
950214bc6a2SJed Brown   } else {
951e1244c69SJed Brown     Mat Arhs = NULL, Brhs = NULL;
9521e66621cSBarry Smith 
9531e66621cSBarry Smith     /* RHSJacobian needs to be converted to part of IJacobian if exists */
9541e66621cSBarry Smith     if (rhsjacobian) PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs));
955e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
956e8b1e424SHong Zhang       PetscObjectState Ustate;
957e8b1e424SHong Zhang       PetscObjectId    Uid;
958e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
959e8b1e424SHong Zhang 
9609566063dSJacob Faibussowitsch       PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL));
9619566063dSJacob Faibussowitsch       PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate));
9629566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetId((PetscObject)U, &Uid));
9639371c9d4SSatish Balay       if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) &&
9649371c9d4SSatish Balay           ts->rhsjacobian.scale == -1.) {                      /* No need to recompute RHSJacobian */
9659566063dSJacob Faibussowitsch         PetscCall(MatShift(A, shift - ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */
9661e66621cSBarry Smith         if (A != B) PetscCall(MatShift(B, shift - ts->rhsjacobian.shift));
967e8b1e424SHong Zhang       } else {
9683565c898SBarry Smith         PetscBool flg;
969e8b1e424SHong Zhang 
970e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
971e8b1e424SHong Zhang           /* MatScale has a short path for this case.
972e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
973e8b1e424SHong Zhang              and the matrices have not been assembled yet */
9749566063dSJacob Faibussowitsch           PetscCall(TSRecoverRHSJacobian(ts, A, B));
975e8b1e424SHong Zhang         }
9769566063dSJacob Faibussowitsch         PetscCall(TSComputeRHSJacobian(ts, t, U, A, B));
9779566063dSJacob Faibussowitsch         PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg));
9783565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
9793565c898SBarry Smith         if (!flg) {
9809566063dSJacob Faibussowitsch           PetscCall(MatScale(A, -1));
9819566063dSJacob Faibussowitsch           PetscCall(MatShift(A, shift));
9823565c898SBarry Smith         }
98394ab13aaSBarry Smith         if (A != B) {
9849566063dSJacob Faibussowitsch           PetscCall(MatScale(B, -1));
9859566063dSJacob Faibussowitsch           PetscCall(MatShift(B, shift));
986316643e7SJed Brown         }
987e8b1e424SHong Zhang       }
988e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
989e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
990d60b7d5cSBarry Smith     } else if (Arhs) {  /* Both IJacobian and RHSJacobian */
991e1244c69SJed Brown       if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */
9929566063dSJacob Faibussowitsch         PetscCall(MatZeroEntries(A));
9939566063dSJacob Faibussowitsch         PetscCall(MatShift(A, shift));
99494ab13aaSBarry Smith         if (A != B) {
9959566063dSJacob Faibussowitsch           PetscCall(MatZeroEntries(B));
9969566063dSJacob Faibussowitsch           PetscCall(MatShift(B, shift));
997214bc6a2SJed Brown         }
998316643e7SJed Brown       }
9999566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs));
10009566063dSJacob Faibussowitsch       PetscCall(MatAXPY(A, -1, Arhs, ts->axpy_pattern));
10011e66621cSBarry Smith       if (A != B) PetscCall(MatAXPY(B, -1, Brhs, ts->axpy_pattern));
1002316643e7SJed Brown     }
1003316643e7SJed Brown   }
10049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, A, B));
10053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1006316643e7SJed Brown }
1007316643e7SJed Brown 
1008d763cef2SBarry Smith /*@C
1009d763cef2SBarry Smith   TSSetRHSFunction - Sets the routine for evaluating the function,
1010b5abc632SBarry Smith   where U_t = G(t,u).
1011d763cef2SBarry Smith 
1012c3339decSBarry Smith   Logically Collective
1013d763cef2SBarry Smith 
1014d763cef2SBarry Smith   Input Parameters:
1015bcf0153eSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
1016195e9b02SBarry Smith . r   - vector to put the computed right hand side (or `NULL` to have it created)
1017d763cef2SBarry Smith . f   - routine for evaluating the right-hand-side function
1018195e9b02SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
1019d763cef2SBarry Smith 
102020f4b53cSBarry Smith   Calling sequence of `f`:
102120f4b53cSBarry Smith $     PetscErrorCode f(TS ts, PetscReal t, Vec u, Vec F, void *ctx)
1022a96d6ef6SBarry Smith + ts  - timestep context
1023a96d6ef6SBarry Smith .   t - current timestep
1024d763cef2SBarry Smith .   u - input vector
1025d763cef2SBarry Smith .   F - function vector
1026d763cef2SBarry Smith - ctx - [optional] user-defined function context
1027d763cef2SBarry Smith 
1028d763cef2SBarry Smith   Level: beginner
1029d763cef2SBarry Smith 
1030bcf0153eSBarry Smith   Note:
1031bcf0153eSBarry Smith   You must call this function or `TSSetIFunction()` to define your ODE. You cannot use this function when solving a DAE.
10322bbac0d3SBarry Smith 
10331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSSetIFunction()`
1034d763cef2SBarry Smith @*/
1035d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetRHSFunction(TS ts, Vec r, PetscErrorCode (*f)(TS, PetscReal, Vec, Vec, void *), void *ctx)
1036d71ae5a4SJacob Faibussowitsch {
1037089b2837SJed Brown   SNES snes;
10380298fd71SBarry Smith   Vec  ralloc = NULL;
103924989b8cSPeter Brune   DM   dm;
1040d763cef2SBarry Smith 
1041089b2837SJed Brown   PetscFunctionBegin;
10420700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1043ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2);
104424989b8cSPeter Brune 
10459566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
10469566063dSJacob Faibussowitsch   PetscCall(DMTSSetRHSFunction(dm, f, ctx));
10479566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
1048e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
10499566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->vec_sol, &ralloc));
1050e856ceecSJed Brown     r = ralloc;
1051e856ceecSJed Brown   }
10529566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts));
10539566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ralloc));
10543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1055d763cef2SBarry Smith }
1056d763cef2SBarry Smith 
1057ef20d060SBarry Smith /*@C
1058abd5a294SJed Brown   TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1059ef20d060SBarry Smith 
1060c3339decSBarry Smith   Logically Collective
1061ef20d060SBarry Smith 
1062ef20d060SBarry Smith   Input Parameters:
1063bcf0153eSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
1064ef20d060SBarry Smith . f   - routine for evaluating the solution
1065ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the
1066195e9b02SBarry Smith           function evaluation routine (may be `NULL`)
1067ef20d060SBarry Smith 
106820f4b53cSBarry Smith   Calling sequence of `f`:
106920f4b53cSBarry Smith $   PetscErrorCode f(TS ts, PetscReal t, Vec u, void *ctx)
107020f4b53cSBarry Smith + ts  - the integrator context
107120f4b53cSBarry Smith .   t - current timestep
1072ef20d060SBarry Smith .   u - output vector
1073ef20d060SBarry Smith - ctx - [optional] user-defined function context
1074ef20d060SBarry Smith 
1075bcf0153eSBarry Smith   Options Database Keys:
1076bcf0153eSBarry Smith + -ts_monitor_lg_error   - create a graphical monitor of error history, requires user to have provided `TSSetSolutionFunction()`
1077bcf0153eSBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided `TSSetSolutionFunction()`
1078bcf0153eSBarry Smith 
1079bcf0153eSBarry Smith   Level: intermediate
10800ed3bfb6SBarry Smith 
1081abd5a294SJed Brown   Notes:
1082abd5a294SJed Brown   This routine is used for testing accuracy of time integration schemes when you already know the solution.
1083abd5a294SJed Brown   If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1084abd5a294SJed Brown   create closed-form solutions with non-physical forcing terms.
1085abd5a294SJed Brown 
1086bcf0153eSBarry Smith   For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history.
1087abd5a294SJed Brown 
10881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetForcingFunction()`, `TSSetSolution()`, `TSGetSolution()`, `TSMonitorLGError()`, `TSMonitorDrawError()`
1089ef20d060SBarry Smith @*/
1090d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSolutionFunction(TS ts, PetscErrorCode (*f)(TS, PetscReal, Vec, void *), void *ctx)
1091d71ae5a4SJacob Faibussowitsch {
1092ef20d060SBarry Smith   DM dm;
1093ef20d060SBarry Smith 
1094ef20d060SBarry Smith   PetscFunctionBegin;
1095ef20d060SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
10969566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
10979566063dSJacob Faibussowitsch   PetscCall(DMTSSetSolutionFunction(dm, f, ctx));
10983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1099ef20d060SBarry Smith }
1100ef20d060SBarry Smith 
11019b7cd975SBarry Smith /*@C
11029b7cd975SBarry Smith   TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11039b7cd975SBarry Smith 
1104c3339decSBarry Smith   Logically Collective
11059b7cd975SBarry Smith 
11069b7cd975SBarry Smith   Input Parameters:
1107bcf0153eSBarry Smith + ts   - the `TS` context obtained from `TSCreate()`
1108e162b725SBarry Smith . func - routine for evaluating the forcing function
11099b7cd975SBarry Smith - ctx  - [optional] user-defined context for private data for the
1110195e9b02SBarry Smith           function evaluation routine (may be `NULL`)
11119b7cd975SBarry Smith 
11129b7cd975SBarry Smith   Calling sequence of func:
111320f4b53cSBarry Smith $   PetscErrorCode func(TS ts, PetscReal t, Vec f, void *ctx)
111420f4b53cSBarry Smith + ts  - the integrator context
111520f4b53cSBarry Smith .   t - current timestep
1116e162b725SBarry Smith .   f - output vector
11179b7cd975SBarry Smith - ctx - [optional] user-defined function context
11189b7cd975SBarry Smith 
1119bcf0153eSBarry Smith   Level: intermediate
1120bcf0153eSBarry Smith 
11219b7cd975SBarry Smith   Notes:
11229b7cd975SBarry Smith   This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1123e162b725SBarry 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
1124e162b725SBarry Smith   definition of the problem you are solving and hence possibly introducing bugs.
1125e162b725SBarry Smith 
1126e162b725SBarry Smith   This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1127e162b725SBarry Smith 
1128e162b725SBarry 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
1129e162b725SBarry Smith   parameters can be passed in the ctx variable.
11309b7cd975SBarry Smith 
1131bcf0153eSBarry Smith   For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history.
11329b7cd975SBarry Smith 
11331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetSolutionFunction()`
11349b7cd975SBarry Smith @*/
1135d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetForcingFunction(TS ts, TSForcingFunction func, void *ctx)
1136d71ae5a4SJacob Faibussowitsch {
11379b7cd975SBarry Smith   DM dm;
11389b7cd975SBarry Smith 
11399b7cd975SBarry Smith   PetscFunctionBegin;
11409b7cd975SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
11419566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
11429566063dSJacob Faibussowitsch   PetscCall(DMTSSetForcingFunction(dm, func, ctx));
11433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11449b7cd975SBarry Smith }
11459b7cd975SBarry Smith 
1146d763cef2SBarry Smith /*@C
1147f7ab8db6SBarry Smith   TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1148b5abc632SBarry Smith   where U_t = G(U,t), as well as the location to store the matrix.
1149d763cef2SBarry Smith 
1150c3339decSBarry Smith   Logically Collective
1151d763cef2SBarry Smith 
1152d763cef2SBarry Smith   Input Parameters:
1153bcf0153eSBarry Smith + ts   - the `TS` context obtained from `TSCreate()`
1154195e9b02SBarry Smith . Amat - (approximate) location to store Jacobian matrix entries computed by `f`
1155195e9b02SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as `Amat`)
1156d763cef2SBarry Smith . f    - the Jacobian evaluation routine
1157195e9b02SBarry Smith - ctx  - [optional] user-defined context for private data for the Jacobian evaluation routine (may be `NULL`)
1158d763cef2SBarry Smith 
115920f4b53cSBarry Smith   Calling sequence of `f`:
116020f4b53cSBarry Smith $  PetscErrorCode f(TS ts, PetscReal t, Vec u, Mat A, Mat B, void *ctx)
116120f4b53cSBarry Smith + ts   - the `TS` context obtained from `TSCreate()`
116220f4b53cSBarry Smith .  t - current timestep
1163d763cef2SBarry Smith .  u - input vector
1164e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix
1165195e9b02SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as `Amat`)
1166d763cef2SBarry Smith - ctx  - [optional] user-defined context for matrix evaluation routine
1167d763cef2SBarry Smith 
1168bcf0153eSBarry Smith   Level: beginner
1169bcf0153eSBarry Smith 
11706cd88445SBarry Smith   Notes:
11716cd88445SBarry Smith   You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11726cd88445SBarry Smith 
1173bcf0153eSBarry Smith   The `TS` solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1174ca5f011dSBarry Smith   You should not assume the values are the same in the next call to f() as you set them in the previous call.
1175d763cef2SBarry Smith 
11761cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNESComputeJacobianDefaultColor()`, `TSSetRHSFunction()`, `TSRHSJacobianSetReuse()`, `TSSetIJacobian()`
1177d763cef2SBarry Smith @*/
1178d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetRHSJacobian(TS ts, Mat Amat, Mat Pmat, TSRHSJacobian f, void *ctx)
1179d71ae5a4SJacob Faibussowitsch {
1180089b2837SJed Brown   SNES        snes;
118124989b8cSPeter Brune   DM          dm;
118224989b8cSPeter Brune   TSIJacobian ijacobian;
1183277b19d0SLisandro Dalcin 
1184d763cef2SBarry Smith   PetscFunctionBegin;
11850700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1186e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2);
1187e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3);
1188e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts, 1, Amat, 2);
1189e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3);
1190d763cef2SBarry Smith 
11919566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
11929566063dSJacob Faibussowitsch   PetscCall(DMTSSetRHSJacobian(dm, f, ctx));
11939566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm, &ijacobian, NULL));
11949566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
11951e66621cSBarry Smith   if (!ijacobian) PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts));
1196e5d3d808SBarry Smith   if (Amat) {
11979566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)Amat));
11989566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&ts->Arhs));
1199e5d3d808SBarry Smith     ts->Arhs = Amat;
12000e4ef248SJed Brown   }
1201e5d3d808SBarry Smith   if (Pmat) {
12029566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)Pmat));
12039566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&ts->Brhs));
1204e5d3d808SBarry Smith     ts->Brhs = Pmat;
12050e4ef248SJed Brown   }
12063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1207d763cef2SBarry Smith }
1208d763cef2SBarry Smith 
1209316643e7SJed Brown /*@C
1210b5abc632SBarry Smith   TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1211316643e7SJed Brown 
1212c3339decSBarry Smith   Logically Collective
1213316643e7SJed Brown 
1214316643e7SJed Brown   Input Parameters:
1215bcf0153eSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
1216195e9b02SBarry Smith . r   - vector to hold the residual (or `NULL` to have it created internally)
1217316643e7SJed Brown . f   - the function evaluation routine
1218195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`)
1219316643e7SJed Brown 
122020f4b53cSBarry Smith   Calling sequence of `f`:
122120f4b53cSBarry Smith $   PetscErrorCode f(TS ts, PetscReal t, Vec u, Vec u_t, Vec F, void *ctx)
122220f4b53cSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
122320f4b53cSBarry Smith .  t   - time at step/stage being solved
1224316643e7SJed Brown .  u   - state vector
1225316643e7SJed Brown .  u_t - time derivative of state vector
1226316643e7SJed Brown .  F   - function vector
1227316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine
1228316643e7SJed Brown 
1229316643e7SJed Brown   Level: beginner
1230316643e7SJed Brown 
1231bcf0153eSBarry Smith   Note:
1232bcf0153eSBarry Smith   The user MUST call either this routine or `TSSetRHSFunction()` to define the ODE.  When solving DAEs you must use this function.
1233bcf0153eSBarry Smith 
12341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `TSSetRHSFunction()`, `TSSetIJacobian()`
1235316643e7SJed Brown @*/
1236d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetIFunction(TS ts, Vec r, TSIFunction f, void *ctx)
1237d71ae5a4SJacob Faibussowitsch {
1238089b2837SJed Brown   SNES snes;
123951699248SLisandro Dalcin   Vec  ralloc = NULL;
124024989b8cSPeter Brune   DM   dm;
1241316643e7SJed Brown 
1242316643e7SJed Brown   PetscFunctionBegin;
12430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
124451699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2);
124524989b8cSPeter Brune 
12469566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
12479566063dSJacob Faibussowitsch   PetscCall(DMTSSetIFunction(dm, f, ctx));
124824989b8cSPeter Brune 
12499566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
125051699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
12519566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->vec_sol, &ralloc));
125251699248SLisandro Dalcin     r = ralloc;
1253e856ceecSJed Brown   }
12549566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts));
12559566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ralloc));
12563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1257089b2837SJed Brown }
1258089b2837SJed Brown 
1259089b2837SJed Brown /*@C
1260a5b23f4aSJose E. Roman   TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it.
1261089b2837SJed Brown 
1262089b2837SJed Brown   Not Collective
1263089b2837SJed Brown 
1264089b2837SJed Brown   Input Parameter:
1265bcf0153eSBarry Smith . ts - the `TS` context
1266089b2837SJed Brown 
1267d8d19677SJose E. Roman   Output Parameters:
1268195e9b02SBarry Smith + r    - vector to hold residual (or `NULL`)
1269195e9b02SBarry Smith . func - the function to compute residual (or `NULL`)
1270195e9b02SBarry Smith - ctx  - the function context (or `NULL`)
1271089b2837SJed Brown 
1272089b2837SJed Brown   Level: advanced
1273089b2837SJed Brown 
12741cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()`
1275089b2837SJed Brown @*/
1276d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetIFunction(TS ts, Vec *r, TSIFunction *func, void **ctx)
1277d71ae5a4SJacob Faibussowitsch {
1278089b2837SJed Brown   SNES snes;
127924989b8cSPeter Brune   DM   dm;
1280089b2837SJed Brown 
1281089b2837SJed Brown   PetscFunctionBegin;
1282089b2837SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
12839566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
12849566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes, r, NULL, NULL));
12859566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
12869566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm, func, ctx));
12873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1288089b2837SJed Brown }
1289089b2837SJed Brown 
1290089b2837SJed Brown /*@C
1291089b2837SJed Brown   TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1292089b2837SJed Brown 
1293089b2837SJed Brown   Not Collective
1294089b2837SJed Brown 
1295089b2837SJed Brown   Input Parameter:
1296bcf0153eSBarry Smith . ts - the `TS` context
1297089b2837SJed Brown 
1298d8d19677SJose E. Roman   Output Parameters:
1299195e9b02SBarry Smith + r    - vector to hold computed right hand side (or `NULL`)
1300195e9b02SBarry Smith . func - the function to compute right hand side (or `NULL`)
1301195e9b02SBarry Smith - ctx  - the function context (or `NULL`)
1302089b2837SJed Brown 
1303089b2837SJed Brown   Level: advanced
1304089b2837SJed Brown 
13051cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `SNESGetFunction()`
1306089b2837SJed Brown @*/
1307d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSFunction(TS ts, Vec *r, TSRHSFunction *func, void **ctx)
1308d71ae5a4SJacob Faibussowitsch {
1309089b2837SJed Brown   SNES snes;
131024989b8cSPeter Brune   DM   dm;
1311089b2837SJed Brown 
1312089b2837SJed Brown   PetscFunctionBegin;
1313089b2837SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
13149566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
13159566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes, r, NULL, NULL));
13169566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
13179566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm, func, ctx));
13183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1319316643e7SJed Brown }
1320316643e7SJed Brown 
1321316643e7SJed Brown /*@C
1322a4f0a591SBarry Smith   TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1323bcf0153eSBarry Smith   provided with `TSSetIFunction()`.
1324316643e7SJed Brown 
1325c3339decSBarry Smith   Logically Collective
1326316643e7SJed Brown 
1327316643e7SJed Brown   Input Parameters:
1328bcf0153eSBarry Smith + ts   - the `TS` context obtained from `TSCreate()`
1329aaa8cc7dSPierre Jolivet . Amat - (approximate) matrix to store Jacobian entries computed by `f`
1330195e9b02SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as `Amat`)
1331316643e7SJed Brown . f    - the Jacobian evaluation routine
1332195e9b02SBarry Smith - ctx  - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`)
1333316643e7SJed Brown 
133420f4b53cSBarry Smith   Calling sequence of `f`:
133520f4b53cSBarry Smith $    PetscErrorCode f(TS ts, PetscReal t, Vec U, Vec U_t, PetscReal a, Mat Amat, Mat Pmat, void *ctx)
133620f4b53cSBarry Smith + ts   - the `TS` context obtained from `TSCreate()`
133720f4b53cSBarry Smith .  t    - time at step/stage being solved
13381b4a444bSJed Brown .  U    - state vector
13391b4a444bSJed Brown .  U_t  - time derivative of state vector
1340316643e7SJed Brown .  a    - shift
1341e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1342195e9b02SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as `Amat`
1343316643e7SJed Brown - ctx  - [optional] user-defined context for matrix evaluation routine
1344316643e7SJed Brown 
1345bcf0153eSBarry Smith   Level: beginner
1346316643e7SJed Brown 
1347bcf0153eSBarry Smith   Notes:
1348195e9b02SBarry Smith   The matrices `Amat` and `Pmat` are exactly the matrices that are used by `SNES` for the nonlinear solve.
1349bcf0153eSBarry Smith 
1350bcf0153eSBarry Smith   If you know the operator Amat has a null space you can use `MatSetNullSpace()` and `MatSetTransposeNullSpace()` to supply the null
1351195e9b02SBarry Smith   space to `Amat` and the `KSP` solvers will automatically use that null space as needed during the solution process.
1352895c21f2SBarry Smith 
1353a4f0a591SBarry Smith   The matrix dF/dU + a*dF/dU_t you provide turns out to be
1354b5abc632SBarry Smith   the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1355a4f0a591SBarry Smith   The time integrator internally approximates U_t by W+a*U where the positive "shift"
1356a4f0a591SBarry Smith   a and vector W depend on the integration method, step size, and past states. For example with
1357a4f0a591SBarry Smith   the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1358a4f0a591SBarry Smith   W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1359a4f0a591SBarry Smith 
13606cd88445SBarry Smith   You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13616cd88445SBarry Smith 
1362195e9b02SBarry Smith   The TS solver may modify the nonzero structure and the entries of the matrices `Amat` and `Pmat` between the calls to `f`
1363195e9b02SBarry Smith   You should not assume the values are the same in the next call to `f` as you set them in the previous call.
1364ca5f011dSBarry Smith 
13651cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSSetRHSJacobian()`, `SNESComputeJacobianDefaultColor()`, `SNESComputeJacobianDefault()`, `TSSetRHSFunction()`
1366316643e7SJed Brown @*/
1367d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetIJacobian(TS ts, Mat Amat, Mat Pmat, TSIJacobian f, void *ctx)
1368d71ae5a4SJacob Faibussowitsch {
1369089b2837SJed Brown   SNES snes;
137024989b8cSPeter Brune   DM   dm;
1371316643e7SJed Brown 
1372316643e7SJed Brown   PetscFunctionBegin;
13730700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1374e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2);
1375e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3);
1376e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts, 1, Amat, 2);
1377e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3);
137824989b8cSPeter Brune 
13799566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
13809566063dSJacob Faibussowitsch   PetscCall(DMTSSetIJacobian(dm, f, ctx));
138124989b8cSPeter Brune 
13829566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
13839566063dSJacob Faibussowitsch   PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts));
13843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1385316643e7SJed Brown }
1386316643e7SJed Brown 
1387e1244c69SJed Brown /*@
1388bcf0153eSBarry Smith   TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, `TS` will change the sign and
1389e1244c69SJed Brown   shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1390e1244c69SJed Brown   the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1391195e9b02SBarry Smith   not been changed by the `TS`.
1392e1244c69SJed Brown 
1393e1244c69SJed Brown   Logically Collective
1394e1244c69SJed Brown 
13954165533cSJose E. Roman   Input Parameters:
1396bcf0153eSBarry Smith + ts    - `TS` context obtained from `TSCreate()`
1397bcf0153eSBarry Smith - reuse - `PETSC_TRUE` if the RHS Jacobian
1398e1244c69SJed Brown 
1399e1244c69SJed Brown   Level: intermediate
1400e1244c69SJed Brown 
14011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()`
1402e1244c69SJed Brown @*/
1403d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianSetReuse(TS ts, PetscBool reuse)
1404d71ae5a4SJacob Faibussowitsch {
1405e1244c69SJed Brown   PetscFunctionBegin;
1406e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
14073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1408e1244c69SJed Brown }
1409e1244c69SJed Brown 
1410efe9872eSLisandro Dalcin /*@C
1411efe9872eSLisandro Dalcin   TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1412efe9872eSLisandro Dalcin 
1413c3339decSBarry Smith   Logically Collective
1414efe9872eSLisandro Dalcin 
1415efe9872eSLisandro Dalcin   Input Parameters:
1416bcf0153eSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
1417195e9b02SBarry Smith . F   - vector to hold the residual (or `NULL` to have it created internally)
1418efe9872eSLisandro Dalcin . fun - the function evaluation routine
1419195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`)
1420efe9872eSLisandro Dalcin 
142120f4b53cSBarry Smith   Calling sequence of `fun`:
142220f4b53cSBarry Smith $   PetscErrorCode fun(TS ts, PetscReal t, Vec U, Vec U_t, Vec U_tt, Vec F,void *ctx);
142320f4b53cSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
142420f4b53cSBarry Smith .  t    - time at step/stage being solved
1425efe9872eSLisandro Dalcin .  U    - state vector
1426efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1427efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1428efe9872eSLisandro Dalcin . F   - function vector
1429195e9b02SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine (may be `NULL`)
1430efe9872eSLisandro Dalcin 
1431efe9872eSLisandro Dalcin   Level: beginner
1432efe9872eSLisandro Dalcin 
14331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Jacobian()`, `TSSetIFunction()`, `TSCreate()`, `TSSetRHSFunction()`
1434efe9872eSLisandro Dalcin @*/
1435d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetI2Function(TS ts, Vec F, TSI2Function fun, void *ctx)
1436d71ae5a4SJacob Faibussowitsch {
1437efe9872eSLisandro Dalcin   DM dm;
1438efe9872eSLisandro Dalcin 
1439efe9872eSLisandro Dalcin   PetscFunctionBegin;
1440efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1441efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F, VEC_CLASSID, 2);
14429566063dSJacob Faibussowitsch   PetscCall(TSSetIFunction(ts, F, NULL, NULL));
14439566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
14449566063dSJacob Faibussowitsch   PetscCall(DMTSSetI2Function(dm, fun, ctx));
14453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1446efe9872eSLisandro Dalcin }
1447efe9872eSLisandro Dalcin 
1448efe9872eSLisandro Dalcin /*@C
1449a5b23f4aSJose E. Roman   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it.
1450efe9872eSLisandro Dalcin 
1451efe9872eSLisandro Dalcin   Not Collective
1452efe9872eSLisandro Dalcin 
1453efe9872eSLisandro Dalcin   Input Parameter:
1454bcf0153eSBarry Smith . ts - the `TS` context
1455efe9872eSLisandro Dalcin 
1456d8d19677SJose E. Roman   Output Parameters:
1457195e9b02SBarry Smith + r   - vector to hold residual (or `NULL`)
1458195e9b02SBarry Smith . fun - the function to compute residual (or `NULL`)
1459195e9b02SBarry Smith - ctx - the function context (or `NULL`)
1460efe9872eSLisandro Dalcin 
1461efe9872eSLisandro Dalcin   Level: advanced
1462efe9872eSLisandro Dalcin 
14631cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()`, `TSCreate()`
1464efe9872eSLisandro Dalcin @*/
1465d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetI2Function(TS ts, Vec *r, TSI2Function *fun, void **ctx)
1466d71ae5a4SJacob Faibussowitsch {
1467efe9872eSLisandro Dalcin   SNES snes;
1468efe9872eSLisandro Dalcin   DM   dm;
1469efe9872eSLisandro Dalcin 
1470efe9872eSLisandro Dalcin   PetscFunctionBegin;
1471efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
14729566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
14739566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes, r, NULL, NULL));
14749566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
14759566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Function(dm, fun, ctx));
14763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1477efe9872eSLisandro Dalcin }
1478efe9872eSLisandro Dalcin 
1479efe9872eSLisandro Dalcin /*@C
1480bc77d74cSLisandro Dalcin   TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1481bcf0153eSBarry Smith   where F(t,U,U_t,U_tt) is the function you provided with `TSSetI2Function()`.
1482efe9872eSLisandro Dalcin 
1483c3339decSBarry Smith   Logically Collective
1484efe9872eSLisandro Dalcin 
1485efe9872eSLisandro Dalcin   Input Parameters:
1486bcf0153eSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
1487195e9b02SBarry Smith . J   - matrix to hold the Jacobian values
1488195e9b02SBarry Smith . P   - matrix for constructing the preconditioner (may be same as `J`)
1489efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine
1490195e9b02SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`)
1491efe9872eSLisandro Dalcin 
149220f4b53cSBarry Smith   Calling sequence of `jac`:
149320f4b53cSBarry 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)
149420f4b53cSBarry Smith + ts  - the `TS` context obtained from `TSCreate()`
149520f4b53cSBarry Smith .  t    - time at step/stage being solved
1496efe9872eSLisandro Dalcin .  U    - state vector
1497efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1498efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1499efe9872eSLisandro Dalcin .  v    - shift for U_t
1500efe9872eSLisandro Dalcin .  a    - shift for U_tt
1501efe9872eSLisandro 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
1502efe9872eSLisandro Dalcin . P   - preconditioning matrix for J, may be same as J
1503efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine
1504efe9872eSLisandro Dalcin 
1505bcf0153eSBarry Smith   Level: beginner
1506bcf0153eSBarry Smith 
1507efe9872eSLisandro Dalcin   Notes:
1508195e9b02SBarry Smith   The matrices `J` and `P` are exactly the matrices that are used by `SNES` for the nonlinear solve.
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin   The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1511efe9872eSLisandro 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.
1512efe9872eSLisandro Dalcin   The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1513bc77d74cSLisandro Dalcin   parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1514efe9872eSLisandro Dalcin 
15151cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Function()`, `TSGetI2Jacobian()`
1516efe9872eSLisandro Dalcin @*/
1517d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetI2Jacobian(TS ts, Mat J, Mat P, TSI2Jacobian jac, void *ctx)
1518d71ae5a4SJacob Faibussowitsch {
1519efe9872eSLisandro Dalcin   DM dm;
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin   PetscFunctionBegin;
1522efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1523efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J, MAT_CLASSID, 2);
1524efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P, MAT_CLASSID, 3);
15259566063dSJacob Faibussowitsch   PetscCall(TSSetIJacobian(ts, J, P, NULL, NULL));
15269566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
15279566063dSJacob Faibussowitsch   PetscCall(DMTSSetI2Jacobian(dm, jac, ctx));
15283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1529efe9872eSLisandro Dalcin }
1530efe9872eSLisandro Dalcin 
1531efe9872eSLisandro Dalcin /*@C
1532efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1533efe9872eSLisandro Dalcin 
1534bcf0153eSBarry Smith   Not Collective, but parallel objects are returned if `TS` is parallel
1535efe9872eSLisandro Dalcin 
1536efe9872eSLisandro Dalcin   Input Parameter:
1537bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin   Output Parameters:
1540efe9872eSLisandro Dalcin + J   - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1541195e9b02SBarry Smith . P   - The matrix from which the preconditioner is constructed, often the same as `J`
1542efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1543efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1544efe9872eSLisandro Dalcin 
1545bcf0153eSBarry Smith   Level: advanced
1546bcf0153eSBarry Smith 
1547195e9b02SBarry Smith   Note:
1548195e9b02SBarry Smith   You can pass in `NULL` for any return argument you do not need.
1549efe9872eSLisandro Dalcin 
15501cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`, `TSSetI2Jacobian()`, `TSGetI2Function()`, `TSCreate()`
1551efe9872eSLisandro Dalcin @*/
1552d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetI2Jacobian(TS ts, Mat *J, Mat *P, TSI2Jacobian *jac, void **ctx)
1553d71ae5a4SJacob Faibussowitsch {
1554efe9872eSLisandro Dalcin   SNES snes;
1555efe9872eSLisandro Dalcin   DM   dm;
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin   PetscFunctionBegin;
15589566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
15599566063dSJacob Faibussowitsch   PetscCall(SNESSetUpMatrices(snes));
15609566063dSJacob Faibussowitsch   PetscCall(SNESGetJacobian(snes, J, P, NULL, NULL));
15619566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
15629566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm, jac, ctx));
15633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1564efe9872eSLisandro Dalcin }
1565efe9872eSLisandro Dalcin 
1566efe9872eSLisandro Dalcin /*@
1567efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1568efe9872eSLisandro Dalcin 
1569c3339decSBarry Smith   Collective
1570efe9872eSLisandro Dalcin 
1571efe9872eSLisandro Dalcin   Input Parameters:
1572bcf0153eSBarry Smith + ts - the `TS` context
1573efe9872eSLisandro Dalcin . t  - current time
1574efe9872eSLisandro Dalcin . U  - state vector
1575efe9872eSLisandro Dalcin . V  - time derivative of state vector (U_t)
1576efe9872eSLisandro Dalcin - A  - second time derivative of state vector (U_tt)
1577efe9872eSLisandro Dalcin 
1578efe9872eSLisandro Dalcin   Output Parameter:
1579efe9872eSLisandro Dalcin . F - the residual vector
1580efe9872eSLisandro Dalcin 
1581bcf0153eSBarry Smith   Level: developer
1582bcf0153eSBarry Smith 
1583efe9872eSLisandro Dalcin   Note:
1584efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1585efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1586efe9872eSLisandro Dalcin 
15871cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Function()`, `TSGetI2Function()`
1588efe9872eSLisandro Dalcin @*/
1589d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Function(TS ts, PetscReal t, Vec U, Vec V, Vec A, Vec F)
1590d71ae5a4SJacob Faibussowitsch {
1591efe9872eSLisandro Dalcin   DM            dm;
1592efe9872eSLisandro Dalcin   TSI2Function  I2Function;
1593efe9872eSLisandro Dalcin   void         *ctx;
1594efe9872eSLisandro Dalcin   TSRHSFunction rhsfunction;
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   PetscFunctionBegin;
1597efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1598efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
1599efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V, VEC_CLASSID, 4);
1600efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A, VEC_CLASSID, 5);
1601efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F, VEC_CLASSID, 6);
1602efe9872eSLisandro Dalcin 
16039566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
16049566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Function(dm, &I2Function, &ctx));
16059566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL));
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   if (!I2Function) {
16089566063dSJacob Faibussowitsch     PetscCall(TSComputeIFunction(ts, t, U, A, F, PETSC_FALSE));
16093ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1610efe9872eSLisandro Dalcin   }
1611efe9872eSLisandro Dalcin 
16129566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, V, F));
1613efe9872eSLisandro Dalcin 
1614792fecdfSBarry Smith   PetscCallBack("TS callback implicit function", I2Function(ts, t, U, V, A, F, ctx));
1615efe9872eSLisandro Dalcin 
1616efe9872eSLisandro Dalcin   if (rhsfunction) {
1617efe9872eSLisandro Dalcin     Vec Frhs;
16189566063dSJacob Faibussowitsch     PetscCall(TSGetRHSVec_Private(ts, &Frhs));
16199566063dSJacob Faibussowitsch     PetscCall(TSComputeRHSFunction(ts, t, U, Frhs));
16209566063dSJacob Faibussowitsch     PetscCall(VecAXPY(F, -1, Frhs));
1621efe9872eSLisandro Dalcin   }
1622efe9872eSLisandro Dalcin 
16239566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, V, F));
16243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1625efe9872eSLisandro Dalcin }
1626efe9872eSLisandro Dalcin 
1627efe9872eSLisandro Dalcin /*@
1628efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1629efe9872eSLisandro Dalcin 
1630c3339decSBarry Smith   Collective
1631efe9872eSLisandro Dalcin 
1632efe9872eSLisandro Dalcin   Input Parameters:
1633bcf0153eSBarry Smith + ts     - the `TS` context
1634efe9872eSLisandro Dalcin . t      - current timestep
1635efe9872eSLisandro Dalcin . U      - state vector
1636efe9872eSLisandro Dalcin . V      - time derivative of state vector
1637efe9872eSLisandro Dalcin . A      - second time derivative of state vector
1638efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1639efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1640efe9872eSLisandro Dalcin 
1641efe9872eSLisandro Dalcin   Output Parameters:
1642efe9872eSLisandro Dalcin + J - Jacobian matrix
1643efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1644efe9872eSLisandro Dalcin 
1645bcf0153eSBarry Smith   Level: developer
1646bcf0153eSBarry Smith 
1647efe9872eSLisandro Dalcin   Notes:
1648efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1649efe9872eSLisandro Dalcin 
1650efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1651efe9872eSLisandro Dalcin 
1652efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1653efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1654efe9872eSLisandro Dalcin 
16551cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Jacobian()`
1656efe9872eSLisandro Dalcin @*/
1657d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Jacobian(TS ts, PetscReal t, Vec U, Vec V, Vec A, PetscReal shiftV, PetscReal shiftA, Mat J, Mat P)
1658d71ae5a4SJacob Faibussowitsch {
1659efe9872eSLisandro Dalcin   DM            dm;
1660efe9872eSLisandro Dalcin   TSI2Jacobian  I2Jacobian;
1661efe9872eSLisandro Dalcin   void         *ctx;
1662efe9872eSLisandro Dalcin   TSRHSJacobian rhsjacobian;
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin   PetscFunctionBegin;
1665efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1666efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
1667efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V, VEC_CLASSID, 4);
1668efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A, VEC_CLASSID, 5);
1669efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J, MAT_CLASSID, 8);
1670efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P, MAT_CLASSID, 9);
1671efe9872eSLisandro Dalcin 
16729566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
16739566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm, &I2Jacobian, &ctx));
16749566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL));
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   if (!I2Jacobian) {
16779566063dSJacob Faibussowitsch     PetscCall(TSComputeIJacobian(ts, t, U, A, shiftA, J, P, PETSC_FALSE));
16783ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1679efe9872eSLisandro Dalcin   }
1680efe9872eSLisandro Dalcin 
16819566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, J, P));
1682792fecdfSBarry Smith   PetscCallBack("TS callback implicit Jacobian", I2Jacobian(ts, t, U, V, A, shiftV, shiftA, J, P, ctx));
1683efe9872eSLisandro Dalcin   if (rhsjacobian) {
1684d60b7d5cSBarry Smith     Mat Jrhs, Prhs;
16859566063dSJacob Faibussowitsch     PetscCall(TSGetRHSMats_Private(ts, &Jrhs, &Prhs));
16869566063dSJacob Faibussowitsch     PetscCall(TSComputeRHSJacobian(ts, t, U, Jrhs, Prhs));
16879566063dSJacob Faibussowitsch     PetscCall(MatAXPY(J, -1, Jrhs, ts->axpy_pattern));
16889566063dSJacob Faibussowitsch     if (P != J) PetscCall(MatAXPY(P, -1, Prhs, ts->axpy_pattern));
1689efe9872eSLisandro Dalcin   }
1690efe9872eSLisandro Dalcin 
16919566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, J, P));
16923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1693efe9872eSLisandro Dalcin }
1694efe9872eSLisandro Dalcin 
1695438f35afSJed Brown /*@C
1696438f35afSJed Brown   TSSetTransientVariable - sets function to transform from state to transient variables
1697438f35afSJed Brown 
1698438f35afSJed Brown   Logically Collective
1699438f35afSJed Brown 
17004165533cSJose E. Roman   Input Parameters:
1701438f35afSJed Brown + ts   - time stepping context on which to change the transient variable
1702a96d6ef6SBarry Smith . tvar - a function that transforms to transient variables
1703438f35afSJed Brown - ctx  - a context for tvar
1704438f35afSJed Brown 
170520f4b53cSBarry Smith   Calling sequence of `tvar`:
170620f4b53cSBarry Smith $   PetscErrorCode tvar(TS ts, Vec p, Vec c, void *ctx)
1707a96d6ef6SBarry Smith + ts  - timestep context
17086aad120cSJose E. Roman .   p - input vector (primitive form)
1709a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1710a96d6ef6SBarry Smith - ctx - [optional] user-defined function context
1711a96d6ef6SBarry Smith 
1712438f35afSJed Brown   Level: advanced
1713438f35afSJed Brown 
1714438f35afSJed Brown   Notes:
1715bcf0153eSBarry Smith   This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., `TSBDF`)
1716438f35afSJed Brown   can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1717438f35afSJed Brown   well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1718438f35afSJed Brown   C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1719438f35afSJed Brown   evaluated via the chain rule, as in
1720195e9b02SBarry Smith .vb
1721438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1722195e9b02SBarry Smith .ve
1723438f35afSJed Brown 
17241cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `DMTSGetTransientVariable()`, `TSSetIFunction()`, `TSSetIJacobian()`
1725438f35afSJed Brown @*/
1726d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTransientVariable(TS ts, TSTransientVariable tvar, void *ctx)
1727d71ae5a4SJacob Faibussowitsch {
1728438f35afSJed Brown   DM dm;
1729438f35afSJed Brown 
1730438f35afSJed Brown   PetscFunctionBegin;
1731438f35afSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
17329566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
17339566063dSJacob Faibussowitsch   PetscCall(DMTSSetTransientVariable(dm, tvar, ctx));
17343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1735438f35afSJed Brown }
1736438f35afSJed Brown 
1737efe9872eSLisandro Dalcin /*@
1738e3c11fc1SJed Brown   TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1739e3c11fc1SJed Brown 
1740e3c11fc1SJed Brown   Logically Collective
1741e3c11fc1SJed Brown 
1742e3c11fc1SJed Brown   Input Parameters:
1743e3c11fc1SJed Brown + ts - TS on which to compute
1744e3c11fc1SJed Brown - U  - state vector to be transformed to transient variables
1745e3c11fc1SJed Brown 
17462fe279fdSBarry Smith   Output Parameter:
1747e3c11fc1SJed Brown . C - transient (conservative) variable
1748e3c11fc1SJed Brown 
1749e3c11fc1SJed Brown   Level: developer
1750e3c11fc1SJed Brown 
1751b43aa488SJacob Faibussowitsch   Developer Notes:
1752195e9b02SBarry Smith   If `DMTSSetTransientVariable()` has not been called, then C is not modified in this routine and C = `NULL` is allowed.
1753bcf0153eSBarry Smith   This makes it safe to call without a guard.  One can use `TSHasTransientVariable()` to check if transient variables are
1754bcf0153eSBarry Smith   being used.
1755bcf0153eSBarry Smith 
17561cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeIFunction()`, `TSComputeIJacobian()`
1757e3c11fc1SJed Brown @*/
1758d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeTransientVariable(TS ts, Vec U, Vec C)
1759d71ae5a4SJacob Faibussowitsch {
1760e3c11fc1SJed Brown   DM   dm;
1761e3c11fc1SJed Brown   DMTS dmts;
1762e3c11fc1SJed Brown 
1763e3c11fc1SJed Brown   PetscFunctionBegin;
1764e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1765e3c11fc1SJed Brown   PetscValidHeaderSpecific(U, VEC_CLASSID, 2);
17669566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
17679566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm, &dmts));
1768e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1769e3c11fc1SJed Brown     PetscValidHeaderSpecific(C, VEC_CLASSID, 3);
17709566063dSJacob Faibussowitsch     PetscCall((*dmts->ops->transientvar)(ts, U, C, dmts->transientvarctx));
1771e3c11fc1SJed Brown   }
17723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1773e3c11fc1SJed Brown }
1774e3c11fc1SJed Brown 
1775e3c11fc1SJed Brown /*@
1776e3c11fc1SJed Brown   TSHasTransientVariable - determine whether transient variables have been set
1777e3c11fc1SJed Brown 
1778e3c11fc1SJed Brown   Logically Collective
1779e3c11fc1SJed Brown 
17802fe279fdSBarry Smith   Input Parameter:
1781195e9b02SBarry Smith . ts - `TS` on which to compute
1782e3c11fc1SJed Brown 
17832fe279fdSBarry Smith   Output Parameter:
1784bcf0153eSBarry Smith . has - `PETSC_TRUE` if transient variables have been set
1785e3c11fc1SJed Brown 
1786e3c11fc1SJed Brown   Level: developer
1787e3c11fc1SJed Brown 
17881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeTransientVariable()`
1789e3c11fc1SJed Brown @*/
1790d71ae5a4SJacob Faibussowitsch PetscErrorCode TSHasTransientVariable(TS ts, PetscBool *has)
1791d71ae5a4SJacob Faibussowitsch {
1792e3c11fc1SJed Brown   DM   dm;
1793e3c11fc1SJed Brown   DMTS dmts;
1794e3c11fc1SJed Brown 
1795e3c11fc1SJed Brown   PetscFunctionBegin;
1796e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
17979566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
17989566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm, &dmts));
1799e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
18003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1801e3c11fc1SJed Brown }
1802e3c11fc1SJed Brown 
1803e3c11fc1SJed Brown /*@
1804efe9872eSLisandro Dalcin   TS2SetSolution - Sets the initial solution and time derivative vectors
1805bcf0153eSBarry Smith   for use by the `TS` routines handling second order equations.
1806efe9872eSLisandro Dalcin 
1807c3339decSBarry Smith   Logically Collective
1808efe9872eSLisandro Dalcin 
1809efe9872eSLisandro Dalcin   Input Parameters:
1810bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()`
1811efe9872eSLisandro Dalcin . u  - the solution vector
1812efe9872eSLisandro Dalcin - v  - the time derivative vector
1813efe9872eSLisandro Dalcin 
1814efe9872eSLisandro Dalcin   Level: beginner
1815efe9872eSLisandro Dalcin 
18161cc06b55SBarry Smith .seealso: [](ch_ts), `TS`
1817efe9872eSLisandro Dalcin @*/
1818d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2SetSolution(TS ts, Vec u, Vec v)
1819d71ae5a4SJacob Faibussowitsch {
1820efe9872eSLisandro Dalcin   PetscFunctionBegin;
1821efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1822efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
1823efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
18249566063dSJacob Faibussowitsch   PetscCall(TSSetSolution(ts, u));
18259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)v));
18269566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_dot));
1827efe9872eSLisandro Dalcin   ts->vec_dot = v;
18283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1829efe9872eSLisandro Dalcin }
1830efe9872eSLisandro Dalcin 
1831efe9872eSLisandro Dalcin /*@
1832efe9872eSLisandro Dalcin   TS2GetSolution - Returns the solution and time derivative at the present timestep
1833efe9872eSLisandro Dalcin   for second order equations. It is valid to call this routine inside the function
1834efe9872eSLisandro Dalcin   that you are evaluating in order to move to the new timestep. This vector not
1835efe9872eSLisandro Dalcin   changed until the solution at the next timestep has been calculated.
1836efe9872eSLisandro Dalcin 
1837195e9b02SBarry Smith   Not Collective, but `u` returned is parallel if `TS` is parallel
1838efe9872eSLisandro Dalcin 
1839efe9872eSLisandro Dalcin   Input Parameter:
1840bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
1841efe9872eSLisandro Dalcin 
1842d8d19677SJose E. Roman   Output Parameters:
1843efe9872eSLisandro Dalcin + u - the vector containing the solution
1844efe9872eSLisandro Dalcin - v - the vector containing the time derivative
1845efe9872eSLisandro Dalcin 
1846efe9872eSLisandro Dalcin   Level: intermediate
1847efe9872eSLisandro Dalcin 
18481cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TS2SetSolution()`, `TSGetTimeStep()`, `TSGetTime()`
1849efe9872eSLisandro Dalcin @*/
1850d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2GetSolution(TS ts, Vec *u, Vec *v)
1851d71ae5a4SJacob Faibussowitsch {
1852efe9872eSLisandro Dalcin   PetscFunctionBegin;
1853efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1854efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u, 2);
1855efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v, 3);
1856efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1857efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
18583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1859efe9872eSLisandro Dalcin }
1860efe9872eSLisandro Dalcin 
186155849f57SBarry Smith /*@C
1862bcf0153eSBarry Smith   TSLoad - Loads a `TS` that has been stored in binary  with `TSView()`.
186355849f57SBarry Smith 
1864c3339decSBarry Smith   Collective
186555849f57SBarry Smith 
186655849f57SBarry Smith   Input Parameters:
1867b43aa488SJacob Faibussowitsch + ts     - the newly loaded `TS`, this needs to have been created with `TSCreate()` or
1868bcf0153eSBarry Smith            some related function before a call to `TSLoad()`.
1869bcf0153eSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()`
187055849f57SBarry Smith 
187155849f57SBarry Smith   Level: intermediate
187255849f57SBarry Smith 
1873bcf0153eSBarry Smith   Note:
1874bcf0153eSBarry Smith   The type is determined by the data in the file, any type set into the `TS` before this call is ignored.
187555849f57SBarry Smith 
18761cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerBinaryOpen()`, `TSView()`, `MatLoad()`, `VecLoad()`
187755849f57SBarry Smith @*/
1878d71ae5a4SJacob Faibussowitsch PetscErrorCode TSLoad(TS ts, PetscViewer viewer)
1879d71ae5a4SJacob Faibussowitsch {
188055849f57SBarry Smith   PetscBool isbinary;
188155849f57SBarry Smith   PetscInt  classid;
188255849f57SBarry Smith   char      type[256];
18832d53ad75SBarry Smith   DMTS      sdm;
1884ad6bc421SBarry Smith   DM        dm;
188555849f57SBarry Smith 
188655849f57SBarry Smith   PetscFunctionBegin;
1887f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
188855849f57SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
18899566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
18903c633725SBarry Smith   PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()");
189155849f57SBarry Smith 
18929566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
18933c633725SBarry Smith   PetscCheck(classid == TS_FILE_CLASSID, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Not TS next in file");
18949566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
18959566063dSJacob Faibussowitsch   PetscCall(TSSetType(ts, type));
1896dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, load, viewer);
18979566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)ts), &dm));
18989566063dSJacob Faibussowitsch   PetscCall(DMLoad(dm, viewer));
18999566063dSJacob Faibussowitsch   PetscCall(TSSetDM(ts, dm));
19009566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol));
19019566063dSJacob Faibussowitsch   PetscCall(VecLoad(ts->vec_sol, viewer));
19029566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(ts->dm, &sdm));
19039566063dSJacob Faibussowitsch   PetscCall(DMTSLoad(sdm, viewer));
19043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
190555849f57SBarry Smith }
190655849f57SBarry Smith 
19079804daf3SBarry Smith #include <petscdraw.h>
1908e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1909e04113cfSBarry Smith   #include <petscviewersaws.h>
1910f05ece33SBarry Smith #endif
1911fe2efc57SMark 
1912fe2efc57SMark /*@C
1913bcf0153eSBarry Smith   TSViewFromOptions - View a `TS` based on values in the options database
1914fe2efc57SMark 
1915c3339decSBarry Smith   Collective
1916fe2efc57SMark 
1917fe2efc57SMark   Input Parameters:
1918bcf0153eSBarry Smith + ts   - the `TS` context
1919bcf0153eSBarry Smith . obj  - Optional object that provides the prefix for the options database keys
1920bcf0153eSBarry Smith - name - command line option string to be passed by user
1921fe2efc57SMark 
1922fe2efc57SMark   Level: intermediate
1923bcf0153eSBarry Smith 
19241cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSView`, `PetscObjectViewFromOptions()`, `TSCreate()`
1925fe2efc57SMark @*/
1926bcf0153eSBarry Smith PetscErrorCode TSViewFromOptions(TS ts, PetscObject obj, const char name[])
1927d71ae5a4SJacob Faibussowitsch {
1928fe2efc57SMark   PetscFunctionBegin;
1929bcf0153eSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
1930bcf0153eSBarry Smith   PetscCall(PetscObjectViewFromOptions((PetscObject)ts, obj, name));
19313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1932fe2efc57SMark }
1933fe2efc57SMark 
19347e2c5f70SBarry Smith /*@C
1935bcf0153eSBarry Smith   TSView - Prints the `TS` data structure.
1936d763cef2SBarry Smith 
1937c3339decSBarry Smith   Collective
1938d763cef2SBarry Smith 
1939d763cef2SBarry Smith   Input Parameters:
1940bcf0153eSBarry Smith + ts     - the `TS` context obtained from `TSCreate()`
1941d763cef2SBarry Smith - viewer - visualization context
1942d763cef2SBarry Smith 
1943d763cef2SBarry Smith   Options Database Key:
1944bcf0153eSBarry Smith . -ts_view - calls `TSView()` at end of `TSStep()`
1945bcf0153eSBarry Smith 
1946bcf0153eSBarry Smith   Level: beginner
1947d763cef2SBarry Smith 
1948d763cef2SBarry Smith   Notes:
1949d763cef2SBarry Smith   The available visualization contexts include
1950bcf0153eSBarry Smith +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
1951bcf0153eSBarry Smith -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
1952d763cef2SBarry Smith   output where only the first processor opens
1953d763cef2SBarry Smith   the file.  All other processors send their
1954d763cef2SBarry Smith   data to the first processor to print.
1955d763cef2SBarry Smith 
1956d763cef2SBarry Smith   The user can open an alternative visualization context with
1957bcf0153eSBarry Smith   `PetscViewerASCIIOpen()` - output to a specified file.
1958d763cef2SBarry Smith 
1959bcf0153eSBarry Smith   In the debugger you can do call `TSView`(ts,0) to display the `TS` solver. (The same holds for any PETSc object viewer).
1960595c91d4SBarry Smith 
19611cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerASCIIOpen()`
1962d763cef2SBarry Smith @*/
1963d71ae5a4SJacob Faibussowitsch PetscErrorCode TSView(TS ts, PetscViewer viewer)
1964d71ae5a4SJacob Faibussowitsch {
196519fd82e9SBarry Smith   TSType    type;
19662b0a91c0SBarry Smith   PetscBool iascii, isstring, isundials, isbinary, isdraw;
19672d53ad75SBarry Smith   DMTS      sdm;
1968e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1969536b137fSBarry Smith   PetscBool issaws;
1970f05ece33SBarry Smith #endif
1971d763cef2SBarry Smith 
1972d763cef2SBarry Smith   PetscFunctionBegin;
19730700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
19741e66621cSBarry Smith   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts), &viewer));
19750700a824SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1976c9780b6fSBarry Smith   PetscCheckSameComm(ts, 1, viewer, 2);
1977fd16b177SBarry Smith 
19789566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
19799566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
19809566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
19819566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1982e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
19839566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
1984f05ece33SBarry Smith #endif
198532077d6dSBarry Smith   if (iascii) {
19869566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts, viewer));
1987efd4aadfSBarry Smith     if (ts->ops->view) {
19889566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
1989dbbe0bcdSBarry Smith       PetscUseTypeMethod(ts, view, viewer);
19909566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
1991efd4aadfSBarry Smith     }
19921e66621cSBarry Smith     if (ts->max_steps < PETSC_MAX_INT) PetscCall(PetscViewerASCIIPrintf(viewer, "  maximum steps=%" PetscInt_FMT "\n", ts->max_steps));
19931e66621cSBarry Smith     if (ts->max_time < PETSC_MAX_REAL) PetscCall(PetscViewerASCIIPrintf(viewer, "  maximum time=%g\n", (double)ts->max_time));
19941e66621cSBarry Smith     if (ts->ifuncs) PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of I function evaluations=%" PetscInt_FMT "\n", ts->ifuncs));
19951e66621cSBarry Smith     if (ts->ijacs) PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of I Jacobian evaluations=%" PetscInt_FMT "\n", ts->ijacs));
19961e66621cSBarry Smith     if (ts->rhsfuncs) PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of RHS function evaluations=%" PetscInt_FMT "\n", ts->rhsfuncs));
19971e66621cSBarry Smith     if (ts->rhsjacs) PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of RHS Jacobian evaluations=%" PetscInt_FMT "\n", ts->rhsjacs));
1998efd4aadfSBarry Smith     if (ts->usessnes) {
1999efd4aadfSBarry Smith       PetscBool lin;
20001e66621cSBarry Smith       if (ts->problem_type == TS_NONLINEAR) PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of nonlinear solver iterations=%" PetscInt_FMT "\n", ts->snes_its));
200163a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of linear solver iterations=%" PetscInt_FMT "\n", ts->ksp_its));
20029566063dSJacob Faibussowitsch       PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &lin, SNESKSPONLY, SNESKSPTRANSPOSEONLY, ""));
200363a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of %slinear solve failures=%" PetscInt_FMT "\n", lin ? "" : "non", ts->num_snes_failures));
2004efd4aadfSBarry Smith     }
200563a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of rejected steps=%" PetscInt_FMT "\n", ts->reject));
20061e66621cSBarry Smith     if (ts->vrtol) PetscCall(PetscViewerASCIIPrintf(viewer, "  using vector of relative error tolerances, "));
20071e66621cSBarry Smith     else PetscCall(PetscViewerASCIIPrintf(viewer, "  using relative error tolerance of %g, ", (double)ts->rtol));
20081e66621cSBarry Smith     if (ts->vatol) PetscCall(PetscViewerASCIIPrintf(viewer, "  using vector of absolute error tolerances\n"));
20091e66621cSBarry Smith     else PetscCall(PetscViewerASCIIPrintf(viewer, "  using absolute error tolerance of %g\n", (double)ts->atol));
20109566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
20119566063dSJacob Faibussowitsch     PetscCall(TSAdaptView(ts->adapt, viewer));
20129566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
20130f5bd95cSBarry Smith   } else if (isstring) {
20149566063dSJacob Faibussowitsch     PetscCall(TSGetType(ts, &type));
20159566063dSJacob Faibussowitsch     PetscCall(PetscViewerStringSPrintf(viewer, " TSType: %-7.7s", type));
2016dbbe0bcdSBarry Smith     PetscTryTypeMethod(ts, view, viewer);
201755849f57SBarry Smith   } else if (isbinary) {
201855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
201955849f57SBarry Smith     MPI_Comm    comm;
202055849f57SBarry Smith     PetscMPIInt rank;
202155849f57SBarry Smith     char        type[256];
202255849f57SBarry Smith 
20239566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetComm((PetscObject)ts, &comm));
20249566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(comm, &rank));
2025dd400576SPatrick Sanan     if (rank == 0) {
20269566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT));
20279566063dSJacob Faibussowitsch       PetscCall(PetscStrncpy(type, ((PetscObject)ts)->type_name, 256));
20289566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer, type, 256, PETSC_CHAR));
202955849f57SBarry Smith     }
2030dbbe0bcdSBarry Smith     PetscTryTypeMethod(ts, view, viewer);
20319566063dSJacob Faibussowitsch     if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer));
20329566063dSJacob Faibussowitsch     PetscCall(DMView(ts->dm, viewer));
20339566063dSJacob Faibussowitsch     PetscCall(VecView(ts->vec_sol, viewer));
20349566063dSJacob Faibussowitsch     PetscCall(DMGetDMTS(ts->dm, &sdm));
20359566063dSJacob Faibussowitsch     PetscCall(DMTSView(sdm, viewer));
20362b0a91c0SBarry Smith   } else if (isdraw) {
20372b0a91c0SBarry Smith     PetscDraw draw;
20382b0a91c0SBarry Smith     char      str[36];
203989fd9fafSBarry Smith     PetscReal x, y, bottom, h;
20402b0a91c0SBarry Smith 
20419566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
20429566063dSJacob Faibussowitsch     PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
2043c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(str, "TS: ", sizeof(str)));
2044c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(str, ((PetscObject)ts)->type_name, sizeof(str)));
20459566063dSJacob Faibussowitsch     PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_BLACK, PETSC_DRAW_BLACK, str, NULL, &h));
204689fd9fafSBarry Smith     bottom = y - h;
20479566063dSJacob Faibussowitsch     PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom));
2048dbbe0bcdSBarry Smith     PetscTryTypeMethod(ts, view, viewer);
20499566063dSJacob Faibussowitsch     if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer));
20509566063dSJacob Faibussowitsch     if (ts->snes) PetscCall(SNESView(ts->snes, viewer));
20519566063dSJacob Faibussowitsch     PetscCall(PetscDrawPopCurrentPoint(draw));
2052e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2053536b137fSBarry Smith   } else if (issaws) {
2054d45a07a7SBarry Smith     PetscMPIInt rank;
20552657e9d9SBarry Smith     const char *name;
20562657e9d9SBarry Smith 
20579566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)ts, &name));
20589566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
2059dd400576SPatrick Sanan     if (!((PetscObject)ts)->amsmem && rank == 0) {
2060d45a07a7SBarry Smith       char dir[1024];
2061d45a07a7SBarry Smith 
20629566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)ts, viewer));
20639566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time_step", name));
2064792fecdfSBarry Smith       PetscCallSAWs(SAWs_Register, (dir, &ts->steps, 1, SAWs_READ, SAWs_INT));
20659566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time", name));
2066792fecdfSBarry Smith       PetscCallSAWs(SAWs_Register, (dir, &ts->ptime, 1, SAWs_READ, SAWs_DOUBLE));
2067d763cef2SBarry Smith     }
2068dbbe0bcdSBarry Smith     PetscTryTypeMethod(ts, view, viewer);
2069f05ece33SBarry Smith #endif
2070f05ece33SBarry Smith   }
207136a9e3b9SBarry Smith   if (ts->snes && ts->usessnes) {
20729566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
20739566063dSJacob Faibussowitsch     PetscCall(SNESView(ts->snes, viewer));
20749566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
207536a9e3b9SBarry Smith   }
20769566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(ts->dm, &sdm));
20779566063dSJacob Faibussowitsch   PetscCall(DMTSView(sdm, viewer));
2078f05ece33SBarry Smith 
20799566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushTab(viewer));
20809566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)ts, TSSUNDIALS, &isundials));
20819566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopTab(viewer));
20823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2083d763cef2SBarry Smith }
2084d763cef2SBarry Smith 
2085b07ff414SBarry Smith /*@
2086d763cef2SBarry Smith   TSSetApplicationContext - Sets an optional user-defined context for
2087d763cef2SBarry Smith   the timesteppers.
2088d763cef2SBarry Smith 
2089c3339decSBarry Smith   Logically Collective
2090d763cef2SBarry Smith 
2091d763cef2SBarry Smith   Input Parameters:
2092bcf0153eSBarry Smith + ts   - the `TS` context obtained from `TSCreate()`
2093bcf0153eSBarry Smith - usrP - user context
2094daf670e6SBarry Smith 
2095d763cef2SBarry Smith   Level: intermediate
2096d763cef2SBarry Smith 
2097b43aa488SJacob Faibussowitsch   Fortran Notes:
2098195e9b02SBarry Smith   You must write a Fortran interface definition for this
2099195e9b02SBarry Smith   function that tells Fortran the Fortran derived data type that you are passing in as the `ctx` argument.
2100bcf0153eSBarry Smith 
21011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetApplicationContext()`
2102d763cef2SBarry Smith @*/
2103d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetApplicationContext(TS ts, void *usrP)
2104d71ae5a4SJacob Faibussowitsch {
2105d763cef2SBarry Smith   PetscFunctionBegin;
21060700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2107d763cef2SBarry Smith   ts->user = usrP;
21083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2109d763cef2SBarry Smith }
2110d763cef2SBarry Smith 
2111b07ff414SBarry Smith /*@
2112d763cef2SBarry Smith   TSGetApplicationContext - Gets the user-defined context for the
2113bcf0153eSBarry Smith   timestepper that was set with `TSSetApplicationContext()`
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith   Not Collective
2116d763cef2SBarry Smith 
2117d763cef2SBarry Smith   Input Parameter:
2118bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2119d763cef2SBarry Smith 
2120d763cef2SBarry Smith   Output Parameter:
2121d763cef2SBarry Smith . usrP - user context
2122d763cef2SBarry Smith 
2123bcf0153eSBarry Smith   Level: intermediate
2124bcf0153eSBarry Smith 
2125b43aa488SJacob Faibussowitsch   Fortran Notes:
2126195e9b02SBarry Smith   You must write a Fortran interface definition for this
2127195e9b02SBarry Smith   function that tells Fortran the Fortran derived data type that you are passing in as the `ctx` argument.
2128daf670e6SBarry Smith 
21291cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetApplicationContext()`
2130d763cef2SBarry Smith @*/
2131d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetApplicationContext(TS ts, void *usrP)
2132d71ae5a4SJacob Faibussowitsch {
2133d763cef2SBarry Smith   PetscFunctionBegin;
21340700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2135e71120c6SJed Brown   *(void **)usrP = ts->user;
21363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2137d763cef2SBarry Smith }
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith /*@
2140bcf0153eSBarry Smith   TSGetStepNumber - Gets the number of time steps completed.
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith   Not Collective
2143d763cef2SBarry Smith 
2144d763cef2SBarry Smith   Input Parameter:
2145bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2146d763cef2SBarry Smith 
2147d763cef2SBarry Smith   Output Parameter:
214880275a0aSLisandro Dalcin . steps - number of steps completed so far
2149d763cef2SBarry Smith 
2150d763cef2SBarry Smith   Level: intermediate
2151d763cef2SBarry Smith 
21521cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSGetTimeStep()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`
2153d763cef2SBarry Smith @*/
2154d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepNumber(TS ts, PetscInt *steps)
2155d71ae5a4SJacob Faibussowitsch {
2156d763cef2SBarry Smith   PetscFunctionBegin;
21570700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
215880275a0aSLisandro Dalcin   PetscValidIntPointer(steps, 2);
215980275a0aSLisandro Dalcin   *steps = ts->steps;
21603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
216180275a0aSLisandro Dalcin }
216280275a0aSLisandro Dalcin 
216380275a0aSLisandro Dalcin /*@
216480275a0aSLisandro Dalcin   TSSetStepNumber - Sets the number of steps completed.
216580275a0aSLisandro Dalcin 
2166c3339decSBarry Smith   Logically Collective
216780275a0aSLisandro Dalcin 
216880275a0aSLisandro Dalcin   Input Parameters:
2169bcf0153eSBarry Smith + ts    - the `TS` context
217080275a0aSLisandro Dalcin - steps - number of steps completed so far
217180275a0aSLisandro Dalcin 
2172bcf0153eSBarry Smith   Level: developer
2173bcf0153eSBarry Smith 
2174bcf0153eSBarry Smith   Note:
2175bcf0153eSBarry Smith   For most uses of the `TS` solvers the user need not explicitly call
2176bcf0153eSBarry Smith   `TSSetStepNumber()`, as the step counter is appropriately updated in
2177bcf0153eSBarry Smith   `TSSolve()`/`TSStep()`/`TSRollBack()`. Power users may call this routine to
217880275a0aSLisandro Dalcin   reinitialize timestepping by setting the step counter to zero (and time
217980275a0aSLisandro Dalcin   to the initial time) to solve a similar problem with different initial
218080275a0aSLisandro Dalcin   conditions or parameters. Other possible use case is to continue
2181195e9b02SBarry Smith   timestepping from a previously interrupted run in such a way that `TS`
218280275a0aSLisandro Dalcin   monitors will be called with a initial nonzero step counter.
218380275a0aSLisandro Dalcin 
21841cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetStepNumber()`, `TSSetTime()`, `TSSetTimeStep()`, `TSSetSolution()`
218580275a0aSLisandro Dalcin @*/
2186d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetStepNumber(TS ts, PetscInt steps)
2187d71ae5a4SJacob Faibussowitsch {
218880275a0aSLisandro Dalcin   PetscFunctionBegin;
218980275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
219080275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts, steps, 2);
21913c633725SBarry Smith   PetscCheck(steps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Step number must be non-negative");
219280275a0aSLisandro Dalcin   ts->steps = steps;
21933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2194d763cef2SBarry Smith }
2195d763cef2SBarry Smith 
2196d763cef2SBarry Smith /*@
2197d763cef2SBarry Smith   TSSetTimeStep - Allows one to reset the timestep at any time,
2198d763cef2SBarry Smith   useful for simple pseudo-timestepping codes.
2199d763cef2SBarry Smith 
2200c3339decSBarry Smith   Logically Collective
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith   Input Parameters:
2203bcf0153eSBarry Smith + ts        - the `TS` context obtained from `TSCreate()`
2204d763cef2SBarry Smith - time_step - the size of the timestep
2205d763cef2SBarry Smith 
2206d763cef2SBarry Smith   Level: intermediate
2207d763cef2SBarry Smith 
22081cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPSEUDO`, `TSGetTimeStep()`, `TSSetTime()`
2209d763cef2SBarry Smith @*/
2210d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeStep(TS ts, PetscReal time_step)
2211d71ae5a4SJacob Faibussowitsch {
2212d763cef2SBarry Smith   PetscFunctionBegin;
22130700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2214c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts, time_step, 2);
2215d763cef2SBarry Smith   ts->time_step = time_step;
22163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2217d763cef2SBarry Smith }
2218d763cef2SBarry Smith 
2219a43b19c4SJed Brown /*@
222049354f04SShri Abhyankar   TSSetExactFinalTime - Determines whether to adapt the final time step to
222149354f04SShri Abhyankar   match the exact final time, interpolate solution to the exact final time,
2222bcf0153eSBarry Smith   or just return at the final time `TS` computed.
2223a43b19c4SJed Brown 
2224c3339decSBarry Smith   Logically Collective
2225a43b19c4SJed Brown 
2226d8d19677SJose E. Roman   Input Parameters:
2227a43b19c4SJed Brown + ts     - the time-step context
222849354f04SShri Abhyankar - eftopt - exact final time option
2229bcf0153eSBarry Smith .vb
2230bcf0153eSBarry Smith   TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2231bcf0153eSBarry Smith   TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2232bcf0153eSBarry Smith   TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2233bcf0153eSBarry Smith .ve
2234a43b19c4SJed Brown 
2235bcf0153eSBarry Smith   Options Database Key:
2236feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2237feed9e9dSBarry Smith 
2238a43b19c4SJed Brown   Level: beginner
2239a43b19c4SJed Brown 
2240bcf0153eSBarry Smith   Note:
2241bcf0153eSBarry Smith   If you use the option `TS_EXACTFINALTIME_STEPOVER` the solution may be at a very different time
2242bcf0153eSBarry Smith   then the final time you selected.
2243bcf0153eSBarry Smith 
22441cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSGetExactFinalTime()`
2245a43b19c4SJed Brown @*/
2246d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetExactFinalTime(TS ts, TSExactFinalTimeOption eftopt)
2247d71ae5a4SJacob Faibussowitsch {
2248a43b19c4SJed Brown   PetscFunctionBegin;
2249a43b19c4SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
225049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts, eftopt, 2);
225149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
22523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2253a43b19c4SJed Brown }
2254a43b19c4SJed Brown 
2255d763cef2SBarry Smith /*@
2256bcf0153eSBarry Smith   TSGetExactFinalTime - Gets the exact final time option set with `TSSetExactFinalTime()`
2257f6953c82SLisandro Dalcin 
2258f6953c82SLisandro Dalcin   Not Collective
2259f6953c82SLisandro Dalcin 
2260f6953c82SLisandro Dalcin   Input Parameter:
2261bcf0153eSBarry Smith . ts - the `TS` context
2262f6953c82SLisandro Dalcin 
2263f6953c82SLisandro Dalcin   Output Parameter:
2264f6953c82SLisandro Dalcin . eftopt - exact final time option
2265f6953c82SLisandro Dalcin 
2266f6953c82SLisandro Dalcin   Level: beginner
2267f6953c82SLisandro Dalcin 
22681cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSSetExactFinalTime()`
2269f6953c82SLisandro Dalcin @*/
2270d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetExactFinalTime(TS ts, TSExactFinalTimeOption *eftopt)
2271d71ae5a4SJacob Faibussowitsch {
2272f6953c82SLisandro Dalcin   PetscFunctionBegin;
2273f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2274f6953c82SLisandro Dalcin   PetscValidPointer(eftopt, 2);
2275f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
22763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2277f6953c82SLisandro Dalcin }
2278f6953c82SLisandro Dalcin 
2279f6953c82SLisandro Dalcin /*@
2280d763cef2SBarry Smith   TSGetTimeStep - Gets the current timestep size.
2281d763cef2SBarry Smith 
2282d763cef2SBarry Smith   Not Collective
2283d763cef2SBarry Smith 
2284d763cef2SBarry Smith   Input Parameter:
2285bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith   Output Parameter:
2288d763cef2SBarry Smith . dt - the current timestep size
2289d763cef2SBarry Smith 
2290d763cef2SBarry Smith   Level: intermediate
2291d763cef2SBarry Smith 
22921cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeStep()`, `TSGetTime()`
2293d763cef2SBarry Smith @*/
2294d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStep(TS ts, PetscReal *dt)
2295d71ae5a4SJacob Faibussowitsch {
2296d763cef2SBarry Smith   PetscFunctionBegin;
22970700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2298f7cf8827SBarry Smith   PetscValidRealPointer(dt, 2);
2299d763cef2SBarry Smith   *dt = ts->time_step;
23003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2301d763cef2SBarry Smith }
2302d763cef2SBarry Smith 
2303d8e5e3e6SSatish Balay /*@
2304d763cef2SBarry Smith   TSGetSolution - Returns the solution at the present timestep. It
2305d763cef2SBarry Smith   is valid to call this routine inside the function that you are evaluating
2306d763cef2SBarry Smith   in order to move to the new timestep. This vector not changed until
2307d763cef2SBarry Smith   the solution at the next timestep has been calculated.
2308d763cef2SBarry Smith 
2309bcf0153eSBarry Smith   Not Collective, but v returned is parallel if ts is parallel
2310d763cef2SBarry Smith 
2311d763cef2SBarry Smith   Input Parameter:
2312bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2313d763cef2SBarry Smith 
2314d763cef2SBarry Smith   Output Parameter:
2315d763cef2SBarry Smith . v - the vector containing the solution
2316d763cef2SBarry Smith 
2317d763cef2SBarry Smith   Level: intermediate
2318d763cef2SBarry Smith 
2319bcf0153eSBarry Smith   Note:
2320bcf0153eSBarry Smith   If you used `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`); this does not return the solution at the requested
2321bcf0153eSBarry Smith   final time. It returns the solution at the next timestep.
2322d763cef2SBarry Smith 
23231cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetSolutionComponents()`, `TSSetSolutionFunction()`
2324d763cef2SBarry Smith @*/
2325d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolution(TS ts, Vec *v)
2326d71ae5a4SJacob Faibussowitsch {
2327d763cef2SBarry Smith   PetscFunctionBegin;
23280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
23294482741eSBarry Smith   PetscValidPointer(v, 2);
23308737fe31SLisandro Dalcin   *v = ts->vec_sol;
23313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2332d763cef2SBarry Smith }
2333d763cef2SBarry Smith 
233403fe5f5eSDebojyoti Ghosh /*@
2335b2bf4f3aSDebojyoti Ghosh   TSGetSolutionComponents - Returns any solution components at the present
233603fe5f5eSDebojyoti Ghosh   timestep, if available for the time integration method being used.
2337b2bf4f3aSDebojyoti Ghosh   Solution components are quantities that share the same size and
233803fe5f5eSDebojyoti Ghosh   structure as the solution vector.
233903fe5f5eSDebojyoti Ghosh 
2340bcf0153eSBarry Smith   Not Collective, but v returned is parallel if ts is parallel
234103fe5f5eSDebojyoti Ghosh 
2342b43aa488SJacob Faibussowitsch   Input Parameters:
2343bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter).
2344195e9b02SBarry Smith . n  - If v is `NULL`, then the number of solution components is
2345b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
234603fe5f5eSDebojyoti Ghosh        returned in v.
2347a2b725a8SWilliam Gropp - v  - the vector containing the n-th solution component
2348195e9b02SBarry Smith        (may be `NULL` to use this function to find out
2349b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
235003fe5f5eSDebojyoti Ghosh 
23514cdd57e5SDebojyoti Ghosh   Level: advanced
235203fe5f5eSDebojyoti Ghosh 
23531cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()`
235403fe5f5eSDebojyoti Ghosh @*/
2355d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolutionComponents(TS ts, PetscInt *n, Vec *v)
2356d71ae5a4SJacob Faibussowitsch {
235703fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
235803fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2359b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
2360dbbe0bcdSBarry Smith   else PetscUseTypeMethod(ts, getsolutioncomponents, n, v);
23613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
236203fe5f5eSDebojyoti Ghosh }
236303fe5f5eSDebojyoti Ghosh 
23644cdd57e5SDebojyoti Ghosh /*@
23654cdd57e5SDebojyoti Ghosh   TSGetAuxSolution - Returns an auxiliary solution at the present
23664cdd57e5SDebojyoti Ghosh   timestep, if available for the time integration method being used.
23674cdd57e5SDebojyoti Ghosh 
2368bcf0153eSBarry Smith   Not Collective, but v returned is parallel if ts is parallel
23694cdd57e5SDebojyoti Ghosh 
2370b43aa488SJacob Faibussowitsch   Input Parameters:
2371bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter).
2372a2b725a8SWilliam Gropp - v  - the vector containing the auxiliary solution
23734cdd57e5SDebojyoti Ghosh 
23744cdd57e5SDebojyoti Ghosh   Level: intermediate
23754cdd57e5SDebojyoti Ghosh 
23761cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()`
23774cdd57e5SDebojyoti Ghosh @*/
2378d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAuxSolution(TS ts, Vec *v)
2379d71ae5a4SJacob Faibussowitsch {
23804cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23814cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2382dbbe0bcdSBarry Smith   if (ts->ops->getauxsolution) PetscUseTypeMethod(ts, getauxsolution, v);
23831e66621cSBarry Smith   else PetscCall(VecZeroEntries(*v));
23843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23854cdd57e5SDebojyoti Ghosh }
23864cdd57e5SDebojyoti Ghosh 
23874cdd57e5SDebojyoti Ghosh /*@
23884cdd57e5SDebojyoti Ghosh   TSGetTimeError - Returns the estimated error vector, if the chosen
2389bcf0153eSBarry Smith   `TSType` has an error estimation functionality and `TSSetTimeError()` was called
23904cdd57e5SDebojyoti Ghosh 
2391bcf0153eSBarry Smith   Not Collective, but v returned is parallel if ts is parallel
23929657682dSDebojyoti Ghosh 
2393b43aa488SJacob Faibussowitsch   Input Parameters:
2394bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter).
2395657c1e31SEmil Constantinescu . n  - current estimate (n=0) or previous one (n=-1)
2396a2b725a8SWilliam Gropp - v  - the vector containing the error (same size as the solution).
23974cdd57e5SDebojyoti Ghosh 
23984cdd57e5SDebojyoti Ghosh   Level: intermediate
23994cdd57e5SDebojyoti Ghosh 
2400bcf0153eSBarry Smith   Note:
2401bcf0153eSBarry Smith   MUST call after `TSSetUp()`
24024cdd57e5SDebojyoti Ghosh 
24031cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetSolution()`, `TSSetTimeError()`
24044cdd57e5SDebojyoti Ghosh @*/
2405d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeError(TS ts, PetscInt n, Vec *v)
2406d71ae5a4SJacob Faibussowitsch {
24074cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24084cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2409dbbe0bcdSBarry Smith   if (ts->ops->gettimeerror) PetscUseTypeMethod(ts, gettimeerror, n, v);
24101e66621cSBarry Smith   else PetscCall(VecZeroEntries(*v));
24113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24124cdd57e5SDebojyoti Ghosh }
24134cdd57e5SDebojyoti Ghosh 
241457df6a1bSDebojyoti Ghosh /*@
241557df6a1bSDebojyoti Ghosh   TSSetTimeError - Sets the estimated error vector, if the chosen
2416bcf0153eSBarry Smith   `TSType` has an error estimation functionality. This can be used
241757df6a1bSDebojyoti Ghosh   to restart such a time integrator with a given error vector.
241857df6a1bSDebojyoti Ghosh 
2419bcf0153eSBarry Smith   Not Collective, but v returned is parallel if ts is parallel
242057df6a1bSDebojyoti Ghosh 
2421b43aa488SJacob Faibussowitsch   Input Parameters:
2422bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter).
2423a2b725a8SWilliam Gropp - v  - the vector containing the error (same size as the solution).
242457df6a1bSDebojyoti Ghosh 
242557df6a1bSDebojyoti Ghosh   Level: intermediate
242657df6a1bSDebojyoti Ghosh 
2427b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSSetSolution()`, `TSGetTimeError()`
242857df6a1bSDebojyoti Ghosh @*/
2429d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeError(TS ts, Vec v)
2430d71ae5a4SJacob Faibussowitsch {
243157df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
243257df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
24333c633725SBarry Smith   PetscCheck(ts->setupcalled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetUp() first");
2434dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, settimeerror, v);
24353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
243657df6a1bSDebojyoti Ghosh }
243757df6a1bSDebojyoti Ghosh 
2438bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2439d8e5e3e6SSatish Balay /*@
2440bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2441d763cef2SBarry Smith 
2442bdad233fSMatthew Knepley   Not collective
2443d763cef2SBarry Smith 
2444bdad233fSMatthew Knepley   Input Parameters:
2445bcf0153eSBarry Smith + ts   - The `TS`
2446bcf0153eSBarry Smith - type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms
2447d763cef2SBarry Smith .vb
24480910c330SBarry Smith          U_t - A U = 0      (linear)
24490910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24500910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2451d763cef2SBarry Smith .ve
2452d763cef2SBarry Smith 
2453d763cef2SBarry Smith   Level: beginner
2454d763cef2SBarry Smith 
24551cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS`
2456d763cef2SBarry Smith @*/
2457d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetProblemType(TS ts, TSProblemType type)
2458d71ae5a4SJacob Faibussowitsch {
2459d763cef2SBarry Smith   PetscFunctionBegin;
24600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2461bdad233fSMatthew Knepley   ts->problem_type = type;
24629e2a6581SJed Brown   if (type == TS_LINEAR) {
24639e2a6581SJed Brown     SNES snes;
24649566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts, &snes));
24659566063dSJacob Faibussowitsch     PetscCall(SNESSetType(snes, SNESKSPONLY));
24669e2a6581SJed Brown   }
24673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2468d763cef2SBarry Smith }
2469d763cef2SBarry Smith 
2470bdad233fSMatthew Knepley /*@C
2471bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2472bdad233fSMatthew Knepley 
2473bdad233fSMatthew Knepley   Not collective
2474bdad233fSMatthew Knepley 
2475bdad233fSMatthew Knepley   Input Parameter:
2476bcf0153eSBarry Smith . ts - The `TS`
2477bdad233fSMatthew Knepley 
2478bdad233fSMatthew Knepley   Output Parameter:
2479bcf0153eSBarry Smith . type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms
2480bdad233fSMatthew Knepley .vb
2481089b2837SJed Brown          M U_t = A U
2482089b2837SJed Brown          M(t) U_t = A(t) U
2483b5abc632SBarry Smith          F(t,U,U_t)
2484bdad233fSMatthew Knepley .ve
2485bdad233fSMatthew Knepley 
2486bdad233fSMatthew Knepley   Level: beginner
2487bdad233fSMatthew Knepley 
24881cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS`
2489bdad233fSMatthew Knepley @*/
2490d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type)
2491d71ae5a4SJacob Faibussowitsch {
2492bdad233fSMatthew Knepley   PetscFunctionBegin;
24930700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
24944482741eSBarry Smith   PetscValidIntPointer(type, 2);
2495bdad233fSMatthew Knepley   *type = ts->problem_type;
24963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2497bdad233fSMatthew Knepley }
2498d763cef2SBarry Smith 
2499303a5415SBarry Smith /*
2500303a5415SBarry 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()
2501303a5415SBarry Smith */
2502d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2503d71ae5a4SJacob Faibussowitsch {
2504303a5415SBarry Smith   PetscBool isnone;
2505303a5415SBarry Smith 
2506303a5415SBarry Smith   PetscFunctionBegin;
25079566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts, &ts->adapt));
25089566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type));
2509303a5415SBarry Smith 
25109566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt, TSADAPTNONE, &isnone));
25111e66621cSBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25121e66621cSBarry Smith   else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
25133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2514303a5415SBarry Smith }
2515303a5415SBarry Smith 
2516d763cef2SBarry Smith /*@
2517303a5415SBarry Smith   TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2518d763cef2SBarry Smith 
2519c3339decSBarry Smith   Collective
2520d763cef2SBarry Smith 
2521d763cef2SBarry Smith   Input Parameter:
2522bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2523d763cef2SBarry Smith 
2524d763cef2SBarry Smith   Level: advanced
2525d763cef2SBarry Smith 
2526bcf0153eSBarry Smith   Note:
2527bcf0153eSBarry Smith   For basic use of the `TS` solvers the user need not explicitly call
2528bcf0153eSBarry Smith   `TSSetUp()`, since these actions will automatically occur during
2529bcf0153eSBarry Smith   the call to `TSStep()` or `TSSolve()`.  However, if one wishes to control this
2530bcf0153eSBarry Smith   phase separately, `TSSetUp()` should be called after `TSCreate()`
2531bcf0153eSBarry Smith   and optional routines of the form TSSetXXX(), but before `TSStep()` and `TSSolve()`.
2532bcf0153eSBarry Smith 
25331cc06b55SBarry Smith .seealso: [](ch_ts), `TSCreate()`, `TS`, `TSStep()`, `TSDestroy()`, `TSSolve()`
2534d763cef2SBarry Smith @*/
2535d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUp(TS ts)
2536d71ae5a4SJacob Faibussowitsch {
25376c6b9e74SPeter Brune   DM dm;
25386c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES, Vec, Vec, void *);
2539d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES, Vec, Mat, Mat, void *);
2540cd11d68dSLisandro Dalcin   TSIFunction   ifun;
25416c6b9e74SPeter Brune   TSIJacobian   ijac;
2542efe9872eSLisandro Dalcin   TSI2Jacobian  i2jac;
25436c6b9e74SPeter Brune   TSRHSJacobian rhsjac;
2544d763cef2SBarry Smith 
2545d763cef2SBarry Smith   PetscFunctionBegin;
25460700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
25473ba16761SJacob Faibussowitsch   if (ts->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
2548277b19d0SLisandro Dalcin 
25497adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
25509566063dSJacob Faibussowitsch     PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL));
25519566063dSJacob Faibussowitsch     PetscCall(TSSetType(ts, ifun ? TSBEULER : TSEULER));
2552d763cef2SBarry Smith   }
2553277b19d0SLisandro Dalcin 
25541a638600SStefano Zampini   if (!ts->vec_sol) {
25551e66621cSBarry Smith     PetscCheck(ts->dm, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetSolution() first");
25569566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol));
25571a638600SStefano Zampini   }
2558277b19d0SLisandro Dalcin 
25594a658b32SHong Zhang   if (ts->tspan) {
25601e66621cSBarry Smith     if (!ts->tspan->vecs_sol) PetscCall(VecDuplicateVecs(ts->vec_sol, ts->tspan->num_span_times, &ts->tspan->vecs_sol));
25614a658b32SHong Zhang   }
2562298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
25639566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs));
2564298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2565298bade4SHong Zhang   }
2566298bade4SHong Zhang 
2567cd4cee2dSHong Zhang   if (ts->quadraturets) {
25689566063dSJacob Faibussowitsch     PetscCall(TSSetUp(ts->quadraturets));
25699566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vec_costintegrand));
25709566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->quadraturets->vec_sol, &ts->vec_costintegrand));
2571cd4cee2dSHong Zhang   }
2572cd4cee2dSHong Zhang 
25739566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts, NULL, NULL, &rhsjac, NULL));
2574f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2575e1244c69SJed Brown     Mat  Amat, Pmat;
2576e1244c69SJed Brown     SNES snes;
25779566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts, &snes));
25789566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes, &Amat, &Pmat, NULL, NULL));
2579e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2580e1244c69SJed Brown      * have displaced the RHS matrix */
2581971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2582abc0d4abSBarry 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 */
25839566063dSJacob Faibussowitsch       PetscCall(MatDuplicate(ts->Arhs, MAT_COPY_VALUES, &Amat));
25849566063dSJacob Faibussowitsch       PetscCall(SNESSetJacobian(snes, Amat, NULL, NULL, NULL));
25859566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Amat));
2586e1244c69SJed Brown     }
2587971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
25889566063dSJacob Faibussowitsch       PetscCall(MatDuplicate(ts->Brhs, MAT_COPY_VALUES, &Pmat));
25899566063dSJacob Faibussowitsch       PetscCall(SNESSetJacobian(snes, NULL, Pmat, NULL, NULL));
25909566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Pmat));
2591e1244c69SJed Brown     }
2592e1244c69SJed Brown   }
25932ffb9264SLisandro Dalcin 
25949566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts, &ts->adapt));
25959566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type));
25962ffb9264SLisandro Dalcin 
2597dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, setup);
2598277b19d0SLisandro Dalcin 
25999566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTimeDefault(ts));
26002ffb9264SLisandro Dalcin 
2601a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26026c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26036c6b9e74SPeter Brune    */
26049566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
26059566063dSJacob Faibussowitsch   PetscCall(DMSNESGetFunction(dm, &func, NULL));
26061e66621cSBarry Smith   if (!func) PetscCall(DMSNESSetFunction(dm, SNESTSFormFunction, ts));
26071e66621cSBarry Smith 
2608a6772fa2SLisandro 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.
26096c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26106c6b9e74SPeter Brune    */
26119566063dSJacob Faibussowitsch   PetscCall(DMSNESGetJacobian(dm, &jac, NULL));
26129566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm, &ijac, NULL));
26139566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm, &i2jac, NULL));
26149566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm, &rhsjac, NULL));
26151e66621cSBarry Smith   if (!jac && (ijac || i2jac || rhsjac)) PetscCall(DMSNESSetJacobian(dm, SNESTSFormJacobian, ts));
2616c0517034SDebojyoti Ghosh 
2617c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2618dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, startingmethod);
2619c0517034SDebojyoti Ghosh 
2620277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
26213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2622277b19d0SLisandro Dalcin }
2623277b19d0SLisandro Dalcin 
2624f6a906c0SBarry Smith /*@
2625bcf0153eSBarry Smith   TSReset - Resets a `TS` context and removes any allocated `Vec`s and `Mat`s.
2626277b19d0SLisandro Dalcin 
2627c3339decSBarry Smith   Collective
2628277b19d0SLisandro Dalcin 
2629277b19d0SLisandro Dalcin   Input Parameter:
2630bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2631277b19d0SLisandro Dalcin 
2632277b19d0SLisandro Dalcin   Level: beginner
2633277b19d0SLisandro Dalcin 
26341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetup()`, `TSDestroy()`
2635277b19d0SLisandro Dalcin @*/
2636d71ae5a4SJacob Faibussowitsch PetscErrorCode TSReset(TS ts)
2637d71ae5a4SJacob Faibussowitsch {
26381d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit, next;
2639277b19d0SLisandro Dalcin 
2640277b19d0SLisandro Dalcin   PetscFunctionBegin;
2641277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2642b18ea86cSHong Zhang 
2643dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, reset);
26449566063dSJacob Faibussowitsch   if (ts->snes) PetscCall(SNESReset(ts->snes));
26459566063dSJacob Faibussowitsch   if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt));
2646bbd56ea5SKarl Rupp 
26479566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Arhs));
26489566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Brhs));
26499566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->Frhs));
26509566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_sol));
26519566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_dot));
26529566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vatol));
26539566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vrtol));
26549566063dSJacob Faibussowitsch   PetscCall(VecDestroyVecs(ts->nwork, &ts->work));
2655bbd56ea5SKarl Rupp 
26569566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Jacprhs));
26579566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Jacp));
26581baa6e33SBarry Smith   if (ts->forward_solve) PetscCall(TSForwardReset(ts));
2659cd4cee2dSHong Zhang   if (ts->quadraturets) {
26609566063dSJacob Faibussowitsch     PetscCall(TSReset(ts->quadraturets));
26619566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vec_costintegrand));
2662cd4cee2dSHong Zhang   }
26631d06f6b3SHong Zhang   while (ilink) {
26641d06f6b3SHong Zhang     next = ilink->next;
26659566063dSJacob Faibussowitsch     PetscCall(TSDestroy(&ilink->ts));
26669566063dSJacob Faibussowitsch     PetscCall(PetscFree(ilink->splitname));
26679566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&ilink->is));
26689566063dSJacob Faibussowitsch     PetscCall(PetscFree(ilink));
26691d06f6b3SHong Zhang     ilink = next;
267087f4e208SHong Zhang   }
26716bf673ebSJoe Pusztay   ts->tsrhssplit     = NULL;
2672545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
26734a658b32SHong Zhang   if (ts->tspan) {
26744a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan->span_times));
26754a658b32SHong Zhang     PetscCall(VecDestroyVecs(ts->tspan->num_span_times, &ts->tspan->vecs_sol));
26764a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan));
26774a658b32SHong Zhang   }
2678277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
26793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2680d763cef2SBarry Smith }
2681d763cef2SBarry Smith 
26821fb7b255SJunchao Zhang /*@C
2683d763cef2SBarry Smith   TSDestroy - Destroys the timestepper context that was created
2684bcf0153eSBarry Smith   with `TSCreate()`.
2685d763cef2SBarry Smith 
2686c3339decSBarry Smith   Collective
2687d763cef2SBarry Smith 
2688d763cef2SBarry Smith   Input Parameter:
2689bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2690d763cef2SBarry Smith 
2691d763cef2SBarry Smith   Level: beginner
2692d763cef2SBarry Smith 
26931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSSolve()`
2694d763cef2SBarry Smith @*/
2695d71ae5a4SJacob Faibussowitsch PetscErrorCode TSDestroy(TS *ts)
2696d71ae5a4SJacob Faibussowitsch {
2697d763cef2SBarry Smith   PetscFunctionBegin;
26983ba16761SJacob Faibussowitsch   if (!*ts) PetscFunctionReturn(PETSC_SUCCESS);
2699ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts, TS_CLASSID, 1);
27009371c9d4SSatish Balay   if (--((PetscObject)(*ts))->refct > 0) {
27019371c9d4SSatish Balay     *ts = NULL;
27023ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
27039371c9d4SSatish Balay   }
2704d763cef2SBarry Smith 
27059566063dSJacob Faibussowitsch   PetscCall(TSReset(*ts));
27069566063dSJacob Faibussowitsch   PetscCall(TSAdjointReset(*ts));
27071e66621cSBarry Smith   if ((*ts)->forward_solve) PetscCall(TSForwardReset(*ts));
2708*6bd3a4fdSStefano Zampini   PetscCall(TSResizeReset(*ts));
27091e66621cSBarry Smith 
2710e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
27119566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts));
2712dbbe0bcdSBarry Smith   PetscTryTypeMethod((*ts), destroy);
27136d4c513bSLisandro Dalcin 
27149566063dSJacob Faibussowitsch   PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory));
2715bc952696SBarry Smith 
27169566063dSJacob Faibussowitsch   PetscCall(TSAdaptDestroy(&(*ts)->adapt));
27179566063dSJacob Faibussowitsch   PetscCall(TSEventDestroy(&(*ts)->event));
27186427ac75SLisandro Dalcin 
27199566063dSJacob Faibussowitsch   PetscCall(SNESDestroy(&(*ts)->snes));
27209566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*ts)->dm));
27219566063dSJacob Faibussowitsch   PetscCall(TSMonitorCancel((*ts)));
27229566063dSJacob Faibussowitsch   PetscCall(TSAdjointMonitorCancel((*ts)));
27236d4c513bSLisandro Dalcin 
27249566063dSJacob Faibussowitsch   PetscCall(TSDestroy(&(*ts)->quadraturets));
27259566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(ts));
27263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2727d763cef2SBarry Smith }
2728d763cef2SBarry Smith 
2729d8e5e3e6SSatish Balay /*@
2730bcf0153eSBarry Smith   TSGetSNES - Returns the `SNES` (nonlinear solver) associated with
2731bcf0153eSBarry Smith   a `TS` (timestepper) context. Valid only for nonlinear problems.
2732d763cef2SBarry Smith 
2733bcf0153eSBarry Smith   Not Collective, but snes is parallel if ts is parallel
2734d763cef2SBarry Smith 
2735d763cef2SBarry Smith   Input Parameter:
2736bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2737d763cef2SBarry Smith 
2738d763cef2SBarry Smith   Output Parameter:
2739d763cef2SBarry Smith . snes - the nonlinear solver context
2740d763cef2SBarry Smith 
2741d763cef2SBarry Smith   Level: beginner
2742d763cef2SBarry Smith 
2743bcf0153eSBarry Smith   Notes:
2744bcf0153eSBarry Smith   The user can then directly manipulate the `SNES` context to set various
2745bcf0153eSBarry Smith   options, etc.  Likewise, the user can then extract and manipulate the
2746bcf0153eSBarry Smith   `KSP`, and `PC` contexts as well.
2747bcf0153eSBarry Smith 
2748bcf0153eSBarry Smith   `TSGetSNES()` does not work for integrators that do not use `SNES`; in
2749195e9b02SBarry Smith   this case `TSGetSNES()` returns `NULL` in `snes`.
2750bcf0153eSBarry Smith 
27511cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()`
2752d763cef2SBarry Smith @*/
2753d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNES(TS ts, SNES *snes)
2754d71ae5a4SJacob Faibussowitsch {
2755d763cef2SBarry Smith   PetscFunctionBegin;
27560700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
27574482741eSBarry Smith   PetscValidPointer(snes, 2);
2758d372ba47SLisandro Dalcin   if (!ts->snes) {
27599566063dSJacob Faibussowitsch     PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts), &ts->snes));
27609566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptions((PetscObject)ts->snes, ((PetscObject)ts)->options));
27619566063dSJacob Faibussowitsch     PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts));
27629566063dSJacob Faibussowitsch     PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes, (PetscObject)ts, 1));
27639566063dSJacob Faibussowitsch     if (ts->dm) PetscCall(SNESSetDM(ts->snes, ts->dm));
27641e66621cSBarry Smith     if (ts->problem_type == TS_LINEAR) PetscCall(SNESSetType(ts->snes, SNESKSPONLY));
2765d372ba47SLisandro Dalcin   }
2766d763cef2SBarry Smith   *snes = ts->snes;
27673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2768d763cef2SBarry Smith }
2769d763cef2SBarry Smith 
2770deb2cd25SJed Brown /*@
2771bcf0153eSBarry Smith   TSSetSNES - Set the `SNES` (nonlinear solver) to be used by the timestepping context
2772deb2cd25SJed Brown 
2773deb2cd25SJed Brown   Collective
2774deb2cd25SJed Brown 
2775d8d19677SJose E. Roman   Input Parameters:
2776bcf0153eSBarry Smith + ts   - the `TS` context obtained from `TSCreate()`
2777deb2cd25SJed Brown - snes - the nonlinear solver context
2778deb2cd25SJed Brown 
2779deb2cd25SJed Brown   Level: developer
2780deb2cd25SJed Brown 
2781bcf0153eSBarry Smith   Note:
2782bcf0153eSBarry Smith   Most users should have the `TS` created by calling `TSGetSNES()`
2783bcf0153eSBarry Smith 
27841cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()`
2785deb2cd25SJed Brown @*/
2786d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSNES(TS ts, SNES snes)
2787d71ae5a4SJacob Faibussowitsch {
2788d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES, Vec, Mat, Mat, void *);
2789deb2cd25SJed Brown 
2790deb2cd25SJed Brown   PetscFunctionBegin;
2791deb2cd25SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2792deb2cd25SJed Brown   PetscValidHeaderSpecific(snes, SNES_CLASSID, 2);
27939566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)snes));
27949566063dSJacob Faibussowitsch   PetscCall(SNESDestroy(&ts->snes));
2795bbd56ea5SKarl Rupp 
2796deb2cd25SJed Brown   ts->snes = snes;
2797bbd56ea5SKarl Rupp 
27989566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts));
27999566063dSJacob Faibussowitsch   PetscCall(SNESGetJacobian(ts->snes, NULL, NULL, &func, NULL));
28001e66621cSBarry Smith   if (func == SNESTSFormJacobian) PetscCall(SNESSetJacobian(ts->snes, NULL, NULL, SNESTSFormJacobian, ts));
28013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2802deb2cd25SJed Brown }
2803deb2cd25SJed Brown 
2804d8e5e3e6SSatish Balay /*@
2805bcf0153eSBarry Smith   TSGetKSP - Returns the `KSP` (linear solver) associated with
2806bcf0153eSBarry Smith   a `TS` (timestepper) context.
2807d763cef2SBarry Smith 
2808195e9b02SBarry Smith   Not Collective, but `ksp` is parallel if `ts` is parallel
2809d763cef2SBarry Smith 
2810d763cef2SBarry Smith   Input Parameter:
2811bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2812d763cef2SBarry Smith 
2813d763cef2SBarry Smith   Output Parameter:
281494b7f48cSBarry Smith . ksp - the nonlinear solver context
2815d763cef2SBarry Smith 
2816d763cef2SBarry Smith   Level: beginner
2817d763cef2SBarry Smith 
2818bcf0153eSBarry Smith   Notes:
2819bcf0153eSBarry Smith   The user can then directly manipulate the `KSP` context to set various
2820bcf0153eSBarry Smith   options, etc.  Likewise, the user can then extract and manipulate the
2821bcf0153eSBarry Smith   `PC` context as well.
2822bcf0153eSBarry Smith 
2823bcf0153eSBarry Smith   `TSGetKSP()` does not work for integrators that do not use `KSP`;
2824195e9b02SBarry Smith   in this case `TSGetKSP()` returns `NULL` in `ksp`.
2825bcf0153eSBarry Smith 
28261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `KSP`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()`
2827d763cef2SBarry Smith @*/
2828d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSP(TS ts, KSP *ksp)
2829d71ae5a4SJacob Faibussowitsch {
2830089b2837SJed Brown   SNES snes;
2831d372ba47SLisandro Dalcin 
2832d763cef2SBarry Smith   PetscFunctionBegin;
28330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
28344482741eSBarry Smith   PetscValidPointer(ksp, 2);
28353c633725SBarry Smith   PetscCheck(((PetscObject)ts)->type_name, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "KSP is not created yet. Call TSSetType() first");
28363c633725SBarry Smith   PetscCheck(ts->problem_type == TS_LINEAR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Linear only; use TSGetSNES()");
28379566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
28389566063dSJacob Faibussowitsch   PetscCall(SNESGetKSP(snes, ksp));
28393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2840d763cef2SBarry Smith }
2841d763cef2SBarry Smith 
2842d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2843d763cef2SBarry Smith 
2844adb62b0dSMatthew Knepley /*@
2845618ce8baSLisandro Dalcin   TSSetMaxSteps - Sets the maximum number of steps to use.
2846618ce8baSLisandro Dalcin 
2847c3339decSBarry Smith   Logically Collective
2848618ce8baSLisandro Dalcin 
2849618ce8baSLisandro Dalcin   Input Parameters:
2850bcf0153eSBarry Smith + ts       - the `TS` context obtained from `TSCreate()`
2851618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use
2852618ce8baSLisandro Dalcin 
2853bcf0153eSBarry Smith   Options Database Key:
2854618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps
2855618ce8baSLisandro Dalcin 
2856618ce8baSLisandro Dalcin   Level: intermediate
2857618ce8baSLisandro Dalcin 
2858bcf0153eSBarry Smith   Note:
2859bcf0153eSBarry Smith   The default maximum number of steps is 5000
2860bcf0153eSBarry Smith 
28611cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()`
2862618ce8baSLisandro Dalcin @*/
2863d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSteps(TS ts, PetscInt maxsteps)
2864d71ae5a4SJacob Faibussowitsch {
2865618ce8baSLisandro Dalcin   PetscFunctionBegin;
2866618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2867618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts, maxsteps, 2);
28683c633725SBarry Smith   PetscCheck(maxsteps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of steps must be non-negative");
2869618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
28703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2871618ce8baSLisandro Dalcin }
2872618ce8baSLisandro Dalcin 
2873618ce8baSLisandro Dalcin /*@
2874618ce8baSLisandro Dalcin   TSGetMaxSteps - Gets the maximum number of steps to use.
2875618ce8baSLisandro Dalcin 
2876618ce8baSLisandro Dalcin   Not Collective
2877618ce8baSLisandro Dalcin 
28782fe279fdSBarry Smith   Input Parameter:
2879bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2880618ce8baSLisandro Dalcin 
2881618ce8baSLisandro Dalcin   Output Parameter:
2882618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use
2883618ce8baSLisandro Dalcin 
2884618ce8baSLisandro Dalcin   Level: advanced
2885618ce8baSLisandro Dalcin 
28861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()`
2887618ce8baSLisandro Dalcin @*/
2888d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxSteps(TS ts, PetscInt *maxsteps)
2889d71ae5a4SJacob Faibussowitsch {
2890618ce8baSLisandro Dalcin   PetscFunctionBegin;
2891618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2892618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps, 2);
2893618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
28943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2895618ce8baSLisandro Dalcin }
2896618ce8baSLisandro Dalcin 
2897618ce8baSLisandro Dalcin /*@
2898618ce8baSLisandro Dalcin   TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2899618ce8baSLisandro Dalcin 
2900c3339decSBarry Smith   Logically Collective
2901618ce8baSLisandro Dalcin 
2902618ce8baSLisandro Dalcin   Input Parameters:
2903bcf0153eSBarry Smith + ts      - the `TS` context obtained from `TSCreate()`
2904618ce8baSLisandro Dalcin - maxtime - final time to step to
2905618ce8baSLisandro Dalcin 
2906bcf0153eSBarry Smith   Options Database Key:
2907ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime
2908618ce8baSLisandro Dalcin 
2909bcf0153eSBarry Smith   Level: intermediate
2910bcf0153eSBarry Smith 
2911618ce8baSLisandro Dalcin   Notes:
2912618ce8baSLisandro Dalcin   The default maximum time is 5.0
2913618ce8baSLisandro Dalcin 
29141cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxTime()`, `TSSetMaxSteps()`, `TSSetExactFinalTime()`
2915618ce8baSLisandro Dalcin @*/
2916d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxTime(TS ts, PetscReal maxtime)
2917d71ae5a4SJacob Faibussowitsch {
2918618ce8baSLisandro Dalcin   PetscFunctionBegin;
2919618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2920618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts, maxtime, 2);
2921618ce8baSLisandro Dalcin   ts->max_time = maxtime;
29223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2923618ce8baSLisandro Dalcin }
2924618ce8baSLisandro Dalcin 
2925618ce8baSLisandro Dalcin /*@
2926618ce8baSLisandro Dalcin   TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2927618ce8baSLisandro Dalcin 
2928618ce8baSLisandro Dalcin   Not Collective
2929618ce8baSLisandro Dalcin 
29302fe279fdSBarry Smith   Input Parameter:
2931bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
2932618ce8baSLisandro Dalcin 
2933618ce8baSLisandro Dalcin   Output Parameter:
2934618ce8baSLisandro Dalcin . maxtime - final time to step to
2935618ce8baSLisandro Dalcin 
2936618ce8baSLisandro Dalcin   Level: advanced
2937618ce8baSLisandro Dalcin 
29381cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxTime()`, `TSGetMaxSteps()`, `TSSetMaxSteps()`
2939618ce8baSLisandro Dalcin @*/
2940d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxTime(TS ts, PetscReal *maxtime)
2941d71ae5a4SJacob Faibussowitsch {
2942618ce8baSLisandro Dalcin   PetscFunctionBegin;
2943618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2944618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime, 2);
2945618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
29463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2947618ce8baSLisandro Dalcin }
2948618ce8baSLisandro Dalcin 
2949618ce8baSLisandro Dalcin /*@
2950bcf0153eSBarry Smith   TSSetInitialTimeStep - Deprecated, use `TSSetTime()` and `TSSetTimeStep()`.
2951edc382c3SSatish Balay 
2952edc382c3SSatish Balay   Level: deprecated
2953edc382c3SSatish Balay 
2954aaa6c58dSLisandro Dalcin @*/
2955d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetInitialTimeStep(TS ts, PetscReal initial_time, PetscReal time_step)
2956d71ae5a4SJacob Faibussowitsch {
2957aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2958aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
29599566063dSJacob Faibussowitsch   PetscCall(TSSetTime(ts, initial_time));
29609566063dSJacob Faibussowitsch   PetscCall(TSSetTimeStep(ts, time_step));
29613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2962aaa6c58dSLisandro Dalcin }
2963aaa6c58dSLisandro Dalcin 
2964aaa6c58dSLisandro Dalcin /*@
2965bcf0153eSBarry Smith   TSGetDuration - Deprecated, use `TSGetMaxSteps()` and `TSGetMaxTime()`.
2966edc382c3SSatish Balay 
2967edc382c3SSatish Balay   Level: deprecated
2968edc382c3SSatish Balay 
2969adb62b0dSMatthew Knepley @*/
2970d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2971d71ae5a4SJacob Faibussowitsch {
2972adb62b0dSMatthew Knepley   PetscFunctionBegin;
29730700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2974abc0a331SBarry Smith   if (maxsteps) {
29754482741eSBarry Smith     PetscValidIntPointer(maxsteps, 2);
2976adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2977adb62b0dSMatthew Knepley   }
2978abc0a331SBarry Smith   if (maxtime) {
2979064a246eSJacob Faibussowitsch     PetscValidRealPointer(maxtime, 3);
2980adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2981adb62b0dSMatthew Knepley   }
29823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2983adb62b0dSMatthew Knepley }
2984adb62b0dSMatthew Knepley 
2985d763cef2SBarry Smith /*@
2986bcf0153eSBarry Smith   TSSetDuration - Deprecated, use `TSSetMaxSteps()` and `TSSetMaxTime()`.
2987edc382c3SSatish Balay 
2988edc382c3SSatish Balay   Level: deprecated
2989edc382c3SSatish Balay 
2990d763cef2SBarry Smith @*/
2991d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDuration(TS ts, PetscInt maxsteps, PetscReal maxtime)
2992d71ae5a4SJacob Faibussowitsch {
2993d763cef2SBarry Smith   PetscFunctionBegin;
29940700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
2995c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts, maxsteps, 2);
2996064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveReal(ts, maxtime, 3);
299739b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
299813bcc0bdSJacob Faibussowitsch   if (maxtime != (PetscReal)PETSC_DEFAULT) ts->max_time = maxtime;
29993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3000d763cef2SBarry Smith }
3001d763cef2SBarry Smith 
3002d763cef2SBarry Smith /*@
3003bcf0153eSBarry Smith   TSGetTimeStepNumber - Deprecated, use `TSGetStepNumber()`.
3004edc382c3SSatish Balay 
3005edc382c3SSatish Balay   Level: deprecated
3006edc382c3SSatish Balay 
30075c5f5948SLisandro Dalcin @*/
3008d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStepNumber(TS ts, PetscInt *steps)
3009d71ae5a4SJacob Faibussowitsch {
30109371c9d4SSatish Balay   return TSGetStepNumber(ts, steps);
30119371c9d4SSatish Balay }
30125c5f5948SLisandro Dalcin 
301319eac22cSLisandro Dalcin /*@
3014bcf0153eSBarry Smith   TSGetTotalSteps - Deprecated, use `TSGetStepNumber()`.
3015edc382c3SSatish Balay 
3016edc382c3SSatish Balay   Level: deprecated
3017edc382c3SSatish Balay 
30184f4e0956SLisandro Dalcin @*/
3019d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTotalSteps(TS ts, PetscInt *steps)
3020d71ae5a4SJacob Faibussowitsch {
30219371c9d4SSatish Balay   return TSGetStepNumber(ts, steps);
30229371c9d4SSatish Balay }
30234f4e0956SLisandro Dalcin 
30244f4e0956SLisandro Dalcin /*@
3025d763cef2SBarry Smith   TSSetSolution - Sets the initial solution vector
3026bcf0153eSBarry Smith   for use by the `TS` routines.
3027d763cef2SBarry Smith 
3028c3339decSBarry Smith   Logically Collective
3029d763cef2SBarry Smith 
3030d763cef2SBarry Smith   Input Parameters:
3031bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()`
30320910c330SBarry Smith - u  - the solution vector
3033d763cef2SBarry Smith 
3034d763cef2SBarry Smith   Level: beginner
3035d763cef2SBarry Smith 
30361cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSGetSolution()`, `TSCreate()`
3037d763cef2SBarry Smith @*/
3038d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSolution(TS ts, Vec u)
3039d71ae5a4SJacob Faibussowitsch {
3040496e6a7aSJed Brown   DM dm;
30418737fe31SLisandro Dalcin 
3042d763cef2SBarry Smith   PetscFunctionBegin;
30430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
30440910c330SBarry Smith   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
30459566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)u));
30469566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_sol));
30470910c330SBarry Smith   ts->vec_sol = u;
3048bbd56ea5SKarl Rupp 
30499566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
30509566063dSJacob Faibussowitsch   PetscCall(DMShellSetGlobalVector(dm, u));
30513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3052d763cef2SBarry Smith }
3053d763cef2SBarry Smith 
3054ac226902SBarry Smith /*@C
3055000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30563f2090d5SJed Brown   called once at the beginning of each time step.
3057000e7ae3SMatthew Knepley 
3058c3339decSBarry Smith   Logically Collective
3059000e7ae3SMatthew Knepley 
3060000e7ae3SMatthew Knepley   Input Parameters:
3061bcf0153eSBarry Smith + ts   - The `TS` context obtained from `TSCreate()`
3062000e7ae3SMatthew Knepley - func - The function
3063000e7ae3SMatthew Knepley 
306420f4b53cSBarry Smith   Calling sequence of `func`:
306567b8a455SSatish Balay .vb
306620f4b53cSBarry Smith   PetscErrorCode func (TS ts)
306767b8a455SSatish Balay .ve
3068000e7ae3SMatthew Knepley 
3069000e7ae3SMatthew Knepley   Level: intermediate
3070000e7ae3SMatthew Knepley 
30711cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`, `TSStep()`, `TSRestartStep()`
3072000e7ae3SMatthew Knepley @*/
3073d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3074d71ae5a4SJacob Faibussowitsch {
3075000e7ae3SMatthew Knepley   PetscFunctionBegin;
30760700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3077ae60f76fSBarry Smith   ts->prestep = func;
30783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3079000e7ae3SMatthew Knepley }
3080000e7ae3SMatthew Knepley 
308109ee8438SJed Brown /*@
3082bcf0153eSBarry Smith   TSPreStep - Runs the user-defined pre-step function provided with `TSSetPreStep()`
30833f2090d5SJed Brown 
3084c3339decSBarry Smith   Collective
30853f2090d5SJed Brown 
30862fe279fdSBarry Smith   Input Parameter:
3087bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
30883f2090d5SJed Brown 
30893f2090d5SJed Brown   Level: developer
30903f2090d5SJed Brown 
3091bcf0153eSBarry Smith   Note:
3092bcf0153eSBarry Smith   `TSPreStep()` is typically used within time stepping implementations,
3093bcf0153eSBarry Smith   so most users would not generally call this routine themselves.
3094bcf0153eSBarry Smith 
30951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSPreStage()`, `TSPostStage()`, `TSPostStep()`
30963f2090d5SJed Brown @*/
3097d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStep(TS ts)
3098d71ae5a4SJacob Faibussowitsch {
30993f2090d5SJed Brown   PetscFunctionBegin;
31000700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3101ae60f76fSBarry Smith   if (ts->prestep) {
31025efd42a4SStefano Zampini     Vec              U;
3103ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3104ef8d1ce0SJohann Rudi     PetscBool        sameObject;
31055efd42a4SStefano Zampini     PetscObjectState sprev, spost;
31065efd42a4SStefano Zampini 
31079566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts, &U));
31089566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetId((PetscObject)U, &idprev));
31099566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U, &sprev));
311025e27a38SBarry Smith     PetscCallBack("TS callback preset", (*ts->prestep)(ts));
31119566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts, &U));
31129566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject));
31139566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U, &spost));
31149566063dSJacob Faibussowitsch     if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts));
3115312ce896SJed Brown   }
31163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31173f2090d5SJed Brown }
31183f2090d5SJed Brown 
3119b8123daeSJed Brown /*@C
3120b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3121b8123daeSJed Brown   called once at the beginning of each stage.
3122b8123daeSJed Brown 
3123c3339decSBarry Smith   Logically Collective
3124b8123daeSJed Brown 
3125b8123daeSJed Brown   Input Parameters:
3126bcf0153eSBarry Smith + ts   - The `TS` context obtained from `TSCreate()`
3127b8123daeSJed Brown - func - The function
3128b8123daeSJed Brown 
312920f4b53cSBarry Smith   Calling sequence of `func`:
313067b8a455SSatish Balay .vb
313120f4b53cSBarry Smith   PetscErrorCode func(TS ts, PetscReal stagetime)
313267b8a455SSatish Balay .ve
3133b8123daeSJed Brown 
3134b8123daeSJed Brown   Level: intermediate
3135b8123daeSJed Brown 
3136b8123daeSJed Brown   Note:
3137b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3138bcf0153eSBarry Smith   The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being
3139bcf0153eSBarry Smith   attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`.
3140b8123daeSJed Brown 
31411cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()`
3142b8123daeSJed Brown @*/
3143d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS, PetscReal))
3144d71ae5a4SJacob Faibussowitsch {
3145b8123daeSJed Brown   PetscFunctionBegin;
3146b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3147ae60f76fSBarry Smith   ts->prestage = func;
31483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3149b8123daeSJed Brown }
3150b8123daeSJed Brown 
31519be3e283SDebojyoti Ghosh /*@C
3152bcf0153eSBarry Smith   TSSetPostStage - Sets the general-purpose function, provided with `TSSetPostStep()`,
31539be3e283SDebojyoti Ghosh   called once at the end of each stage.
31549be3e283SDebojyoti Ghosh 
3155c3339decSBarry Smith   Logically Collective
31569be3e283SDebojyoti Ghosh 
31579be3e283SDebojyoti Ghosh   Input Parameters:
3158bcf0153eSBarry Smith + ts   - The `TS` context obtained from `TSCreate()`
31599be3e283SDebojyoti Ghosh - func - The function
31609be3e283SDebojyoti Ghosh 
316120f4b53cSBarry Smith   Calling sequence of `func`:
316267b8a455SSatish Balay .vb
316320f4b53cSBarry Smith   PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y)
316467b8a455SSatish Balay .ve
31659be3e283SDebojyoti Ghosh 
31669be3e283SDebojyoti Ghosh   Level: intermediate
31679be3e283SDebojyoti Ghosh 
31689be3e283SDebojyoti Ghosh   Note:
31699be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3170bcf0153eSBarry Smith   The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being
3171bcf0153eSBarry Smith   attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`.
31729be3e283SDebojyoti Ghosh 
31731cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()`
31749be3e283SDebojyoti Ghosh @*/
3175d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS, PetscReal, PetscInt, Vec *))
3176d71ae5a4SJacob Faibussowitsch {
31779be3e283SDebojyoti Ghosh   PetscFunctionBegin;
31789be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
31799be3e283SDebojyoti Ghosh   ts->poststage = func;
31803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31819be3e283SDebojyoti Ghosh }
31829be3e283SDebojyoti Ghosh 
3183c688d042SShri Abhyankar /*@C
3184c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3185c688d042SShri Abhyankar   called once at the end of each step evaluation.
3186c688d042SShri Abhyankar 
3187c3339decSBarry Smith   Logically Collective
3188c688d042SShri Abhyankar 
3189c688d042SShri Abhyankar   Input Parameters:
3190bcf0153eSBarry Smith + ts   - The `TS` context obtained from `TSCreate()`
3191c688d042SShri Abhyankar - func - The function
3192c688d042SShri Abhyankar 
319320f4b53cSBarry Smith   Calling sequence of `func`:
319467b8a455SSatish Balay .vb
319520f4b53cSBarry Smith   PetscErrorCode func(TS ts)
319667b8a455SSatish Balay .ve
3197c688d042SShri Abhyankar 
3198c688d042SShri Abhyankar   Level: intermediate
3199c688d042SShri Abhyankar 
3200c688d042SShri Abhyankar   Note:
3201bcf0153eSBarry Smith   Semantically, `TSSetPostEvaluate()` differs from `TSSetPostStep()` since the function it sets is called before event-handling
3202bcf0153eSBarry Smith   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, `TSPostStep()`
3203bcf0153eSBarry Smith   may be passed a different solution, possibly changed by the event handler. `TSPostEvaluate()` is called after the next step
3204bcf0153eSBarry Smith   solution is evaluated allowing to modify it, if need be. The solution can be obtained with `TSGetSolution()`, the time step
3205bcf0153eSBarry Smith   with `TSGetTimeStep()`, and the time at the start of the step is available via `TSGetTime()`
3206c688d042SShri Abhyankar 
32071cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()`
3208c688d042SShri Abhyankar @*/
3209d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3210d71ae5a4SJacob Faibussowitsch {
3211c688d042SShri Abhyankar   PetscFunctionBegin;
3212c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3213c688d042SShri Abhyankar   ts->postevaluate = func;
32143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3215c688d042SShri Abhyankar }
3216c688d042SShri Abhyankar 
3217b8123daeSJed Brown /*@
3218bcf0153eSBarry Smith   TSPreStage - Runs the user-defined pre-stage function set using `TSSetPreStage()`
3219b8123daeSJed Brown 
3220c3339decSBarry Smith   Collective
3221b8123daeSJed Brown 
3222b8123daeSJed Brown   Input Parameters:
3223bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
32249be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3225b8123daeSJed Brown 
3226b8123daeSJed Brown   Level: developer
3227b8123daeSJed Brown 
3228bcf0153eSBarry Smith   Note:
3229bcf0153eSBarry Smith   `TSPreStage()` is typically used within time stepping implementations,
3230bcf0153eSBarry Smith   most users would not generally call this routine themselves.
3231bcf0153eSBarry Smith 
32321cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPostStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()`
3233b8123daeSJed Brown @*/
3234d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStage(TS ts, PetscReal stagetime)
3235d71ae5a4SJacob Faibussowitsch {
3236b8123daeSJed Brown   PetscFunctionBegin;
3237b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
32381e66621cSBarry Smith   if (ts->prestage) PetscCallBack("TS callback prestage", (*ts->prestage)(ts, stagetime));
32393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3240b8123daeSJed Brown }
3241b8123daeSJed Brown 
32429be3e283SDebojyoti Ghosh /*@
3243bcf0153eSBarry Smith   TSPostStage - Runs the user-defined post-stage function set using `TSSetPostStage()`
32449be3e283SDebojyoti Ghosh 
3245c3339decSBarry Smith   Collective
32469be3e283SDebojyoti Ghosh 
32479be3e283SDebojyoti Ghosh   Input Parameters:
3248bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
32499be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32509be3e283SDebojyoti Ghosh   stageindex  - Stage number
32519be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32529be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32539be3e283SDebojyoti Ghosh 
32549be3e283SDebojyoti Ghosh   Level: developer
32559be3e283SDebojyoti Ghosh 
3256bcf0153eSBarry Smith   Note:
3257bcf0153eSBarry Smith   `TSPostStage()` is typically used within time stepping implementations,
3258bcf0153eSBarry Smith   most users would not generally call this routine themselves.
3259bcf0153eSBarry Smith 
32601cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPreStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()`
32619be3e283SDebojyoti Ghosh @*/
3262d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
3263d71ae5a4SJacob Faibussowitsch {
32649be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32659be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
32661e66621cSBarry Smith   if (ts->poststage) PetscCallBack("TS callback poststage", (*ts->poststage)(ts, stagetime, stageindex, Y));
32673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32689be3e283SDebojyoti Ghosh }
32699be3e283SDebojyoti Ghosh 
3270c688d042SShri Abhyankar /*@
3271bcf0153eSBarry Smith   TSPostEvaluate - Runs the user-defined post-evaluate function set using `TSSetPostEvaluate()`
3272c688d042SShri Abhyankar 
3273c3339decSBarry Smith   Collective
3274c688d042SShri Abhyankar 
32752fe279fdSBarry Smith   Input Parameter:
3276bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
3277c688d042SShri Abhyankar 
3278c688d042SShri Abhyankar   Level: developer
3279c688d042SShri Abhyankar 
3280bcf0153eSBarry Smith   Note:
3281bcf0153eSBarry Smith   `TSPostEvaluate()` is typically used within time stepping implementations,
3282bcf0153eSBarry Smith   most users would not generally call this routine themselves.
3283bcf0153eSBarry Smith 
32841cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostEvaluate()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()`
3285c688d042SShri Abhyankar @*/
3286d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostEvaluate(TS ts)
3287d71ae5a4SJacob Faibussowitsch {
3288c688d042SShri Abhyankar   PetscFunctionBegin;
3289c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3290c688d042SShri Abhyankar   if (ts->postevaluate) {
3291dcb233daSLisandro Dalcin     Vec              U;
3292dcb233daSLisandro Dalcin     PetscObjectState sprev, spost;
3293dcb233daSLisandro Dalcin 
32949566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts, &U));
32959566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U, &sprev));
329625e27a38SBarry Smith     PetscCallBack("TS callback postevaluate", (*ts->postevaluate)(ts));
32979566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U, &spost));
32989566063dSJacob Faibussowitsch     if (sprev != spost) PetscCall(TSRestartStep(ts));
3299c688d042SShri Abhyankar   }
33003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3301c688d042SShri Abhyankar }
3302c688d042SShri Abhyankar 
3303ac226902SBarry Smith /*@C
3304000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
33053f2090d5SJed Brown   called once at the end of each time step.
3306000e7ae3SMatthew Knepley 
3307c3339decSBarry Smith   Logically Collective
3308000e7ae3SMatthew Knepley 
3309000e7ae3SMatthew Knepley   Input Parameters:
3310bcf0153eSBarry Smith + ts   - The `TS` context obtained from `TSCreate()`
3311000e7ae3SMatthew Knepley - func - The function
3312000e7ae3SMatthew Knepley 
331320f4b53cSBarry Smith   Calling sequence of `func`:
331420f4b53cSBarry Smith $ PetscErrorCode func(TS ts)
3315000e7ae3SMatthew Knepley 
3316000e7ae3SMatthew Knepley   Level: intermediate
3317000e7ae3SMatthew Knepley 
3318bcf0153eSBarry Smith   Note:
3319195e9b02SBarry Smith   The function set by `TSSetPostStep()` is called after each successful step. The solution vector
3320bcf0153eSBarry Smith   obtained by `TSGetSolution()` may be different than that computed at the step end if the event handler
3321bcf0153eSBarry Smith   locates an event and `TSPostEvent()` modifies it. Use `TSSetPostEvaluate()` if an unmodified solution is needed instead.
3322bcf0153eSBarry Smith 
33231cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSRestartStep()`
3324000e7ae3SMatthew Knepley @*/
3325d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3326d71ae5a4SJacob Faibussowitsch {
3327000e7ae3SMatthew Knepley   PetscFunctionBegin;
33280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3329ae60f76fSBarry Smith   ts->poststep = func;
33303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3331000e7ae3SMatthew Knepley }
3332000e7ae3SMatthew Knepley 
333309ee8438SJed Brown /*@
3334195e9b02SBarry Smith   TSPostStep - Runs the user-defined post-step function that was set with `TSSetPostStep()`
33353f2090d5SJed Brown 
3336c3339decSBarry Smith   Collective
33373f2090d5SJed Brown 
33382fe279fdSBarry Smith   Input Parameter:
3339bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
33403f2090d5SJed Brown 
3341bcf0153eSBarry Smith   Note:
3342bcf0153eSBarry Smith   `TSPostStep()` is typically used within time stepping implementations,
33433f2090d5SJed Brown   so most users would not generally call this routine themselves.
33443f2090d5SJed Brown 
33453f2090d5SJed Brown   Level: developer
33463f2090d5SJed Brown 
33471cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSSetPotsStep()`
33483f2090d5SJed Brown @*/
3349d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStep(TS ts)
3350d71ae5a4SJacob Faibussowitsch {
33513f2090d5SJed Brown   PetscFunctionBegin;
33520700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3353ae60f76fSBarry Smith   if (ts->poststep) {
33545efd42a4SStefano Zampini     Vec              U;
3355ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3356ef8d1ce0SJohann Rudi     PetscBool        sameObject;
33575efd42a4SStefano Zampini     PetscObjectState sprev, spost;
33585efd42a4SStefano Zampini 
33599566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts, &U));
33609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetId((PetscObject)U, &idprev));
33619566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U, &sprev));
336225e27a38SBarry Smith     PetscCallBack("TS callback poststep", (*ts->poststep)(ts));
33639566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts, &U));
33649566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject));
33659566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U, &spost));
33669566063dSJacob Faibussowitsch     if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts));
336772ac3e02SJed Brown   }
33683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
33693f2090d5SJed Brown }
33703f2090d5SJed Brown 
3371cd652676SJed Brown /*@
3372cd652676SJed Brown   TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3373cd652676SJed Brown 
3374c3339decSBarry Smith   Collective
3375cd652676SJed Brown 
33764165533cSJose E. Roman   Input Parameters:
3377cd652676SJed Brown + ts - time stepping context
3378cd652676SJed Brown - t  - time to interpolate to
3379cd652676SJed Brown 
33804165533cSJose E. Roman   Output Parameter:
33810910c330SBarry Smith . U - state at given time
3382cd652676SJed Brown 
3383cd652676SJed Brown   Level: intermediate
3384cd652676SJed Brown 
3385b43aa488SJacob Faibussowitsch   Developer Notes:
3386bcf0153eSBarry Smith   `TSInterpolate()` and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3387cd652676SJed Brown 
33881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetExactFinalTime()`, `TSSolve()`
3389cd652676SJed Brown @*/
3390d71ae5a4SJacob Faibussowitsch PetscErrorCode TSInterpolate(TS ts, PetscReal t, Vec U)
3391d71ae5a4SJacob Faibussowitsch {
3392cd652676SJed Brown   PetscFunctionBegin;
3393cd652676SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3394b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
339563a3b9bcSJacob Faibussowitsch   PetscCheck(t >= ts->ptime_prev && t <= ts->ptime, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Requested time %g not in last time steps [%g,%g]", (double)t, (double)ts->ptime_prev, (double)ts->ptime);
3396dbbe0bcdSBarry Smith   PetscUseTypeMethod(ts, interpolate, t, U);
33973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3398cd652676SJed Brown }
3399cd652676SJed Brown 
3400d763cef2SBarry Smith /*@
34016d9e5789SSean Farley   TSStep - Steps one time step
3402d763cef2SBarry Smith 
3403c3339decSBarry Smith   Collective
3404d763cef2SBarry Smith 
3405d763cef2SBarry Smith   Input Parameter:
3406bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
3407d763cef2SBarry Smith 
340827829d71SBarry Smith   Level: developer
3409d763cef2SBarry Smith 
3410b8123daeSJed Brown   Notes:
3411bcf0153eSBarry Smith   The public interface for the ODE/DAE solvers is `TSSolve()`, you should almost for sure be using that routine and not this routine.
341227829d71SBarry Smith 
3413bcf0153eSBarry Smith   The hook set using `TSSetPreStep()` is called before each attempt to take the step. In general, the time step size may
3414b8123daeSJed Brown   be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3415b8123daeSJed Brown 
3416bcf0153eSBarry Smith   This may over-step the final time provided in `TSSetMaxTime()` depending on the time-step used. `TSSolve()` interpolates to exactly the
3417bcf0153eSBarry Smith   time provided in `TSSetMaxTime()`. One can use `TSInterpolate()` to determine an interpolated solution within the final timestep.
341825cb2221SBarry Smith 
34191cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSInterpolate()`
3420d763cef2SBarry Smith @*/
3421d71ae5a4SJacob Faibussowitsch PetscErrorCode TSStep(TS ts)
3422d71ae5a4SJacob Faibussowitsch {
3423fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3424be5899b3SLisandro Dalcin   PetscReal        ptime;
3425d763cef2SBarry Smith 
3426d763cef2SBarry Smith   PetscFunctionBegin;
34270700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3428d0609cedSBarry Smith   PetscCall(PetscCitationsRegister("@article{tspaper,\n"
3429fffbeea8SBarry Smith                                    "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3430f1d62c27SHong Zhang                                    "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3431f1d62c27SHong Zhang                                    "  journal       = {arXiv e-preprints},\n"
3432f1d62c27SHong Zhang                                    "  eprint        = {1806.01437},\n"
3433f1d62c27SHong Zhang                                    "  archivePrefix = {arXiv},\n"
34349371c9d4SSatish Balay                                    "  year          = {2018}\n}\n",
34359371c9d4SSatish Balay                                    &cite));
34369566063dSJacob Faibussowitsch   PetscCall(TSSetUp(ts));
34379566063dSJacob Faibussowitsch   PetscCall(TSTrajectorySetUp(ts->trajectory, ts));
3438d405a339SMatthew Knepley 
34393c633725SBarry Smith   PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_MAX_INT, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
34403c633725SBarry Smith   PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_UNSPECIFIED, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()");
34413c633725SBarry Smith   PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP || ts->adapt, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
3442a6772fa2SLisandro Dalcin 
3443be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
34449371c9d4SSatish Balay   ptime                   = ts->ptime;
34459371c9d4SSatish Balay   ts->ptime_prev_rollback = ts->ptime_prev;
34462808aa04SLisandro Dalcin   ts->reason              = TS_CONVERGED_ITERATING;
3447fc8dbba5SLisandro Dalcin 
34489566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_Step, ts, 0, 0, 0));
3449dbbe0bcdSBarry Smith   PetscUseTypeMethod(ts, step);
34509566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_Step, ts, 0, 0, 0));
3451fc8dbba5SLisandro Dalcin 
34529371c9d4SSatish Balay   if (ts->tspan && PetscIsCloseAtTol(ts->ptime, ts->tspan->span_times[ts->tspan->spanctr], ts->tspan->reltol * ts->time_step + ts->tspan->abstol, 0) && ts->tspan->spanctr < ts->tspan->num_span_times)
34539371c9d4SSatish Balay     PetscCall(VecCopy(ts->vec_sol, ts->tspan->vecs_sol[ts->tspan->spanctr++]));
3454fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3455be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
34562808aa04SLisandro Dalcin     ts->steps++;
3457be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
345828d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3459d2daff3dSHong Zhang   }
3460fc8dbba5SLisandro Dalcin   if (!ts->reason) {
346108c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
346208c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
346308c7845fSBarry Smith   }
3464fc8dbba5SLisandro Dalcin 
34655c9bbc89SJed Brown   if (ts->reason < 0 && ts->errorifstepfailed) {
34665c9bbc89SJed Brown     PetscCall(TSMonitorCancel(ts));
34675c9bbc89SJed Brown     PetscCheck(ts->reason != TS_DIVERGED_NONLINEAR_SOLVE, 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]);
34685c9bbc89SJed Brown     SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_NOT_CONVERGED, "TSStep has failed due to %s", TSConvergedReasons[ts->reason]);
34695c9bbc89SJed Brown   }
34703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
347108c7845fSBarry Smith }
347208c7845fSBarry Smith 
347308c7845fSBarry Smith /*@
34747cbde773SLisandro Dalcin   TSEvaluateWLTE - Evaluate the weighted local truncation error norm
34757cbde773SLisandro Dalcin   at the end of a time step with a given order of accuracy.
34767cbde773SLisandro Dalcin 
3477c3339decSBarry Smith   Collective
34787cbde773SLisandro Dalcin 
34794165533cSJose E. Roman   Input Parameters:
34807cbde773SLisandro Dalcin + ts        - time stepping context
3481bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY`
34827cbde773SLisandro Dalcin 
348397bb3fdcSJose E. Roman   Input/Output Parameter:
3484bcf0153eSBarry Smith . order - optional, desired order for the error evaluation or `PETSC_DECIDE`;
348597bb3fdcSJose E. Roman            on output, the actual order of the error evaluation
348697bb3fdcSJose E. Roman 
348797bb3fdcSJose E. Roman   Output Parameter:
348897bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm
34897cbde773SLisandro Dalcin 
34907cbde773SLisandro Dalcin   Level: advanced
34917cbde773SLisandro Dalcin 
3492bcf0153eSBarry Smith   Note:
34937cbde773SLisandro Dalcin   If the timestepper cannot evaluate the error in a particular step
34947cbde773SLisandro Dalcin   (eg. in the first step or restart steps after event handling),
34957cbde773SLisandro Dalcin   this routine returns wlte=-1.0 .
34967cbde773SLisandro Dalcin 
34971cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()`
34987cbde773SLisandro Dalcin @*/
3499d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateWLTE(TS ts, NormType wnormtype, PetscInt *order, PetscReal *wlte)
3500d71ae5a4SJacob Faibussowitsch {
35017cbde773SLisandro Dalcin   PetscFunctionBegin;
35027cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
35037cbde773SLisandro Dalcin   PetscValidType(ts, 1);
3504064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveEnum(ts, wnormtype, 2);
35057cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order, 3);
35067cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts, *order, 3);
35077cbde773SLisandro Dalcin   PetscValidRealPointer(wlte, 4);
35083c633725SBarry Smith   PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]);
3509dbbe0bcdSBarry Smith   PetscUseTypeMethod(ts, evaluatewlte, wnormtype, order, wlte);
35103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35117cbde773SLisandro Dalcin }
35127cbde773SLisandro Dalcin 
351305175c85SJed Brown /*@
351405175c85SJed Brown   TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
351505175c85SJed Brown 
3516c3339decSBarry Smith   Collective
351705175c85SJed Brown 
35184165533cSJose E. Roman   Input Parameters:
35191c3436cfSJed Brown + ts    - time stepping context
35201c3436cfSJed Brown . order - desired order of accuracy
3521195e9b02SBarry Smith - done  - whether the step was evaluated at this order (pass `NULL` to generate an error if not available)
352205175c85SJed Brown 
35234165533cSJose E. Roman   Output Parameter:
35240910c330SBarry Smith . U - state at the end of the current step
352505175c85SJed Brown 
352605175c85SJed Brown   Level: advanced
352705175c85SJed Brown 
3528108c343cSJed Brown   Notes:
3529108c343cSJed Brown   This function cannot be called until all stages have been evaluated.
3530108c343cSJed Brown 
3531bcf0153eSBarry Smith   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.
3532bcf0153eSBarry Smith 
35331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt`
353405175c85SJed Brown @*/
3535d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateStep(TS ts, PetscInt order, Vec U, PetscBool *done)
3536d71ae5a4SJacob Faibussowitsch {
353705175c85SJed Brown   PetscFunctionBegin;
353805175c85SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
353905175c85SJed Brown   PetscValidType(ts, 1);
35400910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 3);
3541dbbe0bcdSBarry Smith   PetscUseTypeMethod(ts, evaluatestep, order, U, done);
35423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
354305175c85SJed Brown }
354405175c85SJed Brown 
3545aad739acSMatthew G. Knepley /*@C
35462e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3547aad739acSMatthew G. Knepley 
3548aad739acSMatthew G. Knepley   Not collective
3549aad739acSMatthew G. Knepley 
35504165533cSJose E. Roman   Input Parameter:
3551aad739acSMatthew G. Knepley . ts - time stepping context
3552aad739acSMatthew G. Knepley 
35534165533cSJose E. Roman   Output Parameter:
3554b43aa488SJacob Faibussowitsch . initCondition - The function which computes an initial condition
3555aad739acSMatthew G. Knepley 
355620f4b53cSBarry Smith   Calling sequence of `initCondition`:
3557bcf0153eSBarry Smith .vb
355820f4b53cSBarry Smith   PetscErrorCode initCondition(TS ts, Vec u)
3559bcf0153eSBarry Smith .ve
356020f4b53cSBarry Smith + ts - The timestepping context
356120f4b53cSBarry Smith - u  - The input vector in which the initial condition is stored
3562bcf0153eSBarry Smith 
3563aad739acSMatthew G. Knepley   Level: advanced
3564aad739acSMatthew G. Knepley 
35651cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()`
3566aad739acSMatthew G. Knepley @*/
3567d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3568d71ae5a4SJacob Faibussowitsch {
3569aad739acSMatthew G. Knepley   PetscFunctionBegin;
3570aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
35712e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
35722e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
35733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3574aad739acSMatthew G. Knepley }
3575aad739acSMatthew G. Knepley 
3576aad739acSMatthew G. Knepley /*@C
35772e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3578aad739acSMatthew G. Knepley 
3579c3339decSBarry Smith   Logically collective
3580aad739acSMatthew G. Knepley 
35814165533cSJose E. Roman   Input Parameters:
3582aad739acSMatthew G. Knepley + ts            - time stepping context
35832e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3584aad739acSMatthew G. Knepley 
358520f4b53cSBarry Smith   Calling sequence of `initCondition`:
3586a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3587a96d6ef6SBarry Smith + ts - The timestepping context
3588a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3589aad739acSMatthew G. Knepley 
3590bcf0153eSBarry Smith   Level: advanced
3591bcf0153eSBarry Smith 
35921cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()`
3593aad739acSMatthew G. Knepley @*/
3594d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3595d71ae5a4SJacob Faibussowitsch {
3596aad739acSMatthew G. Knepley   PetscFunctionBegin;
3597aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
35982e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
35992e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
36003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3601aad739acSMatthew G. Knepley }
3602aad739acSMatthew G. Knepley 
3603aad739acSMatthew G. Knepley /*@
3604bcf0153eSBarry Smith   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set with `TSSetComputeInitialCondition()`
3605aad739acSMatthew G. Knepley 
3606c3339decSBarry Smith   Collective
3607aad739acSMatthew G. Knepley 
36084165533cSJose E. Roman   Input Parameters:
3609aad739acSMatthew G. Knepley + ts - time stepping context
3610bcf0153eSBarry Smith - u  - The `Vec` to store the condition in which will be used in `TSSolve()`
3611aad739acSMatthew G. Knepley 
3612aad739acSMatthew G. Knepley   Level: advanced
3613aad739acSMatthew G. Knepley 
36141cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()`
3615aad739acSMatthew G. Knepley @*/
3616d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3617d71ae5a4SJacob Faibussowitsch {
3618aad739acSMatthew G. Knepley   PetscFunctionBegin;
3619aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3620aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3621dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, initcondition, u);
36223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3623aad739acSMatthew G. Knepley }
3624aad739acSMatthew G. Knepley 
3625aad739acSMatthew G. Knepley /*@C
3626aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3627aad739acSMatthew G. Knepley 
3628aad739acSMatthew G. Knepley   Not collective
3629aad739acSMatthew G. Knepley 
36304165533cSJose E. Roman   Input Parameter:
3631aad739acSMatthew G. Knepley . ts - time stepping context
3632aad739acSMatthew G. Knepley 
36334165533cSJose E. Roman   Output Parameter:
3634aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3635aad739acSMatthew G. Knepley 
363620f4b53cSBarry Smith   Calling sequence of `exactError`:
363720f4b53cSBarry Smith $ PetscErrorCode exactError(TS ts, Vec u, Vec e)
3638a96d6ef6SBarry Smith + ts - The timestepping context
3639a96d6ef6SBarry Smith . u  - The approximate solution vector
364020f4b53cSBarry Smith - e  - The vector in which the error is stored
3641aad739acSMatthew G. Knepley 
3642bcf0153eSBarry Smith   Level: advanced
3643bcf0153eSBarry Smith 
36441cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()`
3645aad739acSMatthew G. Knepley @*/
3646d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3647d71ae5a4SJacob Faibussowitsch {
3648aad739acSMatthew G. Knepley   PetscFunctionBegin;
3649aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3650aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3651aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
36523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3653aad739acSMatthew G. Knepley }
3654aad739acSMatthew G. Knepley 
3655aad739acSMatthew G. Knepley /*@C
3656aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3657aad739acSMatthew G. Knepley 
3658c3339decSBarry Smith   Logically collective
3659aad739acSMatthew G. Knepley 
36604165533cSJose E. Roman   Input Parameters:
3661aad739acSMatthew G. Knepley + ts         - time stepping context
3662aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3663aad739acSMatthew G. Knepley 
366420f4b53cSBarry Smith   Calling sequence of `exactError`:
3665a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3666a96d6ef6SBarry Smith + ts - The timestepping context
3667a96d6ef6SBarry Smith . u  - The approximate solution vector
366820f4b53cSBarry Smith - e  - The  vector in which the error is stored
3669aad739acSMatthew G. Knepley 
3670bcf0153eSBarry Smith   Level: advanced
3671bcf0153eSBarry Smith 
36721cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()`
3673aad739acSMatthew G. Knepley @*/
3674d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3675d71ae5a4SJacob Faibussowitsch {
3676aad739acSMatthew G. Knepley   PetscFunctionBegin;
3677aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3678f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3679aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
36803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3681aad739acSMatthew G. Knepley }
3682aad739acSMatthew G. Knepley 
3683aad739acSMatthew G. Knepley /*@
3684bcf0153eSBarry Smith   TSComputeExactError - Compute the solution error for the timestepping using the function previously set with `TSSetComputeExactError()`
3685aad739acSMatthew G. Knepley 
3686c3339decSBarry Smith   Collective
3687aad739acSMatthew G. Knepley 
36884165533cSJose E. Roman   Input Parameters:
3689aad739acSMatthew G. Knepley + ts - time stepping context
3690aad739acSMatthew G. Knepley . u  - The approximate solution
3691bcf0153eSBarry Smith - e  - The `Vec` used to store the error
3692aad739acSMatthew G. Knepley 
3693aad739acSMatthew G. Knepley   Level: advanced
3694aad739acSMatthew G. Knepley 
36951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()`
3696aad739acSMatthew G. Knepley @*/
3697d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3698d71ae5a4SJacob Faibussowitsch {
3699aad739acSMatthew G. Knepley   PetscFunctionBegin;
3700aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3701aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3702aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
3703dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, exacterror, u, e);
37043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3705aad739acSMatthew G. Knepley }
3706aad739acSMatthew G. Knepley 
3707*6bd3a4fdSStefano Zampini /*@C
3708*6bd3a4fdSStefano Zampini   TSSetResize - Sets the resize callbacks.
3709*6bd3a4fdSStefano Zampini 
3710*6bd3a4fdSStefano Zampini   Logically Collective
3711*6bd3a4fdSStefano Zampini 
3712*6bd3a4fdSStefano Zampini   Input Parameters:
3713*6bd3a4fdSStefano Zampini + ts   - The `TS` context obtained from `TSCreate()`
3714*6bd3a4fdSStefano Zampini . setup - The setup function
3715*6bd3a4fdSStefano Zampini - transfer - The transfer function
3716*6bd3a4fdSStefano Zampini 
3717*6bd3a4fdSStefano Zampini   Calling sequence of `setup`:
3718*6bd3a4fdSStefano Zampini $   PetscErrorCode setup(TS ts, PetscInt step, PetscReal time, Vec state, PetscBool *resize, void *ctx)
3719*6bd3a4fdSStefano Zampini +   ts - the TS context
3720*6bd3a4fdSStefano Zampini .   step - the current step
3721*6bd3a4fdSStefano Zampini .   time - the current time
3722*6bd3a4fdSStefano Zampini .   state - the current vector of state
3723*6bd3a4fdSStefano Zampini .   resize - (output parameter) `PETSC_TRUE` if need resizing, `PETSC_FALSE` otherwise
3724*6bd3a4fdSStefano Zampini -   ctx - user defined context
3725*6bd3a4fdSStefano Zampini 
3726*6bd3a4fdSStefano Zampini   Calling sequence of `transfer`:
3727*6bd3a4fdSStefano Zampini $   PetscErrorCode transfer(TS ts, PetscInt nv, Vec vecsin[], Vec vecsout[], void *ctx)
3728*6bd3a4fdSStefano Zampini +   ts - the TS context
3729*6bd3a4fdSStefano Zampini .   nv - the number of vectors to be transferred
3730*6bd3a4fdSStefano Zampini .   vecsin - array of vectors to be transferred
3731*6bd3a4fdSStefano Zampini .   vecsout - array of transferred vectors
3732*6bd3a4fdSStefano Zampini -   ctx - user defined context
3733*6bd3a4fdSStefano Zampini 
3734*6bd3a4fdSStefano Zampini   Notes:
3735*6bd3a4fdSStefano Zampini   The `setup` function is called inside `TSSolve()` after `TSPostStep()` at the end of each time step
3736*6bd3a4fdSStefano Zampini   to determine if the problem size has changed.
3737*6bd3a4fdSStefano Zampini   If it is the case, the solver will collect the needed vectors that need to be
3738*6bd3a4fdSStefano Zampini   transferred from the old to the new sizes using `transfer`. These vectors will include the current
3739*6bd3a4fdSStefano Zampini   solution vector, and other vectors needed by the specific solver used.
3740*6bd3a4fdSStefano Zampini   For example, `TSBDF` uses previous solutions vectors to solve for the next time step.
3741*6bd3a4fdSStefano Zampini   Other application specific objects associated with the solver, i.e. Jacobian matrices and `DM`,
3742*6bd3a4fdSStefano Zampini   will be automatically reset if the sizes are changed and they must be specified again by the user
3743*6bd3a4fdSStefano Zampini   inside the `transfer` function.
3744*6bd3a4fdSStefano Zampini   The input and output arrays passed to `transfer` are allocated by PETSc.
3745*6bd3a4fdSStefano Zampini   Vectors in `vecsout` must be created by the user.
3746*6bd3a4fdSStefano Zampini   Ownership of vectors in `vecsout` is transferred to PETSc.
3747*6bd3a4fdSStefano Zampini 
3748*6bd3a4fdSStefano Zampini   Level: advanced
3749*6bd3a4fdSStefano Zampini 
3750*6bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetDM()`, `TSSetIJacobian()`, `TSSetRHSJacobian()`
3751*6bd3a4fdSStefano Zampini @*/
3752*6bd3a4fdSStefano Zampini PetscErrorCode TSSetResize(TS ts, PetscErrorCode (*setup)(TS, PetscInt, PetscReal, Vec, PetscBool *, void *), PetscErrorCode (*transfer)(TS, PetscInt, Vec[], Vec[], void *), void *ctx)
3753*6bd3a4fdSStefano Zampini {
3754*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3755*6bd3a4fdSStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3756*6bd3a4fdSStefano Zampini   ts->resizesetup    = setup;
3757*6bd3a4fdSStefano Zampini   ts->resizetransfer = transfer;
3758*6bd3a4fdSStefano Zampini   ts->resizectx      = ctx;
3759*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3760*6bd3a4fdSStefano Zampini }
3761*6bd3a4fdSStefano Zampini 
3762*6bd3a4fdSStefano Zampini /*@C
3763*6bd3a4fdSStefano Zampini   TSResizeRegisterOrRetrieve - Register or import vectors transferred with `TSResize()`.
3764*6bd3a4fdSStefano Zampini 
3765*6bd3a4fdSStefano Zampini   Collective
3766*6bd3a4fdSStefano Zampini 
3767*6bd3a4fdSStefano Zampini   Input Parameters:
3768*6bd3a4fdSStefano Zampini + ts   - The `TS` context obtained from `TSCreate()`
3769*6bd3a4fdSStefano Zampini - flg - If `PETSC_TRUE` each TS implementation (e.g. `TSBDF`) will register vectors to be transferred, if `PETSC_FALSE` vectors will be imported from transferred vectors.
3770*6bd3a4fdSStefano Zampini 
3771*6bd3a4fdSStefano Zampini   Level: developer
3772*6bd3a4fdSStefano Zampini 
3773*6bd3a4fdSStefano Zampini   Note:
3774*6bd3a4fdSStefano Zampini   `TSResizeRegisterOrRetrieve()` is declared PETSC_INTERN since it is
3775*6bd3a4fdSStefano Zampini    used within time stepping implementations,
3776*6bd3a4fdSStefano Zampini    so most users would not generally call this routine themselves.
3777*6bd3a4fdSStefano Zampini 
3778*6bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`
3779*6bd3a4fdSStefano Zampini @*/
3780*6bd3a4fdSStefano Zampini PetscErrorCode TSResizeRegisterOrRetrieve(TS ts, PetscBool flg)
3781*6bd3a4fdSStefano Zampini {
3782*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3783*6bd3a4fdSStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3784*6bd3a4fdSStefano Zampini   PetscTryTypeMethod(ts, resizeregister, flg);
3785*6bd3a4fdSStefano Zampini   /* PetscTryTypeMethod(adapt, resizeregister, flg); */
3786*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3787*6bd3a4fdSStefano Zampini }
3788*6bd3a4fdSStefano Zampini 
3789*6bd3a4fdSStefano Zampini PetscErrorCode TSResizeReset(TS ts)
3790*6bd3a4fdSStefano Zampini {
3791*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3792*6bd3a4fdSStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3793*6bd3a4fdSStefano Zampini   PetscCall(PetscObjectListDestroy(&ts->resizetransferobjs));
3794*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3795*6bd3a4fdSStefano Zampini }
3796*6bd3a4fdSStefano Zampini 
3797*6bd3a4fdSStefano Zampini static PetscErrorCode TSResizeTransferVecs(TS ts, PetscInt cnt, Vec vecsin[], Vec vecsout[])
3798*6bd3a4fdSStefano Zampini {
3799*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3800*6bd3a4fdSStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3801*6bd3a4fdSStefano Zampini   PetscValidLogicalCollectiveInt(ts, cnt, 2);
3802*6bd3a4fdSStefano Zampini   for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPush(vecsin[i]));
3803*6bd3a4fdSStefano Zampini   if (ts->resizetransfer) {
3804*6bd3a4fdSStefano Zampini     PetscCall(PetscInfo(ts, "Transferring %" PetscInt_FMT " vectors\n", cnt));
3805*6bd3a4fdSStefano Zampini     PetscCallBack("TS callback resize transfer", (*ts->resizetransfer)(ts, cnt, vecsin, vecsout, ts->resizectx));
3806*6bd3a4fdSStefano Zampini   }
3807*6bd3a4fdSStefano Zampini   for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPop(vecsin[i]));
3808*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3809*6bd3a4fdSStefano Zampini }
3810*6bd3a4fdSStefano Zampini 
3811*6bd3a4fdSStefano Zampini /*@C
3812*6bd3a4fdSStefano Zampini   TSResizeRegisterVec - Register a vector to be transferred with `TSResize()`.
3813*6bd3a4fdSStefano Zampini 
3814*6bd3a4fdSStefano Zampini   Collective
3815*6bd3a4fdSStefano Zampini 
3816*6bd3a4fdSStefano Zampini   Input Parameters:
3817*6bd3a4fdSStefano Zampini + ts   - The `TS` context obtained from `TSCreate()`
3818*6bd3a4fdSStefano Zampini . name - A string identifiying the vector
3819*6bd3a4fdSStefano Zampini - vec - The vector
3820*6bd3a4fdSStefano Zampini 
3821*6bd3a4fdSStefano Zampini   Level: developer
3822*6bd3a4fdSStefano Zampini 
3823*6bd3a4fdSStefano Zampini   Note:
3824*6bd3a4fdSStefano Zampini   `TSResizeRegisterVec()` is typically used within time stepping implementations,
3825*6bd3a4fdSStefano Zampini   so most users would not generally call this routine themselves.
3826*6bd3a4fdSStefano Zampini 
3827*6bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRetrieveVec()`
3828*6bd3a4fdSStefano Zampini @*/
3829*6bd3a4fdSStefano Zampini PetscErrorCode TSResizeRegisterVec(TS ts, const char *name, Vec vec)
3830*6bd3a4fdSStefano Zampini {
3831*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3832*6bd3a4fdSStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3833*6bd3a4fdSStefano Zampini   PetscValidCharPointer(name, 2);
3834*6bd3a4fdSStefano Zampini   if (vec) PetscValidHeaderSpecific(vec, VEC_CLASSID, 3);
3835*6bd3a4fdSStefano Zampini   PetscCall(PetscObjectListAdd(&ts->resizetransferobjs, name, (PetscObject)vec));
3836*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3837*6bd3a4fdSStefano Zampini }
3838*6bd3a4fdSStefano Zampini 
3839*6bd3a4fdSStefano Zampini /*@C
3840*6bd3a4fdSStefano Zampini   TSResizeRetrieveVec - Retrieve a vector registered with `TSResizeRegisterVec()`.
3841*6bd3a4fdSStefano Zampini 
3842*6bd3a4fdSStefano Zampini   Collective
3843*6bd3a4fdSStefano Zampini 
3844*6bd3a4fdSStefano Zampini   Input Parameters:
3845*6bd3a4fdSStefano Zampini + ts   - The `TS` context obtained from `TSCreate()`
3846*6bd3a4fdSStefano Zampini . name - A string identifiying the vector
3847*6bd3a4fdSStefano Zampini - vec - The vector
3848*6bd3a4fdSStefano Zampini 
3849*6bd3a4fdSStefano Zampini   Level: developer
3850*6bd3a4fdSStefano Zampini 
3851*6bd3a4fdSStefano Zampini   Note:
3852*6bd3a4fdSStefano Zampini   `TSResizeRetrieveVec()` is typically used within time stepping implementations,
3853*6bd3a4fdSStefano Zampini   so most users would not generally call this routine themselves.
3854*6bd3a4fdSStefano Zampini 
3855*6bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRegisterVec()`
3856*6bd3a4fdSStefano Zampini @*/
3857*6bd3a4fdSStefano Zampini PetscErrorCode TSResizeRetrieveVec(TS ts, const char *name, Vec *vec)
3858*6bd3a4fdSStefano Zampini {
3859*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3860*6bd3a4fdSStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3861*6bd3a4fdSStefano Zampini   PetscValidCharPointer(name, 2);
3862*6bd3a4fdSStefano Zampini   PetscValidPointer(vec, 3);
3863*6bd3a4fdSStefano Zampini   PetscCall(PetscObjectListFind(ts->resizetransferobjs, name, (PetscObject *)vec));
3864*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3865*6bd3a4fdSStefano Zampini }
3866*6bd3a4fdSStefano Zampini 
3867*6bd3a4fdSStefano Zampini static PetscErrorCode TSResizeGetVecArray(TS ts, PetscInt *nv, const char **names[], Vec *vecs[])
3868*6bd3a4fdSStefano Zampini {
3869*6bd3a4fdSStefano Zampini   PetscInt        cnt;
3870*6bd3a4fdSStefano Zampini   PetscObjectList tmp;
3871*6bd3a4fdSStefano Zampini   Vec            *vecsin  = NULL;
3872*6bd3a4fdSStefano Zampini   const char    **namesin = NULL;
3873*6bd3a4fdSStefano Zampini 
3874*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3875*6bd3a4fdSStefano Zampini   for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next)
3876*6bd3a4fdSStefano Zampini     if (tmp->obj && tmp->obj->classid == VEC_CLASSID) cnt++;
3877*6bd3a4fdSStefano Zampini   if (names) PetscCall(PetscMalloc1(cnt, &vecsin));
3878*6bd3a4fdSStefano Zampini   if (vecs) PetscCall(PetscMalloc1(cnt, &namesin));
3879*6bd3a4fdSStefano Zampini   for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next) {
3880*6bd3a4fdSStefano Zampini     if (tmp->obj && tmp->obj->classid == VEC_CLASSID) {
3881*6bd3a4fdSStefano Zampini       if (vecs) vecsin[cnt] = (Vec)tmp->obj;
3882*6bd3a4fdSStefano Zampini       if (names) namesin[cnt] = tmp->name;
3883*6bd3a4fdSStefano Zampini       cnt++;
3884*6bd3a4fdSStefano Zampini     }
3885*6bd3a4fdSStefano Zampini   }
3886*6bd3a4fdSStefano Zampini   if (nv) *nv = cnt;
3887*6bd3a4fdSStefano Zampini   if (names) *names = namesin;
3888*6bd3a4fdSStefano Zampini   if (vecs) *vecs = vecsin;
3889*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3890*6bd3a4fdSStefano Zampini }
3891*6bd3a4fdSStefano Zampini 
3892*6bd3a4fdSStefano Zampini /*@
3893*6bd3a4fdSStefano Zampini   TSResize - Runs the user-defined transfer functions provided with `TSSetResize()`
3894*6bd3a4fdSStefano Zampini 
3895*6bd3a4fdSStefano Zampini   Collective
3896*6bd3a4fdSStefano Zampini 
3897*6bd3a4fdSStefano Zampini   Input Parameter:
3898*6bd3a4fdSStefano Zampini . ts   - The `TS` context obtained from `TSCreate()`
3899*6bd3a4fdSStefano Zampini 
3900*6bd3a4fdSStefano Zampini   Level: developer
3901*6bd3a4fdSStefano Zampini 
3902*6bd3a4fdSStefano Zampini   Note:
3903*6bd3a4fdSStefano Zampini   `TSResize()` is typically used within time stepping implementations,
3904*6bd3a4fdSStefano Zampini   so most users would not generally call this routine themselves.
3905*6bd3a4fdSStefano Zampini 
3906*6bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`
3907*6bd3a4fdSStefano Zampini @*/
3908*6bd3a4fdSStefano Zampini PetscErrorCode TSResize(TS ts)
3909*6bd3a4fdSStefano Zampini {
3910*6bd3a4fdSStefano Zampini   PetscInt     nv      = 0;
3911*6bd3a4fdSStefano Zampini   const char **names   = NULL;
3912*6bd3a4fdSStefano Zampini   Vec         *vecsin  = NULL;
3913*6bd3a4fdSStefano Zampini   const char  *solname = "ts:vec_sol";
3914*6bd3a4fdSStefano Zampini 
3915*6bd3a4fdSStefano Zampini   PetscFunctionBegin;
3916*6bd3a4fdSStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3917*6bd3a4fdSStefano Zampini   if (ts->resizesetup) {
3918*6bd3a4fdSStefano Zampini     PetscBool flg = PETSC_FALSE;
3919*6bd3a4fdSStefano Zampini 
3920*6bd3a4fdSStefano Zampini     PetscCall(VecLockReadPush(ts->vec_sol));
3921*6bd3a4fdSStefano Zampini     PetscCallBack("TS callback resize setup", (*ts->resizesetup)(ts, ts->steps, ts->ptime, ts->vec_sol, &flg, ts->resizectx));
3922*6bd3a4fdSStefano Zampini     PetscCall(VecLockReadPop(ts->vec_sol));
3923*6bd3a4fdSStefano Zampini     if (flg) {
3924*6bd3a4fdSStefano Zampini       PetscCall(TSResizeRegisterVec(ts, solname, ts->vec_sol));
3925*6bd3a4fdSStefano Zampini       PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_TRUE)); /* specific impls register their own objects */
3926*6bd3a4fdSStefano Zampini     }
3927*6bd3a4fdSStefano Zampini   }
3928*6bd3a4fdSStefano Zampini 
3929*6bd3a4fdSStefano Zampini   PetscCall(TSResizeGetVecArray(ts, &nv, &names, &vecsin));
3930*6bd3a4fdSStefano Zampini   if (nv) {
3931*6bd3a4fdSStefano Zampini     Vec *vecsout, vecsol;
3932*6bd3a4fdSStefano Zampini 
3933*6bd3a4fdSStefano Zampini     /* Reset internal objects */
3934*6bd3a4fdSStefano Zampini     PetscCall(TSReset(ts));
3935*6bd3a4fdSStefano Zampini 
3936*6bd3a4fdSStefano Zampini     /* Transfer needed vectors (users can call SetJacobian, SetDM here) */
3937*6bd3a4fdSStefano Zampini     PetscCall(PetscCalloc1(nv, &vecsout));
3938*6bd3a4fdSStefano Zampini     PetscCall(TSResizeTransferVecs(ts, nv, vecsin, vecsout));
3939*6bd3a4fdSStefano Zampini     for (PetscInt i = 0; i < nv; i++) {
3940*6bd3a4fdSStefano Zampini       PetscCall(TSResizeRegisterVec(ts, names[i], vecsout[i]));
3941*6bd3a4fdSStefano Zampini       PetscCall(VecDestroy(&vecsout[i]));
3942*6bd3a4fdSStefano Zampini     }
3943*6bd3a4fdSStefano Zampini     PetscCall(PetscFree(vecsout));
3944*6bd3a4fdSStefano Zampini     PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_FALSE)); /* specific impls import the transferred objects */
3945*6bd3a4fdSStefano Zampini 
3946*6bd3a4fdSStefano Zampini     PetscCall(TSResizeRetrieveVec(ts, solname, &vecsol));
3947*6bd3a4fdSStefano Zampini     if (vecsol) PetscCall(TSSetSolution(ts, vecsol));
3948*6bd3a4fdSStefano Zampini     PetscAssert(ts->vec_sol, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_NULL, "Missing TS solution");
3949*6bd3a4fdSStefano Zampini   }
3950*6bd3a4fdSStefano Zampini 
3951*6bd3a4fdSStefano Zampini   PetscCall(PetscFree(names));
3952*6bd3a4fdSStefano Zampini   PetscCall(PetscFree(vecsin));
3953*6bd3a4fdSStefano Zampini   PetscCall(TSResizeReset(ts));
3954*6bd3a4fdSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
3955*6bd3a4fdSStefano Zampini }
3956*6bd3a4fdSStefano Zampini 
3957b1cb36f3SHong Zhang /*@
39586a4d4014SLisandro Dalcin   TSSolve - Steps the requested number of timesteps.
39596a4d4014SLisandro Dalcin 
3960c3339decSBarry Smith   Collective
39616a4d4014SLisandro Dalcin 
3962d8d19677SJose E. Roman   Input Parameters:
3963bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()`
3964bcf0153eSBarry Smith - u  - the solution vector  (can be null if `TSSetSolution()` was used and `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`) was not used,
396563e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
39665a3a76d0SJed Brown 
39676a4d4014SLisandro Dalcin   Level: beginner
39686a4d4014SLisandro Dalcin 
39695a3a76d0SJed Brown   Notes:
39705a3a76d0SJed Brown   The final time returned by this function may be different from the time of the internally
3971bcf0153eSBarry Smith   held state accessible by `TSGetSolution()` and `TSGetTime()` because the method may have
39725a3a76d0SJed Brown   stepped over the final time.
39735a3a76d0SJed Brown 
39741cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()`
39756a4d4014SLisandro Dalcin @*/
3976d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSolve(TS ts, Vec u)
3977d71ae5a4SJacob Faibussowitsch {
3978b06615a5SLisandro Dalcin   Vec solution;
3979f22f69f0SBarry Smith 
39806a4d4014SLisandro Dalcin   PetscFunctionBegin;
39810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3982f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3983303a5415SBarry Smith 
39849566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTimeDefault(ts));
3985ee41a567SStefano 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 */
39860910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
39879566063dSJacob Faibussowitsch       PetscCall(VecDuplicate(u, &solution));
39889566063dSJacob Faibussowitsch       PetscCall(TSSetSolution(ts, solution));
39899566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&solution)); /* grant ownership */
39905a3a76d0SJed Brown     }
39919566063dSJacob Faibussowitsch     PetscCall(VecCopy(u, ts->vec_sol));
39923c633725SBarry Smith     PetscCheck(!ts->forward_solve, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
39931baa6e33SBarry Smith   } else if (u) PetscCall(TSSetSolution(ts, u));
39949566063dSJacob Faibussowitsch   PetscCall(TSSetUp(ts));
39959566063dSJacob Faibussowitsch   PetscCall(TSTrajectorySetUp(ts->trajectory, ts));
3996a6772fa2SLisandro Dalcin 
39973c633725SBarry Smith   PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_MAX_INT, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
39983c633725SBarry Smith   PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_UNSPECIFIED, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()");
39993c633725SBarry Smith   PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP || ts->adapt, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
40004a658b32SHong Zhang   PetscCheck(!(ts->tspan && ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP), PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "You must use TS_EXACTFINALTIME_MATCHSTEP when using time span");
40014a658b32SHong Zhang 
4002e1db57b0SHong Zhang   if (ts->tspan && PetscIsCloseAtTol(ts->ptime, ts->tspan->span_times[0], ts->tspan->reltol * ts->time_step + ts->tspan->abstol, 0)) { /* starting point in time span */
40034a658b32SHong Zhang     PetscCall(VecCopy(ts->vec_sol, ts->tspan->vecs_sol[0]));
40044a658b32SHong Zhang     ts->tspan->spanctr = 1;
40054a658b32SHong Zhang   }
4006a6772fa2SLisandro Dalcin 
40071baa6e33SBarry Smith   if (ts->forward_solve) PetscCall(TSForwardSetUp(ts));
4008715f1b00SHong Zhang 
4009e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4010715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4011e7069c78SShri      TSTrajectory to incorrectly save the output files
4012e7069c78SShri   */
4013715f1b00SHong Zhang   /* reset time step and iteration counters */
40142808aa04SLisandro Dalcin   if (!ts->steps) {
40155ef26d82SJed Brown     ts->ksp_its           = 0;
40165ef26d82SJed Brown     ts->snes_its          = 0;
4017c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4018c610991cSLisandro Dalcin     ts->reject            = 0;
40192808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40202808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40217d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
40222808aa04SLisandro Dalcin   }
4023e97c63d7SStefano Zampini 
40244a658b32SHong Zhang   /* make sure initial time step does not overshoot final time or the next point in tspan */
4025e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
40264a658b32SHong Zhang     PetscReal maxdt;
4027e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4028e97c63d7SStefano Zampini 
40294a658b32SHong Zhang     if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime;
40304a658b32SHong Zhang     else maxdt = ts->max_time - ts->ptime;
4031e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt, maxdt, 10 * PETSC_MACHINE_EPSILON, 0) ? maxdt : dt);
4032e97c63d7SStefano Zampini   }
4033193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4034193ac0bcSJed Brown 
4035900f6b5bSMatthew G. Knepley   {
4036900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4037900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4038900f6b5bSMatthew G. Knepley     PetscBool         flg;
4039900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4040900f6b5bSMatthew G. Knepley 
4041900f6b5bSMatthew G. Knepley     if (!incall) {
4042900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
40439566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts), ((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg));
4044900f6b5bSMatthew G. Knepley       if (flg) {
4045900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4046900f6b5bSMatthew G. Knepley         DM           dm;
4047900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4048900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4049f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4050900f6b5bSMatthew G. Knepley 
4051900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
40529566063dSJacob Faibussowitsch         PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg));
40539566063dSJacob Faibussowitsch         PetscCall(TSGetDM(ts, &dm));
40549566063dSJacob Faibussowitsch         PetscCall(DMGetNumFields(dm, &Nf));
40559566063dSJacob Faibussowitsch         PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha));
40569566063dSJacob Faibussowitsch         PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)ts), &conv));
40579566063dSJacob Faibussowitsch         PetscCall(PetscConvEstUseTS(conv, checkTemporal));
40589566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetSolver(conv, (PetscObject)ts));
40599566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetFromOptions(conv));
40609566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetUp(conv));
40619566063dSJacob Faibussowitsch         PetscCall(PetscConvEstGetConvRate(conv, alpha));
40629566063dSJacob Faibussowitsch         PetscCall(PetscViewerPushFormat(viewer, format));
40639566063dSJacob Faibussowitsch         PetscCall(PetscConvEstRateView(conv, alpha, viewer));
40649566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
40659566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
40669566063dSJacob Faibussowitsch         PetscCall(PetscConvEstDestroy(&conv));
40679566063dSJacob Faibussowitsch         PetscCall(PetscFree(alpha));
4068900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4069900f6b5bSMatthew G. Knepley       }
4070900f6b5bSMatthew G. Knepley     }
4071900f6b5bSMatthew G. Knepley   }
4072900f6b5bSMatthew G. Knepley 
40739566063dSJacob Faibussowitsch   PetscCall(TSViewFromOptions(ts, NULL, "-ts_view_pre"));
4074f05ece33SBarry Smith 
4075193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4076dbbe0bcdSBarry Smith     PetscUseTypeMethod(ts, solve);
40779566063dSJacob Faibussowitsch     if (u) PetscCall(VecCopy(ts->vec_sol, u));
4078cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4079a6772fa2SLisandro Dalcin     solution      = ts->vec_sol;
4080be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4081db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4082db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4083e7069c78SShri 
40842808aa04SLisandro Dalcin     if (!ts->steps) {
40859566063dSJacob Faibussowitsch       PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol));
40869566063dSJacob Faibussowitsch       PetscCall(TSEventInitialize(ts->event, ts, ts->ptime, ts->vec_sol));
40872808aa04SLisandro Dalcin     }
40886427ac75SLisandro Dalcin 
4089e1a7a14fSJed Brown     while (!ts->reason) {
40909566063dSJacob Faibussowitsch       PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol));
40911e66621cSBarry Smith       if (!ts->steprollback) PetscCall(TSPreStep(ts));
40929566063dSJacob Faibussowitsch       PetscCall(TSStep(ts));
40931baa6e33SBarry Smith       if (ts->testjacobian) PetscCall(TSRHSJacobianTest(ts, NULL));
40941baa6e33SBarry Smith       if (ts->testjacobiantranspose) PetscCall(TSRHSJacobianTestTranspose(ts, NULL));
4095cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
40967b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--;            /* Revert the step number changed by TSStep() */
40979566063dSJacob Faibussowitsch         PetscCall(TSForwardCostIntegral(ts));
40987b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4099b1cb36f3SHong Zhang       }
410058818c2dSLisandro Dalcin       if (ts->forward_solve) {            /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41017b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
41029566063dSJacob Faibussowitsch         PetscCall(TSForwardStep(ts));
41037b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4104715f1b00SHong Zhang       }
41059566063dSJacob Faibussowitsch       PetscCall(TSPostEvaluate(ts));
41069566063dSJacob Faibussowitsch       PetscCall(TSEventHandler(ts)); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
41071baa6e33SBarry Smith       if (ts->steprollback) PetscCall(TSPostEvaluate(ts));
4108e783b05fSHong Zhang       if (!ts->steprollback) {
41099566063dSJacob Faibussowitsch         PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol));
41109566063dSJacob Faibussowitsch         PetscCall(TSPostStep(ts));
4111*6bd3a4fdSStefano Zampini         PetscCall(TSResize(ts));
4112aeb4809dSShri Abhyankar       }
4113193ac0bcSJed Brown     }
41149566063dSJacob Faibussowitsch     PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol));
41156427ac75SLisandro Dalcin 
411649354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41179566063dSJacob Faibussowitsch       PetscCall(TSInterpolate(ts, ts->max_time, u));
4118cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4119b06615a5SLisandro Dalcin       solution      = u;
41209566063dSJacob Faibussowitsch       PetscCall(TSMonitor(ts, -1, ts->solvetime, solution));
41210574a7fbSJed Brown     } else {
41229566063dSJacob Faibussowitsch       if (u) PetscCall(VecCopy(ts->vec_sol, u));
4123cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4124b06615a5SLisandro Dalcin       solution      = ts->vec_sol;
41250574a7fbSJed Brown     }
4126193ac0bcSJed Brown   }
4127d2daff3dSHong Zhang 
41289566063dSJacob Faibussowitsch   PetscCall(TSViewFromOptions(ts, NULL, "-ts_view"));
41299566063dSJacob Faibussowitsch   PetscCall(VecViewFromOptions(solution, (PetscObject)ts, "-ts_view_solution"));
41309566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsBlock((PetscObject)ts));
41311baa6e33SBarry Smith   if (ts->adjoint_solve) PetscCall(TSAdjointSolve(ts));
41323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41336a4d4014SLisandro Dalcin }
41346a4d4014SLisandro Dalcin 
4135d763cef2SBarry Smith /*@
4136b8123daeSJed Brown   TSGetTime - Gets the time of the most recently completed step.
4137d763cef2SBarry Smith 
4138d763cef2SBarry Smith   Not Collective
4139d763cef2SBarry Smith 
4140d763cef2SBarry Smith   Input Parameter:
4141bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
4142d763cef2SBarry Smith 
4143d763cef2SBarry Smith   Output Parameter:
4144bcf0153eSBarry Smith . t - the current time. This time may not corresponds to the final time set with `TSSetMaxTime()`, use `TSGetSolveTime()`.
4145d763cef2SBarry Smith 
4146d763cef2SBarry Smith   Level: beginner
4147d763cef2SBarry Smith 
4148b8123daeSJed Brown   Note:
4149bcf0153eSBarry Smith   When called during time step evaluation (e.g. during residual evaluation or via hooks set using `TSSetPreStep()`,
4150bcf0153eSBarry Smith   `TSSetPreStage()`, `TSSetPostStage()`, or `TSSetPostStep()`), the time is the time at the start of the step being evaluated.
4151b8123daeSJed Brown 
4152b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, ``TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()`
4153d763cef2SBarry Smith @*/
4154d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTime(TS ts, PetscReal *t)
4155d71ae5a4SJacob Faibussowitsch {
4156d763cef2SBarry Smith   PetscFunctionBegin;
41570700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4158f7cf8827SBarry Smith   PetscValidRealPointer(t, 2);
4159d763cef2SBarry Smith   *t = ts->ptime;
41603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4161d763cef2SBarry Smith }
4162d763cef2SBarry Smith 
4163e5e524a1SHong Zhang /*@
4164e5e524a1SHong Zhang   TSGetPrevTime - Gets the starting time of the previously completed step.
4165e5e524a1SHong Zhang 
4166e5e524a1SHong Zhang   Not Collective
4167e5e524a1SHong Zhang 
4168e5e524a1SHong Zhang   Input Parameter:
4169bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
4170e5e524a1SHong Zhang 
4171e5e524a1SHong Zhang   Output Parameter:
4172e5e524a1SHong Zhang . t - the previous time
4173e5e524a1SHong Zhang 
4174e5e524a1SHong Zhang   Level: beginner
4175e5e524a1SHong Zhang 
4176b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, ``TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()`
4177e5e524a1SHong Zhang @*/
4178d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetPrevTime(TS ts, PetscReal *t)
4179d71ae5a4SJacob Faibussowitsch {
4180e5e524a1SHong Zhang   PetscFunctionBegin;
4181e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4182e5e524a1SHong Zhang   PetscValidRealPointer(t, 2);
4183e5e524a1SHong Zhang   *t = ts->ptime_prev;
41843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4185e5e524a1SHong Zhang }
4186e5e524a1SHong Zhang 
41876a4d4014SLisandro Dalcin /*@
41886a4d4014SLisandro Dalcin   TSSetTime - Allows one to reset the time.
41896a4d4014SLisandro Dalcin 
4190c3339decSBarry Smith   Logically Collective
41916a4d4014SLisandro Dalcin 
41926a4d4014SLisandro Dalcin   Input Parameters:
4193bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()`
4194b43aa488SJacob Faibussowitsch - t  - the time
41956a4d4014SLisandro Dalcin 
41966a4d4014SLisandro Dalcin   Level: intermediate
41976a4d4014SLisandro Dalcin 
41981cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSSetMaxSteps()`
41996a4d4014SLisandro Dalcin @*/
4200d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTime(TS ts, PetscReal t)
4201d71ae5a4SJacob Faibussowitsch {
42026a4d4014SLisandro Dalcin   PetscFunctionBegin;
42030700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4204c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts, t, 2);
42056a4d4014SLisandro Dalcin   ts->ptime = t;
42063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42076a4d4014SLisandro Dalcin }
42086a4d4014SLisandro Dalcin 
4209d763cef2SBarry Smith /*@C
4210d763cef2SBarry Smith   TSSetOptionsPrefix - Sets the prefix used for searching for all
4211d763cef2SBarry Smith   TS options in the database.
4212d763cef2SBarry Smith 
4213c3339decSBarry Smith   Logically Collective
4214d763cef2SBarry Smith 
4215d8d19677SJose E. Roman   Input Parameters:
4216bcf0153eSBarry Smith + ts     - The `TS` context
4217d763cef2SBarry Smith - prefix - The prefix to prepend to all option names
4218d763cef2SBarry Smith 
4219bcf0153eSBarry Smith   Level: advanced
4220bcf0153eSBarry Smith 
4221bcf0153eSBarry Smith   Note:
4222d763cef2SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
4223d763cef2SBarry Smith   The first character of all runtime options is AUTOMATICALLY the
4224d763cef2SBarry Smith   hyphen.
4225d763cef2SBarry Smith 
42261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFromOptions()`, `TSAppendOptionsPrefix()`
4227d763cef2SBarry Smith @*/
4228d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetOptionsPrefix(TS ts, const char prefix[])
4229d71ae5a4SJacob Faibussowitsch {
4230089b2837SJed Brown   SNES snes;
4231d763cef2SBarry Smith 
4232d763cef2SBarry Smith   PetscFunctionBegin;
42330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
42349566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts, prefix));
42359566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
42369566063dSJacob Faibussowitsch   PetscCall(SNESSetOptionsPrefix(snes, prefix));
42373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4238d763cef2SBarry Smith }
4239d763cef2SBarry Smith 
4240d763cef2SBarry Smith /*@C
4241d763cef2SBarry Smith   TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4242d763cef2SBarry Smith   TS options in the database.
4243d763cef2SBarry Smith 
4244c3339decSBarry Smith   Logically Collective
4245d763cef2SBarry Smith 
4246d8d19677SJose E. Roman   Input Parameters:
4247bcf0153eSBarry Smith + ts     - The `TS` context
4248d763cef2SBarry Smith - prefix - The prefix to prepend to all option names
4249d763cef2SBarry Smith 
4250bcf0153eSBarry Smith   Level: advanced
4251bcf0153eSBarry Smith 
4252bcf0153eSBarry Smith   Note:
4253d763cef2SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
4254d763cef2SBarry Smith   The first character of all runtime options is AUTOMATICALLY the
4255d763cef2SBarry Smith   hyphen.
4256d763cef2SBarry Smith 
42571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `TSSetFromOptions()`
4258d763cef2SBarry Smith @*/
4259d71ae5a4SJacob Faibussowitsch PetscErrorCode TSAppendOptionsPrefix(TS ts, const char prefix[])
4260d71ae5a4SJacob Faibussowitsch {
4261089b2837SJed Brown   SNES snes;
4262d763cef2SBarry Smith 
4263d763cef2SBarry Smith   PetscFunctionBegin;
42640700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
42659566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts, prefix));
42669566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
42679566063dSJacob Faibussowitsch   PetscCall(SNESAppendOptionsPrefix(snes, prefix));
42683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4269d763cef2SBarry Smith }
4270d763cef2SBarry Smith 
4271d763cef2SBarry Smith /*@C
4272d763cef2SBarry Smith   TSGetOptionsPrefix - Sets the prefix used for searching for all
4273bcf0153eSBarry Smith   `TS` options in the database.
4274d763cef2SBarry Smith 
4275d763cef2SBarry Smith   Not Collective
4276d763cef2SBarry Smith 
4277d763cef2SBarry Smith   Input Parameter:
4278bcf0153eSBarry Smith . ts - The `TS` context
4279d763cef2SBarry Smith 
4280d763cef2SBarry Smith   Output Parameter:
4281d763cef2SBarry Smith . prefix - A pointer to the prefix string used
4282d763cef2SBarry Smith 
4283d763cef2SBarry Smith   Level: intermediate
4284d763cef2SBarry Smith 
4285b43aa488SJacob Faibussowitsch   Fortran Notes:
4286195e9b02SBarry Smith   The user should pass in a string 'prefix' of
4287bcf0153eSBarry Smith   sufficient length to hold the prefix.
4288bcf0153eSBarry Smith 
42891cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAppendOptionsPrefix()`, `TSSetFromOptions()`
4290d763cef2SBarry Smith @*/
4291d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetOptionsPrefix(TS ts, const char *prefix[])
4292d71ae5a4SJacob Faibussowitsch {
4293d763cef2SBarry Smith   PetscFunctionBegin;
42940700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
42954482741eSBarry Smith   PetscValidPointer(prefix, 2);
42969566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts, prefix));
42973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4298d763cef2SBarry Smith }
4299d763cef2SBarry Smith 
4300d763cef2SBarry Smith /*@C
4301d763cef2SBarry Smith   TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4302d763cef2SBarry Smith 
4303bcf0153eSBarry Smith   Not Collective, but parallel objects are returned if ts is parallel
4304d763cef2SBarry Smith 
4305d763cef2SBarry Smith   Input Parameter:
4306bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
4307d763cef2SBarry Smith 
4308d763cef2SBarry Smith   Output Parameters:
4309195e9b02SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or `NULL`)
4310195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as `Amat`  (or `NULL`)
4311195e9b02SBarry Smith . func - Function to compute the Jacobian of the RHS  (or `NULL`)
4312195e9b02SBarry Smith - ctx  - User-defined context for Jacobian evaluation routine  (or `NULL`)
4313d763cef2SBarry Smith 
4314d763cef2SBarry Smith   Level: intermediate
4315d763cef2SBarry Smith 
4316bcf0153eSBarry Smith   Note:
4317195e9b02SBarry Smith   You can pass in `NULL` for any return argument you do not need.
4318bcf0153eSBarry Smith 
43191cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`
4320d763cef2SBarry Smith 
4321d763cef2SBarry Smith @*/
4322d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSJacobian(TS ts, Mat *Amat, Mat *Pmat, TSRHSJacobian *func, void **ctx)
4323d71ae5a4SJacob Faibussowitsch {
432424989b8cSPeter Brune   DM dm;
4325089b2837SJed Brown 
4326d763cef2SBarry Smith   PetscFunctionBegin;
432723a57915SBarry Smith   if (Amat || Pmat) {
432823a57915SBarry Smith     SNES snes;
43299566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts, &snes));
43309566063dSJacob Faibussowitsch     PetscCall(SNESSetUpMatrices(snes));
43319566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL));
433223a57915SBarry Smith   }
43339566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
43349566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm, func, ctx));
43353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4336d763cef2SBarry Smith }
4337d763cef2SBarry Smith 
43382eca1d9cSJed Brown /*@C
43392eca1d9cSJed Brown   TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
43402eca1d9cSJed Brown 
4341bcf0153eSBarry Smith   Not Collective, but parallel objects are returned if ts is parallel
43422eca1d9cSJed Brown 
43432eca1d9cSJed Brown   Input Parameter:
4344bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()`
43452eca1d9cSJed Brown 
43462eca1d9cSJed Brown   Output Parameters:
4347e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t)
4348195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as `Amat`
43492eca1d9cSJed Brown . f    - The function to compute the matrices
43502eca1d9cSJed Brown - ctx  - User-defined context for Jacobian evaluation routine
43512eca1d9cSJed Brown 
43522eca1d9cSJed Brown   Level: advanced
43532eca1d9cSJed Brown 
4354bcf0153eSBarry Smith   Note:
4355195e9b02SBarry Smith   You can pass in `NULL` for any return argument you do not need.
4356bcf0153eSBarry Smith 
43571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`
43582eca1d9cSJed Brown @*/
4359d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetIJacobian(TS ts, Mat *Amat, Mat *Pmat, TSIJacobian *f, void **ctx)
4360d71ae5a4SJacob Faibussowitsch {
436124989b8cSPeter Brune   DM dm;
43620910c330SBarry Smith 
43632eca1d9cSJed Brown   PetscFunctionBegin;
4364c0aab802Sstefano_zampini   if (Amat || Pmat) {
4365c0aab802Sstefano_zampini     SNES snes;
43669566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts, &snes));
43679566063dSJacob Faibussowitsch     PetscCall(SNESSetUpMatrices(snes));
43689566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL));
4369c0aab802Sstefano_zampini   }
43709566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts, &dm));
43719566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm, f, ctx));
43723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43732eca1d9cSJed Brown }
43742eca1d9cSJed Brown 
4375af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
43766c699258SBarry Smith /*@
4377bcf0153eSBarry Smith   TSSetDM - Sets the `DM` that may be used by some nonlinear solvers or preconditioners under the `TS`
43786c699258SBarry Smith 
4379c3339decSBarry Smith   Logically Collective
43806c699258SBarry Smith 
43816c699258SBarry Smith   Input Parameters:
4382bcf0153eSBarry Smith + ts - the `TS` integrator object
4383195e9b02SBarry Smith - dm - the dm, cannot be `NULL`
43846c699258SBarry Smith 
43856c699258SBarry Smith   Level: intermediate
43866c699258SBarry Smith 
4387bcf0153eSBarry Smith   Notes:
4388bcf0153eSBarry Smith   A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`,
4389bcf0153eSBarry Smith   even when not using interfaces like `DMTSSetIFunction()`.  Use `DMClone()` to get a distinct `DM` when solving
4390bcf0153eSBarry Smith   different problems using the same function space.
4391bcf0153eSBarry Smith 
43921cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()`
43936c699258SBarry Smith @*/
4394d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDM(TS ts, DM dm)
4395d71ae5a4SJacob Faibussowitsch {
4396089b2837SJed Brown   SNES snes;
4397942e3340SBarry Smith   DMTS tsdm;
43986c699258SBarry Smith 
43996c699258SBarry Smith   PetscFunctionBegin;
44000700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
44012a808120SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
44029566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)dm));
4403942e3340SBarry Smith   if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */
44042a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
44059566063dSJacob Faibussowitsch       PetscCall(DMCopyDMTS(ts->dm, dm));
44069566063dSJacob Faibussowitsch       PetscCall(DMGetDMTS(ts->dm, &tsdm));
44071e66621cSBarry Smith       /* Grant write privileges to the replacement DM */
44081e66621cSBarry Smith       if (tsdm->originaldm == ts->dm) tsdm->originaldm = dm;
440924989b8cSPeter Brune     }
44109566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&ts->dm));
441124989b8cSPeter Brune   }
44126c699258SBarry Smith   ts->dm = dm;
4413bbd56ea5SKarl Rupp 
44149566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
44159566063dSJacob Faibussowitsch   PetscCall(SNESSetDM(snes, dm));
44163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44176c699258SBarry Smith }
44186c699258SBarry Smith 
44196c699258SBarry Smith /*@
4420bcf0153eSBarry Smith   TSGetDM - Gets the `DM` that may be used by some preconditioners
44216c699258SBarry Smith 
44223f9fe445SBarry Smith   Not Collective
44236c699258SBarry Smith 
44246c699258SBarry Smith   Input Parameter:
4425bcf0153eSBarry Smith . ts - the `TS`
44266c699258SBarry Smith 
44276c699258SBarry Smith   Output Parameter:
4428195e9b02SBarry Smith . dm - the `DM`
44296c699258SBarry Smith 
44306c699258SBarry Smith   Level: intermediate
44316c699258SBarry Smith 
44321cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()`
44336c699258SBarry Smith @*/
4434d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDM(TS ts, DM *dm)
4435d71ae5a4SJacob Faibussowitsch {
44366c699258SBarry Smith   PetscFunctionBegin;
44370700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4438496e6a7aSJed Brown   if (!ts->dm) {
44399566063dSJacob Faibussowitsch     PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts), &ts->dm));
44409566063dSJacob Faibussowitsch     if (ts->snes) PetscCall(SNESSetDM(ts->snes, ts->dm));
4441496e6a7aSJed Brown   }
44426c699258SBarry Smith   *dm = ts->dm;
44433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44446c699258SBarry Smith }
44451713a123SBarry Smith 
44460f5c6efeSJed Brown /*@
44470f5c6efeSJed Brown   SNESTSFormFunction - Function to evaluate nonlinear residual
44480f5c6efeSJed Brown 
4449c3339decSBarry Smith   Logically Collective
44500f5c6efeSJed Brown 
4451d8d19677SJose E. Roman   Input Parameters:
4452d42a1c89SJed Brown + snes - nonlinear solver
44530910c330SBarry Smith . U    - the current state at which to evaluate the residual
4454d42a1c89SJed Brown - ctx  - user context, must be a TS
44550f5c6efeSJed Brown 
44560f5c6efeSJed Brown   Output Parameter:
44570f5c6efeSJed Brown . F - the nonlinear residual
44580f5c6efeSJed Brown 
44590f5c6efeSJed Brown   Level: advanced
44600f5c6efeSJed Brown 
4461bcf0153eSBarry Smith   Note:
4462bcf0153eSBarry Smith   This function is not normally called by users and is automatically registered with the `SNES` used by `TS`.
4463bcf0153eSBarry Smith   It is most frequently passed to `MatFDColoringSetFunction()`.
4464bcf0153eSBarry Smith 
44651cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetFunction()`, `MatFDColoringSetFunction()`
44660f5c6efeSJed Brown @*/
4467d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormFunction(SNES snes, Vec U, Vec F, void *ctx)
4468d71ae5a4SJacob Faibussowitsch {
44690f5c6efeSJed Brown   TS ts = (TS)ctx;
44700f5c6efeSJed Brown 
44710f5c6efeSJed Brown   PetscFunctionBegin;
44720f5c6efeSJed Brown   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
44730910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 2);
44740f5c6efeSJed Brown   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
44750f5c6efeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 4);
44769566063dSJacob Faibussowitsch   PetscCall((ts->ops->snesfunction)(snes, U, F, ts));
44773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44780f5c6efeSJed Brown }
44790f5c6efeSJed Brown 
44800f5c6efeSJed Brown /*@
44810f5c6efeSJed Brown   SNESTSFormJacobian - Function to evaluate the Jacobian
44820f5c6efeSJed Brown 
4483c3339decSBarry Smith   Collective
44840f5c6efeSJed Brown 
4485d8d19677SJose E. Roman   Input Parameters:
44860f5c6efeSJed Brown + snes - nonlinear solver
44870910c330SBarry Smith . U    - the current state at which to evaluate the residual
4488bcf0153eSBarry Smith - ctx  - user context, must be a `TS`
44890f5c6efeSJed Brown 
4490d8d19677SJose E. Roman   Output Parameters:
44910f5c6efeSJed Brown + A - the Jacobian
44926b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A)
44930f5c6efeSJed Brown 
44940f5c6efeSJed Brown   Level: developer
44950f5c6efeSJed Brown 
4496bcf0153eSBarry Smith   Note:
4497bcf0153eSBarry Smith   This function is not normally called by users and is automatically registered with the `SNES` used by `TS`.
4498bcf0153eSBarry Smith 
44991cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetJacobian()`
45000f5c6efeSJed Brown @*/
4501d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormJacobian(SNES snes, Vec U, Mat A, Mat B, void *ctx)
4502d71ae5a4SJacob Faibussowitsch {
45030f5c6efeSJed Brown   TS ts = (TS)ctx;
45040f5c6efeSJed Brown 
45050f5c6efeSJed Brown   PetscFunctionBegin;
45060f5c6efeSJed Brown   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
45070910c330SBarry Smith   PetscValidHeaderSpecific(U, VEC_CLASSID, 2);
45080f5c6efeSJed Brown   PetscValidPointer(A, 3);
450994ab13aaSBarry Smith   PetscValidHeaderSpecific(A, MAT_CLASSID, 3);
45100f5c6efeSJed Brown   PetscValidPointer(B, 4);
451194ab13aaSBarry Smith   PetscValidHeaderSpecific(B, MAT_CLASSID, 4);
4512064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ts, TS_CLASSID, 5);
45139566063dSJacob Faibussowitsch   PetscCall((ts->ops->snesjacobian)(snes, U, A, B, ts));
45143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45150f5c6efeSJed Brown }
4516325fc9f4SBarry Smith 
45170e4ef248SJed Brown /*@C
45189ae8fd06SBarry Smith   TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
45190e4ef248SJed Brown 
4520c3339decSBarry Smith   Collective
45210e4ef248SJed Brown 
45224165533cSJose E. Roman   Input Parameters:
45230e4ef248SJed Brown + ts  - time stepping context
45240e4ef248SJed Brown . t   - time at which to evaluate
45250910c330SBarry Smith . U   - state at which to evaluate
45260e4ef248SJed Brown - ctx - context
45270e4ef248SJed Brown 
45284165533cSJose E. Roman   Output Parameter:
45290e4ef248SJed Brown . F - right hand side
45300e4ef248SJed Brown 
45310e4ef248SJed Brown   Level: intermediate
45320e4ef248SJed Brown 
4533bcf0153eSBarry Smith   Note:
4534bcf0153eSBarry Smith   This function is intended to be passed to `TSSetRHSFunction()` to evaluate the right hand side for linear problems.
4535bcf0153eSBarry Smith   The matrix (and optionally the evaluation context) should be passed to `TSSetRHSJacobian()`.
45360e4ef248SJed Brown 
45371cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()`
45380e4ef248SJed Brown @*/
4539d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunctionLinear(TS ts, PetscReal t, Vec U, Vec F, void *ctx)
4540d71ae5a4SJacob Faibussowitsch {
45410e4ef248SJed Brown   Mat Arhs, Brhs;
45420e4ef248SJed Brown 
45430e4ef248SJed Brown   PetscFunctionBegin;
45449566063dSJacob Faibussowitsch   PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs));
45452663174eSHong Zhang   /* undo the damage caused by shifting */
45469566063dSJacob Faibussowitsch   PetscCall(TSRecoverRHSJacobian(ts, Arhs, Brhs));
45479566063dSJacob Faibussowitsch   PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs));
45489566063dSJacob Faibussowitsch   PetscCall(MatMult(Arhs, U, F));
45493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45500e4ef248SJed Brown }
45510e4ef248SJed Brown 
45520e4ef248SJed Brown /*@C
45530e4ef248SJed Brown   TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
45540e4ef248SJed Brown 
4555c3339decSBarry Smith   Collective
45560e4ef248SJed Brown 
45574165533cSJose E. Roman   Input Parameters:
45580e4ef248SJed Brown + ts  - time stepping context
45590e4ef248SJed Brown . t   - time at which to evaluate
45600910c330SBarry Smith . U   - state at which to evaluate
45610e4ef248SJed Brown - ctx - context
45620e4ef248SJed Brown 
45634165533cSJose E. Roman   Output Parameters:
45640e4ef248SJed Brown + A - pointer to operator
456597bb3fdcSJose E. Roman - B - pointer to preconditioning matrix
45660e4ef248SJed Brown 
45670e4ef248SJed Brown   Level: intermediate
45680e4ef248SJed Brown 
4569bcf0153eSBarry Smith   Note:
4570bcf0153eSBarry Smith   This function is intended to be passed to `TSSetRHSJacobian()` to evaluate the Jacobian for linear time-independent problems.
45710e4ef248SJed Brown 
45721cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()`
45730e4ef248SJed Brown @*/
4574d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobianConstant(TS ts, PetscReal t, Vec U, Mat A, Mat B, void *ctx)
4575d71ae5a4SJacob Faibussowitsch {
45760e4ef248SJed Brown   PetscFunctionBegin;
45773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45780e4ef248SJed Brown }
45790e4ef248SJed Brown 
45800026cea9SSean Farley /*@C
45810026cea9SSean Farley   TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
45820026cea9SSean Farley 
4583c3339decSBarry Smith   Collective
45840026cea9SSean Farley 
45854165533cSJose E. Roman   Input Parameters:
45860026cea9SSean Farley + ts   - time stepping context
45870026cea9SSean Farley . t    - time at which to evaluate
45880910c330SBarry Smith . U    - state at which to evaluate
45890910c330SBarry Smith . Udot - time derivative of state vector
45900026cea9SSean Farley - ctx  - context
45910026cea9SSean Farley 
45924165533cSJose E. Roman   Output Parameter:
45930026cea9SSean Farley . F - left hand side
45940026cea9SSean Farley 
45950026cea9SSean Farley   Level: intermediate
45960026cea9SSean Farley 
45970026cea9SSean Farley   Notes:
45980910c330SBarry 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
4599bcf0153eSBarry Smith   user is required to write their own `TSComputeIFunction()`.
4600bcf0153eSBarry Smith   This function is intended to be passed to `TSSetIFunction()` to evaluate the left hand side for linear problems.
4601bcf0153eSBarry Smith   The matrix (and optionally the evaluation context) should be passed to `TSSetIJacobian()`.
46020026cea9SSean Farley 
4603bcf0153eSBarry Smith   Note that using this function is NOT equivalent to using `TSComputeRHSFunctionLinear()` since that solves Udot = A U
46049ae8fd06SBarry Smith 
46051cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()`
46060026cea9SSean Farley @*/
4607d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunctionLinear(TS ts, PetscReal t, Vec U, Vec Udot, Vec F, void *ctx)
4608d71ae5a4SJacob Faibussowitsch {
46090026cea9SSean Farley   Mat A, B;
46100026cea9SSean Farley 
46110026cea9SSean Farley   PetscFunctionBegin;
46129566063dSJacob Faibussowitsch   PetscCall(TSGetIJacobian(ts, &A, &B, NULL, NULL));
46139566063dSJacob Faibussowitsch   PetscCall(TSComputeIJacobian(ts, t, U, Udot, 1.0, A, B, PETSC_TRUE));
46149566063dSJacob Faibussowitsch   PetscCall(MatMult(A, Udot, F));
46153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46160026cea9SSean Farley }
46170026cea9SSean Farley 
46180026cea9SSean Farley /*@C
4619b41af12eSJed Brown   TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
46200026cea9SSean Farley 
4621c3339decSBarry Smith   Collective
46220026cea9SSean Farley 
46234165533cSJose E. Roman   Input Parameters:
46240026cea9SSean Farley + ts    - time stepping context
46250026cea9SSean Farley . t     - time at which to evaluate
46260910c330SBarry Smith . U     - state at which to evaluate
46270910c330SBarry Smith . Udot  - time derivative of state vector
46280026cea9SSean Farley . shift - shift to apply
46290026cea9SSean Farley - ctx   - context
46300026cea9SSean Farley 
46314165533cSJose E. Roman   Output Parameters:
46320026cea9SSean Farley + A - pointer to operator
463397bb3fdcSJose E. Roman - B - pointer to preconditioning matrix
46340026cea9SSean Farley 
4635b41af12eSJed Brown   Level: advanced
46360026cea9SSean Farley 
46370026cea9SSean Farley   Notes:
4638bcf0153eSBarry Smith   This function is intended to be passed to `TSSetIJacobian()` to evaluate the Jacobian for linear time-independent problems.
46390026cea9SSean Farley 
4640b41af12eSJed Brown   It is only appropriate for problems of the form
4641b41af12eSJed Brown 
4642b41af12eSJed Brown $     M Udot = F(U,t)
4643b41af12eSJed Brown 
4644bcf0153eSBarry Smith   where M is constant and F is non-stiff.  The user must pass M to `TSSetIJacobian()`.  The current implementation only
4645bcf0153eSBarry Smith   works with IMEX time integration methods such as `TSROSW` and `TSARKIMEX`, since there is no support for de-constructing
4646b41af12eSJed Brown   an implicit operator of the form
4647b41af12eSJed Brown 
4648b41af12eSJed Brown $    shift*M + J
4649b41af12eSJed Brown 
4650b41af12eSJed 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
4651b41af12eSJed Brown   a copy of M or reassemble it when requested.
4652b41af12eSJed Brown 
46531cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSROSW`, `TSARKIMEX`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()`
46540026cea9SSean Farley @*/
4655d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianConstant(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, void *ctx)
4656d71ae5a4SJacob Faibussowitsch {
46570026cea9SSean Farley   PetscFunctionBegin;
46589566063dSJacob Faibussowitsch   PetscCall(MatScale(A, shift / ts->ijacobian.shift));
4659b41af12eSJed Brown   ts->ijacobian.shift = shift;
46603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46610026cea9SSean Farley }
4662b41af12eSJed Brown 
4663e817cc15SEmil Constantinescu /*@
4664bcf0153eSBarry Smith   TSGetEquationType - Gets the type of the equation that `TS` is solving.
4665e817cc15SEmil Constantinescu 
4666e817cc15SEmil Constantinescu   Not Collective
4667e817cc15SEmil Constantinescu 
4668e817cc15SEmil Constantinescu   Input Parameter:
4669bcf0153eSBarry Smith . ts - the `TS` context
4670e817cc15SEmil Constantinescu 
4671e817cc15SEmil Constantinescu   Output Parameter:
4672bcf0153eSBarry Smith . equation_type - see `TSEquationType`
4673e817cc15SEmil Constantinescu 
4674e817cc15SEmil Constantinescu   Level: beginner
4675e817cc15SEmil Constantinescu 
46761cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetEquationType()`, `TSEquationType`
4677e817cc15SEmil Constantinescu @*/
4678d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetEquationType(TS ts, TSEquationType *equation_type)
4679d71ae5a4SJacob Faibussowitsch {
4680e817cc15SEmil Constantinescu   PetscFunctionBegin;
4681e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4682e817cc15SEmil Constantinescu   PetscValidPointer(equation_type, 2);
4683e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
46843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4685e817cc15SEmil Constantinescu }
4686e817cc15SEmil Constantinescu 
4687e817cc15SEmil Constantinescu /*@
4688bcf0153eSBarry Smith   TSSetEquationType - Sets the type of the equation that `TS` is solving.
4689e817cc15SEmil Constantinescu 
4690e817cc15SEmil Constantinescu   Not Collective
4691e817cc15SEmil Constantinescu 
4692d8d19677SJose E. Roman   Input Parameters:
4693bcf0153eSBarry Smith + ts            - the `TS` context
4694bcf0153eSBarry Smith - equation_type - see `TSEquationType`
4695e817cc15SEmil Constantinescu 
4696e817cc15SEmil Constantinescu   Level: advanced
4697e817cc15SEmil Constantinescu 
46981cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetEquationType()`, `TSEquationType`
4699e817cc15SEmil Constantinescu @*/
4700d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetEquationType(TS ts, TSEquationType equation_type)
4701d71ae5a4SJacob Faibussowitsch {
4702e817cc15SEmil Constantinescu   PetscFunctionBegin;
4703e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4704e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
47053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4706e817cc15SEmil Constantinescu }
47070026cea9SSean Farley 
47084af1b03aSJed Brown /*@
4709bcf0153eSBarry Smith   TSGetConvergedReason - Gets the reason the `TS` iteration was stopped.
47104af1b03aSJed Brown 
47114af1b03aSJed Brown   Not Collective
47124af1b03aSJed Brown 
47134af1b03aSJed Brown   Input Parameter:
4714bcf0153eSBarry Smith . ts - the `TS` context
47154af1b03aSJed Brown 
47164af1b03aSJed Brown   Output Parameter:
4717bcf0153eSBarry Smith . reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the
47184af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
47194af1b03aSJed Brown 
4720487e0bb9SJed Brown   Level: beginner
47214af1b03aSJed Brown 
4722bcf0153eSBarry Smith   Note:
4723bcf0153eSBarry Smith   Can only be called after the call to `TSSolve()` is complete.
47244af1b03aSJed Brown 
47251cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSSetConvergenceTest()`, `TSConvergedReason`
47264af1b03aSJed Brown @*/
4727d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetConvergedReason(TS ts, TSConvergedReason *reason)
4728d71ae5a4SJacob Faibussowitsch {
47294af1b03aSJed Brown   PetscFunctionBegin;
47304af1b03aSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
47314af1b03aSJed Brown   PetscValidPointer(reason, 2);
47324af1b03aSJed Brown   *reason = ts->reason;
47333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47344af1b03aSJed Brown }
47354af1b03aSJed Brown 
4736d6ad946cSShri Abhyankar /*@
4737bcf0153eSBarry Smith   TSSetConvergedReason - Sets the reason for handling the convergence of `TSSolve()`.
4738d6ad946cSShri Abhyankar 
47396b221cbeSPatrick Sanan   Logically Collective; reason must contain common value
4740d6ad946cSShri Abhyankar 
47416b221cbeSPatrick Sanan   Input Parameters:
4742bcf0153eSBarry Smith + ts     - the `TS` context
4743bcf0153eSBarry Smith - reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the
4744d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4745d6ad946cSShri Abhyankar 
4746f5abba47SShri Abhyankar   Level: advanced
4747d6ad946cSShri Abhyankar 
4748bcf0153eSBarry Smith   Note:
4749bcf0153eSBarry Smith   Can only be called while `TSSolve()` is active.
4750d6ad946cSShri Abhyankar 
47511cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason`
4752d6ad946cSShri Abhyankar @*/
4753d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetConvergedReason(TS ts, TSConvergedReason reason)
4754d71ae5a4SJacob Faibussowitsch {
4755d6ad946cSShri Abhyankar   PetscFunctionBegin;
4756d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4757d6ad946cSShri Abhyankar   ts->reason = reason;
47583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4759d6ad946cSShri Abhyankar }
4760d6ad946cSShri Abhyankar 
4761cc708dedSBarry Smith /*@
4762bcf0153eSBarry Smith   TSGetSolveTime - Gets the time after a call to `TSSolve()`
4763cc708dedSBarry Smith 
4764cc708dedSBarry Smith   Not Collective
4765cc708dedSBarry Smith 
4766cc708dedSBarry Smith   Input Parameter:
4767bcf0153eSBarry Smith . ts - the `TS` context
4768cc708dedSBarry Smith 
4769cc708dedSBarry Smith   Output Parameter:
4770bcf0153eSBarry Smith . ftime - the final time. This time corresponds to the final time set with `TSSetMaxTime()`
4771cc708dedSBarry Smith 
4772487e0bb9SJed Brown   Level: beginner
4773cc708dedSBarry Smith 
4774bcf0153eSBarry Smith   Note:
4775bcf0153eSBarry Smith   Can only be called after the call to `TSSolve()` is complete.
4776cc708dedSBarry Smith 
47771cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSSetConvergenceTest()`, `TSConvergedReason`
4778cc708dedSBarry Smith @*/
4779d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolveTime(TS ts, PetscReal *ftime)
4780d71ae5a4SJacob Faibussowitsch {
4781cc708dedSBarry Smith   PetscFunctionBegin;
4782cc708dedSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4783dadcf809SJacob Faibussowitsch   PetscValidRealPointer(ftime, 2);
4784cc708dedSBarry Smith   *ftime = ts->solvetime;
47853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4786cc708dedSBarry Smith }
4787cc708dedSBarry Smith 
47882c18e0fdSBarry Smith /*@
47895ef26d82SJed Brown   TSGetSNESIterations - Gets the total number of nonlinear iterations
47909f67acb7SJed Brown   used by the time integrator.
47919f67acb7SJed Brown 
47929f67acb7SJed Brown   Not Collective
47939f67acb7SJed Brown 
47949f67acb7SJed Brown   Input Parameter:
4795bcf0153eSBarry Smith . ts - `TS` context
47969f67acb7SJed Brown 
47979f67acb7SJed Brown   Output Parameter:
47989f67acb7SJed Brown . nits - number of nonlinear iterations
47999f67acb7SJed Brown 
48009f67acb7SJed Brown   Level: intermediate
48019f67acb7SJed Brown 
4802195e9b02SBarry Smith   Note:
4803bcf0153eSBarry Smith   This counter is reset to zero for each successive call to `TSSolve()`.
4804bcf0153eSBarry Smith 
48051cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetKSPIterations()`
48069f67acb7SJed Brown @*/
4807d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESIterations(TS ts, PetscInt *nits)
4808d71ae5a4SJacob Faibussowitsch {
48099f67acb7SJed Brown   PetscFunctionBegin;
48109f67acb7SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
48119f67acb7SJed Brown   PetscValidIntPointer(nits, 2);
48125ef26d82SJed Brown   *nits = ts->snes_its;
48133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48149f67acb7SJed Brown }
48159f67acb7SJed Brown 
48169f67acb7SJed Brown /*@
48175ef26d82SJed Brown   TSGetKSPIterations - Gets the total number of linear iterations
48189f67acb7SJed Brown   used by the time integrator.
48199f67acb7SJed Brown 
48209f67acb7SJed Brown   Not Collective
48219f67acb7SJed Brown 
48229f67acb7SJed Brown   Input Parameter:
4823bcf0153eSBarry Smith . ts - `TS` context
48249f67acb7SJed Brown 
48259f67acb7SJed Brown   Output Parameter:
48269f67acb7SJed Brown . lits - number of linear iterations
48279f67acb7SJed Brown 
48289f67acb7SJed Brown   Level: intermediate
48299f67acb7SJed Brown 
4830bcf0153eSBarry Smith   Note:
4831bcf0153eSBarry Smith   This counter is reset to zero for each successive call to `TSSolve()`.
4832bcf0153eSBarry Smith 
48331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `SNESGetKSPIterations()`
48349f67acb7SJed Brown @*/
4835d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSPIterations(TS ts, PetscInt *lits)
4836d71ae5a4SJacob Faibussowitsch {
48379f67acb7SJed Brown   PetscFunctionBegin;
48389f67acb7SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
48399f67acb7SJed Brown   PetscValidIntPointer(lits, 2);
48405ef26d82SJed Brown   *lits = ts->ksp_its;
48413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48429f67acb7SJed Brown }
48439f67acb7SJed Brown 
4844cef5090cSJed Brown /*@
4845cef5090cSJed Brown   TSGetStepRejections - Gets the total number of rejected steps.
4846cef5090cSJed Brown 
4847cef5090cSJed Brown   Not Collective
4848cef5090cSJed Brown 
4849cef5090cSJed Brown   Input Parameter:
4850bcf0153eSBarry Smith . ts - `TS` context
4851cef5090cSJed Brown 
4852cef5090cSJed Brown   Output Parameter:
4853cef5090cSJed Brown . rejects - number of steps rejected
4854cef5090cSJed Brown 
4855cef5090cSJed Brown   Level: intermediate
4856cef5090cSJed Brown 
4857bcf0153eSBarry Smith   Note:
4858bcf0153eSBarry Smith   This counter is reset to zero for each successive call to `TSSolve()`.
4859bcf0153eSBarry Smith 
48601cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()`
4861cef5090cSJed Brown @*/
4862d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepRejections(TS ts, PetscInt *rejects)
4863d71ae5a4SJacob Faibussowitsch {
4864cef5090cSJed Brown   PetscFunctionBegin;
4865cef5090cSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4866cef5090cSJed Brown   PetscValidIntPointer(rejects, 2);
4867cef5090cSJed Brown   *rejects = ts->reject;
48683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4869cef5090cSJed Brown }
4870cef5090cSJed Brown 
4871cef5090cSJed Brown /*@
4872bcf0153eSBarry Smith   TSGetSNESFailures - Gets the total number of failed `SNES` solves in a `TS`
4873cef5090cSJed Brown 
4874cef5090cSJed Brown   Not Collective
4875cef5090cSJed Brown 
4876cef5090cSJed Brown   Input Parameter:
4877bcf0153eSBarry Smith . ts - `TS` context
4878cef5090cSJed Brown 
4879cef5090cSJed Brown   Output Parameter:
4880cef5090cSJed Brown . fails - number of failed nonlinear solves
4881cef5090cSJed Brown 
4882cef5090cSJed Brown   Level: intermediate
4883cef5090cSJed Brown 
4884bcf0153eSBarry Smith   Note:
4885bcf0153eSBarry Smith   This counter is reset to zero for each successive call to `TSSolve()`.
4886bcf0153eSBarry Smith 
48871cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()`
4888cef5090cSJed Brown @*/
4889d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESFailures(TS ts, PetscInt *fails)
4890d71ae5a4SJacob Faibussowitsch {
4891cef5090cSJed Brown   PetscFunctionBegin;
4892cef5090cSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4893cef5090cSJed Brown   PetscValidIntPointer(fails, 2);
4894cef5090cSJed Brown   *fails = ts->num_snes_failures;
48953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4896cef5090cSJed Brown }
4897cef5090cSJed Brown 
4898cef5090cSJed Brown /*@
4899bcf0153eSBarry Smith   TSSetMaxStepRejections - Sets the maximum number of step rejections before a time step fails
4900cef5090cSJed Brown 
4901cef5090cSJed Brown   Not Collective
4902cef5090cSJed Brown 
4903d8d19677SJose E. Roman   Input Parameters:
4904bcf0153eSBarry Smith + ts      - `TS` context
4905cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited
4906cef5090cSJed Brown 
4907cef5090cSJed Brown   Options Database Key:
4908cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails
4909cef5090cSJed Brown 
4910cef5090cSJed Brown   Level: intermediate
4911cef5090cSJed Brown 
49121cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()`
4913cef5090cSJed Brown @*/
4914d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxStepRejections(TS ts, PetscInt rejects)
4915d71ae5a4SJacob Faibussowitsch {
4916cef5090cSJed Brown   PetscFunctionBegin;
4917cef5090cSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4918cef5090cSJed Brown   ts->max_reject = rejects;
49193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4920cef5090cSJed Brown }
4921cef5090cSJed Brown 
4922cef5090cSJed Brown /*@
4923bcf0153eSBarry Smith   TSSetMaxSNESFailures - Sets the maximum number of failed `SNES` solves
4924cef5090cSJed Brown 
4925cef5090cSJed Brown   Not Collective
4926cef5090cSJed Brown 
4927d8d19677SJose E. Roman   Input Parameters:
4928bcf0153eSBarry Smith + ts    - `TS` context
4929cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4930cef5090cSJed Brown 
4931cef5090cSJed Brown   Options Database Key:
4932cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures
4933cef5090cSJed Brown 
4934cef5090cSJed Brown   Level: intermediate
4935cef5090cSJed Brown 
49361cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()`
4937cef5090cSJed Brown @*/
4938d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSNESFailures(TS ts, PetscInt fails)
4939d71ae5a4SJacob Faibussowitsch {
4940cef5090cSJed Brown   PetscFunctionBegin;
4941cef5090cSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4942cef5090cSJed Brown   ts->max_snes_failures = fails;
49433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4944cef5090cSJed Brown }
4945cef5090cSJed Brown 
4946cef5090cSJed Brown /*@
4947195e9b02SBarry Smith   TSSetErrorIfStepFails - Immediately error if no step succeeds during `TSSolve()`
4948cef5090cSJed Brown 
4949cef5090cSJed Brown   Not Collective
4950cef5090cSJed Brown 
4951d8d19677SJose E. Roman   Input Parameters:
4952bcf0153eSBarry Smith + ts  - `TS` context
4953bcf0153eSBarry Smith - err - `PETSC_TRUE` to error if no step succeeds, `PETSC_FALSE` to return without failure
4954cef5090cSJed Brown 
4955cef5090cSJed Brown   Options Database Key:
4956cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds
4957cef5090cSJed Brown 
4958cef5090cSJed Brown   Level: intermediate
4959cef5090cSJed Brown 
49601cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()`
4961cef5090cSJed Brown @*/
4962d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetErrorIfStepFails(TS ts, PetscBool err)
4963d71ae5a4SJacob Faibussowitsch {
4964cef5090cSJed Brown   PetscFunctionBegin;
4965cef5090cSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4966cef5090cSJed Brown   ts->errorifstepfailed = err;
49673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4968cef5090cSJed Brown }
4969cef5090cSJed Brown 
497084df9cb4SJed Brown /*@
4971552698daSJed Brown   TSGetAdapt - Get the adaptive controller context for the current method
497284df9cb4SJed Brown 
4973195e9b02SBarry Smith   Collective on `ts` if controller has not been created yet
497484df9cb4SJed Brown 
49754165533cSJose E. Roman   Input Parameter:
4976ed81e22dSJed Brown . ts - time stepping context
497784df9cb4SJed Brown 
49784165533cSJose E. Roman   Output Parameter:
4979ed81e22dSJed Brown . adapt - adaptive controller
498084df9cb4SJed Brown 
498184df9cb4SJed Brown   Level: intermediate
498284df9cb4SJed Brown 
49831cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()`
498484df9cb4SJed Brown @*/
4985d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAdapt(TS ts, TSAdapt *adapt)
4986d71ae5a4SJacob Faibussowitsch {
498784df9cb4SJed Brown   PetscFunctionBegin;
498884df9cb4SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4989bec58848SLisandro Dalcin   PetscValidPointer(adapt, 2);
499084df9cb4SJed Brown   if (!ts->adapt) {
49919566063dSJacob Faibussowitsch     PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts), &ts->adapt));
49929566063dSJacob Faibussowitsch     PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt, (PetscObject)ts, 1));
499384df9cb4SJed Brown   }
4994bec58848SLisandro Dalcin   *adapt = ts->adapt;
49953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
499684df9cb4SJed Brown }
4997d6ebe24aSShri Abhyankar 
49981c3436cfSJed Brown /*@
4999195e9b02SBarry Smith   TSSetTolerances - Set tolerances for local truncation error when using an adaptive controller
50001c3436cfSJed Brown 
50011c3436cfSJed Brown   Logically Collective
50021c3436cfSJed Brown 
50034165533cSJose E. Roman   Input Parameters:
50041c3436cfSJed Brown + ts    - time integration context
5005bcf0153eSBarry Smith . atol  - scalar absolute tolerances, `PETSC_DECIDE` to leave current value
5006195e9b02SBarry Smith . vatol - vector of absolute tolerances or `NULL`, used in preference to atol if present
5007bcf0153eSBarry Smith . rtol  - scalar relative tolerances, `PETSC_DECIDE` to leave current value
5008195e9b02SBarry Smith - vrtol - vector of relative tolerances or `NULL`, used in preference to atol if present
50091c3436cfSJed Brown 
5010195e9b02SBarry Smith   Options Database Keys:
5011a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error
501267b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error
5013a3cdaa26SBarry Smith 
5014bcf0153eSBarry Smith   Level: beginner
5015bcf0153eSBarry Smith 
50163ff766beSShri Abhyankar   Notes:
50173ff766beSShri Abhyankar   With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
50183ff766beSShri Abhyankar   (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
50193ff766beSShri Abhyankar   computed only for the differential or the algebraic part then this can be done using the vector of
50203ff766beSShri Abhyankar   tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
50213ff766beSShri Abhyankar   differential part and infinity for the algebraic part, the LTE calculation will include only the
50223ff766beSShri Abhyankar   differential variables.
50233ff766beSShri Abhyankar 
50241cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()`
50251c3436cfSJed Brown @*/
5026d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTolerances(TS ts, PetscReal atol, Vec vatol, PetscReal rtol, Vec vrtol)
5027d71ae5a4SJacob Faibussowitsch {
50281c3436cfSJed Brown   PetscFunctionBegin;
502913bcc0bdSJacob Faibussowitsch   if (atol != (PetscReal)PETSC_DECIDE && atol != (PetscReal)PETSC_DEFAULT) ts->atol = atol;
50301c3436cfSJed Brown   if (vatol) {
50319566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)vatol));
50329566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vatol));
50331c3436cfSJed Brown     ts->vatol = vatol;
50341c3436cfSJed Brown   }
503513bcc0bdSJacob Faibussowitsch   if (rtol != (PetscReal)PETSC_DECIDE && rtol != (PetscReal)PETSC_DEFAULT) ts->rtol = rtol;
50361c3436cfSJed Brown   if (vrtol) {
50379566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)vrtol));
50389566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vrtol));
50391c3436cfSJed Brown     ts->vrtol = vrtol;
50401c3436cfSJed Brown   }
50413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
50421c3436cfSJed Brown }
50431c3436cfSJed Brown 
5044c5033834SJed Brown /*@
5045c5033834SJed Brown   TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5046c5033834SJed Brown 
5047c5033834SJed Brown   Logically Collective
5048c5033834SJed Brown 
50494165533cSJose E. Roman   Input Parameter:
5050c5033834SJed Brown . ts - time integration context
5051c5033834SJed Brown 
50524165533cSJose E. Roman   Output Parameters:
5053195e9b02SBarry Smith + atol  - scalar absolute tolerances, `NULL` to ignore
5054195e9b02SBarry Smith . vatol - vector of absolute tolerances, `NULL` to ignore
5055195e9b02SBarry Smith . rtol  - scalar relative tolerances, `NULL` to ignore
5056195e9b02SBarry Smith - vrtol - vector of relative tolerances, `NULL` to ignore
5057c5033834SJed Brown 
5058c5033834SJed Brown   Level: beginner
5059c5033834SJed Brown 
50601cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()`
5061c5033834SJed Brown @*/
5062d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTolerances(TS ts, PetscReal *atol, Vec *vatol, PetscReal *rtol, Vec *vrtol)
5063d71ae5a4SJacob Faibussowitsch {
5064c5033834SJed Brown   PetscFunctionBegin;
5065c5033834SJed Brown   if (atol) *atol = ts->atol;
5066c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5067c5033834SJed Brown   if (rtol) *rtol = ts->rtol;
5068c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
50693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5070c5033834SJed Brown }
5071c5033834SJed Brown 
50729c6b16b5SShri Abhyankar /*@
50738a175baeSEmil Constantinescu   TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
50741c3436cfSJed Brown 
5075c3339decSBarry Smith   Collective
50761c3436cfSJed Brown 
50774165533cSJose E. Roman   Input Parameters:
50781c3436cfSJed Brown + ts        - time stepping context
5079a4868fbcSLisandro Dalcin . U         - state vector, usually ts->vec_sol
5080a4868fbcSLisandro Dalcin . Y         - state vector to be compared to U
5081bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY`
50827619abb3SShri 
50834165533cSJose E. Roman   Output Parameters:
5084a2b725a8SWilliam Gropp + norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
50858a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5086a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5087a4868fbcSLisandro Dalcin 
5088bcf0153eSBarry Smith   Options Database Key:
5089a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5090a4868fbcSLisandro Dalcin 
50911c3436cfSJed Brown   Level: developer
50921c3436cfSJed Brown 
50938c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedENorm()`
50941c3436cfSJed Brown @*/
5095d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm(TS ts, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr)
5096d71ae5a4SJacob Faibussowitsch {
5097037d63e4SStefano Zampini   PetscInt norma_loc, norm_loc, normr_loc;
50988a175baeSEmil Constantinescu 
50998a175baeSEmil Constantinescu   PetscFunctionBegin;
51008a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5101037d63e4SStefano Zampini   PetscCall(VecErrorWeightedNorms(U, Y, NULL, wnormtype, ts->atol, ts->vatol, ts->rtol, ts->vrtol, ts->adapt->ignore_max, norm, &norm_loc, norma, &norma_loc, normr, &normr_loc));
5102037d63e4SStefano Zampini   if (wnormtype == NORM_2) {
5103037d63e4SStefano Zampini     if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc);
5104037d63e4SStefano Zampini     if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc);
5105037d63e4SStefano Zampini     if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc);
51068a175baeSEmil Constantinescu   }
51073c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm");
51083c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma");
51093c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr");
51103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
51118a175baeSEmil Constantinescu }
51128a175baeSEmil Constantinescu 
51138a175baeSEmil Constantinescu /*@
51148a175baeSEmil Constantinescu   TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
51158a175baeSEmil Constantinescu 
5116c3339decSBarry Smith   Collective
51178a175baeSEmil Constantinescu 
51184165533cSJose E. Roman   Input Parameters:
51198a175baeSEmil Constantinescu + ts        - time stepping context
51208a175baeSEmil Constantinescu . E         - error vector
51218a175baeSEmil Constantinescu . U         - state vector, usually ts->vec_sol
51228a175baeSEmil Constantinescu . Y         - state vector, previous time step
5123bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY`
51248a175baeSEmil Constantinescu 
51254165533cSJose E. Roman   Output Parameters:
5126a2b725a8SWilliam Gropp + norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
51278a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5128a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
51298a175baeSEmil Constantinescu 
5130bcf0153eSBarry Smith   Options Database Key:
51318a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
51328a175baeSEmil Constantinescu 
51338a175baeSEmil Constantinescu   Level: developer
51348a175baeSEmil Constantinescu 
51358c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedNorm()`
51368a175baeSEmil Constantinescu @*/
5137d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm(TS ts, Vec E, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr)
5138d71ae5a4SJacob Faibussowitsch {
5139037d63e4SStefano Zampini   PetscInt norma_loc, norm_loc, normr_loc;
5140037d63e4SStefano Zampini 
51418a175baeSEmil Constantinescu   PetscFunctionBegin;
5142037d63e4SStefano Zampini   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5143037d63e4SStefano Zampini   PetscCall(VecErrorWeightedNorms(U, Y, E, wnormtype, ts->atol, ts->vatol, ts->rtol, ts->vrtol, ts->adapt->ignore_max, norm, &norm_loc, norma, &norma_loc, normr, &normr_loc));
5144037d63e4SStefano Zampini   if (wnormtype == NORM_2) {
5145037d63e4SStefano Zampini     if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc);
5146037d63e4SStefano Zampini     if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc);
5147037d63e4SStefano Zampini     if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc);
5148037d63e4SStefano Zampini   }
5149037d63e4SStefano Zampini   PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm");
5150037d63e4SStefano Zampini   PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma");
5151037d63e4SStefano Zampini   PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr");
51523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
51538a175baeSEmil Constantinescu }
51548a175baeSEmil Constantinescu 
51558d59e960SJed Brown /*@
51568d59e960SJed Brown   TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
51578d59e960SJed Brown 
5158c3339decSBarry Smith   Logically Collective
51598d59e960SJed Brown 
51604165533cSJose E. Roman   Input Parameters:
51618d59e960SJed Brown + ts      - time stepping context
51628d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process)
51638d59e960SJed Brown 
51648d59e960SJed Brown   Note:
51658d59e960SJed Brown   After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
51668d59e960SJed Brown 
51678d59e960SJed Brown   Level: intermediate
51688d59e960SJed Brown 
51691cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetCFLTime()`, `TSADAPTCFL`
51708d59e960SJed Brown @*/
5171d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetCFLTimeLocal(TS ts, PetscReal cfltime)
5172d71ae5a4SJacob Faibussowitsch {
51738d59e960SJed Brown   PetscFunctionBegin;
51748d59e960SJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
51758d59e960SJed Brown   ts->cfltime_local = cfltime;
51768d59e960SJed Brown   ts->cfltime       = -1.;
51773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
51788d59e960SJed Brown }
51798d59e960SJed Brown 
51808d59e960SJed Brown /*@
51818d59e960SJed Brown   TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
51828d59e960SJed Brown 
5183c3339decSBarry Smith   Collective
51848d59e960SJed Brown 
51854165533cSJose E. Roman   Input Parameter:
51868d59e960SJed Brown . ts - time stepping context
51878d59e960SJed Brown 
51884165533cSJose E. Roman   Output Parameter:
51898d59e960SJed Brown . cfltime - maximum stable time step for forward Euler
51908d59e960SJed Brown 
51918d59e960SJed Brown   Level: advanced
51928d59e960SJed Brown 
51931cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetCFLTimeLocal()`
51948d59e960SJed Brown @*/
5195d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetCFLTime(TS ts, PetscReal *cfltime)
5196d71ae5a4SJacob Faibussowitsch {
51978d59e960SJed Brown   PetscFunctionBegin;
51981e66621cSBarry Smith   if (ts->cfltime < 0) PetscCall(MPIU_Allreduce(&ts->cfltime_local, &ts->cfltime, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)ts)));
51998d59e960SJed Brown   *cfltime = ts->cfltime;
52003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
52018d59e960SJed Brown }
52028d59e960SJed Brown 
5203d6ebe24aSShri Abhyankar /*@
5204d6ebe24aSShri Abhyankar   TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5205d6ebe24aSShri Abhyankar 
5206d6ebe24aSShri Abhyankar   Input Parameters:
5207bcf0153eSBarry Smith + ts - the `TS` context.
5208d6ebe24aSShri Abhyankar . xl - lower bound.
5209a2b725a8SWilliam Gropp - xu - upper bound.
5210d6ebe24aSShri Abhyankar 
52112bd2b0e6SSatish Balay   Level: advanced
52122bd2b0e6SSatish Balay 
5213bcf0153eSBarry Smith   Note:
5214bcf0153eSBarry Smith   If this routine is not called then the lower and upper bounds are set to
5215bcf0153eSBarry Smith   `PETSC_NINFINITY` and `PETSC_INFINITY` respectively during `SNESSetUp()`.
5216bcf0153eSBarry Smith 
52171cc06b55SBarry Smith .seealso: [](ch_ts), `TS`
5218d6ebe24aSShri Abhyankar @*/
5219d71ae5a4SJacob Faibussowitsch PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5220d71ae5a4SJacob Faibussowitsch {
5221d6ebe24aSShri Abhyankar   SNES snes;
5222d6ebe24aSShri Abhyankar 
5223d6ebe24aSShri Abhyankar   PetscFunctionBegin;
52249566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
52259566063dSJacob Faibussowitsch   PetscCall(SNESVISetVariableBounds(snes, xl, xu));
52263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5227d6ebe24aSShri Abhyankar }
5228d6ebe24aSShri Abhyankar 
5229f9c1d6abSBarry Smith /*@
5230f9c1d6abSBarry Smith   TSComputeLinearStability - computes the linear stability function at a point
5231f9c1d6abSBarry Smith 
5232c3339decSBarry Smith   Collective
5233f9c1d6abSBarry Smith 
5234f9c1d6abSBarry Smith   Input Parameters:
5235bcf0153eSBarry Smith + ts - the `TS` context
52362fe279fdSBarry Smith . xr - real part of input argument
52372fe279fdSBarry Smith - xi - imaginary part of input argument
5238f9c1d6abSBarry Smith 
5239f9c1d6abSBarry Smith   Output Parameters:
52402fe279fdSBarry Smith + yr - real part of function value
52412fe279fdSBarry Smith - yi - imaginary part of function value
5242f9c1d6abSBarry Smith 
5243f9c1d6abSBarry Smith   Level: developer
5244f9c1d6abSBarry Smith 
52451cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()`
5246f9c1d6abSBarry Smith @*/
5247d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeLinearStability(TS ts, PetscReal xr, PetscReal xi, PetscReal *yr, PetscReal *yi)
5248d71ae5a4SJacob Faibussowitsch {
5249f9c1d6abSBarry Smith   PetscFunctionBegin;
5250f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5251dbbe0bcdSBarry Smith   PetscUseTypeMethod(ts, linearstability, xr, xi, yr, yi);
52523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5253f9c1d6abSBarry Smith }
525424655328SShri 
525524655328SShri /*@
5256dcb233daSLisandro Dalcin   TSRestartStep - Flags the solver to restart the next step
5257dcb233daSLisandro Dalcin 
5258c3339decSBarry Smith   Collective
5259dcb233daSLisandro Dalcin 
5260dcb233daSLisandro Dalcin   Input Parameter:
5261bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
5262dcb233daSLisandro Dalcin 
5263dcb233daSLisandro Dalcin   Level: advanced
5264dcb233daSLisandro Dalcin 
5265dcb233daSLisandro Dalcin   Notes:
5266bcf0153eSBarry Smith   Multistep methods like `TSBDF` or Runge-Kutta methods with FSAL property require restarting the solver in the event of
5267dcb233daSLisandro Dalcin   discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
5268dcb233daSLisandro Dalcin   vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
5269bcf0153eSBarry Smith   the sake of correctness and maximum safety, users are expected to call `TSRestart()` whenever they introduce
5270dcb233daSLisandro Dalcin   discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
5271dcb233daSLisandro Dalcin   discontinuous source terms).
5272dcb233daSLisandro Dalcin 
52731cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()`
5274dcb233daSLisandro Dalcin @*/
5275d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRestartStep(TS ts)
5276d71ae5a4SJacob Faibussowitsch {
5277dcb233daSLisandro Dalcin   PetscFunctionBegin;
5278dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5279dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
52803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5281dcb233daSLisandro Dalcin }
5282dcb233daSLisandro Dalcin 
5283dcb233daSLisandro Dalcin /*@
528424655328SShri   TSRollBack - Rolls back one time step
528524655328SShri 
5286c3339decSBarry Smith   Collective
528724655328SShri 
528824655328SShri   Input Parameter:
5289bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
529024655328SShri 
529124655328SShri   Level: advanced
529224655328SShri 
52931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()`
529424655328SShri @*/
5295d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRollBack(TS ts)
5296d71ae5a4SJacob Faibussowitsch {
529724655328SShri   PetscFunctionBegin;
529824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
52993c633725SBarry Smith   PetscCheck(!ts->steprollback, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TSRollBack already called");
5300dbbe0bcdSBarry Smith   PetscUseTypeMethod(ts, rollback);
530124655328SShri   ts->time_step  = ts->ptime - ts->ptime_prev;
530224655328SShri   ts->ptime      = ts->ptime_prev;
5303be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
53042808aa04SLisandro Dalcin   ts->steps--;
5305b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
53063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
530724655328SShri }
5308aeb4809dSShri Abhyankar 
5309ff22ae23SHong Zhang /*@
5310ff22ae23SHong Zhang   TSGetStages - Get the number of stages and stage values
5311ff22ae23SHong Zhang 
5312ff22ae23SHong Zhang   Input Parameter:
5313bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
5314ff22ae23SHong Zhang 
53150429704eSStefano Zampini   Output Parameters:
53160429704eSStefano Zampini + ns - the number of stages
53170429704eSStefano Zampini - Y  - the current stage vectors
53180429704eSStefano Zampini 
5319ff22ae23SHong Zhang   Level: advanced
5320ff22ae23SHong Zhang 
5321bcf0153eSBarry Smith   Note:
5322195e9b02SBarry Smith   Both `ns` and `Y` can be `NULL`.
53230429704eSStefano Zampini 
53241cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`
5325ff22ae23SHong Zhang @*/
5326d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStages(TS ts, PetscInt *ns, Vec **Y)
5327d71ae5a4SJacob Faibussowitsch {
5328ff22ae23SHong Zhang   PetscFunctionBegin;
5329ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5330dadcf809SJacob Faibussowitsch   if (ns) PetscValidIntPointer(ns, 2);
53310429704eSStefano Zampini   if (Y) PetscValidPointer(Y, 3);
53320429704eSStefano Zampini   if (!ts->ops->getstages) {
53330429704eSStefano Zampini     if (ns) *ns = 0;
53340429704eSStefano Zampini     if (Y) *Y = NULL;
5335dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(ts, getstages, ns, Y);
53363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5337ff22ae23SHong Zhang }
5338ff22ae23SHong Zhang 
5339847ff0e1SMatthew G. Knepley /*@C
5340847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
5341847ff0e1SMatthew G. Knepley 
5342c3339decSBarry Smith   Collective
5343847ff0e1SMatthew G. Knepley 
5344847ff0e1SMatthew G. Knepley   Input Parameters:
5345bcf0153eSBarry Smith + ts    - the `TS` context
5346847ff0e1SMatthew G. Knepley . t     - current timestep
5347847ff0e1SMatthew G. Knepley . U     - state vector
5348847ff0e1SMatthew G. Knepley . Udot  - time derivative of state vector
5349847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
5350847ff0e1SMatthew G. Knepley - ctx   - an optional user context
5351847ff0e1SMatthew G. Knepley 
5352847ff0e1SMatthew G. Knepley   Output Parameters:
5353847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
5354195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner (generally the same as `J`)
5355847ff0e1SMatthew G. Knepley 
5356847ff0e1SMatthew G. Knepley   Level: intermediate
5357847ff0e1SMatthew G. Knepley 
5358847ff0e1SMatthew G. Knepley   Notes:
5359847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
5360847ff0e1SMatthew G. Knepley 
5361847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
5362847ff0e1SMatthew G. Knepley 
5363847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
5364847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
5365847ff0e1SMatthew G. Knepley 
5366bcf0153eSBarry Smith   This will first try to get the coloring from the `DM`.  If the `DM` type has no coloring
5367847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
5368847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
5369847ff0e1SMatthew G. Knepley 
53701cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()`
5371847ff0e1SMatthew G. Knepley @*/
5372d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianDefaultColor(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat J, Mat B, void *ctx)
5373d71ae5a4SJacob Faibussowitsch {
5374847ff0e1SMatthew G. Knepley   SNES          snes;
5375847ff0e1SMatthew G. Knepley   MatFDColoring color;
5376847ff0e1SMatthew G. Knepley   PetscBool     hascolor, matcolor = PETSC_FALSE;
5377847ff0e1SMatthew G. Knepley 
5378847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
53799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL));
53809566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)B, "TSMatFDColoring", (PetscObject *)&color));
5381847ff0e1SMatthew G. Knepley   if (!color) {
5382847ff0e1SMatthew G. Knepley     DM         dm;
5383847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
5384847ff0e1SMatthew G. Knepley 
53859566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &dm));
53869566063dSJacob Faibussowitsch     PetscCall(DMHasColoring(dm, &hascolor));
5387847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
53889566063dSJacob Faibussowitsch       PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring));
53899566063dSJacob Faibussowitsch       PetscCall(MatFDColoringCreate(B, iscoloring, &color));
53909566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts));
53919566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFromOptions(color));
53929566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetUp(B, iscoloring, color));
53939566063dSJacob Faibussowitsch       PetscCall(ISColoringDestroy(&iscoloring));
5394847ff0e1SMatthew G. Knepley     } else {
5395847ff0e1SMatthew G. Knepley       MatColoring mc;
5396847ff0e1SMatthew G. Knepley 
53979566063dSJacob Faibussowitsch       PetscCall(MatColoringCreate(B, &mc));
53989566063dSJacob Faibussowitsch       PetscCall(MatColoringSetDistance(mc, 2));
53999566063dSJacob Faibussowitsch       PetscCall(MatColoringSetType(mc, MATCOLORINGSL));
54009566063dSJacob Faibussowitsch       PetscCall(MatColoringSetFromOptions(mc));
54019566063dSJacob Faibussowitsch       PetscCall(MatColoringApply(mc, &iscoloring));
54029566063dSJacob Faibussowitsch       PetscCall(MatColoringDestroy(&mc));
54039566063dSJacob Faibussowitsch       PetscCall(MatFDColoringCreate(B, iscoloring, &color));
54049566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts));
54059566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFromOptions(color));
54069566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetUp(B, iscoloring, color));
54079566063dSJacob Faibussowitsch       PetscCall(ISColoringDestroy(&iscoloring));
5408847ff0e1SMatthew G. Knepley     }
54099566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)color));
54109566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)color));
5411847ff0e1SMatthew G. Knepley   }
54129566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
54139566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply(B, color, U, snes));
5414847ff0e1SMatthew G. Knepley   if (J != B) {
54159566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
54169566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
5417847ff0e1SMatthew G. Knepley   }
54183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5419847ff0e1SMatthew G. Knepley }
542093b34091SDebojyoti Ghosh 
5421b43aa488SJacob Faibussowitsch /*@C
54226bc98fa9SBarry Smith   TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
5423cb9d8021SPierre Barbier de Reuille 
5424cb9d8021SPierre Barbier de Reuille   Input Parameters:
5425bcf0153eSBarry Smith + ts   - the `TS` context
5426bcf0153eSBarry Smith - func - function called within `TSFunctionDomainError()`
54276bc98fa9SBarry Smith 
542820f4b53cSBarry Smith   Calling sequence of `func`:
54296bc98fa9SBarry Smith $   PetscErrorCode func(TS ts, PetscReal time, Vec state, PetscBool reject)
54306bc98fa9SBarry Smith + ts - the TS context
54316bc98fa9SBarry Smith .   time - the current time (of the stage)
54326bc98fa9SBarry Smith .   state - the state to check if it is valid
543320f4b53cSBarry Smith -   reject - (output parameter) `PETSC_FALSE` if the state is acceptable, `PETSC_TRUE` if not acceptable
5434cb9d8021SPierre Barbier de Reuille 
5435cb9d8021SPierre Barbier de Reuille   Level: intermediate
5436cb9d8021SPierre Barbier de Reuille 
54376bc98fa9SBarry Smith   Notes:
54386bc98fa9SBarry Smith   If an implicit ODE solver is being used then, in addition to providing this routine, the
5439bcf0153eSBarry Smith   user's code should call `SNESSetFunctionDomainError()` when domain errors occur during
5440bcf0153eSBarry Smith   function evaluations where the functions are provided by `TSSetIFunction()` or `TSSetRHSFunction()`.
5441bcf0153eSBarry Smith   Use `TSGetSNES()` to obtain the `SNES` object
54426bc98fa9SBarry Smith 
5443b43aa488SJacob Faibussowitsch   Developer Notes:
5444bcf0153eSBarry Smith   The naming of this function is inconsistent with the `SNESSetFunctionDomainError()`
54456bc98fa9SBarry Smith   since one takes a function pointer and the other does not.
54466bc98fa9SBarry Smith 
54471cc06b55SBarry Smith .seealso: [](ch_ts), `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()`
5448cb9d8021SPierre Barbier de Reuille @*/
5449d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS, PetscReal, Vec, PetscBool *))
5450d71ae5a4SJacob Faibussowitsch {
5451cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
5452cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5453cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
54543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5455cb9d8021SPierre Barbier de Reuille }
5456cb9d8021SPierre Barbier de Reuille 
5457cb9d8021SPierre Barbier de Reuille /*@
54586bc98fa9SBarry Smith   TSFunctionDomainError - Checks if the current state is valid
5459cb9d8021SPierre Barbier de Reuille 
5460cb9d8021SPierre Barbier de Reuille   Input Parameters:
5461bcf0153eSBarry Smith + ts        - the `TS` context
54626bc98fa9SBarry Smith . stagetime - time of the simulation
54636bc98fa9SBarry Smith - Y         - state vector to check.
5464cb9d8021SPierre Barbier de Reuille 
5465cb9d8021SPierre Barbier de Reuille   Output Parameter:
5466bcf0153eSBarry Smith . accept - Set to `PETSC_FALSE` if the current state vector is valid.
546796a0c994SBarry Smith 
54686bc98fa9SBarry Smith   Level: developer
54696bc98fa9SBarry Smith 
5470bcf0153eSBarry Smith   Note:
5471bcf0153eSBarry Smith   This function is called by the `TS` integration routines and calls the user provided function (set with `TSSetFunctionDomainError()`)
5472bcf0153eSBarry Smith   to check if the current state is valid.
5473bcf0153eSBarry Smith 
54741cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFunctionDomainError()`
5475cb9d8021SPierre Barbier de Reuille @*/
5476d71ae5a4SJacob Faibussowitsch PetscErrorCode TSFunctionDomainError(TS ts, PetscReal stagetime, Vec Y, PetscBool *accept)
5477d71ae5a4SJacob Faibussowitsch {
5478cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
5479cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5480cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
54811e66621cSBarry Smith   if (ts->functiondomainerror) PetscCall((*ts->functiondomainerror)(ts, stagetime, Y, accept));
54823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5483cb9d8021SPierre Barbier de Reuille }
54841ceb14c0SBarry Smith 
548593b34091SDebojyoti Ghosh /*@C
5486195e9b02SBarry Smith   TSClone - This function clones a time step `TS` object.
548793b34091SDebojyoti Ghosh 
5488d083f849SBarry Smith   Collective
548993b34091SDebojyoti Ghosh 
549093b34091SDebojyoti Ghosh   Input Parameter:
5491bcf0153eSBarry Smith . tsin - The input `TS`
549293b34091SDebojyoti Ghosh 
549393b34091SDebojyoti Ghosh   Output Parameter:
5494bcf0153eSBarry Smith . tsout - The output `TS` (cloned)
54955eca1a21SEmil Constantinescu 
54965eca1a21SEmil Constantinescu   Level: developer
549793b34091SDebojyoti Ghosh 
5498bcf0153eSBarry Smith   Notes:
5499bcf0153eSBarry Smith   This function is used to create a clone of a `TS` object. It is used in `TSARKIMEX` for initializing the slope for first stage explicit methods.
5500bcf0153eSBarry Smith   It will likely be replaced in the future with a mechanism of switching methods on the fly.
5501bcf0153eSBarry Smith 
5502195e9b02SBarry Smith   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
5503195e9b02SBarry Smith .vb
5504195e9b02SBarry Smith  SNES snes_dup = NULL;
5505195e9b02SBarry Smith  TSGetSNES(ts,&snes_dup);
5506195e9b02SBarry Smith  TSSetSNES(ts,snes_dup);
5507195e9b02SBarry Smith .ve
5508bcf0153eSBarry Smith 
55091cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()`
551093b34091SDebojyoti Ghosh @*/
5511d71ae5a4SJacob Faibussowitsch PetscErrorCode TSClone(TS tsin, TS *tsout)
5512d71ae5a4SJacob Faibussowitsch {
551393b34091SDebojyoti Ghosh   TS     t;
5514dc846ba4SSatish Balay   SNES   snes_start;
5515dc846ba4SSatish Balay   DM     dm;
5516dc846ba4SSatish Balay   TSType type;
551793b34091SDebojyoti Ghosh 
551893b34091SDebojyoti Ghosh   PetscFunctionBegin;
551993b34091SDebojyoti Ghosh   PetscValidPointer(tsin, 1);
552093b34091SDebojyoti Ghosh   *tsout = NULL;
552193b34091SDebojyoti Ghosh 
55229566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView));
552393b34091SDebojyoti Ghosh 
552493b34091SDebojyoti Ghosh   /* General TS description */
552593b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
5526d0c080abSJoseph Pusztay   t->monitorFrequency  = 1;
552793b34091SDebojyoti Ghosh   t->setupcalled       = 0;
552893b34091SDebojyoti Ghosh   t->ksp_its           = 0;
552993b34091SDebojyoti Ghosh   t->snes_its          = 0;
553093b34091SDebojyoti Ghosh   t->nwork             = 0;
55317d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
553293b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
553393b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
553493b34091SDebojyoti Ghosh 
55359566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(tsin, &snes_start));
55369566063dSJacob Faibussowitsch   PetscCall(TSSetSNES(t, snes_start));
5537d15a3a53SEmil Constantinescu 
55389566063dSJacob Faibussowitsch   PetscCall(TSGetDM(tsin, &dm));
55399566063dSJacob Faibussowitsch   PetscCall(TSSetDM(t, dm));
554093b34091SDebojyoti Ghosh 
554193b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
55429566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)t->adapt));
554393b34091SDebojyoti Ghosh 
5544e7069c78SShri   t->trajectory = tsin->trajectory;
55459566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)t->trajectory));
5546e7069c78SShri 
5547e7069c78SShri   t->event = tsin->event;
55486b10a48eSSatish Balay   if (t->event) t->event->refct++;
5549e7069c78SShri 
555093b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
555193b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
5552e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
555393b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
555493b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
555593b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
555693b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
555793b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
555893b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
555993b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
556093b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
556193b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
556293b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
556393b34091SDebojyoti Ghosh 
55649566063dSJacob Faibussowitsch   PetscCall(TSGetType(tsin, &type));
55659566063dSJacob Faibussowitsch   PetscCall(TSSetType(t, type));
556693b34091SDebojyoti Ghosh 
556793b34091SDebojyoti Ghosh   t->vec_sol = NULL;
556893b34091SDebojyoti Ghosh 
556993b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
557093b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
557193b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
557293b34091SDebojyoti Ghosh 
5573aea10558SJacob Faibussowitsch   t->ops[0] = tsin->ops[0];
557493b34091SDebojyoti Ghosh 
55750d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
55760d4fed19SBarry Smith     PetscInt i;
55779566063dSJacob Faibussowitsch     PetscCall(PetscMalloc((10) * sizeof(void (*)(void)), &((PetscObject)t)->fortran_func_pointers));
5578ad540459SPierre Jolivet     for (i = 0; i < 10; i++) ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
55790d4fed19SBarry Smith   }
558093b34091SDebojyoti Ghosh   *tsout = t;
55813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
558293b34091SDebojyoti Ghosh }
5583f3b1f45cSBarry Smith 
5584d71ae5a4SJacob Faibussowitsch static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void *ctx, Vec x, Vec y)
5585d71ae5a4SJacob Faibussowitsch {
5586f3b1f45cSBarry Smith   TS ts = (TS)ctx;
5587f3b1f45cSBarry Smith 
5588f3b1f45cSBarry Smith   PetscFunctionBegin;
55899566063dSJacob Faibussowitsch   PetscCall(TSComputeRHSFunction(ts, 0, x, y));
55903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5591f3b1f45cSBarry Smith }
5592f3b1f45cSBarry Smith 
5593f3b1f45cSBarry Smith /*@
5594bcf0153eSBarry Smith   TSRHSJacobianTest - Compares the multiply routine provided to the `MATSHELL` with differencing on the `TS` given RHS function.
5595f3b1f45cSBarry Smith 
5596c3339decSBarry Smith   Logically Collective
5597f3b1f45cSBarry Smith 
55982fe279fdSBarry Smith   Input Parameter:
5599b43aa488SJacob Faibussowitsch . ts - the time stepping routine
5600f3b1f45cSBarry Smith 
5601f3b1f45cSBarry Smith   Output Parameter:
5602bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct
5603f3b1f45cSBarry Smith 
5604bcf0153eSBarry Smith   Options Database Key:
5605f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
5606f3b1f45cSBarry Smith 
5607f3b1f45cSBarry Smith   Level: advanced
5608f3b1f45cSBarry Smith 
5609bcf0153eSBarry Smith   Note:
5610195e9b02SBarry Smith   This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian
5611f3b1f45cSBarry Smith 
56121cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MATSHELL`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()`
5613f3b1f45cSBarry Smith @*/
5614d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTest(TS ts, PetscBool *flg)
5615d71ae5a4SJacob Faibussowitsch {
5616f3b1f45cSBarry Smith   Mat           J, B;
5617f3b1f45cSBarry Smith   TSRHSJacobian func;
5618f3b1f45cSBarry Smith   void         *ctx;
5619f3b1f45cSBarry Smith 
5620f3b1f45cSBarry Smith   PetscFunctionBegin;
56219566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx));
56229566063dSJacob Faibussowitsch   PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx));
56239566063dSJacob Faibussowitsch   PetscCall(MatShellTestMult(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg));
56243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5625f3b1f45cSBarry Smith }
5626f3b1f45cSBarry Smith 
5627f3b1f45cSBarry Smith /*@C
5628bcf0153eSBarry Smith   TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the `MATSHELL` with differencing on the `TS` given RHS function.
5629f3b1f45cSBarry Smith 
5630c3339decSBarry Smith   Logically Collective
5631f3b1f45cSBarry Smith 
56322fe279fdSBarry Smith   Input Parameter:
5633b43aa488SJacob Faibussowitsch . ts - the time stepping routine
5634f3b1f45cSBarry Smith 
5635f3b1f45cSBarry Smith   Output Parameter:
5636bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct
5637f3b1f45cSBarry Smith 
5638bcf0153eSBarry Smith   Options Database Key:
5639f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
5640f3b1f45cSBarry Smith 
5641bcf0153eSBarry Smith   Level: advanced
5642bcf0153eSBarry Smith 
564395452b02SPatrick Sanan   Notes:
5644195e9b02SBarry Smith   This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian
5645f3b1f45cSBarry Smith 
56461cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()`
5647f3b1f45cSBarry Smith @*/
5648d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTestTranspose(TS ts, PetscBool *flg)
5649d71ae5a4SJacob Faibussowitsch {
5650f3b1f45cSBarry Smith   Mat           J, B;
5651f3b1f45cSBarry Smith   void         *ctx;
5652f3b1f45cSBarry Smith   TSRHSJacobian func;
5653f3b1f45cSBarry Smith 
5654f3b1f45cSBarry Smith   PetscFunctionBegin;
56559566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx));
56569566063dSJacob Faibussowitsch   PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx));
56579566063dSJacob Faibussowitsch   PetscCall(MatShellTestMultTranspose(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg));
56583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5659f3b1f45cSBarry Smith }
56600fe4d17eSHong Zhang 
56610fe4d17eSHong Zhang /*@
56620fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
56630fe4d17eSHong Zhang 
566420f4b53cSBarry Smith   Logically Collective
56650fe4d17eSHong Zhang 
5666d8d19677SJose E. Roman   Input Parameters:
56670fe4d17eSHong Zhang + ts                   - timestepping context
5668bcf0153eSBarry Smith - use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used
56690fe4d17eSHong Zhang 
5670bcf0153eSBarry Smith   Options Database Key:
56710fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false>
56720fe4d17eSHong Zhang 
56730fe4d17eSHong Zhang   Level: intermediate
56740fe4d17eSHong Zhang 
5675bcf0153eSBarry Smith   Note:
5676195e9b02SBarry Smith   This is only for multirate methods
5677bcf0153eSBarry Smith 
56781cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetUseSplitRHSFunction()`
56790fe4d17eSHong Zhang @*/
5680d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
5681d71ae5a4SJacob Faibussowitsch {
56820fe4d17eSHong Zhang   PetscFunctionBegin;
56830fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
56840fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
56853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56860fe4d17eSHong Zhang }
56870fe4d17eSHong Zhang 
56880fe4d17eSHong Zhang /*@
56890fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
56900fe4d17eSHong Zhang 
569120f4b53cSBarry Smith   Not Collective
56920fe4d17eSHong Zhang 
56930fe4d17eSHong Zhang   Input Parameter:
56940fe4d17eSHong Zhang . ts - timestepping context
56950fe4d17eSHong Zhang 
56960fe4d17eSHong Zhang   Output Parameter:
5697bcf0153eSBarry Smith . use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used
56980fe4d17eSHong Zhang 
56990fe4d17eSHong Zhang   Level: intermediate
57000fe4d17eSHong Zhang 
57011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetUseSplitRHSFunction()`
57020fe4d17eSHong Zhang @*/
5703d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
5704d71ae5a4SJacob Faibussowitsch {
57050fe4d17eSHong Zhang   PetscFunctionBegin;
57060fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
57070fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
57083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57090fe4d17eSHong Zhang }
5710d60b7d5cSBarry Smith 
5711d60b7d5cSBarry Smith /*@
5712d60b7d5cSBarry Smith   TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix.
5713d60b7d5cSBarry Smith 
5714c3339decSBarry Smith   Logically  Collective
5715d60b7d5cSBarry Smith 
5716d60b7d5cSBarry Smith   Input Parameters:
5717d60b7d5cSBarry Smith + ts  - the time-stepper
5718bcf0153eSBarry Smith - str - the structure (the default is `UNKNOWN_NONZERO_PATTERN`)
5719d60b7d5cSBarry Smith 
5720d60b7d5cSBarry Smith   Level: intermediate
5721d60b7d5cSBarry Smith 
5722bcf0153eSBarry Smith   Note:
5723d60b7d5cSBarry Smith   When the relationship between the nonzero structures is known and supplied the solution process can be much faster
5724d60b7d5cSBarry Smith 
57251cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatAXPY()`, `MatStructure`
5726d60b7d5cSBarry Smith  @*/
5727d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMatStructure(TS ts, MatStructure str)
5728d71ae5a4SJacob Faibussowitsch {
5729d60b7d5cSBarry Smith   PetscFunctionBegin;
5730d60b7d5cSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
5731d60b7d5cSBarry Smith   ts->axpy_pattern = str;
57323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5733d60b7d5cSBarry Smith }
57344a658b32SHong Zhang 
57354a658b32SHong Zhang /*@
5736bcf0153eSBarry Smith   TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested in the span
57374a658b32SHong Zhang 
5738c3339decSBarry Smith   Collective
57394a658b32SHong Zhang 
57404a658b32SHong Zhang   Input Parameters:
57414a658b32SHong Zhang + ts         - the time-stepper
57424a658b32SHong Zhang . n          - number of the time points (>=2)
57434a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively.
57444a658b32SHong Zhang 
5745bcf0153eSBarry Smith   Options Database Key:
57464a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span
57474a658b32SHong Zhang 
5748bcf0153eSBarry Smith   Level: intermediate
57494a658b32SHong Zhang 
57504a658b32SHong Zhang   Notes:
57514a658b32SHong Zhang   The elements in tspan must be all increasing. They correspond to the intermediate points for time integration.
5752bcf0153eSBarry Smith   `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified.
5753bcf0153eSBarry Smith   The intermediate solutions are saved in a vector array that can be accessed with `TSGetTimeSpanSolutions()`. Thus using time span may
57544a658b32SHong Zhang   pressure the memory system when using a large number of span points.
57554a658b32SHong Zhang 
57561cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeSpan()`, `TSGetTimeSpanSolutions()`
57574a658b32SHong Zhang  @*/
5758d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeSpan(TS ts, PetscInt n, PetscReal *span_times)
5759d71ae5a4SJacob Faibussowitsch {
57604a658b32SHong Zhang   PetscFunctionBegin;
57614a658b32SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
576263a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 2, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Minimum time span size is 2 but %" PetscInt_FMT " is provided", n);
57634a658b32SHong Zhang   if (ts->tspan && n != ts->tspan->num_span_times) {
57644a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan->span_times));
57654a658b32SHong Zhang     PetscCall(VecDestroyVecs(ts->tspan->num_span_times, &ts->tspan->vecs_sol));
57664a658b32SHong Zhang     PetscCall(PetscMalloc1(n, &ts->tspan->span_times));
57674a658b32SHong Zhang   }
57684a658b32SHong Zhang   if (!ts->tspan) {
57694a658b32SHong Zhang     TSTimeSpan tspan;
57704a658b32SHong Zhang     PetscCall(PetscNew(&tspan));
57714a658b32SHong Zhang     PetscCall(PetscMalloc1(n, &tspan->span_times));
5772e1db57b0SHong Zhang     tspan->reltol = 1e-6;
5773e1db57b0SHong Zhang     tspan->abstol = 10 * PETSC_MACHINE_EPSILON;
57744a658b32SHong Zhang     ts->tspan     = tspan;
57754a658b32SHong Zhang   }
57764a658b32SHong Zhang   ts->tspan->num_span_times = n;
57774a658b32SHong Zhang   PetscCall(PetscArraycpy(ts->tspan->span_times, span_times, n));
57784a658b32SHong Zhang   PetscCall(TSSetTime(ts, ts->tspan->span_times[0]));
57794a658b32SHong Zhang   PetscCall(TSSetMaxTime(ts, ts->tspan->span_times[n - 1]));
57803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57814a658b32SHong Zhang }
57824a658b32SHong Zhang 
57834a658b32SHong Zhang /*@C
5784195e9b02SBarry Smith   TSGetTimeSpan - gets the time span set with `TSSetTimeSpan()`
57854a658b32SHong Zhang 
57864a658b32SHong Zhang   Not Collective
57874a658b32SHong Zhang 
57884a658b32SHong Zhang   Input Parameter:
57894a658b32SHong Zhang . ts - the time-stepper
57904a658b32SHong Zhang 
57914a658b32SHong Zhang   Output Parameters:
57924a658b32SHong Zhang + n          - number of the time points (>=2)
5793bcf0153eSBarry Smith - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively.
57944a658b32SHong Zhang 
57954a658b32SHong Zhang   Level: beginner
57964a658b32SHong Zhang 
5797bcf0153eSBarry Smith   Note:
5798195e9b02SBarry Smith   The values obtained are valid until the `TS` object is destroyed.
5799195e9b02SBarry Smith 
5800195e9b02SBarry Smith   Both `n` and `span_times` can be `NULL`.
5801bcf0153eSBarry Smith 
58021cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeSpan()`, `TSGetTimeSpanSolutions()`
58034a658b32SHong Zhang  @*/
5804d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeSpan(TS ts, PetscInt *n, const PetscReal **span_times)
5805d71ae5a4SJacob Faibussowitsch {
58064a658b32SHong Zhang   PetscFunctionBegin;
58074a658b32SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
58084a658b32SHong Zhang   if (n) PetscValidIntPointer(n, 2);
58094a658b32SHong Zhang   if (span_times) PetscValidPointer(span_times, 3);
58104a658b32SHong Zhang   if (!ts->tspan) {
58114a658b32SHong Zhang     if (n) *n = 0;
58124a658b32SHong Zhang     if (span_times) *span_times = NULL;
58134a658b32SHong Zhang   } else {
58144a658b32SHong Zhang     if (n) *n = ts->tspan->num_span_times;
58154a658b32SHong Zhang     if (span_times) *span_times = ts->tspan->span_times;
58164a658b32SHong Zhang   }
58173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58184a658b32SHong Zhang }
58194a658b32SHong Zhang 
58204a658b32SHong Zhang /*@
58214a658b32SHong Zhang   TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span.
58224a658b32SHong Zhang 
58234a658b32SHong Zhang   Input Parameter:
5824bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()`
58254a658b32SHong Zhang 
58264a658b32SHong Zhang   Output Parameters:
58274a658b32SHong Zhang + nsol - the number of solutions
58284a658b32SHong Zhang - Sols - the solution vectors
58294a658b32SHong Zhang 
5830bcf0153eSBarry Smith   Level: intermediate
58314a658b32SHong Zhang 
583240bd4cedSHong Zhang   Notes:
5833195e9b02SBarry Smith   Both `nsol` and `Sols` can be `NULL`.
58344a658b32SHong Zhang 
5835195e9b02SBarry Smith   Some time points in the time span may be skipped by `TS` so that `nsol` is less than the number of points specified by `TSSetTimeSpan()`.
5836bcf0153eSBarry Smith   For example, manipulating the step size, especially with a reduced precision, may cause `TS` to step over certain points in the span.
5837bcf0153eSBarry Smith 
58381cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeSpan()`
58394a658b32SHong Zhang @*/
5840d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeSpanSolutions(TS ts, PetscInt *nsol, Vec **Sols)
5841d71ae5a4SJacob Faibussowitsch {
58424a658b32SHong Zhang   PetscFunctionBegin;
58434a658b32SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
58444a658b32SHong Zhang   if (nsol) PetscValidIntPointer(nsol, 2);
58454a658b32SHong Zhang   if (Sols) PetscValidPointer(Sols, 3);
58464a658b32SHong Zhang   if (!ts->tspan) {
58474a658b32SHong Zhang     if (nsol) *nsol = 0;
58484a658b32SHong Zhang     if (Sols) *Sols = NULL;
58494a658b32SHong Zhang   } else {
585040bd4cedSHong Zhang     if (nsol) *nsol = ts->tspan->spanctr;
58514a658b32SHong Zhang     if (Sols) *Sols = ts->tspan->vecs_sol;
58524a658b32SHong Zhang   }
58533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58544a658b32SHong Zhang }
5855d7cfae9bSHong Zhang 
5856d7cfae9bSHong Zhang /*@C
58577b2dc123SHong Zhang   TSPruneIJacobianColor - Remove nondiagonal zeros in the Jacobian matrix and update the `MatMFFD` coloring information.
5858d7cfae9bSHong Zhang 
5859195e9b02SBarry Smith   Collective
5860d7cfae9bSHong Zhang 
5861d7cfae9bSHong Zhang   Input Parameters:
5862195e9b02SBarry Smith + ts - the `TS` context
5863d7cfae9bSHong Zhang . J  - Jacobian matrix (not altered in this routine)
5864195e9b02SBarry Smith - B  - newly computed Jacobian matrix to use with preconditioner
5865d7cfae9bSHong Zhang 
5866d7cfae9bSHong Zhang   Level: intermediate
5867d7cfae9bSHong Zhang 
5868d7cfae9bSHong Zhang   Notes:
5869195e9b02SBarry Smith   This function improves the `MatFDColoring` performance when the Jacobian matrix was over-allocated or contains
5870195e9b02SBarry Smith   many constant zeros entries, which is typically the case when the matrix is generated by a `DM`
5871d7cfae9bSHong Zhang   and multiple fields are involved.
5872d7cfae9bSHong Zhang 
5873d7cfae9bSHong Zhang   Users need to make sure that the Jacobian matrix is properly filled to reflect the sparsity
5874195e9b02SBarry Smith   structure. For `MatFDColoring`, the values of nonzero entries are not important. So one can
58757b2dc123SHong Zhang   usually call `TSComputeIJacobian()` with randomized input vectors to generate a dummy Jacobian.
58767b2dc123SHong Zhang   `TSComputeIJacobian()` should be called before `TSSolve()` but after `TSSetUp()`.
5877d7cfae9bSHong Zhang 
58781cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatFDColoring`, `TSComputeIJacobianDefaultColor()`, `MatEliminateZeros()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()`
5879d7cfae9bSHong Zhang @*/
5880d7cfae9bSHong Zhang PetscErrorCode TSPruneIJacobianColor(TS ts, Mat J, Mat B)
5881d7cfae9bSHong Zhang {
5882d7cfae9bSHong Zhang   MatColoring   mc            = NULL;
5883d7cfae9bSHong Zhang   ISColoring    iscoloring    = NULL;
5884d7cfae9bSHong Zhang   MatFDColoring matfdcoloring = NULL;
5885d7cfae9bSHong Zhang 
5886d7cfae9bSHong Zhang   PetscFunctionBegin;
5887d7cfae9bSHong Zhang   /* Generate new coloring after eliminating zeros in the matrix */
5888d7cfae9bSHong Zhang   PetscCall(MatEliminateZeros(B));
5889d7cfae9bSHong Zhang   PetscCall(MatColoringCreate(B, &mc));
5890d7cfae9bSHong Zhang   PetscCall(MatColoringSetDistance(mc, 2));
5891d7cfae9bSHong Zhang   PetscCall(MatColoringSetType(mc, MATCOLORINGSL));
5892d7cfae9bSHong Zhang   PetscCall(MatColoringSetFromOptions(mc));
5893d7cfae9bSHong Zhang   PetscCall(MatColoringApply(mc, &iscoloring));
5894d7cfae9bSHong Zhang   PetscCall(MatColoringDestroy(&mc));
5895d7cfae9bSHong Zhang   /* Replace the old coloring with the new one */
5896d7cfae9bSHong Zhang   PetscCall(MatFDColoringCreate(B, iscoloring, &matfdcoloring));
5897d7cfae9bSHong Zhang   PetscCall(MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts));
5898d7cfae9bSHong Zhang   PetscCall(MatFDColoringSetFromOptions(matfdcoloring));
5899d7cfae9bSHong Zhang   PetscCall(MatFDColoringSetUp(B, iscoloring, matfdcoloring));
5900d7cfae9bSHong Zhang   PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)matfdcoloring));
5901d7cfae9bSHong Zhang   PetscCall(PetscObjectDereference((PetscObject)matfdcoloring));
5902d7cfae9bSHong Zhang   PetscCall(ISColoringDestroy(&iscoloring));
59033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5904d7cfae9bSHong Zhang }
5905