xref: /petsc/src/ts/interface/ts.c (revision 6a5217c03994f2d95bb2e6dbd8bed42381aeb015)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6900f6b5bSMatthew G. Knepley #include <petscconvest.h>
7d763cef2SBarry Smith 
81c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if (PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;
91c167fc2SEmil Constantinescu 
10d5ba7fb7SMatthew Knepley /* Logging support */
11d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
13d405a339SMatthew Knepley 
14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",NULL};
1549354f04SShri Abhyankar 
162ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
172ffb9264SLisandro Dalcin {
182ffb9264SLisandro Dalcin   PetscFunctionBegin;
19b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
20b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
212ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
229566063dSJacob Faibussowitsch     PetscCall(TSAdaptSetType(adapt,default_type));
232ffb9264SLisandro Dalcin   }
242ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
252ffb9264SLisandro Dalcin }
262ffb9264SLisandro Dalcin 
27bdad233fSMatthew Knepley /*@
28bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
29bdad233fSMatthew Knepley 
30bdad233fSMatthew Knepley    Collective on TS
31bdad233fSMatthew Knepley 
32bdad233fSMatthew Knepley    Input Parameter:
33bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
34bdad233fSMatthew Knepley 
35bdad233fSMatthew Knepley    Options Database Keys:
36d2567f34SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP, TSIRK
37ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
38ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
394a658b32SHong Zhang .  -ts_time_span <t0,...tf> - sets the time span, solutions are computed and stored for each indicated time
40ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
41ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
424dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
433e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
441628793fSSatish 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
45a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
46a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
47a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
48a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
4967b8a455SSatish Balay .  -ts_atol <atol> - Absolute tolerance for local truncation error
50f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
51f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
5267b8a455SSatish Balay .  -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
53847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
54bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
55aee7a9fbSMatthew G. Knepley .  -ts_monitor_cancel - Cancel all monitors
56de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
57de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
587cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
596934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
608b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
61de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
62de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
63de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
64de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
653e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
663e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
67fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
68e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
699e336e28SPatrick Sanan -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
7053ea634cSHong Zhang 
713d5a8a6aSBarry Smith    Notes:
723d5a8a6aSBarry Smith      See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper.
733d5a8a6aSBarry Smith 
743d5a8a6aSBarry Smith      Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order
753d5a8a6aSBarry Smith      to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and
763d5a8a6aSBarry Smith      -snes_lag_preconditioner_persists true
773d5a8a6aSBarry Smith 
789e336e28SPatrick Sanan    Developer Note:
799e336e28SPatrick Sanan      We should unify all the -ts_monitor options in the way that -xxx_view has been unified
80bdad233fSMatthew Knepley 
81bdad233fSMatthew Knepley    Level: beginner
82bdad233fSMatthew Knepley 
83a313700dSBarry Smith .seealso: TSGetType()
84bdad233fSMatthew Knepley @*/
857087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
86bdad233fSMatthew Knepley {
87bc952696SBarry Smith   PetscBool              opt,flg,tflg;
88dfbe8321SBarry Smith   PetscErrorCode         ierr;
89eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
904a658b32SHong Zhang   PetscReal              time_step,tspan[100];
914a658b32SHong Zhang   PetscInt               nt = PETSC_STATIC_ARRAY_LENGTH(tspan);
9249354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
93d1212d36SBarry Smith   char                   dir[16];
94cd11d68dSLisandro Dalcin   TSIFunction            ifun;
956991f827SBarry Smith   const char             *defaultType;
966991f827SBarry Smith   char                   typeName[256];
97bdad233fSMatthew Knepley 
98bdad233fSMatthew Knepley   PetscFunctionBegin;
990700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1006991f827SBarry Smith 
1019566063dSJacob Faibussowitsch   PetscCall(TSRegisterAll());
1029566063dSJacob Faibussowitsch   PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL));
103cd11d68dSLisandro Dalcin 
1049566063dSJacob Faibussowitsch   ierr = PetscObjectOptionsBegin((PetscObject)ts);PetscCall(ierr);
1051ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1061ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt));
1086991f827SBarry Smith   if (opt) {
1099566063dSJacob Faibussowitsch     PetscCall(TSSetType(ts,typeName));
1106991f827SBarry Smith   } else {
1119566063dSJacob Faibussowitsch     PetscCall(TSSetType(ts,defaultType));
1126991f827SBarry Smith   }
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley   /* Handle generic TS options */
1159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL));
1169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL));
1174a658b32SHong Zhang   PetscCall(PetscOptionsRealArray("-ts_time_span","Time span","TSSetTimeSpan",tspan,&nt,&flg));
1184a658b32SHong Zhang   if (flg) PetscCall(TSSetTimeSpan(ts,nt,tspan));
1199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL));
1209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL));
1219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg));
1229566063dSJacob Faibussowitsch   if (flg) PetscCall(TSSetTimeStep(ts,time_step));
1239566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg));
1249566063dSJacob Faibussowitsch   if (flg) PetscCall(TSSetExactFinalTime(ts,eftopt));
1259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL));
1269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL));
1279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL));
1289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL));
1299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL));
130bdad233fSMatthew Knepley 
1319566063dSJacob 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));
1329566063dSJacob 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));
1339566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL));
13456f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
13556f85f32SBarry Smith   {
13656f85f32SBarry Smith     PetscBool set;
13756f85f32SBarry Smith     flg  = PETSC_FALSE;
1389566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set));
13956f85f32SBarry Smith     if (set) {
1409566063dSJacob Faibussowitsch       PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts,flg));
14156f85f32SBarry Smith     }
14256f85f32SBarry Smith   }
14356f85f32SBarry Smith #endif
14456f85f32SBarry Smith 
145bdad233fSMatthew Knepley   /* Monitor options */
1469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL));
1479566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL));
1489566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL));
1499566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL));
1509566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts,"-ts_dmswarm_monitor_moments","Monitor moments of particle distribution","TSDMSwarmMonitorMoments",TSDMSwarmMonitorMoments,NULL));
151fde5950dSBarry Smith 
1529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg));
1539566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts,monfilename));
1545180491cSLisandro Dalcin 
1559566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt));
156b3603a34SBarry Smith   if (opt) {
1573923b477SBarry Smith     PetscInt       howoften = 1;
158e669de00SBarry Smith     DM             dm;
159e669de00SBarry Smith     PetscBool      net;
160b3603a34SBarry Smith 
1619566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL));
1629566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts,&dm));
1639566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net));
164e669de00SBarry Smith     if (net) {
165e669de00SBarry Smith       TSMonitorLGCtxNetwork ctx;
1669566063dSJacob Faibussowitsch       PetscCall(TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx));
1679566063dSJacob Faibussowitsch       PetscCall(TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy));
1689566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL));
169e669de00SBarry Smith     } else {
170e669de00SBarry Smith       TSMonitorLGCtx ctx;
1719566063dSJacob Faibussowitsch       PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx));
1729566063dSJacob Faibussowitsch       PetscCall(TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy));
173bdad233fSMatthew Knepley     }
174e669de00SBarry Smith   }
1756ba87a44SLisandro Dalcin 
1769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt));
177ef20d060SBarry Smith   if (opt) {
1780b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1793923b477SBarry Smith     PetscInt       howoften = 1;
180ef20d060SBarry Smith 
1819566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL));
1829566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx));
1839566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy));
184ef20d060SBarry Smith   }
1859566063dSJacob Faibussowitsch   PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL));
1867cf37e64SBarry Smith 
1879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt));
1886934998bSLisandro Dalcin   if (opt) {
1896934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
1906934998bSLisandro Dalcin     PetscInt       howoften = 1;
1916934998bSLisandro Dalcin 
1929566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL));
1939566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx));
1949566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy));
1956934998bSLisandro Dalcin   }
1969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt));
1978b668821SLisandro Dalcin   if (opt) {
1988b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
1998b668821SLisandro Dalcin     PetscInt       howoften = 1;
2008b668821SLisandro Dalcin 
2019566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL));
2029566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx));
2039566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy));
2048b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2058b668821SLisandro Dalcin   }
2068b668821SLisandro Dalcin 
2079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt));
208201da799SBarry Smith   if (opt) {
209201da799SBarry Smith     TSMonitorLGCtx ctx;
210201da799SBarry Smith     PetscInt       howoften = 1;
211201da799SBarry Smith 
2129566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL));
2139566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx));
2149566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy));
215201da799SBarry Smith   }
2169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt));
217201da799SBarry Smith   if (opt) {
218201da799SBarry Smith     TSMonitorLGCtx ctx;
219201da799SBarry Smith     PetscInt       howoften = 1;
220201da799SBarry Smith 
2219566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL));
2229566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx));
2239566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy));
224201da799SBarry Smith   }
2259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt));
2268189c53fSBarry Smith   if (opt) {
2278189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2288189c53fSBarry Smith     PetscInt          howoften = 1;
2298189c53fSBarry Smith 
2309566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL));
2319566063dSJacob Faibussowitsch     PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx));
2329566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy));
2338189c53fSBarry Smith   }
2349566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt));
2351b575b74SJoseph Pusztay   if (opt) {
2361b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
237d7462660SMatthew Knepley     PetscInt        howoften = 1, retain = 0;
238*6a5217c0SMatthew G. Knepley     PetscBool       phase = PETSC_TRUE, create = PETSC_TRUE;
239d7462660SMatthew Knepley 
240*6a5217c0SMatthew G. Knepley     for (PetscInt i = 0; i < ts->numbermonitors; ++i) if (ts->monitor[i] == TSMonitorSPSwarmSolution) {create = PETSC_FALSE;break;}
241*6a5217c0SMatthew G. Knepley     if (create) {
2429566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL));
2439566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL));
2449566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL));
2459566063dSJacob Faibussowitsch       PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject) ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx));
2469566063dSJacob Faibussowitsch       PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy));
2471b575b74SJoseph Pusztay     }
248*6a5217c0SMatthew G. Knepley   }
249ef20d060SBarry Smith   opt  = PETSC_FALSE;
2509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt));
251a7cc72afSBarry Smith   if (opt) {
25283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
25383a4ac43SBarry Smith     PetscInt         howoften = 1;
254a80ad3e0SBarry Smith 
2559566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL));
2569566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx));
2579566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
258bdad233fSMatthew Knepley   }
259fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt));
2612d5ee99bSBarry Smith   if (opt) {
2622d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2632d5ee99bSBarry Smith     PetscReal        bounds[4];
2642d5ee99bSBarry Smith     PetscInt         n = 4;
2652d5ee99bSBarry Smith     PetscDraw        draw;
2666934998bSLisandro Dalcin     PetscDrawAxis    axis;
2672d5ee99bSBarry Smith 
2689566063dSJacob Faibussowitsch     PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL));
2693c633725SBarry Smith     PetscCheck(n == 4,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2709566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx));
2719566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(ctx->viewer,0,&draw));
2729566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis));
2739566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]));
2749566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2"));
2759566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
2762d5ee99bSBarry Smith   }
2772d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt));
2793a471f94SBarry Smith   if (opt) {
28083a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28183a4ac43SBarry Smith     PetscInt         howoften = 1;
2823a471f94SBarry Smith 
2839566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL));
2849566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx));
2859566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
2863a471f94SBarry Smith   }
2870ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
2889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt));
2890ed3bfb6SBarry Smith   if (opt) {
2900ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
2910ed3bfb6SBarry Smith     PetscInt         howoften = 1;
2920ed3bfb6SBarry Smith 
2939566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL));
2949566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx));
2959566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
2960ed3bfb6SBarry Smith   }
297fde5950dSBarry Smith 
298ed81e22dSJed Brown   opt  = PETSC_FALSE;
2999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",NULL,monfilename,sizeof(monfilename),&flg));
300ed81e22dSJed Brown   if (flg) {
301ed81e22dSJed Brown     const char *ptr,*ptr2;
302ed81e22dSJed Brown     char       *filetemplate;
3033c633725SBarry Smith     PetscCheck(monfilename[0],PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
304ed81e22dSJed Brown     /* Do some cursory validation of the input. */
3059566063dSJacob Faibussowitsch     PetscCall(PetscStrstr(monfilename,"%",(char**)&ptr));
3063c633725SBarry Smith     PetscCheck(ptr,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
307ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
3089566063dSJacob Faibussowitsch       PetscCall(PetscStrchr("DdiouxX",*ptr,(char**)&ptr2));
3093c633725SBarry Smith       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-%%03D.vts");
310ed81e22dSJed Brown       if (ptr2) break;
311ed81e22dSJed Brown     }
3129566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(monfilename,&filetemplate));
3139566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy));
314ed81e22dSJed Brown   }
315bdad233fSMatthew Knepley 
3169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg));
317d1212d36SBarry Smith   if (flg) {
318d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
319d1212d36SBarry Smith     int                  ray = 0;
3203ee9839eSMatthew G. Knepley     DMDirection          ddir;
321d1212d36SBarry Smith     DM                   da;
322d1212d36SBarry Smith     PetscMPIInt          rank;
323d1212d36SBarry Smith 
3243c633725SBarry Smith     PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3253ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3263ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
32798921bdaSJacob Faibussowitsch     else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
328d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
329d1212d36SBarry Smith 
3309566063dSJacob Faibussowitsch     PetscCall(PetscInfo(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray));
3319566063dSJacob Faibussowitsch     PetscCall(PetscNew(&rayctx));
3329566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts,&da));
3339566063dSJacob Faibussowitsch     PetscCall(DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter));
3349566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank));
335dd400576SPatrick Sanan     if (rank == 0) {
3369566063dSJacob Faibussowitsch       PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer));
337d1212d36SBarry Smith     }
33851b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
3399566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy));
340d1212d36SBarry Smith   }
3419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg));
34251b4a12fSMatthew G. Knepley   if (flg) {
34351b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
34451b4a12fSMatthew G. Knepley     int                 ray = 0;
3453ee9839eSMatthew G. Knepley     DMDirection         ddir;
34651b4a12fSMatthew G. Knepley     DM                  da;
34751b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
348d1212d36SBarry Smith 
3493c633725SBarry Smith     PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed 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 direction %s", dir);
35351b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3541c3436cfSJed Brown 
3559566063dSJacob Faibussowitsch     PetscCall(PetscInfo(((PetscObject) ts),"Displaying LG 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     PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx));
3609566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy));
36151b4a12fSMatthew G. Knepley   }
362a7a1495cSBarry Smith 
3639566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt));
364b3d3934dSBarry Smith   if (opt) {
365b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
366b3d3934dSBarry Smith 
3679566063dSJacob Faibussowitsch     PetscCall(TSMonitorEnvelopeCtxCreate(ts,&ctx));
3689566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy));
369b3d3934dSBarry Smith   }
370aee7a9fbSMatthew G. Knepley   flg  = PETSC_FALSE;
3719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt));
3729566063dSJacob Faibussowitsch   if (opt && flg) PetscCall(TSMonitorCancel(ts));
373b3d3934dSBarry Smith 
374847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
3759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL));
376847ff0e1SMatthew G. Knepley   if (flg) {
377847ff0e1SMatthew G. Knepley     DM   dm;
378847ff0e1SMatthew G. Knepley     DMTS tdm;
379847ff0e1SMatthew G. Knepley 
3809566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &dm));
3819566063dSJacob Faibussowitsch     PetscCall(DMGetDMTS(dm, &tdm));
382847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
3839566063dSJacob Faibussowitsch     PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL));
3849566063dSJacob Faibussowitsch     PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n"));
385847ff0e1SMatthew G. Knepley   }
386847ff0e1SMatthew G. Knepley 
387d763cef2SBarry Smith   /* Handle specific TS options */
388d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3899566063dSJacob Faibussowitsch     PetscCall((*ts->ops->setfromoptions)(PetscOptionsObject,ts));
390d763cef2SBarry Smith   }
391fbc52257SHong Zhang 
392a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
3939566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts,&ts->adapt));
3949566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type));
3959566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt));
396a7bdc993SLisandro Dalcin 
39768bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
3984f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
3999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL));
40068bece0bSHong Zhang   if (tflg) {
4019566063dSJacob Faibussowitsch     PetscCall(TSSetSaveTrajectory(ts));
40268bece0bSHong Zhang   }
403a05bf03eSHong Zhang 
4049566063dSJacob Faibussowitsch   PetscCall(TSAdjointSetFromOptions(PetscOptionsObject,ts));
405d763cef2SBarry Smith 
406d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4079566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts));
4089566063dSJacob Faibussowitsch   ierr = PetscOptionsEnd();PetscCall(ierr);
409d763cef2SBarry Smith 
4104f122a70SLisandro Dalcin   if (ts->trajectory) {
4119566063dSJacob Faibussowitsch     PetscCall(TSTrajectorySetFromOptions(ts->trajectory,ts));
412d763cef2SBarry Smith   }
41368bece0bSHong Zhang 
4141ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4159566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&ts->snes));
4161ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4179566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,""));
4189566063dSJacob Faibussowitsch     if (!flg) PetscCall(SNESSetType(ts->snes,SNESKSPONLY));
4191ef27442SStefano Zampini   }
4209566063dSJacob Faibussowitsch   PetscCall(SNESSetFromOptions(ts->snes));
421d763cef2SBarry Smith   PetscFunctionReturn(0);
422d763cef2SBarry Smith }
423d763cef2SBarry Smith 
424d2daff3dSHong Zhang /*@
42578fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
42678fbdcc8SBarry Smith 
42778fbdcc8SBarry Smith    Collective on TS
42878fbdcc8SBarry Smith 
42978fbdcc8SBarry Smith    Input Parameters:
43078fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
43178fbdcc8SBarry Smith 
4327a7aea1fSJed Brown    Output Parameters:
43378fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
43478fbdcc8SBarry Smith 
43578fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
43678fbdcc8SBarry Smith 
43778fbdcc8SBarry Smith    Level: advanced
43878fbdcc8SBarry Smith 
43978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
44078fbdcc8SBarry Smith 
44178fbdcc8SBarry Smith @*/
44278fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
44378fbdcc8SBarry Smith {
44478fbdcc8SBarry Smith   PetscFunctionBegin;
44578fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44678fbdcc8SBarry Smith   *tr = ts->trajectory;
44778fbdcc8SBarry Smith   PetscFunctionReturn(0);
44878fbdcc8SBarry Smith }
44978fbdcc8SBarry Smith 
45078fbdcc8SBarry Smith /*@
451bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
452d2daff3dSHong Zhang 
453d2daff3dSHong Zhang    Collective on TS
454d2daff3dSHong Zhang 
455f899ff85SJose E. Roman    Input Parameter:
456bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
457bc952696SBarry Smith 
45878fbdcc8SBarry Smith    Options Database:
45978fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
46067b8a455SSatish Balay -  -ts_trajectory_type type - set trajectory type
46178fbdcc8SBarry Smith 
46268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
463d2daff3dSHong Zhang 
464c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
46578fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
46678fbdcc8SBarry Smith 
467d2daff3dSHong Zhang    Level: intermediate
468d2daff3dSHong Zhang 
4692d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
470d2daff3dSHong Zhang 
471d2daff3dSHong Zhang @*/
472bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
473d2daff3dSHong Zhang {
474d2daff3dSHong Zhang   PetscFunctionBegin;
475d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
476bc952696SBarry Smith   if (!ts->trajectory) {
4779566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory));
478bc952696SBarry Smith   }
479d2daff3dSHong Zhang   PetscFunctionReturn(0);
480d2daff3dSHong Zhang }
481d2daff3dSHong Zhang 
482a7a1495cSBarry Smith /*@
4832d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
4842d29f1f2SStefano Zampini 
4852d29f1f2SStefano Zampini    Collective on TS
4862d29f1f2SStefano Zampini 
4872d29f1f2SStefano Zampini    Input Parameters:
4882d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
4892d29f1f2SStefano Zampini 
4902d29f1f2SStefano Zampini    Level: intermediate
4912d29f1f2SStefano Zampini 
49267a3cfb0SHong Zhang .seealso: TSGetTrajectory(), TSAdjointSolve(), TSRemoveTrajectory()
4932d29f1f2SStefano Zampini 
4942d29f1f2SStefano Zampini @*/
4952d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
4962d29f1f2SStefano Zampini {
4972d29f1f2SStefano Zampini   PetscFunctionBegin;
4982d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4992d29f1f2SStefano Zampini   if (ts->trajectory) {
5009566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryDestroy(&ts->trajectory));
5019566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory));
5022d29f1f2SStefano Zampini   }
5032d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5042d29f1f2SStefano Zampini }
5052d29f1f2SStefano Zampini 
5062d29f1f2SStefano Zampini /*@
50767a3cfb0SHong Zhang    TSRemoveTrajectory - Destroys and removes the internal TSTrajectory object from TS
50867a3cfb0SHong Zhang 
50967a3cfb0SHong Zhang    Collective on TS
51067a3cfb0SHong Zhang 
51167a3cfb0SHong Zhang    Input Parameters:
51267a3cfb0SHong Zhang .  ts - the TS context obtained from TSCreate()
51367a3cfb0SHong Zhang 
51467a3cfb0SHong Zhang    Level: intermediate
51567a3cfb0SHong Zhang 
51667a3cfb0SHong Zhang .seealso: TSResetTrajectory(), TSAdjointSolve()
51767a3cfb0SHong Zhang 
51867a3cfb0SHong Zhang @*/
51967a3cfb0SHong Zhang PetscErrorCode TSRemoveTrajectory(TS ts)
52067a3cfb0SHong Zhang {
52167a3cfb0SHong Zhang   PetscFunctionBegin;
52267a3cfb0SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52367a3cfb0SHong Zhang   if (ts->trajectory) {
5249566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryDestroy(&ts->trajectory));
52567a3cfb0SHong Zhang   }
52667a3cfb0SHong Zhang   PetscFunctionReturn(0);
52767a3cfb0SHong Zhang }
52867a3cfb0SHong Zhang 
52967a3cfb0SHong Zhang /*@
530a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
531a7a1495cSBarry Smith       set with TSSetRHSJacobian().
532a7a1495cSBarry Smith 
533d083f849SBarry Smith    Collective on TS
534a7a1495cSBarry Smith 
535a7a1495cSBarry Smith    Input Parameters:
536316643e7SJed Brown +  ts - the TS context
537a7a1495cSBarry Smith .  t - current timestep
5380910c330SBarry Smith -  U - input vector
539a7a1495cSBarry Smith 
540a7a1495cSBarry Smith    Output Parameters:
541a7a1495cSBarry Smith +  A - Jacobian matrix
5426b867d5aSJose E. Roman -  B - optional preconditioning matrix
543a7a1495cSBarry Smith 
544a7a1495cSBarry Smith    Notes:
545a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
546a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
547a7a1495cSBarry Smith 
548a7a1495cSBarry Smith    Level: developer
549a7a1495cSBarry Smith 
55094b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
551a7a1495cSBarry Smith @*/
552d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
553a7a1495cSBarry Smith {
554270bf2e7SJed Brown   PetscObjectState Ustate;
5556c1e1eecSBarry Smith   PetscObjectId    Uid;
55624989b8cSPeter Brune   DM               dm;
557942e3340SBarry Smith   DMTS             tsdm;
55824989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
55924989b8cSPeter Brune   void             *ctx;
560b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
561a7a1495cSBarry Smith 
562a7a1495cSBarry Smith   PetscFunctionBegin;
5630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5640910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5650910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
5669566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
5679566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
5689566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
5699566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx));
5709566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate));
5719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetId((PetscObject)U,&Uid));
572971015bcSStefano Zampini 
5732663174eSHong Zhang   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0);
574d90be118SSean Farley 
5753c633725SBarry Smith   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.",ts->rhsjacobian.shift);
57624989b8cSPeter Brune   if (rhsjacobianfunc) {
5779566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B));
578a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
5799566063dSJacob Faibussowitsch     PetscCall((*rhsjacobianfunc)(ts,t,U,A,B,ctx));
580a7a1495cSBarry Smith     PetscStackPop;
581a6ab3590SBarry Smith     ts->rhsjacs++;
5829566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B));
583ef66eb69SBarry Smith   } else {
5849566063dSJacob Faibussowitsch     PetscCall(MatZeroEntries(A));
5859566063dSJacob Faibussowitsch     if (B && A != B) PetscCall(MatZeroEntries(B));
586ef66eb69SBarry Smith   }
5870e4ef248SJed Brown   ts->rhsjacobian.time  = t;
588971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
589971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
5909566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid));
5919566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate));
592a7a1495cSBarry Smith   PetscFunctionReturn(0);
593a7a1495cSBarry Smith }
594a7a1495cSBarry Smith 
595316643e7SJed Brown /*@
596d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
597d763cef2SBarry Smith 
598d083f849SBarry Smith    Collective on TS
599316643e7SJed Brown 
600316643e7SJed Brown    Input Parameters:
601316643e7SJed Brown +  ts - the TS context
602316643e7SJed Brown .  t - current time
6030910c330SBarry Smith -  U - state vector
604316643e7SJed Brown 
605316643e7SJed Brown    Output Parameter:
606316643e7SJed Brown .  y - right hand side
607316643e7SJed Brown 
608316643e7SJed Brown    Note:
609316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
610316643e7SJed Brown    is used internally within the nonlinear solvers.
611316643e7SJed Brown 
612316643e7SJed Brown    Level: developer
613316643e7SJed Brown 
614316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
615316643e7SJed Brown @*/
6160910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
617d763cef2SBarry Smith {
61824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
61924989b8cSPeter Brune   TSIFunction    ifunction;
62024989b8cSPeter Brune   void           *ctx;
62124989b8cSPeter Brune   DM             dm;
62224989b8cSPeter Brune 
623d763cef2SBarry Smith   PetscFunctionBegin;
6240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6250910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6260700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
6279566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
6289566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,&ctx));
6299566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm,&ifunction,NULL));
630d763cef2SBarry Smith 
6313c633725SBarry Smith   PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
632d763cef2SBarry Smith 
63324989b8cSPeter Brune   if (rhsfunction) {
6349566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,y,0));
6359566063dSJacob Faibussowitsch     PetscCall(VecLockReadPush(U));
636d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6379566063dSJacob Faibussowitsch     PetscCall((*rhsfunction)(ts,t,U,y,ctx));
638d763cef2SBarry Smith     PetscStackPop;
6399566063dSJacob Faibussowitsch     PetscCall(VecLockReadPop(U));
640a6ab3590SBarry Smith     ts->rhsfuncs++;
6419566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,y,0));
642214bc6a2SJed Brown   } else {
6439566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(y));
644b2cd27e8SJed Brown   }
645d763cef2SBarry Smith   PetscFunctionReturn(0);
646d763cef2SBarry Smith }
647d763cef2SBarry Smith 
648ef20d060SBarry Smith /*@
649ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
650ef20d060SBarry Smith 
651d083f849SBarry Smith    Collective on TS
652ef20d060SBarry Smith 
653ef20d060SBarry Smith    Input Parameters:
654ef20d060SBarry Smith +  ts - the TS context
655ef20d060SBarry Smith -  t - current time
656ef20d060SBarry Smith 
657ef20d060SBarry Smith    Output Parameter:
6580910c330SBarry Smith .  U - the solution
659ef20d060SBarry Smith 
660ef20d060SBarry Smith    Note:
661ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
662ef20d060SBarry Smith    is used internally within the nonlinear solvers.
663ef20d060SBarry Smith 
664ef20d060SBarry Smith    Level: developer
665ef20d060SBarry Smith 
666abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
667ef20d060SBarry Smith @*/
6680910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
669ef20d060SBarry Smith {
670ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
671ef20d060SBarry Smith   void               *ctx;
672ef20d060SBarry Smith   DM                 dm;
673ef20d060SBarry Smith 
674ef20d060SBarry Smith   PetscFunctionBegin;
675ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6760910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6779566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
6789566063dSJacob Faibussowitsch   PetscCall(DMTSGetSolutionFunction(dm,&solutionfunction,&ctx));
679ef20d060SBarry Smith 
680ef20d060SBarry Smith   if (solutionfunction) {
6819b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6829566063dSJacob Faibussowitsch     PetscCall((*solutionfunction)(ts,t,U,ctx));
683ef20d060SBarry Smith     PetscStackPop;
684ef20d060SBarry Smith   }
685ef20d060SBarry Smith   PetscFunctionReturn(0);
686ef20d060SBarry Smith }
6879b7cd975SBarry Smith /*@
6889b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6899b7cd975SBarry Smith 
690d083f849SBarry Smith    Collective on TS
6919b7cd975SBarry Smith 
6929b7cd975SBarry Smith    Input Parameters:
6939b7cd975SBarry Smith +  ts - the TS context
6949b7cd975SBarry Smith -  t - current time
6959b7cd975SBarry Smith 
6969b7cd975SBarry Smith    Output Parameter:
6979b7cd975SBarry Smith .  U - the function value
6989b7cd975SBarry Smith 
6999b7cd975SBarry Smith    Note:
7009b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7019b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7029b7cd975SBarry Smith 
7039b7cd975SBarry Smith    Level: developer
7049b7cd975SBarry Smith 
7059b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7069b7cd975SBarry Smith @*/
7079b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7089b7cd975SBarry Smith {
7099b7cd975SBarry Smith   void              *ctx;
7109b7cd975SBarry Smith   DM                 dm;
7115f80ce2aSJacob Faibussowitsch   TSForcingFunction  forcing;
7129b7cd975SBarry Smith 
7139b7cd975SBarry Smith   PetscFunctionBegin;
7149b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7159b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7169566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
7179566063dSJacob Faibussowitsch   PetscCall(DMTSGetForcingFunction(dm,&forcing,&ctx));
7189b7cd975SBarry Smith 
7199b7cd975SBarry Smith   if (forcing) {
7209b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7219566063dSJacob Faibussowitsch     PetscCall((*forcing)(ts,t,U,ctx));
7229b7cd975SBarry Smith     PetscStackPop;
7239b7cd975SBarry Smith   }
7249b7cd975SBarry Smith   PetscFunctionReturn(0);
7259b7cd975SBarry Smith }
726ef20d060SBarry Smith 
727214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
728214bc6a2SJed Brown {
7292dd45cf8SJed Brown   Vec            F;
730214bc6a2SJed Brown 
731214bc6a2SJed Brown   PetscFunctionBegin;
7320298fd71SBarry Smith   *Frhs = NULL;
7339566063dSJacob Faibussowitsch   PetscCall(TSGetIFunction(ts,&F,NULL,NULL));
734214bc6a2SJed Brown   if (!ts->Frhs) {
7359566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(F,&ts->Frhs));
736214bc6a2SJed Brown   }
737214bc6a2SJed Brown   *Frhs = ts->Frhs;
738214bc6a2SJed Brown   PetscFunctionReturn(0);
739214bc6a2SJed Brown }
740214bc6a2SJed Brown 
741971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
742214bc6a2SJed Brown {
743214bc6a2SJed Brown   Mat            A,B;
74441a1d4d2SBarry Smith   TSIJacobian    ijacobian;
745214bc6a2SJed Brown 
746214bc6a2SJed Brown   PetscFunctionBegin;
747c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
748c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7499566063dSJacob Faibussowitsch   PetscCall(TSGetIJacobian(ts,&A,&B,&ijacobian,NULL));
750214bc6a2SJed Brown   if (Arhs) {
751214bc6a2SJed Brown     if (!ts->Arhs) {
75241a1d4d2SBarry Smith       if (ijacobian) {
7539566063dSJacob Faibussowitsch         PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs));
7549566063dSJacob Faibussowitsch         PetscCall(TSSetMatStructure(ts,SAME_NONZERO_PATTERN));
75541a1d4d2SBarry Smith       } else {
75641a1d4d2SBarry Smith         ts->Arhs = A;
7579566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)A));
75841a1d4d2SBarry Smith       }
7593565c898SBarry Smith     } else {
7603565c898SBarry Smith       PetscBool flg;
7619566063dSJacob Faibussowitsch       PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg));
7623565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7633565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs) {
7649566063dSJacob Faibussowitsch         PetscCall(PetscObjectDereference((PetscObject)ts->Arhs));
7653565c898SBarry Smith         ts->Arhs = A;
7669566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)A));
7673565c898SBarry Smith       }
768214bc6a2SJed Brown     }
769214bc6a2SJed Brown     *Arhs = ts->Arhs;
770214bc6a2SJed Brown   }
771214bc6a2SJed Brown   if (Brhs) {
772214bc6a2SJed Brown     if (!ts->Brhs) {
773bdb70873SJed Brown       if (A != B) {
77441a1d4d2SBarry Smith         if (ijacobian) {
7759566063dSJacob Faibussowitsch           PetscCall(MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs));
776bdb70873SJed Brown         } else {
77741a1d4d2SBarry Smith           ts->Brhs = B;
7789566063dSJacob Faibussowitsch           PetscCall(PetscObjectReference((PetscObject)B));
77941a1d4d2SBarry Smith         }
78041a1d4d2SBarry Smith       } else {
7819566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)ts->Arhs));
78251699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
783bdb70873SJed Brown       }
784214bc6a2SJed Brown     }
785214bc6a2SJed Brown     *Brhs = ts->Brhs;
786214bc6a2SJed Brown   }
787214bc6a2SJed Brown   PetscFunctionReturn(0);
788214bc6a2SJed Brown }
789214bc6a2SJed Brown 
790316643e7SJed Brown /*@
7910910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
792316643e7SJed Brown 
793d083f849SBarry Smith    Collective on TS
794316643e7SJed Brown 
795316643e7SJed Brown    Input Parameters:
796316643e7SJed Brown +  ts - the TS context
797316643e7SJed Brown .  t - current time
7980910c330SBarry Smith .  U - state vector
7990910c330SBarry Smith .  Udot - time derivative of state vector
800214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
801316643e7SJed Brown 
802316643e7SJed Brown    Output Parameter:
803316643e7SJed Brown .  Y - right hand side
804316643e7SJed Brown 
805316643e7SJed Brown    Note:
806316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
807316643e7SJed Brown    is used internally within the nonlinear solvers.
808316643e7SJed Brown 
809316643e7SJed Brown    If the user did did not write their equations in implicit form, this
810316643e7SJed Brown    function recasts them in implicit form.
811316643e7SJed Brown 
812316643e7SJed Brown    Level: developer
813316643e7SJed Brown 
814316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
815316643e7SJed Brown @*/
8160910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
817316643e7SJed Brown {
81824989b8cSPeter Brune   TSIFunction    ifunction;
81924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
82024989b8cSPeter Brune   void           *ctx;
82124989b8cSPeter Brune   DM             dm;
822316643e7SJed Brown 
823316643e7SJed Brown   PetscFunctionBegin;
8240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8250910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8260910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8270700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
828316643e7SJed Brown 
8299566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
8309566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm,&ifunction,&ctx));
8319566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
83224989b8cSPeter Brune 
8333c633725SBarry Smith   PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
834d90be118SSean Farley 
8359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y));
83624989b8cSPeter Brune   if (ifunction) {
837316643e7SJed Brown     PetscStackPush("TS user implicit function");
8389566063dSJacob Faibussowitsch     PetscCall((*ifunction)(ts,t,U,Udot,Y,ctx));
839316643e7SJed Brown     PetscStackPop;
840a6ab3590SBarry Smith     ts->ifuncs++;
841214bc6a2SJed Brown   }
842214bc6a2SJed Brown   if (imex) {
84324989b8cSPeter Brune     if (!ifunction) {
8449566063dSJacob Faibussowitsch       PetscCall(VecCopy(Udot,Y));
8452dd45cf8SJed Brown     }
84624989b8cSPeter Brune   } else if (rhsfunction) {
84724989b8cSPeter Brune     if (ifunction) {
848214bc6a2SJed Brown       Vec Frhs;
8499566063dSJacob Faibussowitsch       PetscCall(TSGetRHSVec_Private(ts,&Frhs));
8509566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSFunction(ts,t,U,Frhs));
8519566063dSJacob Faibussowitsch       PetscCall(VecAXPY(Y,-1,Frhs));
8522dd45cf8SJed Brown     } else {
8539566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSFunction(ts,t,U,Y));
8549566063dSJacob Faibussowitsch       PetscCall(VecAYPX(Y,-1,Udot));
855316643e7SJed Brown     }
8564a6899ffSJed Brown   }
8579566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y));
858316643e7SJed Brown   PetscFunctionReturn(0);
859316643e7SJed Brown }
860316643e7SJed Brown 
861cfa8a9a2SHong Zhang /*
862cfa8a9a2SHong Zhang    TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it.
863cfa8a9a2SHong Zhang 
864cfa8a9a2SHong Zhang    Note:
865cfa8a9a2SHong Zhang    This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian().
866cfa8a9a2SHong Zhang 
867cfa8a9a2SHong Zhang */
868cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B)
869cfa8a9a2SHong Zhang {
870cfa8a9a2SHong Zhang   PetscFunctionBegin;
871cfa8a9a2SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8723c633725SBarry Smith   PetscCheck(A == ts->Arhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat");
8733c633725SBarry Smith   PetscCheck(B == ts->Brhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat");
874cfa8a9a2SHong Zhang 
875cfa8a9a2SHong Zhang   if (ts->rhsjacobian.shift) {
8769566063dSJacob Faibussowitsch     PetscCall(MatShift(A,-ts->rhsjacobian.shift));
877cfa8a9a2SHong Zhang   }
878cfa8a9a2SHong Zhang   if (ts->rhsjacobian.scale == -1.) {
8799566063dSJacob Faibussowitsch     PetscCall(MatScale(A,-1));
880cfa8a9a2SHong Zhang   }
881cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
882cfa8a9a2SHong Zhang     if (ts->rhsjacobian.shift) {
8839566063dSJacob Faibussowitsch       PetscCall(MatShift(B,-ts->rhsjacobian.shift));
884cfa8a9a2SHong Zhang     }
885cfa8a9a2SHong Zhang     if (ts->rhsjacobian.scale == -1.) {
8869566063dSJacob Faibussowitsch       PetscCall(MatScale(B,-1));
887cfa8a9a2SHong Zhang     }
888cfa8a9a2SHong Zhang   }
889cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
890cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
891cfa8a9a2SHong Zhang   PetscFunctionReturn(0);
892cfa8a9a2SHong Zhang }
893cfa8a9a2SHong Zhang 
894316643e7SJed Brown /*@
895316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
896316643e7SJed Brown 
897d083f849SBarry Smith    Collective on TS
898316643e7SJed Brown 
899316643e7SJed Brown    Input
900316643e7SJed Brown       Input Parameters:
901316643e7SJed Brown +  ts - the TS context
902316643e7SJed Brown .  t - current timestep
9030910c330SBarry Smith .  U - state vector
9040910c330SBarry Smith .  Udot - time derivative of state vector
905214bc6a2SJed Brown .  shift - shift to apply, see note below
906214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
907316643e7SJed Brown 
908316643e7SJed Brown    Output Parameters:
909316643e7SJed Brown +  A - Jacobian matrix
9103565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
911316643e7SJed Brown 
912316643e7SJed Brown    Notes:
9130910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
914316643e7SJed Brown 
9150910c330SBarry Smith    dF/dU + shift*dF/dUdot
916316643e7SJed Brown 
917316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
918316643e7SJed Brown    is used internally within the nonlinear solvers.
919316643e7SJed Brown 
920316643e7SJed Brown    Level: developer
921316643e7SJed Brown 
922316643e7SJed Brown .seealso:  TSSetIJacobian()
92363495f91SJed Brown @*/
924d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
925316643e7SJed Brown {
92624989b8cSPeter Brune   TSIJacobian    ijacobian;
92724989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
92824989b8cSPeter Brune   DM             dm;
92924989b8cSPeter Brune   void           *ctx;
930316643e7SJed Brown 
931316643e7SJed Brown   PetscFunctionBegin;
9320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9330910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9340910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
935316643e7SJed Brown   PetscValidPointer(A,6);
93694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
937316643e7SJed Brown   PetscValidPointer(B,7);
93894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
93924989b8cSPeter Brune 
9409566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
9419566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,&ijacobian,&ctx));
9429566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL));
94324989b8cSPeter Brune 
9443c633725SBarry Smith   PetscCheck(rhsjacobian || ijacobian,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
945316643e7SJed Brown 
9469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B));
94724989b8cSPeter Brune   if (ijacobian) {
948316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
9499566063dSJacob Faibussowitsch     PetscCall((*ijacobian)(ts,t,U,Udot,shift,A,B,ctx));
950a6ab3590SBarry Smith     ts->ijacs++;
951316643e7SJed Brown     PetscStackPop;
9524a6899ffSJed Brown   }
953214bc6a2SJed Brown   if (imex) {
954b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9554c26be97Sstefano_zampini       PetscBool assembled;
956971015bcSStefano Zampini       if (rhsjacobian) {
957971015bcSStefano Zampini         Mat Arhs = NULL;
9589566063dSJacob Faibussowitsch         PetscCall(TSGetRHSMats_Private(ts,&Arhs,NULL));
959971015bcSStefano Zampini         if (A == Arhs) {
9603c633725SBarry 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 */
961971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
962971015bcSStefano Zampini         }
963971015bcSStefano Zampini       }
9649566063dSJacob Faibussowitsch       PetscCall(MatZeroEntries(A));
9659566063dSJacob Faibussowitsch       PetscCall(MatAssembled(A,&assembled));
9664c26be97Sstefano_zampini       if (!assembled) {
9679566063dSJacob Faibussowitsch         PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
9689566063dSJacob Faibussowitsch         PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
9694c26be97Sstefano_zampini       }
9709566063dSJacob Faibussowitsch       PetscCall(MatShift(A,shift));
97194ab13aaSBarry Smith       if (A != B) {
9729566063dSJacob Faibussowitsch         PetscCall(MatZeroEntries(B));
9739566063dSJacob Faibussowitsch         PetscCall(MatAssembled(B,&assembled));
9744c26be97Sstefano_zampini         if (!assembled) {
9759566063dSJacob Faibussowitsch           PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
9769566063dSJacob Faibussowitsch           PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
9774c26be97Sstefano_zampini         }
9789566063dSJacob Faibussowitsch         PetscCall(MatShift(B,shift));
979214bc6a2SJed Brown       }
980214bc6a2SJed Brown     }
981214bc6a2SJed Brown   } else {
982e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
983e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
9849566063dSJacob Faibussowitsch       PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs));
985e1244c69SJed Brown     }
986e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
987e8b1e424SHong Zhang       PetscObjectState Ustate;
988e8b1e424SHong Zhang       PetscObjectId    Uid;
989e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
990e8b1e424SHong Zhang 
9919566063dSJacob Faibussowitsch       PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
9929566063dSJacob Faibussowitsch       PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate));
9939566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetId((PetscObject)U,&Uid));
994097a96a2SHong Zhang       if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */
9959566063dSJacob Faibussowitsch         PetscCall(MatShift(A,shift-ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */
99634d322a1SHong Zhang         if (A != B) {
9979566063dSJacob Faibussowitsch           PetscCall(MatShift(B,shift-ts->rhsjacobian.shift));
99834d322a1SHong Zhang         }
999e8b1e424SHong Zhang       } else {
10003565c898SBarry Smith         PetscBool flg;
1001e8b1e424SHong Zhang 
1002e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1003e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1004e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1005e8b1e424SHong Zhang              and the matrices have not been assembled yet */
10069566063dSJacob Faibussowitsch           PetscCall(TSRecoverRHSJacobian(ts,A,B));
1007e8b1e424SHong Zhang         }
10089566063dSJacob Faibussowitsch         PetscCall(TSComputeRHSJacobian(ts,t,U,A,B));
10099566063dSJacob Faibussowitsch         PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg));
10103565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10113565c898SBarry Smith         if (!flg) {
10129566063dSJacob Faibussowitsch           PetscCall(MatScale(A,-1));
10139566063dSJacob Faibussowitsch           PetscCall(MatShift(A,shift));
10143565c898SBarry Smith         }
101594ab13aaSBarry Smith         if (A != B) {
10169566063dSJacob Faibussowitsch           PetscCall(MatScale(B,-1));
10179566063dSJacob Faibussowitsch           PetscCall(MatShift(B,shift));
1018316643e7SJed Brown         }
1019e8b1e424SHong Zhang       }
1020e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1021e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1022d60b7d5cSBarry Smith     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1023e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
10249566063dSJacob Faibussowitsch         PetscCall(MatZeroEntries(A));
10259566063dSJacob Faibussowitsch         PetscCall(MatShift(A,shift));
102694ab13aaSBarry Smith         if (A != B) {
10279566063dSJacob Faibussowitsch           PetscCall(MatZeroEntries(B));
10289566063dSJacob Faibussowitsch           PetscCall(MatShift(B,shift));
1029214bc6a2SJed Brown         }
1030316643e7SJed Brown       }
10319566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs));
10329566063dSJacob Faibussowitsch       PetscCall(MatAXPY(A,-1,Arhs,ts->axpy_pattern));
103394ab13aaSBarry Smith       if (A != B) {
10349566063dSJacob Faibussowitsch         PetscCall(MatAXPY(B,-1,Brhs,ts->axpy_pattern));
1035316643e7SJed Brown       }
1036316643e7SJed Brown     }
1037316643e7SJed Brown   }
10389566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B));
1039316643e7SJed Brown   PetscFunctionReturn(0);
1040316643e7SJed Brown }
1041316643e7SJed Brown 
1042d763cef2SBarry Smith /*@C
1043d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1044b5abc632SBarry Smith     where U_t = G(t,u).
1045d763cef2SBarry Smith 
10463f9fe445SBarry Smith     Logically Collective on TS
1047d763cef2SBarry Smith 
1048d763cef2SBarry Smith     Input Parameters:
1049d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10500298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1051d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1052d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10530298fd71SBarry Smith           function evaluation routine (may be NULL)
1054d763cef2SBarry Smith 
1055a96d6ef6SBarry Smith     Calling sequence of f:
1056a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1057d763cef2SBarry Smith 
1058a96d6ef6SBarry Smith +   ts - timestep context
1059a96d6ef6SBarry Smith .   t - current timestep
1060d763cef2SBarry Smith .   u - input vector
1061d763cef2SBarry Smith .   F - function vector
1062d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith     Level: beginner
1065d763cef2SBarry Smith 
106695452b02SPatrick Sanan     Notes:
106795452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10682bbac0d3SBarry Smith 
1069ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1070d763cef2SBarry Smith @*/
1071089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1072d763cef2SBarry Smith {
1073089b2837SJed Brown   SNES           snes;
10740298fd71SBarry Smith   Vec            ralloc = NULL;
107524989b8cSPeter Brune   DM             dm;
1076d763cef2SBarry Smith 
1077089b2837SJed Brown   PetscFunctionBegin;
10780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1079ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
108024989b8cSPeter Brune 
10819566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
10829566063dSJacob Faibussowitsch   PetscCall(DMTSSetRHSFunction(dm,f,ctx));
10839566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
1084e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
10859566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->vec_sol,&ralloc));
1086e856ceecSJed Brown     r = ralloc;
1087e856ceecSJed Brown   }
10889566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts));
10899566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ralloc));
1090d763cef2SBarry Smith   PetscFunctionReturn(0);
1091d763cef2SBarry Smith }
1092d763cef2SBarry Smith 
1093ef20d060SBarry Smith /*@C
1094abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1095ef20d060SBarry Smith 
1096ef20d060SBarry Smith     Logically Collective on TS
1097ef20d060SBarry Smith 
1098ef20d060SBarry Smith     Input Parameters:
1099ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1100ef20d060SBarry Smith .   f - routine for evaluating the solution
1101ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11020298fd71SBarry Smith           function evaluation routine (may be NULL)
1103ef20d060SBarry Smith 
1104a96d6ef6SBarry Smith     Calling sequence of f:
1105a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1106ef20d060SBarry Smith 
1107ef20d060SBarry Smith +   t - current timestep
1108ef20d060SBarry Smith .   u - output vector
1109ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1110ef20d060SBarry Smith 
11110ed3bfb6SBarry Smith     Options Database:
11120ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11130ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11140ed3bfb6SBarry Smith 
1115abd5a294SJed Brown     Notes:
1116abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1117abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1118abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1119abd5a294SJed Brown 
11204f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1121abd5a294SJed Brown 
1122ef20d060SBarry Smith     Level: beginner
1123ef20d060SBarry Smith 
11240ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1125ef20d060SBarry Smith @*/
1126ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1127ef20d060SBarry Smith {
1128ef20d060SBarry Smith   DM             dm;
1129ef20d060SBarry Smith 
1130ef20d060SBarry Smith   PetscFunctionBegin;
1131ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11329566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
11339566063dSJacob Faibussowitsch   PetscCall(DMTSSetSolutionFunction(dm,f,ctx));
1134ef20d060SBarry Smith   PetscFunctionReturn(0);
1135ef20d060SBarry Smith }
1136ef20d060SBarry Smith 
11379b7cd975SBarry Smith /*@C
11389b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11399b7cd975SBarry Smith 
11409b7cd975SBarry Smith     Logically Collective on TS
11419b7cd975SBarry Smith 
11429b7cd975SBarry Smith     Input Parameters:
11439b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1144e162b725SBarry Smith .   func - routine for evaluating the forcing function
11459b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11460298fd71SBarry Smith           function evaluation routine (may be NULL)
11479b7cd975SBarry Smith 
11489b7cd975SBarry Smith     Calling sequence of func:
11496bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11509b7cd975SBarry Smith 
11519b7cd975SBarry Smith +   t - current timestep
1152e162b725SBarry Smith .   f - output vector
11539b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11549b7cd975SBarry Smith 
11559b7cd975SBarry Smith     Notes:
11569b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1157e162b725SBarry 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
1158e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1159e162b725SBarry Smith 
1160e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1161e162b725SBarry Smith 
1162e162b725SBarry 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
1163e162b725SBarry Smith     parameters can be passed in the ctx variable.
11649b7cd975SBarry Smith 
11659b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11669b7cd975SBarry Smith 
11679b7cd975SBarry Smith     Level: beginner
11689b7cd975SBarry Smith 
11699b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11709b7cd975SBarry Smith @*/
1171e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11729b7cd975SBarry Smith {
11739b7cd975SBarry Smith   DM             dm;
11749b7cd975SBarry Smith 
11759b7cd975SBarry Smith   PetscFunctionBegin;
11769b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11779566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
11789566063dSJacob Faibussowitsch   PetscCall(DMTSSetForcingFunction(dm,func,ctx));
11799b7cd975SBarry Smith   PetscFunctionReturn(0);
11809b7cd975SBarry Smith }
11819b7cd975SBarry Smith 
1182d763cef2SBarry Smith /*@C
1183f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1184b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1185d763cef2SBarry Smith 
11863f9fe445SBarry Smith    Logically Collective on TS
1187d763cef2SBarry Smith 
1188d763cef2SBarry Smith    Input Parameters:
1189d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1190e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1191e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1192d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1193d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11940298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1195d763cef2SBarry Smith 
1196f7ab8db6SBarry Smith    Calling sequence of f:
1197a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1198d763cef2SBarry Smith 
1199d763cef2SBarry Smith +  t - current timestep
1200d763cef2SBarry Smith .  u - input vector
1201e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1202e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1203d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1204d763cef2SBarry Smith 
12056cd88445SBarry Smith    Notes:
12066cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12076cd88445SBarry Smith 
12086cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1209ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1210d763cef2SBarry Smith 
1211d763cef2SBarry Smith    Level: beginner
1212d763cef2SBarry Smith 
1213ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1214d763cef2SBarry Smith 
1215d763cef2SBarry Smith @*/
1216e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1217d763cef2SBarry Smith {
1218089b2837SJed Brown   SNES           snes;
121924989b8cSPeter Brune   DM             dm;
122024989b8cSPeter Brune   TSIJacobian    ijacobian;
1221277b19d0SLisandro Dalcin 
1222d763cef2SBarry Smith   PetscFunctionBegin;
12230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1224e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1225e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1226e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1227e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1228d763cef2SBarry Smith 
12299566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
12309566063dSJacob Faibussowitsch   PetscCall(DMTSSetRHSJacobian(dm,f,ctx));
12319566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,&ijacobian,NULL));
12329566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
12335f659677SPeter Brune   if (!ijacobian) {
12349566063dSJacob Faibussowitsch     PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts));
12350e4ef248SJed Brown   }
1236e5d3d808SBarry Smith   if (Amat) {
12379566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)Amat));
12389566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&ts->Arhs));
1239e5d3d808SBarry Smith     ts->Arhs = Amat;
12400e4ef248SJed Brown   }
1241e5d3d808SBarry Smith   if (Pmat) {
12429566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)Pmat));
12439566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&ts->Brhs));
1244e5d3d808SBarry Smith     ts->Brhs = Pmat;
12450e4ef248SJed Brown   }
1246d763cef2SBarry Smith   PetscFunctionReturn(0);
1247d763cef2SBarry Smith }
1248d763cef2SBarry Smith 
1249316643e7SJed Brown /*@C
1250b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1251316643e7SJed Brown 
12523f9fe445SBarry Smith    Logically Collective on TS
1253316643e7SJed Brown 
1254316643e7SJed Brown    Input Parameters:
1255316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12560298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1257316643e7SJed Brown .  f   - the function evaluation routine
12580298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1259316643e7SJed Brown 
1260316643e7SJed Brown    Calling sequence of f:
12616bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1262316643e7SJed Brown 
1263316643e7SJed Brown +  t   - time at step/stage being solved
1264316643e7SJed Brown .  u   - state vector
1265316643e7SJed Brown .  u_t - time derivative of state vector
1266316643e7SJed Brown .  F   - function vector
1267316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1268316643e7SJed Brown 
1269316643e7SJed Brown    Important:
12702bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1271316643e7SJed Brown 
1272316643e7SJed Brown    Level: beginner
1273316643e7SJed Brown 
1274d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1275316643e7SJed Brown @*/
127651699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1277316643e7SJed Brown {
1278089b2837SJed Brown   SNES           snes;
127951699248SLisandro Dalcin   Vec            ralloc = NULL;
128024989b8cSPeter Brune   DM             dm;
1281316643e7SJed Brown 
1282316643e7SJed Brown   PetscFunctionBegin;
12830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
128451699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
128524989b8cSPeter Brune 
12869566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
12879566063dSJacob Faibussowitsch   PetscCall(DMTSSetIFunction(dm,f,ctx));
128824989b8cSPeter Brune 
12899566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
129051699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
12919566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->vec_sol,&ralloc));
129251699248SLisandro Dalcin     r  = ralloc;
1293e856ceecSJed Brown   }
12949566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts));
12959566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ralloc));
1296089b2837SJed Brown   PetscFunctionReturn(0);
1297089b2837SJed Brown }
1298089b2837SJed Brown 
1299089b2837SJed Brown /*@C
1300a5b23f4aSJose E. Roman    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it.
1301089b2837SJed Brown 
1302089b2837SJed Brown    Not Collective
1303089b2837SJed Brown 
1304089b2837SJed Brown    Input Parameter:
1305089b2837SJed Brown .  ts - the TS context
1306089b2837SJed Brown 
1307d8d19677SJose E. Roman    Output Parameters:
13080298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13090298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13100298fd71SBarry Smith -  ctx - the function context (or NULL)
1311089b2837SJed Brown 
1312089b2837SJed Brown    Level: advanced
1313089b2837SJed Brown 
1314089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1315089b2837SJed Brown @*/
1316089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1317089b2837SJed Brown {
1318089b2837SJed Brown   SNES           snes;
131924989b8cSPeter Brune   DM             dm;
1320089b2837SJed Brown 
1321089b2837SJed Brown   PetscFunctionBegin;
1322089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13239566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
13249566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes,r,NULL,NULL));
13259566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
13269566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm,func,ctx));
1327089b2837SJed Brown   PetscFunctionReturn(0);
1328089b2837SJed Brown }
1329089b2837SJed Brown 
1330089b2837SJed Brown /*@C
1331089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1332089b2837SJed Brown 
1333089b2837SJed Brown    Not Collective
1334089b2837SJed Brown 
1335089b2837SJed Brown    Input Parameter:
1336089b2837SJed Brown .  ts - the TS context
1337089b2837SJed Brown 
1338d8d19677SJose E. Roman    Output Parameters:
13390298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13400298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13410298fd71SBarry Smith -  ctx - the function context (or NULL)
1342089b2837SJed Brown 
1343089b2837SJed Brown    Level: advanced
1344089b2837SJed Brown 
13452bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1346089b2837SJed Brown @*/
1347089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1348089b2837SJed Brown {
1349089b2837SJed Brown   SNES           snes;
135024989b8cSPeter Brune   DM             dm;
1351089b2837SJed Brown 
1352089b2837SJed Brown   PetscFunctionBegin;
1353089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13549566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
13559566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes,r,NULL,NULL));
13569566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
13579566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,func,ctx));
1358316643e7SJed Brown   PetscFunctionReturn(0);
1359316643e7SJed Brown }
1360316643e7SJed Brown 
1361316643e7SJed Brown /*@C
1362a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1363ae8867d6SBarry Smith         provided with TSSetIFunction().
1364316643e7SJed Brown 
13653f9fe445SBarry Smith    Logically Collective on TS
1366316643e7SJed Brown 
1367316643e7SJed Brown    Input Parameters:
1368316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1369e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1370e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1371316643e7SJed Brown .  f   - the Jacobian evaluation routine
13720298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1373316643e7SJed Brown 
1374316643e7SJed Brown    Calling sequence of f:
13756bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1376316643e7SJed Brown 
1377316643e7SJed Brown +  t    - time at step/stage being solved
13781b4a444bSJed Brown .  U    - state vector
13791b4a444bSJed Brown .  U_t  - time derivative of state vector
1380316643e7SJed Brown .  a    - shift
1381e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1382e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1383316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1384316643e7SJed Brown 
1385316643e7SJed Brown    Notes:
1386e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1387316643e7SJed Brown 
1388895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1389895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1390895c21f2SBarry Smith 
1391a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1392b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1393a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1394a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1395a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1396a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1397a4f0a591SBarry Smith 
13986cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13996cd88445SBarry Smith 
14006cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1401ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1402ca5f011dSBarry Smith 
1403316643e7SJed Brown    Level: beginner
1404316643e7SJed Brown 
1405ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1406316643e7SJed Brown 
1407316643e7SJed Brown @*/
1408e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1409316643e7SJed Brown {
1410089b2837SJed Brown   SNES           snes;
141124989b8cSPeter Brune   DM             dm;
1412316643e7SJed Brown 
1413316643e7SJed Brown   PetscFunctionBegin;
14140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1415e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1416e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1417e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1418e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
141924989b8cSPeter Brune 
14209566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
14219566063dSJacob Faibussowitsch   PetscCall(DMTSSetIJacobian(dm,f,ctx));
142224989b8cSPeter Brune 
14239566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
14249566063dSJacob Faibussowitsch   PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts));
1425316643e7SJed Brown   PetscFunctionReturn(0);
1426316643e7SJed Brown }
1427316643e7SJed Brown 
1428e1244c69SJed Brown /*@
1429e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1430e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1431e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1432e1244c69SJed Brown    not been changed by the TS.
1433e1244c69SJed Brown 
1434e1244c69SJed Brown    Logically Collective
1435e1244c69SJed Brown 
14364165533cSJose E. Roman    Input Parameters:
1437e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1438e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1439e1244c69SJed Brown 
1440e1244c69SJed Brown    Level: intermediate
1441e1244c69SJed Brown 
1442e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1443e1244c69SJed Brown @*/
1444e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1445e1244c69SJed Brown {
1446e1244c69SJed Brown   PetscFunctionBegin;
1447e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1448e1244c69SJed Brown   PetscFunctionReturn(0);
1449e1244c69SJed Brown }
1450e1244c69SJed Brown 
1451efe9872eSLisandro Dalcin /*@C
1452efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1453efe9872eSLisandro Dalcin 
1454efe9872eSLisandro Dalcin    Logically Collective on TS
1455efe9872eSLisandro Dalcin 
1456efe9872eSLisandro Dalcin    Input Parameters:
1457efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1458efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1459efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1460efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1461efe9872eSLisandro Dalcin 
1462efe9872eSLisandro Dalcin    Calling sequence of fun:
14636bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1464efe9872eSLisandro Dalcin 
1465efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1466efe9872eSLisandro Dalcin .  U    - state vector
1467efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1468efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1469efe9872eSLisandro Dalcin .  F    - function vector
1470efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1471efe9872eSLisandro Dalcin 
1472efe9872eSLisandro Dalcin    Level: beginner
1473efe9872eSLisandro Dalcin 
1474a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1475efe9872eSLisandro Dalcin @*/
1476efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1477efe9872eSLisandro Dalcin {
1478efe9872eSLisandro Dalcin   DM             dm;
1479efe9872eSLisandro Dalcin 
1480efe9872eSLisandro Dalcin   PetscFunctionBegin;
1481efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1482efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
14839566063dSJacob Faibussowitsch   PetscCall(TSSetIFunction(ts,F,NULL,NULL));
14849566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
14859566063dSJacob Faibussowitsch   PetscCall(DMTSSetI2Function(dm,fun,ctx));
1486efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1487efe9872eSLisandro Dalcin }
1488efe9872eSLisandro Dalcin 
1489efe9872eSLisandro Dalcin /*@C
1490a5b23f4aSJose E. Roman   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it.
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin   Not Collective
1493efe9872eSLisandro Dalcin 
1494efe9872eSLisandro Dalcin   Input Parameter:
1495efe9872eSLisandro Dalcin . ts - the TS context
1496efe9872eSLisandro Dalcin 
1497d8d19677SJose E. Roman   Output Parameters:
1498efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1499efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1500efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1501efe9872eSLisandro Dalcin 
1502efe9872eSLisandro Dalcin   Level: advanced
1503efe9872eSLisandro Dalcin 
1504a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1505efe9872eSLisandro Dalcin @*/
1506efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1507efe9872eSLisandro Dalcin {
1508efe9872eSLisandro Dalcin   SNES           snes;
1509efe9872eSLisandro Dalcin   DM             dm;
1510efe9872eSLisandro Dalcin 
1511efe9872eSLisandro Dalcin   PetscFunctionBegin;
1512efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15139566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
15149566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes,r,NULL,NULL));
15159566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
15169566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Function(dm,fun,ctx));
1517efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1518efe9872eSLisandro Dalcin }
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin /*@C
1521bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1522efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin    Logically Collective on TS
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin    Input Parameters:
1527efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1528efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1529efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1530efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1531efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1532efe9872eSLisandro Dalcin 
1533efe9872eSLisandro Dalcin    Calling sequence of jac:
15346bc98fa9SBarry 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);
1535efe9872eSLisandro Dalcin 
1536efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1537efe9872eSLisandro Dalcin .  U    - state vector
1538efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1539efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1540efe9872eSLisandro Dalcin .  v    - shift for U_t
1541efe9872eSLisandro Dalcin .  a    - shift for U_tt
1542efe9872eSLisandro 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
1543efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1544efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1545efe9872eSLisandro Dalcin 
1546efe9872eSLisandro Dalcin    Notes:
1547efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1548efe9872eSLisandro Dalcin 
1549efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1550efe9872eSLisandro 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.
1551efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1552bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1553efe9872eSLisandro Dalcin 
1554efe9872eSLisandro Dalcin    Level: beginner
1555efe9872eSLisandro Dalcin 
1556a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1557efe9872eSLisandro Dalcin @*/
1558efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1559efe9872eSLisandro Dalcin {
1560efe9872eSLisandro Dalcin   DM             dm;
1561efe9872eSLisandro Dalcin 
1562efe9872eSLisandro Dalcin   PetscFunctionBegin;
1563efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1564efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1565efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
15669566063dSJacob Faibussowitsch   PetscCall(TSSetIJacobian(ts,J,P,NULL,NULL));
15679566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
15689566063dSJacob Faibussowitsch   PetscCall(DMTSSetI2Jacobian(dm,jac,ctx));
1569efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1570efe9872eSLisandro Dalcin }
1571efe9872eSLisandro Dalcin 
1572efe9872eSLisandro Dalcin /*@C
1573efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1574efe9872eSLisandro Dalcin 
1575efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1576efe9872eSLisandro Dalcin 
1577efe9872eSLisandro Dalcin   Input Parameter:
1578efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin   Output Parameters:
1581efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1582efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1583efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1584efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1585efe9872eSLisandro Dalcin 
158695452b02SPatrick Sanan   Notes:
158795452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1588efe9872eSLisandro Dalcin 
1589efe9872eSLisandro Dalcin   Level: advanced
1590efe9872eSLisandro Dalcin 
1591a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1592efe9872eSLisandro Dalcin 
1593efe9872eSLisandro Dalcin @*/
1594efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1595efe9872eSLisandro Dalcin {
1596efe9872eSLisandro Dalcin   SNES           snes;
1597efe9872eSLisandro Dalcin   DM             dm;
1598efe9872eSLisandro Dalcin 
1599efe9872eSLisandro Dalcin   PetscFunctionBegin;
16009566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
16019566063dSJacob Faibussowitsch   PetscCall(SNESSetUpMatrices(snes));
16029566063dSJacob Faibussowitsch   PetscCall(SNESGetJacobian(snes,J,P,NULL,NULL));
16039566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
16049566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm,jac,ctx));
1605efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1606efe9872eSLisandro Dalcin }
1607efe9872eSLisandro Dalcin 
1608efe9872eSLisandro Dalcin /*@
1609efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1610efe9872eSLisandro Dalcin 
1611d083f849SBarry Smith   Collective on TS
1612efe9872eSLisandro Dalcin 
1613efe9872eSLisandro Dalcin   Input Parameters:
1614efe9872eSLisandro Dalcin + ts - the TS context
1615efe9872eSLisandro Dalcin . t - current time
1616efe9872eSLisandro Dalcin . U - state vector
1617efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1618efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1619efe9872eSLisandro Dalcin 
1620efe9872eSLisandro Dalcin   Output Parameter:
1621efe9872eSLisandro Dalcin . F - the residual vector
1622efe9872eSLisandro Dalcin 
1623efe9872eSLisandro Dalcin   Note:
1624efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1625efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1626efe9872eSLisandro Dalcin 
1627efe9872eSLisandro Dalcin   Level: developer
1628efe9872eSLisandro Dalcin 
1629a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1630efe9872eSLisandro Dalcin @*/
1631efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1632efe9872eSLisandro Dalcin {
1633efe9872eSLisandro Dalcin   DM             dm;
1634efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1635efe9872eSLisandro Dalcin   void           *ctx;
1636efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1637efe9872eSLisandro Dalcin 
1638efe9872eSLisandro Dalcin   PetscFunctionBegin;
1639efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1640efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1641efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1642efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1643efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1644efe9872eSLisandro Dalcin 
16459566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
16469566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Function(dm,&I2Function,&ctx));
16479566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin   if (!I2Function) {
16509566063dSJacob Faibussowitsch     PetscCall(TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE));
1651efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1652efe9872eSLisandro Dalcin   }
1653efe9872eSLisandro Dalcin 
16549566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,V,F));
1655efe9872eSLisandro Dalcin 
1656efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
16579566063dSJacob Faibussowitsch   PetscCall(I2Function(ts,t,U,V,A,F,ctx));
1658efe9872eSLisandro Dalcin   PetscStackPop;
1659efe9872eSLisandro Dalcin 
1660efe9872eSLisandro Dalcin   if (rhsfunction) {
1661efe9872eSLisandro Dalcin     Vec Frhs;
16629566063dSJacob Faibussowitsch     PetscCall(TSGetRHSVec_Private(ts,&Frhs));
16639566063dSJacob Faibussowitsch     PetscCall(TSComputeRHSFunction(ts,t,U,Frhs));
16649566063dSJacob Faibussowitsch     PetscCall(VecAXPY(F,-1,Frhs));
1665efe9872eSLisandro Dalcin   }
1666efe9872eSLisandro Dalcin 
16679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,V,F));
1668efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1669efe9872eSLisandro Dalcin }
1670efe9872eSLisandro Dalcin 
1671efe9872eSLisandro Dalcin /*@
1672efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1673efe9872eSLisandro Dalcin 
1674d083f849SBarry Smith   Collective on TS
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   Input Parameters:
1677efe9872eSLisandro Dalcin + ts - the TS context
1678efe9872eSLisandro Dalcin . t - current timestep
1679efe9872eSLisandro Dalcin . U - state vector
1680efe9872eSLisandro Dalcin . V - time derivative of state vector
1681efe9872eSLisandro Dalcin . A - second time derivative of state vector
1682efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1683efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1684efe9872eSLisandro Dalcin 
1685efe9872eSLisandro Dalcin   Output Parameters:
1686efe9872eSLisandro Dalcin + J - Jacobian matrix
1687efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1688efe9872eSLisandro Dalcin 
1689efe9872eSLisandro Dalcin   Notes:
1690efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1691efe9872eSLisandro Dalcin 
1692efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1695efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1696efe9872eSLisandro Dalcin 
1697efe9872eSLisandro Dalcin   Level: developer
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1700efe9872eSLisandro Dalcin @*/
1701efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1702efe9872eSLisandro Dalcin {
1703efe9872eSLisandro Dalcin   DM             dm;
1704efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1705efe9872eSLisandro Dalcin   void           *ctx;
1706efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin   PetscFunctionBegin;
1709efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1710efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1711efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1712efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1713efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1714efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1715efe9872eSLisandro Dalcin 
17169566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
17179566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx));
17189566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL));
1719efe9872eSLisandro Dalcin 
1720efe9872eSLisandro Dalcin   if (!I2Jacobian) {
17219566063dSJacob Faibussowitsch     PetscCall(TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE));
1722efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1723efe9872eSLisandro Dalcin   }
1724efe9872eSLisandro Dalcin 
17259566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,J,P));
1726efe9872eSLisandro Dalcin 
1727efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
17289566063dSJacob Faibussowitsch   PetscCall(I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx));
1729efe9872eSLisandro Dalcin   PetscStackPop;
1730efe9872eSLisandro Dalcin 
1731efe9872eSLisandro Dalcin   if (rhsjacobian) {
1732d60b7d5cSBarry Smith     Mat Jrhs,Prhs;
17339566063dSJacob Faibussowitsch     PetscCall(TSGetRHSMats_Private(ts,&Jrhs,&Prhs));
17349566063dSJacob Faibussowitsch     PetscCall(TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs));
17359566063dSJacob Faibussowitsch     PetscCall(MatAXPY(J,-1,Jrhs,ts->axpy_pattern));
17369566063dSJacob Faibussowitsch     if (P != J) PetscCall(MatAXPY(P,-1,Prhs,ts->axpy_pattern));
1737efe9872eSLisandro Dalcin   }
1738efe9872eSLisandro Dalcin 
17399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,J,P));
1740efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1741efe9872eSLisandro Dalcin }
1742efe9872eSLisandro Dalcin 
1743438f35afSJed Brown /*@C
1744438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1745438f35afSJed Brown 
1746438f35afSJed Brown    Logically Collective
1747438f35afSJed Brown 
17484165533cSJose E. Roman    Input Parameters:
1749438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1750a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1751438f35afSJed Brown -  ctx - a context for tvar
1752438f35afSJed Brown 
1753a96d6ef6SBarry Smith     Calling sequence of tvar:
1754a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1755a96d6ef6SBarry Smith 
1756a96d6ef6SBarry Smith +   ts - timestep context
1757a96d6ef6SBarry Smith .   p - input vector (primative form)
1758a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1759a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1760a96d6ef6SBarry Smith 
1761438f35afSJed Brown    Level: advanced
1762438f35afSJed Brown 
1763438f35afSJed Brown    Notes:
1764438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1765438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1766438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1767438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1768438f35afSJed Brown    evaluated via the chain rule, as in
1769438f35afSJed Brown 
1770438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1771438f35afSJed Brown 
1772438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1773438f35afSJed Brown @*/
1774438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1775438f35afSJed Brown {
1776438f35afSJed Brown   DM             dm;
1777438f35afSJed Brown 
1778438f35afSJed Brown   PetscFunctionBegin;
1779438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17809566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
17819566063dSJacob Faibussowitsch   PetscCall(DMTSSetTransientVariable(dm,tvar,ctx));
1782438f35afSJed Brown   PetscFunctionReturn(0);
1783438f35afSJed Brown }
1784438f35afSJed Brown 
1785efe9872eSLisandro Dalcin /*@
1786e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1787e3c11fc1SJed Brown 
1788e3c11fc1SJed Brown    Logically Collective
1789e3c11fc1SJed Brown 
1790e3c11fc1SJed Brown    Input Parameters:
1791e3c11fc1SJed Brown +  ts - TS on which to compute
1792e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1793e3c11fc1SJed Brown 
1794e3c11fc1SJed Brown    Output Parameters:
1795e3c11fc1SJed Brown .  C - transient (conservative) variable
1796e3c11fc1SJed Brown 
1797e3c11fc1SJed Brown    Developer Notes:
1798e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1799e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1800e3c11fc1SJed Brown    being used.
1801e3c11fc1SJed Brown 
1802e3c11fc1SJed Brown    Level: developer
1803e3c11fc1SJed Brown 
1804e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1805e3c11fc1SJed Brown @*/
1806e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1807e3c11fc1SJed Brown {
1808e3c11fc1SJed Brown   DM             dm;
1809e3c11fc1SJed Brown   DMTS           dmts;
1810e3c11fc1SJed Brown 
1811e3c11fc1SJed Brown   PetscFunctionBegin;
1812e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1813e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
18149566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
18159566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&dmts));
1816e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1817e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
18189566063dSJacob Faibussowitsch     PetscCall((*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx));
1819e3c11fc1SJed Brown   }
1820e3c11fc1SJed Brown   PetscFunctionReturn(0);
1821e3c11fc1SJed Brown }
1822e3c11fc1SJed Brown 
1823e3c11fc1SJed Brown /*@
1824e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1825e3c11fc1SJed Brown 
1826e3c11fc1SJed Brown    Logically Collective
1827e3c11fc1SJed Brown 
1828e3c11fc1SJed Brown    Input Parameters:
1829e3c11fc1SJed Brown .  ts - TS on which to compute
1830e3c11fc1SJed Brown 
1831e3c11fc1SJed Brown    Output Parameters:
1832e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1833e3c11fc1SJed Brown 
1834e3c11fc1SJed Brown    Level: developer
1835e3c11fc1SJed Brown 
1836e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1837e3c11fc1SJed Brown @*/
1838e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1839e3c11fc1SJed Brown {
1840e3c11fc1SJed Brown   DM             dm;
1841e3c11fc1SJed Brown   DMTS           dmts;
1842e3c11fc1SJed Brown 
1843e3c11fc1SJed Brown   PetscFunctionBegin;
1844e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18459566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
18469566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&dmts));
1847e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1848e3c11fc1SJed Brown   PetscFunctionReturn(0);
1849e3c11fc1SJed Brown }
1850e3c11fc1SJed Brown 
1851e3c11fc1SJed Brown /*@
1852efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1853efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1854efe9872eSLisandro Dalcin 
1855d083f849SBarry Smith    Logically Collective on TS
1856efe9872eSLisandro Dalcin 
1857efe9872eSLisandro Dalcin    Input Parameters:
1858efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1859efe9872eSLisandro Dalcin .  u - the solution vector
1860efe9872eSLisandro Dalcin -  v - the time derivative vector
1861efe9872eSLisandro Dalcin 
1862efe9872eSLisandro Dalcin    Level: beginner
1863efe9872eSLisandro Dalcin 
1864efe9872eSLisandro Dalcin @*/
1865efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1866efe9872eSLisandro Dalcin {
1867efe9872eSLisandro Dalcin   PetscFunctionBegin;
1868efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1869efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1870efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
18719566063dSJacob Faibussowitsch   PetscCall(TSSetSolution(ts,u));
18729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)v));
18739566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_dot));
1874efe9872eSLisandro Dalcin   ts->vec_dot = v;
1875efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1876efe9872eSLisandro Dalcin }
1877efe9872eSLisandro Dalcin 
1878efe9872eSLisandro Dalcin /*@
1879efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1880efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1881efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1882efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1883efe9872eSLisandro Dalcin 
1884efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1885efe9872eSLisandro Dalcin 
1886efe9872eSLisandro Dalcin    Input Parameter:
1887efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1888efe9872eSLisandro Dalcin 
1889d8d19677SJose E. Roman    Output Parameters:
1890efe9872eSLisandro Dalcin +  u - the vector containing the solution
1891efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1892efe9872eSLisandro Dalcin 
1893efe9872eSLisandro Dalcin    Level: intermediate
1894efe9872eSLisandro Dalcin 
1895efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1896efe9872eSLisandro Dalcin 
1897efe9872eSLisandro Dalcin @*/
1898efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1899efe9872eSLisandro Dalcin {
1900efe9872eSLisandro Dalcin   PetscFunctionBegin;
1901efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1902efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1903efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1904efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1905efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1906efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1907efe9872eSLisandro Dalcin }
1908efe9872eSLisandro Dalcin 
190955849f57SBarry Smith /*@C
191055849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
191155849f57SBarry Smith 
191255849f57SBarry Smith   Collective on PetscViewer
191355849f57SBarry Smith 
191455849f57SBarry Smith   Input Parameters:
191555849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
191655849f57SBarry Smith            some related function before a call to TSLoad().
191755849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
191855849f57SBarry Smith 
191955849f57SBarry Smith    Level: intermediate
192055849f57SBarry Smith 
192155849f57SBarry Smith   Notes:
192255849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
192355849f57SBarry Smith 
192455849f57SBarry Smith   Notes for advanced users:
192555849f57SBarry Smith   Most users should not need to know the details of the binary storage
192655849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
192755849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
192855849f57SBarry Smith   format is
192955849f57SBarry Smith .vb
193055849f57SBarry Smith      has not yet been determined
193155849f57SBarry Smith .ve
193255849f57SBarry Smith 
193355849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
193455849f57SBarry Smith @*/
1935f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
193655849f57SBarry Smith {
193755849f57SBarry Smith   PetscBool      isbinary;
193855849f57SBarry Smith   PetscInt       classid;
193955849f57SBarry Smith   char           type[256];
19402d53ad75SBarry Smith   DMTS           sdm;
1941ad6bc421SBarry Smith   DM             dm;
194255849f57SBarry Smith 
194355849f57SBarry Smith   PetscFunctionBegin;
1944f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
194555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
19469566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
19473c633725SBarry Smith   PetscCheck(isbinary,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
194855849f57SBarry Smith 
19499566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
19503c633725SBarry Smith   PetscCheck(classid == TS_FILE_CLASSID,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
19519566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
19529566063dSJacob Faibussowitsch   PetscCall(TSSetType(ts, type));
1953f2c2a1b9SBarry Smith   if (ts->ops->load) {
19549566063dSJacob Faibussowitsch     PetscCall((*ts->ops->load)(ts,viewer));
1955f2c2a1b9SBarry Smith   }
19569566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)ts),&dm));
19579566063dSJacob Faibussowitsch   PetscCall(DMLoad(dm,viewer));
19589566063dSJacob Faibussowitsch   PetscCall(TSSetDM(ts,dm));
19599566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol));
19609566063dSJacob Faibussowitsch   PetscCall(VecLoad(ts->vec_sol,viewer));
19619566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(ts->dm,&sdm));
19629566063dSJacob Faibussowitsch   PetscCall(DMTSLoad(sdm,viewer));
196355849f57SBarry Smith   PetscFunctionReturn(0);
196455849f57SBarry Smith }
196555849f57SBarry Smith 
19669804daf3SBarry Smith #include <petscdraw.h>
1967e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1968e04113cfSBarry Smith #include <petscviewersaws.h>
1969f05ece33SBarry Smith #endif
1970fe2efc57SMark 
1971fe2efc57SMark /*@C
1972fe2efc57SMark    TSViewFromOptions - View from Options
1973fe2efc57SMark 
1974fe2efc57SMark    Collective on TS
1975fe2efc57SMark 
1976fe2efc57SMark    Input Parameters:
1977fe2efc57SMark +  A - the application ordering context
1978736c3998SJose E. Roman .  obj - Optional object
1979736c3998SJose E. Roman -  name - command line option
1980fe2efc57SMark 
1981fe2efc57SMark    Level: intermediate
1982fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
1983fe2efc57SMark @*/
1984fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
1985fe2efc57SMark {
1986fe2efc57SMark   PetscFunctionBegin;
1987fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
19889566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name));
1989fe2efc57SMark   PetscFunctionReturn(0);
1990fe2efc57SMark }
1991fe2efc57SMark 
19927e2c5f70SBarry Smith /*@C
1993d763cef2SBarry Smith     TSView - Prints the TS data structure.
1994d763cef2SBarry Smith 
19954c49b128SBarry Smith     Collective on TS
1996d763cef2SBarry Smith 
1997d763cef2SBarry Smith     Input Parameters:
1998d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1999d763cef2SBarry Smith -   viewer - visualization context
2000d763cef2SBarry Smith 
2001d763cef2SBarry Smith     Options Database Key:
2002d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2003d763cef2SBarry Smith 
2004d763cef2SBarry Smith     Notes:
2005d763cef2SBarry Smith     The available visualization contexts include
2006b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2007b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2008d763cef2SBarry Smith          output where only the first processor opens
2009d763cef2SBarry Smith          the file.  All other processors send their
2010d763cef2SBarry Smith          data to the first processor to print.
2011d763cef2SBarry Smith 
2012d763cef2SBarry Smith     The user can open an alternative visualization context with
2013b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2014d763cef2SBarry Smith 
2015595c91d4SBarry Smith     In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer).
2016595c91d4SBarry Smith 
2017d763cef2SBarry Smith     Level: beginner
2018d763cef2SBarry Smith 
2019b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2020d763cef2SBarry Smith @*/
20217087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2022d763cef2SBarry Smith {
202319fd82e9SBarry Smith   TSType         type;
20242b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20252d53ad75SBarry Smith   DMTS           sdm;
2026e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2027536b137fSBarry Smith   PetscBool      issaws;
2028f05ece33SBarry Smith #endif
2029d763cef2SBarry Smith 
2030d763cef2SBarry Smith   PetscFunctionBegin;
20310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20323050cee2SBarry Smith   if (!viewer) {
20339566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer));
20343050cee2SBarry Smith   }
20350700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2036c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2037fd16b177SBarry Smith 
20389566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii));
20399566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring));
20409566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
20419566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw));
2042e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
20439566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws));
2044f05ece33SBarry Smith #endif
204532077d6dSBarry Smith   if (iascii) {
20469566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer));
2047efd4aadfSBarry Smith     if (ts->ops->view) {
20489566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
20499566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
20509566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
2051efd4aadfSBarry Smith     }
2052ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
20539566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps));
2054ef85077eSLisandro Dalcin     }
2055ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20569566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time));
2057ef85077eSLisandro Dalcin     }
2058a6ab3590SBarry Smith     if (ts->ifuncs) {
20599566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of I function evaluations=%D\n",ts->ifuncs));
2060a6ab3590SBarry Smith     }
2061a6ab3590SBarry Smith     if (ts->ijacs) {
20629566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of I Jacobian evaluations=%D\n",ts->ijacs));
2063a6ab3590SBarry Smith     }
2064a6ab3590SBarry Smith     if (ts->rhsfuncs) {
20659566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of RHS function evaluations=%D\n",ts->rhsfuncs));
2066a6ab3590SBarry Smith     }
2067a6ab3590SBarry Smith     if (ts->rhsjacs) {
20689566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of RHS Jacobian evaluations=%D\n",ts->rhsjacs));
2069a6ab3590SBarry Smith     }
2070efd4aadfSBarry Smith     if (ts->usessnes) {
2071efd4aadfSBarry Smith       PetscBool lin;
2072d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20739566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its));
2074d763cef2SBarry Smith       }
20759566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its));
20769566063dSJacob Faibussowitsch       PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,""));
20779566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures));
2078efd4aadfSBarry Smith     }
20799566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject));
2080a0af407cSBarry Smith     if (ts->vrtol) {
20819566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, "));
2082a0af407cSBarry Smith     } else {
20839566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol));
2084a0af407cSBarry Smith     }
2085a0af407cSBarry Smith     if (ts->vatol) {
20869566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n"));
2087a0af407cSBarry Smith     } else {
20889566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol));
2089a0af407cSBarry Smith     }
20909566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
20919566063dSJacob Faibussowitsch     PetscCall(TSAdaptView(ts->adapt,viewer));
20929566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
20930f5bd95cSBarry Smith   } else if (isstring) {
20949566063dSJacob Faibussowitsch     PetscCall(TSGetType(ts,&type));
20959566063dSJacob Faibussowitsch     PetscCall(PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type));
20969566063dSJacob Faibussowitsch     if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer));
209755849f57SBarry Smith   } else if (isbinary) {
209855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
209955849f57SBarry Smith     MPI_Comm    comm;
210055849f57SBarry Smith     PetscMPIInt rank;
210155849f57SBarry Smith     char        type[256];
210255849f57SBarry Smith 
21039566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetComm((PetscObject)ts,&comm));
21049566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(comm,&rank));
2105dd400576SPatrick Sanan     if (rank == 0) {
21069566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT));
21079566063dSJacob Faibussowitsch       PetscCall(PetscStrncpy(type,((PetscObject)ts)->type_name,256));
21089566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR));
210955849f57SBarry Smith     }
211055849f57SBarry Smith     if (ts->ops->view) {
21119566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
211255849f57SBarry Smith     }
21139566063dSJacob Faibussowitsch     if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer));
21149566063dSJacob Faibussowitsch     PetscCall(DMView(ts->dm,viewer));
21159566063dSJacob Faibussowitsch     PetscCall(VecView(ts->vec_sol,viewer));
21169566063dSJacob Faibussowitsch     PetscCall(DMGetDMTS(ts->dm,&sdm));
21179566063dSJacob Faibussowitsch     PetscCall(DMTSView(sdm,viewer));
21182b0a91c0SBarry Smith   } else if (isdraw) {
21192b0a91c0SBarry Smith     PetscDraw draw;
21202b0a91c0SBarry Smith     char      str[36];
212189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21222b0a91c0SBarry Smith 
21239566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw));
21249566063dSJacob Faibussowitsch     PetscCall(PetscDrawGetCurrentPoint(draw,&x,&y));
21259566063dSJacob Faibussowitsch     PetscCall(PetscStrcpy(str,"TS: "));
21269566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(str,((PetscObject)ts)->type_name));
21279566063dSJacob Faibussowitsch     PetscCall(PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h));
212889fd9fafSBarry Smith     bottom = y - h;
21299566063dSJacob Faibussowitsch     PetscCall(PetscDrawPushCurrentPoint(draw,x,bottom));
21302b0a91c0SBarry Smith     if (ts->ops->view) {
21319566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
21322b0a91c0SBarry Smith     }
21339566063dSJacob Faibussowitsch     if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer));
21349566063dSJacob Faibussowitsch     if (ts->snes)  PetscCall(SNESView(ts->snes,viewer));
21359566063dSJacob Faibussowitsch     PetscCall(PetscDrawPopCurrentPoint(draw));
2136e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2137536b137fSBarry Smith   } else if (issaws) {
2138d45a07a7SBarry Smith     PetscMPIInt rank;
21392657e9d9SBarry Smith     const char  *name;
21402657e9d9SBarry Smith 
21419566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)ts,&name));
21429566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
2143dd400576SPatrick Sanan     if (!((PetscObject)ts)->amsmem && rank == 0) {
2144d45a07a7SBarry Smith       char       dir[1024];
2145d45a07a7SBarry Smith 
21469566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)ts,viewer));
21479566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name));
21482657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21499566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name));
21502657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2151d763cef2SBarry Smith     }
21520acecf5bSBarry Smith     if (ts->ops->view) {
21539566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
21540acecf5bSBarry Smith     }
2155f05ece33SBarry Smith #endif
2156f05ece33SBarry Smith   }
215736a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
21589566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
21599566063dSJacob Faibussowitsch     PetscCall(SNESView(ts->snes,viewer));
21609566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
216136a9e3b9SBarry Smith   }
21629566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(ts->dm,&sdm));
21639566063dSJacob Faibussowitsch   PetscCall(DMTSView(sdm,viewer));
2164f05ece33SBarry Smith 
21659566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushTab(viewer));
21669566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials));
21679566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopTab(viewer));
2168d763cef2SBarry Smith   PetscFunctionReturn(0);
2169d763cef2SBarry Smith }
2170d763cef2SBarry Smith 
2171b07ff414SBarry Smith /*@
2172d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2173d763cef2SBarry Smith    the timesteppers.
2174d763cef2SBarry Smith 
21753f9fe445SBarry Smith    Logically Collective on TS
2176d763cef2SBarry Smith 
2177d763cef2SBarry Smith    Input Parameters:
2178d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2179d763cef2SBarry Smith -  usrP - optional user context
2180d763cef2SBarry Smith 
218195452b02SPatrick Sanan    Fortran Notes:
218295452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2183daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2184daf670e6SBarry Smith 
2185d763cef2SBarry Smith    Level: intermediate
2186d763cef2SBarry Smith 
2187d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2188d763cef2SBarry Smith @*/
21897087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2190d763cef2SBarry Smith {
2191d763cef2SBarry Smith   PetscFunctionBegin;
21920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2193d763cef2SBarry Smith   ts->user = usrP;
2194d763cef2SBarry Smith   PetscFunctionReturn(0);
2195d763cef2SBarry Smith }
2196d763cef2SBarry Smith 
2197b07ff414SBarry Smith /*@
2198d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2199d763cef2SBarry Smith     timestepper.
2200d763cef2SBarry Smith 
2201d763cef2SBarry Smith     Not Collective
2202d763cef2SBarry Smith 
2203d763cef2SBarry Smith     Input Parameter:
2204d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2205d763cef2SBarry Smith 
2206d763cef2SBarry Smith     Output Parameter:
2207d763cef2SBarry Smith .   usrP - user context
2208d763cef2SBarry Smith 
220995452b02SPatrick Sanan    Fortran Notes:
221095452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2211daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2212daf670e6SBarry Smith 
2213d763cef2SBarry Smith     Level: intermediate
2214d763cef2SBarry Smith 
2215d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2216d763cef2SBarry Smith @*/
2217e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2218d763cef2SBarry Smith {
2219d763cef2SBarry Smith   PetscFunctionBegin;
22200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2221e71120c6SJed Brown   *(void**)usrP = ts->user;
2222d763cef2SBarry Smith   PetscFunctionReturn(0);
2223d763cef2SBarry Smith }
2224d763cef2SBarry Smith 
2225d763cef2SBarry Smith /*@
222680275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2227d763cef2SBarry Smith 
2228d763cef2SBarry Smith    Not Collective
2229d763cef2SBarry Smith 
2230d763cef2SBarry Smith    Input Parameter:
2231d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2232d763cef2SBarry Smith 
2233d763cef2SBarry Smith    Output Parameter:
223480275a0aSLisandro Dalcin .  steps - number of steps completed so far
2235d763cef2SBarry Smith 
2236d763cef2SBarry Smith    Level: intermediate
2237d763cef2SBarry Smith 
22389be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2239d763cef2SBarry Smith @*/
224080275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2241d763cef2SBarry Smith {
2242d763cef2SBarry Smith   PetscFunctionBegin;
22430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224480275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
224580275a0aSLisandro Dalcin   *steps = ts->steps;
224680275a0aSLisandro Dalcin   PetscFunctionReturn(0);
224780275a0aSLisandro Dalcin }
224880275a0aSLisandro Dalcin 
224980275a0aSLisandro Dalcin /*@
225080275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
225180275a0aSLisandro Dalcin 
225280275a0aSLisandro Dalcin    Logically Collective on TS
225380275a0aSLisandro Dalcin 
225480275a0aSLisandro Dalcin    Input Parameters:
225580275a0aSLisandro Dalcin +  ts - the TS context
225680275a0aSLisandro Dalcin -  steps - number of steps completed so far
225780275a0aSLisandro Dalcin 
225880275a0aSLisandro Dalcin    Notes:
225980275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
226080275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
226180275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
226280275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
226380275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
226480275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
226580275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
226680275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
226780275a0aSLisandro Dalcin 
226880275a0aSLisandro Dalcin    Level: advanced
226980275a0aSLisandro Dalcin 
227080275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
227180275a0aSLisandro Dalcin @*/
227280275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
227380275a0aSLisandro Dalcin {
227480275a0aSLisandro Dalcin   PetscFunctionBegin;
227580275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227680275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
22773c633725SBarry Smith   PetscCheck(steps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
227880275a0aSLisandro Dalcin   ts->steps = steps;
2279d763cef2SBarry Smith   PetscFunctionReturn(0);
2280d763cef2SBarry Smith }
2281d763cef2SBarry Smith 
2282d763cef2SBarry Smith /*@
2283d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2284d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2285d763cef2SBarry Smith 
22863f9fe445SBarry Smith    Logically Collective on TS
2287d763cef2SBarry Smith 
2288d763cef2SBarry Smith    Input Parameters:
2289d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2290d763cef2SBarry Smith -  time_step - the size of the timestep
2291d763cef2SBarry Smith 
2292d763cef2SBarry Smith    Level: intermediate
2293d763cef2SBarry Smith 
2294aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2295d763cef2SBarry Smith 
2296d763cef2SBarry Smith @*/
22977087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2298d763cef2SBarry Smith {
2299d763cef2SBarry Smith   PetscFunctionBegin;
23000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2301c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2302d763cef2SBarry Smith   ts->time_step = time_step;
2303d763cef2SBarry Smith   PetscFunctionReturn(0);
2304d763cef2SBarry Smith }
2305d763cef2SBarry Smith 
2306a43b19c4SJed Brown /*@
230749354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
230849354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
230949354f04SShri Abhyankar      or just return at the final time TS computed.
2310a43b19c4SJed Brown 
2311a43b19c4SJed Brown   Logically Collective on TS
2312a43b19c4SJed Brown 
2313d8d19677SJose E. Roman    Input Parameters:
2314a43b19c4SJed Brown +   ts - the time-step context
231549354f04SShri Abhyankar -   eftopt - exact final time option
2316a43b19c4SJed Brown 
2317feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2318feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2319feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2320feed9e9dSBarry Smith 
2321feed9e9dSBarry Smith    Options Database:
2322feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2323feed9e9dSBarry Smith 
2324ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2325ee346746SBarry Smith     then the final time you selected.
2326ee346746SBarry Smith 
2327a43b19c4SJed Brown    Level: beginner
2328a43b19c4SJed Brown 
2329f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2330a43b19c4SJed Brown @*/
233149354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2332a43b19c4SJed Brown {
2333a43b19c4SJed Brown   PetscFunctionBegin;
2334a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
233549354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
233649354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2337a43b19c4SJed Brown   PetscFunctionReturn(0);
2338a43b19c4SJed Brown }
2339a43b19c4SJed Brown 
2340d763cef2SBarry Smith /*@
2341f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2342f6953c82SLisandro Dalcin 
2343f6953c82SLisandro Dalcin    Not Collective
2344f6953c82SLisandro Dalcin 
2345f6953c82SLisandro Dalcin    Input Parameter:
2346f6953c82SLisandro Dalcin .  ts - the TS context
2347f6953c82SLisandro Dalcin 
2348f6953c82SLisandro Dalcin    Output Parameter:
2349f6953c82SLisandro Dalcin .  eftopt - exact final time option
2350f6953c82SLisandro Dalcin 
2351f6953c82SLisandro Dalcin    Level: beginner
2352f6953c82SLisandro Dalcin 
2353f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2354f6953c82SLisandro Dalcin @*/
2355f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2356f6953c82SLisandro Dalcin {
2357f6953c82SLisandro Dalcin   PetscFunctionBegin;
2358f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2359f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2360f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2361f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2362f6953c82SLisandro Dalcin }
2363f6953c82SLisandro Dalcin 
2364f6953c82SLisandro Dalcin /*@
2365d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2366d763cef2SBarry Smith 
2367d763cef2SBarry Smith    Not Collective
2368d763cef2SBarry Smith 
2369d763cef2SBarry Smith    Input Parameter:
2370d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2371d763cef2SBarry Smith 
2372d763cef2SBarry Smith    Output Parameter:
2373d763cef2SBarry Smith .  dt - the current timestep size
2374d763cef2SBarry Smith 
2375d763cef2SBarry Smith    Level: intermediate
2376d763cef2SBarry Smith 
2377aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2378d763cef2SBarry Smith 
2379d763cef2SBarry Smith @*/
23807087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2381d763cef2SBarry Smith {
2382d763cef2SBarry Smith   PetscFunctionBegin;
23830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2384f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2385d763cef2SBarry Smith   *dt = ts->time_step;
2386d763cef2SBarry Smith   PetscFunctionReturn(0);
2387d763cef2SBarry Smith }
2388d763cef2SBarry Smith 
2389d8e5e3e6SSatish Balay /*@
2390d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2391d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2392d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2393d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2394d763cef2SBarry Smith 
2395d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2396d763cef2SBarry Smith 
2397d763cef2SBarry Smith    Input Parameter:
2398d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2399d763cef2SBarry Smith 
2400d763cef2SBarry Smith    Output Parameter:
2401d763cef2SBarry Smith .  v - the vector containing the solution
2402d763cef2SBarry Smith 
240363e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
240463e21af5SBarry Smith    final time. It returns the solution at the next timestep.
240563e21af5SBarry Smith 
2406d763cef2SBarry Smith    Level: intermediate
2407d763cef2SBarry Smith 
24080ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2409d763cef2SBarry Smith 
2410d763cef2SBarry Smith @*/
24117087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2412d763cef2SBarry Smith {
2413d763cef2SBarry Smith   PetscFunctionBegin;
24140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24154482741eSBarry Smith   PetscValidPointer(v,2);
24168737fe31SLisandro Dalcin   *v = ts->vec_sol;
2417d763cef2SBarry Smith   PetscFunctionReturn(0);
2418d763cef2SBarry Smith }
2419d763cef2SBarry Smith 
242003fe5f5eSDebojyoti Ghosh /*@
2421b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
242203fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2423b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
242403fe5f5eSDebojyoti Ghosh    structure as the solution vector.
242503fe5f5eSDebojyoti Ghosh 
242603fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
242703fe5f5eSDebojyoti Ghosh 
242803fe5f5eSDebojyoti Ghosh    Parameters :
2429a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2430b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2431b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
243203fe5f5eSDebojyoti Ghosh        returned in v.
2433a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
243403fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2435b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
243603fe5f5eSDebojyoti Ghosh 
24374cdd57e5SDebojyoti Ghosh    Level: advanced
243803fe5f5eSDebojyoti Ghosh 
243903fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
244003fe5f5eSDebojyoti Ghosh 
244103fe5f5eSDebojyoti Ghosh @*/
2442b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
244303fe5f5eSDebojyoti Ghosh {
244403fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
244503fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2446b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
244703fe5f5eSDebojyoti Ghosh   else {
24489566063dSJacob Faibussowitsch     PetscCall((*ts->ops->getsolutioncomponents)(ts,n,v));
244903fe5f5eSDebojyoti Ghosh   }
245003fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
245103fe5f5eSDebojyoti Ghosh }
245203fe5f5eSDebojyoti Ghosh 
24534cdd57e5SDebojyoti Ghosh /*@
24544cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24554cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24564cdd57e5SDebojyoti Ghosh 
24574cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24584cdd57e5SDebojyoti Ghosh 
24594cdd57e5SDebojyoti Ghosh    Parameters :
2460a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2461a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24624cdd57e5SDebojyoti Ghosh 
24634cdd57e5SDebojyoti Ghosh    Level: intermediate
24644cdd57e5SDebojyoti Ghosh 
24654cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24664cdd57e5SDebojyoti Ghosh 
24674cdd57e5SDebojyoti Ghosh @*/
24684cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24694cdd57e5SDebojyoti Ghosh {
24704cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24714cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2472f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24739566063dSJacob Faibussowitsch     PetscCall((*ts->ops->getauxsolution)(ts,v));
2474f6356ec7SDebojyoti Ghosh   } else {
24759566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(*v));
2476f6356ec7SDebojyoti Ghosh   }
24774cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24784cdd57e5SDebojyoti Ghosh }
24794cdd57e5SDebojyoti Ghosh 
24804cdd57e5SDebojyoti Ghosh /*@
24814cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24824cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24834cdd57e5SDebojyoti Ghosh 
24844cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24854cdd57e5SDebojyoti Ghosh 
24869657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24879657682dSDebojyoti Ghosh 
24884cdd57e5SDebojyoti Ghosh    Parameters :
2489a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2490657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2491a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
24924cdd57e5SDebojyoti Ghosh 
24934cdd57e5SDebojyoti Ghosh    Level: intermediate
24944cdd57e5SDebojyoti Ghosh 
249557df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24964cdd57e5SDebojyoti Ghosh 
24974cdd57e5SDebojyoti Ghosh @*/
24980a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24994cdd57e5SDebojyoti Ghosh {
25004cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25014cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2502f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25039566063dSJacob Faibussowitsch     PetscCall((*ts->ops->gettimeerror)(ts,n,v));
2504f6356ec7SDebojyoti Ghosh   } else {
25059566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(*v));
2506f6356ec7SDebojyoti Ghosh   }
25074cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25084cdd57e5SDebojyoti Ghosh }
25094cdd57e5SDebojyoti Ghosh 
251057df6a1bSDebojyoti Ghosh /*@
251157df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
251257df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
251357df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
251457df6a1bSDebojyoti Ghosh 
251557df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
251657df6a1bSDebojyoti Ghosh 
251757df6a1bSDebojyoti Ghosh    Parameters :
2518a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2519a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
252057df6a1bSDebojyoti Ghosh 
252157df6a1bSDebojyoti Ghosh    Level: intermediate
252257df6a1bSDebojyoti Ghosh 
252357df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
252457df6a1bSDebojyoti Ghosh 
252557df6a1bSDebojyoti Ghosh @*/
252657df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
252757df6a1bSDebojyoti Ghosh {
252857df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
252957df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25303c633725SBarry Smith   PetscCheck(ts->setupcalled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
253157df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
25329566063dSJacob Faibussowitsch     PetscCall((*ts->ops->settimeerror)(ts,v));
253357df6a1bSDebojyoti Ghosh   }
253457df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
253557df6a1bSDebojyoti Ghosh }
253657df6a1bSDebojyoti Ghosh 
2537bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2538d8e5e3e6SSatish Balay /*@
2539bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2540d763cef2SBarry Smith 
2541bdad233fSMatthew Knepley   Not collective
2542d763cef2SBarry Smith 
2543bdad233fSMatthew Knepley   Input Parameters:
2544bdad233fSMatthew Knepley + ts   - The TS
2545bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2546d763cef2SBarry Smith .vb
25470910c330SBarry Smith          U_t - A U = 0      (linear)
25480910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25490910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2550d763cef2SBarry Smith .ve
2551d763cef2SBarry Smith 
2552d763cef2SBarry Smith    Level: beginner
2553d763cef2SBarry Smith 
2554bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2555d763cef2SBarry Smith @*/
25567087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2557a7cc72afSBarry Smith {
2558d763cef2SBarry Smith   PetscFunctionBegin;
25590700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2560bdad233fSMatthew Knepley   ts->problem_type = type;
25619e2a6581SJed Brown   if (type == TS_LINEAR) {
25629e2a6581SJed Brown     SNES snes;
25639566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
25649566063dSJacob Faibussowitsch     PetscCall(SNESSetType(snes,SNESKSPONLY));
25659e2a6581SJed Brown   }
2566d763cef2SBarry Smith   PetscFunctionReturn(0);
2567d763cef2SBarry Smith }
2568d763cef2SBarry Smith 
2569bdad233fSMatthew Knepley /*@C
2570bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2571bdad233fSMatthew Knepley 
2572bdad233fSMatthew Knepley   Not collective
2573bdad233fSMatthew Knepley 
2574bdad233fSMatthew Knepley   Input Parameter:
2575bdad233fSMatthew Knepley . ts   - The TS
2576bdad233fSMatthew Knepley 
2577bdad233fSMatthew Knepley   Output Parameter:
2578bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2579bdad233fSMatthew Knepley .vb
2580089b2837SJed Brown          M U_t = A U
2581089b2837SJed Brown          M(t) U_t = A(t) U
2582b5abc632SBarry Smith          F(t,U,U_t)
2583bdad233fSMatthew Knepley .ve
2584bdad233fSMatthew Knepley 
2585bdad233fSMatthew Knepley    Level: beginner
2586bdad233fSMatthew Knepley 
2587bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2588bdad233fSMatthew Knepley @*/
25897087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2590a7cc72afSBarry Smith {
2591bdad233fSMatthew Knepley   PetscFunctionBegin;
25920700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25934482741eSBarry Smith   PetscValidIntPointer(type,2);
2594bdad233fSMatthew Knepley   *type = ts->problem_type;
2595bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2596bdad233fSMatthew Knepley }
2597d763cef2SBarry Smith 
2598303a5415SBarry Smith /*
2599303a5415SBarry 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()
2600303a5415SBarry Smith */
2601303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2602303a5415SBarry Smith {
2603303a5415SBarry Smith   PetscBool      isnone;
2604303a5415SBarry Smith 
2605303a5415SBarry Smith   PetscFunctionBegin;
26069566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts,&ts->adapt));
26079566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type));
2608303a5415SBarry Smith 
26099566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone));
2610303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2611303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2612303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2613303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2614303a5415SBarry Smith   }
2615303a5415SBarry Smith   PetscFunctionReturn(0);
2616303a5415SBarry Smith }
2617303a5415SBarry Smith 
2618d763cef2SBarry Smith /*@
2619303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2620d763cef2SBarry Smith 
2621d763cef2SBarry Smith    Collective on TS
2622d763cef2SBarry Smith 
2623d763cef2SBarry Smith    Input Parameter:
2624d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2625d763cef2SBarry Smith 
2626d763cef2SBarry Smith    Notes:
2627d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2628d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2629141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2630d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2631141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2632d763cef2SBarry Smith 
2633d763cef2SBarry Smith    Level: advanced
2634d763cef2SBarry Smith 
2635141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2636d763cef2SBarry Smith @*/
26377087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2638d763cef2SBarry Smith {
26396c6b9e74SPeter Brune   DM             dm;
26406c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2641d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2642cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26436c6b9e74SPeter Brune   TSIJacobian    ijac;
2644efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26456c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2646d763cef2SBarry Smith 
2647d763cef2SBarry Smith   PetscFunctionBegin;
26480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2649277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2650277b19d0SLisandro Dalcin 
26517adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
26529566063dSJacob Faibussowitsch     PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL));
26539566063dSJacob Faibussowitsch     PetscCall(TSSetType(ts,ifun ? TSBEULER : TSEULER));
2654d763cef2SBarry Smith   }
2655277b19d0SLisandro Dalcin 
26561a638600SStefano Zampini   if (!ts->vec_sol) {
26571a638600SStefano Zampini     if (ts->dm) {
26589566063dSJacob Faibussowitsch       PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol));
26591a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
26601a638600SStefano Zampini   }
2661277b19d0SLisandro Dalcin 
26624a658b32SHong Zhang   if (ts->tspan) {
26634a658b32SHong Zhang     if (!ts->tspan->vecs_sol) {
26644a658b32SHong Zhang       PetscCall(VecDuplicateVecs(ts->vec_sol,ts->tspan->num_span_times,&ts->tspan->vecs_sol));
26654a658b32SHong Zhang     }
26664a658b32SHong Zhang   }
2667298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
26689566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs));
2669298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2670298bade4SHong Zhang   }
2671298bade4SHong Zhang 
2672cd4cee2dSHong Zhang   if (ts->quadraturets) {
26739566063dSJacob Faibussowitsch     PetscCall(TSSetUp(ts->quadraturets));
26749566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vec_costintegrand));
26759566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand));
2676cd4cee2dSHong Zhang   }
2677cd4cee2dSHong Zhang 
26789566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL));
2679f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2680e1244c69SJed Brown     Mat Amat,Pmat;
2681e1244c69SJed Brown     SNES snes;
26829566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
26839566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL));
2684e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2685e1244c69SJed Brown      * have displaced the RHS matrix */
2686971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2687abc0d4abSBarry 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 */
26889566063dSJacob Faibussowitsch       PetscCall(MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat));
26899566063dSJacob Faibussowitsch       PetscCall(SNESSetJacobian(snes,Amat,NULL,NULL,NULL));
26909566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Amat));
2691e1244c69SJed Brown     }
2692971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
26939566063dSJacob Faibussowitsch       PetscCall(MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat));
26949566063dSJacob Faibussowitsch       PetscCall(SNESSetJacobian(snes,NULL,Pmat,NULL,NULL));
26959566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Pmat));
2696e1244c69SJed Brown     }
2697e1244c69SJed Brown   }
26982ffb9264SLisandro Dalcin 
26999566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts,&ts->adapt));
27009566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type));
27012ffb9264SLisandro Dalcin 
2702277b19d0SLisandro Dalcin   if (ts->ops->setup) {
27039566063dSJacob Faibussowitsch     PetscCall((*ts->ops->setup)(ts));
2704277b19d0SLisandro Dalcin   }
2705277b19d0SLisandro Dalcin 
27069566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTimeDefault(ts));
27072ffb9264SLisandro Dalcin 
2708a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27096c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27106c6b9e74SPeter Brune    */
27119566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
27129566063dSJacob Faibussowitsch   PetscCall(DMSNESGetFunction(dm,&func,NULL));
27136c6b9e74SPeter Brune   if (!func) {
27149566063dSJacob Faibussowitsch     PetscCall(DMSNESSetFunction(dm,SNESTSFormFunction,ts));
27156c6b9e74SPeter Brune   }
2716a6772fa2SLisandro 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.
27176c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27186c6b9e74SPeter Brune    */
27199566063dSJacob Faibussowitsch   PetscCall(DMSNESGetJacobian(dm,&jac,NULL));
27209566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,&ijac,NULL));
27219566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm,&i2jac,NULL));
27229566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjac,NULL));
2723efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27249566063dSJacob Faibussowitsch     PetscCall(DMSNESSetJacobian(dm,SNESTSFormJacobian,ts));
27256c6b9e74SPeter Brune   }
2726c0517034SDebojyoti Ghosh 
2727c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2728c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
27299566063dSJacob Faibussowitsch     PetscCall((*ts->ops->startingmethod)(ts));
2730c0517034SDebojyoti Ghosh   }
2731c0517034SDebojyoti Ghosh 
2732277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2733277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2734277b19d0SLisandro Dalcin }
2735277b19d0SLisandro Dalcin 
2736f6a906c0SBarry Smith /*@
2737277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2738277b19d0SLisandro Dalcin 
2739277b19d0SLisandro Dalcin    Collective on TS
2740277b19d0SLisandro Dalcin 
2741277b19d0SLisandro Dalcin    Input Parameter:
2742277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2743277b19d0SLisandro Dalcin 
2744277b19d0SLisandro Dalcin    Level: beginner
2745277b19d0SLisandro Dalcin 
2746277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2747277b19d0SLisandro Dalcin @*/
2748277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2749277b19d0SLisandro Dalcin {
27501d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2751277b19d0SLisandro Dalcin 
2752277b19d0SLisandro Dalcin   PetscFunctionBegin;
2753277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2754b18ea86cSHong Zhang 
2755277b19d0SLisandro Dalcin   if (ts->ops->reset) {
27569566063dSJacob Faibussowitsch     PetscCall((*ts->ops->reset)(ts));
2757277b19d0SLisandro Dalcin   }
27589566063dSJacob Faibussowitsch   if (ts->snes) PetscCall(SNESReset(ts->snes));
27599566063dSJacob Faibussowitsch   if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt));
2760bbd56ea5SKarl Rupp 
27619566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Arhs));
27629566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Brhs));
27639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->Frhs));
27649566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_sol));
27659566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_dot));
27669566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vatol));
27679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vrtol));
27689566063dSJacob Faibussowitsch   PetscCall(VecDestroyVecs(ts->nwork,&ts->work));
2769bbd56ea5SKarl Rupp 
27709566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Jacprhs));
27719566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Jacp));
2772ecf68647SHong Zhang   if (ts->forward_solve) {
27739566063dSJacob Faibussowitsch     PetscCall(TSForwardReset(ts));
2774ecf68647SHong Zhang   }
2775cd4cee2dSHong Zhang   if (ts->quadraturets) {
27769566063dSJacob Faibussowitsch     PetscCall(TSReset(ts->quadraturets));
27779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vec_costintegrand));
2778cd4cee2dSHong Zhang   }
27791d06f6b3SHong Zhang   while (ilink) {
27801d06f6b3SHong Zhang     next = ilink->next;
27819566063dSJacob Faibussowitsch     PetscCall(TSDestroy(&ilink->ts));
27829566063dSJacob Faibussowitsch     PetscCall(PetscFree(ilink->splitname));
27839566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&ilink->is));
27849566063dSJacob Faibussowitsch     PetscCall(PetscFree(ilink));
27851d06f6b3SHong Zhang     ilink = next;
278687f4e208SHong Zhang   }
27876bf673ebSJoe Pusztay   ts->tsrhssplit = NULL;
2788545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
27894a658b32SHong Zhang   if (ts->tspan) {
27904a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan->span_times));
27914a658b32SHong Zhang     PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol));
27924a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan));
27934a658b32SHong Zhang   }
2794277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2795d763cef2SBarry Smith   PetscFunctionReturn(0);
2796d763cef2SBarry Smith }
2797d763cef2SBarry Smith 
27981fb7b255SJunchao Zhang /*@C
2799d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2800d763cef2SBarry Smith    with TSCreate().
2801d763cef2SBarry Smith 
2802d763cef2SBarry Smith    Collective on TS
2803d763cef2SBarry Smith 
2804d763cef2SBarry Smith    Input Parameter:
2805d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2806d763cef2SBarry Smith 
2807d763cef2SBarry Smith    Level: beginner
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2810d763cef2SBarry Smith @*/
28116bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2812d763cef2SBarry Smith {
2813d763cef2SBarry Smith   PetscFunctionBegin;
28146bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2815ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2816c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2817d763cef2SBarry Smith 
28189566063dSJacob Faibussowitsch   PetscCall(TSReset(*ts));
28199566063dSJacob Faibussowitsch   PetscCall(TSAdjointReset(*ts));
2820ecf68647SHong Zhang   if ((*ts)->forward_solve) {
28219566063dSJacob Faibussowitsch     PetscCall(TSForwardReset(*ts));
2822ecf68647SHong Zhang   }
2823e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
28249566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts));
28259566063dSJacob Faibussowitsch   if ((*ts)->ops->destroy) PetscCall((*(*ts)->ops->destroy)((*ts)));
28266d4c513bSLisandro Dalcin 
28279566063dSJacob Faibussowitsch   PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory));
2828bc952696SBarry Smith 
28299566063dSJacob Faibussowitsch   PetscCall(TSAdaptDestroy(&(*ts)->adapt));
28309566063dSJacob Faibussowitsch   PetscCall(TSEventDestroy(&(*ts)->event));
28316427ac75SLisandro Dalcin 
28329566063dSJacob Faibussowitsch   PetscCall(SNESDestroy(&(*ts)->snes));
28339566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*ts)->dm));
28349566063dSJacob Faibussowitsch   PetscCall(TSMonitorCancel((*ts)));
28359566063dSJacob Faibussowitsch   PetscCall(TSAdjointMonitorCancel((*ts)));
28366d4c513bSLisandro Dalcin 
28379566063dSJacob Faibussowitsch   PetscCall(TSDestroy(&(*ts)->quadraturets));
28389566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(ts));
2839d763cef2SBarry Smith   PetscFunctionReturn(0);
2840d763cef2SBarry Smith }
2841d763cef2SBarry Smith 
2842d8e5e3e6SSatish Balay /*@
2843d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2844d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2845d763cef2SBarry Smith 
2846d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2847d763cef2SBarry Smith 
2848d763cef2SBarry Smith    Input Parameter:
2849d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith    Output Parameter:
2852d763cef2SBarry Smith .  snes - the nonlinear solver context
2853d763cef2SBarry Smith 
2854d763cef2SBarry Smith    Notes:
2855d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2856d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
285794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2858d763cef2SBarry Smith 
2859d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28600298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2861d763cef2SBarry Smith 
2862d763cef2SBarry Smith    Level: beginner
2863d763cef2SBarry Smith 
2864d763cef2SBarry Smith @*/
28657087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2866d763cef2SBarry Smith {
2867d763cef2SBarry Smith   PetscFunctionBegin;
28680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28694482741eSBarry Smith   PetscValidPointer(snes,2);
2870d372ba47SLisandro Dalcin   if (!ts->snes) {
28719566063dSJacob Faibussowitsch     PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes));
28729566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options));
28739566063dSJacob Faibussowitsch     PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts));
28749566063dSJacob Faibussowitsch     PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes));
28759566063dSJacob Faibussowitsch     PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1));
28769566063dSJacob Faibussowitsch     if (ts->dm) PetscCall(SNESSetDM(ts->snes,ts->dm));
28779e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28789566063dSJacob Faibussowitsch       PetscCall(SNESSetType(ts->snes,SNESKSPONLY));
28799e2a6581SJed Brown     }
2880d372ba47SLisandro Dalcin   }
2881d763cef2SBarry Smith   *snes = ts->snes;
2882d763cef2SBarry Smith   PetscFunctionReturn(0);
2883d763cef2SBarry Smith }
2884d763cef2SBarry Smith 
2885deb2cd25SJed Brown /*@
2886deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2887deb2cd25SJed Brown 
2888deb2cd25SJed Brown    Collective
2889deb2cd25SJed Brown 
2890d8d19677SJose E. Roman    Input Parameters:
2891deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2892deb2cd25SJed Brown -  snes - the nonlinear solver context
2893deb2cd25SJed Brown 
2894deb2cd25SJed Brown    Notes:
2895deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2896deb2cd25SJed Brown 
2897deb2cd25SJed Brown    Level: developer
2898deb2cd25SJed Brown 
2899deb2cd25SJed Brown @*/
2900deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2901deb2cd25SJed Brown {
2902d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2903deb2cd25SJed Brown 
2904deb2cd25SJed Brown   PetscFunctionBegin;
2905deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2906deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
29079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)snes));
29089566063dSJacob Faibussowitsch   PetscCall(SNESDestroy(&ts->snes));
2909bbd56ea5SKarl Rupp 
2910deb2cd25SJed Brown   ts->snes = snes;
2911bbd56ea5SKarl Rupp 
29129566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts));
29139566063dSJacob Faibussowitsch   PetscCall(SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL));
2914740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29159566063dSJacob Faibussowitsch     PetscCall(SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts));
2916740132f1SEmil Constantinescu   }
2917deb2cd25SJed Brown   PetscFunctionReturn(0);
2918deb2cd25SJed Brown }
2919deb2cd25SJed Brown 
2920d8e5e3e6SSatish Balay /*@
292194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2922d763cef2SBarry Smith    a TS (timestepper) context.
2923d763cef2SBarry Smith 
292494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2925d763cef2SBarry Smith 
2926d763cef2SBarry Smith    Input Parameter:
2927d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2928d763cef2SBarry Smith 
2929d763cef2SBarry Smith    Output Parameter:
293094b7f48cSBarry Smith .  ksp - the nonlinear solver context
2931d763cef2SBarry Smith 
2932d763cef2SBarry Smith    Notes:
293394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2934d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2935d763cef2SBarry Smith    KSP and PC contexts as well.
2936d763cef2SBarry Smith 
293794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29380298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2939d763cef2SBarry Smith 
2940d763cef2SBarry Smith    Level: beginner
2941d763cef2SBarry Smith 
2942d763cef2SBarry Smith @*/
29437087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2944d763cef2SBarry Smith {
2945089b2837SJed Brown   SNES           snes;
2946d372ba47SLisandro Dalcin 
2947d763cef2SBarry Smith   PetscFunctionBegin;
29480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29494482741eSBarry Smith   PetscValidPointer(ksp,2);
29503c633725SBarry Smith   PetscCheck(((PetscObject)ts)->type_name,PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
29513c633725SBarry Smith   PetscCheck(ts->problem_type == TS_LINEAR,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
29529566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
29539566063dSJacob Faibussowitsch   PetscCall(SNESGetKSP(snes,ksp));
2954d763cef2SBarry Smith   PetscFunctionReturn(0);
2955d763cef2SBarry Smith }
2956d763cef2SBarry Smith 
2957d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2958d763cef2SBarry Smith 
2959adb62b0dSMatthew Knepley /*@
2960618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2961618ce8baSLisandro Dalcin 
2962618ce8baSLisandro Dalcin    Logically Collective on TS
2963618ce8baSLisandro Dalcin 
2964618ce8baSLisandro Dalcin    Input Parameters:
2965618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2966618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2967618ce8baSLisandro Dalcin 
2968618ce8baSLisandro Dalcin    Options Database Keys:
2969618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2970618ce8baSLisandro Dalcin 
2971618ce8baSLisandro Dalcin    Notes:
2972618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2973618ce8baSLisandro Dalcin 
2974618ce8baSLisandro Dalcin    Level: intermediate
2975618ce8baSLisandro Dalcin 
2976618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2977618ce8baSLisandro Dalcin @*/
2978618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2979618ce8baSLisandro Dalcin {
2980618ce8baSLisandro Dalcin   PetscFunctionBegin;
2981618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2982618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
29833c633725SBarry Smith   PetscCheck(maxsteps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2984618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2985618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2986618ce8baSLisandro Dalcin }
2987618ce8baSLisandro Dalcin 
2988618ce8baSLisandro Dalcin /*@
2989618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2990618ce8baSLisandro Dalcin 
2991618ce8baSLisandro Dalcin    Not Collective
2992618ce8baSLisandro Dalcin 
2993618ce8baSLisandro Dalcin    Input Parameters:
2994618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2995618ce8baSLisandro Dalcin 
2996618ce8baSLisandro Dalcin    Output Parameter:
2997618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2998618ce8baSLisandro Dalcin 
2999618ce8baSLisandro Dalcin    Level: advanced
3000618ce8baSLisandro Dalcin 
3001618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3002618ce8baSLisandro Dalcin @*/
3003618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3004618ce8baSLisandro Dalcin {
3005618ce8baSLisandro Dalcin   PetscFunctionBegin;
3006618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3007618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3008618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3009618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3010618ce8baSLisandro Dalcin }
3011618ce8baSLisandro Dalcin 
3012618ce8baSLisandro Dalcin /*@
3013618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3014618ce8baSLisandro Dalcin 
3015618ce8baSLisandro Dalcin    Logically Collective on TS
3016618ce8baSLisandro Dalcin 
3017618ce8baSLisandro Dalcin    Input Parameters:
3018618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3019618ce8baSLisandro Dalcin -  maxtime - final time to step to
3020618ce8baSLisandro Dalcin 
3021618ce8baSLisandro Dalcin    Options Database Keys:
3022ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3023618ce8baSLisandro Dalcin 
3024618ce8baSLisandro Dalcin    Notes:
3025618ce8baSLisandro Dalcin    The default maximum time is 5.0
3026618ce8baSLisandro Dalcin 
3027618ce8baSLisandro Dalcin    Level: intermediate
3028618ce8baSLisandro Dalcin 
3029618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3030618ce8baSLisandro Dalcin @*/
3031618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3032618ce8baSLisandro Dalcin {
3033618ce8baSLisandro Dalcin   PetscFunctionBegin;
3034618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3035618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3036618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3037618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3038618ce8baSLisandro Dalcin }
3039618ce8baSLisandro Dalcin 
3040618ce8baSLisandro Dalcin /*@
3041618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3042618ce8baSLisandro Dalcin 
3043618ce8baSLisandro Dalcin    Not Collective
3044618ce8baSLisandro Dalcin 
3045618ce8baSLisandro Dalcin    Input Parameters:
3046618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3047618ce8baSLisandro Dalcin 
3048618ce8baSLisandro Dalcin    Output Parameter:
3049618ce8baSLisandro Dalcin .  maxtime - final time to step to
3050618ce8baSLisandro Dalcin 
3051618ce8baSLisandro Dalcin    Level: advanced
3052618ce8baSLisandro Dalcin 
3053618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3054618ce8baSLisandro Dalcin @*/
3055618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3056618ce8baSLisandro Dalcin {
3057618ce8baSLisandro Dalcin   PetscFunctionBegin;
3058618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3059618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3060618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3061618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3062618ce8baSLisandro Dalcin }
3063618ce8baSLisandro Dalcin 
3064618ce8baSLisandro Dalcin /*@
3065aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3066edc382c3SSatish Balay 
3067edc382c3SSatish Balay    Level: deprecated
3068edc382c3SSatish Balay 
3069aaa6c58dSLisandro Dalcin @*/
3070aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3071aaa6c58dSLisandro Dalcin {
3072aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3073aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30749566063dSJacob Faibussowitsch   PetscCall(TSSetTime(ts,initial_time));
30759566063dSJacob Faibussowitsch   PetscCall(TSSetTimeStep(ts,time_step));
3076aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3077aaa6c58dSLisandro Dalcin }
3078aaa6c58dSLisandro Dalcin 
3079aaa6c58dSLisandro Dalcin /*@
308019eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3081edc382c3SSatish Balay 
3082edc382c3SSatish Balay    Level: deprecated
3083edc382c3SSatish Balay 
3084adb62b0dSMatthew Knepley @*/
30857087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3086adb62b0dSMatthew Knepley {
3087adb62b0dSMatthew Knepley   PetscFunctionBegin;
30880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3089abc0a331SBarry Smith   if (maxsteps) {
30904482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3091adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3092adb62b0dSMatthew Knepley   }
3093abc0a331SBarry Smith   if (maxtime) {
3094064a246eSJacob Faibussowitsch     PetscValidRealPointer(maxtime,3);
3095adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3096adb62b0dSMatthew Knepley   }
3097adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3098adb62b0dSMatthew Knepley }
3099adb62b0dSMatthew Knepley 
3100d763cef2SBarry Smith /*@
310119eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3102edc382c3SSatish Balay 
3103edc382c3SSatish Balay    Level: deprecated
3104edc382c3SSatish Balay 
3105d763cef2SBarry Smith @*/
31067087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3107d763cef2SBarry Smith {
3108d763cef2SBarry Smith   PetscFunctionBegin;
31090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3110c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3111064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveReal(ts,maxtime,3);
311239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
311339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3114d763cef2SBarry Smith   PetscFunctionReturn(0);
3115d763cef2SBarry Smith }
3116d763cef2SBarry Smith 
3117d763cef2SBarry Smith /*@
31185c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3119edc382c3SSatish Balay 
3120edc382c3SSatish Balay    Level: deprecated
3121edc382c3SSatish Balay 
31225c5f5948SLisandro Dalcin @*/
3123e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31245c5f5948SLisandro Dalcin 
312519eac22cSLisandro Dalcin /*@
31264f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3127edc382c3SSatish Balay 
3128edc382c3SSatish Balay    Level: deprecated
3129edc382c3SSatish Balay 
31304f4e0956SLisandro Dalcin @*/
31314f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31324f4e0956SLisandro Dalcin 
31334f4e0956SLisandro Dalcin /*@
3134d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3135d763cef2SBarry Smith    for use by the TS routines.
3136d763cef2SBarry Smith 
3137d083f849SBarry Smith    Logically Collective on TS
3138d763cef2SBarry Smith 
3139d763cef2SBarry Smith    Input Parameters:
3140d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31410910c330SBarry Smith -  u - the solution vector
3142d763cef2SBarry Smith 
3143d763cef2SBarry Smith    Level: beginner
3144d763cef2SBarry Smith 
31450ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3146d763cef2SBarry Smith @*/
31470910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3148d763cef2SBarry Smith {
3149496e6a7aSJed Brown   DM             dm;
31508737fe31SLisandro Dalcin 
3151d763cef2SBarry Smith   PetscFunctionBegin;
31520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31530910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31549566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)u));
31559566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_sol));
31560910c330SBarry Smith   ts->vec_sol = u;
3157bbd56ea5SKarl Rupp 
31589566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
31599566063dSJacob Faibussowitsch   PetscCall(DMShellSetGlobalVector(dm,u));
3160d763cef2SBarry Smith   PetscFunctionReturn(0);
3161d763cef2SBarry Smith }
3162d763cef2SBarry Smith 
3163ac226902SBarry Smith /*@C
3164000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31653f2090d5SJed Brown   called once at the beginning of each time step.
3166000e7ae3SMatthew Knepley 
31673f9fe445SBarry Smith   Logically Collective on TS
3168000e7ae3SMatthew Knepley 
3169000e7ae3SMatthew Knepley   Input Parameters:
3170000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3171000e7ae3SMatthew Knepley - func - The function
3172000e7ae3SMatthew Knepley 
3173000e7ae3SMatthew Knepley   Calling sequence of func:
317467b8a455SSatish Balay .vb
317567b8a455SSatish Balay   PetscErrorCode func (TS ts);
317667b8a455SSatish Balay .ve
3177000e7ae3SMatthew Knepley 
3178000e7ae3SMatthew Knepley   Level: intermediate
3179000e7ae3SMatthew Knepley 
3180dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3181000e7ae3SMatthew Knepley @*/
31827087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3183000e7ae3SMatthew Knepley {
3184000e7ae3SMatthew Knepley   PetscFunctionBegin;
31850700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3186ae60f76fSBarry Smith   ts->prestep = func;
3187000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3188000e7ae3SMatthew Knepley }
3189000e7ae3SMatthew Knepley 
319009ee8438SJed Brown /*@
31913f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31923f2090d5SJed Brown 
31933f2090d5SJed Brown   Collective on TS
31943f2090d5SJed Brown 
31953f2090d5SJed Brown   Input Parameters:
31963f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
31973f2090d5SJed Brown 
31983f2090d5SJed Brown   Notes:
31993f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32003f2090d5SJed Brown   so most users would not generally call this routine themselves.
32013f2090d5SJed Brown 
32023f2090d5SJed Brown   Level: developer
32033f2090d5SJed Brown 
32049be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32053f2090d5SJed Brown @*/
32067087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32073f2090d5SJed Brown {
32083f2090d5SJed Brown   PetscFunctionBegin;
32090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3210ae60f76fSBarry Smith   if (ts->prestep) {
32115efd42a4SStefano Zampini     Vec              U;
3212ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3213ef8d1ce0SJohann Rudi     PetscBool        sameObject;
32145efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32155efd42a4SStefano Zampini 
32169566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
32179566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetId((PetscObject)U,&idprev));
32189566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&sprev));
3219a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->prestep),ts);
32209566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
32219566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject));
32229566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&spost));
32239566063dSJacob Faibussowitsch     if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts));
3224312ce896SJed Brown   }
32253f2090d5SJed Brown   PetscFunctionReturn(0);
32263f2090d5SJed Brown }
32273f2090d5SJed Brown 
3228b8123daeSJed Brown /*@C
3229b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3230b8123daeSJed Brown   called once at the beginning of each stage.
3231b8123daeSJed Brown 
3232b8123daeSJed Brown   Logically Collective on TS
3233b8123daeSJed Brown 
3234b8123daeSJed Brown   Input Parameters:
3235b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3236b8123daeSJed Brown - func - The function
3237b8123daeSJed Brown 
3238b8123daeSJed Brown   Calling sequence of func:
323967b8a455SSatish Balay .vb
324067b8a455SSatish Balay   PetscErrorCode func(TS ts, PetscReal stagetime);
324167b8a455SSatish Balay .ve
3242b8123daeSJed Brown 
3243b8123daeSJed Brown   Level: intermediate
3244b8123daeSJed Brown 
3245b8123daeSJed Brown   Note:
3246b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
324780275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3248b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3249b8123daeSJed Brown 
32509be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3251b8123daeSJed Brown @*/
3252b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3253b8123daeSJed Brown {
3254b8123daeSJed Brown   PetscFunctionBegin;
3255b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3256ae60f76fSBarry Smith   ts->prestage = func;
3257b8123daeSJed Brown   PetscFunctionReturn(0);
3258b8123daeSJed Brown }
3259b8123daeSJed Brown 
32609be3e283SDebojyoti Ghosh /*@C
32619be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32629be3e283SDebojyoti Ghosh   called once at the end of each stage.
32639be3e283SDebojyoti Ghosh 
32649be3e283SDebojyoti Ghosh   Logically Collective on TS
32659be3e283SDebojyoti Ghosh 
32669be3e283SDebojyoti Ghosh   Input Parameters:
32679be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32689be3e283SDebojyoti Ghosh - func - The function
32699be3e283SDebojyoti Ghosh 
32709be3e283SDebojyoti Ghosh   Calling sequence of func:
327167b8a455SSatish Balay .vb
327267b8a455SSatish Balay   PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
327367b8a455SSatish Balay .ve
32749be3e283SDebojyoti Ghosh 
32759be3e283SDebojyoti Ghosh   Level: intermediate
32769be3e283SDebojyoti Ghosh 
32779be3e283SDebojyoti Ghosh   Note:
32789be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
327980275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32809be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32819be3e283SDebojyoti Ghosh 
32829be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32839be3e283SDebojyoti Ghosh @*/
32849be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32859be3e283SDebojyoti Ghosh {
32869be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32879be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32889be3e283SDebojyoti Ghosh   ts->poststage = func;
32899be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32909be3e283SDebojyoti Ghosh }
32919be3e283SDebojyoti Ghosh 
3292c688d042SShri Abhyankar /*@C
3293c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3294c688d042SShri Abhyankar   called once at the end of each step evaluation.
3295c688d042SShri Abhyankar 
3296c688d042SShri Abhyankar   Logically Collective on TS
3297c688d042SShri Abhyankar 
3298c688d042SShri Abhyankar   Input Parameters:
3299c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3300c688d042SShri Abhyankar - func - The function
3301c688d042SShri Abhyankar 
3302c688d042SShri Abhyankar   Calling sequence of func:
330367b8a455SSatish Balay .vb
330467b8a455SSatish Balay   PetscErrorCode func(TS ts);
330567b8a455SSatish Balay .ve
3306c688d042SShri Abhyankar 
3307c688d042SShri Abhyankar   Level: intermediate
3308c688d042SShri Abhyankar 
3309c688d042SShri Abhyankar   Note:
33101785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33111785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3312e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3313e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3314e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3315c688d042SShri Abhyankar 
3316c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3317c688d042SShri Abhyankar @*/
3318c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3319c688d042SShri Abhyankar {
3320c688d042SShri Abhyankar   PetscFunctionBegin;
3321c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3322c688d042SShri Abhyankar   ts->postevaluate = func;
3323c688d042SShri Abhyankar   PetscFunctionReturn(0);
3324c688d042SShri Abhyankar }
3325c688d042SShri Abhyankar 
3326b8123daeSJed Brown /*@
3327b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3328b8123daeSJed Brown 
3329b8123daeSJed Brown   Collective on TS
3330b8123daeSJed Brown 
3331b8123daeSJed Brown   Input Parameters:
3332b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33339be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3334b8123daeSJed Brown 
3335b8123daeSJed Brown   Notes:
3336b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3337b8123daeSJed Brown   most users would not generally call this routine themselves.
3338b8123daeSJed Brown 
3339b8123daeSJed Brown   Level: developer
3340b8123daeSJed Brown 
33419be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3342b8123daeSJed Brown @*/
3343b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3344b8123daeSJed Brown {
3345b8123daeSJed Brown   PetscFunctionBegin;
3346b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3347ae60f76fSBarry Smith   if (ts->prestage) {
3348a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->prestage),ts,stagetime);
3349b8123daeSJed Brown   }
3350b8123daeSJed Brown   PetscFunctionReturn(0);
3351b8123daeSJed Brown }
3352b8123daeSJed Brown 
33539be3e283SDebojyoti Ghosh /*@
33549be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33559be3e283SDebojyoti Ghosh 
33569be3e283SDebojyoti Ghosh   Collective on TS
33579be3e283SDebojyoti Ghosh 
33589be3e283SDebojyoti Ghosh   Input Parameters:
33599be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33609be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33619be3e283SDebojyoti Ghosh   stageindex  - Stage number
33629be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33639be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33649be3e283SDebojyoti Ghosh 
33659be3e283SDebojyoti Ghosh   Notes:
33669be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33679be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33689be3e283SDebojyoti Ghosh 
33699be3e283SDebojyoti Ghosh   Level: developer
33709be3e283SDebojyoti Ghosh 
33719be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33729be3e283SDebojyoti Ghosh @*/
33739be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33749be3e283SDebojyoti Ghosh {
33759be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33769be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33774beae5d8SLisandro Dalcin   if (ts->poststage) {
3378a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->poststage),ts,stagetime,stageindex,Y);
33799be3e283SDebojyoti Ghosh   }
33809be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33819be3e283SDebojyoti Ghosh }
33829be3e283SDebojyoti Ghosh 
3383c688d042SShri Abhyankar /*@
3384c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3385c688d042SShri Abhyankar 
3386c688d042SShri Abhyankar   Collective on TS
3387c688d042SShri Abhyankar 
3388c688d042SShri Abhyankar   Input Parameters:
3389c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3390c688d042SShri Abhyankar 
3391c688d042SShri Abhyankar   Notes:
3392c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3393c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3394c688d042SShri Abhyankar 
3395c688d042SShri Abhyankar   Level: developer
3396c688d042SShri Abhyankar 
3397c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3398c688d042SShri Abhyankar @*/
3399c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3400c688d042SShri Abhyankar {
3401c688d042SShri Abhyankar   PetscFunctionBegin;
3402c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3403c688d042SShri Abhyankar   if (ts->postevaluate) {
3404dcb233daSLisandro Dalcin     Vec              U;
3405dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3406dcb233daSLisandro Dalcin 
34079566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
34089566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&sprev));
3409a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->postevaluate),ts);
34109566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&spost));
34119566063dSJacob Faibussowitsch     if (sprev != spost) PetscCall(TSRestartStep(ts));
3412c688d042SShri Abhyankar   }
3413c688d042SShri Abhyankar   PetscFunctionReturn(0);
3414c688d042SShri Abhyankar }
3415c688d042SShri Abhyankar 
3416ac226902SBarry Smith /*@C
3417000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34183f2090d5SJed Brown   called once at the end of each time step.
3419000e7ae3SMatthew Knepley 
34203f9fe445SBarry Smith   Logically Collective on TS
3421000e7ae3SMatthew Knepley 
3422000e7ae3SMatthew Knepley   Input Parameters:
3423000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3424000e7ae3SMatthew Knepley - func - The function
3425000e7ae3SMatthew Knepley 
3426000e7ae3SMatthew Knepley   Calling sequence of func:
3427b8123daeSJed Brown $ func (TS ts);
3428000e7ae3SMatthew Knepley 
34291785ff2aSShri Abhyankar   Notes:
34301785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34311785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34321785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34331785ff2aSShri Abhyankar 
3434000e7ae3SMatthew Knepley   Level: intermediate
3435000e7ae3SMatthew Knepley 
3436dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3437000e7ae3SMatthew Knepley @*/
34387087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3439000e7ae3SMatthew Knepley {
3440000e7ae3SMatthew Knepley   PetscFunctionBegin;
34410700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3442ae60f76fSBarry Smith   ts->poststep = func;
3443000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3444000e7ae3SMatthew Knepley }
3445000e7ae3SMatthew Knepley 
344609ee8438SJed Brown /*@
34473f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34483f2090d5SJed Brown 
34493f2090d5SJed Brown   Collective on TS
34503f2090d5SJed Brown 
34513f2090d5SJed Brown   Input Parameters:
34523f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34533f2090d5SJed Brown 
34543f2090d5SJed Brown   Notes:
34553f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34563f2090d5SJed Brown   so most users would not generally call this routine themselves.
34573f2090d5SJed Brown 
34583f2090d5SJed Brown   Level: developer
34593f2090d5SJed Brown 
34603f2090d5SJed Brown @*/
34617087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34623f2090d5SJed Brown {
34633f2090d5SJed Brown   PetscFunctionBegin;
34640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3465ae60f76fSBarry Smith   if (ts->poststep) {
34665efd42a4SStefano Zampini     Vec              U;
3467ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3468ef8d1ce0SJohann Rudi     PetscBool        sameObject;
34695efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34705efd42a4SStefano Zampini 
34719566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
34729566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetId((PetscObject)U,&idprev));
34739566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&sprev));
3474a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->poststep),ts);
34759566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
34769566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject));
34779566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&spost));
34789566063dSJacob Faibussowitsch     if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts));
347972ac3e02SJed Brown   }
34803f2090d5SJed Brown   PetscFunctionReturn(0);
34813f2090d5SJed Brown }
34823f2090d5SJed Brown 
3483cd652676SJed Brown /*@
3484cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3485cd652676SJed Brown 
3486cd652676SJed Brown    Collective on TS
3487cd652676SJed Brown 
34884165533cSJose E. Roman    Input Parameters:
3489cd652676SJed Brown +  ts - time stepping context
3490cd652676SJed Brown -  t - time to interpolate to
3491cd652676SJed Brown 
34924165533cSJose E. Roman    Output Parameter:
34930910c330SBarry Smith .  U - state at given time
3494cd652676SJed Brown 
3495cd652676SJed Brown    Level: intermediate
3496cd652676SJed Brown 
3497cd652676SJed Brown    Developer Notes:
3498cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3499cd652676SJed Brown 
3500874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3501cd652676SJed Brown @*/
35020910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3503cd652676SJed Brown {
3504cd652676SJed Brown   PetscFunctionBegin;
3505cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3506b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
35073c633725SBarry Smith   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]",t,(double)ts->ptime_prev,(double)ts->ptime);
35083c633725SBarry Smith   PetscCheck(ts->ops->interpolate,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35099566063dSJacob Faibussowitsch   PetscCall((*ts->ops->interpolate)(ts,t,U));
3510cd652676SJed Brown   PetscFunctionReturn(0);
3511cd652676SJed Brown }
3512cd652676SJed Brown 
3513d763cef2SBarry Smith /*@
35146d9e5789SSean Farley    TSStep - Steps one time step
3515d763cef2SBarry Smith 
3516d763cef2SBarry Smith    Collective on TS
3517d763cef2SBarry Smith 
3518d763cef2SBarry Smith    Input Parameter:
3519d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3520d763cef2SBarry Smith 
352127829d71SBarry Smith    Level: developer
3522d763cef2SBarry Smith 
3523b8123daeSJed Brown    Notes:
352427829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
352527829d71SBarry Smith 
3526b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3527b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3528b8123daeSJed Brown 
352919eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
353019eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
353125cb2221SBarry Smith 
35329be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3533d763cef2SBarry Smith @*/
3534193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3535d763cef2SBarry Smith {
3536dfbe8321SBarry Smith   PetscErrorCode   ierr;
3537fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3538be5899b3SLisandro Dalcin   PetscReal        ptime;
3539d763cef2SBarry Smith 
3540d763cef2SBarry Smith   PetscFunctionBegin;
35410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3542f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3543fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3544f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3545f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3546f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3547f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
35489566063dSJacob Faibussowitsch                                 "  year          = {2018}\n}\n",&cite);PetscCall(ierr);
3549fffbeea8SBarry Smith 
35509566063dSJacob Faibussowitsch   PetscCall(TSSetUp(ts));
35519566063dSJacob Faibussowitsch   PetscCall(TSTrajectorySetUp(ts->trajectory,ts));
3552d405a339SMatthew Knepley 
35533c633725SBarry Smith   PetscCheck(ts->ops->step,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
35543c633725SBarry 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>");
35553c633725SBarry 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()");
35563c633725SBarry 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");
3557a6772fa2SLisandro Dalcin 
3558be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3559be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
35602808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3561fc8dbba5SLisandro Dalcin 
35629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_Step,ts,0,0,0));
35639566063dSJacob Faibussowitsch   PetscCall((*ts->ops->step)(ts));
35649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_Step,ts,0,0,0));
3565fc8dbba5SLisandro Dalcin 
3566fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3567be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
35682808aa04SLisandro Dalcin     ts->steps++;
3569be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
357028d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
35714a658b32SHong Zhang     if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[ts->tspan->spanctr],10*PETSC_MACHINE_EPSILON,0) && ts->tspan->spanctr < ts->tspan->num_span_times) PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[ts->tspan->spanctr++]));
3572d2daff3dSHong Zhang   }
3573fc8dbba5SLisandro Dalcin 
3574fc8dbba5SLisandro Dalcin   if (!ts->reason) {
357508c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
357608c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
357708c7845fSBarry Smith   }
3578fc8dbba5SLisandro Dalcin 
35793c633725SBarry Smith   PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed || 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]);
35803c633725SBarry Smith   PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed,PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
358108c7845fSBarry Smith   PetscFunctionReturn(0);
358208c7845fSBarry Smith }
358308c7845fSBarry Smith 
358408c7845fSBarry Smith /*@
35857cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
35867cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
35877cbde773SLisandro Dalcin 
35887cbde773SLisandro Dalcin    Collective on TS
35897cbde773SLisandro Dalcin 
35904165533cSJose E. Roman    Input Parameters:
35917cbde773SLisandro Dalcin +  ts - time stepping context
359297bb3fdcSJose E. Roman -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
35937cbde773SLisandro Dalcin 
359497bb3fdcSJose E. Roman    Input/Output Parameter:
359597bb3fdcSJose E. Roman .  order - optional, desired order for the error evaluation or PETSC_DECIDE;
359697bb3fdcSJose E. Roman            on output, the actual order of the error evaluation
359797bb3fdcSJose E. Roman 
359897bb3fdcSJose E. Roman    Output Parameter:
359997bb3fdcSJose E. Roman .  wlte - the weighted local truncation error norm
36007cbde773SLisandro Dalcin 
36017cbde773SLisandro Dalcin    Level: advanced
36027cbde773SLisandro Dalcin 
36037cbde773SLisandro Dalcin    Notes:
36047cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36057cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36067cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36077cbde773SLisandro Dalcin 
36087cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36097cbde773SLisandro Dalcin @*/
36107cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36117cbde773SLisandro Dalcin {
36127cbde773SLisandro Dalcin   PetscFunctionBegin;
36137cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36147cbde773SLisandro Dalcin   PetscValidType(ts,1);
3615064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveEnum(ts,wnormtype,2);
36167cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36177cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36187cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36193c633725SBarry Smith   PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36203c633725SBarry Smith   PetscCheck(ts->ops->evaluatewlte,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36219566063dSJacob Faibussowitsch   PetscCall((*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte));
36227cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36237cbde773SLisandro Dalcin }
36247cbde773SLisandro Dalcin 
362505175c85SJed Brown /*@
362605175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
362705175c85SJed Brown 
36281c3436cfSJed Brown    Collective on TS
362905175c85SJed Brown 
36304165533cSJose E. Roman    Input Parameters:
36311c3436cfSJed Brown +  ts - time stepping context
36321c3436cfSJed Brown .  order - desired order of accuracy
36330298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
363405175c85SJed Brown 
36354165533cSJose E. Roman    Output Parameter:
36360910c330SBarry Smith .  U - state at the end of the current step
363705175c85SJed Brown 
363805175c85SJed Brown    Level: advanced
363905175c85SJed Brown 
3640108c343cSJed Brown    Notes:
3641108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3642108c343cSJed Brown    It is normally called by adaptive controllers before a step has been accepted and may also be called by the user after TSStep() has returned.
3643108c343cSJed Brown 
36441c3436cfSJed Brown .seealso: TSStep(), TSAdapt
364505175c85SJed Brown @*/
36460910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
364705175c85SJed Brown {
364805175c85SJed Brown   PetscFunctionBegin;
364905175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
365005175c85SJed Brown   PetscValidType(ts,1);
36510910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
36523c633725SBarry Smith   PetscCheck(ts->ops->evaluatestep,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
36539566063dSJacob Faibussowitsch   PetscCall((*ts->ops->evaluatestep)(ts,order,U,done));
365405175c85SJed Brown   PetscFunctionReturn(0);
365505175c85SJed Brown }
365605175c85SJed Brown 
3657aad739acSMatthew G. Knepley /*@C
36582e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3659aad739acSMatthew G. Knepley 
3660aad739acSMatthew G. Knepley   Not collective
3661aad739acSMatthew G. Knepley 
36624165533cSJose E. Roman   Input Parameter:
3663aad739acSMatthew G. Knepley . ts        - time stepping context
3664aad739acSMatthew G. Knepley 
36654165533cSJose E. Roman   Output Parameter:
36662e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3667aad739acSMatthew G. Knepley 
3668aad739acSMatthew G. Knepley    Level: advanced
3669aad739acSMatthew G. Knepley 
3670aad739acSMatthew G. Knepley    Notes:
3671aad739acSMatthew G. Knepley    The calling sequence for the function is
36722e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3673aad739acSMatthew G. Knepley $ ts - The timestepping context
36742e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3675aad739acSMatthew G. Knepley 
36762e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3677aad739acSMatthew G. Knepley @*/
36782e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3679aad739acSMatthew G. Knepley {
3680aad739acSMatthew G. Knepley   PetscFunctionBegin;
3681aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
36822e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
36832e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3684aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3685aad739acSMatthew G. Knepley }
3686aad739acSMatthew G. Knepley 
3687aad739acSMatthew G. Knepley /*@C
36882e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3689aad739acSMatthew G. Knepley 
3690aad739acSMatthew G. Knepley   Logically collective on ts
3691aad739acSMatthew G. Knepley 
36924165533cSJose E. Roman   Input Parameters:
3693aad739acSMatthew G. Knepley + ts        - time stepping context
36942e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3695aad739acSMatthew G. Knepley 
3696aad739acSMatthew G. Knepley   Level: advanced
3697aad739acSMatthew G. Knepley 
3698a96d6ef6SBarry Smith   Calling sequence for initCondition:
3699a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3700a96d6ef6SBarry Smith 
3701a96d6ef6SBarry Smith + ts - The timestepping context
3702a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3703aad739acSMatthew G. Knepley 
37042e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3705aad739acSMatthew G. Knepley @*/
37062e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3707aad739acSMatthew G. Knepley {
3708aad739acSMatthew G. Knepley   PetscFunctionBegin;
3709aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
37102e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
37112e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3712aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3713aad739acSMatthew G. Knepley }
3714aad739acSMatthew G. Knepley 
3715aad739acSMatthew G. Knepley /*@
37162e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3717aad739acSMatthew G. Knepley 
3718aad739acSMatthew G. Knepley   Collective on ts
3719aad739acSMatthew G. Knepley 
37204165533cSJose E. Roman   Input Parameters:
3721aad739acSMatthew G. Knepley + ts - time stepping context
37222e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3723aad739acSMatthew G. Knepley 
3724aad739acSMatthew G. Knepley   Level: advanced
3725aad739acSMatthew G. Knepley 
37262e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3727aad739acSMatthew G. Knepley @*/
37282e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3729aad739acSMatthew G. Knepley {
3730aad739acSMatthew G. Knepley   PetscFunctionBegin;
3731aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3732aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
37339566063dSJacob Faibussowitsch   if (ts->ops->initcondition) PetscCall((*ts->ops->initcondition)(ts, u));
3734aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3735aad739acSMatthew G. Knepley }
3736aad739acSMatthew G. Knepley 
3737aad739acSMatthew G. Knepley /*@C
3738aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3739aad739acSMatthew G. Knepley 
3740aad739acSMatthew G. Knepley   Not collective
3741aad739acSMatthew G. Knepley 
37424165533cSJose E. Roman   Input Parameter:
3743aad739acSMatthew G. Knepley . ts         - time stepping context
3744aad739acSMatthew G. Knepley 
37454165533cSJose E. Roman   Output Parameter:
3746aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3747aad739acSMatthew G. Knepley 
3748aad739acSMatthew G. Knepley   Level: advanced
3749aad739acSMatthew G. Knepley 
3750a96d6ef6SBarry Smith   Calling sequence for exactError:
3751a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3752a96d6ef6SBarry Smith 
3753a96d6ef6SBarry Smith + ts - The timestepping context
3754a96d6ef6SBarry Smith . u  - The approximate solution vector
3755a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3756aad739acSMatthew G. Knepley 
3757aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3758aad739acSMatthew G. Knepley @*/
3759aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3760aad739acSMatthew G. Knepley {
3761aad739acSMatthew G. Knepley   PetscFunctionBegin;
3762aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3763aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3764aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3765aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3766aad739acSMatthew G. Knepley }
3767aad739acSMatthew G. Knepley 
3768aad739acSMatthew G. Knepley /*@C
3769aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3770aad739acSMatthew G. Knepley 
3771aad739acSMatthew G. Knepley   Logically collective on ts
3772aad739acSMatthew G. Knepley 
37734165533cSJose E. Roman   Input Parameters:
3774aad739acSMatthew G. Knepley + ts         - time stepping context
3775aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3776aad739acSMatthew G. Knepley 
3777aad739acSMatthew G. Knepley   Level: advanced
3778aad739acSMatthew G. Knepley 
3779a96d6ef6SBarry Smith   Calling sequence for exactError:
3780a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3781a96d6ef6SBarry Smith 
3782a96d6ef6SBarry Smith + ts - The timestepping context
3783a96d6ef6SBarry Smith . u  - The approximate solution vector
3784a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3785aad739acSMatthew G. Knepley 
3786aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3787aad739acSMatthew G. Knepley @*/
3788aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3789aad739acSMatthew G. Knepley {
3790aad739acSMatthew G. Knepley   PetscFunctionBegin;
3791aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3792f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3793aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3794aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3795aad739acSMatthew G. Knepley }
3796aad739acSMatthew G. Knepley 
3797aad739acSMatthew G. Knepley /*@
3798aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3799aad739acSMatthew G. Knepley 
3800aad739acSMatthew G. Knepley   Collective on ts
3801aad739acSMatthew G. Knepley 
38024165533cSJose E. Roman   Input Parameters:
3803aad739acSMatthew G. Knepley + ts - time stepping context
3804aad739acSMatthew G. Knepley . u  - The approximate solution
3805aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3806aad739acSMatthew G. Knepley 
3807aad739acSMatthew G. Knepley   Level: advanced
3808aad739acSMatthew G. Knepley 
38092e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3810aad739acSMatthew G. Knepley @*/
3811aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3812aad739acSMatthew G. Knepley {
3813aad739acSMatthew G. Knepley   PetscFunctionBegin;
3814aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3815aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3816aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
38179566063dSJacob Faibussowitsch   if (ts->ops->exacterror) PetscCall((*ts->ops->exacterror)(ts, u, e));
3818aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3819aad739acSMatthew G. Knepley }
3820aad739acSMatthew G. Knepley 
3821b1cb36f3SHong Zhang /*@
38226a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
38236a4d4014SLisandro Dalcin 
38246a4d4014SLisandro Dalcin    Collective on TS
38256a4d4014SLisandro Dalcin 
3826d8d19677SJose E. Roman    Input Parameters:
38276a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
382863e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
382963e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
38305a3a76d0SJed Brown 
38316a4d4014SLisandro Dalcin    Level: beginner
38326a4d4014SLisandro Dalcin 
38335a3a76d0SJed Brown    Notes:
38345a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
38355a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
38365a3a76d0SJed Brown    stepped over the final time.
38375a3a76d0SJed Brown 
383863e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
38396a4d4014SLisandro Dalcin @*/
3840cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
38416a4d4014SLisandro Dalcin {
3842b06615a5SLisandro Dalcin   Vec               solution;
3843f22f69f0SBarry Smith 
38446a4d4014SLisandro Dalcin   PetscFunctionBegin;
38450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3846f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3847303a5415SBarry Smith 
38489566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTimeDefault(ts));
3849ee41a567SStefano 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 */
38500910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
38519566063dSJacob Faibussowitsch       PetscCall(VecDuplicate(u,&solution));
38529566063dSJacob Faibussowitsch       PetscCall(TSSetSolution(ts,solution));
38539566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&solution)); /* grant ownership */
38545a3a76d0SJed Brown     }
38559566063dSJacob Faibussowitsch     PetscCall(VecCopy(u,ts->vec_sol));
38563c633725SBarry Smith     PetscCheck(!ts->forward_solve,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3857bbd56ea5SKarl Rupp   } else if (u) {
38589566063dSJacob Faibussowitsch     PetscCall(TSSetSolution(ts,u));
38595a3a76d0SJed Brown   }
38609566063dSJacob Faibussowitsch   PetscCall(TSSetUp(ts));
38619566063dSJacob Faibussowitsch   PetscCall(TSTrajectorySetUp(ts->trajectory,ts));
3862a6772fa2SLisandro Dalcin 
38633c633725SBarry 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>");
38643c633725SBarry 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()");
38653c633725SBarry 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");
38664a658b32SHong 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");
38674a658b32SHong Zhang 
38684a658b32SHong Zhang   if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[0],10*PETSC_MACHINE_EPSILON,0)) { /* starting point in time span */
38694a658b32SHong Zhang     PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[0]));
38704a658b32SHong Zhang     ts->tspan->spanctr = 1;
38714a658b32SHong Zhang   }
3872a6772fa2SLisandro Dalcin 
3873715f1b00SHong Zhang   if (ts->forward_solve) {
38749566063dSJacob Faibussowitsch     PetscCall(TSForwardSetUp(ts));
3875715f1b00SHong Zhang   }
3876715f1b00SHong Zhang 
3877e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3878715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3879e7069c78SShri      TSTrajectory to incorrectly save the output files
3880e7069c78SShri   */
3881715f1b00SHong Zhang   /* reset time step and iteration counters */
38822808aa04SLisandro Dalcin   if (!ts->steps) {
38835ef26d82SJed Brown     ts->ksp_its           = 0;
38845ef26d82SJed Brown     ts->snes_its          = 0;
3885c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3886c610991cSLisandro Dalcin     ts->reject            = 0;
38872808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
38882808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
38897d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
38902808aa04SLisandro Dalcin   }
3891e97c63d7SStefano Zampini 
38924a658b32SHong Zhang   /* make sure initial time step does not overshoot final time or the next point in tspan */
3893e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
38944a658b32SHong Zhang     PetscReal maxdt;
3895e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
3896e97c63d7SStefano Zampini 
38974a658b32SHong Zhang     if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime;
38984a658b32SHong Zhang     else maxdt = ts->max_time - ts->ptime;
3899e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
3900e97c63d7SStefano Zampini   }
3901193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3902193ac0bcSJed Brown 
3903900f6b5bSMatthew G. Knepley   {
3904900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
3905900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
3906900f6b5bSMatthew G. Knepley     PetscBool         flg;
3907900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
3908900f6b5bSMatthew G. Knepley 
3909900f6b5bSMatthew G. Knepley     if (!incall) {
3910900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
39119566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg));
3912900f6b5bSMatthew G. Knepley       if (flg) {
3913900f6b5bSMatthew G. Knepley         PetscConvEst conv;
3914900f6b5bSMatthew G. Knepley         DM           dm;
3915900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
3916900f6b5bSMatthew G. Knepley         PetscInt     Nf;
3917f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
3918900f6b5bSMatthew G. Knepley 
3919900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
39209566063dSJacob Faibussowitsch         PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg));
39219566063dSJacob Faibussowitsch         PetscCall(TSGetDM(ts, &dm));
39229566063dSJacob Faibussowitsch         PetscCall(DMGetNumFields(dm, &Nf));
39239566063dSJacob Faibussowitsch         PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha));
39249566063dSJacob Faibussowitsch         PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv));
39259566063dSJacob Faibussowitsch         PetscCall(PetscConvEstUseTS(conv, checkTemporal));
39269566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetSolver(conv, (PetscObject) ts));
39279566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetFromOptions(conv));
39289566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetUp(conv));
39299566063dSJacob Faibussowitsch         PetscCall(PetscConvEstGetConvRate(conv, alpha));
39309566063dSJacob Faibussowitsch         PetscCall(PetscViewerPushFormat(viewer, format));
39319566063dSJacob Faibussowitsch         PetscCall(PetscConvEstRateView(conv, alpha, viewer));
39329566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
39339566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
39349566063dSJacob Faibussowitsch         PetscCall(PetscConvEstDestroy(&conv));
39359566063dSJacob Faibussowitsch         PetscCall(PetscFree(alpha));
3936900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
3937900f6b5bSMatthew G. Knepley       }
3938900f6b5bSMatthew G. Knepley     }
3939900f6b5bSMatthew G. Knepley   }
3940900f6b5bSMatthew G. Knepley 
39419566063dSJacob Faibussowitsch   PetscCall(TSViewFromOptions(ts,NULL,"-ts_view_pre"));
3942f05ece33SBarry Smith 
3943193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
39449566063dSJacob Faibussowitsch     PetscCall((*ts->ops->solve)(ts));
39459566063dSJacob Faibussowitsch     if (u) PetscCall(VecCopy(ts->vec_sol,u));
3946cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3947a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3948be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3949db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3950db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3951e7069c78SShri 
39522808aa04SLisandro Dalcin     if (!ts->steps) {
39539566063dSJacob Faibussowitsch       PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol));
39549566063dSJacob Faibussowitsch       PetscCall(TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol));
39552808aa04SLisandro Dalcin     }
39566427ac75SLisandro Dalcin 
3957e1a7a14fSJed Brown     while (!ts->reason) {
39589566063dSJacob Faibussowitsch       PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol));
39599687d888SLisandro Dalcin       if (!ts->steprollback) {
39609566063dSJacob Faibussowitsch         PetscCall(TSPreStep(ts));
39619687d888SLisandro Dalcin       }
39629566063dSJacob Faibussowitsch       PetscCall(TSStep(ts));
3963f3b1f45cSBarry Smith       if (ts->testjacobian) {
39649566063dSJacob Faibussowitsch         PetscCall(TSRHSJacobianTest(ts,NULL));
3965f3b1f45cSBarry Smith       }
3966f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
39679566063dSJacob Faibussowitsch         PetscCall(TSRHSJacobianTestTranspose(ts,NULL));
3968f3b1f45cSBarry Smith       }
3969cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
39707b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
39719566063dSJacob Faibussowitsch         PetscCall(TSForwardCostIntegral(ts));
39727b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
3973b1cb36f3SHong Zhang       }
397458818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
39757b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
39769566063dSJacob Faibussowitsch         PetscCall(TSForwardStep(ts));
39777b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
3978715f1b00SHong Zhang       }
39799566063dSJacob Faibussowitsch       PetscCall(TSPostEvaluate(ts));
39809566063dSJacob 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. */
398158818c2dSLisandro Dalcin       if (ts->steprollback) {
39829566063dSJacob Faibussowitsch         PetscCall(TSPostEvaluate(ts));
398358818c2dSLisandro Dalcin       }
3984e783b05fSHong Zhang       if (!ts->steprollback) {
39859566063dSJacob Faibussowitsch         PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol));
39869566063dSJacob Faibussowitsch         PetscCall(TSPostStep(ts));
3987aeb4809dSShri Abhyankar       }
3988193ac0bcSJed Brown     }
39899566063dSJacob Faibussowitsch     PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol));
39906427ac75SLisandro Dalcin 
399149354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
39929566063dSJacob Faibussowitsch       PetscCall(TSInterpolate(ts,ts->max_time,u));
3993cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3994b06615a5SLisandro Dalcin       solution = u;
39959566063dSJacob Faibussowitsch       PetscCall(TSMonitor(ts,-1,ts->solvetime,solution));
39960574a7fbSJed Brown     } else {
39979566063dSJacob Faibussowitsch       if (u) PetscCall(VecCopy(ts->vec_sol,u));
3998cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3999b06615a5SLisandro Dalcin       solution = ts->vec_sol;
40000574a7fbSJed Brown     }
4001193ac0bcSJed Brown   }
4002d2daff3dSHong Zhang 
40039566063dSJacob Faibussowitsch   PetscCall(TSViewFromOptions(ts,NULL,"-ts_view"));
40049566063dSJacob Faibussowitsch   PetscCall(VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution"));
40059566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsBlock((PetscObject)ts));
4006ef222394SBarry Smith   if (ts->adjoint_solve) {
40079566063dSJacob Faibussowitsch     PetscCall(TSAdjointSolve(ts));
40082b0a91c0SBarry Smith   }
40096a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40106a4d4014SLisandro Dalcin }
40116a4d4014SLisandro Dalcin 
4012d763cef2SBarry Smith /*@
4013b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4014d763cef2SBarry Smith 
4015d763cef2SBarry Smith    Not Collective
4016d763cef2SBarry Smith 
4017d763cef2SBarry Smith    Input Parameter:
4018d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4019d763cef2SBarry Smith 
4020d763cef2SBarry Smith    Output Parameter:
402119eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4022d763cef2SBarry Smith 
4023d763cef2SBarry Smith    Level: beginner
4024d763cef2SBarry Smith 
4025b8123daeSJed Brown    Note:
4026b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40279be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4028b8123daeSJed Brown 
40298f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4030d763cef2SBarry Smith 
4031d763cef2SBarry Smith @*/
40327087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4033d763cef2SBarry Smith {
4034d763cef2SBarry Smith   PetscFunctionBegin;
40350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4036f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4037d763cef2SBarry Smith   *t = ts->ptime;
4038d763cef2SBarry Smith   PetscFunctionReturn(0);
4039d763cef2SBarry Smith }
4040d763cef2SBarry Smith 
4041e5e524a1SHong Zhang /*@
4042e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4043e5e524a1SHong Zhang 
4044e5e524a1SHong Zhang    Not Collective
4045e5e524a1SHong Zhang 
4046e5e524a1SHong Zhang    Input Parameter:
4047e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4048e5e524a1SHong Zhang 
4049e5e524a1SHong Zhang    Output Parameter:
4050e5e524a1SHong Zhang .  t  - the previous time
4051e5e524a1SHong Zhang 
4052e5e524a1SHong Zhang    Level: beginner
4053e5e524a1SHong Zhang 
4054aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4055e5e524a1SHong Zhang 
4056e5e524a1SHong Zhang @*/
4057e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4058e5e524a1SHong Zhang {
4059e5e524a1SHong Zhang   PetscFunctionBegin;
4060e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4061e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4062e5e524a1SHong Zhang   *t = ts->ptime_prev;
4063e5e524a1SHong Zhang   PetscFunctionReturn(0);
4064e5e524a1SHong Zhang }
4065e5e524a1SHong Zhang 
40666a4d4014SLisandro Dalcin /*@
40676a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40686a4d4014SLisandro Dalcin 
40693f9fe445SBarry Smith    Logically Collective on TS
40706a4d4014SLisandro Dalcin 
40716a4d4014SLisandro Dalcin    Input Parameters:
40726a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40736a4d4014SLisandro Dalcin -  time - the time
40746a4d4014SLisandro Dalcin 
40756a4d4014SLisandro Dalcin    Level: intermediate
40766a4d4014SLisandro Dalcin 
407719eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
40786a4d4014SLisandro Dalcin 
40796a4d4014SLisandro Dalcin @*/
40807087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
40816a4d4014SLisandro Dalcin {
40826a4d4014SLisandro Dalcin   PetscFunctionBegin;
40830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4084c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
40856a4d4014SLisandro Dalcin   ts->ptime = t;
40866a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40876a4d4014SLisandro Dalcin }
40886a4d4014SLisandro Dalcin 
4089d763cef2SBarry Smith /*@C
4090d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4091d763cef2SBarry Smith    TS options in the database.
4092d763cef2SBarry Smith 
40933f9fe445SBarry Smith    Logically Collective on TS
4094d763cef2SBarry Smith 
4095d8d19677SJose E. Roman    Input Parameters:
4096d763cef2SBarry Smith +  ts     - The TS context
4097d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4098d763cef2SBarry Smith 
4099d763cef2SBarry Smith    Notes:
4100d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4101d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4102d763cef2SBarry Smith    hyphen.
4103d763cef2SBarry Smith 
4104d763cef2SBarry Smith    Level: advanced
4105d763cef2SBarry Smith 
4106d763cef2SBarry Smith .seealso: TSSetFromOptions()
4107d763cef2SBarry Smith 
4108d763cef2SBarry Smith @*/
41097087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4110d763cef2SBarry Smith {
4111089b2837SJed Brown   SNES           snes;
4112d763cef2SBarry Smith 
4113d763cef2SBarry Smith   PetscFunctionBegin;
41140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41159566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts,prefix));
41169566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
41179566063dSJacob Faibussowitsch   PetscCall(SNESSetOptionsPrefix(snes,prefix));
4118d763cef2SBarry Smith   PetscFunctionReturn(0);
4119d763cef2SBarry Smith }
4120d763cef2SBarry Smith 
4121d763cef2SBarry Smith /*@C
4122d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4123d763cef2SBarry Smith    TS options in the database.
4124d763cef2SBarry Smith 
41253f9fe445SBarry Smith    Logically Collective on TS
4126d763cef2SBarry Smith 
4127d8d19677SJose E. Roman    Input Parameters:
4128d763cef2SBarry Smith +  ts     - The TS context
4129d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4130d763cef2SBarry Smith 
4131d763cef2SBarry Smith    Notes:
4132d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4133d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4134d763cef2SBarry Smith    hyphen.
4135d763cef2SBarry Smith 
4136d763cef2SBarry Smith    Level: advanced
4137d763cef2SBarry Smith 
4138d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4139d763cef2SBarry Smith 
4140d763cef2SBarry Smith @*/
41417087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4142d763cef2SBarry Smith {
4143089b2837SJed Brown   SNES           snes;
4144d763cef2SBarry Smith 
4145d763cef2SBarry Smith   PetscFunctionBegin;
41460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41479566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix));
41489566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
41499566063dSJacob Faibussowitsch   PetscCall(SNESAppendOptionsPrefix(snes,prefix));
4150d763cef2SBarry Smith   PetscFunctionReturn(0);
4151d763cef2SBarry Smith }
4152d763cef2SBarry Smith 
4153d763cef2SBarry Smith /*@C
4154d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4155d763cef2SBarry Smith    TS options in the database.
4156d763cef2SBarry Smith 
4157d763cef2SBarry Smith    Not Collective
4158d763cef2SBarry Smith 
4159d763cef2SBarry Smith    Input Parameter:
4160d763cef2SBarry Smith .  ts - The TS context
4161d763cef2SBarry Smith 
4162d763cef2SBarry Smith    Output Parameter:
4163d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4164d763cef2SBarry Smith 
416595452b02SPatrick Sanan    Notes:
416695452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4167d763cef2SBarry Smith    sufficient length to hold the prefix.
4168d763cef2SBarry Smith 
4169d763cef2SBarry Smith    Level: intermediate
4170d763cef2SBarry Smith 
4171d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4172d763cef2SBarry Smith @*/
41737087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4174d763cef2SBarry Smith {
4175d763cef2SBarry Smith   PetscFunctionBegin;
41760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41774482741eSBarry Smith   PetscValidPointer(prefix,2);
41789566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts,prefix));
4179d763cef2SBarry Smith   PetscFunctionReturn(0);
4180d763cef2SBarry Smith }
4181d763cef2SBarry Smith 
4182d763cef2SBarry Smith /*@C
4183d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4184d763cef2SBarry Smith 
4185d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4186d763cef2SBarry Smith 
4187d763cef2SBarry Smith    Input Parameter:
4188d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4189d763cef2SBarry Smith 
4190d763cef2SBarry Smith    Output Parameters:
4191e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4192e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4193e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4194e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4195d763cef2SBarry Smith 
419695452b02SPatrick Sanan    Notes:
419795452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4198d763cef2SBarry Smith 
4199d763cef2SBarry Smith    Level: intermediate
4200d763cef2SBarry Smith 
420180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4202d763cef2SBarry Smith 
4203d763cef2SBarry Smith @*/
4204e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4205d763cef2SBarry Smith {
420624989b8cSPeter Brune   DM             dm;
4207089b2837SJed Brown 
4208d763cef2SBarry Smith   PetscFunctionBegin;
420923a57915SBarry Smith   if (Amat || Pmat) {
421023a57915SBarry Smith     SNES snes;
42119566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
42129566063dSJacob Faibussowitsch     PetscCall(SNESSetUpMatrices(snes));
42139566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL));
421423a57915SBarry Smith   }
42159566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
42169566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,func,ctx));
4217d763cef2SBarry Smith   PetscFunctionReturn(0);
4218d763cef2SBarry Smith }
4219d763cef2SBarry Smith 
42202eca1d9cSJed Brown /*@C
42212eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42222eca1d9cSJed Brown 
42232eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42242eca1d9cSJed Brown 
42252eca1d9cSJed Brown    Input Parameter:
42262eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42272eca1d9cSJed Brown 
42282eca1d9cSJed Brown    Output Parameters:
4229e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4230e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42312eca1d9cSJed Brown .  f   - The function to compute the matrices
42322eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42332eca1d9cSJed Brown 
423495452b02SPatrick Sanan    Notes:
423595452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42362eca1d9cSJed Brown 
42372eca1d9cSJed Brown    Level: advanced
42382eca1d9cSJed Brown 
423980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42402eca1d9cSJed Brown 
42412eca1d9cSJed Brown @*/
4242e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
42432eca1d9cSJed Brown {
424424989b8cSPeter Brune   DM             dm;
42450910c330SBarry Smith 
42462eca1d9cSJed Brown   PetscFunctionBegin;
4247c0aab802Sstefano_zampini   if (Amat || Pmat) {
4248c0aab802Sstefano_zampini     SNES snes;
42499566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
42509566063dSJacob Faibussowitsch     PetscCall(SNESSetUpMatrices(snes));
42519566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL));
4252c0aab802Sstefano_zampini   }
42539566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
42549566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,f,ctx));
42552eca1d9cSJed Brown   PetscFunctionReturn(0);
42562eca1d9cSJed Brown }
42572eca1d9cSJed Brown 
4258af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
42596c699258SBarry Smith /*@
42602a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
42616c699258SBarry Smith 
4262d083f849SBarry Smith    Logically Collective on ts
42636c699258SBarry Smith 
42646c699258SBarry Smith    Input Parameters:
42652a808120SBarry Smith +  ts - the ODE integrator object
42662a808120SBarry Smith -  dm - the dm, cannot be NULL
42676c699258SBarry Smith 
4268e03a659cSJed Brown    Notes:
4269e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4270e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4271e03a659cSJed Brown    different problems using the same function space.
4272e03a659cSJed Brown 
42736c699258SBarry Smith    Level: intermediate
42746c699258SBarry Smith 
42756c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
42766c699258SBarry Smith @*/
42777087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
42786c699258SBarry Smith {
4279089b2837SJed Brown   SNES           snes;
4280942e3340SBarry Smith   DMTS           tsdm;
42816c699258SBarry Smith 
42826c699258SBarry Smith   PetscFunctionBegin;
42830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42842a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
42859566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)dm));
4286942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
42872a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
42889566063dSJacob Faibussowitsch       PetscCall(DMCopyDMTS(ts->dm,dm));
42899566063dSJacob Faibussowitsch       PetscCall(DMGetDMTS(ts->dm,&tsdm));
429024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
429124989b8cSPeter Brune         tsdm->originaldm = dm;
429224989b8cSPeter Brune       }
429324989b8cSPeter Brune     }
42949566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&ts->dm));
429524989b8cSPeter Brune   }
42966c699258SBarry Smith   ts->dm = dm;
4297bbd56ea5SKarl Rupp 
42989566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
42999566063dSJacob Faibussowitsch   PetscCall(SNESSetDM(snes,dm));
43006c699258SBarry Smith   PetscFunctionReturn(0);
43016c699258SBarry Smith }
43026c699258SBarry Smith 
43036c699258SBarry Smith /*@
43046c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
43056c699258SBarry Smith 
43063f9fe445SBarry Smith    Not Collective
43076c699258SBarry Smith 
43086c699258SBarry Smith    Input Parameter:
43096c699258SBarry Smith . ts - the preconditioner context
43106c699258SBarry Smith 
43116c699258SBarry Smith    Output Parameter:
43126c699258SBarry Smith .  dm - the dm
43136c699258SBarry Smith 
43146c699258SBarry Smith    Level: intermediate
43156c699258SBarry Smith 
43166c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
43176c699258SBarry Smith @*/
43187087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
43196c699258SBarry Smith {
43206c699258SBarry Smith   PetscFunctionBegin;
43210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4322496e6a7aSJed Brown   if (!ts->dm) {
43239566063dSJacob Faibussowitsch     PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm));
43249566063dSJacob Faibussowitsch     if (ts->snes) PetscCall(SNESSetDM(ts->snes,ts->dm));
4325496e6a7aSJed Brown   }
43266c699258SBarry Smith   *dm = ts->dm;
43276c699258SBarry Smith   PetscFunctionReturn(0);
43286c699258SBarry Smith }
43291713a123SBarry Smith 
43300f5c6efeSJed Brown /*@
43310f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
43320f5c6efeSJed Brown 
43333f9fe445SBarry Smith    Logically Collective on SNES
43340f5c6efeSJed Brown 
4335d8d19677SJose E. Roman    Input Parameters:
4336d42a1c89SJed Brown + snes - nonlinear solver
43370910c330SBarry Smith . U - the current state at which to evaluate the residual
4338d42a1c89SJed Brown - ctx - user context, must be a TS
43390f5c6efeSJed Brown 
43400f5c6efeSJed Brown    Output Parameter:
43410f5c6efeSJed Brown . F - the nonlinear residual
43420f5c6efeSJed Brown 
43430f5c6efeSJed Brown    Notes:
43440f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
43450f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
43460f5c6efeSJed Brown 
43470f5c6efeSJed Brown    Level: advanced
43480f5c6efeSJed Brown 
43490f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
43500f5c6efeSJed Brown @*/
43510910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
43520f5c6efeSJed Brown {
43530f5c6efeSJed Brown   TS             ts = (TS)ctx;
43540f5c6efeSJed Brown 
43550f5c6efeSJed Brown   PetscFunctionBegin;
43560f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
43570910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
43580f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
43590f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
43609566063dSJacob Faibussowitsch   PetscCall((ts->ops->snesfunction)(snes,U,F,ts));
43610f5c6efeSJed Brown   PetscFunctionReturn(0);
43620f5c6efeSJed Brown }
43630f5c6efeSJed Brown 
43640f5c6efeSJed Brown /*@
43650f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
43660f5c6efeSJed Brown 
43670f5c6efeSJed Brown    Collective on SNES
43680f5c6efeSJed Brown 
4369d8d19677SJose E. Roman    Input Parameters:
43700f5c6efeSJed Brown + snes - nonlinear solver
43710910c330SBarry Smith . U - the current state at which to evaluate the residual
43720f5c6efeSJed Brown - ctx - user context, must be a TS
43730f5c6efeSJed Brown 
4374d8d19677SJose E. Roman    Output Parameters:
43750f5c6efeSJed Brown + A - the Jacobian
43766b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A)
43770f5c6efeSJed Brown 
43780f5c6efeSJed Brown    Notes:
43790f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
43800f5c6efeSJed Brown 
43810f5c6efeSJed Brown    Level: developer
43820f5c6efeSJed Brown 
43830f5c6efeSJed Brown .seealso: SNESSetJacobian()
43840f5c6efeSJed Brown @*/
4385d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
43860f5c6efeSJed Brown {
43870f5c6efeSJed Brown   TS             ts = (TS)ctx;
43880f5c6efeSJed Brown 
43890f5c6efeSJed Brown   PetscFunctionBegin;
43900f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
43910910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
43920f5c6efeSJed Brown   PetscValidPointer(A,3);
439394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
43940f5c6efeSJed Brown   PetscValidPointer(B,4);
439594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
4396064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ts,TS_CLASSID,5);
43979566063dSJacob Faibussowitsch   PetscCall((ts->ops->snesjacobian)(snes,U,A,B,ts));
43980f5c6efeSJed Brown   PetscFunctionReturn(0);
43990f5c6efeSJed Brown }
4400325fc9f4SBarry Smith 
44010e4ef248SJed Brown /*@C
44029ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
44030e4ef248SJed Brown 
44040e4ef248SJed Brown    Collective on TS
44050e4ef248SJed Brown 
44064165533cSJose E. Roman    Input Parameters:
44070e4ef248SJed Brown +  ts - time stepping context
44080e4ef248SJed Brown .  t - time at which to evaluate
44090910c330SBarry Smith .  U - state at which to evaluate
44100e4ef248SJed Brown -  ctx - context
44110e4ef248SJed Brown 
44124165533cSJose E. Roman    Output Parameter:
44130e4ef248SJed Brown .  F - right hand side
44140e4ef248SJed Brown 
44150e4ef248SJed Brown    Level: intermediate
44160e4ef248SJed Brown 
44170e4ef248SJed Brown    Notes:
44180e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
44190e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
44200e4ef248SJed Brown 
44210e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
44220e4ef248SJed Brown @*/
44230910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
44240e4ef248SJed Brown {
44250e4ef248SJed Brown   Mat            Arhs,Brhs;
44260e4ef248SJed Brown 
44270e4ef248SJed Brown   PetscFunctionBegin;
44289566063dSJacob Faibussowitsch   PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs));
44292663174eSHong Zhang   /* undo the damage caused by shifting */
44309566063dSJacob Faibussowitsch   PetscCall(TSRecoverRHSJacobian(ts,Arhs,Brhs));
44319566063dSJacob Faibussowitsch   PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs));
44329566063dSJacob Faibussowitsch   PetscCall(MatMult(Arhs,U,F));
44330e4ef248SJed Brown   PetscFunctionReturn(0);
44340e4ef248SJed Brown }
44350e4ef248SJed Brown 
44360e4ef248SJed Brown /*@C
44370e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
44380e4ef248SJed Brown 
44390e4ef248SJed Brown    Collective on TS
44400e4ef248SJed Brown 
44414165533cSJose E. Roman    Input Parameters:
44420e4ef248SJed Brown +  ts - time stepping context
44430e4ef248SJed Brown .  t - time at which to evaluate
44440910c330SBarry Smith .  U - state at which to evaluate
44450e4ef248SJed Brown -  ctx - context
44460e4ef248SJed Brown 
44474165533cSJose E. Roman    Output Parameters:
44480e4ef248SJed Brown +  A - pointer to operator
444997bb3fdcSJose E. Roman -  B - pointer to preconditioning matrix
44500e4ef248SJed Brown 
44510e4ef248SJed Brown    Level: intermediate
44520e4ef248SJed Brown 
44530e4ef248SJed Brown    Notes:
44540e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
44550e4ef248SJed Brown 
44560e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
44570e4ef248SJed Brown @*/
4458d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
44590e4ef248SJed Brown {
44600e4ef248SJed Brown   PetscFunctionBegin;
44610e4ef248SJed Brown   PetscFunctionReturn(0);
44620e4ef248SJed Brown }
44630e4ef248SJed Brown 
44640026cea9SSean Farley /*@C
44650026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
44660026cea9SSean Farley 
44670026cea9SSean Farley    Collective on TS
44680026cea9SSean Farley 
44694165533cSJose E. Roman    Input Parameters:
44700026cea9SSean Farley +  ts - time stepping context
44710026cea9SSean Farley .  t - time at which to evaluate
44720910c330SBarry Smith .  U - state at which to evaluate
44730910c330SBarry Smith .  Udot - time derivative of state vector
44740026cea9SSean Farley -  ctx - context
44750026cea9SSean Farley 
44764165533cSJose E. Roman    Output Parameter:
44770026cea9SSean Farley .  F - left hand side
44780026cea9SSean Farley 
44790026cea9SSean Farley    Level: intermediate
44800026cea9SSean Farley 
44810026cea9SSean Farley    Notes:
44820910c330SBarry 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
44830026cea9SSean Farley    user is required to write their own TSComputeIFunction.
44840026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
44850026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
44860026cea9SSean Farley 
44879ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
44889ae8fd06SBarry Smith 
44899ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
44900026cea9SSean Farley @*/
44910910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
44920026cea9SSean Farley {
44930026cea9SSean Farley   Mat            A,B;
44940026cea9SSean Farley 
44950026cea9SSean Farley   PetscFunctionBegin;
44969566063dSJacob Faibussowitsch   PetscCall(TSGetIJacobian(ts,&A,&B,NULL,NULL));
44979566063dSJacob Faibussowitsch   PetscCall(TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE));
44989566063dSJacob Faibussowitsch   PetscCall(MatMult(A,Udot,F));
44990026cea9SSean Farley   PetscFunctionReturn(0);
45000026cea9SSean Farley }
45010026cea9SSean Farley 
45020026cea9SSean Farley /*@C
4503b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
45040026cea9SSean Farley 
45050026cea9SSean Farley    Collective on TS
45060026cea9SSean Farley 
45074165533cSJose E. Roman    Input Parameters:
45080026cea9SSean Farley +  ts - time stepping context
45090026cea9SSean Farley .  t - time at which to evaluate
45100910c330SBarry Smith .  U - state at which to evaluate
45110910c330SBarry Smith .  Udot - time derivative of state vector
45120026cea9SSean Farley .  shift - shift to apply
45130026cea9SSean Farley -  ctx - context
45140026cea9SSean Farley 
45154165533cSJose E. Roman    Output Parameters:
45160026cea9SSean Farley +  A - pointer to operator
451797bb3fdcSJose E. Roman -  B - pointer to preconditioning matrix
45180026cea9SSean Farley 
4519b41af12eSJed Brown    Level: advanced
45200026cea9SSean Farley 
45210026cea9SSean Farley    Notes:
45220026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
45230026cea9SSean Farley 
4524b41af12eSJed Brown    It is only appropriate for problems of the form
4525b41af12eSJed Brown 
4526b41af12eSJed Brown $     M Udot = F(U,t)
4527b41af12eSJed Brown 
4528b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4529b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4530b41af12eSJed Brown   an implicit operator of the form
4531b41af12eSJed Brown 
4532b41af12eSJed Brown $    shift*M + J
4533b41af12eSJed Brown 
4534b41af12eSJed 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
4535b41af12eSJed Brown   a copy of M or reassemble it when requested.
4536b41af12eSJed Brown 
45370026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
45380026cea9SSean Farley @*/
4539d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
45400026cea9SSean Farley {
45410026cea9SSean Farley   PetscFunctionBegin;
45429566063dSJacob Faibussowitsch   PetscCall(MatScale(A, shift / ts->ijacobian.shift));
4543b41af12eSJed Brown   ts->ijacobian.shift = shift;
45440026cea9SSean Farley   PetscFunctionReturn(0);
45450026cea9SSean Farley }
4546b41af12eSJed Brown 
4547e817cc15SEmil Constantinescu /*@
4548e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4549e817cc15SEmil Constantinescu 
4550e817cc15SEmil Constantinescu    Not Collective
4551e817cc15SEmil Constantinescu 
4552e817cc15SEmil Constantinescu    Input Parameter:
4553e817cc15SEmil Constantinescu .  ts - the TS context
4554e817cc15SEmil Constantinescu 
4555e817cc15SEmil Constantinescu    Output Parameter:
45564e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4557e817cc15SEmil Constantinescu 
4558e817cc15SEmil Constantinescu    Level: beginner
4559e817cc15SEmil Constantinescu 
4560e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4561e817cc15SEmil Constantinescu @*/
4562e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4563e817cc15SEmil Constantinescu {
4564e817cc15SEmil Constantinescu   PetscFunctionBegin;
4565e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4566e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4567e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4568e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4569e817cc15SEmil Constantinescu }
4570e817cc15SEmil Constantinescu 
4571e817cc15SEmil Constantinescu /*@
4572e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4573e817cc15SEmil Constantinescu 
4574e817cc15SEmil Constantinescu    Not Collective
4575e817cc15SEmil Constantinescu 
4576d8d19677SJose E. Roman    Input Parameters:
4577e817cc15SEmil Constantinescu +  ts - the TS context
45781297b224SEmil Constantinescu -  equation_type - see TSEquationType
4579e817cc15SEmil Constantinescu 
4580e817cc15SEmil Constantinescu    Level: advanced
4581e817cc15SEmil Constantinescu 
4582e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4583e817cc15SEmil Constantinescu @*/
4584e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4585e817cc15SEmil Constantinescu {
4586e817cc15SEmil Constantinescu   PetscFunctionBegin;
4587e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4588e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4589e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4590e817cc15SEmil Constantinescu }
45910026cea9SSean Farley 
45924af1b03aSJed Brown /*@
45934af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
45944af1b03aSJed Brown 
45954af1b03aSJed Brown    Not Collective
45964af1b03aSJed Brown 
45974af1b03aSJed Brown    Input Parameter:
45984af1b03aSJed Brown .  ts - the TS context
45994af1b03aSJed Brown 
46004af1b03aSJed Brown    Output Parameter:
46014af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
46024af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
46034af1b03aSJed Brown 
4604487e0bb9SJed Brown    Level: beginner
46054af1b03aSJed Brown 
4606cd652676SJed Brown    Notes:
4607cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
46084af1b03aSJed Brown 
46094af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
46104af1b03aSJed Brown @*/
46114af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
46124af1b03aSJed Brown {
46134af1b03aSJed Brown   PetscFunctionBegin;
46144af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46154af1b03aSJed Brown   PetscValidPointer(reason,2);
46164af1b03aSJed Brown   *reason = ts->reason;
46174af1b03aSJed Brown   PetscFunctionReturn(0);
46184af1b03aSJed Brown }
46194af1b03aSJed Brown 
4620d6ad946cSShri Abhyankar /*@
4621d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4622d6ad946cSShri Abhyankar 
46236b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
4624d6ad946cSShri Abhyankar 
46256b221cbeSPatrick Sanan    Input Parameters:
4626d6ad946cSShri Abhyankar +  ts - the TS context
46276b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4628d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4629d6ad946cSShri Abhyankar 
4630f5abba47SShri Abhyankar    Level: advanced
4631d6ad946cSShri Abhyankar 
4632d6ad946cSShri Abhyankar    Notes:
46336b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
4634d6ad946cSShri Abhyankar 
4635d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4636d6ad946cSShri Abhyankar @*/
4637d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4638d6ad946cSShri Abhyankar {
4639d6ad946cSShri Abhyankar   PetscFunctionBegin;
4640d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4641d6ad946cSShri Abhyankar   ts->reason = reason;
4642d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4643d6ad946cSShri Abhyankar }
4644d6ad946cSShri Abhyankar 
4645cc708dedSBarry Smith /*@
4646cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4647cc708dedSBarry Smith 
4648cc708dedSBarry Smith    Not Collective
4649cc708dedSBarry Smith 
4650cc708dedSBarry Smith    Input Parameter:
4651cc708dedSBarry Smith .  ts - the TS context
4652cc708dedSBarry Smith 
4653cc708dedSBarry Smith    Output Parameter:
465419eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4655cc708dedSBarry Smith 
4656487e0bb9SJed Brown    Level: beginner
4657cc708dedSBarry Smith 
4658cc708dedSBarry Smith    Notes:
4659cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4660cc708dedSBarry Smith 
4661cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4662cc708dedSBarry Smith @*/
4663cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4664cc708dedSBarry Smith {
4665cc708dedSBarry Smith   PetscFunctionBegin;
4666cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4667dadcf809SJacob Faibussowitsch   PetscValidRealPointer(ftime,2);
4668cc708dedSBarry Smith   *ftime = ts->solvetime;
4669cc708dedSBarry Smith   PetscFunctionReturn(0);
4670cc708dedSBarry Smith }
4671cc708dedSBarry Smith 
46722c18e0fdSBarry Smith /*@
46735ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
46749f67acb7SJed Brown    used by the time integrator.
46759f67acb7SJed Brown 
46769f67acb7SJed Brown    Not Collective
46779f67acb7SJed Brown 
46789f67acb7SJed Brown    Input Parameter:
46799f67acb7SJed Brown .  ts - TS context
46809f67acb7SJed Brown 
46819f67acb7SJed Brown    Output Parameter:
46829f67acb7SJed Brown .  nits - number of nonlinear iterations
46839f67acb7SJed Brown 
46849f67acb7SJed Brown    Notes:
46859f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
46869f67acb7SJed Brown 
46879f67acb7SJed Brown    Level: intermediate
46889f67acb7SJed Brown 
46895ef26d82SJed Brown .seealso:  TSGetKSPIterations()
46909f67acb7SJed Brown @*/
46915ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
46929f67acb7SJed Brown {
46939f67acb7SJed Brown   PetscFunctionBegin;
46949f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46959f67acb7SJed Brown   PetscValidIntPointer(nits,2);
46965ef26d82SJed Brown   *nits = ts->snes_its;
46979f67acb7SJed Brown   PetscFunctionReturn(0);
46989f67acb7SJed Brown }
46999f67acb7SJed Brown 
47009f67acb7SJed Brown /*@
47015ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
47029f67acb7SJed Brown    used by the time integrator.
47039f67acb7SJed Brown 
47049f67acb7SJed Brown    Not Collective
47059f67acb7SJed Brown 
47069f67acb7SJed Brown    Input Parameter:
47079f67acb7SJed Brown .  ts - TS context
47089f67acb7SJed Brown 
47099f67acb7SJed Brown    Output Parameter:
47109f67acb7SJed Brown .  lits - number of linear iterations
47119f67acb7SJed Brown 
47129f67acb7SJed Brown    Notes:
47139f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
47149f67acb7SJed Brown 
47159f67acb7SJed Brown    Level: intermediate
47169f67acb7SJed Brown 
47175ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
47189f67acb7SJed Brown @*/
47195ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
47209f67acb7SJed Brown {
47219f67acb7SJed Brown   PetscFunctionBegin;
47229f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47239f67acb7SJed Brown   PetscValidIntPointer(lits,2);
47245ef26d82SJed Brown   *lits = ts->ksp_its;
47259f67acb7SJed Brown   PetscFunctionReturn(0);
47269f67acb7SJed Brown }
47279f67acb7SJed Brown 
4728cef5090cSJed Brown /*@
4729cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4730cef5090cSJed Brown 
4731cef5090cSJed Brown    Not Collective
4732cef5090cSJed Brown 
4733cef5090cSJed Brown    Input Parameter:
4734cef5090cSJed Brown .  ts - TS context
4735cef5090cSJed Brown 
4736cef5090cSJed Brown    Output Parameter:
4737cef5090cSJed Brown .  rejects - number of steps rejected
4738cef5090cSJed Brown 
4739cef5090cSJed Brown    Notes:
4740cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4741cef5090cSJed Brown 
4742cef5090cSJed Brown    Level: intermediate
4743cef5090cSJed Brown 
47445ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4745cef5090cSJed Brown @*/
4746cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4747cef5090cSJed Brown {
4748cef5090cSJed Brown   PetscFunctionBegin;
4749cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4750cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4751cef5090cSJed Brown   *rejects = ts->reject;
4752cef5090cSJed Brown   PetscFunctionReturn(0);
4753cef5090cSJed Brown }
4754cef5090cSJed Brown 
4755cef5090cSJed Brown /*@
4756cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4757cef5090cSJed Brown 
4758cef5090cSJed Brown    Not Collective
4759cef5090cSJed Brown 
4760cef5090cSJed Brown    Input Parameter:
4761cef5090cSJed Brown .  ts - TS context
4762cef5090cSJed Brown 
4763cef5090cSJed Brown    Output Parameter:
4764cef5090cSJed Brown .  fails - number of failed nonlinear solves
4765cef5090cSJed Brown 
4766cef5090cSJed Brown    Notes:
4767cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4768cef5090cSJed Brown 
4769cef5090cSJed Brown    Level: intermediate
4770cef5090cSJed Brown 
47715ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4772cef5090cSJed Brown @*/
4773cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4774cef5090cSJed Brown {
4775cef5090cSJed Brown   PetscFunctionBegin;
4776cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4777cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4778cef5090cSJed Brown   *fails = ts->num_snes_failures;
4779cef5090cSJed Brown   PetscFunctionReturn(0);
4780cef5090cSJed Brown }
4781cef5090cSJed Brown 
4782cef5090cSJed Brown /*@
4783cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4784cef5090cSJed Brown 
4785cef5090cSJed Brown    Not Collective
4786cef5090cSJed Brown 
4787d8d19677SJose E. Roman    Input Parameters:
4788cef5090cSJed Brown +  ts - TS context
4789cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4790cef5090cSJed Brown 
4791cef5090cSJed Brown    Notes:
4792cef5090cSJed Brown    The counter is reset to zero for each step
4793cef5090cSJed Brown 
4794cef5090cSJed Brown    Options Database Key:
4795cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4796cef5090cSJed Brown 
4797cef5090cSJed Brown    Level: intermediate
4798cef5090cSJed Brown 
47995ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4800cef5090cSJed Brown @*/
4801cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4802cef5090cSJed Brown {
4803cef5090cSJed Brown   PetscFunctionBegin;
4804cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4805cef5090cSJed Brown   ts->max_reject = rejects;
4806cef5090cSJed Brown   PetscFunctionReturn(0);
4807cef5090cSJed Brown }
4808cef5090cSJed Brown 
4809cef5090cSJed Brown /*@
4810cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4811cef5090cSJed Brown 
4812cef5090cSJed Brown    Not Collective
4813cef5090cSJed Brown 
4814d8d19677SJose E. Roman    Input Parameters:
4815cef5090cSJed Brown +  ts - TS context
4816cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4817cef5090cSJed Brown 
4818cef5090cSJed Brown    Notes:
4819cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4820cef5090cSJed Brown 
4821cef5090cSJed Brown    Options Database Key:
4822cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4823cef5090cSJed Brown 
4824cef5090cSJed Brown    Level: intermediate
4825cef5090cSJed Brown 
48265ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4827cef5090cSJed Brown @*/
4828cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4829cef5090cSJed Brown {
4830cef5090cSJed Brown   PetscFunctionBegin;
4831cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4832cef5090cSJed Brown   ts->max_snes_failures = fails;
4833cef5090cSJed Brown   PetscFunctionReturn(0);
4834cef5090cSJed Brown }
4835cef5090cSJed Brown 
4836cef5090cSJed Brown /*@
4837cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4838cef5090cSJed Brown 
4839cef5090cSJed Brown    Not Collective
4840cef5090cSJed Brown 
4841d8d19677SJose E. Roman    Input Parameters:
4842cef5090cSJed Brown +  ts - TS context
4843cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4844cef5090cSJed Brown 
4845cef5090cSJed Brown    Options Database Key:
4846cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4847cef5090cSJed Brown 
4848cef5090cSJed Brown    Level: intermediate
4849cef5090cSJed Brown 
48505ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4851cef5090cSJed Brown @*/
4852cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4853cef5090cSJed Brown {
4854cef5090cSJed Brown   PetscFunctionBegin;
4855cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4856cef5090cSJed Brown   ts->errorifstepfailed = err;
4857cef5090cSJed Brown   PetscFunctionReturn(0);
4858cef5090cSJed Brown }
4859cef5090cSJed Brown 
486084df9cb4SJed Brown /*@
4861552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
486284df9cb4SJed Brown 
4863ed81e22dSJed Brown    Collective on TS if controller has not been created yet
486484df9cb4SJed Brown 
48654165533cSJose E. Roman    Input Parameter:
4866ed81e22dSJed Brown .  ts - time stepping context
486784df9cb4SJed Brown 
48684165533cSJose E. Roman    Output Parameter:
4869ed81e22dSJed Brown .  adapt - adaptive controller
487084df9cb4SJed Brown 
487184df9cb4SJed Brown    Level: intermediate
487284df9cb4SJed Brown 
4873ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
487484df9cb4SJed Brown @*/
4875552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
487684df9cb4SJed Brown {
487784df9cb4SJed Brown   PetscFunctionBegin;
487884df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4879bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
488084df9cb4SJed Brown   if (!ts->adapt) {
48819566063dSJacob Faibussowitsch     PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt));
48829566063dSJacob Faibussowitsch     PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt));
48839566063dSJacob Faibussowitsch     PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1));
488484df9cb4SJed Brown   }
4885bec58848SLisandro Dalcin   *adapt = ts->adapt;
488684df9cb4SJed Brown   PetscFunctionReturn(0);
488784df9cb4SJed Brown }
4888d6ebe24aSShri Abhyankar 
48891c3436cfSJed Brown /*@
48901c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
48911c3436cfSJed Brown 
48921c3436cfSJed Brown    Logically Collective
48931c3436cfSJed Brown 
48944165533cSJose E. Roman    Input Parameters:
48951c3436cfSJed Brown +  ts - time integration context
48961c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
48970298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
48981c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
48990298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
49001c3436cfSJed Brown 
4901a3cdaa26SBarry Smith    Options Database keys:
4902a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
490367b8a455SSatish Balay -  -ts_atol <atol> - Absolute tolerance for local truncation error
4904a3cdaa26SBarry Smith 
49053ff766beSShri Abhyankar    Notes:
49063ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
49073ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
49083ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
49093ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
49103ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
49113ff766beSShri Abhyankar    differential variables.
49123ff766beSShri Abhyankar 
49131c3436cfSJed Brown    Level: beginner
49141c3436cfSJed Brown 
49155c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
49161c3436cfSJed Brown @*/
49171c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
49181c3436cfSJed Brown {
49191c3436cfSJed Brown   PetscFunctionBegin;
4920c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
49211c3436cfSJed Brown   if (vatol) {
49229566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)vatol));
49239566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vatol));
49241c3436cfSJed Brown     ts->vatol = vatol;
49251c3436cfSJed Brown   }
4926c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
49271c3436cfSJed Brown   if (vrtol) {
49289566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)vrtol));
49299566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vrtol));
49301c3436cfSJed Brown     ts->vrtol = vrtol;
49311c3436cfSJed Brown   }
49321c3436cfSJed Brown   PetscFunctionReturn(0);
49331c3436cfSJed Brown }
49341c3436cfSJed Brown 
4935c5033834SJed Brown /*@
4936c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4937c5033834SJed Brown 
4938c5033834SJed Brown    Logically Collective
4939c5033834SJed Brown 
49404165533cSJose E. Roman    Input Parameter:
4941c5033834SJed Brown .  ts - time integration context
4942c5033834SJed Brown 
49434165533cSJose E. Roman    Output Parameters:
49440298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
49450298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
49460298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
49470298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4948c5033834SJed Brown 
4949c5033834SJed Brown    Level: beginner
4950c5033834SJed Brown 
49515c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
4952c5033834SJed Brown @*/
4953c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4954c5033834SJed Brown {
4955c5033834SJed Brown   PetscFunctionBegin;
4956c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4957c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4958c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4959c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4960c5033834SJed Brown   PetscFunctionReturn(0);
4961c5033834SJed Brown }
4962c5033834SJed Brown 
49639c6b16b5SShri Abhyankar /*@
4964a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
49659c6b16b5SShri Abhyankar 
49669c6b16b5SShri Abhyankar    Collective on TS
49679c6b16b5SShri Abhyankar 
49684165533cSJose E. Roman    Input Parameters:
49699c6b16b5SShri Abhyankar +  ts - time stepping context
4970a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
4971a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
49729c6b16b5SShri Abhyankar 
49734165533cSJose E. Roman    Output Parameters:
4974a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
49757453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
4976a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
49779c6b16b5SShri Abhyankar 
49789c6b16b5SShri Abhyankar    Level: developer
49799c6b16b5SShri Abhyankar 
4980deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
49819c6b16b5SShri Abhyankar @*/
49827453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
49839c6b16b5SShri Abhyankar {
49849c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
49857453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
49867453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
49879c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
49887453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
49897453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
49907453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
49919c6b16b5SShri Abhyankar 
49929c6b16b5SShri Abhyankar   PetscFunctionBegin;
49939c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4994a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
4995a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
4996a4868fbcSLisandro Dalcin   PetscValidType(U,2);
4997a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
4998a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
4999dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,4);
5000dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,5);
5001dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,6);
50023c633725SBarry Smith   PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
50039c6b16b5SShri Abhyankar 
50049566063dSJacob Faibussowitsch   PetscCall(VecGetSize(U,&N));
50059566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(U,&n));
50069566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(U,&rstart,NULL));
50079566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
50089566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
50097453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
50107453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
50117453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
50129c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
50139c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
50149566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
50159566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
50169c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
501776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50187453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50197453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
50207453f775SEmil Constantinescu       if (tola>0.) {
50217453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50227453f775SEmil Constantinescu         na_loc++;
50237453f775SEmil Constantinescu       }
50247453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50257453f775SEmil Constantinescu       if (tolr>0.) {
50267453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50277453f775SEmil Constantinescu         nr_loc++;
50287453f775SEmil Constantinescu       }
50297453f775SEmil Constantinescu       tol=tola+tolr;
50307453f775SEmil Constantinescu       if (tol>0.) {
50317453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
50327453f775SEmil Constantinescu         n_loc++;
50337453f775SEmil Constantinescu       }
50349c6b16b5SShri Abhyankar     }
50359566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
50369566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
50379c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
50389c6b16b5SShri Abhyankar     const PetscScalar *atol;
50399566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
50409c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
504176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50427453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50437453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
50447453f775SEmil Constantinescu       if (tola>0.) {
50457453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50467453f775SEmil Constantinescu         na_loc++;
50477453f775SEmil Constantinescu       }
50487453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50497453f775SEmil Constantinescu       if (tolr>0.) {
50507453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50517453f775SEmil Constantinescu         nr_loc++;
50527453f775SEmil Constantinescu       }
50537453f775SEmil Constantinescu       tol=tola+tolr;
50547453f775SEmil Constantinescu       if (tol>0.) {
50557453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
50567453f775SEmil Constantinescu         n_loc++;
50577453f775SEmil Constantinescu       }
50589c6b16b5SShri Abhyankar     }
50599566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
50609c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
50619c6b16b5SShri Abhyankar     const PetscScalar *rtol;
50629566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
50639c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
506476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50657453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50667453f775SEmil Constantinescu       tola = ts->atol;
50677453f775SEmil Constantinescu       if (tola>0.) {
50687453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50697453f775SEmil Constantinescu         na_loc++;
50707453f775SEmil Constantinescu       }
50717453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50727453f775SEmil Constantinescu       if (tolr>0.) {
50737453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50747453f775SEmil Constantinescu         nr_loc++;
50757453f775SEmil Constantinescu       }
50767453f775SEmil Constantinescu       tol=tola+tolr;
50777453f775SEmil Constantinescu       if (tol>0.) {
50787453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
50797453f775SEmil Constantinescu         n_loc++;
50807453f775SEmil Constantinescu       }
50819c6b16b5SShri Abhyankar     }
50829566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
50839c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
50849c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
508576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50867453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50877453f775SEmil Constantinescu       tola = ts->atol;
50887453f775SEmil Constantinescu       if (tola>0.) {
50897453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50907453f775SEmil Constantinescu         na_loc++;
50917453f775SEmil Constantinescu       }
50927453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50937453f775SEmil Constantinescu       if (tolr>0.) {
50947453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50957453f775SEmil Constantinescu         nr_loc++;
50967453f775SEmil Constantinescu       }
50977453f775SEmil Constantinescu       tol=tola+tolr;
50987453f775SEmil Constantinescu       if (tol>0.) {
50997453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
51007453f775SEmil Constantinescu         n_loc++;
51017453f775SEmil Constantinescu       }
51029c6b16b5SShri Abhyankar     }
51039c6b16b5SShri Abhyankar   }
51049566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
51059566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
51069c6b16b5SShri Abhyankar 
51077453f775SEmil Constantinescu   err_loc[0] = sum;
51087453f775SEmil Constantinescu   err_loc[1] = suma;
51097453f775SEmil Constantinescu   err_loc[2] = sumr;
51107453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
51117453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
51127453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
51137453f775SEmil Constantinescu 
51141c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts)));
51157453f775SEmil Constantinescu 
51167453f775SEmil Constantinescu   gsum   = err_glb[0];
51177453f775SEmil Constantinescu   gsuma  = err_glb[1];
51187453f775SEmil Constantinescu   gsumr  = err_glb[2];
51197453f775SEmil Constantinescu   n_glb  = err_glb[3];
51207453f775SEmil Constantinescu   na_glb = err_glb[4];
51217453f775SEmil Constantinescu   nr_glb = err_glb[5];
51227453f775SEmil Constantinescu 
5123b1316ef9SEmil Constantinescu   *norm  = 0.;
5124b1316ef9SEmil Constantinescu   if (n_glb>0.) {*norm  = PetscSqrtReal(gsum  / n_glb);}
5125b1316ef9SEmil Constantinescu   *norma = 0.;
5126b1316ef9SEmil Constantinescu   if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);}
5127b1316ef9SEmil Constantinescu   *normr = 0.;
5128b1316ef9SEmil Constantinescu   if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);}
51299c6b16b5SShri Abhyankar 
51303c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
51313c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
51323c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
51339c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
51349c6b16b5SShri Abhyankar }
51359c6b16b5SShri Abhyankar 
51369c6b16b5SShri Abhyankar /*@
5137a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
51389c6b16b5SShri Abhyankar 
51399c6b16b5SShri Abhyankar    Collective on TS
51409c6b16b5SShri Abhyankar 
51414165533cSJose E. Roman    Input Parameters:
51429c6b16b5SShri Abhyankar +  ts - time stepping context
5143a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5144a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
51459c6b16b5SShri Abhyankar 
51464165533cSJose E. Roman    Output Parameters:
5147a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
51487453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5149a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
51509c6b16b5SShri Abhyankar 
51519c6b16b5SShri Abhyankar    Level: developer
51529c6b16b5SShri Abhyankar 
5153deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
51549c6b16b5SShri Abhyankar @*/
51557453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
51569c6b16b5SShri Abhyankar {
51577453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
51589c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
51597453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
51607453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
51617453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
51629c6b16b5SShri Abhyankar 
51639c6b16b5SShri Abhyankar   PetscFunctionBegin;
51649c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5165a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5166a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5167a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5168a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5169a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5170dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,4);
5171dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,5);
5172dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,6);
51733c633725SBarry Smith   PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
51749c6b16b5SShri Abhyankar 
51759566063dSJacob Faibussowitsch   PetscCall(VecGetSize(U,&N));
51769566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(U,&n));
51779566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(U,&rstart,NULL));
51789566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
51799566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
51807453f775SEmil Constantinescu 
51817453f775SEmil Constantinescu   max=0.;
51827453f775SEmil Constantinescu   maxa=0.;
51837453f775SEmil Constantinescu   maxr=0.;
51847453f775SEmil Constantinescu 
51857453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
51869c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
51879566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
51889566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
51897453f775SEmil Constantinescu 
51907453f775SEmil Constantinescu     for (i=0; i<n; i++) {
519176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
51927453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
51937453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
51947453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51957453f775SEmil Constantinescu       tol  = tola+tolr;
51967453f775SEmil Constantinescu       if (tola>0.) {
51977453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
51987453f775SEmil Constantinescu       }
51997453f775SEmil Constantinescu       if (tolr>0.) {
52007453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52017453f775SEmil Constantinescu       }
52027453f775SEmil Constantinescu       if (tol>0.) {
52037453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52047453f775SEmil Constantinescu       }
52059c6b16b5SShri Abhyankar     }
52069566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
52079566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
52089c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
52099c6b16b5SShri Abhyankar     const PetscScalar *atol;
52109566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
52117453f775SEmil Constantinescu     for (i=0; i<n; i++) {
521276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52137453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52147453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
52157453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52167453f775SEmil Constantinescu       tol  = tola+tolr;
52177453f775SEmil Constantinescu       if (tola>0.) {
52187453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52197453f775SEmil Constantinescu       }
52207453f775SEmil Constantinescu       if (tolr>0.) {
52217453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52227453f775SEmil Constantinescu       }
52237453f775SEmil Constantinescu       if (tol>0.) {
52247453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52257453f775SEmil Constantinescu       }
52269c6b16b5SShri Abhyankar     }
52279566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
52289c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
52299c6b16b5SShri Abhyankar     const PetscScalar *rtol;
52309566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
52317453f775SEmil Constantinescu 
52327453f775SEmil Constantinescu     for (i=0; i<n; i++) {
523376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52347453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52357453f775SEmil Constantinescu       tola = ts->atol;
52367453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52377453f775SEmil Constantinescu       tol  = tola+tolr;
52387453f775SEmil Constantinescu       if (tola>0.) {
52397453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52407453f775SEmil Constantinescu       }
52417453f775SEmil Constantinescu       if (tolr>0.) {
52427453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52437453f775SEmil Constantinescu       }
52447453f775SEmil Constantinescu       if (tol>0.) {
52457453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52467453f775SEmil Constantinescu       }
52479c6b16b5SShri Abhyankar     }
52489566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
52499c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
52507453f775SEmil Constantinescu 
52517453f775SEmil Constantinescu     for (i=0; i<n; i++) {
525276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52537453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52547453f775SEmil Constantinescu       tola = ts->atol;
52557453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52567453f775SEmil Constantinescu       tol  = tola+tolr;
52577453f775SEmil Constantinescu       if (tola>0.) {
52587453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52597453f775SEmil Constantinescu       }
52607453f775SEmil Constantinescu       if (tolr>0.) {
52617453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52627453f775SEmil Constantinescu       }
52637453f775SEmil Constantinescu       if (tol>0.) {
52647453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52657453f775SEmil Constantinescu       }
52669c6b16b5SShri Abhyankar     }
52679c6b16b5SShri Abhyankar   }
52689566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
52699566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
52707453f775SEmil Constantinescu   err_loc[0] = max;
52717453f775SEmil Constantinescu   err_loc[1] = maxa;
52727453f775SEmil Constantinescu   err_loc[2] = maxr;
52731c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts)));
52747453f775SEmil Constantinescu   gmax   = err_glb[0];
52757453f775SEmil Constantinescu   gmaxa  = err_glb[1];
52767453f775SEmil Constantinescu   gmaxr  = err_glb[2];
52779c6b16b5SShri Abhyankar 
52789c6b16b5SShri Abhyankar   *norm = gmax;
52797453f775SEmil Constantinescu   *norma = gmaxa;
52807453f775SEmil Constantinescu   *normr = gmaxr;
52813c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
52823c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
52833c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
52849c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
52859c6b16b5SShri Abhyankar }
52869c6b16b5SShri Abhyankar 
52871c3436cfSJed Brown /*@
52888a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
52891c3436cfSJed Brown 
52901c3436cfSJed Brown    Collective on TS
52911c3436cfSJed Brown 
52924165533cSJose E. Roman    Input Parameters:
52931c3436cfSJed Brown +  ts - time stepping context
5294a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5295a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5296a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
52977619abb3SShri 
52984165533cSJose E. Roman    Output Parameters:
5299a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
53008a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5301a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5302a4868fbcSLisandro Dalcin 
5303a4868fbcSLisandro Dalcin    Options Database Keys:
5304a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5305a4868fbcSLisandro Dalcin 
53061c3436cfSJed Brown    Level: developer
53071c3436cfSJed Brown 
53088a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
53091c3436cfSJed Brown @*/
53107453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53111c3436cfSJed Brown {
53121c3436cfSJed Brown   PetscFunctionBegin;
5313a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
53149566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr));
5315a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
53169566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr));
531798921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
53181c3436cfSJed Brown   PetscFunctionReturn(0);
53191c3436cfSJed Brown }
53201c3436cfSJed Brown 
53218a175baeSEmil Constantinescu /*@
53228a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
53238a175baeSEmil Constantinescu 
53248a175baeSEmil Constantinescu    Collective on TS
53258a175baeSEmil Constantinescu 
53264165533cSJose E. Roman    Input Parameters:
53278a175baeSEmil Constantinescu +  ts - time stepping context
53288a175baeSEmil Constantinescu .  E - error vector
53298a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
53308a175baeSEmil Constantinescu -  Y - state vector, previous time step
53318a175baeSEmil Constantinescu 
53324165533cSJose E. Roman    Output Parameters:
5333a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
53348a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5335a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
53368a175baeSEmil Constantinescu 
53378a175baeSEmil Constantinescu    Level: developer
53388a175baeSEmil Constantinescu 
53398a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
53408a175baeSEmil Constantinescu @*/
53418a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53428a175baeSEmil Constantinescu {
53438a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
53448a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
53458a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
53468a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
53478a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
53488a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
53498a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
53508a175baeSEmil Constantinescu 
53518a175baeSEmil Constantinescu   PetscFunctionBegin;
53528a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53538a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
53548a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
53558a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
53568a175baeSEmil Constantinescu   PetscValidType(E,2);
53578a175baeSEmil Constantinescu   PetscValidType(U,3);
53588a175baeSEmil Constantinescu   PetscValidType(Y,4);
53598a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
5360064a246eSJacob Faibussowitsch   PetscCheckSameComm(U,3,Y,4);
5361dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,5);
5362dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,6);
5363dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,7);
53648a175baeSEmil Constantinescu 
53659566063dSJacob Faibussowitsch   PetscCall(VecGetSize(E,&N));
53669566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(E,&n));
53679566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(E,&rstart,NULL));
53689566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(E,&e));
53699566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
53709566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
53718a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
53728a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
53738a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
53748a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
53758a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
53769566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
53779566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
53788a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
537976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
53808a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
53818a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
53828a175baeSEmil Constantinescu       if (tola>0.) {
53838a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
53848a175baeSEmil Constantinescu         na_loc++;
53858a175baeSEmil Constantinescu       }
53868a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53878a175baeSEmil Constantinescu       if (tolr>0.) {
53888a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
53898a175baeSEmil Constantinescu         nr_loc++;
53908a175baeSEmil Constantinescu       }
53918a175baeSEmil Constantinescu       tol=tola+tolr;
53928a175baeSEmil Constantinescu       if (tol>0.) {
53938a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
53948a175baeSEmil Constantinescu         n_loc++;
53958a175baeSEmil Constantinescu       }
53968a175baeSEmil Constantinescu     }
53979566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
53989566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
53998a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54008a175baeSEmil Constantinescu     const PetscScalar *atol;
54019566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
54028a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
540376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54048a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54058a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
54068a175baeSEmil Constantinescu       if (tola>0.) {
54078a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54088a175baeSEmil Constantinescu         na_loc++;
54098a175baeSEmil Constantinescu       }
54108a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54118a175baeSEmil Constantinescu       if (tolr>0.) {
54128a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54138a175baeSEmil Constantinescu         nr_loc++;
54148a175baeSEmil Constantinescu       }
54158a175baeSEmil Constantinescu       tol=tola+tolr;
54168a175baeSEmil Constantinescu       if (tol>0.) {
54178a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54188a175baeSEmil Constantinescu         n_loc++;
54198a175baeSEmil Constantinescu       }
54208a175baeSEmil Constantinescu     }
54219566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
54228a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54238a175baeSEmil Constantinescu     const PetscScalar *rtol;
54249566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
54258a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
542676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54278a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54288a175baeSEmil Constantinescu       tola = ts->atol;
54298a175baeSEmil Constantinescu       if (tola>0.) {
54308a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54318a175baeSEmil Constantinescu         na_loc++;
54328a175baeSEmil Constantinescu       }
54338a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54348a175baeSEmil Constantinescu       if (tolr>0.) {
54358a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54368a175baeSEmil Constantinescu         nr_loc++;
54378a175baeSEmil Constantinescu       }
54388a175baeSEmil Constantinescu       tol=tola+tolr;
54398a175baeSEmil Constantinescu       if (tol>0.) {
54408a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54418a175baeSEmil Constantinescu         n_loc++;
54428a175baeSEmil Constantinescu       }
54438a175baeSEmil Constantinescu     }
54449566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
54458a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
54468a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
544776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54488a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54498a175baeSEmil Constantinescu       tola = ts->atol;
54508a175baeSEmil Constantinescu       if (tola>0.) {
54518a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54528a175baeSEmil Constantinescu         na_loc++;
54538a175baeSEmil Constantinescu       }
54548a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54558a175baeSEmil Constantinescu       if (tolr>0.) {
54568a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54578a175baeSEmil Constantinescu         nr_loc++;
54588a175baeSEmil Constantinescu       }
54598a175baeSEmil Constantinescu       tol=tola+tolr;
54608a175baeSEmil Constantinescu       if (tol>0.) {
54618a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54628a175baeSEmil Constantinescu         n_loc++;
54638a175baeSEmil Constantinescu       }
54648a175baeSEmil Constantinescu     }
54658a175baeSEmil Constantinescu   }
54669566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(E,&e));
54679566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
54689566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
54698a175baeSEmil Constantinescu 
54708a175baeSEmil Constantinescu   err_loc[0] = sum;
54718a175baeSEmil Constantinescu   err_loc[1] = suma;
54728a175baeSEmil Constantinescu   err_loc[2] = sumr;
54738a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
54748a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
54758a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
54768a175baeSEmil Constantinescu 
54771c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts)));
54788a175baeSEmil Constantinescu 
54798a175baeSEmil Constantinescu   gsum   = err_glb[0];
54808a175baeSEmil Constantinescu   gsuma  = err_glb[1];
54818a175baeSEmil Constantinescu   gsumr  = err_glb[2];
54828a175baeSEmil Constantinescu   n_glb  = err_glb[3];
54838a175baeSEmil Constantinescu   na_glb = err_glb[4];
54848a175baeSEmil Constantinescu   nr_glb = err_glb[5];
54858a175baeSEmil Constantinescu 
54868a175baeSEmil Constantinescu   *norm  = 0.;
54878a175baeSEmil Constantinescu   if (n_glb>0.) {*norm  = PetscSqrtReal(gsum  / n_glb);}
54888a175baeSEmil Constantinescu   *norma = 0.;
54898a175baeSEmil Constantinescu   if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);}
54908a175baeSEmil Constantinescu   *normr = 0.;
54918a175baeSEmil Constantinescu   if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);}
54928a175baeSEmil Constantinescu 
54933c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
54943c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
54953c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
54968a175baeSEmil Constantinescu   PetscFunctionReturn(0);
54978a175baeSEmil Constantinescu }
54988a175baeSEmil Constantinescu 
54998a175baeSEmil Constantinescu /*@
55008a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
55018a175baeSEmil Constantinescu    Collective on TS
55028a175baeSEmil Constantinescu 
55034165533cSJose E. Roman    Input Parameters:
55048a175baeSEmil Constantinescu +  ts - time stepping context
55058a175baeSEmil Constantinescu .  E - error vector
55068a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
55078a175baeSEmil Constantinescu -  Y - state vector, previous time step
55088a175baeSEmil Constantinescu 
55094165533cSJose E. Roman    Output Parameters:
5510a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55118a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5512a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55138a175baeSEmil Constantinescu 
55148a175baeSEmil Constantinescu    Level: developer
55158a175baeSEmil Constantinescu 
55168a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
55178a175baeSEmil Constantinescu @*/
55188a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55198a175baeSEmil Constantinescu {
55208a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
55218a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
55228a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
55238a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
55248a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
55258a175baeSEmil Constantinescu 
55268a175baeSEmil Constantinescu   PetscFunctionBegin;
55278a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55288a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
55298a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
55308a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
55318a175baeSEmil Constantinescu   PetscValidType(E,2);
55328a175baeSEmil Constantinescu   PetscValidType(U,3);
55338a175baeSEmil Constantinescu   PetscValidType(Y,4);
55348a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
5535064a246eSJacob Faibussowitsch   PetscCheckSameComm(U,3,Y,4);
5536dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,5);
5537dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,6);
5538dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,7);
55398a175baeSEmil Constantinescu 
55409566063dSJacob Faibussowitsch   PetscCall(VecGetSize(E,&N));
55419566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(E,&n));
55429566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(E,&rstart,NULL));
55439566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(E,&e));
55449566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
55459566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
55468a175baeSEmil Constantinescu 
55478a175baeSEmil Constantinescu   max=0.;
55488a175baeSEmil Constantinescu   maxa=0.;
55498a175baeSEmil Constantinescu   maxr=0.;
55508a175baeSEmil Constantinescu 
55518a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
55528a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
55539566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
55549566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
55558a175baeSEmil Constantinescu 
55568a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
555776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55588a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
55598a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
55608a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55618a175baeSEmil Constantinescu       tol  = tola+tolr;
55628a175baeSEmil Constantinescu       if (tola>0.) {
55638a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
55648a175baeSEmil Constantinescu       }
55658a175baeSEmil Constantinescu       if (tolr>0.) {
55668a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
55678a175baeSEmil Constantinescu       }
55688a175baeSEmil Constantinescu       if (tol>0.) {
55698a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
55708a175baeSEmil Constantinescu       }
55718a175baeSEmil Constantinescu     }
55729566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
55739566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
55748a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55758a175baeSEmil Constantinescu     const PetscScalar *atol;
55769566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
55778a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
557876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55798a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
55808a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
55818a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55828a175baeSEmil Constantinescu       tol  = tola+tolr;
55838a175baeSEmil Constantinescu       if (tola>0.) {
55848a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
55858a175baeSEmil Constantinescu       }
55868a175baeSEmil Constantinescu       if (tolr>0.) {
55878a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
55888a175baeSEmil Constantinescu       }
55898a175baeSEmil Constantinescu       if (tol>0.) {
55908a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
55918a175baeSEmil Constantinescu       }
55928a175baeSEmil Constantinescu     }
55939566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
55948a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55958a175baeSEmil Constantinescu     const PetscScalar *rtol;
55969566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
55978a175baeSEmil Constantinescu 
55988a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
559976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56008a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
56018a175baeSEmil Constantinescu       tola = ts->atol;
56028a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56038a175baeSEmil Constantinescu       tol  = tola+tolr;
56048a175baeSEmil Constantinescu       if (tola>0.) {
56058a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
56068a175baeSEmil Constantinescu       }
56078a175baeSEmil Constantinescu       if (tolr>0.) {
56088a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
56098a175baeSEmil Constantinescu       }
56108a175baeSEmil Constantinescu       if (tol>0.) {
56118a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
56128a175baeSEmil Constantinescu       }
56138a175baeSEmil Constantinescu     }
56149566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
56158a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
56168a175baeSEmil Constantinescu 
56178a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
561876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56198a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
56208a175baeSEmil Constantinescu       tola = ts->atol;
56218a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56228a175baeSEmil Constantinescu       tol  = tola+tolr;
56238a175baeSEmil Constantinescu       if (tola>0.) {
56248a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
56258a175baeSEmil Constantinescu       }
56268a175baeSEmil Constantinescu       if (tolr>0.) {
56278a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
56288a175baeSEmil Constantinescu       }
56298a175baeSEmil Constantinescu       if (tol>0.) {
56308a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
56318a175baeSEmil Constantinescu       }
56328a175baeSEmil Constantinescu     }
56338a175baeSEmil Constantinescu   }
56349566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(E,&e));
56359566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
56369566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
56378a175baeSEmil Constantinescu   err_loc[0] = max;
56388a175baeSEmil Constantinescu   err_loc[1] = maxa;
56398a175baeSEmil Constantinescu   err_loc[2] = maxr;
56401c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts)));
56418a175baeSEmil Constantinescu   gmax   = err_glb[0];
56428a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
56438a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
56448a175baeSEmil Constantinescu 
56458a175baeSEmil Constantinescu   *norm = gmax;
56468a175baeSEmil Constantinescu   *norma = gmaxa;
56478a175baeSEmil Constantinescu   *normr = gmaxr;
56483c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56493c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56503c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56518a175baeSEmil Constantinescu   PetscFunctionReturn(0);
56528a175baeSEmil Constantinescu }
56538a175baeSEmil Constantinescu 
56548a175baeSEmil Constantinescu /*@
56558a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
56568a175baeSEmil Constantinescu 
56578a175baeSEmil Constantinescu    Collective on TS
56588a175baeSEmil Constantinescu 
56594165533cSJose E. Roman    Input Parameters:
56608a175baeSEmil Constantinescu +  ts - time stepping context
56618a175baeSEmil Constantinescu .  E - error vector
56628a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
56638a175baeSEmil Constantinescu .  Y - state vector, previous time step
56648a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
56658a175baeSEmil Constantinescu 
56664165533cSJose E. Roman    Output Parameters:
5667a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
56688a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5669a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
56708a175baeSEmil Constantinescu 
56718a175baeSEmil Constantinescu    Options Database Keys:
56728a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
56738a175baeSEmil Constantinescu 
56748a175baeSEmil Constantinescu    Level: developer
56758a175baeSEmil Constantinescu 
56768a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
56778a175baeSEmil Constantinescu @*/
56788a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56798a175baeSEmil Constantinescu {
56808a175baeSEmil Constantinescu   PetscFunctionBegin;
56818a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
56829566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr));
56838a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
56849566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr));
568598921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
56868a175baeSEmil Constantinescu   PetscFunctionReturn(0);
56878a175baeSEmil Constantinescu }
56888a175baeSEmil Constantinescu 
56898d59e960SJed Brown /*@
56908d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
56918d59e960SJed Brown 
56928d59e960SJed Brown    Logically Collective on TS
56938d59e960SJed Brown 
56944165533cSJose E. Roman    Input Parameters:
56958d59e960SJed Brown +  ts - time stepping context
56968d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
56978d59e960SJed Brown 
56988d59e960SJed Brown    Note:
56998d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
57008d59e960SJed Brown 
57018d59e960SJed Brown    Level: intermediate
57028d59e960SJed Brown 
57038d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
57048d59e960SJed Brown @*/
57058d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
57068d59e960SJed Brown {
57078d59e960SJed Brown   PetscFunctionBegin;
57088d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57098d59e960SJed Brown   ts->cfltime_local = cfltime;
57108d59e960SJed Brown   ts->cfltime       = -1.;
57118d59e960SJed Brown   PetscFunctionReturn(0);
57128d59e960SJed Brown }
57138d59e960SJed Brown 
57148d59e960SJed Brown /*@
57158d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
57168d59e960SJed Brown 
57178d59e960SJed Brown    Collective on TS
57188d59e960SJed Brown 
57194165533cSJose E. Roman    Input Parameter:
57208d59e960SJed Brown .  ts - time stepping context
57218d59e960SJed Brown 
57224165533cSJose E. Roman    Output Parameter:
57238d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
57248d59e960SJed Brown 
57258d59e960SJed Brown    Level: advanced
57268d59e960SJed Brown 
57278d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
57288d59e960SJed Brown @*/
57298d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
57308d59e960SJed Brown {
57318d59e960SJed Brown   PetscFunctionBegin;
57328d59e960SJed Brown   if (ts->cfltime < 0) {
57331c2dc1cbSBarry Smith     PetscCall(MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts)));
57348d59e960SJed Brown   }
57358d59e960SJed Brown   *cfltime = ts->cfltime;
57368d59e960SJed Brown   PetscFunctionReturn(0);
57378d59e960SJed Brown }
57388d59e960SJed Brown 
5739d6ebe24aSShri Abhyankar /*@
5740d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5741d6ebe24aSShri Abhyankar 
5742d6ebe24aSShri Abhyankar    Input Parameters:
5743a2b725a8SWilliam Gropp +  ts   - the TS context.
5744d6ebe24aSShri Abhyankar .  xl   - lower bound.
5745a2b725a8SWilliam Gropp -  xu   - upper bound.
5746d6ebe24aSShri Abhyankar 
5747d6ebe24aSShri Abhyankar    Notes:
5748d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5749e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5750d6ebe24aSShri Abhyankar 
57512bd2b0e6SSatish Balay    Level: advanced
57522bd2b0e6SSatish Balay 
5753d6ebe24aSShri Abhyankar @*/
5754d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5755d6ebe24aSShri Abhyankar {
5756d6ebe24aSShri Abhyankar   SNES           snes;
5757d6ebe24aSShri Abhyankar 
5758d6ebe24aSShri Abhyankar   PetscFunctionBegin;
57599566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
57609566063dSJacob Faibussowitsch   PetscCall(SNESVISetVariableBounds(snes,xl,xu));
5761d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5762d6ebe24aSShri Abhyankar }
5763d6ebe24aSShri Abhyankar 
5764f9c1d6abSBarry Smith /*@
5765f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5766f9c1d6abSBarry Smith 
5767d083f849SBarry Smith    Collective on TS
5768f9c1d6abSBarry Smith 
5769f9c1d6abSBarry Smith    Input Parameters:
5770f9c1d6abSBarry Smith +  ts - the TS context
5771f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5772f9c1d6abSBarry Smith 
5773f9c1d6abSBarry Smith    Output Parameters:
5774f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5775f9c1d6abSBarry Smith 
5776f9c1d6abSBarry Smith    Level: developer
5777f9c1d6abSBarry Smith 
5778f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5779f9c1d6abSBarry Smith @*/
5780f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5781f9c1d6abSBarry Smith {
5782f9c1d6abSBarry Smith   PetscFunctionBegin;
5783f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57843c633725SBarry Smith   PetscCheck(ts->ops->linearstability,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
57859566063dSJacob Faibussowitsch   PetscCall((*ts->ops->linearstability)(ts,xr,xi,yr,yi));
5786f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5787f9c1d6abSBarry Smith }
578824655328SShri 
578924655328SShri /*@
5790dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
5791dcb233daSLisandro Dalcin 
5792dcb233daSLisandro Dalcin    Collective on TS
5793dcb233daSLisandro Dalcin 
5794dcb233daSLisandro Dalcin    Input Parameter:
5795dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
5796dcb233daSLisandro Dalcin 
5797dcb233daSLisandro Dalcin    Level: advanced
5798dcb233daSLisandro Dalcin 
5799dcb233daSLisandro Dalcin    Notes:
5800dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
5801dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
5802dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
5803dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
5804dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
5805dcb233daSLisandro Dalcin    discontinuous source terms).
5806dcb233daSLisandro Dalcin 
5807dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
5808dcb233daSLisandro Dalcin @*/
5809dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
5810dcb233daSLisandro Dalcin {
5811dcb233daSLisandro Dalcin   PetscFunctionBegin;
5812dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5813dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
5814dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
5815dcb233daSLisandro Dalcin }
5816dcb233daSLisandro Dalcin 
5817dcb233daSLisandro Dalcin /*@
581824655328SShri    TSRollBack - Rolls back one time step
581924655328SShri 
582024655328SShri    Collective on TS
582124655328SShri 
582224655328SShri    Input Parameter:
582324655328SShri .  ts - the TS context obtained from TSCreate()
582424655328SShri 
582524655328SShri    Level: advanced
582624655328SShri 
582724655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
582824655328SShri @*/
582924655328SShri PetscErrorCode  TSRollBack(TS ts)
583024655328SShri {
583124655328SShri   PetscFunctionBegin;
583224655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
58333c633725SBarry Smith   PetscCheck(!ts->steprollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
58343c633725SBarry Smith   PetscCheck(ts->ops->rollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
58359566063dSJacob Faibussowitsch   PetscCall((*ts->ops->rollback)(ts));
583624655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
583724655328SShri   ts->ptime = ts->ptime_prev;
5838be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
58392808aa04SLisandro Dalcin   ts->steps--;
5840b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
584124655328SShri   PetscFunctionReturn(0);
584224655328SShri }
5843aeb4809dSShri Abhyankar 
5844ff22ae23SHong Zhang /*@
5845ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5846ff22ae23SHong Zhang 
5847ff22ae23SHong Zhang    Input Parameter:
5848ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5849ff22ae23SHong Zhang 
58500429704eSStefano Zampini    Output Parameters:
58510429704eSStefano Zampini +  ns - the number of stages
58520429704eSStefano Zampini -  Y - the current stage vectors
58530429704eSStefano Zampini 
5854ff22ae23SHong Zhang    Level: advanced
5855ff22ae23SHong Zhang 
58560429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
58570429704eSStefano Zampini 
5858ff22ae23SHong Zhang .seealso: TSCreate()
5859ff22ae23SHong Zhang @*/
5860ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
5861ff22ae23SHong Zhang {
5862ff22ae23SHong Zhang   PetscFunctionBegin;
5863ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5864dadcf809SJacob Faibussowitsch   if (ns) PetscValidIntPointer(ns,2);
58650429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
58660429704eSStefano Zampini   if (!ts->ops->getstages) {
58670429704eSStefano Zampini     if (ns) *ns = 0;
58680429704eSStefano Zampini     if (Y) *Y = NULL;
58690429704eSStefano Zampini   } else {
58709566063dSJacob Faibussowitsch     PetscCall((*ts->ops->getstages)(ts,ns,Y));
5871ff22ae23SHong Zhang   }
5872ff22ae23SHong Zhang   PetscFunctionReturn(0);
5873ff22ae23SHong Zhang }
5874ff22ae23SHong Zhang 
5875847ff0e1SMatthew G. Knepley /*@C
5876847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
5877847ff0e1SMatthew G. Knepley 
5878847ff0e1SMatthew G. Knepley   Collective on SNES
5879847ff0e1SMatthew G. Knepley 
5880847ff0e1SMatthew G. Knepley   Input Parameters:
5881847ff0e1SMatthew G. Knepley + ts - the TS context
5882847ff0e1SMatthew G. Knepley . t - current timestep
5883847ff0e1SMatthew G. Knepley . U - state vector
5884847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
5885847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
5886847ff0e1SMatthew G. Knepley - ctx - an optional user context
5887847ff0e1SMatthew G. Knepley 
5888847ff0e1SMatthew G. Knepley   Output Parameters:
5889847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
5890847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
5891847ff0e1SMatthew G. Knepley 
5892847ff0e1SMatthew G. Knepley   Level: intermediate
5893847ff0e1SMatthew G. Knepley 
5894847ff0e1SMatthew G. Knepley   Notes:
5895847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
5896847ff0e1SMatthew G. Knepley 
5897847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
5898847ff0e1SMatthew G. Knepley 
5899847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
5900847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
5901847ff0e1SMatthew G. Knepley 
5902847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
5903847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
5904847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
5905847ff0e1SMatthew G. Knepley 
5906847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
5907847ff0e1SMatthew G. Knepley @*/
5908847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
5909847ff0e1SMatthew G. Knepley {
5910847ff0e1SMatthew G. Knepley   SNES           snes;
5911847ff0e1SMatthew G. Knepley   MatFDColoring  color;
5912847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
5913847ff0e1SMatthew G. Knepley 
5914847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
59159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL));
59169566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color));
5917847ff0e1SMatthew G. Knepley   if (!color) {
5918847ff0e1SMatthew G. Knepley     DM         dm;
5919847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
5920847ff0e1SMatthew G. Knepley 
59219566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &dm));
59229566063dSJacob Faibussowitsch     PetscCall(DMHasColoring(dm, &hascolor));
5923847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
59249566063dSJacob Faibussowitsch       PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring));
59259566063dSJacob Faibussowitsch       PetscCall(MatFDColoringCreate(B, iscoloring, &color));
59269566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts));
59279566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFromOptions(color));
59289566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetUp(B, iscoloring, color));
59299566063dSJacob Faibussowitsch       PetscCall(ISColoringDestroy(&iscoloring));
5930847ff0e1SMatthew G. Knepley     } else {
5931847ff0e1SMatthew G. Knepley       MatColoring mc;
5932847ff0e1SMatthew G. Knepley 
59339566063dSJacob Faibussowitsch       PetscCall(MatColoringCreate(B, &mc));
59349566063dSJacob Faibussowitsch       PetscCall(MatColoringSetDistance(mc, 2));
59359566063dSJacob Faibussowitsch       PetscCall(MatColoringSetType(mc, MATCOLORINGSL));
59369566063dSJacob Faibussowitsch       PetscCall(MatColoringSetFromOptions(mc));
59379566063dSJacob Faibussowitsch       PetscCall(MatColoringApply(mc, &iscoloring));
59389566063dSJacob Faibussowitsch       PetscCall(MatColoringDestroy(&mc));
59399566063dSJacob Faibussowitsch       PetscCall(MatFDColoringCreate(B, iscoloring, &color));
59409566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts));
59419566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFromOptions(color));
59429566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetUp(B, iscoloring, color));
59439566063dSJacob Faibussowitsch       PetscCall(ISColoringDestroy(&iscoloring));
5944847ff0e1SMatthew G. Knepley     }
59459566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color));
59469566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) color));
5947847ff0e1SMatthew G. Knepley   }
59489566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
59499566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply(B, color, U, snes));
5950847ff0e1SMatthew G. Knepley   if (J != B) {
59519566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
59529566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
5953847ff0e1SMatthew G. Knepley   }
5954847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
5955847ff0e1SMatthew G. Knepley }
595693b34091SDebojyoti Ghosh 
5957cb9d8021SPierre Barbier de Reuille /*@
59586bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
5959cb9d8021SPierre Barbier de Reuille 
5960cb9d8021SPierre Barbier de Reuille     Input Parameters:
59616bc98fa9SBarry Smith +    ts - the TS context
59626bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
59636bc98fa9SBarry Smith 
59646bc98fa9SBarry Smith     Calling sequence of func:
59656bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
59666bc98fa9SBarry Smith 
59676bc98fa9SBarry Smith +   ts - the TS context
59686bc98fa9SBarry Smith .   time - the current time (of the stage)
59696bc98fa9SBarry Smith .   state - the state to check if it is valid
59706bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
5971cb9d8021SPierre Barbier de Reuille 
5972cb9d8021SPierre Barbier de Reuille     Level: intermediate
5973cb9d8021SPierre Barbier de Reuille 
59746bc98fa9SBarry Smith     Notes:
59756bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
59766bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
59776bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
59786bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
59796bc98fa9SBarry Smith 
59806bc98fa9SBarry Smith     Developer Notes:
59816bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
59826bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
59836bc98fa9SBarry Smith 
59846bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
5985cb9d8021SPierre Barbier de Reuille @*/
5986cb9d8021SPierre Barbier de Reuille 
5987d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
5988cb9d8021SPierre Barbier de Reuille {
5989cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
5990cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5991cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
5992cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
5993cb9d8021SPierre Barbier de Reuille }
5994cb9d8021SPierre Barbier de Reuille 
5995cb9d8021SPierre Barbier de Reuille /*@
59966bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
5997cb9d8021SPierre Barbier de Reuille 
5998cb9d8021SPierre Barbier de Reuille     Input Parameters:
59996bc98fa9SBarry Smith +    ts - the TS context
60006bc98fa9SBarry Smith .    stagetime - time of the simulation
60016bc98fa9SBarry Smith -    Y - state vector to check.
6002cb9d8021SPierre Barbier de Reuille 
6003cb9d8021SPierre Barbier de Reuille     Output Parameter:
60046bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
6005cb9d8021SPierre Barbier de Reuille 
6006cb9d8021SPierre Barbier de Reuille     Note:
60076bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
60086bc98fa9SBarry Smith     to check if the current state is valid.
600996a0c994SBarry Smith 
60106bc98fa9SBarry Smith     Level: developer
60116bc98fa9SBarry Smith 
60126bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
6013cb9d8021SPierre Barbier de Reuille @*/
6014d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
6015cb9d8021SPierre Barbier de Reuille {
6016cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6017cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6018cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
6019cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
6020a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->functiondomainerror),ts,stagetime,Y,accept);
6021cb9d8021SPierre Barbier de Reuille   }
6022cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6023cb9d8021SPierre Barbier de Reuille }
60241ceb14c0SBarry Smith 
602593b34091SDebojyoti Ghosh /*@C
6026e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
602793b34091SDebojyoti Ghosh 
6028d083f849SBarry Smith   Collective
602993b34091SDebojyoti Ghosh 
603093b34091SDebojyoti Ghosh   Input Parameter:
603193b34091SDebojyoti Ghosh . tsin    - The input TS
603293b34091SDebojyoti Ghosh 
603393b34091SDebojyoti Ghosh   Output Parameter:
6034e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
603593b34091SDebojyoti Ghosh 
60365eca1a21SEmil Constantinescu   Notes:
60375eca1a21SEmil Constantinescu   This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly.
60385eca1a21SEmil Constantinescu 
6039928bb9adSStefano Zampini   When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); TSSetSNES(ts,snes_dup);
60405eca1a21SEmil Constantinescu 
60415eca1a21SEmil Constantinescu   Level: developer
604293b34091SDebojyoti Ghosh 
6043e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
604493b34091SDebojyoti Ghosh @*/
6045baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
604693b34091SDebojyoti Ghosh {
604793b34091SDebojyoti Ghosh   TS             t;
6048dc846ba4SSatish Balay   SNES           snes_start;
6049dc846ba4SSatish Balay   DM             dm;
6050dc846ba4SSatish Balay   TSType         type;
605193b34091SDebojyoti Ghosh 
605293b34091SDebojyoti Ghosh   PetscFunctionBegin;
605393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
605493b34091SDebojyoti Ghosh   *tsout = NULL;
605593b34091SDebojyoti Ghosh 
60569566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView));
605793b34091SDebojyoti Ghosh 
605893b34091SDebojyoti Ghosh   /* General TS description */
605993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
6060d0c080abSJoseph Pusztay   t->monitorFrequency  = 1;
606193b34091SDebojyoti Ghosh   t->setupcalled       = 0;
606293b34091SDebojyoti Ghosh   t->ksp_its           = 0;
606393b34091SDebojyoti Ghosh   t->snes_its          = 0;
606493b34091SDebojyoti Ghosh   t->nwork             = 0;
60657d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
606693b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
606793b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
606893b34091SDebojyoti Ghosh 
60699566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(tsin,&snes_start));
60709566063dSJacob Faibussowitsch   PetscCall(TSSetSNES(t,snes_start));
6071d15a3a53SEmil Constantinescu 
60729566063dSJacob Faibussowitsch   PetscCall(TSGetDM(tsin,&dm));
60739566063dSJacob Faibussowitsch   PetscCall(TSSetDM(t,dm));
607493b34091SDebojyoti Ghosh 
607593b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
60769566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)t->adapt));
607793b34091SDebojyoti Ghosh 
6078e7069c78SShri   t->trajectory = tsin->trajectory;
60799566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)t->trajectory));
6080e7069c78SShri 
6081e7069c78SShri   t->event = tsin->event;
60826b10a48eSSatish Balay   if (t->event) t->event->refct++;
6083e7069c78SShri 
608493b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
608593b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
6086e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
608793b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
608893b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
608993b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
609093b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
609193b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
609293b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
609393b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
609493b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
609593b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
609693b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
609793b34091SDebojyoti Ghosh 
60989566063dSJacob Faibussowitsch   PetscCall(TSGetType(tsin,&type));
60999566063dSJacob Faibussowitsch   PetscCall(TSSetType(t,type));
610093b34091SDebojyoti Ghosh 
610193b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
610293b34091SDebojyoti Ghosh 
610393b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
610493b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
610593b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
610693b34091SDebojyoti Ghosh 
61079566063dSJacob Faibussowitsch   PetscCall(PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps)));
610893b34091SDebojyoti Ghosh 
61090d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
61100d4fed19SBarry Smith     PetscInt i;
61119566063dSJacob Faibussowitsch     PetscCall(PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers));
61120d4fed19SBarry Smith     for (i=0; i<10; i++) {
61130d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
61140d4fed19SBarry Smith     }
61150d4fed19SBarry Smith   }
611693b34091SDebojyoti Ghosh   *tsout = t;
611793b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
611893b34091SDebojyoti Ghosh }
6119f3b1f45cSBarry Smith 
6120f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
6121f3b1f45cSBarry Smith {
6122f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
6123f3b1f45cSBarry Smith 
6124f3b1f45cSBarry Smith   PetscFunctionBegin;
61259566063dSJacob Faibussowitsch   PetscCall(TSComputeRHSFunction(ts,0,x,y));
6126f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6127f3b1f45cSBarry Smith }
6128f3b1f45cSBarry Smith 
6129f3b1f45cSBarry Smith /*@
6130f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
6131f3b1f45cSBarry Smith 
6132d083f849SBarry Smith    Logically Collective on TS
6133f3b1f45cSBarry Smith 
6134f3b1f45cSBarry Smith     Input Parameters:
6135f3b1f45cSBarry Smith     TS - the time stepping routine
6136f3b1f45cSBarry Smith 
6137f3b1f45cSBarry Smith    Output Parameter:
6138f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
6139f3b1f45cSBarry Smith 
6140f3b1f45cSBarry Smith    Options Database:
6141f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
6142f3b1f45cSBarry Smith 
6143f3b1f45cSBarry Smith    Level: advanced
6144f3b1f45cSBarry Smith 
614595452b02SPatrick Sanan    Notes:
614695452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
6147f3b1f45cSBarry Smith 
6148f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
6149f3b1f45cSBarry Smith @*/
6150f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
6151f3b1f45cSBarry Smith {
6152f3b1f45cSBarry Smith   Mat            J,B;
6153f3b1f45cSBarry Smith   TSRHSJacobian  func;
6154f3b1f45cSBarry Smith   void*          ctx;
6155f3b1f45cSBarry Smith 
6156f3b1f45cSBarry Smith   PetscFunctionBegin;
61579566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx));
61589566063dSJacob Faibussowitsch   PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx));
61599566063dSJacob Faibussowitsch   PetscCall(MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg));
6160f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6161f3b1f45cSBarry Smith }
6162f3b1f45cSBarry Smith 
6163f3b1f45cSBarry Smith /*@C
6164f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
6165f3b1f45cSBarry Smith 
6166d083f849SBarry Smith    Logically Collective on TS
6167f3b1f45cSBarry Smith 
6168f3b1f45cSBarry Smith     Input Parameters:
6169f3b1f45cSBarry Smith     TS - the time stepping routine
6170f3b1f45cSBarry Smith 
6171f3b1f45cSBarry Smith    Output Parameter:
6172f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
6173f3b1f45cSBarry Smith 
6174f3b1f45cSBarry Smith    Options Database:
6175f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
6176f3b1f45cSBarry Smith 
617795452b02SPatrick Sanan    Notes:
617895452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
6179f3b1f45cSBarry Smith 
6180f3b1f45cSBarry Smith    Level: advanced
6181f3b1f45cSBarry Smith 
6182f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
6183f3b1f45cSBarry Smith @*/
6184f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
6185f3b1f45cSBarry Smith {
6186f3b1f45cSBarry Smith   Mat            J,B;
6187f3b1f45cSBarry Smith   void           *ctx;
6188f3b1f45cSBarry Smith   TSRHSJacobian  func;
6189f3b1f45cSBarry Smith 
6190f3b1f45cSBarry Smith   PetscFunctionBegin;
61919566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx));
61929566063dSJacob Faibussowitsch   PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx));
61939566063dSJacob Faibussowitsch   PetscCall(MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg));
6194f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6195f3b1f45cSBarry Smith }
61960fe4d17eSHong Zhang 
61970fe4d17eSHong Zhang /*@
61980fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
61990fe4d17eSHong Zhang 
62000fe4d17eSHong Zhang   Logically collective
62010fe4d17eSHong Zhang 
6202d8d19677SJose E. Roman   Input Parameters:
62030fe4d17eSHong Zhang +  ts - timestepping context
62040fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
62050fe4d17eSHong Zhang 
62060fe4d17eSHong Zhang   Options Database:
62070fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
62080fe4d17eSHong Zhang 
62090fe4d17eSHong Zhang   Notes:
62100fe4d17eSHong Zhang     This is only useful for multirate methods
62110fe4d17eSHong Zhang 
62120fe4d17eSHong Zhang   Level: intermediate
62130fe4d17eSHong Zhang 
62140fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
62150fe4d17eSHong Zhang @*/
62160fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
62170fe4d17eSHong Zhang {
62180fe4d17eSHong Zhang   PetscFunctionBegin;
62190fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62200fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
62210fe4d17eSHong Zhang   PetscFunctionReturn(0);
62220fe4d17eSHong Zhang }
62230fe4d17eSHong Zhang 
62240fe4d17eSHong Zhang /*@
62250fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
62260fe4d17eSHong Zhang 
62270fe4d17eSHong Zhang   Not collective
62280fe4d17eSHong Zhang 
62290fe4d17eSHong Zhang   Input Parameter:
62300fe4d17eSHong Zhang .  ts - timestepping context
62310fe4d17eSHong Zhang 
62320fe4d17eSHong Zhang   Output Parameter:
62330fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
62340fe4d17eSHong Zhang 
62350fe4d17eSHong Zhang   Level: intermediate
62360fe4d17eSHong Zhang 
62370fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
62380fe4d17eSHong Zhang @*/
62390fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
62400fe4d17eSHong Zhang {
62410fe4d17eSHong Zhang   PetscFunctionBegin;
62420fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62430fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
62440fe4d17eSHong Zhang   PetscFunctionReturn(0);
62450fe4d17eSHong Zhang }
6246d60b7d5cSBarry Smith 
6247d60b7d5cSBarry Smith /*@
6248d60b7d5cSBarry Smith     TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix.
6249d60b7d5cSBarry Smith 
6250d60b7d5cSBarry Smith    Logically  Collective on ts
6251d60b7d5cSBarry Smith 
6252d60b7d5cSBarry Smith    Input Parameters:
6253d60b7d5cSBarry Smith +  ts - the time-stepper
6254d60b7d5cSBarry Smith -  str - the structure (the default is UNKNOWN_NONZERO_PATTERN)
6255d60b7d5cSBarry Smith 
6256d60b7d5cSBarry Smith    Level: intermediate
6257d60b7d5cSBarry Smith 
6258d60b7d5cSBarry Smith    Notes:
6259d60b7d5cSBarry Smith      When the relationship between the nonzero structures is known and supplied the solution process can be much faster
6260d60b7d5cSBarry Smith 
6261d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure
6262d60b7d5cSBarry Smith  @*/
6263d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str)
6264d60b7d5cSBarry Smith {
6265d60b7d5cSBarry Smith   PetscFunctionBegin;
6266d60b7d5cSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6267d60b7d5cSBarry Smith   ts->axpy_pattern = str;
6268d60b7d5cSBarry Smith   PetscFunctionReturn(0);
6269d60b7d5cSBarry Smith }
62704a658b32SHong Zhang 
62714a658b32SHong Zhang /*@
62724a658b32SHong Zhang   TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested.
62734a658b32SHong Zhang 
62744a658b32SHong Zhang   Collective on ts
62754a658b32SHong Zhang 
62764a658b32SHong Zhang   Input Parameters:
62774a658b32SHong Zhang + ts - the time-stepper
62784a658b32SHong Zhang . n - number of the time points (>=2)
62794a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively.
62804a658b32SHong Zhang 
62814a658b32SHong Zhang   Options Database Keys:
62824a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span
62834a658b32SHong Zhang 
62844a658b32SHong Zhang   Level: beginner
62854a658b32SHong Zhang 
62864a658b32SHong Zhang   Notes:
62874a658b32SHong Zhang   The elements in tspan must be all increasing. They correspond to the intermediate points for time integration.
62884a658b32SHong Zhang   TS_EXACTFINALTIME_MATCHSTEP must be used to make the last time step in each sub-interval match the intermediate points specified.
62894a658b32SHong Zhang   The intermediate solutions are saved in a vector array that can be accessed with TSGetSolutions(). Thus using time span may
62904a658b32SHong Zhang   pressure the memory system when using a large number of span points.
62914a658b32SHong Zhang 
62924a658b32SHong Zhang .seealso: TSGetTimeSpan(),TSGetSolutions()
62934a658b32SHong Zhang  @*/
62944a658b32SHong Zhang PetscErrorCode TSSetTimeSpan(TS ts,PetscInt n,PetscReal *span_times)
62954a658b32SHong Zhang {
62964a658b32SHong Zhang   PetscFunctionBegin;
62974a658b32SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62984a658b32SHong Zhang   PetscCheck(n >= 2,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Minimum time span size is 2 but %D is provided",n);
62994a658b32SHong Zhang   if (ts->tspan && n != ts->tspan->num_span_times) {
63004a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan->span_times));
63014a658b32SHong Zhang     PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol));
63024a658b32SHong Zhang     PetscCall(PetscMalloc1(n,&ts->tspan->span_times));
63034a658b32SHong Zhang   }
63044a658b32SHong Zhang   if (!ts->tspan) {
63054a658b32SHong Zhang     TSTimeSpan tspan;
63064a658b32SHong Zhang     PetscCall(PetscNew(&tspan));
63074a658b32SHong Zhang     PetscCall(PetscMalloc1(n,&tspan->span_times));
63084a658b32SHong Zhang     ts->tspan = tspan;
63094a658b32SHong Zhang   }
63104a658b32SHong Zhang   ts->tspan->num_span_times = n;
63114a658b32SHong Zhang   PetscCall(PetscArraycpy(ts->tspan->span_times,span_times,n));
63124a658b32SHong Zhang   PetscCall(TSSetTime(ts,ts->tspan->span_times[0]));
63134a658b32SHong Zhang   PetscCall(TSSetMaxTime(ts,ts->tspan->span_times[n-1]));
63144a658b32SHong Zhang   PetscFunctionReturn(0);
63154a658b32SHong Zhang }
63164a658b32SHong Zhang 
63174a658b32SHong Zhang /*@C
63184a658b32SHong Zhang   TSGetTimeSpan - gets the time span.
63194a658b32SHong Zhang 
63204a658b32SHong Zhang   Not Collective
63214a658b32SHong Zhang 
63224a658b32SHong Zhang   Input Parameter:
63234a658b32SHong Zhang . ts - the time-stepper
63244a658b32SHong Zhang 
63254a658b32SHong Zhang   Output Parameters:
63264a658b32SHong Zhang + n - number of the time points (>=2)
63274a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. The values are valid until the TS object is destroyed.
63284a658b32SHong Zhang 
63294a658b32SHong Zhang   Level: beginner
63304a658b32SHong Zhang   Notes: Both n and span_times can be NULL.
63314a658b32SHong Zhang 
63324a658b32SHong Zhang .seealso: TSSetTimeSpan(),TSGetSolutions()
63334a658b32SHong Zhang  @*/
63344a658b32SHong Zhang PetscErrorCode TSGetTimeSpan(TS ts,PetscInt *n,const PetscReal **span_times)
63354a658b32SHong Zhang {
63364a658b32SHong Zhang   PetscFunctionBegin;
63374a658b32SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63384a658b32SHong Zhang   if (n) PetscValidIntPointer(n,2);
63394a658b32SHong Zhang   if (span_times) PetscValidPointer(span_times,3);
63404a658b32SHong Zhang   if (!ts->tspan) {
63414a658b32SHong Zhang     if (n) *n = 0;
63424a658b32SHong Zhang     if (span_times) *span_times = NULL;
63434a658b32SHong Zhang   } else {
63444a658b32SHong Zhang     if (n) *n = ts->tspan->num_span_times;
63454a658b32SHong Zhang     if (span_times) *span_times = ts->tspan->span_times;
63464a658b32SHong Zhang   }
63474a658b32SHong Zhang   PetscFunctionReturn(0);
63484a658b32SHong Zhang }
63494a658b32SHong Zhang 
63504a658b32SHong Zhang /*@
63514a658b32SHong Zhang    TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span.
63524a658b32SHong Zhang 
63534a658b32SHong Zhang    Input Parameter:
63544a658b32SHong Zhang .  ts - the TS context obtained from TSCreate()
63554a658b32SHong Zhang 
63564a658b32SHong Zhang    Output Parameters:
63574a658b32SHong Zhang +  nsol - the number of solutions
63584a658b32SHong Zhang -  Sols - the solution vectors
63594a658b32SHong Zhang 
63604a658b32SHong Zhang    Level: beginner
63614a658b32SHong Zhang 
63624a658b32SHong Zhang    Notes: Both nsol and Sols can be NULL.
63634a658b32SHong Zhang 
63644a658b32SHong Zhang .seealso: TSSetTimeSpan()
63654a658b32SHong Zhang @*/
63664a658b32SHong Zhang PetscErrorCode TSGetTimeSpanSolutions(TS ts,PetscInt *nsol,Vec **Sols)
63674a658b32SHong Zhang {
63684a658b32SHong Zhang   PetscFunctionBegin;
63694a658b32SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
63704a658b32SHong Zhang   if (nsol) PetscValidIntPointer(nsol,2);
63714a658b32SHong Zhang   if (Sols) PetscValidPointer(Sols,3);
63724a658b32SHong Zhang   if (!ts->tspan) {
63734a658b32SHong Zhang     if (nsol) *nsol = 0;
63744a658b32SHong Zhang     if (Sols) *Sols = NULL;
63754a658b32SHong Zhang   } else {
63764a658b32SHong Zhang     if (nsol) *nsol = ts->tspan->num_span_times;
63774a658b32SHong Zhang     if (Sols) *Sols = ts->tspan->vecs_sol;
63784a658b32SHong Zhang   }
63794a658b32SHong Zhang   PetscFunctionReturn(0);
63804a658b32SHong Zhang }
6381