xref: /petsc/src/ts/interface/ts.c (revision 4a658b327b6246422d9d13d47da7726e5bf5b4d5)
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
39*4a658b32SHong 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];
90*4a658b32SHong Zhang   PetscReal              time_step,tspan[100];
91*4a658b32SHong 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));
117*4a658b32SHong Zhang   PetscCall(PetscOptionsRealArray("-ts_time_span","Time span","TSSetTimeSpan",tspan,&nt,&flg));
118*4a658b32SHong 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;
238d7462660SMatthew Knepley     PetscBool       phase = PETSC_TRUE;
239d7462660SMatthew Knepley 
2409566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL));
2419566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL));
2429566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL));
2439566063dSJacob Faibussowitsch     PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject) ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx));
2449566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy));
2451b575b74SJoseph Pusztay   }
246ef20d060SBarry Smith   opt  = PETSC_FALSE;
2479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt));
248a7cc72afSBarry Smith   if (opt) {
24983a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
25083a4ac43SBarry Smith     PetscInt         howoften = 1;
251a80ad3e0SBarry Smith 
2529566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL));
2539566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx));
2549566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
255bdad233fSMatthew Knepley   }
256fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2579566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt));
2582d5ee99bSBarry Smith   if (opt) {
2592d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2602d5ee99bSBarry Smith     PetscReal        bounds[4];
2612d5ee99bSBarry Smith     PetscInt         n = 4;
2622d5ee99bSBarry Smith     PetscDraw        draw;
2636934998bSLisandro Dalcin     PetscDrawAxis    axis;
2642d5ee99bSBarry Smith 
2659566063dSJacob Faibussowitsch     PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL));
2663c633725SBarry Smith     PetscCheck(n == 4,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2679566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx));
2689566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(ctx->viewer,0,&draw));
2699566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis));
2709566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]));
2719566063dSJacob Faibussowitsch     PetscCall(PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2"));
2729566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
2732d5ee99bSBarry Smith   }
2742d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt));
2763a471f94SBarry Smith   if (opt) {
27783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
27883a4ac43SBarry Smith     PetscInt         howoften = 1;
2793a471f94SBarry Smith 
2809566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL));
2819566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx));
2829566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
2833a471f94SBarry Smith   }
2840ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
2859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt));
2860ed3bfb6SBarry Smith   if (opt) {
2870ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
2880ed3bfb6SBarry Smith     PetscInt         howoften = 1;
2890ed3bfb6SBarry Smith 
2909566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL));
2919566063dSJacob Faibussowitsch     PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx));
2929566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy));
2930ed3bfb6SBarry Smith   }
294fde5950dSBarry Smith 
295ed81e22dSJed Brown   opt  = PETSC_FALSE;
2969566063dSJacob 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));
297ed81e22dSJed Brown   if (flg) {
298ed81e22dSJed Brown     const char *ptr,*ptr2;
299ed81e22dSJed Brown     char       *filetemplate;
3003c633725SBarry Smith     PetscCheck(monfilename[0],PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
301ed81e22dSJed Brown     /* Do some cursory validation of the input. */
3029566063dSJacob Faibussowitsch     PetscCall(PetscStrstr(monfilename,"%",(char**)&ptr));
3033c633725SBarry Smith     PetscCheck(ptr,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
304ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
3059566063dSJacob Faibussowitsch       PetscCall(PetscStrchr("DdiouxX",*ptr,(char**)&ptr2));
3063c633725SBarry 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");
307ed81e22dSJed Brown       if (ptr2) break;
308ed81e22dSJed Brown     }
3099566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(monfilename,&filetemplate));
3109566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy));
311ed81e22dSJed Brown   }
312bdad233fSMatthew Knepley 
3139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg));
314d1212d36SBarry Smith   if (flg) {
315d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
316d1212d36SBarry Smith     int                  ray = 0;
3173ee9839eSMatthew G. Knepley     DMDirection          ddir;
318d1212d36SBarry Smith     DM                   da;
319d1212d36SBarry Smith     PetscMPIInt          rank;
320d1212d36SBarry Smith 
3213c633725SBarry Smith     PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3223ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3233ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
32498921bdaSJacob Faibussowitsch     else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
325d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
326d1212d36SBarry Smith 
3279566063dSJacob Faibussowitsch     PetscCall(PetscInfo(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray));
3289566063dSJacob Faibussowitsch     PetscCall(PetscNew(&rayctx));
3299566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts,&da));
3309566063dSJacob Faibussowitsch     PetscCall(DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter));
3319566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank));
332dd400576SPatrick Sanan     if (rank == 0) {
3339566063dSJacob Faibussowitsch       PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer));
334d1212d36SBarry Smith     }
33551b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
3369566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy));
337d1212d36SBarry Smith   }
3389566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg));
33951b4a12fSMatthew G. Knepley   if (flg) {
34051b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
34151b4a12fSMatthew G. Knepley     int                 ray = 0;
3423ee9839eSMatthew G. Knepley     DMDirection         ddir;
34351b4a12fSMatthew G. Knepley     DM                  da;
34451b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
345d1212d36SBarry Smith 
3463c633725SBarry Smith     PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3473ee9839eSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DM_X;
3483ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
34998921bdaSJacob Faibussowitsch     else SETERRQ(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
35051b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3511c3436cfSJed Brown 
3529566063dSJacob Faibussowitsch     PetscCall(PetscInfo(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray));
3539566063dSJacob Faibussowitsch     PetscCall(PetscNew(&rayctx));
3549566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &da));
3559566063dSJacob Faibussowitsch     PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter));
3569566063dSJacob Faibussowitsch     PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx));
3579566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy));
35851b4a12fSMatthew G. Knepley   }
359a7a1495cSBarry Smith 
3609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt));
361b3d3934dSBarry Smith   if (opt) {
362b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
363b3d3934dSBarry Smith 
3649566063dSJacob Faibussowitsch     PetscCall(TSMonitorEnvelopeCtxCreate(ts,&ctx));
3659566063dSJacob Faibussowitsch     PetscCall(TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy));
366b3d3934dSBarry Smith   }
367aee7a9fbSMatthew G. Knepley   flg  = PETSC_FALSE;
3689566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt));
3699566063dSJacob Faibussowitsch   if (opt && flg) PetscCall(TSMonitorCancel(ts));
370b3d3934dSBarry Smith 
371847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
3729566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL));
373847ff0e1SMatthew G. Knepley   if (flg) {
374847ff0e1SMatthew G. Knepley     DM   dm;
375847ff0e1SMatthew G. Knepley     DMTS tdm;
376847ff0e1SMatthew G. Knepley 
3779566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &dm));
3789566063dSJacob Faibussowitsch     PetscCall(DMGetDMTS(dm, &tdm));
379847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
3809566063dSJacob Faibussowitsch     PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL));
3819566063dSJacob Faibussowitsch     PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n"));
382847ff0e1SMatthew G. Knepley   }
383847ff0e1SMatthew G. Knepley 
384d763cef2SBarry Smith   /* Handle specific TS options */
385d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3869566063dSJacob Faibussowitsch     PetscCall((*ts->ops->setfromoptions)(PetscOptionsObject,ts));
387d763cef2SBarry Smith   }
388fbc52257SHong Zhang 
389a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
3909566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts,&ts->adapt));
3919566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type));
3929566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt));
393a7bdc993SLisandro Dalcin 
39468bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
3954f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
3969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL));
39768bece0bSHong Zhang   if (tflg) {
3989566063dSJacob Faibussowitsch     PetscCall(TSSetSaveTrajectory(ts));
39968bece0bSHong Zhang   }
400a05bf03eSHong Zhang 
4019566063dSJacob Faibussowitsch   PetscCall(TSAdjointSetFromOptions(PetscOptionsObject,ts));
402d763cef2SBarry Smith 
403d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4049566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts));
4059566063dSJacob Faibussowitsch   ierr = PetscOptionsEnd();PetscCall(ierr);
406d763cef2SBarry Smith 
4074f122a70SLisandro Dalcin   if (ts->trajectory) {
4089566063dSJacob Faibussowitsch     PetscCall(TSTrajectorySetFromOptions(ts->trajectory,ts));
409d763cef2SBarry Smith   }
41068bece0bSHong Zhang 
4111ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4129566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&ts->snes));
4131ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4149566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,""));
4159566063dSJacob Faibussowitsch     if (!flg) PetscCall(SNESSetType(ts->snes,SNESKSPONLY));
4161ef27442SStefano Zampini   }
4179566063dSJacob Faibussowitsch   PetscCall(SNESSetFromOptions(ts->snes));
418d763cef2SBarry Smith   PetscFunctionReturn(0);
419d763cef2SBarry Smith }
420d763cef2SBarry Smith 
421d2daff3dSHong Zhang /*@
42278fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
42378fbdcc8SBarry Smith 
42478fbdcc8SBarry Smith    Collective on TS
42578fbdcc8SBarry Smith 
42678fbdcc8SBarry Smith    Input Parameters:
42778fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
42878fbdcc8SBarry Smith 
4297a7aea1fSJed Brown    Output Parameters:
43078fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
43178fbdcc8SBarry Smith 
43278fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
43378fbdcc8SBarry Smith 
43478fbdcc8SBarry Smith    Level: advanced
43578fbdcc8SBarry Smith 
43678fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
43778fbdcc8SBarry Smith 
43878fbdcc8SBarry Smith @*/
43978fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
44078fbdcc8SBarry Smith {
44178fbdcc8SBarry Smith   PetscFunctionBegin;
44278fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44378fbdcc8SBarry Smith   *tr = ts->trajectory;
44478fbdcc8SBarry Smith   PetscFunctionReturn(0);
44578fbdcc8SBarry Smith }
44678fbdcc8SBarry Smith 
44778fbdcc8SBarry Smith /*@
448bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
449d2daff3dSHong Zhang 
450d2daff3dSHong Zhang    Collective on TS
451d2daff3dSHong Zhang 
452f899ff85SJose E. Roman    Input Parameter:
453bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
454bc952696SBarry Smith 
45578fbdcc8SBarry Smith    Options Database:
45678fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
45767b8a455SSatish Balay -  -ts_trajectory_type type - set trajectory type
45878fbdcc8SBarry Smith 
45968bece0bSHong Zhang Note: This routine should be called after all TS options have been set
460d2daff3dSHong Zhang 
461c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
46278fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
46378fbdcc8SBarry Smith 
464d2daff3dSHong Zhang    Level: intermediate
465d2daff3dSHong Zhang 
4662d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
467d2daff3dSHong Zhang 
468d2daff3dSHong Zhang @*/
469bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
470d2daff3dSHong Zhang {
471d2daff3dSHong Zhang   PetscFunctionBegin;
472d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
473bc952696SBarry Smith   if (!ts->trajectory) {
4749566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory));
475bc952696SBarry Smith   }
476d2daff3dSHong Zhang   PetscFunctionReturn(0);
477d2daff3dSHong Zhang }
478d2daff3dSHong Zhang 
479a7a1495cSBarry Smith /*@
4802d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
4812d29f1f2SStefano Zampini 
4822d29f1f2SStefano Zampini    Collective on TS
4832d29f1f2SStefano Zampini 
4842d29f1f2SStefano Zampini    Input Parameters:
4852d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
4862d29f1f2SStefano Zampini 
4872d29f1f2SStefano Zampini    Level: intermediate
4882d29f1f2SStefano Zampini 
48967a3cfb0SHong Zhang .seealso: TSGetTrajectory(), TSAdjointSolve(), TSRemoveTrajectory()
4902d29f1f2SStefano Zampini 
4912d29f1f2SStefano Zampini @*/
4922d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
4932d29f1f2SStefano Zampini {
4942d29f1f2SStefano Zampini   PetscFunctionBegin;
4952d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4962d29f1f2SStefano Zampini   if (ts->trajectory) {
4979566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryDestroy(&ts->trajectory));
4989566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory));
4992d29f1f2SStefano Zampini   }
5002d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5012d29f1f2SStefano Zampini }
5022d29f1f2SStefano Zampini 
5032d29f1f2SStefano Zampini /*@
50467a3cfb0SHong Zhang    TSRemoveTrajectory - Destroys and removes the internal TSTrajectory object from TS
50567a3cfb0SHong Zhang 
50667a3cfb0SHong Zhang    Collective on TS
50767a3cfb0SHong Zhang 
50867a3cfb0SHong Zhang    Input Parameters:
50967a3cfb0SHong Zhang .  ts - the TS context obtained from TSCreate()
51067a3cfb0SHong Zhang 
51167a3cfb0SHong Zhang    Level: intermediate
51267a3cfb0SHong Zhang 
51367a3cfb0SHong Zhang .seealso: TSResetTrajectory(), TSAdjointSolve()
51467a3cfb0SHong Zhang 
51567a3cfb0SHong Zhang @*/
51667a3cfb0SHong Zhang PetscErrorCode TSRemoveTrajectory(TS ts)
51767a3cfb0SHong Zhang {
51867a3cfb0SHong Zhang   PetscFunctionBegin;
51967a3cfb0SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52067a3cfb0SHong Zhang   if (ts->trajectory) {
5219566063dSJacob Faibussowitsch     PetscCall(TSTrajectoryDestroy(&ts->trajectory));
52267a3cfb0SHong Zhang   }
52367a3cfb0SHong Zhang   PetscFunctionReturn(0);
52467a3cfb0SHong Zhang }
52567a3cfb0SHong Zhang 
52667a3cfb0SHong Zhang /*@
527a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
528a7a1495cSBarry Smith       set with TSSetRHSJacobian().
529a7a1495cSBarry Smith 
530d083f849SBarry Smith    Collective on TS
531a7a1495cSBarry Smith 
532a7a1495cSBarry Smith    Input Parameters:
533316643e7SJed Brown +  ts - the TS context
534a7a1495cSBarry Smith .  t - current timestep
5350910c330SBarry Smith -  U - input vector
536a7a1495cSBarry Smith 
537a7a1495cSBarry Smith    Output Parameters:
538a7a1495cSBarry Smith +  A - Jacobian matrix
5396b867d5aSJose E. Roman -  B - optional preconditioning matrix
540a7a1495cSBarry Smith 
541a7a1495cSBarry Smith    Notes:
542a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
543a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
544a7a1495cSBarry Smith 
545a7a1495cSBarry Smith    Level: developer
546a7a1495cSBarry Smith 
54794b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
548a7a1495cSBarry Smith @*/
549d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
550a7a1495cSBarry Smith {
551270bf2e7SJed Brown   PetscObjectState Ustate;
5526c1e1eecSBarry Smith   PetscObjectId    Uid;
55324989b8cSPeter Brune   DM               dm;
554942e3340SBarry Smith   DMTS             tsdm;
55524989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
55624989b8cSPeter Brune   void             *ctx;
557b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
558a7a1495cSBarry Smith 
559a7a1495cSBarry Smith   PetscFunctionBegin;
5600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5610910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5620910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
5639566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
5649566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
5659566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
5669566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx));
5679566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate));
5689566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetId((PetscObject)U,&Uid));
569971015bcSStefano Zampini 
5702663174eSHong Zhang   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0);
571d90be118SSean Farley 
5723c633725SBarry 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);
57324989b8cSPeter Brune   if (rhsjacobianfunc) {
5749566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B));
575a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
5769566063dSJacob Faibussowitsch     PetscCall((*rhsjacobianfunc)(ts,t,U,A,B,ctx));
577a7a1495cSBarry Smith     PetscStackPop;
578a6ab3590SBarry Smith     ts->rhsjacs++;
5799566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B));
580ef66eb69SBarry Smith   } else {
5819566063dSJacob Faibussowitsch     PetscCall(MatZeroEntries(A));
5829566063dSJacob Faibussowitsch     if (B && A != B) PetscCall(MatZeroEntries(B));
583ef66eb69SBarry Smith   }
5840e4ef248SJed Brown   ts->rhsjacobian.time  = t;
585971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
586971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
5879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid));
5889566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate));
589a7a1495cSBarry Smith   PetscFunctionReturn(0);
590a7a1495cSBarry Smith }
591a7a1495cSBarry Smith 
592316643e7SJed Brown /*@
593d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
594d763cef2SBarry Smith 
595d083f849SBarry Smith    Collective on TS
596316643e7SJed Brown 
597316643e7SJed Brown    Input Parameters:
598316643e7SJed Brown +  ts - the TS context
599316643e7SJed Brown .  t - current time
6000910c330SBarry Smith -  U - state vector
601316643e7SJed Brown 
602316643e7SJed Brown    Output Parameter:
603316643e7SJed Brown .  y - right hand side
604316643e7SJed Brown 
605316643e7SJed Brown    Note:
606316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
607316643e7SJed Brown    is used internally within the nonlinear solvers.
608316643e7SJed Brown 
609316643e7SJed Brown    Level: developer
610316643e7SJed Brown 
611316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
612316643e7SJed Brown @*/
6130910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
614d763cef2SBarry Smith {
61524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
61624989b8cSPeter Brune   TSIFunction    ifunction;
61724989b8cSPeter Brune   void           *ctx;
61824989b8cSPeter Brune   DM             dm;
61924989b8cSPeter Brune 
620d763cef2SBarry Smith   PetscFunctionBegin;
6210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6220910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6230700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
6249566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
6259566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,&ctx));
6269566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm,&ifunction,NULL));
627d763cef2SBarry Smith 
6283c633725SBarry Smith   PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
629d763cef2SBarry Smith 
63024989b8cSPeter Brune   if (rhsfunction) {
6319566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,y,0));
6329566063dSJacob Faibussowitsch     PetscCall(VecLockReadPush(U));
633d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6349566063dSJacob Faibussowitsch     PetscCall((*rhsfunction)(ts,t,U,y,ctx));
635d763cef2SBarry Smith     PetscStackPop;
6369566063dSJacob Faibussowitsch     PetscCall(VecLockReadPop(U));
637a6ab3590SBarry Smith     ts->rhsfuncs++;
6389566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,y,0));
639214bc6a2SJed Brown   } else {
6409566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(y));
641b2cd27e8SJed Brown   }
642d763cef2SBarry Smith   PetscFunctionReturn(0);
643d763cef2SBarry Smith }
644d763cef2SBarry Smith 
645ef20d060SBarry Smith /*@
646ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
647ef20d060SBarry Smith 
648d083f849SBarry Smith    Collective on TS
649ef20d060SBarry Smith 
650ef20d060SBarry Smith    Input Parameters:
651ef20d060SBarry Smith +  ts - the TS context
652ef20d060SBarry Smith -  t - current time
653ef20d060SBarry Smith 
654ef20d060SBarry Smith    Output Parameter:
6550910c330SBarry Smith .  U - the solution
656ef20d060SBarry Smith 
657ef20d060SBarry Smith    Note:
658ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
659ef20d060SBarry Smith    is used internally within the nonlinear solvers.
660ef20d060SBarry Smith 
661ef20d060SBarry Smith    Level: developer
662ef20d060SBarry Smith 
663abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
664ef20d060SBarry Smith @*/
6650910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
666ef20d060SBarry Smith {
667ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
668ef20d060SBarry Smith   void               *ctx;
669ef20d060SBarry Smith   DM                 dm;
670ef20d060SBarry Smith 
671ef20d060SBarry Smith   PetscFunctionBegin;
672ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6730910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6749566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
6759566063dSJacob Faibussowitsch   PetscCall(DMTSGetSolutionFunction(dm,&solutionfunction,&ctx));
676ef20d060SBarry Smith 
677ef20d060SBarry Smith   if (solutionfunction) {
6789b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6799566063dSJacob Faibussowitsch     PetscCall((*solutionfunction)(ts,t,U,ctx));
680ef20d060SBarry Smith     PetscStackPop;
681ef20d060SBarry Smith   }
682ef20d060SBarry Smith   PetscFunctionReturn(0);
683ef20d060SBarry Smith }
6849b7cd975SBarry Smith /*@
6859b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6869b7cd975SBarry Smith 
687d083f849SBarry Smith    Collective on TS
6889b7cd975SBarry Smith 
6899b7cd975SBarry Smith    Input Parameters:
6909b7cd975SBarry Smith +  ts - the TS context
6919b7cd975SBarry Smith -  t - current time
6929b7cd975SBarry Smith 
6939b7cd975SBarry Smith    Output Parameter:
6949b7cd975SBarry Smith .  U - the function value
6959b7cd975SBarry Smith 
6969b7cd975SBarry Smith    Note:
6979b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6989b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6999b7cd975SBarry Smith 
7009b7cd975SBarry Smith    Level: developer
7019b7cd975SBarry Smith 
7029b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7039b7cd975SBarry Smith @*/
7049b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7059b7cd975SBarry Smith {
7069b7cd975SBarry Smith   void              *ctx;
7079b7cd975SBarry Smith   DM                 dm;
7085f80ce2aSJacob Faibussowitsch   TSForcingFunction  forcing;
7099b7cd975SBarry Smith 
7109b7cd975SBarry Smith   PetscFunctionBegin;
7119b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7129b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7139566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
7149566063dSJacob Faibussowitsch   PetscCall(DMTSGetForcingFunction(dm,&forcing,&ctx));
7159b7cd975SBarry Smith 
7169b7cd975SBarry Smith   if (forcing) {
7179b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7189566063dSJacob Faibussowitsch     PetscCall((*forcing)(ts,t,U,ctx));
7199b7cd975SBarry Smith     PetscStackPop;
7209b7cd975SBarry Smith   }
7219b7cd975SBarry Smith   PetscFunctionReturn(0);
7229b7cd975SBarry Smith }
723ef20d060SBarry Smith 
724214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
725214bc6a2SJed Brown {
7262dd45cf8SJed Brown   Vec            F;
727214bc6a2SJed Brown 
728214bc6a2SJed Brown   PetscFunctionBegin;
7290298fd71SBarry Smith   *Frhs = NULL;
7309566063dSJacob Faibussowitsch   PetscCall(TSGetIFunction(ts,&F,NULL,NULL));
731214bc6a2SJed Brown   if (!ts->Frhs) {
7329566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(F,&ts->Frhs));
733214bc6a2SJed Brown   }
734214bc6a2SJed Brown   *Frhs = ts->Frhs;
735214bc6a2SJed Brown   PetscFunctionReturn(0);
736214bc6a2SJed Brown }
737214bc6a2SJed Brown 
738971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
739214bc6a2SJed Brown {
740214bc6a2SJed Brown   Mat            A,B;
74141a1d4d2SBarry Smith   TSIJacobian    ijacobian;
742214bc6a2SJed Brown 
743214bc6a2SJed Brown   PetscFunctionBegin;
744c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
745c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7469566063dSJacob Faibussowitsch   PetscCall(TSGetIJacobian(ts,&A,&B,&ijacobian,NULL));
747214bc6a2SJed Brown   if (Arhs) {
748214bc6a2SJed Brown     if (!ts->Arhs) {
74941a1d4d2SBarry Smith       if (ijacobian) {
7509566063dSJacob Faibussowitsch         PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs));
7519566063dSJacob Faibussowitsch         PetscCall(TSSetMatStructure(ts,SAME_NONZERO_PATTERN));
75241a1d4d2SBarry Smith       } else {
75341a1d4d2SBarry Smith         ts->Arhs = A;
7549566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)A));
75541a1d4d2SBarry Smith       }
7563565c898SBarry Smith     } else {
7573565c898SBarry Smith       PetscBool flg;
7589566063dSJacob Faibussowitsch       PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg));
7593565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7603565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs) {
7619566063dSJacob Faibussowitsch         PetscCall(PetscObjectDereference((PetscObject)ts->Arhs));
7623565c898SBarry Smith         ts->Arhs = A;
7639566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)A));
7643565c898SBarry Smith       }
765214bc6a2SJed Brown     }
766214bc6a2SJed Brown     *Arhs = ts->Arhs;
767214bc6a2SJed Brown   }
768214bc6a2SJed Brown   if (Brhs) {
769214bc6a2SJed Brown     if (!ts->Brhs) {
770bdb70873SJed Brown       if (A != B) {
77141a1d4d2SBarry Smith         if (ijacobian) {
7729566063dSJacob Faibussowitsch           PetscCall(MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs));
773bdb70873SJed Brown         } else {
77441a1d4d2SBarry Smith           ts->Brhs = B;
7759566063dSJacob Faibussowitsch           PetscCall(PetscObjectReference((PetscObject)B));
77641a1d4d2SBarry Smith         }
77741a1d4d2SBarry Smith       } else {
7789566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)ts->Arhs));
77951699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
780bdb70873SJed Brown       }
781214bc6a2SJed Brown     }
782214bc6a2SJed Brown     *Brhs = ts->Brhs;
783214bc6a2SJed Brown   }
784214bc6a2SJed Brown   PetscFunctionReturn(0);
785214bc6a2SJed Brown }
786214bc6a2SJed Brown 
787316643e7SJed Brown /*@
7880910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
789316643e7SJed Brown 
790d083f849SBarry Smith    Collective on TS
791316643e7SJed Brown 
792316643e7SJed Brown    Input Parameters:
793316643e7SJed Brown +  ts - the TS context
794316643e7SJed Brown .  t - current time
7950910c330SBarry Smith .  U - state vector
7960910c330SBarry Smith .  Udot - time derivative of state vector
797214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
798316643e7SJed Brown 
799316643e7SJed Brown    Output Parameter:
800316643e7SJed Brown .  Y - right hand side
801316643e7SJed Brown 
802316643e7SJed Brown    Note:
803316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
804316643e7SJed Brown    is used internally within the nonlinear solvers.
805316643e7SJed Brown 
806316643e7SJed Brown    If the user did did not write their equations in implicit form, this
807316643e7SJed Brown    function recasts them in implicit form.
808316643e7SJed Brown 
809316643e7SJed Brown    Level: developer
810316643e7SJed Brown 
811316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
812316643e7SJed Brown @*/
8130910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
814316643e7SJed Brown {
81524989b8cSPeter Brune   TSIFunction    ifunction;
81624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
81724989b8cSPeter Brune   void           *ctx;
81824989b8cSPeter Brune   DM             dm;
819316643e7SJed Brown 
820316643e7SJed Brown   PetscFunctionBegin;
8210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8220910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8230910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8240700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
825316643e7SJed Brown 
8269566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
8279566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm,&ifunction,&ctx));
8289566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
82924989b8cSPeter Brune 
8303c633725SBarry Smith   PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
831d90be118SSean Farley 
8329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y));
83324989b8cSPeter Brune   if (ifunction) {
834316643e7SJed Brown     PetscStackPush("TS user implicit function");
8359566063dSJacob Faibussowitsch     PetscCall((*ifunction)(ts,t,U,Udot,Y,ctx));
836316643e7SJed Brown     PetscStackPop;
837a6ab3590SBarry Smith     ts->ifuncs++;
838214bc6a2SJed Brown   }
839214bc6a2SJed Brown   if (imex) {
84024989b8cSPeter Brune     if (!ifunction) {
8419566063dSJacob Faibussowitsch       PetscCall(VecCopy(Udot,Y));
8422dd45cf8SJed Brown     }
84324989b8cSPeter Brune   } else if (rhsfunction) {
84424989b8cSPeter Brune     if (ifunction) {
845214bc6a2SJed Brown       Vec Frhs;
8469566063dSJacob Faibussowitsch       PetscCall(TSGetRHSVec_Private(ts,&Frhs));
8479566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSFunction(ts,t,U,Frhs));
8489566063dSJacob Faibussowitsch       PetscCall(VecAXPY(Y,-1,Frhs));
8492dd45cf8SJed Brown     } else {
8509566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSFunction(ts,t,U,Y));
8519566063dSJacob Faibussowitsch       PetscCall(VecAYPX(Y,-1,Udot));
852316643e7SJed Brown     }
8534a6899ffSJed Brown   }
8549566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y));
855316643e7SJed Brown   PetscFunctionReturn(0);
856316643e7SJed Brown }
857316643e7SJed Brown 
858cfa8a9a2SHong Zhang /*
859cfa8a9a2SHong Zhang    TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it.
860cfa8a9a2SHong Zhang 
861cfa8a9a2SHong Zhang    Note:
862cfa8a9a2SHong Zhang    This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian().
863cfa8a9a2SHong Zhang 
864cfa8a9a2SHong Zhang */
865cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B)
866cfa8a9a2SHong Zhang {
867cfa8a9a2SHong Zhang   PetscFunctionBegin;
868cfa8a9a2SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8693c633725SBarry Smith   PetscCheck(A == ts->Arhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat");
8703c633725SBarry Smith   PetscCheck(B == ts->Brhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat");
871cfa8a9a2SHong Zhang 
872cfa8a9a2SHong Zhang   if (ts->rhsjacobian.shift) {
8739566063dSJacob Faibussowitsch     PetscCall(MatShift(A,-ts->rhsjacobian.shift));
874cfa8a9a2SHong Zhang   }
875cfa8a9a2SHong Zhang   if (ts->rhsjacobian.scale == -1.) {
8769566063dSJacob Faibussowitsch     PetscCall(MatScale(A,-1));
877cfa8a9a2SHong Zhang   }
878cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
879cfa8a9a2SHong Zhang     if (ts->rhsjacobian.shift) {
8809566063dSJacob Faibussowitsch       PetscCall(MatShift(B,-ts->rhsjacobian.shift));
881cfa8a9a2SHong Zhang     }
882cfa8a9a2SHong Zhang     if (ts->rhsjacobian.scale == -1.) {
8839566063dSJacob Faibussowitsch       PetscCall(MatScale(B,-1));
884cfa8a9a2SHong Zhang     }
885cfa8a9a2SHong Zhang   }
886cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
887cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
888cfa8a9a2SHong Zhang   PetscFunctionReturn(0);
889cfa8a9a2SHong Zhang }
890cfa8a9a2SHong Zhang 
891316643e7SJed Brown /*@
892316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
893316643e7SJed Brown 
894d083f849SBarry Smith    Collective on TS
895316643e7SJed Brown 
896316643e7SJed Brown    Input
897316643e7SJed Brown       Input Parameters:
898316643e7SJed Brown +  ts - the TS context
899316643e7SJed Brown .  t - current timestep
9000910c330SBarry Smith .  U - state vector
9010910c330SBarry Smith .  Udot - time derivative of state vector
902214bc6a2SJed Brown .  shift - shift to apply, see note below
903214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
904316643e7SJed Brown 
905316643e7SJed Brown    Output Parameters:
906316643e7SJed Brown +  A - Jacobian matrix
9073565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
908316643e7SJed Brown 
909316643e7SJed Brown    Notes:
9100910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
911316643e7SJed Brown 
9120910c330SBarry Smith    dF/dU + shift*dF/dUdot
913316643e7SJed Brown 
914316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
915316643e7SJed Brown    is used internally within the nonlinear solvers.
916316643e7SJed Brown 
917316643e7SJed Brown    Level: developer
918316643e7SJed Brown 
919316643e7SJed Brown .seealso:  TSSetIJacobian()
92063495f91SJed Brown @*/
921d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
922316643e7SJed Brown {
92324989b8cSPeter Brune   TSIJacobian    ijacobian;
92424989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
92524989b8cSPeter Brune   DM             dm;
92624989b8cSPeter Brune   void           *ctx;
927316643e7SJed Brown 
928316643e7SJed Brown   PetscFunctionBegin;
9290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9300910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9310910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
932316643e7SJed Brown   PetscValidPointer(A,6);
93394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
934316643e7SJed Brown   PetscValidPointer(B,7);
93594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
93624989b8cSPeter Brune 
9379566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
9389566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,&ijacobian,&ctx));
9399566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL));
94024989b8cSPeter Brune 
9413c633725SBarry Smith   PetscCheck(rhsjacobian || ijacobian,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
942316643e7SJed Brown 
9439566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B));
94424989b8cSPeter Brune   if (ijacobian) {
945316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
9469566063dSJacob Faibussowitsch     PetscCall((*ijacobian)(ts,t,U,Udot,shift,A,B,ctx));
947a6ab3590SBarry Smith     ts->ijacs++;
948316643e7SJed Brown     PetscStackPop;
9494a6899ffSJed Brown   }
950214bc6a2SJed Brown   if (imex) {
951b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9524c26be97Sstefano_zampini       PetscBool assembled;
953971015bcSStefano Zampini       if (rhsjacobian) {
954971015bcSStefano Zampini         Mat Arhs = NULL;
9559566063dSJacob Faibussowitsch         PetscCall(TSGetRHSMats_Private(ts,&Arhs,NULL));
956971015bcSStefano Zampini         if (A == Arhs) {
9573c633725SBarry 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 */
958971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
959971015bcSStefano Zampini         }
960971015bcSStefano Zampini       }
9619566063dSJacob Faibussowitsch       PetscCall(MatZeroEntries(A));
9629566063dSJacob Faibussowitsch       PetscCall(MatAssembled(A,&assembled));
9634c26be97Sstefano_zampini       if (!assembled) {
9649566063dSJacob Faibussowitsch         PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
9659566063dSJacob Faibussowitsch         PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
9664c26be97Sstefano_zampini       }
9679566063dSJacob Faibussowitsch       PetscCall(MatShift(A,shift));
96894ab13aaSBarry Smith       if (A != B) {
9699566063dSJacob Faibussowitsch         PetscCall(MatZeroEntries(B));
9709566063dSJacob Faibussowitsch         PetscCall(MatAssembled(B,&assembled));
9714c26be97Sstefano_zampini         if (!assembled) {
9729566063dSJacob Faibussowitsch           PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
9739566063dSJacob Faibussowitsch           PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
9744c26be97Sstefano_zampini         }
9759566063dSJacob Faibussowitsch         PetscCall(MatShift(B,shift));
976214bc6a2SJed Brown       }
977214bc6a2SJed Brown     }
978214bc6a2SJed Brown   } else {
979e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
980e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
9819566063dSJacob Faibussowitsch       PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs));
982e1244c69SJed Brown     }
983e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
984e8b1e424SHong Zhang       PetscObjectState Ustate;
985e8b1e424SHong Zhang       PetscObjectId    Uid;
986e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
987e8b1e424SHong Zhang 
9889566063dSJacob Faibussowitsch       PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
9899566063dSJacob Faibussowitsch       PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate));
9909566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetId((PetscObject)U,&Uid));
991097a96a2SHong 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 */
9929566063dSJacob Faibussowitsch         PetscCall(MatShift(A,shift-ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */
99334d322a1SHong Zhang         if (A != B) {
9949566063dSJacob Faibussowitsch           PetscCall(MatShift(B,shift-ts->rhsjacobian.shift));
99534d322a1SHong Zhang         }
996e8b1e424SHong Zhang       } else {
9973565c898SBarry Smith         PetscBool flg;
998e8b1e424SHong Zhang 
999e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1000e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1001e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1002e8b1e424SHong Zhang              and the matrices have not been assembled yet */
10039566063dSJacob Faibussowitsch           PetscCall(TSRecoverRHSJacobian(ts,A,B));
1004e8b1e424SHong Zhang         }
10059566063dSJacob Faibussowitsch         PetscCall(TSComputeRHSJacobian(ts,t,U,A,B));
10069566063dSJacob Faibussowitsch         PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg));
10073565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10083565c898SBarry Smith         if (!flg) {
10099566063dSJacob Faibussowitsch           PetscCall(MatScale(A,-1));
10109566063dSJacob Faibussowitsch           PetscCall(MatShift(A,shift));
10113565c898SBarry Smith         }
101294ab13aaSBarry Smith         if (A != B) {
10139566063dSJacob Faibussowitsch           PetscCall(MatScale(B,-1));
10149566063dSJacob Faibussowitsch           PetscCall(MatShift(B,shift));
1015316643e7SJed Brown         }
1016e8b1e424SHong Zhang       }
1017e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1018e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1019d60b7d5cSBarry Smith     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1020e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
10219566063dSJacob Faibussowitsch         PetscCall(MatZeroEntries(A));
10229566063dSJacob Faibussowitsch         PetscCall(MatShift(A,shift));
102394ab13aaSBarry Smith         if (A != B) {
10249566063dSJacob Faibussowitsch           PetscCall(MatZeroEntries(B));
10259566063dSJacob Faibussowitsch           PetscCall(MatShift(B,shift));
1026214bc6a2SJed Brown         }
1027316643e7SJed Brown       }
10289566063dSJacob Faibussowitsch       PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs));
10299566063dSJacob Faibussowitsch       PetscCall(MatAXPY(A,-1,Arhs,ts->axpy_pattern));
103094ab13aaSBarry Smith       if (A != B) {
10319566063dSJacob Faibussowitsch         PetscCall(MatAXPY(B,-1,Brhs,ts->axpy_pattern));
1032316643e7SJed Brown       }
1033316643e7SJed Brown     }
1034316643e7SJed Brown   }
10359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B));
1036316643e7SJed Brown   PetscFunctionReturn(0);
1037316643e7SJed Brown }
1038316643e7SJed Brown 
1039d763cef2SBarry Smith /*@C
1040d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1041b5abc632SBarry Smith     where U_t = G(t,u).
1042d763cef2SBarry Smith 
10433f9fe445SBarry Smith     Logically Collective on TS
1044d763cef2SBarry Smith 
1045d763cef2SBarry Smith     Input Parameters:
1046d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10470298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1048d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1049d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10500298fd71SBarry Smith           function evaluation routine (may be NULL)
1051d763cef2SBarry Smith 
1052a96d6ef6SBarry Smith     Calling sequence of f:
1053a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1054d763cef2SBarry Smith 
1055a96d6ef6SBarry Smith +   ts - timestep context
1056a96d6ef6SBarry Smith .   t - current timestep
1057d763cef2SBarry Smith .   u - input vector
1058d763cef2SBarry Smith .   F - function vector
1059d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1060d763cef2SBarry Smith 
1061d763cef2SBarry Smith     Level: beginner
1062d763cef2SBarry Smith 
106395452b02SPatrick Sanan     Notes:
106495452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10652bbac0d3SBarry Smith 
1066ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1067d763cef2SBarry Smith @*/
1068089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1069d763cef2SBarry Smith {
1070089b2837SJed Brown   SNES           snes;
10710298fd71SBarry Smith   Vec            ralloc = NULL;
107224989b8cSPeter Brune   DM             dm;
1073d763cef2SBarry Smith 
1074089b2837SJed Brown   PetscFunctionBegin;
10750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1076ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
107724989b8cSPeter Brune 
10789566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
10799566063dSJacob Faibussowitsch   PetscCall(DMTSSetRHSFunction(dm,f,ctx));
10809566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
1081e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
10829566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->vec_sol,&ralloc));
1083e856ceecSJed Brown     r = ralloc;
1084e856ceecSJed Brown   }
10859566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts));
10869566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ralloc));
1087d763cef2SBarry Smith   PetscFunctionReturn(0);
1088d763cef2SBarry Smith }
1089d763cef2SBarry Smith 
1090ef20d060SBarry Smith /*@C
1091abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1092ef20d060SBarry Smith 
1093ef20d060SBarry Smith     Logically Collective on TS
1094ef20d060SBarry Smith 
1095ef20d060SBarry Smith     Input Parameters:
1096ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1097ef20d060SBarry Smith .   f - routine for evaluating the solution
1098ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10990298fd71SBarry Smith           function evaluation routine (may be NULL)
1100ef20d060SBarry Smith 
1101a96d6ef6SBarry Smith     Calling sequence of f:
1102a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1103ef20d060SBarry Smith 
1104ef20d060SBarry Smith +   t - current timestep
1105ef20d060SBarry Smith .   u - output vector
1106ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1107ef20d060SBarry Smith 
11080ed3bfb6SBarry Smith     Options Database:
11090ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11100ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11110ed3bfb6SBarry Smith 
1112abd5a294SJed Brown     Notes:
1113abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1114abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1115abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1116abd5a294SJed Brown 
11174f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1118abd5a294SJed Brown 
1119ef20d060SBarry Smith     Level: beginner
1120ef20d060SBarry Smith 
11210ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1122ef20d060SBarry Smith @*/
1123ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1124ef20d060SBarry Smith {
1125ef20d060SBarry Smith   DM             dm;
1126ef20d060SBarry Smith 
1127ef20d060SBarry Smith   PetscFunctionBegin;
1128ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11299566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
11309566063dSJacob Faibussowitsch   PetscCall(DMTSSetSolutionFunction(dm,f,ctx));
1131ef20d060SBarry Smith   PetscFunctionReturn(0);
1132ef20d060SBarry Smith }
1133ef20d060SBarry Smith 
11349b7cd975SBarry Smith /*@C
11359b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11369b7cd975SBarry Smith 
11379b7cd975SBarry Smith     Logically Collective on TS
11389b7cd975SBarry Smith 
11399b7cd975SBarry Smith     Input Parameters:
11409b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1141e162b725SBarry Smith .   func - routine for evaluating the forcing function
11429b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11430298fd71SBarry Smith           function evaluation routine (may be NULL)
11449b7cd975SBarry Smith 
11459b7cd975SBarry Smith     Calling sequence of func:
11466bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11479b7cd975SBarry Smith 
11489b7cd975SBarry Smith +   t - current timestep
1149e162b725SBarry Smith .   f - output vector
11509b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11519b7cd975SBarry Smith 
11529b7cd975SBarry Smith     Notes:
11539b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1154e162b725SBarry 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
1155e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1156e162b725SBarry Smith 
1157e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1158e162b725SBarry Smith 
1159e162b725SBarry 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
1160e162b725SBarry Smith     parameters can be passed in the ctx variable.
11619b7cd975SBarry Smith 
11629b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11639b7cd975SBarry Smith 
11649b7cd975SBarry Smith     Level: beginner
11659b7cd975SBarry Smith 
11669b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11679b7cd975SBarry Smith @*/
1168e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11699b7cd975SBarry Smith {
11709b7cd975SBarry Smith   DM             dm;
11719b7cd975SBarry Smith 
11729b7cd975SBarry Smith   PetscFunctionBegin;
11739b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11749566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
11759566063dSJacob Faibussowitsch   PetscCall(DMTSSetForcingFunction(dm,func,ctx));
11769b7cd975SBarry Smith   PetscFunctionReturn(0);
11779b7cd975SBarry Smith }
11789b7cd975SBarry Smith 
1179d763cef2SBarry Smith /*@C
1180f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1181b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1182d763cef2SBarry Smith 
11833f9fe445SBarry Smith    Logically Collective on TS
1184d763cef2SBarry Smith 
1185d763cef2SBarry Smith    Input Parameters:
1186d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1187e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1188e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1189d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1190d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11910298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1192d763cef2SBarry Smith 
1193f7ab8db6SBarry Smith    Calling sequence of f:
1194a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1195d763cef2SBarry Smith 
1196d763cef2SBarry Smith +  t - current timestep
1197d763cef2SBarry Smith .  u - input vector
1198e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1199e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1200d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1201d763cef2SBarry Smith 
12026cd88445SBarry Smith    Notes:
12036cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12046cd88445SBarry Smith 
12056cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1206ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1207d763cef2SBarry Smith 
1208d763cef2SBarry Smith    Level: beginner
1209d763cef2SBarry Smith 
1210ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1211d763cef2SBarry Smith 
1212d763cef2SBarry Smith @*/
1213e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1214d763cef2SBarry Smith {
1215089b2837SJed Brown   SNES           snes;
121624989b8cSPeter Brune   DM             dm;
121724989b8cSPeter Brune   TSIJacobian    ijacobian;
1218277b19d0SLisandro Dalcin 
1219d763cef2SBarry Smith   PetscFunctionBegin;
12200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1221e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1222e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1223e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1224e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1225d763cef2SBarry Smith 
12269566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
12279566063dSJacob Faibussowitsch   PetscCall(DMTSSetRHSJacobian(dm,f,ctx));
12289566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,&ijacobian,NULL));
12299566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
12305f659677SPeter Brune   if (!ijacobian) {
12319566063dSJacob Faibussowitsch     PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts));
12320e4ef248SJed Brown   }
1233e5d3d808SBarry Smith   if (Amat) {
12349566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)Amat));
12359566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&ts->Arhs));
1236e5d3d808SBarry Smith     ts->Arhs = Amat;
12370e4ef248SJed Brown   }
1238e5d3d808SBarry Smith   if (Pmat) {
12399566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)Pmat));
12409566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&ts->Brhs));
1241e5d3d808SBarry Smith     ts->Brhs = Pmat;
12420e4ef248SJed Brown   }
1243d763cef2SBarry Smith   PetscFunctionReturn(0);
1244d763cef2SBarry Smith }
1245d763cef2SBarry Smith 
1246316643e7SJed Brown /*@C
1247b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1248316643e7SJed Brown 
12493f9fe445SBarry Smith    Logically Collective on TS
1250316643e7SJed Brown 
1251316643e7SJed Brown    Input Parameters:
1252316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12530298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1254316643e7SJed Brown .  f   - the function evaluation routine
12550298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1256316643e7SJed Brown 
1257316643e7SJed Brown    Calling sequence of f:
12586bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1259316643e7SJed Brown 
1260316643e7SJed Brown +  t   - time at step/stage being solved
1261316643e7SJed Brown .  u   - state vector
1262316643e7SJed Brown .  u_t - time derivative of state vector
1263316643e7SJed Brown .  F   - function vector
1264316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1265316643e7SJed Brown 
1266316643e7SJed Brown    Important:
12672bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1268316643e7SJed Brown 
1269316643e7SJed Brown    Level: beginner
1270316643e7SJed Brown 
1271d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1272316643e7SJed Brown @*/
127351699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1274316643e7SJed Brown {
1275089b2837SJed Brown   SNES           snes;
127651699248SLisandro Dalcin   Vec            ralloc = NULL;
127724989b8cSPeter Brune   DM             dm;
1278316643e7SJed Brown 
1279316643e7SJed Brown   PetscFunctionBegin;
12800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
128151699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
128224989b8cSPeter Brune 
12839566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
12849566063dSJacob Faibussowitsch   PetscCall(DMTSSetIFunction(dm,f,ctx));
128524989b8cSPeter Brune 
12869566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
128751699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
12889566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->vec_sol,&ralloc));
128951699248SLisandro Dalcin     r  = ralloc;
1290e856ceecSJed Brown   }
12919566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts));
12929566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ralloc));
1293089b2837SJed Brown   PetscFunctionReturn(0);
1294089b2837SJed Brown }
1295089b2837SJed Brown 
1296089b2837SJed Brown /*@C
1297a5b23f4aSJose E. Roman    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it.
1298089b2837SJed Brown 
1299089b2837SJed Brown    Not Collective
1300089b2837SJed Brown 
1301089b2837SJed Brown    Input Parameter:
1302089b2837SJed Brown .  ts - the TS context
1303089b2837SJed Brown 
1304d8d19677SJose E. Roman    Output Parameters:
13050298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13060298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13070298fd71SBarry Smith -  ctx - the function context (or NULL)
1308089b2837SJed Brown 
1309089b2837SJed Brown    Level: advanced
1310089b2837SJed Brown 
1311089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1312089b2837SJed Brown @*/
1313089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1314089b2837SJed Brown {
1315089b2837SJed Brown   SNES           snes;
131624989b8cSPeter Brune   DM             dm;
1317089b2837SJed Brown 
1318089b2837SJed Brown   PetscFunctionBegin;
1319089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13209566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
13219566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes,r,NULL,NULL));
13229566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
13239566063dSJacob Faibussowitsch   PetscCall(DMTSGetIFunction(dm,func,ctx));
1324089b2837SJed Brown   PetscFunctionReturn(0);
1325089b2837SJed Brown }
1326089b2837SJed Brown 
1327089b2837SJed Brown /*@C
1328089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1329089b2837SJed Brown 
1330089b2837SJed Brown    Not Collective
1331089b2837SJed Brown 
1332089b2837SJed Brown    Input Parameter:
1333089b2837SJed Brown .  ts - the TS context
1334089b2837SJed Brown 
1335d8d19677SJose E. Roman    Output Parameters:
13360298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13370298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13380298fd71SBarry Smith -  ctx - the function context (or NULL)
1339089b2837SJed Brown 
1340089b2837SJed Brown    Level: advanced
1341089b2837SJed Brown 
13422bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1343089b2837SJed Brown @*/
1344089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1345089b2837SJed Brown {
1346089b2837SJed Brown   SNES           snes;
134724989b8cSPeter Brune   DM             dm;
1348089b2837SJed Brown 
1349089b2837SJed Brown   PetscFunctionBegin;
1350089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13519566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
13529566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes,r,NULL,NULL));
13539566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
13549566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,func,ctx));
1355316643e7SJed Brown   PetscFunctionReturn(0);
1356316643e7SJed Brown }
1357316643e7SJed Brown 
1358316643e7SJed Brown /*@C
1359a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1360ae8867d6SBarry Smith         provided with TSSetIFunction().
1361316643e7SJed Brown 
13623f9fe445SBarry Smith    Logically Collective on TS
1363316643e7SJed Brown 
1364316643e7SJed Brown    Input Parameters:
1365316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1366e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1367e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1368316643e7SJed Brown .  f   - the Jacobian evaluation routine
13690298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1370316643e7SJed Brown 
1371316643e7SJed Brown    Calling sequence of f:
13726bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1373316643e7SJed Brown 
1374316643e7SJed Brown +  t    - time at step/stage being solved
13751b4a444bSJed Brown .  U    - state vector
13761b4a444bSJed Brown .  U_t  - time derivative of state vector
1377316643e7SJed Brown .  a    - shift
1378e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1379e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1380316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1381316643e7SJed Brown 
1382316643e7SJed Brown    Notes:
1383e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1384316643e7SJed Brown 
1385895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1386895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1387895c21f2SBarry Smith 
1388a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1389b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1390a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1391a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1392a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1393a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1394a4f0a591SBarry Smith 
13956cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13966cd88445SBarry Smith 
13976cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1398ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1399ca5f011dSBarry Smith 
1400316643e7SJed Brown    Level: beginner
1401316643e7SJed Brown 
1402ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1403316643e7SJed Brown 
1404316643e7SJed Brown @*/
1405e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1406316643e7SJed Brown {
1407089b2837SJed Brown   SNES           snes;
140824989b8cSPeter Brune   DM             dm;
1409316643e7SJed Brown 
1410316643e7SJed Brown   PetscFunctionBegin;
14110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1412e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1413e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1414e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1415e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
141624989b8cSPeter Brune 
14179566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
14189566063dSJacob Faibussowitsch   PetscCall(DMTSSetIJacobian(dm,f,ctx));
141924989b8cSPeter Brune 
14209566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
14219566063dSJacob Faibussowitsch   PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts));
1422316643e7SJed Brown   PetscFunctionReturn(0);
1423316643e7SJed Brown }
1424316643e7SJed Brown 
1425e1244c69SJed Brown /*@
1426e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1427e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1428e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1429e1244c69SJed Brown    not been changed by the TS.
1430e1244c69SJed Brown 
1431e1244c69SJed Brown    Logically Collective
1432e1244c69SJed Brown 
14334165533cSJose E. Roman    Input Parameters:
1434e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1435e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1436e1244c69SJed Brown 
1437e1244c69SJed Brown    Level: intermediate
1438e1244c69SJed Brown 
1439e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1440e1244c69SJed Brown @*/
1441e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1442e1244c69SJed Brown {
1443e1244c69SJed Brown   PetscFunctionBegin;
1444e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1445e1244c69SJed Brown   PetscFunctionReturn(0);
1446e1244c69SJed Brown }
1447e1244c69SJed Brown 
1448efe9872eSLisandro Dalcin /*@C
1449efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1450efe9872eSLisandro Dalcin 
1451efe9872eSLisandro Dalcin    Logically Collective on TS
1452efe9872eSLisandro Dalcin 
1453efe9872eSLisandro Dalcin    Input Parameters:
1454efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1455efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1456efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1457efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1458efe9872eSLisandro Dalcin 
1459efe9872eSLisandro Dalcin    Calling sequence of fun:
14606bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1461efe9872eSLisandro Dalcin 
1462efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1463efe9872eSLisandro Dalcin .  U    - state vector
1464efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1465efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1466efe9872eSLisandro Dalcin .  F    - function vector
1467efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1468efe9872eSLisandro Dalcin 
1469efe9872eSLisandro Dalcin    Level: beginner
1470efe9872eSLisandro Dalcin 
1471a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1472efe9872eSLisandro Dalcin @*/
1473efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1474efe9872eSLisandro Dalcin {
1475efe9872eSLisandro Dalcin   DM             dm;
1476efe9872eSLisandro Dalcin 
1477efe9872eSLisandro Dalcin   PetscFunctionBegin;
1478efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1479efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
14809566063dSJacob Faibussowitsch   PetscCall(TSSetIFunction(ts,F,NULL,NULL));
14819566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
14829566063dSJacob Faibussowitsch   PetscCall(DMTSSetI2Function(dm,fun,ctx));
1483efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1484efe9872eSLisandro Dalcin }
1485efe9872eSLisandro Dalcin 
1486efe9872eSLisandro Dalcin /*@C
1487a5b23f4aSJose E. Roman   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it.
1488efe9872eSLisandro Dalcin 
1489efe9872eSLisandro Dalcin   Not Collective
1490efe9872eSLisandro Dalcin 
1491efe9872eSLisandro Dalcin   Input Parameter:
1492efe9872eSLisandro Dalcin . ts - the TS context
1493efe9872eSLisandro Dalcin 
1494d8d19677SJose E. Roman   Output Parameters:
1495efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1496efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1497efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1498efe9872eSLisandro Dalcin 
1499efe9872eSLisandro Dalcin   Level: advanced
1500efe9872eSLisandro Dalcin 
1501a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1502efe9872eSLisandro Dalcin @*/
1503efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1504efe9872eSLisandro Dalcin {
1505efe9872eSLisandro Dalcin   SNES           snes;
1506efe9872eSLisandro Dalcin   DM             dm;
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin   PetscFunctionBegin;
1509efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15109566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
15119566063dSJacob Faibussowitsch   PetscCall(SNESGetFunction(snes,r,NULL,NULL));
15129566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
15139566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Function(dm,fun,ctx));
1514efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1515efe9872eSLisandro Dalcin }
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin /*@C
1518bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1519efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin    Logically Collective on TS
1522efe9872eSLisandro Dalcin 
1523efe9872eSLisandro Dalcin    Input Parameters:
1524efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1525efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1526efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1527efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1528efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin    Calling sequence of jac:
15316bc98fa9SBarry 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);
1532efe9872eSLisandro Dalcin 
1533efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1534efe9872eSLisandro Dalcin .  U    - state vector
1535efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1536efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1537efe9872eSLisandro Dalcin .  v    - shift for U_t
1538efe9872eSLisandro Dalcin .  a    - shift for U_tt
1539efe9872eSLisandro 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
1540efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1541efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1542efe9872eSLisandro Dalcin 
1543efe9872eSLisandro Dalcin    Notes:
1544efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1545efe9872eSLisandro Dalcin 
1546efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1547efe9872eSLisandro 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.
1548efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1549bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin    Level: beginner
1552efe9872eSLisandro Dalcin 
1553a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1554efe9872eSLisandro Dalcin @*/
1555efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1556efe9872eSLisandro Dalcin {
1557efe9872eSLisandro Dalcin   DM             dm;
1558efe9872eSLisandro Dalcin 
1559efe9872eSLisandro Dalcin   PetscFunctionBegin;
1560efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1561efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1562efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
15639566063dSJacob Faibussowitsch   PetscCall(TSSetIJacobian(ts,J,P,NULL,NULL));
15649566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
15659566063dSJacob Faibussowitsch   PetscCall(DMTSSetI2Jacobian(dm,jac,ctx));
1566efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1567efe9872eSLisandro Dalcin }
1568efe9872eSLisandro Dalcin 
1569efe9872eSLisandro Dalcin /*@C
1570efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1571efe9872eSLisandro Dalcin 
1572efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1573efe9872eSLisandro Dalcin 
1574efe9872eSLisandro Dalcin   Input Parameter:
1575efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1576efe9872eSLisandro Dalcin 
1577efe9872eSLisandro Dalcin   Output Parameters:
1578efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1579efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1580efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1581efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1582efe9872eSLisandro Dalcin 
158395452b02SPatrick Sanan   Notes:
158495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1585efe9872eSLisandro Dalcin 
1586efe9872eSLisandro Dalcin   Level: advanced
1587efe9872eSLisandro Dalcin 
1588a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1589efe9872eSLisandro Dalcin 
1590efe9872eSLisandro Dalcin @*/
1591efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1592efe9872eSLisandro Dalcin {
1593efe9872eSLisandro Dalcin   SNES           snes;
1594efe9872eSLisandro Dalcin   DM             dm;
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   PetscFunctionBegin;
15979566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
15989566063dSJacob Faibussowitsch   PetscCall(SNESSetUpMatrices(snes));
15999566063dSJacob Faibussowitsch   PetscCall(SNESGetJacobian(snes,J,P,NULL,NULL));
16009566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
16019566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm,jac,ctx));
1602efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1603efe9872eSLisandro Dalcin }
1604efe9872eSLisandro Dalcin 
1605efe9872eSLisandro Dalcin /*@
1606efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1607efe9872eSLisandro Dalcin 
1608d083f849SBarry Smith   Collective on TS
1609efe9872eSLisandro Dalcin 
1610efe9872eSLisandro Dalcin   Input Parameters:
1611efe9872eSLisandro Dalcin + ts - the TS context
1612efe9872eSLisandro Dalcin . t - current time
1613efe9872eSLisandro Dalcin . U - state vector
1614efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1615efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1616efe9872eSLisandro Dalcin 
1617efe9872eSLisandro Dalcin   Output Parameter:
1618efe9872eSLisandro Dalcin . F - the residual vector
1619efe9872eSLisandro Dalcin 
1620efe9872eSLisandro Dalcin   Note:
1621efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1622efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1623efe9872eSLisandro Dalcin 
1624efe9872eSLisandro Dalcin   Level: developer
1625efe9872eSLisandro Dalcin 
1626a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1627efe9872eSLisandro Dalcin @*/
1628efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1629efe9872eSLisandro Dalcin {
1630efe9872eSLisandro Dalcin   DM             dm;
1631efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1632efe9872eSLisandro Dalcin   void           *ctx;
1633efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1634efe9872eSLisandro Dalcin 
1635efe9872eSLisandro Dalcin   PetscFunctionBegin;
1636efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1637efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1638efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1639efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1640efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1641efe9872eSLisandro Dalcin 
16429566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
16439566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Function(dm,&I2Function,&ctx));
16449566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL));
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   if (!I2Function) {
16479566063dSJacob Faibussowitsch     PetscCall(TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE));
1648efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1649efe9872eSLisandro Dalcin   }
1650efe9872eSLisandro Dalcin 
16519566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,V,F));
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
16549566063dSJacob Faibussowitsch   PetscCall(I2Function(ts,t,U,V,A,F,ctx));
1655efe9872eSLisandro Dalcin   PetscStackPop;
1656efe9872eSLisandro Dalcin 
1657efe9872eSLisandro Dalcin   if (rhsfunction) {
1658efe9872eSLisandro Dalcin     Vec Frhs;
16599566063dSJacob Faibussowitsch     PetscCall(TSGetRHSVec_Private(ts,&Frhs));
16609566063dSJacob Faibussowitsch     PetscCall(TSComputeRHSFunction(ts,t,U,Frhs));
16619566063dSJacob Faibussowitsch     PetscCall(VecAXPY(F,-1,Frhs));
1662efe9872eSLisandro Dalcin   }
1663efe9872eSLisandro Dalcin 
16649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,V,F));
1665efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1666efe9872eSLisandro Dalcin }
1667efe9872eSLisandro Dalcin 
1668efe9872eSLisandro Dalcin /*@
1669efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1670efe9872eSLisandro Dalcin 
1671d083f849SBarry Smith   Collective on TS
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   Input Parameters:
1674efe9872eSLisandro Dalcin + ts - the TS context
1675efe9872eSLisandro Dalcin . t - current timestep
1676efe9872eSLisandro Dalcin . U - state vector
1677efe9872eSLisandro Dalcin . V - time derivative of state vector
1678efe9872eSLisandro Dalcin . A - second time derivative of state vector
1679efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1680efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1681efe9872eSLisandro Dalcin 
1682efe9872eSLisandro Dalcin   Output Parameters:
1683efe9872eSLisandro Dalcin + J - Jacobian matrix
1684efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1685efe9872eSLisandro Dalcin 
1686efe9872eSLisandro Dalcin   Notes:
1687efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1688efe9872eSLisandro Dalcin 
1689efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1690efe9872eSLisandro Dalcin 
1691efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1692efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin   Level: developer
1695efe9872eSLisandro Dalcin 
1696efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1697efe9872eSLisandro Dalcin @*/
1698efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1699efe9872eSLisandro Dalcin {
1700efe9872eSLisandro Dalcin   DM             dm;
1701efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1702efe9872eSLisandro Dalcin   void           *ctx;
1703efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin   PetscFunctionBegin;
1706efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1707efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1708efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1709efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1710efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1711efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1712efe9872eSLisandro Dalcin 
17139566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
17149566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx));
17159566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL));
1716efe9872eSLisandro Dalcin 
1717efe9872eSLisandro Dalcin   if (!I2Jacobian) {
17189566063dSJacob Faibussowitsch     PetscCall(TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE));
1719efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1720efe9872eSLisandro Dalcin   }
1721efe9872eSLisandro Dalcin 
17229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,J,P));
1723efe9872eSLisandro Dalcin 
1724efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
17259566063dSJacob Faibussowitsch   PetscCall(I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx));
1726efe9872eSLisandro Dalcin   PetscStackPop;
1727efe9872eSLisandro Dalcin 
1728efe9872eSLisandro Dalcin   if (rhsjacobian) {
1729d60b7d5cSBarry Smith     Mat Jrhs,Prhs;
17309566063dSJacob Faibussowitsch     PetscCall(TSGetRHSMats_Private(ts,&Jrhs,&Prhs));
17319566063dSJacob Faibussowitsch     PetscCall(TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs));
17329566063dSJacob Faibussowitsch     PetscCall(MatAXPY(J,-1,Jrhs,ts->axpy_pattern));
17339566063dSJacob Faibussowitsch     if (P != J) PetscCall(MatAXPY(P,-1,Prhs,ts->axpy_pattern));
1734efe9872eSLisandro Dalcin   }
1735efe9872eSLisandro Dalcin 
17369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,J,P));
1737efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1738efe9872eSLisandro Dalcin }
1739efe9872eSLisandro Dalcin 
1740438f35afSJed Brown /*@C
1741438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1742438f35afSJed Brown 
1743438f35afSJed Brown    Logically Collective
1744438f35afSJed Brown 
17454165533cSJose E. Roman    Input Parameters:
1746438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1747a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1748438f35afSJed Brown -  ctx - a context for tvar
1749438f35afSJed Brown 
1750a96d6ef6SBarry Smith     Calling sequence of tvar:
1751a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1752a96d6ef6SBarry Smith 
1753a96d6ef6SBarry Smith +   ts - timestep context
1754a96d6ef6SBarry Smith .   p - input vector (primative form)
1755a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1756a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1757a96d6ef6SBarry Smith 
1758438f35afSJed Brown    Level: advanced
1759438f35afSJed Brown 
1760438f35afSJed Brown    Notes:
1761438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1762438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1763438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1764438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1765438f35afSJed Brown    evaluated via the chain rule, as in
1766438f35afSJed Brown 
1767438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1768438f35afSJed Brown 
1769438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1770438f35afSJed Brown @*/
1771438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1772438f35afSJed Brown {
1773438f35afSJed Brown   DM             dm;
1774438f35afSJed Brown 
1775438f35afSJed Brown   PetscFunctionBegin;
1776438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17779566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
17789566063dSJacob Faibussowitsch   PetscCall(DMTSSetTransientVariable(dm,tvar,ctx));
1779438f35afSJed Brown   PetscFunctionReturn(0);
1780438f35afSJed Brown }
1781438f35afSJed Brown 
1782efe9872eSLisandro Dalcin /*@
1783e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1784e3c11fc1SJed Brown 
1785e3c11fc1SJed Brown    Logically Collective
1786e3c11fc1SJed Brown 
1787e3c11fc1SJed Brown    Input Parameters:
1788e3c11fc1SJed Brown +  ts - TS on which to compute
1789e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1790e3c11fc1SJed Brown 
1791e3c11fc1SJed Brown    Output Parameters:
1792e3c11fc1SJed Brown .  C - transient (conservative) variable
1793e3c11fc1SJed Brown 
1794e3c11fc1SJed Brown    Developer Notes:
1795e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1796e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1797e3c11fc1SJed Brown    being used.
1798e3c11fc1SJed Brown 
1799e3c11fc1SJed Brown    Level: developer
1800e3c11fc1SJed Brown 
1801e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1802e3c11fc1SJed Brown @*/
1803e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1804e3c11fc1SJed Brown {
1805e3c11fc1SJed Brown   DM             dm;
1806e3c11fc1SJed Brown   DMTS           dmts;
1807e3c11fc1SJed Brown 
1808e3c11fc1SJed Brown   PetscFunctionBegin;
1809e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1810e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
18119566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
18129566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&dmts));
1813e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1814e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
18159566063dSJacob Faibussowitsch     PetscCall((*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx));
1816e3c11fc1SJed Brown   }
1817e3c11fc1SJed Brown   PetscFunctionReturn(0);
1818e3c11fc1SJed Brown }
1819e3c11fc1SJed Brown 
1820e3c11fc1SJed Brown /*@
1821e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1822e3c11fc1SJed Brown 
1823e3c11fc1SJed Brown    Logically Collective
1824e3c11fc1SJed Brown 
1825e3c11fc1SJed Brown    Input Parameters:
1826e3c11fc1SJed Brown .  ts - TS on which to compute
1827e3c11fc1SJed Brown 
1828e3c11fc1SJed Brown    Output Parameters:
1829e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1830e3c11fc1SJed Brown 
1831e3c11fc1SJed Brown    Level: developer
1832e3c11fc1SJed Brown 
1833e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1834e3c11fc1SJed Brown @*/
1835e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1836e3c11fc1SJed Brown {
1837e3c11fc1SJed Brown   DM             dm;
1838e3c11fc1SJed Brown   DMTS           dmts;
1839e3c11fc1SJed Brown 
1840e3c11fc1SJed Brown   PetscFunctionBegin;
1841e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18429566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
18439566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&dmts));
1844e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1845e3c11fc1SJed Brown   PetscFunctionReturn(0);
1846e3c11fc1SJed Brown }
1847e3c11fc1SJed Brown 
1848e3c11fc1SJed Brown /*@
1849efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1850efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1851efe9872eSLisandro Dalcin 
1852d083f849SBarry Smith    Logically Collective on TS
1853efe9872eSLisandro Dalcin 
1854efe9872eSLisandro Dalcin    Input Parameters:
1855efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1856efe9872eSLisandro Dalcin .  u - the solution vector
1857efe9872eSLisandro Dalcin -  v - the time derivative vector
1858efe9872eSLisandro Dalcin 
1859efe9872eSLisandro Dalcin    Level: beginner
1860efe9872eSLisandro Dalcin 
1861efe9872eSLisandro Dalcin @*/
1862efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1863efe9872eSLisandro Dalcin {
1864efe9872eSLisandro Dalcin   PetscFunctionBegin;
1865efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1866efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1867efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
18689566063dSJacob Faibussowitsch   PetscCall(TSSetSolution(ts,u));
18699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)v));
18709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_dot));
1871efe9872eSLisandro Dalcin   ts->vec_dot = v;
1872efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1873efe9872eSLisandro Dalcin }
1874efe9872eSLisandro Dalcin 
1875efe9872eSLisandro Dalcin /*@
1876efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1877efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1878efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1879efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1880efe9872eSLisandro Dalcin 
1881efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1882efe9872eSLisandro Dalcin 
1883efe9872eSLisandro Dalcin    Input Parameter:
1884efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1885efe9872eSLisandro Dalcin 
1886d8d19677SJose E. Roman    Output Parameters:
1887efe9872eSLisandro Dalcin +  u - the vector containing the solution
1888efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1889efe9872eSLisandro Dalcin 
1890efe9872eSLisandro Dalcin    Level: intermediate
1891efe9872eSLisandro Dalcin 
1892efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1893efe9872eSLisandro Dalcin 
1894efe9872eSLisandro Dalcin @*/
1895efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1896efe9872eSLisandro Dalcin {
1897efe9872eSLisandro Dalcin   PetscFunctionBegin;
1898efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1899efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1900efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1901efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1902efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1903efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1904efe9872eSLisandro Dalcin }
1905efe9872eSLisandro Dalcin 
190655849f57SBarry Smith /*@C
190755849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
190855849f57SBarry Smith 
190955849f57SBarry Smith   Collective on PetscViewer
191055849f57SBarry Smith 
191155849f57SBarry Smith   Input Parameters:
191255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
191355849f57SBarry Smith            some related function before a call to TSLoad().
191455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
191555849f57SBarry Smith 
191655849f57SBarry Smith    Level: intermediate
191755849f57SBarry Smith 
191855849f57SBarry Smith   Notes:
191955849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
192055849f57SBarry Smith 
192155849f57SBarry Smith   Notes for advanced users:
192255849f57SBarry Smith   Most users should not need to know the details of the binary storage
192355849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
192455849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
192555849f57SBarry Smith   format is
192655849f57SBarry Smith .vb
192755849f57SBarry Smith      has not yet been determined
192855849f57SBarry Smith .ve
192955849f57SBarry Smith 
193055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
193155849f57SBarry Smith @*/
1932f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
193355849f57SBarry Smith {
193455849f57SBarry Smith   PetscBool      isbinary;
193555849f57SBarry Smith   PetscInt       classid;
193655849f57SBarry Smith   char           type[256];
19372d53ad75SBarry Smith   DMTS           sdm;
1938ad6bc421SBarry Smith   DM             dm;
193955849f57SBarry Smith 
194055849f57SBarry Smith   PetscFunctionBegin;
1941f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
194255849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
19439566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
19443c633725SBarry Smith   PetscCheck(isbinary,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
194555849f57SBarry Smith 
19469566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
19473c633725SBarry Smith   PetscCheck(classid == TS_FILE_CLASSID,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
19489566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
19499566063dSJacob Faibussowitsch   PetscCall(TSSetType(ts, type));
1950f2c2a1b9SBarry Smith   if (ts->ops->load) {
19519566063dSJacob Faibussowitsch     PetscCall((*ts->ops->load)(ts,viewer));
1952f2c2a1b9SBarry Smith   }
19539566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)ts),&dm));
19549566063dSJacob Faibussowitsch   PetscCall(DMLoad(dm,viewer));
19559566063dSJacob Faibussowitsch   PetscCall(TSSetDM(ts,dm));
19569566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol));
19579566063dSJacob Faibussowitsch   PetscCall(VecLoad(ts->vec_sol,viewer));
19589566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(ts->dm,&sdm));
19599566063dSJacob Faibussowitsch   PetscCall(DMTSLoad(sdm,viewer));
196055849f57SBarry Smith   PetscFunctionReturn(0);
196155849f57SBarry Smith }
196255849f57SBarry Smith 
19639804daf3SBarry Smith #include <petscdraw.h>
1964e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1965e04113cfSBarry Smith #include <petscviewersaws.h>
1966f05ece33SBarry Smith #endif
1967fe2efc57SMark 
1968fe2efc57SMark /*@C
1969fe2efc57SMark    TSViewFromOptions - View from Options
1970fe2efc57SMark 
1971fe2efc57SMark    Collective on TS
1972fe2efc57SMark 
1973fe2efc57SMark    Input Parameters:
1974fe2efc57SMark +  A - the application ordering context
1975736c3998SJose E. Roman .  obj - Optional object
1976736c3998SJose E. Roman -  name - command line option
1977fe2efc57SMark 
1978fe2efc57SMark    Level: intermediate
1979fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
1980fe2efc57SMark @*/
1981fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
1982fe2efc57SMark {
1983fe2efc57SMark   PetscFunctionBegin;
1984fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
19859566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name));
1986fe2efc57SMark   PetscFunctionReturn(0);
1987fe2efc57SMark }
1988fe2efc57SMark 
19897e2c5f70SBarry Smith /*@C
1990d763cef2SBarry Smith     TSView - Prints the TS data structure.
1991d763cef2SBarry Smith 
19924c49b128SBarry Smith     Collective on TS
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith     Input Parameters:
1995d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1996d763cef2SBarry Smith -   viewer - visualization context
1997d763cef2SBarry Smith 
1998d763cef2SBarry Smith     Options Database Key:
1999d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2000d763cef2SBarry Smith 
2001d763cef2SBarry Smith     Notes:
2002d763cef2SBarry Smith     The available visualization contexts include
2003b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2004b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2005d763cef2SBarry Smith          output where only the first processor opens
2006d763cef2SBarry Smith          the file.  All other processors send their
2007d763cef2SBarry Smith          data to the first processor to print.
2008d763cef2SBarry Smith 
2009d763cef2SBarry Smith     The user can open an alternative visualization context with
2010b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2011d763cef2SBarry Smith 
2012595c91d4SBarry Smith     In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer).
2013595c91d4SBarry Smith 
2014d763cef2SBarry Smith     Level: beginner
2015d763cef2SBarry Smith 
2016b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2017d763cef2SBarry Smith @*/
20187087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2019d763cef2SBarry Smith {
202019fd82e9SBarry Smith   TSType         type;
20212b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20222d53ad75SBarry Smith   DMTS           sdm;
2023e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2024536b137fSBarry Smith   PetscBool      issaws;
2025f05ece33SBarry Smith #endif
2026d763cef2SBarry Smith 
2027d763cef2SBarry Smith   PetscFunctionBegin;
20280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20293050cee2SBarry Smith   if (!viewer) {
20309566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer));
20313050cee2SBarry Smith   }
20320700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2033c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2034fd16b177SBarry Smith 
20359566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii));
20369566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring));
20379566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
20389566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw));
2039e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
20409566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws));
2041f05ece33SBarry Smith #endif
204232077d6dSBarry Smith   if (iascii) {
20439566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer));
2044efd4aadfSBarry Smith     if (ts->ops->view) {
20459566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
20469566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
20479566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
2048efd4aadfSBarry Smith     }
2049ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
20509566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps));
2051ef85077eSLisandro Dalcin     }
2052ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20539566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time));
2054ef85077eSLisandro Dalcin     }
2055a6ab3590SBarry Smith     if (ts->ifuncs) {
20569566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of I function evaluations=%D\n",ts->ifuncs));
2057a6ab3590SBarry Smith     }
2058a6ab3590SBarry Smith     if (ts->ijacs) {
20599566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of I Jacobian evaluations=%D\n",ts->ijacs));
2060a6ab3590SBarry Smith     }
2061a6ab3590SBarry Smith     if (ts->rhsfuncs) {
20629566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of RHS function evaluations=%D\n",ts->rhsfuncs));
2063a6ab3590SBarry Smith     }
2064a6ab3590SBarry Smith     if (ts->rhsjacs) {
20659566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of RHS Jacobian evaluations=%D\n",ts->rhsjacs));
2066a6ab3590SBarry Smith     }
2067efd4aadfSBarry Smith     if (ts->usessnes) {
2068efd4aadfSBarry Smith       PetscBool lin;
2069d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20709566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its));
2071d763cef2SBarry Smith       }
20729566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its));
20739566063dSJacob Faibussowitsch       PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,""));
20749566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures));
2075efd4aadfSBarry Smith     }
20769566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject));
2077a0af407cSBarry Smith     if (ts->vrtol) {
20789566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, "));
2079a0af407cSBarry Smith     } else {
20809566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol));
2081a0af407cSBarry Smith     }
2082a0af407cSBarry Smith     if (ts->vatol) {
20839566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n"));
2084a0af407cSBarry Smith     } else {
20859566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol));
2086a0af407cSBarry Smith     }
20879566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
20889566063dSJacob Faibussowitsch     PetscCall(TSAdaptView(ts->adapt,viewer));
20899566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
20900f5bd95cSBarry Smith   } else if (isstring) {
20919566063dSJacob Faibussowitsch     PetscCall(TSGetType(ts,&type));
20929566063dSJacob Faibussowitsch     PetscCall(PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type));
20939566063dSJacob Faibussowitsch     if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer));
209455849f57SBarry Smith   } else if (isbinary) {
209555849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
209655849f57SBarry Smith     MPI_Comm    comm;
209755849f57SBarry Smith     PetscMPIInt rank;
209855849f57SBarry Smith     char        type[256];
209955849f57SBarry Smith 
21009566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetComm((PetscObject)ts,&comm));
21019566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(comm,&rank));
2102dd400576SPatrick Sanan     if (rank == 0) {
21039566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT));
21049566063dSJacob Faibussowitsch       PetscCall(PetscStrncpy(type,((PetscObject)ts)->type_name,256));
21059566063dSJacob Faibussowitsch       PetscCall(PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR));
210655849f57SBarry Smith     }
210755849f57SBarry Smith     if (ts->ops->view) {
21089566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
210955849f57SBarry Smith     }
21109566063dSJacob Faibussowitsch     if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer));
21119566063dSJacob Faibussowitsch     PetscCall(DMView(ts->dm,viewer));
21129566063dSJacob Faibussowitsch     PetscCall(VecView(ts->vec_sol,viewer));
21139566063dSJacob Faibussowitsch     PetscCall(DMGetDMTS(ts->dm,&sdm));
21149566063dSJacob Faibussowitsch     PetscCall(DMTSView(sdm,viewer));
21152b0a91c0SBarry Smith   } else if (isdraw) {
21162b0a91c0SBarry Smith     PetscDraw draw;
21172b0a91c0SBarry Smith     char      str[36];
211889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21192b0a91c0SBarry Smith 
21209566063dSJacob Faibussowitsch     PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw));
21219566063dSJacob Faibussowitsch     PetscCall(PetscDrawGetCurrentPoint(draw,&x,&y));
21229566063dSJacob Faibussowitsch     PetscCall(PetscStrcpy(str,"TS: "));
21239566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(str,((PetscObject)ts)->type_name));
21249566063dSJacob Faibussowitsch     PetscCall(PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h));
212589fd9fafSBarry Smith     bottom = y - h;
21269566063dSJacob Faibussowitsch     PetscCall(PetscDrawPushCurrentPoint(draw,x,bottom));
21272b0a91c0SBarry Smith     if (ts->ops->view) {
21289566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
21292b0a91c0SBarry Smith     }
21309566063dSJacob Faibussowitsch     if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer));
21319566063dSJacob Faibussowitsch     if (ts->snes)  PetscCall(SNESView(ts->snes,viewer));
21329566063dSJacob Faibussowitsch     PetscCall(PetscDrawPopCurrentPoint(draw));
2133e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2134536b137fSBarry Smith   } else if (issaws) {
2135d45a07a7SBarry Smith     PetscMPIInt rank;
21362657e9d9SBarry Smith     const char  *name;
21372657e9d9SBarry Smith 
21389566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)ts,&name));
21399566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
2140dd400576SPatrick Sanan     if (!((PetscObject)ts)->amsmem && rank == 0) {
2141d45a07a7SBarry Smith       char       dir[1024];
2142d45a07a7SBarry Smith 
21439566063dSJacob Faibussowitsch       PetscCall(PetscObjectViewSAWs((PetscObject)ts,viewer));
21449566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name));
21452657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21469566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name));
21472657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2148d763cef2SBarry Smith     }
21490acecf5bSBarry Smith     if (ts->ops->view) {
21509566063dSJacob Faibussowitsch       PetscCall((*ts->ops->view)(ts,viewer));
21510acecf5bSBarry Smith     }
2152f05ece33SBarry Smith #endif
2153f05ece33SBarry Smith   }
215436a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
21559566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
21569566063dSJacob Faibussowitsch     PetscCall(SNESView(ts->snes,viewer));
21579566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
215836a9e3b9SBarry Smith   }
21599566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(ts->dm,&sdm));
21609566063dSJacob Faibussowitsch   PetscCall(DMTSView(sdm,viewer));
2161f05ece33SBarry Smith 
21629566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushTab(viewer));
21639566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials));
21649566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopTab(viewer));
2165d763cef2SBarry Smith   PetscFunctionReturn(0);
2166d763cef2SBarry Smith }
2167d763cef2SBarry Smith 
2168b07ff414SBarry Smith /*@
2169d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2170d763cef2SBarry Smith    the timesteppers.
2171d763cef2SBarry Smith 
21723f9fe445SBarry Smith    Logically Collective on TS
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Input Parameters:
2175d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2176d763cef2SBarry Smith -  usrP - optional user context
2177d763cef2SBarry Smith 
217895452b02SPatrick Sanan    Fortran Notes:
217995452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2180daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2181daf670e6SBarry Smith 
2182d763cef2SBarry Smith    Level: intermediate
2183d763cef2SBarry Smith 
2184d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2185d763cef2SBarry Smith @*/
21867087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2187d763cef2SBarry Smith {
2188d763cef2SBarry Smith   PetscFunctionBegin;
21890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2190d763cef2SBarry Smith   ts->user = usrP;
2191d763cef2SBarry Smith   PetscFunctionReturn(0);
2192d763cef2SBarry Smith }
2193d763cef2SBarry Smith 
2194b07ff414SBarry Smith /*@
2195d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2196d763cef2SBarry Smith     timestepper.
2197d763cef2SBarry Smith 
2198d763cef2SBarry Smith     Not Collective
2199d763cef2SBarry Smith 
2200d763cef2SBarry Smith     Input Parameter:
2201d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2202d763cef2SBarry Smith 
2203d763cef2SBarry Smith     Output Parameter:
2204d763cef2SBarry Smith .   usrP - user context
2205d763cef2SBarry Smith 
220695452b02SPatrick Sanan    Fortran Notes:
220795452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2208daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2209daf670e6SBarry Smith 
2210d763cef2SBarry Smith     Level: intermediate
2211d763cef2SBarry Smith 
2212d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2213d763cef2SBarry Smith @*/
2214e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2215d763cef2SBarry Smith {
2216d763cef2SBarry Smith   PetscFunctionBegin;
22170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2218e71120c6SJed Brown   *(void**)usrP = ts->user;
2219d763cef2SBarry Smith   PetscFunctionReturn(0);
2220d763cef2SBarry Smith }
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith /*@
222380275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2224d763cef2SBarry Smith 
2225d763cef2SBarry Smith    Not Collective
2226d763cef2SBarry Smith 
2227d763cef2SBarry Smith    Input Parameter:
2228d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2229d763cef2SBarry Smith 
2230d763cef2SBarry Smith    Output Parameter:
223180275a0aSLisandro Dalcin .  steps - number of steps completed so far
2232d763cef2SBarry Smith 
2233d763cef2SBarry Smith    Level: intermediate
2234d763cef2SBarry Smith 
22359be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2236d763cef2SBarry Smith @*/
223780275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2238d763cef2SBarry Smith {
2239d763cef2SBarry Smith   PetscFunctionBegin;
22400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224180275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
224280275a0aSLisandro Dalcin   *steps = ts->steps;
224380275a0aSLisandro Dalcin   PetscFunctionReturn(0);
224480275a0aSLisandro Dalcin }
224580275a0aSLisandro Dalcin 
224680275a0aSLisandro Dalcin /*@
224780275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
224880275a0aSLisandro Dalcin 
224980275a0aSLisandro Dalcin    Logically Collective on TS
225080275a0aSLisandro Dalcin 
225180275a0aSLisandro Dalcin    Input Parameters:
225280275a0aSLisandro Dalcin +  ts - the TS context
225380275a0aSLisandro Dalcin -  steps - number of steps completed so far
225480275a0aSLisandro Dalcin 
225580275a0aSLisandro Dalcin    Notes:
225680275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
225780275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
225880275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
225980275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
226080275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
226180275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
226280275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
226380275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
226480275a0aSLisandro Dalcin 
226580275a0aSLisandro Dalcin    Level: advanced
226680275a0aSLisandro Dalcin 
226780275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
226880275a0aSLisandro Dalcin @*/
226980275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
227080275a0aSLisandro Dalcin {
227180275a0aSLisandro Dalcin   PetscFunctionBegin;
227280275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227380275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
22743c633725SBarry Smith   PetscCheck(steps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
227580275a0aSLisandro Dalcin   ts->steps = steps;
2276d763cef2SBarry Smith   PetscFunctionReturn(0);
2277d763cef2SBarry Smith }
2278d763cef2SBarry Smith 
2279d763cef2SBarry Smith /*@
2280d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2281d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2282d763cef2SBarry Smith 
22833f9fe445SBarry Smith    Logically Collective on TS
2284d763cef2SBarry Smith 
2285d763cef2SBarry Smith    Input Parameters:
2286d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2287d763cef2SBarry Smith -  time_step - the size of the timestep
2288d763cef2SBarry Smith 
2289d763cef2SBarry Smith    Level: intermediate
2290d763cef2SBarry Smith 
2291aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2292d763cef2SBarry Smith 
2293d763cef2SBarry Smith @*/
22947087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2295d763cef2SBarry Smith {
2296d763cef2SBarry Smith   PetscFunctionBegin;
22970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2298c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2299d763cef2SBarry Smith   ts->time_step = time_step;
2300d763cef2SBarry Smith   PetscFunctionReturn(0);
2301d763cef2SBarry Smith }
2302d763cef2SBarry Smith 
2303a43b19c4SJed Brown /*@
230449354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
230549354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
230649354f04SShri Abhyankar      or just return at the final time TS computed.
2307a43b19c4SJed Brown 
2308a43b19c4SJed Brown   Logically Collective on TS
2309a43b19c4SJed Brown 
2310d8d19677SJose E. Roman    Input Parameters:
2311a43b19c4SJed Brown +   ts - the time-step context
231249354f04SShri Abhyankar -   eftopt - exact final time option
2313a43b19c4SJed Brown 
2314feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2315feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2316feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2317feed9e9dSBarry Smith 
2318feed9e9dSBarry Smith    Options Database:
2319feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2320feed9e9dSBarry Smith 
2321ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2322ee346746SBarry Smith     then the final time you selected.
2323ee346746SBarry Smith 
2324a43b19c4SJed Brown    Level: beginner
2325a43b19c4SJed Brown 
2326f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2327a43b19c4SJed Brown @*/
232849354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2329a43b19c4SJed Brown {
2330a43b19c4SJed Brown   PetscFunctionBegin;
2331a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
233249354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
233349354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2334a43b19c4SJed Brown   PetscFunctionReturn(0);
2335a43b19c4SJed Brown }
2336a43b19c4SJed Brown 
2337d763cef2SBarry Smith /*@
2338f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2339f6953c82SLisandro Dalcin 
2340f6953c82SLisandro Dalcin    Not Collective
2341f6953c82SLisandro Dalcin 
2342f6953c82SLisandro Dalcin    Input Parameter:
2343f6953c82SLisandro Dalcin .  ts - the TS context
2344f6953c82SLisandro Dalcin 
2345f6953c82SLisandro Dalcin    Output Parameter:
2346f6953c82SLisandro Dalcin .  eftopt - exact final time option
2347f6953c82SLisandro Dalcin 
2348f6953c82SLisandro Dalcin    Level: beginner
2349f6953c82SLisandro Dalcin 
2350f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2351f6953c82SLisandro Dalcin @*/
2352f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2353f6953c82SLisandro Dalcin {
2354f6953c82SLisandro Dalcin   PetscFunctionBegin;
2355f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2356f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2357f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2358f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2359f6953c82SLisandro Dalcin }
2360f6953c82SLisandro Dalcin 
2361f6953c82SLisandro Dalcin /*@
2362d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2363d763cef2SBarry Smith 
2364d763cef2SBarry Smith    Not Collective
2365d763cef2SBarry Smith 
2366d763cef2SBarry Smith    Input Parameter:
2367d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2368d763cef2SBarry Smith 
2369d763cef2SBarry Smith    Output Parameter:
2370d763cef2SBarry Smith .  dt - the current timestep size
2371d763cef2SBarry Smith 
2372d763cef2SBarry Smith    Level: intermediate
2373d763cef2SBarry Smith 
2374aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2375d763cef2SBarry Smith 
2376d763cef2SBarry Smith @*/
23777087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2378d763cef2SBarry Smith {
2379d763cef2SBarry Smith   PetscFunctionBegin;
23800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2381f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2382d763cef2SBarry Smith   *dt = ts->time_step;
2383d763cef2SBarry Smith   PetscFunctionReturn(0);
2384d763cef2SBarry Smith }
2385d763cef2SBarry Smith 
2386d8e5e3e6SSatish Balay /*@
2387d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2388d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2389d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2390d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2391d763cef2SBarry Smith 
2392d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2393d763cef2SBarry Smith 
2394d763cef2SBarry Smith    Input Parameter:
2395d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2396d763cef2SBarry Smith 
2397d763cef2SBarry Smith    Output Parameter:
2398d763cef2SBarry Smith .  v - the vector containing the solution
2399d763cef2SBarry Smith 
240063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
240163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
240263e21af5SBarry Smith 
2403d763cef2SBarry Smith    Level: intermediate
2404d763cef2SBarry Smith 
24050ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2406d763cef2SBarry Smith 
2407d763cef2SBarry Smith @*/
24087087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2409d763cef2SBarry Smith {
2410d763cef2SBarry Smith   PetscFunctionBegin;
24110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24124482741eSBarry Smith   PetscValidPointer(v,2);
24138737fe31SLisandro Dalcin   *v = ts->vec_sol;
2414d763cef2SBarry Smith   PetscFunctionReturn(0);
2415d763cef2SBarry Smith }
2416d763cef2SBarry Smith 
241703fe5f5eSDebojyoti Ghosh /*@
2418b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
241903fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2420b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
242103fe5f5eSDebojyoti Ghosh    structure as the solution vector.
242203fe5f5eSDebojyoti Ghosh 
242303fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
242403fe5f5eSDebojyoti Ghosh 
242503fe5f5eSDebojyoti Ghosh    Parameters :
2426a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2427b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2428b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
242903fe5f5eSDebojyoti Ghosh        returned in v.
2430a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
243103fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2432b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
243303fe5f5eSDebojyoti Ghosh 
24344cdd57e5SDebojyoti Ghosh    Level: advanced
243503fe5f5eSDebojyoti Ghosh 
243603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
243703fe5f5eSDebojyoti Ghosh 
243803fe5f5eSDebojyoti Ghosh @*/
2439b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
244003fe5f5eSDebojyoti Ghosh {
244103fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
244203fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2443b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
244403fe5f5eSDebojyoti Ghosh   else {
24459566063dSJacob Faibussowitsch     PetscCall((*ts->ops->getsolutioncomponents)(ts,n,v));
244603fe5f5eSDebojyoti Ghosh   }
244703fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
244803fe5f5eSDebojyoti Ghosh }
244903fe5f5eSDebojyoti Ghosh 
24504cdd57e5SDebojyoti Ghosh /*@
24514cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24524cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24534cdd57e5SDebojyoti Ghosh 
24544cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24554cdd57e5SDebojyoti Ghosh 
24564cdd57e5SDebojyoti Ghosh    Parameters :
2457a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2458a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24594cdd57e5SDebojyoti Ghosh 
24604cdd57e5SDebojyoti Ghosh    Level: intermediate
24614cdd57e5SDebojyoti Ghosh 
24624cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24634cdd57e5SDebojyoti Ghosh 
24644cdd57e5SDebojyoti Ghosh @*/
24654cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24664cdd57e5SDebojyoti Ghosh {
24674cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24684cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2469f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24709566063dSJacob Faibussowitsch     PetscCall((*ts->ops->getauxsolution)(ts,v));
2471f6356ec7SDebojyoti Ghosh   } else {
24729566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(*v));
2473f6356ec7SDebojyoti Ghosh   }
24744cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24754cdd57e5SDebojyoti Ghosh }
24764cdd57e5SDebojyoti Ghosh 
24774cdd57e5SDebojyoti Ghosh /*@
24784cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24794cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24804cdd57e5SDebojyoti Ghosh 
24814cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24824cdd57e5SDebojyoti Ghosh 
24839657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24849657682dSDebojyoti Ghosh 
24854cdd57e5SDebojyoti Ghosh    Parameters :
2486a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2487657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2488a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
24894cdd57e5SDebojyoti Ghosh 
24904cdd57e5SDebojyoti Ghosh    Level: intermediate
24914cdd57e5SDebojyoti Ghosh 
249257df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24934cdd57e5SDebojyoti Ghosh 
24944cdd57e5SDebojyoti Ghosh @*/
24950a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24964cdd57e5SDebojyoti Ghosh {
24974cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24984cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2499f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25009566063dSJacob Faibussowitsch     PetscCall((*ts->ops->gettimeerror)(ts,n,v));
2501f6356ec7SDebojyoti Ghosh   } else {
25029566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(*v));
2503f6356ec7SDebojyoti Ghosh   }
25044cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25054cdd57e5SDebojyoti Ghosh }
25064cdd57e5SDebojyoti Ghosh 
250757df6a1bSDebojyoti Ghosh /*@
250857df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
250957df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
251057df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
251157df6a1bSDebojyoti Ghosh 
251257df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
251357df6a1bSDebojyoti Ghosh 
251457df6a1bSDebojyoti Ghosh    Parameters :
2515a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2516a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
251757df6a1bSDebojyoti Ghosh 
251857df6a1bSDebojyoti Ghosh    Level: intermediate
251957df6a1bSDebojyoti Ghosh 
252057df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
252157df6a1bSDebojyoti Ghosh 
252257df6a1bSDebojyoti Ghosh @*/
252357df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
252457df6a1bSDebojyoti Ghosh {
252557df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
252657df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25273c633725SBarry Smith   PetscCheck(ts->setupcalled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
252857df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
25299566063dSJacob Faibussowitsch     PetscCall((*ts->ops->settimeerror)(ts,v));
253057df6a1bSDebojyoti Ghosh   }
253157df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
253257df6a1bSDebojyoti Ghosh }
253357df6a1bSDebojyoti Ghosh 
2534bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2535d8e5e3e6SSatish Balay /*@
2536bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2537d763cef2SBarry Smith 
2538bdad233fSMatthew Knepley   Not collective
2539d763cef2SBarry Smith 
2540bdad233fSMatthew Knepley   Input Parameters:
2541bdad233fSMatthew Knepley + ts   - The TS
2542bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2543d763cef2SBarry Smith .vb
25440910c330SBarry Smith          U_t - A U = 0      (linear)
25450910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25460910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2547d763cef2SBarry Smith .ve
2548d763cef2SBarry Smith 
2549d763cef2SBarry Smith    Level: beginner
2550d763cef2SBarry Smith 
2551bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2552d763cef2SBarry Smith @*/
25537087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2554a7cc72afSBarry Smith {
2555d763cef2SBarry Smith   PetscFunctionBegin;
25560700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2557bdad233fSMatthew Knepley   ts->problem_type = type;
25589e2a6581SJed Brown   if (type == TS_LINEAR) {
25599e2a6581SJed Brown     SNES snes;
25609566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
25619566063dSJacob Faibussowitsch     PetscCall(SNESSetType(snes,SNESKSPONLY));
25629e2a6581SJed Brown   }
2563d763cef2SBarry Smith   PetscFunctionReturn(0);
2564d763cef2SBarry Smith }
2565d763cef2SBarry Smith 
2566bdad233fSMatthew Knepley /*@C
2567bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2568bdad233fSMatthew Knepley 
2569bdad233fSMatthew Knepley   Not collective
2570bdad233fSMatthew Knepley 
2571bdad233fSMatthew Knepley   Input Parameter:
2572bdad233fSMatthew Knepley . ts   - The TS
2573bdad233fSMatthew Knepley 
2574bdad233fSMatthew Knepley   Output Parameter:
2575bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2576bdad233fSMatthew Knepley .vb
2577089b2837SJed Brown          M U_t = A U
2578089b2837SJed Brown          M(t) U_t = A(t) U
2579b5abc632SBarry Smith          F(t,U,U_t)
2580bdad233fSMatthew Knepley .ve
2581bdad233fSMatthew Knepley 
2582bdad233fSMatthew Knepley    Level: beginner
2583bdad233fSMatthew Knepley 
2584bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2585bdad233fSMatthew Knepley @*/
25867087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2587a7cc72afSBarry Smith {
2588bdad233fSMatthew Knepley   PetscFunctionBegin;
25890700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25904482741eSBarry Smith   PetscValidIntPointer(type,2);
2591bdad233fSMatthew Knepley   *type = ts->problem_type;
2592bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2593bdad233fSMatthew Knepley }
2594d763cef2SBarry Smith 
2595303a5415SBarry Smith /*
2596303a5415SBarry 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()
2597303a5415SBarry Smith */
2598303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2599303a5415SBarry Smith {
2600303a5415SBarry Smith   PetscBool      isnone;
2601303a5415SBarry Smith 
2602303a5415SBarry Smith   PetscFunctionBegin;
26039566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts,&ts->adapt));
26049566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type));
2605303a5415SBarry Smith 
26069566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone));
2607303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2608303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2609303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2610303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2611303a5415SBarry Smith   }
2612303a5415SBarry Smith   PetscFunctionReturn(0);
2613303a5415SBarry Smith }
2614303a5415SBarry Smith 
2615d763cef2SBarry Smith /*@
2616303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2617d763cef2SBarry Smith 
2618d763cef2SBarry Smith    Collective on TS
2619d763cef2SBarry Smith 
2620d763cef2SBarry Smith    Input Parameter:
2621d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2622d763cef2SBarry Smith 
2623d763cef2SBarry Smith    Notes:
2624d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2625d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2626141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2627d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2628141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2629d763cef2SBarry Smith 
2630d763cef2SBarry Smith    Level: advanced
2631d763cef2SBarry Smith 
2632141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2633d763cef2SBarry Smith @*/
26347087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2635d763cef2SBarry Smith {
26366c6b9e74SPeter Brune   DM             dm;
26376c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2638d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2639cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26406c6b9e74SPeter Brune   TSIJacobian    ijac;
2641efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26426c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2643d763cef2SBarry Smith 
2644d763cef2SBarry Smith   PetscFunctionBegin;
26450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2646277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2647277b19d0SLisandro Dalcin 
26487adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
26499566063dSJacob Faibussowitsch     PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL));
26509566063dSJacob Faibussowitsch     PetscCall(TSSetType(ts,ifun ? TSBEULER : TSEULER));
2651d763cef2SBarry Smith   }
2652277b19d0SLisandro Dalcin 
26531a638600SStefano Zampini   if (!ts->vec_sol) {
26541a638600SStefano Zampini     if (ts->dm) {
26559566063dSJacob Faibussowitsch       PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol));
26561a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
26571a638600SStefano Zampini   }
2658277b19d0SLisandro Dalcin 
2659*4a658b32SHong Zhang   if (ts->tspan) {
2660*4a658b32SHong Zhang     if (!ts->tspan->vecs_sol) {
2661*4a658b32SHong Zhang       PetscCall(VecDuplicateVecs(ts->vec_sol,ts->tspan->num_span_times,&ts->tspan->vecs_sol));
2662*4a658b32SHong Zhang     }
2663*4a658b32SHong Zhang   }
2664298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
26659566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs));
2666298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2667298bade4SHong Zhang   }
2668298bade4SHong Zhang 
2669cd4cee2dSHong Zhang   if (ts->quadraturets) {
26709566063dSJacob Faibussowitsch     PetscCall(TSSetUp(ts->quadraturets));
26719566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vec_costintegrand));
26729566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand));
2673cd4cee2dSHong Zhang   }
2674cd4cee2dSHong Zhang 
26759566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL));
2676f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2677e1244c69SJed Brown     Mat Amat,Pmat;
2678e1244c69SJed Brown     SNES snes;
26799566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
26809566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL));
2681e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2682e1244c69SJed Brown      * have displaced the RHS matrix */
2683971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2684abc0d4abSBarry 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 */
26859566063dSJacob Faibussowitsch       PetscCall(MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat));
26869566063dSJacob Faibussowitsch       PetscCall(SNESSetJacobian(snes,Amat,NULL,NULL,NULL));
26879566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Amat));
2688e1244c69SJed Brown     }
2689971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
26909566063dSJacob Faibussowitsch       PetscCall(MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat));
26919566063dSJacob Faibussowitsch       PetscCall(SNESSetJacobian(snes,NULL,Pmat,NULL,NULL));
26929566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Pmat));
2693e1244c69SJed Brown     }
2694e1244c69SJed Brown   }
26952ffb9264SLisandro Dalcin 
26969566063dSJacob Faibussowitsch   PetscCall(TSGetAdapt(ts,&ts->adapt));
26979566063dSJacob Faibussowitsch   PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type));
26982ffb9264SLisandro Dalcin 
2699277b19d0SLisandro Dalcin   if (ts->ops->setup) {
27009566063dSJacob Faibussowitsch     PetscCall((*ts->ops->setup)(ts));
2701277b19d0SLisandro Dalcin   }
2702277b19d0SLisandro Dalcin 
27039566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTimeDefault(ts));
27042ffb9264SLisandro Dalcin 
2705a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27066c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27076c6b9e74SPeter Brune    */
27089566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
27099566063dSJacob Faibussowitsch   PetscCall(DMSNESGetFunction(dm,&func,NULL));
27106c6b9e74SPeter Brune   if (!func) {
27119566063dSJacob Faibussowitsch     PetscCall(DMSNESSetFunction(dm,SNESTSFormFunction,ts));
27126c6b9e74SPeter Brune   }
2713a6772fa2SLisandro 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.
27146c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27156c6b9e74SPeter Brune    */
27169566063dSJacob Faibussowitsch   PetscCall(DMSNESGetJacobian(dm,&jac,NULL));
27179566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,&ijac,NULL));
27189566063dSJacob Faibussowitsch   PetscCall(DMTSGetI2Jacobian(dm,&i2jac,NULL));
27199566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,&rhsjac,NULL));
2720efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27219566063dSJacob Faibussowitsch     PetscCall(DMSNESSetJacobian(dm,SNESTSFormJacobian,ts));
27226c6b9e74SPeter Brune   }
2723c0517034SDebojyoti Ghosh 
2724c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2725c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
27269566063dSJacob Faibussowitsch     PetscCall((*ts->ops->startingmethod)(ts));
2727c0517034SDebojyoti Ghosh   }
2728c0517034SDebojyoti Ghosh 
2729277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2730277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2731277b19d0SLisandro Dalcin }
2732277b19d0SLisandro Dalcin 
2733f6a906c0SBarry Smith /*@
2734277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2735277b19d0SLisandro Dalcin 
2736277b19d0SLisandro Dalcin    Collective on TS
2737277b19d0SLisandro Dalcin 
2738277b19d0SLisandro Dalcin    Input Parameter:
2739277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2740277b19d0SLisandro Dalcin 
2741277b19d0SLisandro Dalcin    Level: beginner
2742277b19d0SLisandro Dalcin 
2743277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2744277b19d0SLisandro Dalcin @*/
2745277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2746277b19d0SLisandro Dalcin {
27471d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2748277b19d0SLisandro Dalcin 
2749277b19d0SLisandro Dalcin   PetscFunctionBegin;
2750277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2751b18ea86cSHong Zhang 
2752277b19d0SLisandro Dalcin   if (ts->ops->reset) {
27539566063dSJacob Faibussowitsch     PetscCall((*ts->ops->reset)(ts));
2754277b19d0SLisandro Dalcin   }
27559566063dSJacob Faibussowitsch   if (ts->snes) PetscCall(SNESReset(ts->snes));
27569566063dSJacob Faibussowitsch   if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt));
2757bbd56ea5SKarl Rupp 
27589566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Arhs));
27599566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Brhs));
27609566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->Frhs));
27619566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_sol));
27629566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_dot));
27639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vatol));
27649566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vrtol));
27659566063dSJacob Faibussowitsch   PetscCall(VecDestroyVecs(ts->nwork,&ts->work));
2766bbd56ea5SKarl Rupp 
27679566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Jacprhs));
27689566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&ts->Jacp));
2769ecf68647SHong Zhang   if (ts->forward_solve) {
27709566063dSJacob Faibussowitsch     PetscCall(TSForwardReset(ts));
2771ecf68647SHong Zhang   }
2772cd4cee2dSHong Zhang   if (ts->quadraturets) {
27739566063dSJacob Faibussowitsch     PetscCall(TSReset(ts->quadraturets));
27749566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vec_costintegrand));
2775cd4cee2dSHong Zhang   }
27761d06f6b3SHong Zhang   while (ilink) {
27771d06f6b3SHong Zhang     next = ilink->next;
27789566063dSJacob Faibussowitsch     PetscCall(TSDestroy(&ilink->ts));
27799566063dSJacob Faibussowitsch     PetscCall(PetscFree(ilink->splitname));
27809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&ilink->is));
27819566063dSJacob Faibussowitsch     PetscCall(PetscFree(ilink));
27821d06f6b3SHong Zhang     ilink = next;
278387f4e208SHong Zhang   }
27846bf673ebSJoe Pusztay   ts->tsrhssplit = NULL;
2785545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2786*4a658b32SHong Zhang   if (ts->tspan) {
2787*4a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan->span_times));
2788*4a658b32SHong Zhang     PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol));
2789*4a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan));
2790*4a658b32SHong Zhang   }
2791277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2792d763cef2SBarry Smith   PetscFunctionReturn(0);
2793d763cef2SBarry Smith }
2794d763cef2SBarry Smith 
27951fb7b255SJunchao Zhang /*@C
2796d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2797d763cef2SBarry Smith    with TSCreate().
2798d763cef2SBarry Smith 
2799d763cef2SBarry Smith    Collective on TS
2800d763cef2SBarry Smith 
2801d763cef2SBarry Smith    Input Parameter:
2802d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2803d763cef2SBarry Smith 
2804d763cef2SBarry Smith    Level: beginner
2805d763cef2SBarry Smith 
2806d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2807d763cef2SBarry Smith @*/
28086bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2809d763cef2SBarry Smith {
2810d763cef2SBarry Smith   PetscFunctionBegin;
28116bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2812ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2813c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2814d763cef2SBarry Smith 
28159566063dSJacob Faibussowitsch   PetscCall(TSReset(*ts));
28169566063dSJacob Faibussowitsch   PetscCall(TSAdjointReset(*ts));
2817ecf68647SHong Zhang   if ((*ts)->forward_solve) {
28189566063dSJacob Faibussowitsch     PetscCall(TSForwardReset(*ts));
2819ecf68647SHong Zhang   }
2820e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
28219566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts));
28229566063dSJacob Faibussowitsch   if ((*ts)->ops->destroy) PetscCall((*(*ts)->ops->destroy)((*ts)));
28236d4c513bSLisandro Dalcin 
28249566063dSJacob Faibussowitsch   PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory));
2825bc952696SBarry Smith 
28269566063dSJacob Faibussowitsch   PetscCall(TSAdaptDestroy(&(*ts)->adapt));
28279566063dSJacob Faibussowitsch   PetscCall(TSEventDestroy(&(*ts)->event));
28286427ac75SLisandro Dalcin 
28299566063dSJacob Faibussowitsch   PetscCall(SNESDestroy(&(*ts)->snes));
28309566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*ts)->dm));
28319566063dSJacob Faibussowitsch   PetscCall(TSMonitorCancel((*ts)));
28329566063dSJacob Faibussowitsch   PetscCall(TSAdjointMonitorCancel((*ts)));
28336d4c513bSLisandro Dalcin 
28349566063dSJacob Faibussowitsch   PetscCall(TSDestroy(&(*ts)->quadraturets));
28359566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(ts));
2836d763cef2SBarry Smith   PetscFunctionReturn(0);
2837d763cef2SBarry Smith }
2838d763cef2SBarry Smith 
2839d8e5e3e6SSatish Balay /*@
2840d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2841d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2842d763cef2SBarry Smith 
2843d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2844d763cef2SBarry Smith 
2845d763cef2SBarry Smith    Input Parameter:
2846d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2847d763cef2SBarry Smith 
2848d763cef2SBarry Smith    Output Parameter:
2849d763cef2SBarry Smith .  snes - the nonlinear solver context
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith    Notes:
2852d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2853d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
285494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2855d763cef2SBarry Smith 
2856d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28570298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2858d763cef2SBarry Smith 
2859d763cef2SBarry Smith    Level: beginner
2860d763cef2SBarry Smith 
2861d763cef2SBarry Smith @*/
28627087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2863d763cef2SBarry Smith {
2864d763cef2SBarry Smith   PetscFunctionBegin;
28650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28664482741eSBarry Smith   PetscValidPointer(snes,2);
2867d372ba47SLisandro Dalcin   if (!ts->snes) {
28689566063dSJacob Faibussowitsch     PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes));
28699566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options));
28709566063dSJacob Faibussowitsch     PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts));
28719566063dSJacob Faibussowitsch     PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes));
28729566063dSJacob Faibussowitsch     PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1));
28739566063dSJacob Faibussowitsch     if (ts->dm) PetscCall(SNESSetDM(ts->snes,ts->dm));
28749e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28759566063dSJacob Faibussowitsch       PetscCall(SNESSetType(ts->snes,SNESKSPONLY));
28769e2a6581SJed Brown     }
2877d372ba47SLisandro Dalcin   }
2878d763cef2SBarry Smith   *snes = ts->snes;
2879d763cef2SBarry Smith   PetscFunctionReturn(0);
2880d763cef2SBarry Smith }
2881d763cef2SBarry Smith 
2882deb2cd25SJed Brown /*@
2883deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2884deb2cd25SJed Brown 
2885deb2cd25SJed Brown    Collective
2886deb2cd25SJed Brown 
2887d8d19677SJose E. Roman    Input Parameters:
2888deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2889deb2cd25SJed Brown -  snes - the nonlinear solver context
2890deb2cd25SJed Brown 
2891deb2cd25SJed Brown    Notes:
2892deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2893deb2cd25SJed Brown 
2894deb2cd25SJed Brown    Level: developer
2895deb2cd25SJed Brown 
2896deb2cd25SJed Brown @*/
2897deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2898deb2cd25SJed Brown {
2899d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2900deb2cd25SJed Brown 
2901deb2cd25SJed Brown   PetscFunctionBegin;
2902deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2903deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
29049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)snes));
29059566063dSJacob Faibussowitsch   PetscCall(SNESDestroy(&ts->snes));
2906bbd56ea5SKarl Rupp 
2907deb2cd25SJed Brown   ts->snes = snes;
2908bbd56ea5SKarl Rupp 
29099566063dSJacob Faibussowitsch   PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts));
29109566063dSJacob Faibussowitsch   PetscCall(SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL));
2911740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29129566063dSJacob Faibussowitsch     PetscCall(SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts));
2913740132f1SEmil Constantinescu   }
2914deb2cd25SJed Brown   PetscFunctionReturn(0);
2915deb2cd25SJed Brown }
2916deb2cd25SJed Brown 
2917d8e5e3e6SSatish Balay /*@
291894b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2919d763cef2SBarry Smith    a TS (timestepper) context.
2920d763cef2SBarry Smith 
292194b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2922d763cef2SBarry Smith 
2923d763cef2SBarry Smith    Input Parameter:
2924d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2925d763cef2SBarry Smith 
2926d763cef2SBarry Smith    Output Parameter:
292794b7f48cSBarry Smith .  ksp - the nonlinear solver context
2928d763cef2SBarry Smith 
2929d763cef2SBarry Smith    Notes:
293094b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2931d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2932d763cef2SBarry Smith    KSP and PC contexts as well.
2933d763cef2SBarry Smith 
293494b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29350298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2936d763cef2SBarry Smith 
2937d763cef2SBarry Smith    Level: beginner
2938d763cef2SBarry Smith 
2939d763cef2SBarry Smith @*/
29407087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2941d763cef2SBarry Smith {
2942089b2837SJed Brown   SNES           snes;
2943d372ba47SLisandro Dalcin 
2944d763cef2SBarry Smith   PetscFunctionBegin;
29450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29464482741eSBarry Smith   PetscValidPointer(ksp,2);
29473c633725SBarry Smith   PetscCheck(((PetscObject)ts)->type_name,PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
29483c633725SBarry Smith   PetscCheck(ts->problem_type == TS_LINEAR,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
29499566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
29509566063dSJacob Faibussowitsch   PetscCall(SNESGetKSP(snes,ksp));
2951d763cef2SBarry Smith   PetscFunctionReturn(0);
2952d763cef2SBarry Smith }
2953d763cef2SBarry Smith 
2954d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2955d763cef2SBarry Smith 
2956adb62b0dSMatthew Knepley /*@
2957618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2958618ce8baSLisandro Dalcin 
2959618ce8baSLisandro Dalcin    Logically Collective on TS
2960618ce8baSLisandro Dalcin 
2961618ce8baSLisandro Dalcin    Input Parameters:
2962618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2963618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2964618ce8baSLisandro Dalcin 
2965618ce8baSLisandro Dalcin    Options Database Keys:
2966618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2967618ce8baSLisandro Dalcin 
2968618ce8baSLisandro Dalcin    Notes:
2969618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2970618ce8baSLisandro Dalcin 
2971618ce8baSLisandro Dalcin    Level: intermediate
2972618ce8baSLisandro Dalcin 
2973618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2974618ce8baSLisandro Dalcin @*/
2975618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2976618ce8baSLisandro Dalcin {
2977618ce8baSLisandro Dalcin   PetscFunctionBegin;
2978618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2979618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
29803c633725SBarry Smith   PetscCheck(maxsteps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2981618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2982618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2983618ce8baSLisandro Dalcin }
2984618ce8baSLisandro Dalcin 
2985618ce8baSLisandro Dalcin /*@
2986618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2987618ce8baSLisandro Dalcin 
2988618ce8baSLisandro Dalcin    Not Collective
2989618ce8baSLisandro Dalcin 
2990618ce8baSLisandro Dalcin    Input Parameters:
2991618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2992618ce8baSLisandro Dalcin 
2993618ce8baSLisandro Dalcin    Output Parameter:
2994618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2995618ce8baSLisandro Dalcin 
2996618ce8baSLisandro Dalcin    Level: advanced
2997618ce8baSLisandro Dalcin 
2998618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2999618ce8baSLisandro Dalcin @*/
3000618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3001618ce8baSLisandro Dalcin {
3002618ce8baSLisandro Dalcin   PetscFunctionBegin;
3003618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3004618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3005618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3006618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3007618ce8baSLisandro Dalcin }
3008618ce8baSLisandro Dalcin 
3009618ce8baSLisandro Dalcin /*@
3010618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3011618ce8baSLisandro Dalcin 
3012618ce8baSLisandro Dalcin    Logically Collective on TS
3013618ce8baSLisandro Dalcin 
3014618ce8baSLisandro Dalcin    Input Parameters:
3015618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3016618ce8baSLisandro Dalcin -  maxtime - final time to step to
3017618ce8baSLisandro Dalcin 
3018618ce8baSLisandro Dalcin    Options Database Keys:
3019ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3020618ce8baSLisandro Dalcin 
3021618ce8baSLisandro Dalcin    Notes:
3022618ce8baSLisandro Dalcin    The default maximum time is 5.0
3023618ce8baSLisandro Dalcin 
3024618ce8baSLisandro Dalcin    Level: intermediate
3025618ce8baSLisandro Dalcin 
3026618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3027618ce8baSLisandro Dalcin @*/
3028618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3029618ce8baSLisandro Dalcin {
3030618ce8baSLisandro Dalcin   PetscFunctionBegin;
3031618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3032618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3033618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3034618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3035618ce8baSLisandro Dalcin }
3036618ce8baSLisandro Dalcin 
3037618ce8baSLisandro Dalcin /*@
3038618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3039618ce8baSLisandro Dalcin 
3040618ce8baSLisandro Dalcin    Not Collective
3041618ce8baSLisandro Dalcin 
3042618ce8baSLisandro Dalcin    Input Parameters:
3043618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3044618ce8baSLisandro Dalcin 
3045618ce8baSLisandro Dalcin    Output Parameter:
3046618ce8baSLisandro Dalcin .  maxtime - final time to step to
3047618ce8baSLisandro Dalcin 
3048618ce8baSLisandro Dalcin    Level: advanced
3049618ce8baSLisandro Dalcin 
3050618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3051618ce8baSLisandro Dalcin @*/
3052618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3053618ce8baSLisandro Dalcin {
3054618ce8baSLisandro Dalcin   PetscFunctionBegin;
3055618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3056618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3057618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3058618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3059618ce8baSLisandro Dalcin }
3060618ce8baSLisandro Dalcin 
3061618ce8baSLisandro Dalcin /*@
3062aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3063edc382c3SSatish Balay 
3064edc382c3SSatish Balay    Level: deprecated
3065edc382c3SSatish Balay 
3066aaa6c58dSLisandro Dalcin @*/
3067aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3068aaa6c58dSLisandro Dalcin {
3069aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3070aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30719566063dSJacob Faibussowitsch   PetscCall(TSSetTime(ts,initial_time));
30729566063dSJacob Faibussowitsch   PetscCall(TSSetTimeStep(ts,time_step));
3073aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3074aaa6c58dSLisandro Dalcin }
3075aaa6c58dSLisandro Dalcin 
3076aaa6c58dSLisandro Dalcin /*@
307719eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3078edc382c3SSatish Balay 
3079edc382c3SSatish Balay    Level: deprecated
3080edc382c3SSatish Balay 
3081adb62b0dSMatthew Knepley @*/
30827087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3083adb62b0dSMatthew Knepley {
3084adb62b0dSMatthew Knepley   PetscFunctionBegin;
30850700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3086abc0a331SBarry Smith   if (maxsteps) {
30874482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3088adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3089adb62b0dSMatthew Knepley   }
3090abc0a331SBarry Smith   if (maxtime) {
3091064a246eSJacob Faibussowitsch     PetscValidRealPointer(maxtime,3);
3092adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3093adb62b0dSMatthew Knepley   }
3094adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3095adb62b0dSMatthew Knepley }
3096adb62b0dSMatthew Knepley 
3097d763cef2SBarry Smith /*@
309819eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3099edc382c3SSatish Balay 
3100edc382c3SSatish Balay    Level: deprecated
3101edc382c3SSatish Balay 
3102d763cef2SBarry Smith @*/
31037087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3104d763cef2SBarry Smith {
3105d763cef2SBarry Smith   PetscFunctionBegin;
31060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3107c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3108064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveReal(ts,maxtime,3);
310939b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
311039b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3111d763cef2SBarry Smith   PetscFunctionReturn(0);
3112d763cef2SBarry Smith }
3113d763cef2SBarry Smith 
3114d763cef2SBarry Smith /*@
31155c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3116edc382c3SSatish Balay 
3117edc382c3SSatish Balay    Level: deprecated
3118edc382c3SSatish Balay 
31195c5f5948SLisandro Dalcin @*/
3120e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31215c5f5948SLisandro Dalcin 
312219eac22cSLisandro Dalcin /*@
31234f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3124edc382c3SSatish Balay 
3125edc382c3SSatish Balay    Level: deprecated
3126edc382c3SSatish Balay 
31274f4e0956SLisandro Dalcin @*/
31284f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31294f4e0956SLisandro Dalcin 
31304f4e0956SLisandro Dalcin /*@
3131d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3132d763cef2SBarry Smith    for use by the TS routines.
3133d763cef2SBarry Smith 
3134d083f849SBarry Smith    Logically Collective on TS
3135d763cef2SBarry Smith 
3136d763cef2SBarry Smith    Input Parameters:
3137d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31380910c330SBarry Smith -  u - the solution vector
3139d763cef2SBarry Smith 
3140d763cef2SBarry Smith    Level: beginner
3141d763cef2SBarry Smith 
31420ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3143d763cef2SBarry Smith @*/
31440910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3145d763cef2SBarry Smith {
3146496e6a7aSJed Brown   DM             dm;
31478737fe31SLisandro Dalcin 
3148d763cef2SBarry Smith   PetscFunctionBegin;
31490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31500910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31519566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)u));
31529566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&ts->vec_sol));
31530910c330SBarry Smith   ts->vec_sol = u;
3154bbd56ea5SKarl Rupp 
31559566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
31569566063dSJacob Faibussowitsch   PetscCall(DMShellSetGlobalVector(dm,u));
3157d763cef2SBarry Smith   PetscFunctionReturn(0);
3158d763cef2SBarry Smith }
3159d763cef2SBarry Smith 
3160ac226902SBarry Smith /*@C
3161000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31623f2090d5SJed Brown   called once at the beginning of each time step.
3163000e7ae3SMatthew Knepley 
31643f9fe445SBarry Smith   Logically Collective on TS
3165000e7ae3SMatthew Knepley 
3166000e7ae3SMatthew Knepley   Input Parameters:
3167000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3168000e7ae3SMatthew Knepley - func - The function
3169000e7ae3SMatthew Knepley 
3170000e7ae3SMatthew Knepley   Calling sequence of func:
317167b8a455SSatish Balay .vb
317267b8a455SSatish Balay   PetscErrorCode func (TS ts);
317367b8a455SSatish Balay .ve
3174000e7ae3SMatthew Knepley 
3175000e7ae3SMatthew Knepley   Level: intermediate
3176000e7ae3SMatthew Knepley 
3177dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3178000e7ae3SMatthew Knepley @*/
31797087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3180000e7ae3SMatthew Knepley {
3181000e7ae3SMatthew Knepley   PetscFunctionBegin;
31820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3183ae60f76fSBarry Smith   ts->prestep = func;
3184000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3185000e7ae3SMatthew Knepley }
3186000e7ae3SMatthew Knepley 
318709ee8438SJed Brown /*@
31883f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31893f2090d5SJed Brown 
31903f2090d5SJed Brown   Collective on TS
31913f2090d5SJed Brown 
31923f2090d5SJed Brown   Input Parameters:
31933f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
31943f2090d5SJed Brown 
31953f2090d5SJed Brown   Notes:
31963f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
31973f2090d5SJed Brown   so most users would not generally call this routine themselves.
31983f2090d5SJed Brown 
31993f2090d5SJed Brown   Level: developer
32003f2090d5SJed Brown 
32019be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32023f2090d5SJed Brown @*/
32037087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32043f2090d5SJed Brown {
32053f2090d5SJed Brown   PetscFunctionBegin;
32060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3207ae60f76fSBarry Smith   if (ts->prestep) {
32085efd42a4SStefano Zampini     Vec              U;
3209ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3210ef8d1ce0SJohann Rudi     PetscBool        sameObject;
32115efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32125efd42a4SStefano Zampini 
32139566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
32149566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetId((PetscObject)U,&idprev));
32159566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&sprev));
3216a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->prestep),ts);
32179566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
32189566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject));
32199566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&spost));
32209566063dSJacob Faibussowitsch     if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts));
3221312ce896SJed Brown   }
32223f2090d5SJed Brown   PetscFunctionReturn(0);
32233f2090d5SJed Brown }
32243f2090d5SJed Brown 
3225b8123daeSJed Brown /*@C
3226b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3227b8123daeSJed Brown   called once at the beginning of each stage.
3228b8123daeSJed Brown 
3229b8123daeSJed Brown   Logically Collective on TS
3230b8123daeSJed Brown 
3231b8123daeSJed Brown   Input Parameters:
3232b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3233b8123daeSJed Brown - func - The function
3234b8123daeSJed Brown 
3235b8123daeSJed Brown   Calling sequence of func:
323667b8a455SSatish Balay .vb
323767b8a455SSatish Balay   PetscErrorCode func(TS ts, PetscReal stagetime);
323867b8a455SSatish Balay .ve
3239b8123daeSJed Brown 
3240b8123daeSJed Brown   Level: intermediate
3241b8123daeSJed Brown 
3242b8123daeSJed Brown   Note:
3243b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
324480275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3245b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3246b8123daeSJed Brown 
32479be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3248b8123daeSJed Brown @*/
3249b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3250b8123daeSJed Brown {
3251b8123daeSJed Brown   PetscFunctionBegin;
3252b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3253ae60f76fSBarry Smith   ts->prestage = func;
3254b8123daeSJed Brown   PetscFunctionReturn(0);
3255b8123daeSJed Brown }
3256b8123daeSJed Brown 
32579be3e283SDebojyoti Ghosh /*@C
32589be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32599be3e283SDebojyoti Ghosh   called once at the end of each stage.
32609be3e283SDebojyoti Ghosh 
32619be3e283SDebojyoti Ghosh   Logically Collective on TS
32629be3e283SDebojyoti Ghosh 
32639be3e283SDebojyoti Ghosh   Input Parameters:
32649be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32659be3e283SDebojyoti Ghosh - func - The function
32669be3e283SDebojyoti Ghosh 
32679be3e283SDebojyoti Ghosh   Calling sequence of func:
326867b8a455SSatish Balay .vb
326967b8a455SSatish Balay   PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
327067b8a455SSatish Balay .ve
32719be3e283SDebojyoti Ghosh 
32729be3e283SDebojyoti Ghosh   Level: intermediate
32739be3e283SDebojyoti Ghosh 
32749be3e283SDebojyoti Ghosh   Note:
32759be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
327680275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32779be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32789be3e283SDebojyoti Ghosh 
32799be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32809be3e283SDebojyoti Ghosh @*/
32819be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32829be3e283SDebojyoti Ghosh {
32839be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32849be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32859be3e283SDebojyoti Ghosh   ts->poststage = func;
32869be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32879be3e283SDebojyoti Ghosh }
32889be3e283SDebojyoti Ghosh 
3289c688d042SShri Abhyankar /*@C
3290c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3291c688d042SShri Abhyankar   called once at the end of each step evaluation.
3292c688d042SShri Abhyankar 
3293c688d042SShri Abhyankar   Logically Collective on TS
3294c688d042SShri Abhyankar 
3295c688d042SShri Abhyankar   Input Parameters:
3296c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3297c688d042SShri Abhyankar - func - The function
3298c688d042SShri Abhyankar 
3299c688d042SShri Abhyankar   Calling sequence of func:
330067b8a455SSatish Balay .vb
330167b8a455SSatish Balay   PetscErrorCode func(TS ts);
330267b8a455SSatish Balay .ve
3303c688d042SShri Abhyankar 
3304c688d042SShri Abhyankar   Level: intermediate
3305c688d042SShri Abhyankar 
3306c688d042SShri Abhyankar   Note:
33071785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33081785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3309e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3310e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3311e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3312c688d042SShri Abhyankar 
3313c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3314c688d042SShri Abhyankar @*/
3315c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3316c688d042SShri Abhyankar {
3317c688d042SShri Abhyankar   PetscFunctionBegin;
3318c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3319c688d042SShri Abhyankar   ts->postevaluate = func;
3320c688d042SShri Abhyankar   PetscFunctionReturn(0);
3321c688d042SShri Abhyankar }
3322c688d042SShri Abhyankar 
3323b8123daeSJed Brown /*@
3324b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3325b8123daeSJed Brown 
3326b8123daeSJed Brown   Collective on TS
3327b8123daeSJed Brown 
3328b8123daeSJed Brown   Input Parameters:
3329b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33309be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3331b8123daeSJed Brown 
3332b8123daeSJed Brown   Notes:
3333b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3334b8123daeSJed Brown   most users would not generally call this routine themselves.
3335b8123daeSJed Brown 
3336b8123daeSJed Brown   Level: developer
3337b8123daeSJed Brown 
33389be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3339b8123daeSJed Brown @*/
3340b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3341b8123daeSJed Brown {
3342b8123daeSJed Brown   PetscFunctionBegin;
3343b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3344ae60f76fSBarry Smith   if (ts->prestage) {
3345a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->prestage),ts,stagetime);
3346b8123daeSJed Brown   }
3347b8123daeSJed Brown   PetscFunctionReturn(0);
3348b8123daeSJed Brown }
3349b8123daeSJed Brown 
33509be3e283SDebojyoti Ghosh /*@
33519be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33529be3e283SDebojyoti Ghosh 
33539be3e283SDebojyoti Ghosh   Collective on TS
33549be3e283SDebojyoti Ghosh 
33559be3e283SDebojyoti Ghosh   Input Parameters:
33569be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33579be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33589be3e283SDebojyoti Ghosh   stageindex  - Stage number
33599be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33609be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33619be3e283SDebojyoti Ghosh 
33629be3e283SDebojyoti Ghosh   Notes:
33639be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33649be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33659be3e283SDebojyoti Ghosh 
33669be3e283SDebojyoti Ghosh   Level: developer
33679be3e283SDebojyoti Ghosh 
33689be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33699be3e283SDebojyoti Ghosh @*/
33709be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33719be3e283SDebojyoti Ghosh {
33729be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33739be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33744beae5d8SLisandro Dalcin   if (ts->poststage) {
3375a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->poststage),ts,stagetime,stageindex,Y);
33769be3e283SDebojyoti Ghosh   }
33779be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33789be3e283SDebojyoti Ghosh }
33799be3e283SDebojyoti Ghosh 
3380c688d042SShri Abhyankar /*@
3381c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3382c688d042SShri Abhyankar 
3383c688d042SShri Abhyankar   Collective on TS
3384c688d042SShri Abhyankar 
3385c688d042SShri Abhyankar   Input Parameters:
3386c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3387c688d042SShri Abhyankar 
3388c688d042SShri Abhyankar   Notes:
3389c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3390c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3391c688d042SShri Abhyankar 
3392c688d042SShri Abhyankar   Level: developer
3393c688d042SShri Abhyankar 
3394c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3395c688d042SShri Abhyankar @*/
3396c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3397c688d042SShri Abhyankar {
3398c688d042SShri Abhyankar   PetscFunctionBegin;
3399c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3400c688d042SShri Abhyankar   if (ts->postevaluate) {
3401dcb233daSLisandro Dalcin     Vec              U;
3402dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3403dcb233daSLisandro Dalcin 
34049566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
34059566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&sprev));
3406a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->postevaluate),ts);
34079566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&spost));
34089566063dSJacob Faibussowitsch     if (sprev != spost) PetscCall(TSRestartStep(ts));
3409c688d042SShri Abhyankar   }
3410c688d042SShri Abhyankar   PetscFunctionReturn(0);
3411c688d042SShri Abhyankar }
3412c688d042SShri Abhyankar 
3413ac226902SBarry Smith /*@C
3414000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34153f2090d5SJed Brown   called once at the end of each time step.
3416000e7ae3SMatthew Knepley 
34173f9fe445SBarry Smith   Logically Collective on TS
3418000e7ae3SMatthew Knepley 
3419000e7ae3SMatthew Knepley   Input Parameters:
3420000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3421000e7ae3SMatthew Knepley - func - The function
3422000e7ae3SMatthew Knepley 
3423000e7ae3SMatthew Knepley   Calling sequence of func:
3424b8123daeSJed Brown $ func (TS ts);
3425000e7ae3SMatthew Knepley 
34261785ff2aSShri Abhyankar   Notes:
34271785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34281785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34291785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34301785ff2aSShri Abhyankar 
3431000e7ae3SMatthew Knepley   Level: intermediate
3432000e7ae3SMatthew Knepley 
3433dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3434000e7ae3SMatthew Knepley @*/
34357087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3436000e7ae3SMatthew Knepley {
3437000e7ae3SMatthew Knepley   PetscFunctionBegin;
34380700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3439ae60f76fSBarry Smith   ts->poststep = func;
3440000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3441000e7ae3SMatthew Knepley }
3442000e7ae3SMatthew Knepley 
344309ee8438SJed Brown /*@
34443f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34453f2090d5SJed Brown 
34463f2090d5SJed Brown   Collective on TS
34473f2090d5SJed Brown 
34483f2090d5SJed Brown   Input Parameters:
34493f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34503f2090d5SJed Brown 
34513f2090d5SJed Brown   Notes:
34523f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34533f2090d5SJed Brown   so most users would not generally call this routine themselves.
34543f2090d5SJed Brown 
34553f2090d5SJed Brown   Level: developer
34563f2090d5SJed Brown 
34573f2090d5SJed Brown @*/
34587087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34593f2090d5SJed Brown {
34603f2090d5SJed Brown   PetscFunctionBegin;
34610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3462ae60f76fSBarry Smith   if (ts->poststep) {
34635efd42a4SStefano Zampini     Vec              U;
3464ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3465ef8d1ce0SJohann Rudi     PetscBool        sameObject;
34665efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34675efd42a4SStefano Zampini 
34689566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
34699566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetId((PetscObject)U,&idprev));
34709566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&sprev));
3471a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->poststep),ts);
34729566063dSJacob Faibussowitsch     PetscCall(TSGetSolution(ts,&U));
34739566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject));
34749566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)U,&spost));
34759566063dSJacob Faibussowitsch     if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts));
347672ac3e02SJed Brown   }
34773f2090d5SJed Brown   PetscFunctionReturn(0);
34783f2090d5SJed Brown }
34793f2090d5SJed Brown 
3480cd652676SJed Brown /*@
3481cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3482cd652676SJed Brown 
3483cd652676SJed Brown    Collective on TS
3484cd652676SJed Brown 
34854165533cSJose E. Roman    Input Parameters:
3486cd652676SJed Brown +  ts - time stepping context
3487cd652676SJed Brown -  t - time to interpolate to
3488cd652676SJed Brown 
34894165533cSJose E. Roman    Output Parameter:
34900910c330SBarry Smith .  U - state at given time
3491cd652676SJed Brown 
3492cd652676SJed Brown    Level: intermediate
3493cd652676SJed Brown 
3494cd652676SJed Brown    Developer Notes:
3495cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3496cd652676SJed Brown 
3497874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3498cd652676SJed Brown @*/
34990910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3500cd652676SJed Brown {
3501cd652676SJed Brown   PetscFunctionBegin;
3502cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3503b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
35043c633725SBarry 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);
35053c633725SBarry Smith   PetscCheck(ts->ops->interpolate,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35069566063dSJacob Faibussowitsch   PetscCall((*ts->ops->interpolate)(ts,t,U));
3507cd652676SJed Brown   PetscFunctionReturn(0);
3508cd652676SJed Brown }
3509cd652676SJed Brown 
3510d763cef2SBarry Smith /*@
35116d9e5789SSean Farley    TSStep - Steps one time step
3512d763cef2SBarry Smith 
3513d763cef2SBarry Smith    Collective on TS
3514d763cef2SBarry Smith 
3515d763cef2SBarry Smith    Input Parameter:
3516d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3517d763cef2SBarry Smith 
351827829d71SBarry Smith    Level: developer
3519d763cef2SBarry Smith 
3520b8123daeSJed Brown    Notes:
352127829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
352227829d71SBarry Smith 
3523b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3524b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3525b8123daeSJed Brown 
352619eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
352719eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
352825cb2221SBarry Smith 
35299be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3530d763cef2SBarry Smith @*/
3531193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3532d763cef2SBarry Smith {
3533dfbe8321SBarry Smith   PetscErrorCode   ierr;
3534fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3535be5899b3SLisandro Dalcin   PetscReal        ptime;
3536d763cef2SBarry Smith 
3537d763cef2SBarry Smith   PetscFunctionBegin;
35380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3539f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3540fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3541f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3542f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3543f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3544f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
35459566063dSJacob Faibussowitsch                                 "  year          = {2018}\n}\n",&cite);PetscCall(ierr);
3546fffbeea8SBarry Smith 
35479566063dSJacob Faibussowitsch   PetscCall(TSSetUp(ts));
35489566063dSJacob Faibussowitsch   PetscCall(TSTrajectorySetUp(ts->trajectory,ts));
3549d405a339SMatthew Knepley 
35503c633725SBarry Smith   PetscCheck(ts->ops->step,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
35513c633725SBarry 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>");
35523c633725SBarry 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()");
35533c633725SBarry 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");
3554a6772fa2SLisandro Dalcin 
3555be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3556be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
35572808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3558fc8dbba5SLisandro Dalcin 
35599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TS_Step,ts,0,0,0));
35609566063dSJacob Faibussowitsch   PetscCall((*ts->ops->step)(ts));
35619566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TS_Step,ts,0,0,0));
3562fc8dbba5SLisandro Dalcin 
3563fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3564be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
35652808aa04SLisandro Dalcin     ts->steps++;
3566be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
356728d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3568*4a658b32SHong 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++]));
3569d2daff3dSHong Zhang   }
3570fc8dbba5SLisandro Dalcin 
3571fc8dbba5SLisandro Dalcin   if (!ts->reason) {
357208c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
357308c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
357408c7845fSBarry Smith   }
3575fc8dbba5SLisandro Dalcin 
35763c633725SBarry 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]);
35773c633725SBarry Smith   PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed,PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
357808c7845fSBarry Smith   PetscFunctionReturn(0);
357908c7845fSBarry Smith }
358008c7845fSBarry Smith 
358108c7845fSBarry Smith /*@
35827cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
35837cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
35847cbde773SLisandro Dalcin 
35857cbde773SLisandro Dalcin    Collective on TS
35867cbde773SLisandro Dalcin 
35874165533cSJose E. Roman    Input Parameters:
35887cbde773SLisandro Dalcin +  ts - time stepping context
358997bb3fdcSJose E. Roman -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
35907cbde773SLisandro Dalcin 
359197bb3fdcSJose E. Roman    Input/Output Parameter:
359297bb3fdcSJose E. Roman .  order - optional, desired order for the error evaluation or PETSC_DECIDE;
359397bb3fdcSJose E. Roman            on output, the actual order of the error evaluation
359497bb3fdcSJose E. Roman 
359597bb3fdcSJose E. Roman    Output Parameter:
359697bb3fdcSJose E. Roman .  wlte - the weighted local truncation error norm
35977cbde773SLisandro Dalcin 
35987cbde773SLisandro Dalcin    Level: advanced
35997cbde773SLisandro Dalcin 
36007cbde773SLisandro Dalcin    Notes:
36017cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36027cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36037cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36047cbde773SLisandro Dalcin 
36057cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36067cbde773SLisandro Dalcin @*/
36077cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36087cbde773SLisandro Dalcin {
36097cbde773SLisandro Dalcin   PetscFunctionBegin;
36107cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36117cbde773SLisandro Dalcin   PetscValidType(ts,1);
3612064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveEnum(ts,wnormtype,2);
36137cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36147cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36157cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36163c633725SBarry Smith   PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36173c633725SBarry Smith   PetscCheck(ts->ops->evaluatewlte,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36189566063dSJacob Faibussowitsch   PetscCall((*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte));
36197cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36207cbde773SLisandro Dalcin }
36217cbde773SLisandro Dalcin 
362205175c85SJed Brown /*@
362305175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
362405175c85SJed Brown 
36251c3436cfSJed Brown    Collective on TS
362605175c85SJed Brown 
36274165533cSJose E. Roman    Input Parameters:
36281c3436cfSJed Brown +  ts - time stepping context
36291c3436cfSJed Brown .  order - desired order of accuracy
36300298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
363105175c85SJed Brown 
36324165533cSJose E. Roman    Output Parameter:
36330910c330SBarry Smith .  U - state at the end of the current step
363405175c85SJed Brown 
363505175c85SJed Brown    Level: advanced
363605175c85SJed Brown 
3637108c343cSJed Brown    Notes:
3638108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3639108c343cSJed 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.
3640108c343cSJed Brown 
36411c3436cfSJed Brown .seealso: TSStep(), TSAdapt
364205175c85SJed Brown @*/
36430910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
364405175c85SJed Brown {
364505175c85SJed Brown   PetscFunctionBegin;
364605175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
364705175c85SJed Brown   PetscValidType(ts,1);
36480910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
36493c633725SBarry Smith   PetscCheck(ts->ops->evaluatestep,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
36509566063dSJacob Faibussowitsch   PetscCall((*ts->ops->evaluatestep)(ts,order,U,done));
365105175c85SJed Brown   PetscFunctionReturn(0);
365205175c85SJed Brown }
365305175c85SJed Brown 
3654aad739acSMatthew G. Knepley /*@C
36552e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3656aad739acSMatthew G. Knepley 
3657aad739acSMatthew G. Knepley   Not collective
3658aad739acSMatthew G. Knepley 
36594165533cSJose E. Roman   Input Parameter:
3660aad739acSMatthew G. Knepley . ts        - time stepping context
3661aad739acSMatthew G. Knepley 
36624165533cSJose E. Roman   Output Parameter:
36632e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3664aad739acSMatthew G. Knepley 
3665aad739acSMatthew G. Knepley    Level: advanced
3666aad739acSMatthew G. Knepley 
3667aad739acSMatthew G. Knepley    Notes:
3668aad739acSMatthew G. Knepley    The calling sequence for the function is
36692e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3670aad739acSMatthew G. Knepley $ ts - The timestepping context
36712e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3672aad739acSMatthew G. Knepley 
36732e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3674aad739acSMatthew G. Knepley @*/
36752e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3676aad739acSMatthew G. Knepley {
3677aad739acSMatthew G. Knepley   PetscFunctionBegin;
3678aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
36792e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
36802e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3681aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3682aad739acSMatthew G. Knepley }
3683aad739acSMatthew G. Knepley 
3684aad739acSMatthew G. Knepley /*@C
36852e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3686aad739acSMatthew G. Knepley 
3687aad739acSMatthew G. Knepley   Logically collective on ts
3688aad739acSMatthew G. Knepley 
36894165533cSJose E. Roman   Input Parameters:
3690aad739acSMatthew G. Knepley + ts        - time stepping context
36912e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3692aad739acSMatthew G. Knepley 
3693aad739acSMatthew G. Knepley   Level: advanced
3694aad739acSMatthew G. Knepley 
3695a96d6ef6SBarry Smith   Calling sequence for initCondition:
3696a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3697a96d6ef6SBarry Smith 
3698a96d6ef6SBarry Smith + ts - The timestepping context
3699a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3700aad739acSMatthew G. Knepley 
37012e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3702aad739acSMatthew G. Knepley @*/
37032e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3704aad739acSMatthew G. Knepley {
3705aad739acSMatthew G. Knepley   PetscFunctionBegin;
3706aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
37072e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
37082e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3709aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3710aad739acSMatthew G. Knepley }
3711aad739acSMatthew G. Knepley 
3712aad739acSMatthew G. Knepley /*@
37132e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3714aad739acSMatthew G. Knepley 
3715aad739acSMatthew G. Knepley   Collective on ts
3716aad739acSMatthew G. Knepley 
37174165533cSJose E. Roman   Input Parameters:
3718aad739acSMatthew G. Knepley + ts - time stepping context
37192e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3720aad739acSMatthew G. Knepley 
3721aad739acSMatthew G. Knepley   Level: advanced
3722aad739acSMatthew G. Knepley 
37232e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3724aad739acSMatthew G. Knepley @*/
37252e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3726aad739acSMatthew G. Knepley {
3727aad739acSMatthew G. Knepley   PetscFunctionBegin;
3728aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3729aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
37309566063dSJacob Faibussowitsch   if (ts->ops->initcondition) PetscCall((*ts->ops->initcondition)(ts, u));
3731aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3732aad739acSMatthew G. Knepley }
3733aad739acSMatthew G. Knepley 
3734aad739acSMatthew G. Knepley /*@C
3735aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3736aad739acSMatthew G. Knepley 
3737aad739acSMatthew G. Knepley   Not collective
3738aad739acSMatthew G. Knepley 
37394165533cSJose E. Roman   Input Parameter:
3740aad739acSMatthew G. Knepley . ts         - time stepping context
3741aad739acSMatthew G. Knepley 
37424165533cSJose E. Roman   Output Parameter:
3743aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3744aad739acSMatthew G. Knepley 
3745aad739acSMatthew G. Knepley   Level: advanced
3746aad739acSMatthew G. Knepley 
3747a96d6ef6SBarry Smith   Calling sequence for exactError:
3748a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3749a96d6ef6SBarry Smith 
3750a96d6ef6SBarry Smith + ts - The timestepping context
3751a96d6ef6SBarry Smith . u  - The approximate solution vector
3752a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3753aad739acSMatthew G. Knepley 
3754aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3755aad739acSMatthew G. Knepley @*/
3756aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3757aad739acSMatthew G. Knepley {
3758aad739acSMatthew G. Knepley   PetscFunctionBegin;
3759aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3760aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3761aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3762aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3763aad739acSMatthew G. Knepley }
3764aad739acSMatthew G. Knepley 
3765aad739acSMatthew G. Knepley /*@C
3766aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3767aad739acSMatthew G. Knepley 
3768aad739acSMatthew G. Knepley   Logically collective on ts
3769aad739acSMatthew G. Knepley 
37704165533cSJose E. Roman   Input Parameters:
3771aad739acSMatthew G. Knepley + ts         - time stepping context
3772aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3773aad739acSMatthew G. Knepley 
3774aad739acSMatthew G. Knepley   Level: advanced
3775aad739acSMatthew G. Knepley 
3776a96d6ef6SBarry Smith   Calling sequence for exactError:
3777a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3778a96d6ef6SBarry Smith 
3779a96d6ef6SBarry Smith + ts - The timestepping context
3780a96d6ef6SBarry Smith . u  - The approximate solution vector
3781a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3782aad739acSMatthew G. Knepley 
3783aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3784aad739acSMatthew G. Knepley @*/
3785aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3786aad739acSMatthew G. Knepley {
3787aad739acSMatthew G. Knepley   PetscFunctionBegin;
3788aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3789f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3790aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3791aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3792aad739acSMatthew G. Knepley }
3793aad739acSMatthew G. Knepley 
3794aad739acSMatthew G. Knepley /*@
3795aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3796aad739acSMatthew G. Knepley 
3797aad739acSMatthew G. Knepley   Collective on ts
3798aad739acSMatthew G. Knepley 
37994165533cSJose E. Roman   Input Parameters:
3800aad739acSMatthew G. Knepley + ts - time stepping context
3801aad739acSMatthew G. Knepley . u  - The approximate solution
3802aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3803aad739acSMatthew G. Knepley 
3804aad739acSMatthew G. Knepley   Level: advanced
3805aad739acSMatthew G. Knepley 
38062e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3807aad739acSMatthew G. Knepley @*/
3808aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3809aad739acSMatthew G. Knepley {
3810aad739acSMatthew G. Knepley   PetscFunctionBegin;
3811aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3812aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3813aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
38149566063dSJacob Faibussowitsch   if (ts->ops->exacterror) PetscCall((*ts->ops->exacterror)(ts, u, e));
3815aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3816aad739acSMatthew G. Knepley }
3817aad739acSMatthew G. Knepley 
3818b1cb36f3SHong Zhang /*@
38196a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
38206a4d4014SLisandro Dalcin 
38216a4d4014SLisandro Dalcin    Collective on TS
38226a4d4014SLisandro Dalcin 
3823d8d19677SJose E. Roman    Input Parameters:
38246a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
382563e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
382663e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
38275a3a76d0SJed Brown 
38286a4d4014SLisandro Dalcin    Level: beginner
38296a4d4014SLisandro Dalcin 
38305a3a76d0SJed Brown    Notes:
38315a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
38325a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
38335a3a76d0SJed Brown    stepped over the final time.
38345a3a76d0SJed Brown 
383563e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
38366a4d4014SLisandro Dalcin @*/
3837cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
38386a4d4014SLisandro Dalcin {
3839b06615a5SLisandro Dalcin   Vec               solution;
3840f22f69f0SBarry Smith 
38416a4d4014SLisandro Dalcin   PetscFunctionBegin;
38420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3843f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3844303a5415SBarry Smith 
38459566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTimeDefault(ts));
3846ee41a567SStefano 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 */
38470910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
38489566063dSJacob Faibussowitsch       PetscCall(VecDuplicate(u,&solution));
38499566063dSJacob Faibussowitsch       PetscCall(TSSetSolution(ts,solution));
38509566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&solution)); /* grant ownership */
38515a3a76d0SJed Brown     }
38529566063dSJacob Faibussowitsch     PetscCall(VecCopy(u,ts->vec_sol));
38533c633725SBarry Smith     PetscCheck(!ts->forward_solve,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3854bbd56ea5SKarl Rupp   } else if (u) {
38559566063dSJacob Faibussowitsch     PetscCall(TSSetSolution(ts,u));
38565a3a76d0SJed Brown   }
38579566063dSJacob Faibussowitsch   PetscCall(TSSetUp(ts));
38589566063dSJacob Faibussowitsch   PetscCall(TSTrajectorySetUp(ts->trajectory,ts));
3859a6772fa2SLisandro Dalcin 
38603c633725SBarry 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>");
38613c633725SBarry 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()");
38623c633725SBarry 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");
3863*4a658b32SHong 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");
3864*4a658b32SHong Zhang 
3865*4a658b32SHong Zhang   if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[0],10*PETSC_MACHINE_EPSILON,0)) { /* starting point in time span */
3866*4a658b32SHong Zhang     PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[0]));
3867*4a658b32SHong Zhang     ts->tspan->spanctr = 1;
3868*4a658b32SHong Zhang   }
3869a6772fa2SLisandro Dalcin 
3870715f1b00SHong Zhang   if (ts->forward_solve) {
38719566063dSJacob Faibussowitsch     PetscCall(TSForwardSetUp(ts));
3872715f1b00SHong Zhang   }
3873715f1b00SHong Zhang 
3874e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3875715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3876e7069c78SShri      TSTrajectory to incorrectly save the output files
3877e7069c78SShri   */
3878715f1b00SHong Zhang   /* reset time step and iteration counters */
38792808aa04SLisandro Dalcin   if (!ts->steps) {
38805ef26d82SJed Brown     ts->ksp_its           = 0;
38815ef26d82SJed Brown     ts->snes_its          = 0;
3882c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3883c610991cSLisandro Dalcin     ts->reject            = 0;
38842808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
38852808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
38867d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
38872808aa04SLisandro Dalcin   }
3888e97c63d7SStefano Zampini 
3889*4a658b32SHong Zhang   /* make sure initial time step does not overshoot final time or the next point in tspan */
3890e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
3891*4a658b32SHong Zhang     PetscReal maxdt;
3892e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
3893e97c63d7SStefano Zampini 
3894*4a658b32SHong Zhang     if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime;
3895*4a658b32SHong Zhang     else maxdt = ts->max_time - ts->ptime;
3896e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
3897e97c63d7SStefano Zampini   }
3898193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3899193ac0bcSJed Brown 
3900900f6b5bSMatthew G. Knepley   {
3901900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
3902900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
3903900f6b5bSMatthew G. Knepley     PetscBool         flg;
3904900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
3905900f6b5bSMatthew G. Knepley 
3906900f6b5bSMatthew G. Knepley     if (!incall) {
3907900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
39089566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg));
3909900f6b5bSMatthew G. Knepley       if (flg) {
3910900f6b5bSMatthew G. Knepley         PetscConvEst conv;
3911900f6b5bSMatthew G. Knepley         DM           dm;
3912900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
3913900f6b5bSMatthew G. Knepley         PetscInt     Nf;
3914f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
3915900f6b5bSMatthew G. Knepley 
3916900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
39179566063dSJacob Faibussowitsch         PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg));
39189566063dSJacob Faibussowitsch         PetscCall(TSGetDM(ts, &dm));
39199566063dSJacob Faibussowitsch         PetscCall(DMGetNumFields(dm, &Nf));
39209566063dSJacob Faibussowitsch         PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha));
39219566063dSJacob Faibussowitsch         PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv));
39229566063dSJacob Faibussowitsch         PetscCall(PetscConvEstUseTS(conv, checkTemporal));
39239566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetSolver(conv, (PetscObject) ts));
39249566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetFromOptions(conv));
39259566063dSJacob Faibussowitsch         PetscCall(PetscConvEstSetUp(conv));
39269566063dSJacob Faibussowitsch         PetscCall(PetscConvEstGetConvRate(conv, alpha));
39279566063dSJacob Faibussowitsch         PetscCall(PetscViewerPushFormat(viewer, format));
39289566063dSJacob Faibussowitsch         PetscCall(PetscConvEstRateView(conv, alpha, viewer));
39299566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
39309566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
39319566063dSJacob Faibussowitsch         PetscCall(PetscConvEstDestroy(&conv));
39329566063dSJacob Faibussowitsch         PetscCall(PetscFree(alpha));
3933900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
3934900f6b5bSMatthew G. Knepley       }
3935900f6b5bSMatthew G. Knepley     }
3936900f6b5bSMatthew G. Knepley   }
3937900f6b5bSMatthew G. Knepley 
39389566063dSJacob Faibussowitsch   PetscCall(TSViewFromOptions(ts,NULL,"-ts_view_pre"));
3939f05ece33SBarry Smith 
3940193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
39419566063dSJacob Faibussowitsch     PetscCall((*ts->ops->solve)(ts));
39429566063dSJacob Faibussowitsch     if (u) PetscCall(VecCopy(ts->vec_sol,u));
3943cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3944a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3945be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3946db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3947db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3948e7069c78SShri 
39492808aa04SLisandro Dalcin     if (!ts->steps) {
39509566063dSJacob Faibussowitsch       PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol));
39519566063dSJacob Faibussowitsch       PetscCall(TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol));
39522808aa04SLisandro Dalcin     }
39536427ac75SLisandro Dalcin 
3954e1a7a14fSJed Brown     while (!ts->reason) {
39559566063dSJacob Faibussowitsch       PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol));
39569687d888SLisandro Dalcin       if (!ts->steprollback) {
39579566063dSJacob Faibussowitsch         PetscCall(TSPreStep(ts));
39589687d888SLisandro Dalcin       }
39599566063dSJacob Faibussowitsch       PetscCall(TSStep(ts));
3960f3b1f45cSBarry Smith       if (ts->testjacobian) {
39619566063dSJacob Faibussowitsch         PetscCall(TSRHSJacobianTest(ts,NULL));
3962f3b1f45cSBarry Smith       }
3963f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
39649566063dSJacob Faibussowitsch         PetscCall(TSRHSJacobianTestTranspose(ts,NULL));
3965f3b1f45cSBarry Smith       }
3966cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
39677b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
39689566063dSJacob Faibussowitsch         PetscCall(TSForwardCostIntegral(ts));
39697b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
3970b1cb36f3SHong Zhang       }
397158818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
39727b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
39739566063dSJacob Faibussowitsch         PetscCall(TSForwardStep(ts));
39747b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
3975715f1b00SHong Zhang       }
39769566063dSJacob Faibussowitsch       PetscCall(TSPostEvaluate(ts));
39779566063dSJacob 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. */
397858818c2dSLisandro Dalcin       if (ts->steprollback) {
39799566063dSJacob Faibussowitsch         PetscCall(TSPostEvaluate(ts));
398058818c2dSLisandro Dalcin       }
3981e783b05fSHong Zhang       if (!ts->steprollback) {
39829566063dSJacob Faibussowitsch         PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol));
39839566063dSJacob Faibussowitsch         PetscCall(TSPostStep(ts));
3984aeb4809dSShri Abhyankar       }
3985193ac0bcSJed Brown     }
39869566063dSJacob Faibussowitsch     PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol));
39876427ac75SLisandro Dalcin 
398849354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
39899566063dSJacob Faibussowitsch       PetscCall(TSInterpolate(ts,ts->max_time,u));
3990cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3991b06615a5SLisandro Dalcin       solution = u;
39929566063dSJacob Faibussowitsch       PetscCall(TSMonitor(ts,-1,ts->solvetime,solution));
39930574a7fbSJed Brown     } else {
39949566063dSJacob Faibussowitsch       if (u) PetscCall(VecCopy(ts->vec_sol,u));
3995cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3996b06615a5SLisandro Dalcin       solution = ts->vec_sol;
39970574a7fbSJed Brown     }
3998193ac0bcSJed Brown   }
3999d2daff3dSHong Zhang 
40009566063dSJacob Faibussowitsch   PetscCall(TSViewFromOptions(ts,NULL,"-ts_view"));
40019566063dSJacob Faibussowitsch   PetscCall(VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution"));
40029566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsBlock((PetscObject)ts));
4003ef222394SBarry Smith   if (ts->adjoint_solve) {
40049566063dSJacob Faibussowitsch     PetscCall(TSAdjointSolve(ts));
40052b0a91c0SBarry Smith   }
40066a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40076a4d4014SLisandro Dalcin }
40086a4d4014SLisandro Dalcin 
4009d763cef2SBarry Smith /*@
4010b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4011d763cef2SBarry Smith 
4012d763cef2SBarry Smith    Not Collective
4013d763cef2SBarry Smith 
4014d763cef2SBarry Smith    Input Parameter:
4015d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4016d763cef2SBarry Smith 
4017d763cef2SBarry Smith    Output Parameter:
401819eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4019d763cef2SBarry Smith 
4020d763cef2SBarry Smith    Level: beginner
4021d763cef2SBarry Smith 
4022b8123daeSJed Brown    Note:
4023b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40249be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4025b8123daeSJed Brown 
40268f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4027d763cef2SBarry Smith 
4028d763cef2SBarry Smith @*/
40297087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4030d763cef2SBarry Smith {
4031d763cef2SBarry Smith   PetscFunctionBegin;
40320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4033f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4034d763cef2SBarry Smith   *t = ts->ptime;
4035d763cef2SBarry Smith   PetscFunctionReturn(0);
4036d763cef2SBarry Smith }
4037d763cef2SBarry Smith 
4038e5e524a1SHong Zhang /*@
4039e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4040e5e524a1SHong Zhang 
4041e5e524a1SHong Zhang    Not Collective
4042e5e524a1SHong Zhang 
4043e5e524a1SHong Zhang    Input Parameter:
4044e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4045e5e524a1SHong Zhang 
4046e5e524a1SHong Zhang    Output Parameter:
4047e5e524a1SHong Zhang .  t  - the previous time
4048e5e524a1SHong Zhang 
4049e5e524a1SHong Zhang    Level: beginner
4050e5e524a1SHong Zhang 
4051aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4052e5e524a1SHong Zhang 
4053e5e524a1SHong Zhang @*/
4054e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4055e5e524a1SHong Zhang {
4056e5e524a1SHong Zhang   PetscFunctionBegin;
4057e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4058e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4059e5e524a1SHong Zhang   *t = ts->ptime_prev;
4060e5e524a1SHong Zhang   PetscFunctionReturn(0);
4061e5e524a1SHong Zhang }
4062e5e524a1SHong Zhang 
40636a4d4014SLisandro Dalcin /*@
40646a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40656a4d4014SLisandro Dalcin 
40663f9fe445SBarry Smith    Logically Collective on TS
40676a4d4014SLisandro Dalcin 
40686a4d4014SLisandro Dalcin    Input Parameters:
40696a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40706a4d4014SLisandro Dalcin -  time - the time
40716a4d4014SLisandro Dalcin 
40726a4d4014SLisandro Dalcin    Level: intermediate
40736a4d4014SLisandro Dalcin 
407419eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
40756a4d4014SLisandro Dalcin 
40766a4d4014SLisandro Dalcin @*/
40777087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
40786a4d4014SLisandro Dalcin {
40796a4d4014SLisandro Dalcin   PetscFunctionBegin;
40800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4081c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
40826a4d4014SLisandro Dalcin   ts->ptime = t;
40836a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40846a4d4014SLisandro Dalcin }
40856a4d4014SLisandro Dalcin 
4086d763cef2SBarry Smith /*@C
4087d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4088d763cef2SBarry Smith    TS options in the database.
4089d763cef2SBarry Smith 
40903f9fe445SBarry Smith    Logically Collective on TS
4091d763cef2SBarry Smith 
4092d8d19677SJose E. Roman    Input Parameters:
4093d763cef2SBarry Smith +  ts     - The TS context
4094d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4095d763cef2SBarry Smith 
4096d763cef2SBarry Smith    Notes:
4097d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4098d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4099d763cef2SBarry Smith    hyphen.
4100d763cef2SBarry Smith 
4101d763cef2SBarry Smith    Level: advanced
4102d763cef2SBarry Smith 
4103d763cef2SBarry Smith .seealso: TSSetFromOptions()
4104d763cef2SBarry Smith 
4105d763cef2SBarry Smith @*/
41067087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4107d763cef2SBarry Smith {
4108089b2837SJed Brown   SNES           snes;
4109d763cef2SBarry Smith 
4110d763cef2SBarry Smith   PetscFunctionBegin;
41110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41129566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts,prefix));
41139566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
41149566063dSJacob Faibussowitsch   PetscCall(SNESSetOptionsPrefix(snes,prefix));
4115d763cef2SBarry Smith   PetscFunctionReturn(0);
4116d763cef2SBarry Smith }
4117d763cef2SBarry Smith 
4118d763cef2SBarry Smith /*@C
4119d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4120d763cef2SBarry Smith    TS options in the database.
4121d763cef2SBarry Smith 
41223f9fe445SBarry Smith    Logically Collective on TS
4123d763cef2SBarry Smith 
4124d8d19677SJose E. Roman    Input Parameters:
4125d763cef2SBarry Smith +  ts     - The TS context
4126d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4127d763cef2SBarry Smith 
4128d763cef2SBarry Smith    Notes:
4129d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4130d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4131d763cef2SBarry Smith    hyphen.
4132d763cef2SBarry Smith 
4133d763cef2SBarry Smith    Level: advanced
4134d763cef2SBarry Smith 
4135d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4136d763cef2SBarry Smith 
4137d763cef2SBarry Smith @*/
41387087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4139d763cef2SBarry Smith {
4140089b2837SJed Brown   SNES           snes;
4141d763cef2SBarry Smith 
4142d763cef2SBarry Smith   PetscFunctionBegin;
41430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41449566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix));
41459566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
41469566063dSJacob Faibussowitsch   PetscCall(SNESAppendOptionsPrefix(snes,prefix));
4147d763cef2SBarry Smith   PetscFunctionReturn(0);
4148d763cef2SBarry Smith }
4149d763cef2SBarry Smith 
4150d763cef2SBarry Smith /*@C
4151d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4152d763cef2SBarry Smith    TS options in the database.
4153d763cef2SBarry Smith 
4154d763cef2SBarry Smith    Not Collective
4155d763cef2SBarry Smith 
4156d763cef2SBarry Smith    Input Parameter:
4157d763cef2SBarry Smith .  ts - The TS context
4158d763cef2SBarry Smith 
4159d763cef2SBarry Smith    Output Parameter:
4160d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4161d763cef2SBarry Smith 
416295452b02SPatrick Sanan    Notes:
416395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4164d763cef2SBarry Smith    sufficient length to hold the prefix.
4165d763cef2SBarry Smith 
4166d763cef2SBarry Smith    Level: intermediate
4167d763cef2SBarry Smith 
4168d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4169d763cef2SBarry Smith @*/
41707087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4171d763cef2SBarry Smith {
4172d763cef2SBarry Smith   PetscFunctionBegin;
41730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41744482741eSBarry Smith   PetscValidPointer(prefix,2);
41759566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts,prefix));
4176d763cef2SBarry Smith   PetscFunctionReturn(0);
4177d763cef2SBarry Smith }
4178d763cef2SBarry Smith 
4179d763cef2SBarry Smith /*@C
4180d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4181d763cef2SBarry Smith 
4182d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4183d763cef2SBarry Smith 
4184d763cef2SBarry Smith    Input Parameter:
4185d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4186d763cef2SBarry Smith 
4187d763cef2SBarry Smith    Output Parameters:
4188e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4189e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4190e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4191e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4192d763cef2SBarry Smith 
419395452b02SPatrick Sanan    Notes:
419495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4195d763cef2SBarry Smith 
4196d763cef2SBarry Smith    Level: intermediate
4197d763cef2SBarry Smith 
419880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4199d763cef2SBarry Smith 
4200d763cef2SBarry Smith @*/
4201e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4202d763cef2SBarry Smith {
420324989b8cSPeter Brune   DM             dm;
4204089b2837SJed Brown 
4205d763cef2SBarry Smith   PetscFunctionBegin;
420623a57915SBarry Smith   if (Amat || Pmat) {
420723a57915SBarry Smith     SNES snes;
42089566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
42099566063dSJacob Faibussowitsch     PetscCall(SNESSetUpMatrices(snes));
42109566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL));
421123a57915SBarry Smith   }
42129566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
42139566063dSJacob Faibussowitsch   PetscCall(DMTSGetRHSJacobian(dm,func,ctx));
4214d763cef2SBarry Smith   PetscFunctionReturn(0);
4215d763cef2SBarry Smith }
4216d763cef2SBarry Smith 
42172eca1d9cSJed Brown /*@C
42182eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42192eca1d9cSJed Brown 
42202eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42212eca1d9cSJed Brown 
42222eca1d9cSJed Brown    Input Parameter:
42232eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42242eca1d9cSJed Brown 
42252eca1d9cSJed Brown    Output Parameters:
4226e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4227e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42282eca1d9cSJed Brown .  f   - The function to compute the matrices
42292eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42302eca1d9cSJed Brown 
423195452b02SPatrick Sanan    Notes:
423295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42332eca1d9cSJed Brown 
42342eca1d9cSJed Brown    Level: advanced
42352eca1d9cSJed Brown 
423680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42372eca1d9cSJed Brown 
42382eca1d9cSJed Brown @*/
4239e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
42402eca1d9cSJed Brown {
424124989b8cSPeter Brune   DM             dm;
42420910c330SBarry Smith 
42432eca1d9cSJed Brown   PetscFunctionBegin;
4244c0aab802Sstefano_zampini   if (Amat || Pmat) {
4245c0aab802Sstefano_zampini     SNES snes;
42469566063dSJacob Faibussowitsch     PetscCall(TSGetSNES(ts,&snes));
42479566063dSJacob Faibussowitsch     PetscCall(SNESSetUpMatrices(snes));
42489566063dSJacob Faibussowitsch     PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL));
4249c0aab802Sstefano_zampini   }
42509566063dSJacob Faibussowitsch   PetscCall(TSGetDM(ts,&dm));
42519566063dSJacob Faibussowitsch   PetscCall(DMTSGetIJacobian(dm,f,ctx));
42522eca1d9cSJed Brown   PetscFunctionReturn(0);
42532eca1d9cSJed Brown }
42542eca1d9cSJed Brown 
4255af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
42566c699258SBarry Smith /*@
42572a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
42586c699258SBarry Smith 
4259d083f849SBarry Smith    Logically Collective on ts
42606c699258SBarry Smith 
42616c699258SBarry Smith    Input Parameters:
42622a808120SBarry Smith +  ts - the ODE integrator object
42632a808120SBarry Smith -  dm - the dm, cannot be NULL
42646c699258SBarry Smith 
4265e03a659cSJed Brown    Notes:
4266e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4267e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4268e03a659cSJed Brown    different problems using the same function space.
4269e03a659cSJed Brown 
42706c699258SBarry Smith    Level: intermediate
42716c699258SBarry Smith 
42726c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
42736c699258SBarry Smith @*/
42747087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
42756c699258SBarry Smith {
4276089b2837SJed Brown   SNES           snes;
4277942e3340SBarry Smith   DMTS           tsdm;
42786c699258SBarry Smith 
42796c699258SBarry Smith   PetscFunctionBegin;
42800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42812a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
42829566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)dm));
4283942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
42842a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
42859566063dSJacob Faibussowitsch       PetscCall(DMCopyDMTS(ts->dm,dm));
42869566063dSJacob Faibussowitsch       PetscCall(DMGetDMTS(ts->dm,&tsdm));
428724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
428824989b8cSPeter Brune         tsdm->originaldm = dm;
428924989b8cSPeter Brune       }
429024989b8cSPeter Brune     }
42919566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&ts->dm));
429224989b8cSPeter Brune   }
42936c699258SBarry Smith   ts->dm = dm;
4294bbd56ea5SKarl Rupp 
42959566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
42969566063dSJacob Faibussowitsch   PetscCall(SNESSetDM(snes,dm));
42976c699258SBarry Smith   PetscFunctionReturn(0);
42986c699258SBarry Smith }
42996c699258SBarry Smith 
43006c699258SBarry Smith /*@
43016c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
43026c699258SBarry Smith 
43033f9fe445SBarry Smith    Not Collective
43046c699258SBarry Smith 
43056c699258SBarry Smith    Input Parameter:
43066c699258SBarry Smith . ts - the preconditioner context
43076c699258SBarry Smith 
43086c699258SBarry Smith    Output Parameter:
43096c699258SBarry Smith .  dm - the dm
43106c699258SBarry Smith 
43116c699258SBarry Smith    Level: intermediate
43126c699258SBarry Smith 
43136c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
43146c699258SBarry Smith @*/
43157087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
43166c699258SBarry Smith {
43176c699258SBarry Smith   PetscFunctionBegin;
43180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4319496e6a7aSJed Brown   if (!ts->dm) {
43209566063dSJacob Faibussowitsch     PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm));
43219566063dSJacob Faibussowitsch     if (ts->snes) PetscCall(SNESSetDM(ts->snes,ts->dm));
4322496e6a7aSJed Brown   }
43236c699258SBarry Smith   *dm = ts->dm;
43246c699258SBarry Smith   PetscFunctionReturn(0);
43256c699258SBarry Smith }
43261713a123SBarry Smith 
43270f5c6efeSJed Brown /*@
43280f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
43290f5c6efeSJed Brown 
43303f9fe445SBarry Smith    Logically Collective on SNES
43310f5c6efeSJed Brown 
4332d8d19677SJose E. Roman    Input Parameters:
4333d42a1c89SJed Brown + snes - nonlinear solver
43340910c330SBarry Smith . U - the current state at which to evaluate the residual
4335d42a1c89SJed Brown - ctx - user context, must be a TS
43360f5c6efeSJed Brown 
43370f5c6efeSJed Brown    Output Parameter:
43380f5c6efeSJed Brown . F - the nonlinear residual
43390f5c6efeSJed Brown 
43400f5c6efeSJed Brown    Notes:
43410f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
43420f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
43430f5c6efeSJed Brown 
43440f5c6efeSJed Brown    Level: advanced
43450f5c6efeSJed Brown 
43460f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
43470f5c6efeSJed Brown @*/
43480910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
43490f5c6efeSJed Brown {
43500f5c6efeSJed Brown   TS             ts = (TS)ctx;
43510f5c6efeSJed Brown 
43520f5c6efeSJed Brown   PetscFunctionBegin;
43530f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
43540910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
43550f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
43560f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
43579566063dSJacob Faibussowitsch   PetscCall((ts->ops->snesfunction)(snes,U,F,ts));
43580f5c6efeSJed Brown   PetscFunctionReturn(0);
43590f5c6efeSJed Brown }
43600f5c6efeSJed Brown 
43610f5c6efeSJed Brown /*@
43620f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
43630f5c6efeSJed Brown 
43640f5c6efeSJed Brown    Collective on SNES
43650f5c6efeSJed Brown 
4366d8d19677SJose E. Roman    Input Parameters:
43670f5c6efeSJed Brown + snes - nonlinear solver
43680910c330SBarry Smith . U - the current state at which to evaluate the residual
43690f5c6efeSJed Brown - ctx - user context, must be a TS
43700f5c6efeSJed Brown 
4371d8d19677SJose E. Roman    Output Parameters:
43720f5c6efeSJed Brown + A - the Jacobian
43736b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A)
43740f5c6efeSJed Brown 
43750f5c6efeSJed Brown    Notes:
43760f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
43770f5c6efeSJed Brown 
43780f5c6efeSJed Brown    Level: developer
43790f5c6efeSJed Brown 
43800f5c6efeSJed Brown .seealso: SNESSetJacobian()
43810f5c6efeSJed Brown @*/
4382d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
43830f5c6efeSJed Brown {
43840f5c6efeSJed Brown   TS             ts = (TS)ctx;
43850f5c6efeSJed Brown 
43860f5c6efeSJed Brown   PetscFunctionBegin;
43870f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
43880910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
43890f5c6efeSJed Brown   PetscValidPointer(A,3);
439094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
43910f5c6efeSJed Brown   PetscValidPointer(B,4);
439294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
4393064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ts,TS_CLASSID,5);
43949566063dSJacob Faibussowitsch   PetscCall((ts->ops->snesjacobian)(snes,U,A,B,ts));
43950f5c6efeSJed Brown   PetscFunctionReturn(0);
43960f5c6efeSJed Brown }
4397325fc9f4SBarry Smith 
43980e4ef248SJed Brown /*@C
43999ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
44000e4ef248SJed Brown 
44010e4ef248SJed Brown    Collective on TS
44020e4ef248SJed Brown 
44034165533cSJose E. Roman    Input Parameters:
44040e4ef248SJed Brown +  ts - time stepping context
44050e4ef248SJed Brown .  t - time at which to evaluate
44060910c330SBarry Smith .  U - state at which to evaluate
44070e4ef248SJed Brown -  ctx - context
44080e4ef248SJed Brown 
44094165533cSJose E. Roman    Output Parameter:
44100e4ef248SJed Brown .  F - right hand side
44110e4ef248SJed Brown 
44120e4ef248SJed Brown    Level: intermediate
44130e4ef248SJed Brown 
44140e4ef248SJed Brown    Notes:
44150e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
44160e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
44170e4ef248SJed Brown 
44180e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
44190e4ef248SJed Brown @*/
44200910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
44210e4ef248SJed Brown {
44220e4ef248SJed Brown   Mat            Arhs,Brhs;
44230e4ef248SJed Brown 
44240e4ef248SJed Brown   PetscFunctionBegin;
44259566063dSJacob Faibussowitsch   PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs));
44262663174eSHong Zhang   /* undo the damage caused by shifting */
44279566063dSJacob Faibussowitsch   PetscCall(TSRecoverRHSJacobian(ts,Arhs,Brhs));
44289566063dSJacob Faibussowitsch   PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs));
44299566063dSJacob Faibussowitsch   PetscCall(MatMult(Arhs,U,F));
44300e4ef248SJed Brown   PetscFunctionReturn(0);
44310e4ef248SJed Brown }
44320e4ef248SJed Brown 
44330e4ef248SJed Brown /*@C
44340e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
44350e4ef248SJed Brown 
44360e4ef248SJed Brown    Collective on TS
44370e4ef248SJed Brown 
44384165533cSJose E. Roman    Input Parameters:
44390e4ef248SJed Brown +  ts - time stepping context
44400e4ef248SJed Brown .  t - time at which to evaluate
44410910c330SBarry Smith .  U - state at which to evaluate
44420e4ef248SJed Brown -  ctx - context
44430e4ef248SJed Brown 
44444165533cSJose E. Roman    Output Parameters:
44450e4ef248SJed Brown +  A - pointer to operator
444697bb3fdcSJose E. Roman -  B - pointer to preconditioning matrix
44470e4ef248SJed Brown 
44480e4ef248SJed Brown    Level: intermediate
44490e4ef248SJed Brown 
44500e4ef248SJed Brown    Notes:
44510e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
44520e4ef248SJed Brown 
44530e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
44540e4ef248SJed Brown @*/
4455d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
44560e4ef248SJed Brown {
44570e4ef248SJed Brown   PetscFunctionBegin;
44580e4ef248SJed Brown   PetscFunctionReturn(0);
44590e4ef248SJed Brown }
44600e4ef248SJed Brown 
44610026cea9SSean Farley /*@C
44620026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
44630026cea9SSean Farley 
44640026cea9SSean Farley    Collective on TS
44650026cea9SSean Farley 
44664165533cSJose E. Roman    Input Parameters:
44670026cea9SSean Farley +  ts - time stepping context
44680026cea9SSean Farley .  t - time at which to evaluate
44690910c330SBarry Smith .  U - state at which to evaluate
44700910c330SBarry Smith .  Udot - time derivative of state vector
44710026cea9SSean Farley -  ctx - context
44720026cea9SSean Farley 
44734165533cSJose E. Roman    Output Parameter:
44740026cea9SSean Farley .  F - left hand side
44750026cea9SSean Farley 
44760026cea9SSean Farley    Level: intermediate
44770026cea9SSean Farley 
44780026cea9SSean Farley    Notes:
44790910c330SBarry 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
44800026cea9SSean Farley    user is required to write their own TSComputeIFunction.
44810026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
44820026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
44830026cea9SSean Farley 
44849ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
44859ae8fd06SBarry Smith 
44869ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
44870026cea9SSean Farley @*/
44880910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
44890026cea9SSean Farley {
44900026cea9SSean Farley   Mat            A,B;
44910026cea9SSean Farley 
44920026cea9SSean Farley   PetscFunctionBegin;
44939566063dSJacob Faibussowitsch   PetscCall(TSGetIJacobian(ts,&A,&B,NULL,NULL));
44949566063dSJacob Faibussowitsch   PetscCall(TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE));
44959566063dSJacob Faibussowitsch   PetscCall(MatMult(A,Udot,F));
44960026cea9SSean Farley   PetscFunctionReturn(0);
44970026cea9SSean Farley }
44980026cea9SSean Farley 
44990026cea9SSean Farley /*@C
4500b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
45010026cea9SSean Farley 
45020026cea9SSean Farley    Collective on TS
45030026cea9SSean Farley 
45044165533cSJose E. Roman    Input Parameters:
45050026cea9SSean Farley +  ts - time stepping context
45060026cea9SSean Farley .  t - time at which to evaluate
45070910c330SBarry Smith .  U - state at which to evaluate
45080910c330SBarry Smith .  Udot - time derivative of state vector
45090026cea9SSean Farley .  shift - shift to apply
45100026cea9SSean Farley -  ctx - context
45110026cea9SSean Farley 
45124165533cSJose E. Roman    Output Parameters:
45130026cea9SSean Farley +  A - pointer to operator
451497bb3fdcSJose E. Roman -  B - pointer to preconditioning matrix
45150026cea9SSean Farley 
4516b41af12eSJed Brown    Level: advanced
45170026cea9SSean Farley 
45180026cea9SSean Farley    Notes:
45190026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
45200026cea9SSean Farley 
4521b41af12eSJed Brown    It is only appropriate for problems of the form
4522b41af12eSJed Brown 
4523b41af12eSJed Brown $     M Udot = F(U,t)
4524b41af12eSJed Brown 
4525b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4526b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4527b41af12eSJed Brown   an implicit operator of the form
4528b41af12eSJed Brown 
4529b41af12eSJed Brown $    shift*M + J
4530b41af12eSJed Brown 
4531b41af12eSJed 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
4532b41af12eSJed Brown   a copy of M or reassemble it when requested.
4533b41af12eSJed Brown 
45340026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
45350026cea9SSean Farley @*/
4536d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
45370026cea9SSean Farley {
45380026cea9SSean Farley   PetscFunctionBegin;
45399566063dSJacob Faibussowitsch   PetscCall(MatScale(A, shift / ts->ijacobian.shift));
4540b41af12eSJed Brown   ts->ijacobian.shift = shift;
45410026cea9SSean Farley   PetscFunctionReturn(0);
45420026cea9SSean Farley }
4543b41af12eSJed Brown 
4544e817cc15SEmil Constantinescu /*@
4545e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4546e817cc15SEmil Constantinescu 
4547e817cc15SEmil Constantinescu    Not Collective
4548e817cc15SEmil Constantinescu 
4549e817cc15SEmil Constantinescu    Input Parameter:
4550e817cc15SEmil Constantinescu .  ts - the TS context
4551e817cc15SEmil Constantinescu 
4552e817cc15SEmil Constantinescu    Output Parameter:
45534e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4554e817cc15SEmil Constantinescu 
4555e817cc15SEmil Constantinescu    Level: beginner
4556e817cc15SEmil Constantinescu 
4557e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4558e817cc15SEmil Constantinescu @*/
4559e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4560e817cc15SEmil Constantinescu {
4561e817cc15SEmil Constantinescu   PetscFunctionBegin;
4562e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4563e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4564e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4565e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4566e817cc15SEmil Constantinescu }
4567e817cc15SEmil Constantinescu 
4568e817cc15SEmil Constantinescu /*@
4569e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4570e817cc15SEmil Constantinescu 
4571e817cc15SEmil Constantinescu    Not Collective
4572e817cc15SEmil Constantinescu 
4573d8d19677SJose E. Roman    Input Parameters:
4574e817cc15SEmil Constantinescu +  ts - the TS context
45751297b224SEmil Constantinescu -  equation_type - see TSEquationType
4576e817cc15SEmil Constantinescu 
4577e817cc15SEmil Constantinescu    Level: advanced
4578e817cc15SEmil Constantinescu 
4579e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4580e817cc15SEmil Constantinescu @*/
4581e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4582e817cc15SEmil Constantinescu {
4583e817cc15SEmil Constantinescu   PetscFunctionBegin;
4584e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4585e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4586e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4587e817cc15SEmil Constantinescu }
45880026cea9SSean Farley 
45894af1b03aSJed Brown /*@
45904af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
45914af1b03aSJed Brown 
45924af1b03aSJed Brown    Not Collective
45934af1b03aSJed Brown 
45944af1b03aSJed Brown    Input Parameter:
45954af1b03aSJed Brown .  ts - the TS context
45964af1b03aSJed Brown 
45974af1b03aSJed Brown    Output Parameter:
45984af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
45994af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
46004af1b03aSJed Brown 
4601487e0bb9SJed Brown    Level: beginner
46024af1b03aSJed Brown 
4603cd652676SJed Brown    Notes:
4604cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
46054af1b03aSJed Brown 
46064af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
46074af1b03aSJed Brown @*/
46084af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
46094af1b03aSJed Brown {
46104af1b03aSJed Brown   PetscFunctionBegin;
46114af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46124af1b03aSJed Brown   PetscValidPointer(reason,2);
46134af1b03aSJed Brown   *reason = ts->reason;
46144af1b03aSJed Brown   PetscFunctionReturn(0);
46154af1b03aSJed Brown }
46164af1b03aSJed Brown 
4617d6ad946cSShri Abhyankar /*@
4618d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4619d6ad946cSShri Abhyankar 
46206b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
4621d6ad946cSShri Abhyankar 
46226b221cbeSPatrick Sanan    Input Parameters:
4623d6ad946cSShri Abhyankar +  ts - the TS context
46246b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4625d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4626d6ad946cSShri Abhyankar 
4627f5abba47SShri Abhyankar    Level: advanced
4628d6ad946cSShri Abhyankar 
4629d6ad946cSShri Abhyankar    Notes:
46306b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
4631d6ad946cSShri Abhyankar 
4632d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4633d6ad946cSShri Abhyankar @*/
4634d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4635d6ad946cSShri Abhyankar {
4636d6ad946cSShri Abhyankar   PetscFunctionBegin;
4637d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4638d6ad946cSShri Abhyankar   ts->reason = reason;
4639d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4640d6ad946cSShri Abhyankar }
4641d6ad946cSShri Abhyankar 
4642cc708dedSBarry Smith /*@
4643cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4644cc708dedSBarry Smith 
4645cc708dedSBarry Smith    Not Collective
4646cc708dedSBarry Smith 
4647cc708dedSBarry Smith    Input Parameter:
4648cc708dedSBarry Smith .  ts - the TS context
4649cc708dedSBarry Smith 
4650cc708dedSBarry Smith    Output Parameter:
465119eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4652cc708dedSBarry Smith 
4653487e0bb9SJed Brown    Level: beginner
4654cc708dedSBarry Smith 
4655cc708dedSBarry Smith    Notes:
4656cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4657cc708dedSBarry Smith 
4658cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4659cc708dedSBarry Smith @*/
4660cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4661cc708dedSBarry Smith {
4662cc708dedSBarry Smith   PetscFunctionBegin;
4663cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4664dadcf809SJacob Faibussowitsch   PetscValidRealPointer(ftime,2);
4665cc708dedSBarry Smith   *ftime = ts->solvetime;
4666cc708dedSBarry Smith   PetscFunctionReturn(0);
4667cc708dedSBarry Smith }
4668cc708dedSBarry Smith 
46692c18e0fdSBarry Smith /*@
46705ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
46719f67acb7SJed Brown    used by the time integrator.
46729f67acb7SJed Brown 
46739f67acb7SJed Brown    Not Collective
46749f67acb7SJed Brown 
46759f67acb7SJed Brown    Input Parameter:
46769f67acb7SJed Brown .  ts - TS context
46779f67acb7SJed Brown 
46789f67acb7SJed Brown    Output Parameter:
46799f67acb7SJed Brown .  nits - number of nonlinear iterations
46809f67acb7SJed Brown 
46819f67acb7SJed Brown    Notes:
46829f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
46839f67acb7SJed Brown 
46849f67acb7SJed Brown    Level: intermediate
46859f67acb7SJed Brown 
46865ef26d82SJed Brown .seealso:  TSGetKSPIterations()
46879f67acb7SJed Brown @*/
46885ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
46899f67acb7SJed Brown {
46909f67acb7SJed Brown   PetscFunctionBegin;
46919f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46929f67acb7SJed Brown   PetscValidIntPointer(nits,2);
46935ef26d82SJed Brown   *nits = ts->snes_its;
46949f67acb7SJed Brown   PetscFunctionReturn(0);
46959f67acb7SJed Brown }
46969f67acb7SJed Brown 
46979f67acb7SJed Brown /*@
46985ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
46999f67acb7SJed Brown    used by the time integrator.
47009f67acb7SJed Brown 
47019f67acb7SJed Brown    Not Collective
47029f67acb7SJed Brown 
47039f67acb7SJed Brown    Input Parameter:
47049f67acb7SJed Brown .  ts - TS context
47059f67acb7SJed Brown 
47069f67acb7SJed Brown    Output Parameter:
47079f67acb7SJed Brown .  lits - number of linear iterations
47089f67acb7SJed Brown 
47099f67acb7SJed Brown    Notes:
47109f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
47119f67acb7SJed Brown 
47129f67acb7SJed Brown    Level: intermediate
47139f67acb7SJed Brown 
47145ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
47159f67acb7SJed Brown @*/
47165ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
47179f67acb7SJed Brown {
47189f67acb7SJed Brown   PetscFunctionBegin;
47199f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47209f67acb7SJed Brown   PetscValidIntPointer(lits,2);
47215ef26d82SJed Brown   *lits = ts->ksp_its;
47229f67acb7SJed Brown   PetscFunctionReturn(0);
47239f67acb7SJed Brown }
47249f67acb7SJed Brown 
4725cef5090cSJed Brown /*@
4726cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4727cef5090cSJed Brown 
4728cef5090cSJed Brown    Not Collective
4729cef5090cSJed Brown 
4730cef5090cSJed Brown    Input Parameter:
4731cef5090cSJed Brown .  ts - TS context
4732cef5090cSJed Brown 
4733cef5090cSJed Brown    Output Parameter:
4734cef5090cSJed Brown .  rejects - number of steps rejected
4735cef5090cSJed Brown 
4736cef5090cSJed Brown    Notes:
4737cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4738cef5090cSJed Brown 
4739cef5090cSJed Brown    Level: intermediate
4740cef5090cSJed Brown 
47415ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4742cef5090cSJed Brown @*/
4743cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4744cef5090cSJed Brown {
4745cef5090cSJed Brown   PetscFunctionBegin;
4746cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4747cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4748cef5090cSJed Brown   *rejects = ts->reject;
4749cef5090cSJed Brown   PetscFunctionReturn(0);
4750cef5090cSJed Brown }
4751cef5090cSJed Brown 
4752cef5090cSJed Brown /*@
4753cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4754cef5090cSJed Brown 
4755cef5090cSJed Brown    Not Collective
4756cef5090cSJed Brown 
4757cef5090cSJed Brown    Input Parameter:
4758cef5090cSJed Brown .  ts - TS context
4759cef5090cSJed Brown 
4760cef5090cSJed Brown    Output Parameter:
4761cef5090cSJed Brown .  fails - number of failed nonlinear solves
4762cef5090cSJed Brown 
4763cef5090cSJed Brown    Notes:
4764cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4765cef5090cSJed Brown 
4766cef5090cSJed Brown    Level: intermediate
4767cef5090cSJed Brown 
47685ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4769cef5090cSJed Brown @*/
4770cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4771cef5090cSJed Brown {
4772cef5090cSJed Brown   PetscFunctionBegin;
4773cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4774cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4775cef5090cSJed Brown   *fails = ts->num_snes_failures;
4776cef5090cSJed Brown   PetscFunctionReturn(0);
4777cef5090cSJed Brown }
4778cef5090cSJed Brown 
4779cef5090cSJed Brown /*@
4780cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4781cef5090cSJed Brown 
4782cef5090cSJed Brown    Not Collective
4783cef5090cSJed Brown 
4784d8d19677SJose E. Roman    Input Parameters:
4785cef5090cSJed Brown +  ts - TS context
4786cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4787cef5090cSJed Brown 
4788cef5090cSJed Brown    Notes:
4789cef5090cSJed Brown    The counter is reset to zero for each step
4790cef5090cSJed Brown 
4791cef5090cSJed Brown    Options Database Key:
4792cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4793cef5090cSJed Brown 
4794cef5090cSJed Brown    Level: intermediate
4795cef5090cSJed Brown 
47965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4797cef5090cSJed Brown @*/
4798cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4799cef5090cSJed Brown {
4800cef5090cSJed Brown   PetscFunctionBegin;
4801cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4802cef5090cSJed Brown   ts->max_reject = rejects;
4803cef5090cSJed Brown   PetscFunctionReturn(0);
4804cef5090cSJed Brown }
4805cef5090cSJed Brown 
4806cef5090cSJed Brown /*@
4807cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4808cef5090cSJed Brown 
4809cef5090cSJed Brown    Not Collective
4810cef5090cSJed Brown 
4811d8d19677SJose E. Roman    Input Parameters:
4812cef5090cSJed Brown +  ts - TS context
4813cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4814cef5090cSJed Brown 
4815cef5090cSJed Brown    Notes:
4816cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4817cef5090cSJed Brown 
4818cef5090cSJed Brown    Options Database Key:
4819cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4820cef5090cSJed Brown 
4821cef5090cSJed Brown    Level: intermediate
4822cef5090cSJed Brown 
48235ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4824cef5090cSJed Brown @*/
4825cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4826cef5090cSJed Brown {
4827cef5090cSJed Brown   PetscFunctionBegin;
4828cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4829cef5090cSJed Brown   ts->max_snes_failures = fails;
4830cef5090cSJed Brown   PetscFunctionReturn(0);
4831cef5090cSJed Brown }
4832cef5090cSJed Brown 
4833cef5090cSJed Brown /*@
4834cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4835cef5090cSJed Brown 
4836cef5090cSJed Brown    Not Collective
4837cef5090cSJed Brown 
4838d8d19677SJose E. Roman    Input Parameters:
4839cef5090cSJed Brown +  ts - TS context
4840cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4841cef5090cSJed Brown 
4842cef5090cSJed Brown    Options Database Key:
4843cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4844cef5090cSJed Brown 
4845cef5090cSJed Brown    Level: intermediate
4846cef5090cSJed Brown 
48475ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4848cef5090cSJed Brown @*/
4849cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4850cef5090cSJed Brown {
4851cef5090cSJed Brown   PetscFunctionBegin;
4852cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4853cef5090cSJed Brown   ts->errorifstepfailed = err;
4854cef5090cSJed Brown   PetscFunctionReturn(0);
4855cef5090cSJed Brown }
4856cef5090cSJed Brown 
485784df9cb4SJed Brown /*@
4858552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
485984df9cb4SJed Brown 
4860ed81e22dSJed Brown    Collective on TS if controller has not been created yet
486184df9cb4SJed Brown 
48624165533cSJose E. Roman    Input Parameter:
4863ed81e22dSJed Brown .  ts - time stepping context
486484df9cb4SJed Brown 
48654165533cSJose E. Roman    Output Parameter:
4866ed81e22dSJed Brown .  adapt - adaptive controller
486784df9cb4SJed Brown 
486884df9cb4SJed Brown    Level: intermediate
486984df9cb4SJed Brown 
4870ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
487184df9cb4SJed Brown @*/
4872552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
487384df9cb4SJed Brown {
487484df9cb4SJed Brown   PetscFunctionBegin;
487584df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4876bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
487784df9cb4SJed Brown   if (!ts->adapt) {
48789566063dSJacob Faibussowitsch     PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt));
48799566063dSJacob Faibussowitsch     PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt));
48809566063dSJacob Faibussowitsch     PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1));
488184df9cb4SJed Brown   }
4882bec58848SLisandro Dalcin   *adapt = ts->adapt;
488384df9cb4SJed Brown   PetscFunctionReturn(0);
488484df9cb4SJed Brown }
4885d6ebe24aSShri Abhyankar 
48861c3436cfSJed Brown /*@
48871c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
48881c3436cfSJed Brown 
48891c3436cfSJed Brown    Logically Collective
48901c3436cfSJed Brown 
48914165533cSJose E. Roman    Input Parameters:
48921c3436cfSJed Brown +  ts - time integration context
48931c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
48940298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
48951c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
48960298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
48971c3436cfSJed Brown 
4898a3cdaa26SBarry Smith    Options Database keys:
4899a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
490067b8a455SSatish Balay -  -ts_atol <atol> - Absolute tolerance for local truncation error
4901a3cdaa26SBarry Smith 
49023ff766beSShri Abhyankar    Notes:
49033ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
49043ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
49053ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
49063ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
49073ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
49083ff766beSShri Abhyankar    differential variables.
49093ff766beSShri Abhyankar 
49101c3436cfSJed Brown    Level: beginner
49111c3436cfSJed Brown 
49125c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
49131c3436cfSJed Brown @*/
49141c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
49151c3436cfSJed Brown {
49161c3436cfSJed Brown   PetscFunctionBegin;
4917c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
49181c3436cfSJed Brown   if (vatol) {
49199566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)vatol));
49209566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vatol));
49211c3436cfSJed Brown     ts->vatol = vatol;
49221c3436cfSJed Brown   }
4923c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
49241c3436cfSJed Brown   if (vrtol) {
49259566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)vrtol));
49269566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&ts->vrtol));
49271c3436cfSJed Brown     ts->vrtol = vrtol;
49281c3436cfSJed Brown   }
49291c3436cfSJed Brown   PetscFunctionReturn(0);
49301c3436cfSJed Brown }
49311c3436cfSJed Brown 
4932c5033834SJed Brown /*@
4933c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4934c5033834SJed Brown 
4935c5033834SJed Brown    Logically Collective
4936c5033834SJed Brown 
49374165533cSJose E. Roman    Input Parameter:
4938c5033834SJed Brown .  ts - time integration context
4939c5033834SJed Brown 
49404165533cSJose E. Roman    Output Parameters:
49410298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
49420298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
49430298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
49440298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4945c5033834SJed Brown 
4946c5033834SJed Brown    Level: beginner
4947c5033834SJed Brown 
49485c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
4949c5033834SJed Brown @*/
4950c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4951c5033834SJed Brown {
4952c5033834SJed Brown   PetscFunctionBegin;
4953c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4954c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4955c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4956c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4957c5033834SJed Brown   PetscFunctionReturn(0);
4958c5033834SJed Brown }
4959c5033834SJed Brown 
49609c6b16b5SShri Abhyankar /*@
4961a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
49629c6b16b5SShri Abhyankar 
49639c6b16b5SShri Abhyankar    Collective on TS
49649c6b16b5SShri Abhyankar 
49654165533cSJose E. Roman    Input Parameters:
49669c6b16b5SShri Abhyankar +  ts - time stepping context
4967a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
4968a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
49699c6b16b5SShri Abhyankar 
49704165533cSJose E. Roman    Output Parameters:
4971a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
49727453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
4973a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
49749c6b16b5SShri Abhyankar 
49759c6b16b5SShri Abhyankar    Level: developer
49769c6b16b5SShri Abhyankar 
4977deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
49789c6b16b5SShri Abhyankar @*/
49797453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
49809c6b16b5SShri Abhyankar {
49819c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
49827453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
49837453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
49849c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
49857453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
49867453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
49877453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
49889c6b16b5SShri Abhyankar 
49899c6b16b5SShri Abhyankar   PetscFunctionBegin;
49909c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4991a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
4992a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
4993a4868fbcSLisandro Dalcin   PetscValidType(U,2);
4994a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
4995a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
4996dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,4);
4997dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,5);
4998dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,6);
49993c633725SBarry Smith   PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
50009c6b16b5SShri Abhyankar 
50019566063dSJacob Faibussowitsch   PetscCall(VecGetSize(U,&N));
50029566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(U,&n));
50039566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(U,&rstart,NULL));
50049566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
50059566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
50067453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
50077453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
50087453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
50099c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
50109c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
50119566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
50129566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
50139c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
501476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50157453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50167453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
50177453f775SEmil Constantinescu       if (tola>0.) {
50187453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50197453f775SEmil Constantinescu         na_loc++;
50207453f775SEmil Constantinescu       }
50217453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50227453f775SEmil Constantinescu       if (tolr>0.) {
50237453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50247453f775SEmil Constantinescu         nr_loc++;
50257453f775SEmil Constantinescu       }
50267453f775SEmil Constantinescu       tol=tola+tolr;
50277453f775SEmil Constantinescu       if (tol>0.) {
50287453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
50297453f775SEmil Constantinescu         n_loc++;
50307453f775SEmil Constantinescu       }
50319c6b16b5SShri Abhyankar     }
50329566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
50339566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
50349c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
50359c6b16b5SShri Abhyankar     const PetscScalar *atol;
50369566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
50379c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
503876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50397453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50407453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
50417453f775SEmil Constantinescu       if (tola>0.) {
50427453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50437453f775SEmil Constantinescu         na_loc++;
50447453f775SEmil Constantinescu       }
50457453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50467453f775SEmil Constantinescu       if (tolr>0.) {
50477453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50487453f775SEmil Constantinescu         nr_loc++;
50497453f775SEmil Constantinescu       }
50507453f775SEmil Constantinescu       tol=tola+tolr;
50517453f775SEmil Constantinescu       if (tol>0.) {
50527453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
50537453f775SEmil Constantinescu         n_loc++;
50547453f775SEmil Constantinescu       }
50559c6b16b5SShri Abhyankar     }
50569566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
50579c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
50589c6b16b5SShri Abhyankar     const PetscScalar *rtol;
50599566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
50609c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
506176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50627453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50637453f775SEmil Constantinescu       tola = ts->atol;
50647453f775SEmil Constantinescu       if (tola>0.) {
50657453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50667453f775SEmil Constantinescu         na_loc++;
50677453f775SEmil Constantinescu       }
50687453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50697453f775SEmil Constantinescu       if (tolr>0.) {
50707453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50717453f775SEmil Constantinescu         nr_loc++;
50727453f775SEmil Constantinescu       }
50737453f775SEmil Constantinescu       tol=tola+tolr;
50747453f775SEmil Constantinescu       if (tol>0.) {
50757453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
50767453f775SEmil Constantinescu         n_loc++;
50777453f775SEmil Constantinescu       }
50789c6b16b5SShri Abhyankar     }
50799566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
50809c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
50819c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
508276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50837453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50847453f775SEmil Constantinescu       tola = ts->atol;
50857453f775SEmil Constantinescu       if (tola>0.) {
50867453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50877453f775SEmil Constantinescu         na_loc++;
50887453f775SEmil Constantinescu       }
50897453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50907453f775SEmil Constantinescu       if (tolr>0.) {
50917453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50927453f775SEmil Constantinescu         nr_loc++;
50937453f775SEmil Constantinescu       }
50947453f775SEmil Constantinescu       tol=tola+tolr;
50957453f775SEmil Constantinescu       if (tol>0.) {
50967453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
50977453f775SEmil Constantinescu         n_loc++;
50987453f775SEmil Constantinescu       }
50999c6b16b5SShri Abhyankar     }
51009c6b16b5SShri Abhyankar   }
51019566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
51029566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
51039c6b16b5SShri Abhyankar 
51047453f775SEmil Constantinescu   err_loc[0] = sum;
51057453f775SEmil Constantinescu   err_loc[1] = suma;
51067453f775SEmil Constantinescu   err_loc[2] = sumr;
51077453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
51087453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
51097453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
51107453f775SEmil Constantinescu 
51111c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts)));
51127453f775SEmil Constantinescu 
51137453f775SEmil Constantinescu   gsum   = err_glb[0];
51147453f775SEmil Constantinescu   gsuma  = err_glb[1];
51157453f775SEmil Constantinescu   gsumr  = err_glb[2];
51167453f775SEmil Constantinescu   n_glb  = err_glb[3];
51177453f775SEmil Constantinescu   na_glb = err_glb[4];
51187453f775SEmil Constantinescu   nr_glb = err_glb[5];
51197453f775SEmil Constantinescu 
5120b1316ef9SEmil Constantinescu   *norm  = 0.;
5121b1316ef9SEmil Constantinescu   if (n_glb>0.) {*norm  = PetscSqrtReal(gsum  / n_glb);}
5122b1316ef9SEmil Constantinescu   *norma = 0.;
5123b1316ef9SEmil Constantinescu   if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);}
5124b1316ef9SEmil Constantinescu   *normr = 0.;
5125b1316ef9SEmil Constantinescu   if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);}
51269c6b16b5SShri Abhyankar 
51273c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
51283c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
51293c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
51309c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
51319c6b16b5SShri Abhyankar }
51329c6b16b5SShri Abhyankar 
51339c6b16b5SShri Abhyankar /*@
5134a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
51359c6b16b5SShri Abhyankar 
51369c6b16b5SShri Abhyankar    Collective on TS
51379c6b16b5SShri Abhyankar 
51384165533cSJose E. Roman    Input Parameters:
51399c6b16b5SShri Abhyankar +  ts - time stepping context
5140a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5141a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
51429c6b16b5SShri Abhyankar 
51434165533cSJose E. Roman    Output Parameters:
5144a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
51457453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5146a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
51479c6b16b5SShri Abhyankar 
51489c6b16b5SShri Abhyankar    Level: developer
51499c6b16b5SShri Abhyankar 
5150deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
51519c6b16b5SShri Abhyankar @*/
51527453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
51539c6b16b5SShri Abhyankar {
51547453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
51559c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
51567453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
51577453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
51587453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
51599c6b16b5SShri Abhyankar 
51609c6b16b5SShri Abhyankar   PetscFunctionBegin;
51619c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5162a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5163a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5164a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5165a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5166a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5167dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,4);
5168dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,5);
5169dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,6);
51703c633725SBarry Smith   PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
51719c6b16b5SShri Abhyankar 
51729566063dSJacob Faibussowitsch   PetscCall(VecGetSize(U,&N));
51739566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(U,&n));
51749566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(U,&rstart,NULL));
51759566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
51769566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
51777453f775SEmil Constantinescu 
51787453f775SEmil Constantinescu   max=0.;
51797453f775SEmil Constantinescu   maxa=0.;
51807453f775SEmil Constantinescu   maxr=0.;
51817453f775SEmil Constantinescu 
51827453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
51839c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
51849566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
51859566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
51867453f775SEmil Constantinescu 
51877453f775SEmil Constantinescu     for (i=0; i<n; i++) {
518876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
51897453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
51907453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
51917453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51927453f775SEmil Constantinescu       tol  = tola+tolr;
51937453f775SEmil Constantinescu       if (tola>0.) {
51947453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
51957453f775SEmil Constantinescu       }
51967453f775SEmil Constantinescu       if (tolr>0.) {
51977453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
51987453f775SEmil Constantinescu       }
51997453f775SEmil Constantinescu       if (tol>0.) {
52007453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52017453f775SEmil Constantinescu       }
52029c6b16b5SShri Abhyankar     }
52039566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
52049566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
52059c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
52069c6b16b5SShri Abhyankar     const PetscScalar *atol;
52079566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
52087453f775SEmil Constantinescu     for (i=0; i<n; i++) {
520976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52107453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52117453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
52127453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52137453f775SEmil Constantinescu       tol  = tola+tolr;
52147453f775SEmil Constantinescu       if (tola>0.) {
52157453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52167453f775SEmil Constantinescu       }
52177453f775SEmil Constantinescu       if (tolr>0.) {
52187453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52197453f775SEmil Constantinescu       }
52207453f775SEmil Constantinescu       if (tol>0.) {
52217453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52227453f775SEmil Constantinescu       }
52239c6b16b5SShri Abhyankar     }
52249566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
52259c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
52269c6b16b5SShri Abhyankar     const PetscScalar *rtol;
52279566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
52287453f775SEmil Constantinescu 
52297453f775SEmil Constantinescu     for (i=0; i<n; i++) {
523076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52317453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52327453f775SEmil Constantinescu       tola = ts->atol;
52337453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52347453f775SEmil Constantinescu       tol  = tola+tolr;
52357453f775SEmil Constantinescu       if (tola>0.) {
52367453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52377453f775SEmil Constantinescu       }
52387453f775SEmil Constantinescu       if (tolr>0.) {
52397453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52407453f775SEmil Constantinescu       }
52417453f775SEmil Constantinescu       if (tol>0.) {
52427453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52437453f775SEmil Constantinescu       }
52449c6b16b5SShri Abhyankar     }
52459566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
52469c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
52477453f775SEmil Constantinescu 
52487453f775SEmil Constantinescu     for (i=0; i<n; i++) {
524976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52507453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52517453f775SEmil Constantinescu       tola = ts->atol;
52527453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52537453f775SEmil Constantinescu       tol  = tola+tolr;
52547453f775SEmil Constantinescu       if (tola>0.) {
52557453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52567453f775SEmil Constantinescu       }
52577453f775SEmil Constantinescu       if (tolr>0.) {
52587453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52597453f775SEmil Constantinescu       }
52607453f775SEmil Constantinescu       if (tol>0.) {
52617453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52627453f775SEmil Constantinescu       }
52639c6b16b5SShri Abhyankar     }
52649c6b16b5SShri Abhyankar   }
52659566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
52669566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
52677453f775SEmil Constantinescu   err_loc[0] = max;
52687453f775SEmil Constantinescu   err_loc[1] = maxa;
52697453f775SEmil Constantinescu   err_loc[2] = maxr;
52701c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts)));
52717453f775SEmil Constantinescu   gmax   = err_glb[0];
52727453f775SEmil Constantinescu   gmaxa  = err_glb[1];
52737453f775SEmil Constantinescu   gmaxr  = err_glb[2];
52749c6b16b5SShri Abhyankar 
52759c6b16b5SShri Abhyankar   *norm = gmax;
52767453f775SEmil Constantinescu   *norma = gmaxa;
52777453f775SEmil Constantinescu   *normr = gmaxr;
52783c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
52793c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
52803c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
52819c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
52829c6b16b5SShri Abhyankar }
52839c6b16b5SShri Abhyankar 
52841c3436cfSJed Brown /*@
52858a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
52861c3436cfSJed Brown 
52871c3436cfSJed Brown    Collective on TS
52881c3436cfSJed Brown 
52894165533cSJose E. Roman    Input Parameters:
52901c3436cfSJed Brown +  ts - time stepping context
5291a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5292a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5293a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
52947619abb3SShri 
52954165533cSJose E. Roman    Output Parameters:
5296a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
52978a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5298a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5299a4868fbcSLisandro Dalcin 
5300a4868fbcSLisandro Dalcin    Options Database Keys:
5301a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5302a4868fbcSLisandro Dalcin 
53031c3436cfSJed Brown    Level: developer
53041c3436cfSJed Brown 
53058a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
53061c3436cfSJed Brown @*/
53077453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53081c3436cfSJed Brown {
53091c3436cfSJed Brown   PetscFunctionBegin;
5310a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
53119566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr));
5312a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
53139566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr));
531498921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
53151c3436cfSJed Brown   PetscFunctionReturn(0);
53161c3436cfSJed Brown }
53171c3436cfSJed Brown 
53188a175baeSEmil Constantinescu /*@
53198a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
53208a175baeSEmil Constantinescu 
53218a175baeSEmil Constantinescu    Collective on TS
53228a175baeSEmil Constantinescu 
53234165533cSJose E. Roman    Input Parameters:
53248a175baeSEmil Constantinescu +  ts - time stepping context
53258a175baeSEmil Constantinescu .  E - error vector
53268a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
53278a175baeSEmil Constantinescu -  Y - state vector, previous time step
53288a175baeSEmil Constantinescu 
53294165533cSJose E. Roman    Output Parameters:
5330a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
53318a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5332a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
53338a175baeSEmil Constantinescu 
53348a175baeSEmil Constantinescu    Level: developer
53358a175baeSEmil Constantinescu 
53368a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
53378a175baeSEmil Constantinescu @*/
53388a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53398a175baeSEmil Constantinescu {
53408a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
53418a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
53428a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
53438a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
53448a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
53458a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
53468a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
53478a175baeSEmil Constantinescu 
53488a175baeSEmil Constantinescu   PetscFunctionBegin;
53498a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53508a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
53518a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
53528a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
53538a175baeSEmil Constantinescu   PetscValidType(E,2);
53548a175baeSEmil Constantinescu   PetscValidType(U,3);
53558a175baeSEmil Constantinescu   PetscValidType(Y,4);
53568a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
5357064a246eSJacob Faibussowitsch   PetscCheckSameComm(U,3,Y,4);
5358dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,5);
5359dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,6);
5360dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,7);
53618a175baeSEmil Constantinescu 
53629566063dSJacob Faibussowitsch   PetscCall(VecGetSize(E,&N));
53639566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(E,&n));
53649566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(E,&rstart,NULL));
53659566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(E,&e));
53669566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
53679566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
53688a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
53698a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
53708a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
53718a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
53728a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
53739566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
53749566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
53758a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
537676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
53778a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
53788a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
53798a175baeSEmil Constantinescu       if (tola>0.) {
53808a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
53818a175baeSEmil Constantinescu         na_loc++;
53828a175baeSEmil Constantinescu       }
53838a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53848a175baeSEmil Constantinescu       if (tolr>0.) {
53858a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
53868a175baeSEmil Constantinescu         nr_loc++;
53878a175baeSEmil Constantinescu       }
53888a175baeSEmil Constantinescu       tol=tola+tolr;
53898a175baeSEmil Constantinescu       if (tol>0.) {
53908a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
53918a175baeSEmil Constantinescu         n_loc++;
53928a175baeSEmil Constantinescu       }
53938a175baeSEmil Constantinescu     }
53949566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
53959566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
53968a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
53978a175baeSEmil Constantinescu     const PetscScalar *atol;
53989566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
53998a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
540076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54018a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54028a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
54038a175baeSEmil Constantinescu       if (tola>0.) {
54048a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54058a175baeSEmil Constantinescu         na_loc++;
54068a175baeSEmil Constantinescu       }
54078a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54088a175baeSEmil Constantinescu       if (tolr>0.) {
54098a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54108a175baeSEmil Constantinescu         nr_loc++;
54118a175baeSEmil Constantinescu       }
54128a175baeSEmil Constantinescu       tol=tola+tolr;
54138a175baeSEmil Constantinescu       if (tol>0.) {
54148a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54158a175baeSEmil Constantinescu         n_loc++;
54168a175baeSEmil Constantinescu       }
54178a175baeSEmil Constantinescu     }
54189566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
54198a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54208a175baeSEmil Constantinescu     const PetscScalar *rtol;
54219566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
54228a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
542376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54248a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54258a175baeSEmil Constantinescu       tola = ts->atol;
54268a175baeSEmil Constantinescu       if (tola>0.) {
54278a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54288a175baeSEmil Constantinescu         na_loc++;
54298a175baeSEmil Constantinescu       }
54308a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54318a175baeSEmil Constantinescu       if (tolr>0.) {
54328a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54338a175baeSEmil Constantinescu         nr_loc++;
54348a175baeSEmil Constantinescu       }
54358a175baeSEmil Constantinescu       tol=tola+tolr;
54368a175baeSEmil Constantinescu       if (tol>0.) {
54378a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54388a175baeSEmil Constantinescu         n_loc++;
54398a175baeSEmil Constantinescu       }
54408a175baeSEmil Constantinescu     }
54419566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
54428a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
54438a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
544476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54458a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54468a175baeSEmil Constantinescu       tola = ts->atol;
54478a175baeSEmil Constantinescu       if (tola>0.) {
54488a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54498a175baeSEmil Constantinescu         na_loc++;
54508a175baeSEmil Constantinescu       }
54518a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54528a175baeSEmil Constantinescu       if (tolr>0.) {
54538a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54548a175baeSEmil Constantinescu         nr_loc++;
54558a175baeSEmil Constantinescu       }
54568a175baeSEmil Constantinescu       tol=tola+tolr;
54578a175baeSEmil Constantinescu       if (tol>0.) {
54588a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54598a175baeSEmil Constantinescu         n_loc++;
54608a175baeSEmil Constantinescu       }
54618a175baeSEmil Constantinescu     }
54628a175baeSEmil Constantinescu   }
54639566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(E,&e));
54649566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
54659566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
54668a175baeSEmil Constantinescu 
54678a175baeSEmil Constantinescu   err_loc[0] = sum;
54688a175baeSEmil Constantinescu   err_loc[1] = suma;
54698a175baeSEmil Constantinescu   err_loc[2] = sumr;
54708a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
54718a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
54728a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
54738a175baeSEmil Constantinescu 
54741c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts)));
54758a175baeSEmil Constantinescu 
54768a175baeSEmil Constantinescu   gsum   = err_glb[0];
54778a175baeSEmil Constantinescu   gsuma  = err_glb[1];
54788a175baeSEmil Constantinescu   gsumr  = err_glb[2];
54798a175baeSEmil Constantinescu   n_glb  = err_glb[3];
54808a175baeSEmil Constantinescu   na_glb = err_glb[4];
54818a175baeSEmil Constantinescu   nr_glb = err_glb[5];
54828a175baeSEmil Constantinescu 
54838a175baeSEmil Constantinescu   *norm  = 0.;
54848a175baeSEmil Constantinescu   if (n_glb>0.) {*norm  = PetscSqrtReal(gsum  / n_glb);}
54858a175baeSEmil Constantinescu   *norma = 0.;
54868a175baeSEmil Constantinescu   if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);}
54878a175baeSEmil Constantinescu   *normr = 0.;
54888a175baeSEmil Constantinescu   if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);}
54898a175baeSEmil Constantinescu 
54903c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
54913c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
54923c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
54938a175baeSEmil Constantinescu   PetscFunctionReturn(0);
54948a175baeSEmil Constantinescu }
54958a175baeSEmil Constantinescu 
54968a175baeSEmil Constantinescu /*@
54978a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
54988a175baeSEmil Constantinescu    Collective on TS
54998a175baeSEmil Constantinescu 
55004165533cSJose E. Roman    Input Parameters:
55018a175baeSEmil Constantinescu +  ts - time stepping context
55028a175baeSEmil Constantinescu .  E - error vector
55038a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
55048a175baeSEmil Constantinescu -  Y - state vector, previous time step
55058a175baeSEmil Constantinescu 
55064165533cSJose E. Roman    Output Parameters:
5507a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55088a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5509a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55108a175baeSEmil Constantinescu 
55118a175baeSEmil Constantinescu    Level: developer
55128a175baeSEmil Constantinescu 
55138a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
55148a175baeSEmil Constantinescu @*/
55158a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55168a175baeSEmil Constantinescu {
55178a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
55188a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
55198a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
55208a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
55218a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
55228a175baeSEmil Constantinescu 
55238a175baeSEmil Constantinescu   PetscFunctionBegin;
55248a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55258a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
55268a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
55278a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
55288a175baeSEmil Constantinescu   PetscValidType(E,2);
55298a175baeSEmil Constantinescu   PetscValidType(U,3);
55308a175baeSEmil Constantinescu   PetscValidType(Y,4);
55318a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
5532064a246eSJacob Faibussowitsch   PetscCheckSameComm(U,3,Y,4);
5533dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norm,5);
5534dadcf809SJacob Faibussowitsch   PetscValidRealPointer(norma,6);
5535dadcf809SJacob Faibussowitsch   PetscValidRealPointer(normr,7);
55368a175baeSEmil Constantinescu 
55379566063dSJacob Faibussowitsch   PetscCall(VecGetSize(E,&N));
55389566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(E,&n));
55399566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(E,&rstart,NULL));
55409566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(E,&e));
55419566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(U,&u));
55429566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(Y,&y));
55438a175baeSEmil Constantinescu 
55448a175baeSEmil Constantinescu   max=0.;
55458a175baeSEmil Constantinescu   maxa=0.;
55468a175baeSEmil Constantinescu   maxr=0.;
55478a175baeSEmil Constantinescu 
55488a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
55498a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
55509566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
55519566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
55528a175baeSEmil Constantinescu 
55538a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
555476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55558a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
55568a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
55578a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55588a175baeSEmil Constantinescu       tol  = tola+tolr;
55598a175baeSEmil Constantinescu       if (tola>0.) {
55608a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
55618a175baeSEmil Constantinescu       }
55628a175baeSEmil Constantinescu       if (tolr>0.) {
55638a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
55648a175baeSEmil Constantinescu       }
55658a175baeSEmil Constantinescu       if (tol>0.) {
55668a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
55678a175baeSEmil Constantinescu       }
55688a175baeSEmil Constantinescu     }
55699566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
55709566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
55718a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55728a175baeSEmil Constantinescu     const PetscScalar *atol;
55739566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vatol,&atol));
55748a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
557576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55768a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
55778a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
55788a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55798a175baeSEmil Constantinescu       tol  = tola+tolr;
55808a175baeSEmil Constantinescu       if (tola>0.) {
55818a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
55828a175baeSEmil Constantinescu       }
55838a175baeSEmil Constantinescu       if (tolr>0.) {
55848a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
55858a175baeSEmil Constantinescu       }
55868a175baeSEmil Constantinescu       if (tol>0.) {
55878a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
55888a175baeSEmil Constantinescu       }
55898a175baeSEmil Constantinescu     }
55909566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vatol,&atol));
55918a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55928a175baeSEmil Constantinescu     const PetscScalar *rtol;
55939566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(ts->vrtol,&rtol));
55948a175baeSEmil Constantinescu 
55958a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
559676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55978a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
55988a175baeSEmil Constantinescu       tola = ts->atol;
55998a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56008a175baeSEmil Constantinescu       tol  = tola+tolr;
56018a175baeSEmil Constantinescu       if (tola>0.) {
56028a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
56038a175baeSEmil Constantinescu       }
56048a175baeSEmil Constantinescu       if (tolr>0.) {
56058a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
56068a175baeSEmil Constantinescu       }
56078a175baeSEmil Constantinescu       if (tol>0.) {
56088a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
56098a175baeSEmil Constantinescu       }
56108a175baeSEmil Constantinescu     }
56119566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol));
56128a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
56138a175baeSEmil Constantinescu 
56148a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
561576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56168a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
56178a175baeSEmil Constantinescu       tola = ts->atol;
56188a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56198a175baeSEmil Constantinescu       tol  = tola+tolr;
56208a175baeSEmil Constantinescu       if (tola>0.) {
56218a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
56228a175baeSEmil Constantinescu       }
56238a175baeSEmil Constantinescu       if (tolr>0.) {
56248a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
56258a175baeSEmil Constantinescu       }
56268a175baeSEmil Constantinescu       if (tol>0.) {
56278a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
56288a175baeSEmil Constantinescu       }
56298a175baeSEmil Constantinescu     }
56308a175baeSEmil Constantinescu   }
56319566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(E,&e));
56329566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(U,&u));
56339566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(Y,&y));
56348a175baeSEmil Constantinescu   err_loc[0] = max;
56358a175baeSEmil Constantinescu   err_loc[1] = maxa;
56368a175baeSEmil Constantinescu   err_loc[2] = maxr;
56371c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts)));
56388a175baeSEmil Constantinescu   gmax   = err_glb[0];
56398a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
56408a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
56418a175baeSEmil Constantinescu 
56428a175baeSEmil Constantinescu   *norm = gmax;
56438a175baeSEmil Constantinescu   *norma = gmaxa;
56448a175baeSEmil Constantinescu   *normr = gmaxr;
56453c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56463c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56473c633725SBarry Smith   PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56488a175baeSEmil Constantinescu   PetscFunctionReturn(0);
56498a175baeSEmil Constantinescu }
56508a175baeSEmil Constantinescu 
56518a175baeSEmil Constantinescu /*@
56528a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
56538a175baeSEmil Constantinescu 
56548a175baeSEmil Constantinescu    Collective on TS
56558a175baeSEmil Constantinescu 
56564165533cSJose E. Roman    Input Parameters:
56578a175baeSEmil Constantinescu +  ts - time stepping context
56588a175baeSEmil Constantinescu .  E - error vector
56598a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
56608a175baeSEmil Constantinescu .  Y - state vector, previous time step
56618a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
56628a175baeSEmil Constantinescu 
56634165533cSJose E. Roman    Output Parameters:
5664a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
56658a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5666a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
56678a175baeSEmil Constantinescu 
56688a175baeSEmil Constantinescu    Options Database Keys:
56698a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
56708a175baeSEmil Constantinescu 
56718a175baeSEmil Constantinescu    Level: developer
56728a175baeSEmil Constantinescu 
56738a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
56748a175baeSEmil Constantinescu @*/
56758a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56768a175baeSEmil Constantinescu {
56778a175baeSEmil Constantinescu   PetscFunctionBegin;
56788a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
56799566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr));
56808a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
56819566063dSJacob Faibussowitsch     PetscCall(TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr));
568298921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
56838a175baeSEmil Constantinescu   PetscFunctionReturn(0);
56848a175baeSEmil Constantinescu }
56858a175baeSEmil Constantinescu 
56868d59e960SJed Brown /*@
56878d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
56888d59e960SJed Brown 
56898d59e960SJed Brown    Logically Collective on TS
56908d59e960SJed Brown 
56914165533cSJose E. Roman    Input Parameters:
56928d59e960SJed Brown +  ts - time stepping context
56938d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
56948d59e960SJed Brown 
56958d59e960SJed Brown    Note:
56968d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
56978d59e960SJed Brown 
56988d59e960SJed Brown    Level: intermediate
56998d59e960SJed Brown 
57008d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
57018d59e960SJed Brown @*/
57028d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
57038d59e960SJed Brown {
57048d59e960SJed Brown   PetscFunctionBegin;
57058d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57068d59e960SJed Brown   ts->cfltime_local = cfltime;
57078d59e960SJed Brown   ts->cfltime       = -1.;
57088d59e960SJed Brown   PetscFunctionReturn(0);
57098d59e960SJed Brown }
57108d59e960SJed Brown 
57118d59e960SJed Brown /*@
57128d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
57138d59e960SJed Brown 
57148d59e960SJed Brown    Collective on TS
57158d59e960SJed Brown 
57164165533cSJose E. Roman    Input Parameter:
57178d59e960SJed Brown .  ts - time stepping context
57188d59e960SJed Brown 
57194165533cSJose E. Roman    Output Parameter:
57208d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
57218d59e960SJed Brown 
57228d59e960SJed Brown    Level: advanced
57238d59e960SJed Brown 
57248d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
57258d59e960SJed Brown @*/
57268d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
57278d59e960SJed Brown {
57288d59e960SJed Brown   PetscFunctionBegin;
57298d59e960SJed Brown   if (ts->cfltime < 0) {
57301c2dc1cbSBarry Smith     PetscCall(MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts)));
57318d59e960SJed Brown   }
57328d59e960SJed Brown   *cfltime = ts->cfltime;
57338d59e960SJed Brown   PetscFunctionReturn(0);
57348d59e960SJed Brown }
57358d59e960SJed Brown 
5736d6ebe24aSShri Abhyankar /*@
5737d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5738d6ebe24aSShri Abhyankar 
5739d6ebe24aSShri Abhyankar    Input Parameters:
5740a2b725a8SWilliam Gropp +  ts   - the TS context.
5741d6ebe24aSShri Abhyankar .  xl   - lower bound.
5742a2b725a8SWilliam Gropp -  xu   - upper bound.
5743d6ebe24aSShri Abhyankar 
5744d6ebe24aSShri Abhyankar    Notes:
5745d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5746e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5747d6ebe24aSShri Abhyankar 
57482bd2b0e6SSatish Balay    Level: advanced
57492bd2b0e6SSatish Balay 
5750d6ebe24aSShri Abhyankar @*/
5751d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5752d6ebe24aSShri Abhyankar {
5753d6ebe24aSShri Abhyankar   SNES           snes;
5754d6ebe24aSShri Abhyankar 
5755d6ebe24aSShri Abhyankar   PetscFunctionBegin;
57569566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts,&snes));
57579566063dSJacob Faibussowitsch   PetscCall(SNESVISetVariableBounds(snes,xl,xu));
5758d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5759d6ebe24aSShri Abhyankar }
5760d6ebe24aSShri Abhyankar 
5761f9c1d6abSBarry Smith /*@
5762f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5763f9c1d6abSBarry Smith 
5764d083f849SBarry Smith    Collective on TS
5765f9c1d6abSBarry Smith 
5766f9c1d6abSBarry Smith    Input Parameters:
5767f9c1d6abSBarry Smith +  ts - the TS context
5768f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5769f9c1d6abSBarry Smith 
5770f9c1d6abSBarry Smith    Output Parameters:
5771f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5772f9c1d6abSBarry Smith 
5773f9c1d6abSBarry Smith    Level: developer
5774f9c1d6abSBarry Smith 
5775f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5776f9c1d6abSBarry Smith @*/
5777f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5778f9c1d6abSBarry Smith {
5779f9c1d6abSBarry Smith   PetscFunctionBegin;
5780f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57813c633725SBarry Smith   PetscCheck(ts->ops->linearstability,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
57829566063dSJacob Faibussowitsch   PetscCall((*ts->ops->linearstability)(ts,xr,xi,yr,yi));
5783f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5784f9c1d6abSBarry Smith }
578524655328SShri 
578624655328SShri /*@
5787dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
5788dcb233daSLisandro Dalcin 
5789dcb233daSLisandro Dalcin    Collective on TS
5790dcb233daSLisandro Dalcin 
5791dcb233daSLisandro Dalcin    Input Parameter:
5792dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
5793dcb233daSLisandro Dalcin 
5794dcb233daSLisandro Dalcin    Level: advanced
5795dcb233daSLisandro Dalcin 
5796dcb233daSLisandro Dalcin    Notes:
5797dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
5798dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
5799dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
5800dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
5801dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
5802dcb233daSLisandro Dalcin    discontinuous source terms).
5803dcb233daSLisandro Dalcin 
5804dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
5805dcb233daSLisandro Dalcin @*/
5806dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
5807dcb233daSLisandro Dalcin {
5808dcb233daSLisandro Dalcin   PetscFunctionBegin;
5809dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5810dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
5811dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
5812dcb233daSLisandro Dalcin }
5813dcb233daSLisandro Dalcin 
5814dcb233daSLisandro Dalcin /*@
581524655328SShri    TSRollBack - Rolls back one time step
581624655328SShri 
581724655328SShri    Collective on TS
581824655328SShri 
581924655328SShri    Input Parameter:
582024655328SShri .  ts - the TS context obtained from TSCreate()
582124655328SShri 
582224655328SShri    Level: advanced
582324655328SShri 
582424655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
582524655328SShri @*/
582624655328SShri PetscErrorCode  TSRollBack(TS ts)
582724655328SShri {
582824655328SShri   PetscFunctionBegin;
582924655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
58303c633725SBarry Smith   PetscCheck(!ts->steprollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
58313c633725SBarry Smith   PetscCheck(ts->ops->rollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
58329566063dSJacob Faibussowitsch   PetscCall((*ts->ops->rollback)(ts));
583324655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
583424655328SShri   ts->ptime = ts->ptime_prev;
5835be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
58362808aa04SLisandro Dalcin   ts->steps--;
5837b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
583824655328SShri   PetscFunctionReturn(0);
583924655328SShri }
5840aeb4809dSShri Abhyankar 
5841ff22ae23SHong Zhang /*@
5842ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5843ff22ae23SHong Zhang 
5844ff22ae23SHong Zhang    Input Parameter:
5845ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5846ff22ae23SHong Zhang 
58470429704eSStefano Zampini    Output Parameters:
58480429704eSStefano Zampini +  ns - the number of stages
58490429704eSStefano Zampini -  Y - the current stage vectors
58500429704eSStefano Zampini 
5851ff22ae23SHong Zhang    Level: advanced
5852ff22ae23SHong Zhang 
58530429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
58540429704eSStefano Zampini 
5855ff22ae23SHong Zhang .seealso: TSCreate()
5856ff22ae23SHong Zhang @*/
5857ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
5858ff22ae23SHong Zhang {
5859ff22ae23SHong Zhang   PetscFunctionBegin;
5860ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5861dadcf809SJacob Faibussowitsch   if (ns) PetscValidIntPointer(ns,2);
58620429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
58630429704eSStefano Zampini   if (!ts->ops->getstages) {
58640429704eSStefano Zampini     if (ns) *ns = 0;
58650429704eSStefano Zampini     if (Y) *Y = NULL;
58660429704eSStefano Zampini   } else {
58679566063dSJacob Faibussowitsch     PetscCall((*ts->ops->getstages)(ts,ns,Y));
5868ff22ae23SHong Zhang   }
5869ff22ae23SHong Zhang   PetscFunctionReturn(0);
5870ff22ae23SHong Zhang }
5871ff22ae23SHong Zhang 
5872847ff0e1SMatthew G. Knepley /*@C
5873847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
5874847ff0e1SMatthew G. Knepley 
5875847ff0e1SMatthew G. Knepley   Collective on SNES
5876847ff0e1SMatthew G. Knepley 
5877847ff0e1SMatthew G. Knepley   Input Parameters:
5878847ff0e1SMatthew G. Knepley + ts - the TS context
5879847ff0e1SMatthew G. Knepley . t - current timestep
5880847ff0e1SMatthew G. Knepley . U - state vector
5881847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
5882847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
5883847ff0e1SMatthew G. Knepley - ctx - an optional user context
5884847ff0e1SMatthew G. Knepley 
5885847ff0e1SMatthew G. Knepley   Output Parameters:
5886847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
5887847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
5888847ff0e1SMatthew G. Knepley 
5889847ff0e1SMatthew G. Knepley   Level: intermediate
5890847ff0e1SMatthew G. Knepley 
5891847ff0e1SMatthew G. Knepley   Notes:
5892847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
5893847ff0e1SMatthew G. Knepley 
5894847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
5895847ff0e1SMatthew G. Knepley 
5896847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
5897847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
5898847ff0e1SMatthew G. Knepley 
5899847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
5900847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
5901847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
5902847ff0e1SMatthew G. Knepley 
5903847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
5904847ff0e1SMatthew G. Knepley @*/
5905847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
5906847ff0e1SMatthew G. Knepley {
5907847ff0e1SMatthew G. Knepley   SNES           snes;
5908847ff0e1SMatthew G. Knepley   MatFDColoring  color;
5909847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
5910847ff0e1SMatthew G. Knepley 
5911847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
59129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL));
59139566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color));
5914847ff0e1SMatthew G. Knepley   if (!color) {
5915847ff0e1SMatthew G. Knepley     DM         dm;
5916847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
5917847ff0e1SMatthew G. Knepley 
59189566063dSJacob Faibussowitsch     PetscCall(TSGetDM(ts, &dm));
59199566063dSJacob Faibussowitsch     PetscCall(DMHasColoring(dm, &hascolor));
5920847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
59219566063dSJacob Faibussowitsch       PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring));
59229566063dSJacob Faibussowitsch       PetscCall(MatFDColoringCreate(B, iscoloring, &color));
59239566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts));
59249566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFromOptions(color));
59259566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetUp(B, iscoloring, color));
59269566063dSJacob Faibussowitsch       PetscCall(ISColoringDestroy(&iscoloring));
5927847ff0e1SMatthew G. Knepley     } else {
5928847ff0e1SMatthew G. Knepley       MatColoring mc;
5929847ff0e1SMatthew G. Knepley 
59309566063dSJacob Faibussowitsch       PetscCall(MatColoringCreate(B, &mc));
59319566063dSJacob Faibussowitsch       PetscCall(MatColoringSetDistance(mc, 2));
59329566063dSJacob Faibussowitsch       PetscCall(MatColoringSetType(mc, MATCOLORINGSL));
59339566063dSJacob Faibussowitsch       PetscCall(MatColoringSetFromOptions(mc));
59349566063dSJacob Faibussowitsch       PetscCall(MatColoringApply(mc, &iscoloring));
59359566063dSJacob Faibussowitsch       PetscCall(MatColoringDestroy(&mc));
59369566063dSJacob Faibussowitsch       PetscCall(MatFDColoringCreate(B, iscoloring, &color));
59379566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts));
59389566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetFromOptions(color));
59399566063dSJacob Faibussowitsch       PetscCall(MatFDColoringSetUp(B, iscoloring, color));
59409566063dSJacob Faibussowitsch       PetscCall(ISColoringDestroy(&iscoloring));
5941847ff0e1SMatthew G. Knepley     }
59429566063dSJacob Faibussowitsch     PetscCall(PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color));
59439566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject) color));
5944847ff0e1SMatthew G. Knepley   }
59459566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(ts, &snes));
59469566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply(B, color, U, snes));
5947847ff0e1SMatthew G. Knepley   if (J != B) {
59489566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
59499566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
5950847ff0e1SMatthew G. Knepley   }
5951847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
5952847ff0e1SMatthew G. Knepley }
595393b34091SDebojyoti Ghosh 
5954cb9d8021SPierre Barbier de Reuille /*@
59556bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
5956cb9d8021SPierre Barbier de Reuille 
5957cb9d8021SPierre Barbier de Reuille     Input Parameters:
59586bc98fa9SBarry Smith +    ts - the TS context
59596bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
59606bc98fa9SBarry Smith 
59616bc98fa9SBarry Smith     Calling sequence of func:
59626bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
59636bc98fa9SBarry Smith 
59646bc98fa9SBarry Smith +   ts - the TS context
59656bc98fa9SBarry Smith .   time - the current time (of the stage)
59666bc98fa9SBarry Smith .   state - the state to check if it is valid
59676bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
5968cb9d8021SPierre Barbier de Reuille 
5969cb9d8021SPierre Barbier de Reuille     Level: intermediate
5970cb9d8021SPierre Barbier de Reuille 
59716bc98fa9SBarry Smith     Notes:
59726bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
59736bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
59746bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
59756bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
59766bc98fa9SBarry Smith 
59776bc98fa9SBarry Smith     Developer Notes:
59786bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
59796bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
59806bc98fa9SBarry Smith 
59816bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
5982cb9d8021SPierre Barbier de Reuille @*/
5983cb9d8021SPierre Barbier de Reuille 
5984d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
5985cb9d8021SPierre Barbier de Reuille {
5986cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
5987cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5988cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
5989cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
5990cb9d8021SPierre Barbier de Reuille }
5991cb9d8021SPierre Barbier de Reuille 
5992cb9d8021SPierre Barbier de Reuille /*@
59936bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
5994cb9d8021SPierre Barbier de Reuille 
5995cb9d8021SPierre Barbier de Reuille     Input Parameters:
59966bc98fa9SBarry Smith +    ts - the TS context
59976bc98fa9SBarry Smith .    stagetime - time of the simulation
59986bc98fa9SBarry Smith -    Y - state vector to check.
5999cb9d8021SPierre Barbier de Reuille 
6000cb9d8021SPierre Barbier de Reuille     Output Parameter:
60016bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
6002cb9d8021SPierre Barbier de Reuille 
6003cb9d8021SPierre Barbier de Reuille     Note:
60046bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
60056bc98fa9SBarry Smith     to check if the current state is valid.
600696a0c994SBarry Smith 
60076bc98fa9SBarry Smith     Level: developer
60086bc98fa9SBarry Smith 
60096bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
6010cb9d8021SPierre Barbier de Reuille @*/
6011d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
6012cb9d8021SPierre Barbier de Reuille {
6013cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6014cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6015cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
6016cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
6017a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->functiondomainerror),ts,stagetime,Y,accept);
6018cb9d8021SPierre Barbier de Reuille   }
6019cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6020cb9d8021SPierre Barbier de Reuille }
60211ceb14c0SBarry Smith 
602293b34091SDebojyoti Ghosh /*@C
6023e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
602493b34091SDebojyoti Ghosh 
6025d083f849SBarry Smith   Collective
602693b34091SDebojyoti Ghosh 
602793b34091SDebojyoti Ghosh   Input Parameter:
602893b34091SDebojyoti Ghosh . tsin    - The input TS
602993b34091SDebojyoti Ghosh 
603093b34091SDebojyoti Ghosh   Output Parameter:
6031e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
603293b34091SDebojyoti Ghosh 
60335eca1a21SEmil Constantinescu   Notes:
60345eca1a21SEmil 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.
60355eca1a21SEmil Constantinescu 
6036928bb9adSStefano 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);
60375eca1a21SEmil Constantinescu 
60385eca1a21SEmil Constantinescu   Level: developer
603993b34091SDebojyoti Ghosh 
6040e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
604193b34091SDebojyoti Ghosh @*/
6042baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
604393b34091SDebojyoti Ghosh {
604493b34091SDebojyoti Ghosh   TS             t;
6045dc846ba4SSatish Balay   SNES           snes_start;
6046dc846ba4SSatish Balay   DM             dm;
6047dc846ba4SSatish Balay   TSType         type;
604893b34091SDebojyoti Ghosh 
604993b34091SDebojyoti Ghosh   PetscFunctionBegin;
605093b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
605193b34091SDebojyoti Ghosh   *tsout = NULL;
605293b34091SDebojyoti Ghosh 
60539566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView));
605493b34091SDebojyoti Ghosh 
605593b34091SDebojyoti Ghosh   /* General TS description */
605693b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
6057d0c080abSJoseph Pusztay   t->monitorFrequency  = 1;
605893b34091SDebojyoti Ghosh   t->setupcalled       = 0;
605993b34091SDebojyoti Ghosh   t->ksp_its           = 0;
606093b34091SDebojyoti Ghosh   t->snes_its          = 0;
606193b34091SDebojyoti Ghosh   t->nwork             = 0;
60627d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
606393b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
606493b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
606593b34091SDebojyoti Ghosh 
60669566063dSJacob Faibussowitsch   PetscCall(TSGetSNES(tsin,&snes_start));
60679566063dSJacob Faibussowitsch   PetscCall(TSSetSNES(t,snes_start));
6068d15a3a53SEmil Constantinescu 
60699566063dSJacob Faibussowitsch   PetscCall(TSGetDM(tsin,&dm));
60709566063dSJacob Faibussowitsch   PetscCall(TSSetDM(t,dm));
607193b34091SDebojyoti Ghosh 
607293b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
60739566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)t->adapt));
607493b34091SDebojyoti Ghosh 
6075e7069c78SShri   t->trajectory = tsin->trajectory;
60769566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)t->trajectory));
6077e7069c78SShri 
6078e7069c78SShri   t->event = tsin->event;
60796b10a48eSSatish Balay   if (t->event) t->event->refct++;
6080e7069c78SShri 
608193b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
608293b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
6083e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
608493b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
608593b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
608693b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
608793b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
608893b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
608993b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
609093b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
609193b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
609293b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
609393b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
609493b34091SDebojyoti Ghosh 
60959566063dSJacob Faibussowitsch   PetscCall(TSGetType(tsin,&type));
60969566063dSJacob Faibussowitsch   PetscCall(TSSetType(t,type));
609793b34091SDebojyoti Ghosh 
609893b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
609993b34091SDebojyoti Ghosh 
610093b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
610193b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
610293b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
610393b34091SDebojyoti Ghosh 
61049566063dSJacob Faibussowitsch   PetscCall(PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps)));
610593b34091SDebojyoti Ghosh 
61060d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
61070d4fed19SBarry Smith     PetscInt i;
61089566063dSJacob Faibussowitsch     PetscCall(PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers));
61090d4fed19SBarry Smith     for (i=0; i<10; i++) {
61100d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
61110d4fed19SBarry Smith     }
61120d4fed19SBarry Smith   }
611393b34091SDebojyoti Ghosh   *tsout = t;
611493b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
611593b34091SDebojyoti Ghosh }
6116f3b1f45cSBarry Smith 
6117f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
6118f3b1f45cSBarry Smith {
6119f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
6120f3b1f45cSBarry Smith 
6121f3b1f45cSBarry Smith   PetscFunctionBegin;
61229566063dSJacob Faibussowitsch   PetscCall(TSComputeRHSFunction(ts,0,x,y));
6123f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6124f3b1f45cSBarry Smith }
6125f3b1f45cSBarry Smith 
6126f3b1f45cSBarry Smith /*@
6127f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
6128f3b1f45cSBarry Smith 
6129d083f849SBarry Smith    Logically Collective on TS
6130f3b1f45cSBarry Smith 
6131f3b1f45cSBarry Smith     Input Parameters:
6132f3b1f45cSBarry Smith     TS - the time stepping routine
6133f3b1f45cSBarry Smith 
6134f3b1f45cSBarry Smith    Output Parameter:
6135f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
6136f3b1f45cSBarry Smith 
6137f3b1f45cSBarry Smith    Options Database:
6138f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
6139f3b1f45cSBarry Smith 
6140f3b1f45cSBarry Smith    Level: advanced
6141f3b1f45cSBarry Smith 
614295452b02SPatrick Sanan    Notes:
614395452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
6144f3b1f45cSBarry Smith 
6145f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
6146f3b1f45cSBarry Smith @*/
6147f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
6148f3b1f45cSBarry Smith {
6149f3b1f45cSBarry Smith   Mat            J,B;
6150f3b1f45cSBarry Smith   TSRHSJacobian  func;
6151f3b1f45cSBarry Smith   void*          ctx;
6152f3b1f45cSBarry Smith 
6153f3b1f45cSBarry Smith   PetscFunctionBegin;
61549566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx));
61559566063dSJacob Faibussowitsch   PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx));
61569566063dSJacob Faibussowitsch   PetscCall(MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg));
6157f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6158f3b1f45cSBarry Smith }
6159f3b1f45cSBarry Smith 
6160f3b1f45cSBarry Smith /*@C
6161f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
6162f3b1f45cSBarry Smith 
6163d083f849SBarry Smith    Logically Collective on TS
6164f3b1f45cSBarry Smith 
6165f3b1f45cSBarry Smith     Input Parameters:
6166f3b1f45cSBarry Smith     TS - the time stepping routine
6167f3b1f45cSBarry Smith 
6168f3b1f45cSBarry Smith    Output Parameter:
6169f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
6170f3b1f45cSBarry Smith 
6171f3b1f45cSBarry Smith    Options Database:
6172f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
6173f3b1f45cSBarry Smith 
617495452b02SPatrick Sanan    Notes:
617595452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
6176f3b1f45cSBarry Smith 
6177f3b1f45cSBarry Smith    Level: advanced
6178f3b1f45cSBarry Smith 
6179f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
6180f3b1f45cSBarry Smith @*/
6181f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
6182f3b1f45cSBarry Smith {
6183f3b1f45cSBarry Smith   Mat            J,B;
6184f3b1f45cSBarry Smith   void           *ctx;
6185f3b1f45cSBarry Smith   TSRHSJacobian  func;
6186f3b1f45cSBarry Smith 
6187f3b1f45cSBarry Smith   PetscFunctionBegin;
61889566063dSJacob Faibussowitsch   PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx));
61899566063dSJacob Faibussowitsch   PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx));
61909566063dSJacob Faibussowitsch   PetscCall(MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg));
6191f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6192f3b1f45cSBarry Smith }
61930fe4d17eSHong Zhang 
61940fe4d17eSHong Zhang /*@
61950fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
61960fe4d17eSHong Zhang 
61970fe4d17eSHong Zhang   Logically collective
61980fe4d17eSHong Zhang 
6199d8d19677SJose E. Roman   Input Parameters:
62000fe4d17eSHong Zhang +  ts - timestepping context
62010fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
62020fe4d17eSHong Zhang 
62030fe4d17eSHong Zhang   Options Database:
62040fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
62050fe4d17eSHong Zhang 
62060fe4d17eSHong Zhang   Notes:
62070fe4d17eSHong Zhang     This is only useful for multirate methods
62080fe4d17eSHong Zhang 
62090fe4d17eSHong Zhang   Level: intermediate
62100fe4d17eSHong Zhang 
62110fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
62120fe4d17eSHong Zhang @*/
62130fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
62140fe4d17eSHong Zhang {
62150fe4d17eSHong Zhang   PetscFunctionBegin;
62160fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62170fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
62180fe4d17eSHong Zhang   PetscFunctionReturn(0);
62190fe4d17eSHong Zhang }
62200fe4d17eSHong Zhang 
62210fe4d17eSHong Zhang /*@
62220fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
62230fe4d17eSHong Zhang 
62240fe4d17eSHong Zhang   Not collective
62250fe4d17eSHong Zhang 
62260fe4d17eSHong Zhang   Input Parameter:
62270fe4d17eSHong Zhang .  ts - timestepping context
62280fe4d17eSHong Zhang 
62290fe4d17eSHong Zhang   Output Parameter:
62300fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
62310fe4d17eSHong Zhang 
62320fe4d17eSHong Zhang   Level: intermediate
62330fe4d17eSHong Zhang 
62340fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
62350fe4d17eSHong Zhang @*/
62360fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
62370fe4d17eSHong Zhang {
62380fe4d17eSHong Zhang   PetscFunctionBegin;
62390fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62400fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
62410fe4d17eSHong Zhang   PetscFunctionReturn(0);
62420fe4d17eSHong Zhang }
6243d60b7d5cSBarry Smith 
6244d60b7d5cSBarry Smith /*@
6245d60b7d5cSBarry Smith     TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix.
6246d60b7d5cSBarry Smith 
6247d60b7d5cSBarry Smith    Logically  Collective on ts
6248d60b7d5cSBarry Smith 
6249d60b7d5cSBarry Smith    Input Parameters:
6250d60b7d5cSBarry Smith +  ts - the time-stepper
6251d60b7d5cSBarry Smith -  str - the structure (the default is UNKNOWN_NONZERO_PATTERN)
6252d60b7d5cSBarry Smith 
6253d60b7d5cSBarry Smith    Level: intermediate
6254d60b7d5cSBarry Smith 
6255d60b7d5cSBarry Smith    Notes:
6256d60b7d5cSBarry Smith      When the relationship between the nonzero structures is known and supplied the solution process can be much faster
6257d60b7d5cSBarry Smith 
6258d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure
6259d60b7d5cSBarry Smith  @*/
6260d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str)
6261d60b7d5cSBarry Smith {
6262d60b7d5cSBarry Smith   PetscFunctionBegin;
6263d60b7d5cSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6264d60b7d5cSBarry Smith   ts->axpy_pattern = str;
6265d60b7d5cSBarry Smith   PetscFunctionReturn(0);
6266d60b7d5cSBarry Smith }
6267*4a658b32SHong Zhang 
6268*4a658b32SHong Zhang /*@
6269*4a658b32SHong Zhang   TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested.
6270*4a658b32SHong Zhang 
6271*4a658b32SHong Zhang   Collective on ts
6272*4a658b32SHong Zhang 
6273*4a658b32SHong Zhang   Input Parameters:
6274*4a658b32SHong Zhang + ts - the time-stepper
6275*4a658b32SHong Zhang . n - number of the time points (>=2)
6276*4a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively.
6277*4a658b32SHong Zhang 
6278*4a658b32SHong Zhang   Options Database Keys:
6279*4a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span
6280*4a658b32SHong Zhang 
6281*4a658b32SHong Zhang   Level: beginner
6282*4a658b32SHong Zhang 
6283*4a658b32SHong Zhang   Notes:
6284*4a658b32SHong Zhang   The elements in tspan must be all increasing. They correspond to the intermediate points for time integration.
6285*4a658b32SHong Zhang   TS_EXACTFINALTIME_MATCHSTEP must be used to make the last time step in each sub-interval match the intermediate points specified.
6286*4a658b32SHong Zhang   The intermediate solutions are saved in a vector array that can be accessed with TSGetSolutions(). Thus using time span may
6287*4a658b32SHong Zhang   pressure the memory system when using a large number of span points.
6288*4a658b32SHong Zhang 
6289*4a658b32SHong Zhang .seealso: TSGetTimeSpan(),TSGetSolutions()
6290*4a658b32SHong Zhang  @*/
6291*4a658b32SHong Zhang PetscErrorCode TSSetTimeSpan(TS ts,PetscInt n,PetscReal *span_times)
6292*4a658b32SHong Zhang {
6293*4a658b32SHong Zhang   PetscFunctionBegin;
6294*4a658b32SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6295*4a658b32SHong Zhang   PetscCheck(n >= 2,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Minimum time span size is 2 but %D is provided",n);
6296*4a658b32SHong Zhang   if (ts->tspan && n != ts->tspan->num_span_times) {
6297*4a658b32SHong Zhang     PetscCall(PetscFree(ts->tspan->span_times));
6298*4a658b32SHong Zhang     PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol));
6299*4a658b32SHong Zhang     PetscCall(PetscMalloc1(n,&ts->tspan->span_times));
6300*4a658b32SHong Zhang   }
6301*4a658b32SHong Zhang   if (!ts->tspan) {
6302*4a658b32SHong Zhang     TSTimeSpan tspan;
6303*4a658b32SHong Zhang     PetscCall(PetscNew(&tspan));
6304*4a658b32SHong Zhang     PetscCall(PetscMalloc1(n,&tspan->span_times));
6305*4a658b32SHong Zhang     ts->tspan = tspan;
6306*4a658b32SHong Zhang   }
6307*4a658b32SHong Zhang   ts->tspan->num_span_times = n;
6308*4a658b32SHong Zhang   PetscCall(PetscArraycpy(ts->tspan->span_times,span_times,n));
6309*4a658b32SHong Zhang   PetscCall(TSSetTime(ts,ts->tspan->span_times[0]));
6310*4a658b32SHong Zhang   PetscCall(TSSetMaxTime(ts,ts->tspan->span_times[n-1]));
6311*4a658b32SHong Zhang   PetscFunctionReturn(0);
6312*4a658b32SHong Zhang }
6313*4a658b32SHong Zhang 
6314*4a658b32SHong Zhang /*@C
6315*4a658b32SHong Zhang   TSGetTimeSpan - gets the time span.
6316*4a658b32SHong Zhang 
6317*4a658b32SHong Zhang   Not Collective
6318*4a658b32SHong Zhang 
6319*4a658b32SHong Zhang   Input Parameter:
6320*4a658b32SHong Zhang . ts - the time-stepper
6321*4a658b32SHong Zhang 
6322*4a658b32SHong Zhang   Output Parameters:
6323*4a658b32SHong Zhang + n - number of the time points (>=2)
6324*4a658b32SHong 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.
6325*4a658b32SHong Zhang 
6326*4a658b32SHong Zhang   Level: beginner
6327*4a658b32SHong Zhang   Notes: Both n and span_times can be NULL.
6328*4a658b32SHong Zhang 
6329*4a658b32SHong Zhang .seealso: TSSetTimeSpan(),TSGetSolutions()
6330*4a658b32SHong Zhang  @*/
6331*4a658b32SHong Zhang PetscErrorCode TSGetTimeSpan(TS ts,PetscInt *n,const PetscReal **span_times)
6332*4a658b32SHong Zhang {
6333*4a658b32SHong Zhang   PetscFunctionBegin;
6334*4a658b32SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6335*4a658b32SHong Zhang   if (n) PetscValidIntPointer(n,2);
6336*4a658b32SHong Zhang   if (span_times) PetscValidPointer(span_times,3);
6337*4a658b32SHong Zhang   if (!ts->tspan) {
6338*4a658b32SHong Zhang     if (n) *n = 0;
6339*4a658b32SHong Zhang     if (span_times) *span_times = NULL;
6340*4a658b32SHong Zhang   } else {
6341*4a658b32SHong Zhang     if (n) *n = ts->tspan->num_span_times;
6342*4a658b32SHong Zhang     if (span_times) *span_times = ts->tspan->span_times;
6343*4a658b32SHong Zhang   }
6344*4a658b32SHong Zhang   PetscFunctionReturn(0);
6345*4a658b32SHong Zhang }
6346*4a658b32SHong Zhang 
6347*4a658b32SHong Zhang /*@
6348*4a658b32SHong Zhang    TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span.
6349*4a658b32SHong Zhang 
6350*4a658b32SHong Zhang    Input Parameter:
6351*4a658b32SHong Zhang .  ts - the TS context obtained from TSCreate()
6352*4a658b32SHong Zhang 
6353*4a658b32SHong Zhang    Output Parameters:
6354*4a658b32SHong Zhang +  nsol - the number of solutions
6355*4a658b32SHong Zhang -  Sols - the solution vectors
6356*4a658b32SHong Zhang 
6357*4a658b32SHong Zhang    Level: beginner
6358*4a658b32SHong Zhang 
6359*4a658b32SHong Zhang    Notes: Both nsol and Sols can be NULL.
6360*4a658b32SHong Zhang 
6361*4a658b32SHong Zhang .seealso: TSSetTimeSpan()
6362*4a658b32SHong Zhang @*/
6363*4a658b32SHong Zhang PetscErrorCode TSGetTimeSpanSolutions(TS ts,PetscInt *nsol,Vec **Sols)
6364*4a658b32SHong Zhang {
6365*4a658b32SHong Zhang   PetscFunctionBegin;
6366*4a658b32SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6367*4a658b32SHong Zhang   if (nsol) PetscValidIntPointer(nsol,2);
6368*4a658b32SHong Zhang   if (Sols) PetscValidPointer(Sols,3);
6369*4a658b32SHong Zhang   if (!ts->tspan) {
6370*4a658b32SHong Zhang     if (nsol) *nsol = 0;
6371*4a658b32SHong Zhang     if (Sols) *Sols = NULL;
6372*4a658b32SHong Zhang   } else {
6373*4a658b32SHong Zhang     if (nsol) *nsol = ts->tspan->num_span_times;
6374*4a658b32SHong Zhang     if (Sols) *Sols = ts->tspan->vecs_sol;
6375*4a658b32SHong Zhang   }
6376*4a658b32SHong Zhang   PetscFunctionReturn(0);
6377*4a658b32SHong Zhang }
6378