xref: /petsc/src/ts/interface/ts.c (revision 67b8a455b8687dfb2f87ed52b249206f831b8117)
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   PetscErrorCode ierr;
192ffb9264SLisandro Dalcin 
202ffb9264SLisandro Dalcin   PetscFunctionBegin;
21b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
22b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
232ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
242ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
252ffb9264SLisandro Dalcin   }
262ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
272ffb9264SLisandro Dalcin }
282ffb9264SLisandro Dalcin 
29bdad233fSMatthew Knepley /*@
30bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
31bdad233fSMatthew Knepley 
32bdad233fSMatthew Knepley    Collective on TS
33bdad233fSMatthew Knepley 
34bdad233fSMatthew Knepley    Input Parameter:
35bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
36bdad233fSMatthew Knepley 
37bdad233fSMatthew Knepley    Options Database Keys:
38d2567f34SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP, TSIRK
39ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
40ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
41ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
42ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
434dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
443e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
451628793fSSatish 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
46a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
47a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
48a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
49a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
50*67b8a455SSatish Balay .  -ts_atol <atol> - Absolute tolerance for local truncation error
51f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
52f3b1f45cSBarry 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
53*67b8a455SSatish Balay .  -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
54847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
55bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
56aee7a9fbSMatthew G. Knepley .  -ts_monitor_cancel - Cancel all monitors
57de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
58de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
597cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
606934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
618b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
62de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
63de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
64de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
65de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
663e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
673e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
68fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
69e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
709e336e28SPatrick Sanan -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
7153ea634cSHong Zhang 
723d5a8a6aSBarry Smith    Notes:
733d5a8a6aSBarry Smith      See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper.
743d5a8a6aSBarry Smith 
753d5a8a6aSBarry Smith      Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order
763d5a8a6aSBarry Smith      to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and
773d5a8a6aSBarry Smith      -snes_lag_preconditioner_persists true
783d5a8a6aSBarry Smith 
799e336e28SPatrick Sanan    Developer Note:
809e336e28SPatrick Sanan      We should unify all the -ts_monitor options in the way that -xxx_view has been unified
81bdad233fSMatthew Knepley 
82bdad233fSMatthew Knepley    Level: beginner
83bdad233fSMatthew Knepley 
84a313700dSBarry Smith .seealso: TSGetType()
85bdad233fSMatthew Knepley @*/
867087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
87bdad233fSMatthew Knepley {
88bc952696SBarry Smith   PetscBool              opt,flg,tflg;
89dfbe8321SBarry Smith   PetscErrorCode         ierr;
90eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
9131748224SBarry Smith   PetscReal              time_step;
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 
1016991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
102cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
103cd11d68dSLisandro Dalcin 
104cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1051ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1061ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1076991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1086991f827SBarry Smith   if (opt) {
1096991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1106991f827SBarry Smith   } else {
1116991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1126991f827SBarry Smith   }
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley   /* Handle generic TS options */
1154dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
116ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
11719eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1180298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11931748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
120cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
12149354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
12249354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1230298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1240298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1250298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1260298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1270298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
128bdad233fSMatthew Knepley 
129f3b1f45cSBarry Smith   ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL);CHKERRQ(ierr);
130f3b1f45cSBarry Smith   ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult_transpose","Test the RHS Jacobian transpose for consistency with RHS at each solve ","None",ts->testjacobiantranspose,&ts->testjacobiantranspose,NULL);CHKERRQ(ierr);
1310fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr);
13256f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
13356f85f32SBarry Smith   {
13456f85f32SBarry Smith     PetscBool set;
13556f85f32SBarry Smith     flg  = PETSC_FALSE;
13656f85f32SBarry Smith     ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
13756f85f32SBarry Smith     if (set) {
13856f85f32SBarry Smith       ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
13956f85f32SBarry Smith     }
14056f85f32SBarry Smith   }
14156f85f32SBarry Smith #endif
14256f85f32SBarry Smith 
143bdad233fSMatthew Knepley   /* Monitor options */
144d0c080abSJoseph Pusztay   ierr = PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL);CHKERRQ(ierr);
145cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
146cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
147fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
148d0c080abSJoseph Pusztay   ierr = TSMonitorSetFromOptions(ts,"-ts_dmswarm_monitor_moments","Monitor moments of particle distribution","TSDMSwarmMonitorMoments",TSDMSwarmMonitorMoments,NULL);CHKERRQ(ierr);
149fde5950dSBarry Smith 
150589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
1515180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1525180491cSLisandro Dalcin 
1534f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
154b3603a34SBarry Smith   if (opt) {
1553923b477SBarry Smith     PetscInt       howoften = 1;
156e669de00SBarry Smith     DM             dm;
157e669de00SBarry Smith     PetscBool      net;
158b3603a34SBarry Smith 
1590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
160e669de00SBarry Smith     ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
161e669de00SBarry Smith     ierr = PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net);CHKERRQ(ierr);
162e669de00SBarry Smith     if (net) {
163e669de00SBarry Smith       TSMonitorLGCtxNetwork ctx;
164e669de00SBarry Smith       ierr = TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
165e669de00SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy);CHKERRQ(ierr);
166e669de00SBarry Smith       ierr = PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL);CHKERRQ(ierr);
167e669de00SBarry Smith     } else {
168e669de00SBarry Smith       TSMonitorLGCtx ctx;
169c793f718SLisandro Dalcin       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1704f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
171bdad233fSMatthew Knepley     }
172e669de00SBarry Smith   }
1736ba87a44SLisandro Dalcin 
1744f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
175ef20d060SBarry Smith   if (opt) {
1760b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1773923b477SBarry Smith     PetscInt       howoften = 1;
178ef20d060SBarry Smith 
1790298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
180c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1814f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
182ef20d060SBarry Smith   }
183edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
1847cf37e64SBarry Smith 
1856934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
1866934998bSLisandro Dalcin   if (opt) {
1876934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
1886934998bSLisandro Dalcin     PetscInt       howoften = 1;
1896934998bSLisandro Dalcin 
1906934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
1916ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1926934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
1936934998bSLisandro Dalcin   }
1948b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
1958b668821SLisandro Dalcin   if (opt) {
1968b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
1978b668821SLisandro Dalcin     PetscInt       howoften = 1;
1988b668821SLisandro Dalcin 
1998b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2008b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2018b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2028b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2038b668821SLisandro Dalcin   }
2048b668821SLisandro Dalcin 
205201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
206201da799SBarry Smith   if (opt) {
207201da799SBarry Smith     TSMonitorLGCtx ctx;
208201da799SBarry Smith     PetscInt       howoften = 1;
209201da799SBarry Smith 
2100298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2116ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
212201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
213201da799SBarry Smith   }
214201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
215201da799SBarry Smith   if (opt) {
216201da799SBarry Smith     TSMonitorLGCtx ctx;
217201da799SBarry Smith     PetscInt       howoften = 1;
218201da799SBarry Smith 
2190298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2206ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
221201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
222201da799SBarry Smith   }
2238189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2248189c53fSBarry Smith   if (opt) {
2258189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2268189c53fSBarry Smith     PetscInt          howoften = 1;
2278189c53fSBarry Smith 
2280298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
229c793f718SLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2308189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2318189c53fSBarry Smith   }
2320ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2331b575b74SJoseph Pusztay   if (opt) {
2341b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
235d7462660SMatthew Knepley     PetscInt        howoften = 1, retain = 0;
236d7462660SMatthew Knepley     PetscBool       phase = PETSC_TRUE;
237d7462660SMatthew Knepley 
2380ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL);CHKERRQ(ierr);
239d7462660SMatthew Knepley     ierr = PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL);CHKERRQ(ierr);
240d7462660SMatthew Knepley     ierr = PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL);CHKERRQ(ierr);
241d7462660SMatthew Knepley     ierr = TSMonitorSPCtxCreate(PetscObjectComm((PetscObject) ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx);CHKERRQ(ierr);
2420ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2431b575b74SJoseph Pusztay   }
244ef20d060SBarry Smith   opt  = PETSC_FALSE;
2450dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
246a7cc72afSBarry Smith   if (opt) {
24783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
24883a4ac43SBarry Smith     PetscInt         howoften = 1;
249a80ad3e0SBarry Smith 
2500298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
251c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
25283a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
253bdad233fSMatthew Knepley   }
254fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2552d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2562d5ee99bSBarry Smith   if (opt) {
2572d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2582d5ee99bSBarry Smith     PetscReal        bounds[4];
2592d5ee99bSBarry Smith     PetscInt         n = 4;
2602d5ee99bSBarry Smith     PetscDraw        draw;
2616934998bSLisandro Dalcin     PetscDrawAxis    axis;
2622d5ee99bSBarry Smith 
2632d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2642c71b3e2SJacob Faibussowitsch     PetscCheckFalse(n != 4,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
265c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2662d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2676934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2686934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2696934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2702d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2712d5ee99bSBarry Smith   }
2722d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2730dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2743a471f94SBarry Smith   if (opt) {
27583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
27683a4ac43SBarry Smith     PetscInt         howoften = 1;
2773a471f94SBarry Smith 
2780298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
279c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
28083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2813a471f94SBarry Smith   }
2820ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
2830ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
2840ed3bfb6SBarry Smith   if (opt) {
2850ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
2860ed3bfb6SBarry Smith     PetscInt         howoften = 1;
2870ed3bfb6SBarry Smith 
2880ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
289c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2900ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2910ed3bfb6SBarry Smith   }
292fde5950dSBarry Smith 
293ed81e22dSJed Brown   opt  = PETSC_FALSE;
294589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
295ed81e22dSJed Brown   if (flg) {
296ed81e22dSJed Brown     const char *ptr,*ptr2;
297ed81e22dSJed Brown     char       *filetemplate;
2982c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!monfilename[0],PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
299ed81e22dSJed Brown     /* Do some cursory validation of the input. */
300ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
3012c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!ptr,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
302ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
303ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
3042c71b3e2SJacob Faibussowitsch       PetscCheckFalse(!ptr2 && (*ptr < '0' || '9' < *ptr),PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
305ed81e22dSJed Brown       if (ptr2) break;
306ed81e22dSJed Brown     }
307ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
308ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
309ed81e22dSJed Brown   }
310bdad233fSMatthew Knepley 
311589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
312d1212d36SBarry Smith   if (flg) {
313d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
314d1212d36SBarry Smith     int                  ray = 0;
3153ee9839eSMatthew G. Knepley     DMDirection          ddir;
316d1212d36SBarry Smith     DM                   da;
317d1212d36SBarry Smith     PetscMPIInt          rank;
318d1212d36SBarry Smith 
3192c71b3e2SJacob Faibussowitsch     PetscCheckFalse(dir[1] != '=',PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3203ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3213ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
32298921bdaSJacob Faibussowitsch     else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
323d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
324d1212d36SBarry Smith 
3257d3de750SJacob Faibussowitsch     ierr = PetscInfo(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr);
326b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
327d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
328d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
329ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRMPI(ierr);
330dd400576SPatrick Sanan     if (rank == 0) {
331c793f718SLisandro Dalcin       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
332d1212d36SBarry Smith     }
33351b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
334d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
335d1212d36SBarry Smith   }
336589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
33751b4a12fSMatthew G. Knepley   if (flg) {
33851b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
33951b4a12fSMatthew G. Knepley     int                 ray = 0;
3403ee9839eSMatthew G. Knepley     DMDirection         ddir;
34151b4a12fSMatthew G. Knepley     DM                  da;
34251b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
343d1212d36SBarry Smith 
3442c71b3e2SJacob Faibussowitsch     PetscCheckFalse(dir[1] != '=',PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3453ee9839eSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DM_X;
3463ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
34798921bdaSJacob Faibussowitsch     else SETERRQ(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
34851b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3491c3436cfSJed Brown 
3507d3de750SJacob Faibussowitsch     ierr = PetscInfo(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr);
351b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
35251b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
35351b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
354c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
35551b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
35651b4a12fSMatthew G. Knepley   }
357a7a1495cSBarry Smith 
358b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
359b3d3934dSBarry Smith   if (opt) {
360b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
361b3d3934dSBarry Smith 
362b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
363b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
364b3d3934dSBarry Smith   }
365aee7a9fbSMatthew G. Knepley   flg  = PETSC_FALSE;
366aee7a9fbSMatthew G. Knepley   ierr = PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt);CHKERRQ(ierr);
367aee7a9fbSMatthew G. Knepley   if (opt && flg) {ierr = TSMonitorCancel(ts);CHKERRQ(ierr);}
368b3d3934dSBarry Smith 
369847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
370847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
371847ff0e1SMatthew G. Knepley   if (flg) {
372847ff0e1SMatthew G. Knepley     DM   dm;
373847ff0e1SMatthew G. Knepley     DMTS tdm;
374847ff0e1SMatthew G. Knepley 
375847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
376847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
377847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
378c793f718SLisandro Dalcin     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL);CHKERRQ(ierr);
379847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
380847ff0e1SMatthew G. Knepley   }
381847ff0e1SMatthew G. Knepley 
382d763cef2SBarry Smith   /* Handle specific TS options */
383d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3846991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
385d763cef2SBarry Smith   }
386fbc52257SHong Zhang 
387a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
388a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
389a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
390a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
391a7bdc993SLisandro Dalcin 
39268bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
3934f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
39468bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
39568bece0bSHong Zhang   if (tflg) {
39668bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
39768bece0bSHong Zhang   }
398a05bf03eSHong Zhang 
399a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
400d763cef2SBarry Smith 
401d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4020633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
403fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
404d763cef2SBarry Smith 
4054f122a70SLisandro Dalcin   if (ts->trajectory) {
4064f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
407d763cef2SBarry Smith   }
40868bece0bSHong Zhang 
4091ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4104f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4111ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4121ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4131ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4141ef27442SStefano Zampini   }
4154f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
416d763cef2SBarry Smith   PetscFunctionReturn(0);
417d763cef2SBarry Smith }
418d763cef2SBarry Smith 
419d2daff3dSHong Zhang /*@
42078fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
42178fbdcc8SBarry Smith 
42278fbdcc8SBarry Smith    Collective on TS
42378fbdcc8SBarry Smith 
42478fbdcc8SBarry Smith    Input Parameters:
42578fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
42678fbdcc8SBarry Smith 
4277a7aea1fSJed Brown    Output Parameters:
42878fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
42978fbdcc8SBarry Smith 
43078fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
43178fbdcc8SBarry Smith 
43278fbdcc8SBarry Smith    Level: advanced
43378fbdcc8SBarry Smith 
43478fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
43578fbdcc8SBarry Smith 
43678fbdcc8SBarry Smith @*/
43778fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
43878fbdcc8SBarry Smith {
43978fbdcc8SBarry Smith   PetscFunctionBegin;
44078fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44178fbdcc8SBarry Smith   *tr = ts->trajectory;
44278fbdcc8SBarry Smith   PetscFunctionReturn(0);
44378fbdcc8SBarry Smith }
44478fbdcc8SBarry Smith 
44578fbdcc8SBarry Smith /*@
446bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
447d2daff3dSHong Zhang 
448d2daff3dSHong Zhang    Collective on TS
449d2daff3dSHong Zhang 
450f899ff85SJose E. Roman    Input Parameter:
451bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
452bc952696SBarry Smith 
45378fbdcc8SBarry Smith    Options Database:
45478fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
455*67b8a455SSatish Balay -  -ts_trajectory_type type - set trajectory type
45678fbdcc8SBarry Smith 
45768bece0bSHong Zhang Note: This routine should be called after all TS options have been set
458d2daff3dSHong Zhang 
459c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
46078fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
46178fbdcc8SBarry Smith 
462d2daff3dSHong Zhang    Level: intermediate
463d2daff3dSHong Zhang 
4642d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
465d2daff3dSHong Zhang 
466d2daff3dSHong Zhang @*/
467bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
468d2daff3dSHong Zhang {
469bc952696SBarry Smith   PetscErrorCode ierr;
470bc952696SBarry Smith 
471d2daff3dSHong Zhang   PetscFunctionBegin;
472d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
473bc952696SBarry Smith   if (!ts->trajectory) {
474bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
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   PetscErrorCode ierr;
4952d29f1f2SStefano Zampini 
4962d29f1f2SStefano Zampini   PetscFunctionBegin;
4972d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4982d29f1f2SStefano Zampini   if (ts->trajectory) {
4992d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5002d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5012d29f1f2SStefano Zampini   }
5022d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5032d29f1f2SStefano Zampini }
5042d29f1f2SStefano Zampini 
5052d29f1f2SStefano Zampini /*@
50667a3cfb0SHong Zhang    TSRemoveTrajectory - Destroys and removes the internal TSTrajectory object from TS
50767a3cfb0SHong Zhang 
50867a3cfb0SHong Zhang    Collective on TS
50967a3cfb0SHong Zhang 
51067a3cfb0SHong Zhang    Input Parameters:
51167a3cfb0SHong Zhang .  ts - the TS context obtained from TSCreate()
51267a3cfb0SHong Zhang 
51367a3cfb0SHong Zhang    Level: intermediate
51467a3cfb0SHong Zhang 
51567a3cfb0SHong Zhang .seealso: TSResetTrajectory(), TSAdjointSolve()
51667a3cfb0SHong Zhang 
51767a3cfb0SHong Zhang @*/
51867a3cfb0SHong Zhang PetscErrorCode TSRemoveTrajectory(TS ts)
51967a3cfb0SHong Zhang {
52067a3cfb0SHong Zhang   PetscErrorCode ierr;
52167a3cfb0SHong Zhang 
52267a3cfb0SHong Zhang   PetscFunctionBegin;
52367a3cfb0SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52467a3cfb0SHong Zhang   if (ts->trajectory) {
52567a3cfb0SHong Zhang     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
52667a3cfb0SHong Zhang   }
52767a3cfb0SHong Zhang   PetscFunctionReturn(0);
52867a3cfb0SHong Zhang }
52967a3cfb0SHong Zhang 
53067a3cfb0SHong Zhang /*@
531a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
532a7a1495cSBarry Smith       set with TSSetRHSJacobian().
533a7a1495cSBarry Smith 
534d083f849SBarry Smith    Collective on TS
535a7a1495cSBarry Smith 
536a7a1495cSBarry Smith    Input Parameters:
537316643e7SJed Brown +  ts - the TS context
538a7a1495cSBarry Smith .  t - current timestep
5390910c330SBarry Smith -  U - input vector
540a7a1495cSBarry Smith 
541a7a1495cSBarry Smith    Output Parameters:
542a7a1495cSBarry Smith +  A - Jacobian matrix
5436b867d5aSJose E. Roman -  B - optional preconditioning matrix
544a7a1495cSBarry Smith 
545a7a1495cSBarry Smith    Notes:
546a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
547a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
548a7a1495cSBarry Smith 
549a7a1495cSBarry Smith    Level: developer
550a7a1495cSBarry Smith 
55194b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
552a7a1495cSBarry Smith @*/
553d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
554a7a1495cSBarry Smith {
555dfbe8321SBarry Smith   PetscErrorCode   ierr;
556270bf2e7SJed Brown   PetscObjectState Ustate;
5576c1e1eecSBarry Smith   PetscObjectId    Uid;
55824989b8cSPeter Brune   DM               dm;
559942e3340SBarry Smith   DMTS             tsdm;
56024989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
56124989b8cSPeter Brune   void             *ctx;
562b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
563a7a1495cSBarry Smith 
564a7a1495cSBarry Smith   PetscFunctionBegin;
5650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5660910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5670910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
56824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
569942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
5706374d434SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
5712663174eSHong Zhang   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
57259e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5736c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
574971015bcSStefano Zampini 
5752663174eSHong Zhang   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0);
576d90be118SSean Farley 
5772c71b3e2SJacob Faibussowitsch   PetscCheckFalse(ts->rhsjacobian.shift && 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);
57824989b8cSPeter Brune   if (rhsjacobianfunc) {
57994ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
580a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
581d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
582a7a1495cSBarry Smith     PetscStackPop;
583a6ab3590SBarry Smith     ts->rhsjacs++;
58494ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
585ef66eb69SBarry Smith   } else {
58694ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
587971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
588ef66eb69SBarry Smith   }
5890e4ef248SJed Brown   ts->rhsjacobian.time  = t;
590971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
591971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
5927f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
59359e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
594a7a1495cSBarry Smith   PetscFunctionReturn(0);
595a7a1495cSBarry Smith }
596a7a1495cSBarry Smith 
597316643e7SJed Brown /*@
598d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
599d763cef2SBarry Smith 
600d083f849SBarry Smith    Collective on TS
601316643e7SJed Brown 
602316643e7SJed Brown    Input Parameters:
603316643e7SJed Brown +  ts - the TS context
604316643e7SJed Brown .  t - current time
6050910c330SBarry Smith -  U - state vector
606316643e7SJed Brown 
607316643e7SJed Brown    Output Parameter:
608316643e7SJed Brown .  y - right hand side
609316643e7SJed Brown 
610316643e7SJed Brown    Note:
611316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
612316643e7SJed Brown    is used internally within the nonlinear solvers.
613316643e7SJed Brown 
614316643e7SJed Brown    Level: developer
615316643e7SJed Brown 
616316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
617316643e7SJed Brown @*/
6180910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
619d763cef2SBarry Smith {
620dfbe8321SBarry Smith   PetscErrorCode ierr;
62124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
62224989b8cSPeter Brune   TSIFunction    ifunction;
62324989b8cSPeter Brune   void           *ctx;
62424989b8cSPeter Brune   DM             dm;
62524989b8cSPeter Brune 
626d763cef2SBarry Smith   PetscFunctionBegin;
6270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6280910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6290700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
63024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
63124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6320298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
633d763cef2SBarry Smith 
6342c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!rhsfunction && !ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
635d763cef2SBarry Smith 
63624989b8cSPeter Brune   if (rhsfunction) {
637a6ab3590SBarry Smith     ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
6381be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
639d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6400910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
641d763cef2SBarry Smith     PetscStackPop;
6421be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
643a6ab3590SBarry Smith     ts->rhsfuncs++;
644a6ab3590SBarry Smith     ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
645214bc6a2SJed Brown   } else {
646214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
647b2cd27e8SJed Brown   }
648d763cef2SBarry Smith   PetscFunctionReturn(0);
649d763cef2SBarry Smith }
650d763cef2SBarry Smith 
651ef20d060SBarry Smith /*@
652ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
653ef20d060SBarry Smith 
654d083f849SBarry Smith    Collective on TS
655ef20d060SBarry Smith 
656ef20d060SBarry Smith    Input Parameters:
657ef20d060SBarry Smith +  ts - the TS context
658ef20d060SBarry Smith -  t - current time
659ef20d060SBarry Smith 
660ef20d060SBarry Smith    Output Parameter:
6610910c330SBarry Smith .  U - the solution
662ef20d060SBarry Smith 
663ef20d060SBarry Smith    Note:
664ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
665ef20d060SBarry Smith    is used internally within the nonlinear solvers.
666ef20d060SBarry Smith 
667ef20d060SBarry Smith    Level: developer
668ef20d060SBarry Smith 
669abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
670ef20d060SBarry Smith @*/
6710910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
672ef20d060SBarry Smith {
673ef20d060SBarry Smith   PetscErrorCode     ierr;
674ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
675ef20d060SBarry Smith   void               *ctx;
676ef20d060SBarry Smith   DM                 dm;
677ef20d060SBarry Smith 
678ef20d060SBarry Smith   PetscFunctionBegin;
679ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6800910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
681ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
682ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
683ef20d060SBarry Smith 
684ef20d060SBarry Smith   if (solutionfunction) {
6859b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6860910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
687ef20d060SBarry Smith     PetscStackPop;
688ef20d060SBarry Smith   }
689ef20d060SBarry Smith   PetscFunctionReturn(0);
690ef20d060SBarry Smith }
6919b7cd975SBarry Smith /*@
6929b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6939b7cd975SBarry Smith 
694d083f849SBarry Smith    Collective on TS
6959b7cd975SBarry Smith 
6969b7cd975SBarry Smith    Input Parameters:
6979b7cd975SBarry Smith +  ts - the TS context
6989b7cd975SBarry Smith -  t - current time
6999b7cd975SBarry Smith 
7009b7cd975SBarry Smith    Output Parameter:
7019b7cd975SBarry Smith .  U - the function value
7029b7cd975SBarry Smith 
7039b7cd975SBarry Smith    Note:
7049b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7059b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7069b7cd975SBarry Smith 
7079b7cd975SBarry Smith    Level: developer
7089b7cd975SBarry Smith 
7099b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7109b7cd975SBarry Smith @*/
7119b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7129b7cd975SBarry Smith {
7139b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7149b7cd975SBarry Smith   void               *ctx;
7159b7cd975SBarry Smith   DM                 dm;
7169b7cd975SBarry Smith 
7179b7cd975SBarry Smith   PetscFunctionBegin;
7189b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7199b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7209b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7219b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7229b7cd975SBarry Smith 
7239b7cd975SBarry Smith   if (forcing) {
7249b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7259b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7269b7cd975SBarry Smith     PetscStackPop;
7279b7cd975SBarry Smith   }
7289b7cd975SBarry Smith   PetscFunctionReturn(0);
7299b7cd975SBarry Smith }
730ef20d060SBarry Smith 
731214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
732214bc6a2SJed Brown {
7332dd45cf8SJed Brown   Vec            F;
734214bc6a2SJed Brown   PetscErrorCode ierr;
735214bc6a2SJed Brown 
736214bc6a2SJed Brown   PetscFunctionBegin;
7370298fd71SBarry Smith   *Frhs = NULL;
7380298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
739214bc6a2SJed Brown   if (!ts->Frhs) {
7402dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
741214bc6a2SJed Brown   }
742214bc6a2SJed Brown   *Frhs = ts->Frhs;
743214bc6a2SJed Brown   PetscFunctionReturn(0);
744214bc6a2SJed Brown }
745214bc6a2SJed Brown 
746971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
747214bc6a2SJed Brown {
748214bc6a2SJed Brown   Mat            A,B;
7492dd45cf8SJed Brown   PetscErrorCode ierr;
75041a1d4d2SBarry Smith   TSIJacobian    ijacobian;
751214bc6a2SJed Brown 
752214bc6a2SJed Brown   PetscFunctionBegin;
753c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
754c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
75541a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
756214bc6a2SJed Brown   if (Arhs) {
757214bc6a2SJed Brown     if (!ts->Arhs) {
75841a1d4d2SBarry Smith       if (ijacobian) {
759214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
760d60b7d5cSBarry Smith         ierr = TSSetMatStructure(ts,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
76141a1d4d2SBarry Smith       } else {
76241a1d4d2SBarry Smith         ts->Arhs = A;
76341a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
76441a1d4d2SBarry Smith       }
7653565c898SBarry Smith     } else {
7663565c898SBarry Smith       PetscBool flg;
7673565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7683565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7693565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs) {
7703565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7713565c898SBarry Smith         ts->Arhs = A;
7723565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7733565c898SBarry Smith       }
774214bc6a2SJed Brown     }
775214bc6a2SJed Brown     *Arhs = ts->Arhs;
776214bc6a2SJed Brown   }
777214bc6a2SJed Brown   if (Brhs) {
778214bc6a2SJed Brown     if (!ts->Brhs) {
779bdb70873SJed Brown       if (A != B) {
78041a1d4d2SBarry Smith         if (ijacobian) {
781214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
782bdb70873SJed Brown         } else {
78341a1d4d2SBarry Smith           ts->Brhs = B;
78441a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
78541a1d4d2SBarry Smith         }
78641a1d4d2SBarry Smith       } else {
787bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
78851699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
789bdb70873SJed Brown       }
790214bc6a2SJed Brown     }
791214bc6a2SJed Brown     *Brhs = ts->Brhs;
792214bc6a2SJed Brown   }
793214bc6a2SJed Brown   PetscFunctionReturn(0);
794214bc6a2SJed Brown }
795214bc6a2SJed Brown 
796316643e7SJed Brown /*@
7970910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
798316643e7SJed Brown 
799d083f849SBarry Smith    Collective on TS
800316643e7SJed Brown 
801316643e7SJed Brown    Input Parameters:
802316643e7SJed Brown +  ts - the TS context
803316643e7SJed Brown .  t - current time
8040910c330SBarry Smith .  U - state vector
8050910c330SBarry Smith .  Udot - time derivative of state vector
806214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
807316643e7SJed Brown 
808316643e7SJed Brown    Output Parameter:
809316643e7SJed Brown .  Y - right hand side
810316643e7SJed Brown 
811316643e7SJed Brown    Note:
812316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
813316643e7SJed Brown    is used internally within the nonlinear solvers.
814316643e7SJed Brown 
815316643e7SJed Brown    If the user did did not write their equations in implicit form, this
816316643e7SJed Brown    function recasts them in implicit form.
817316643e7SJed Brown 
818316643e7SJed Brown    Level: developer
819316643e7SJed Brown 
820316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
821316643e7SJed Brown @*/
8220910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
823316643e7SJed Brown {
824316643e7SJed Brown   PetscErrorCode ierr;
82524989b8cSPeter Brune   TSIFunction    ifunction;
82624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
82724989b8cSPeter Brune   void           *ctx;
82824989b8cSPeter Brune   DM             dm;
829316643e7SJed Brown 
830316643e7SJed Brown   PetscFunctionBegin;
8310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8320910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8330910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8340700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
835316643e7SJed Brown 
83624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
83724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8380298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
83924989b8cSPeter Brune 
8402c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!rhsfunction && !ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
841d90be118SSean Farley 
8420910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
84324989b8cSPeter Brune   if (ifunction) {
844316643e7SJed Brown     PetscStackPush("TS user implicit function");
8450910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
846316643e7SJed Brown     PetscStackPop;
847a6ab3590SBarry Smith     ts->ifuncs++;
848214bc6a2SJed Brown   }
849214bc6a2SJed Brown   if (imex) {
85024989b8cSPeter Brune     if (!ifunction) {
8510910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8522dd45cf8SJed Brown     }
85324989b8cSPeter Brune   } else if (rhsfunction) {
85424989b8cSPeter Brune     if (ifunction) {
855214bc6a2SJed Brown       Vec Frhs;
856214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8570910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
858214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8592dd45cf8SJed Brown     } else {
8600910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8610910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
862316643e7SJed Brown     }
8634a6899ffSJed Brown   }
8640910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
865316643e7SJed Brown   PetscFunctionReturn(0);
866316643e7SJed Brown }
867316643e7SJed Brown 
868cfa8a9a2SHong Zhang /*
869cfa8a9a2SHong Zhang    TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it.
870cfa8a9a2SHong Zhang 
871cfa8a9a2SHong Zhang    Note:
872cfa8a9a2SHong Zhang    This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian().
873cfa8a9a2SHong Zhang 
874cfa8a9a2SHong Zhang */
875cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B)
876cfa8a9a2SHong Zhang {
877cfa8a9a2SHong Zhang   PetscErrorCode   ierr;
878cfa8a9a2SHong Zhang 
879cfa8a9a2SHong Zhang   PetscFunctionBegin;
880cfa8a9a2SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8812c71b3e2SJacob Faibussowitsch   PetscCheckFalse(A != ts->Arhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat");
8822c71b3e2SJacob Faibussowitsch   PetscCheckFalse(B != ts->Brhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat");
883cfa8a9a2SHong Zhang 
884cfa8a9a2SHong Zhang   if (ts->rhsjacobian.shift) {
885cfa8a9a2SHong Zhang     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
886cfa8a9a2SHong Zhang   }
887cfa8a9a2SHong Zhang   if (ts->rhsjacobian.scale == -1.) {
888cfa8a9a2SHong Zhang     ierr = MatScale(A,-1);CHKERRQ(ierr);
889cfa8a9a2SHong Zhang   }
890cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
891cfa8a9a2SHong Zhang     if (ts->rhsjacobian.shift) {
892cfa8a9a2SHong Zhang       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
893cfa8a9a2SHong Zhang     }
894cfa8a9a2SHong Zhang     if (ts->rhsjacobian.scale == -1.) {
895cfa8a9a2SHong Zhang       ierr = MatScale(B,-1);CHKERRQ(ierr);
896cfa8a9a2SHong Zhang     }
897cfa8a9a2SHong Zhang   }
898cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
899cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
900cfa8a9a2SHong Zhang   PetscFunctionReturn(0);
901cfa8a9a2SHong Zhang }
902cfa8a9a2SHong Zhang 
903316643e7SJed Brown /*@
904316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
905316643e7SJed Brown 
906d083f849SBarry Smith    Collective on TS
907316643e7SJed Brown 
908316643e7SJed Brown    Input
909316643e7SJed Brown       Input Parameters:
910316643e7SJed Brown +  ts - the TS context
911316643e7SJed Brown .  t - current timestep
9120910c330SBarry Smith .  U - state vector
9130910c330SBarry Smith .  Udot - time derivative of state vector
914214bc6a2SJed Brown .  shift - shift to apply, see note below
915214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
916316643e7SJed Brown 
917316643e7SJed Brown    Output Parameters:
918316643e7SJed Brown +  A - Jacobian matrix
9193565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
920316643e7SJed Brown 
921316643e7SJed Brown    Notes:
9220910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
923316643e7SJed Brown 
9240910c330SBarry Smith    dF/dU + shift*dF/dUdot
925316643e7SJed Brown 
926316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
927316643e7SJed Brown    is used internally within the nonlinear solvers.
928316643e7SJed Brown 
929316643e7SJed Brown    Level: developer
930316643e7SJed Brown 
931316643e7SJed Brown .seealso:  TSSetIJacobian()
93263495f91SJed Brown @*/
933d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
934316643e7SJed Brown {
935316643e7SJed Brown   PetscErrorCode ierr;
93624989b8cSPeter Brune   TSIJacobian    ijacobian;
93724989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
93824989b8cSPeter Brune   DM             dm;
93924989b8cSPeter Brune   void           *ctx;
940316643e7SJed Brown 
941316643e7SJed Brown   PetscFunctionBegin;
9420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9430910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9440910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
945316643e7SJed Brown   PetscValidPointer(A,6);
94694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
947316643e7SJed Brown   PetscValidPointer(B,7);
94894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
94924989b8cSPeter Brune 
95024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
95124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9520298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
95324989b8cSPeter Brune 
9542c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!rhsjacobian && !ijacobian,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
955316643e7SJed Brown 
95694ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
95724989b8cSPeter Brune   if (ijacobian) {
958316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
959d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
960a6ab3590SBarry Smith     ts->ijacs++;
961316643e7SJed Brown     PetscStackPop;
9624a6899ffSJed Brown   }
963214bc6a2SJed Brown   if (imex) {
964b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9654c26be97Sstefano_zampini       PetscBool assembled;
966971015bcSStefano Zampini       if (rhsjacobian) {
967971015bcSStefano Zampini         Mat Arhs = NULL;
968971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
969971015bcSStefano Zampini         if (A == Arhs) {
9702c71b3e2SJacob Faibussowitsch           PetscCheckFalse(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 */
971971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
972971015bcSStefano Zampini         }
973971015bcSStefano Zampini       }
97494ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9754c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9764c26be97Sstefano_zampini       if (!assembled) {
9774c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9784c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9794c26be97Sstefano_zampini       }
98094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
98194ab13aaSBarry Smith       if (A != B) {
98294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9834c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9844c26be97Sstefano_zampini         if (!assembled) {
9854c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9864c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9874c26be97Sstefano_zampini         }
98894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
989214bc6a2SJed Brown       }
990214bc6a2SJed Brown     }
991214bc6a2SJed Brown   } else {
992e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
993e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
994214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
995e1244c69SJed Brown     }
996e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
997e8b1e424SHong Zhang       PetscObjectState Ustate;
998e8b1e424SHong Zhang       PetscObjectId    Uid;
999e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
1000e8b1e424SHong Zhang 
1001e8b1e424SHong Zhang       ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1002e8b1e424SHong Zhang       ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
1003e8b1e424SHong Zhang       ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
1004097a96a2SHong 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 */
1005e8b1e424SHong Zhang         ierr = MatShift(A,shift-ts->rhsjacobian.shift);CHKERRQ(ierr); /* revert the old shift and add the new shift with a single call to MatShift */
100634d322a1SHong Zhang         if (A != B) {
100734d322a1SHong Zhang           ierr = MatShift(B,shift-ts->rhsjacobian.shift);CHKERRQ(ierr);
100834d322a1SHong Zhang         }
1009e8b1e424SHong Zhang       } else {
10103565c898SBarry Smith         PetscBool flg;
1011e8b1e424SHong Zhang 
1012e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1013e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1014e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1015e8b1e424SHong Zhang              and the matrices have not been assembled yet */
1016cfa8a9a2SHong Zhang           ierr = TSRecoverRHSJacobian(ts,A,B);CHKERRQ(ierr);
1017e8b1e424SHong Zhang         }
1018e8b1e424SHong Zhang         ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr);
10193565c898SBarry Smith         ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10203565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10213565c898SBarry Smith         if (!flg) {
102294ab13aaSBarry Smith           ierr = MatScale(A,-1);CHKERRQ(ierr);
102394ab13aaSBarry Smith           ierr = MatShift(A,shift);CHKERRQ(ierr);
10243565c898SBarry Smith         }
102594ab13aaSBarry Smith         if (A != B) {
102694ab13aaSBarry Smith           ierr = MatScale(B,-1);CHKERRQ(ierr);
102794ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1028316643e7SJed Brown         }
1029e8b1e424SHong Zhang       }
1030e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1031e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1032d60b7d5cSBarry Smith     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1033e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
103494ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
103594ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
103694ab13aaSBarry Smith         if (A != B) {
103794ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
103894ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1039214bc6a2SJed Brown         }
1040316643e7SJed Brown       }
1041e8b1e424SHong Zhang       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1042d60b7d5cSBarry Smith       ierr = MatAXPY(A,-1,Arhs,ts->axpy_pattern);CHKERRQ(ierr);
104394ab13aaSBarry Smith       if (A != B) {
1044d60b7d5cSBarry Smith         ierr = MatAXPY(B,-1,Brhs,ts->axpy_pattern);CHKERRQ(ierr);
1045316643e7SJed Brown       }
1046316643e7SJed Brown     }
1047316643e7SJed Brown   }
104894ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1049316643e7SJed Brown   PetscFunctionReturn(0);
1050316643e7SJed Brown }
1051316643e7SJed Brown 
1052d763cef2SBarry Smith /*@C
1053d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1054b5abc632SBarry Smith     where U_t = G(t,u).
1055d763cef2SBarry Smith 
10563f9fe445SBarry Smith     Logically Collective on TS
1057d763cef2SBarry Smith 
1058d763cef2SBarry Smith     Input Parameters:
1059d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10600298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1061d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1062d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10630298fd71SBarry Smith           function evaluation routine (may be NULL)
1064d763cef2SBarry Smith 
1065a96d6ef6SBarry Smith     Calling sequence of f:
1066a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1067d763cef2SBarry Smith 
1068a96d6ef6SBarry Smith +   ts - timestep context
1069a96d6ef6SBarry Smith .   t - current timestep
1070d763cef2SBarry Smith .   u - input vector
1071d763cef2SBarry Smith .   F - function vector
1072d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1073d763cef2SBarry Smith 
1074d763cef2SBarry Smith     Level: beginner
1075d763cef2SBarry Smith 
107695452b02SPatrick Sanan     Notes:
107795452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10782bbac0d3SBarry Smith 
1079ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1080d763cef2SBarry Smith @*/
1081089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1082d763cef2SBarry Smith {
1083089b2837SJed Brown   PetscErrorCode ierr;
1084089b2837SJed Brown   SNES           snes;
10850298fd71SBarry Smith   Vec            ralloc = NULL;
108624989b8cSPeter Brune   DM             dm;
1087d763cef2SBarry Smith 
1088089b2837SJed Brown   PetscFunctionBegin;
10890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1090ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
109124989b8cSPeter Brune 
109224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
109324989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1094089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1095e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1096e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1097e856ceecSJed Brown     r = ralloc;
1098e856ceecSJed Brown   }
1099089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1100e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1101d763cef2SBarry Smith   PetscFunctionReturn(0);
1102d763cef2SBarry Smith }
1103d763cef2SBarry Smith 
1104ef20d060SBarry Smith /*@C
1105abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1106ef20d060SBarry Smith 
1107ef20d060SBarry Smith     Logically Collective on TS
1108ef20d060SBarry Smith 
1109ef20d060SBarry Smith     Input Parameters:
1110ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1111ef20d060SBarry Smith .   f - routine for evaluating the solution
1112ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11130298fd71SBarry Smith           function evaluation routine (may be NULL)
1114ef20d060SBarry Smith 
1115a96d6ef6SBarry Smith     Calling sequence of f:
1116a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1117ef20d060SBarry Smith 
1118ef20d060SBarry Smith +   t - current timestep
1119ef20d060SBarry Smith .   u - output vector
1120ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1121ef20d060SBarry Smith 
11220ed3bfb6SBarry Smith     Options Database:
11230ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11240ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11250ed3bfb6SBarry Smith 
1126abd5a294SJed Brown     Notes:
1127abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1128abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1129abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1130abd5a294SJed Brown 
11314f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1132abd5a294SJed Brown 
1133ef20d060SBarry Smith     Level: beginner
1134ef20d060SBarry Smith 
11350ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1136ef20d060SBarry Smith @*/
1137ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1138ef20d060SBarry Smith {
1139ef20d060SBarry Smith   PetscErrorCode ierr;
1140ef20d060SBarry Smith   DM             dm;
1141ef20d060SBarry Smith 
1142ef20d060SBarry Smith   PetscFunctionBegin;
1143ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1144ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1145ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1146ef20d060SBarry Smith   PetscFunctionReturn(0);
1147ef20d060SBarry Smith }
1148ef20d060SBarry Smith 
11499b7cd975SBarry Smith /*@C
11509b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11519b7cd975SBarry Smith 
11529b7cd975SBarry Smith     Logically Collective on TS
11539b7cd975SBarry Smith 
11549b7cd975SBarry Smith     Input Parameters:
11559b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1156e162b725SBarry Smith .   func - routine for evaluating the forcing function
11579b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11580298fd71SBarry Smith           function evaluation routine (may be NULL)
11599b7cd975SBarry Smith 
11609b7cd975SBarry Smith     Calling sequence of func:
11616bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11629b7cd975SBarry Smith 
11639b7cd975SBarry Smith +   t - current timestep
1164e162b725SBarry Smith .   f - output vector
11659b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11669b7cd975SBarry Smith 
11679b7cd975SBarry Smith     Notes:
11689b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1169e162b725SBarry 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
1170e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1171e162b725SBarry Smith 
1172e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1173e162b725SBarry Smith 
1174e162b725SBarry 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
1175e162b725SBarry Smith     parameters can be passed in the ctx variable.
11769b7cd975SBarry Smith 
11779b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11789b7cd975SBarry Smith 
11799b7cd975SBarry Smith     Level: beginner
11809b7cd975SBarry Smith 
11819b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11829b7cd975SBarry Smith @*/
1183e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11849b7cd975SBarry Smith {
11859b7cd975SBarry Smith   PetscErrorCode ierr;
11869b7cd975SBarry Smith   DM             dm;
11879b7cd975SBarry Smith 
11889b7cd975SBarry Smith   PetscFunctionBegin;
11899b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11909b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1191e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11929b7cd975SBarry Smith   PetscFunctionReturn(0);
11939b7cd975SBarry Smith }
11949b7cd975SBarry Smith 
1195d763cef2SBarry Smith /*@C
1196f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1197b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1198d763cef2SBarry Smith 
11993f9fe445SBarry Smith    Logically Collective on TS
1200d763cef2SBarry Smith 
1201d763cef2SBarry Smith    Input Parameters:
1202d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1203e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1204e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1205d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1206d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12070298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1208d763cef2SBarry Smith 
1209f7ab8db6SBarry Smith    Calling sequence of f:
1210a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1211d763cef2SBarry Smith 
1212d763cef2SBarry Smith +  t - current timestep
1213d763cef2SBarry Smith .  u - input vector
1214e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1215e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1216d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1217d763cef2SBarry Smith 
12186cd88445SBarry Smith    Notes:
12196cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12206cd88445SBarry Smith 
12216cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1222ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1223d763cef2SBarry Smith 
1224d763cef2SBarry Smith    Level: beginner
1225d763cef2SBarry Smith 
1226ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1227d763cef2SBarry Smith 
1228d763cef2SBarry Smith @*/
1229e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1230d763cef2SBarry Smith {
1231277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1232089b2837SJed Brown   SNES           snes;
123324989b8cSPeter Brune   DM             dm;
123424989b8cSPeter Brune   TSIJacobian    ijacobian;
1235277b19d0SLisandro Dalcin 
1236d763cef2SBarry Smith   PetscFunctionBegin;
12370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1238e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1239e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1240e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1241e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1242d763cef2SBarry Smith 
124324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124424989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
12450298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1246089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12475f659677SPeter Brune   if (!ijacobian) {
1248e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12490e4ef248SJed Brown   }
1250e5d3d808SBarry Smith   if (Amat) {
1251e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12520e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1253e5d3d808SBarry Smith     ts->Arhs = Amat;
12540e4ef248SJed Brown   }
1255e5d3d808SBarry Smith   if (Pmat) {
1256e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12570e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1258e5d3d808SBarry Smith     ts->Brhs = Pmat;
12590e4ef248SJed Brown   }
1260d763cef2SBarry Smith   PetscFunctionReturn(0);
1261d763cef2SBarry Smith }
1262d763cef2SBarry Smith 
1263316643e7SJed Brown /*@C
1264b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1265316643e7SJed Brown 
12663f9fe445SBarry Smith    Logically Collective on TS
1267316643e7SJed Brown 
1268316643e7SJed Brown    Input Parameters:
1269316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12700298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1271316643e7SJed Brown .  f   - the function evaluation routine
12720298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1273316643e7SJed Brown 
1274316643e7SJed Brown    Calling sequence of f:
12756bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1276316643e7SJed Brown 
1277316643e7SJed Brown +  t   - time at step/stage being solved
1278316643e7SJed Brown .  u   - state vector
1279316643e7SJed Brown .  u_t - time derivative of state vector
1280316643e7SJed Brown .  F   - function vector
1281316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1282316643e7SJed Brown 
1283316643e7SJed Brown    Important:
12842bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1285316643e7SJed Brown 
1286316643e7SJed Brown    Level: beginner
1287316643e7SJed Brown 
1288d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1289316643e7SJed Brown @*/
129051699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1291316643e7SJed Brown {
1292089b2837SJed Brown   PetscErrorCode ierr;
1293089b2837SJed Brown   SNES           snes;
129451699248SLisandro Dalcin   Vec            ralloc = NULL;
129524989b8cSPeter Brune   DM             dm;
1296316643e7SJed Brown 
1297316643e7SJed Brown   PetscFunctionBegin;
12980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
129951699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
130024989b8cSPeter Brune 
130124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130224989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
130324989b8cSPeter Brune 
1304089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
130551699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
130651699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
130751699248SLisandro Dalcin     r  = ralloc;
1308e856ceecSJed Brown   }
130951699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
131051699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1311089b2837SJed Brown   PetscFunctionReturn(0);
1312089b2837SJed Brown }
1313089b2837SJed Brown 
1314089b2837SJed Brown /*@C
1315a5b23f4aSJose E. Roman    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it.
1316089b2837SJed Brown 
1317089b2837SJed Brown    Not Collective
1318089b2837SJed Brown 
1319089b2837SJed Brown    Input Parameter:
1320089b2837SJed Brown .  ts - the TS context
1321089b2837SJed Brown 
1322d8d19677SJose E. Roman    Output Parameters:
13230298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13240298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13250298fd71SBarry Smith -  ctx - the function context (or NULL)
1326089b2837SJed Brown 
1327089b2837SJed Brown    Level: advanced
1328089b2837SJed Brown 
1329089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1330089b2837SJed Brown @*/
1331089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1332089b2837SJed Brown {
1333089b2837SJed Brown   PetscErrorCode ierr;
1334089b2837SJed Brown   SNES           snes;
133524989b8cSPeter Brune   DM             dm;
1336089b2837SJed Brown 
1337089b2837SJed Brown   PetscFunctionBegin;
1338089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1339089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13400298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
134124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
134224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1343089b2837SJed Brown   PetscFunctionReturn(0);
1344089b2837SJed Brown }
1345089b2837SJed Brown 
1346089b2837SJed Brown /*@C
1347089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1348089b2837SJed Brown 
1349089b2837SJed Brown    Not Collective
1350089b2837SJed Brown 
1351089b2837SJed Brown    Input Parameter:
1352089b2837SJed Brown .  ts - the TS context
1353089b2837SJed Brown 
1354d8d19677SJose E. Roman    Output Parameters:
13550298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13560298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13570298fd71SBarry Smith -  ctx - the function context (or NULL)
1358089b2837SJed Brown 
1359089b2837SJed Brown    Level: advanced
1360089b2837SJed Brown 
13612bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1362089b2837SJed Brown @*/
1363089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1364089b2837SJed Brown {
1365089b2837SJed Brown   PetscErrorCode ierr;
1366089b2837SJed Brown   SNES           snes;
136724989b8cSPeter Brune   DM             dm;
1368089b2837SJed Brown 
1369089b2837SJed Brown   PetscFunctionBegin;
1370089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1371089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13720298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
137324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1375316643e7SJed Brown   PetscFunctionReturn(0);
1376316643e7SJed Brown }
1377316643e7SJed Brown 
1378316643e7SJed Brown /*@C
1379a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1380ae8867d6SBarry Smith         provided with TSSetIFunction().
1381316643e7SJed Brown 
13823f9fe445SBarry Smith    Logically Collective on TS
1383316643e7SJed Brown 
1384316643e7SJed Brown    Input Parameters:
1385316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1386e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1387e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1388316643e7SJed Brown .  f   - the Jacobian evaluation routine
13890298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1390316643e7SJed Brown 
1391316643e7SJed Brown    Calling sequence of f:
13926bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1393316643e7SJed Brown 
1394316643e7SJed Brown +  t    - time at step/stage being solved
13951b4a444bSJed Brown .  U    - state vector
13961b4a444bSJed Brown .  U_t  - time derivative of state vector
1397316643e7SJed Brown .  a    - shift
1398e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1399e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1400316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1401316643e7SJed Brown 
1402316643e7SJed Brown    Notes:
1403e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1404316643e7SJed Brown 
1405895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1406895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1407895c21f2SBarry Smith 
1408a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1409b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1410a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1411a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1412a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1413a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1414a4f0a591SBarry Smith 
14156cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14166cd88445SBarry Smith 
14176cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1418ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1419ca5f011dSBarry Smith 
1420316643e7SJed Brown    Level: beginner
1421316643e7SJed Brown 
1422ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1423316643e7SJed Brown 
1424316643e7SJed Brown @*/
1425e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1426316643e7SJed Brown {
1427316643e7SJed Brown   PetscErrorCode ierr;
1428089b2837SJed Brown   SNES           snes;
142924989b8cSPeter Brune   DM             dm;
1430316643e7SJed Brown 
1431316643e7SJed Brown   PetscFunctionBegin;
14320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1433e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1434e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1435e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1436e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
143724989b8cSPeter Brune 
143824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
143924989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
144024989b8cSPeter Brune 
1441089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1442e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1443316643e7SJed Brown   PetscFunctionReturn(0);
1444316643e7SJed Brown }
1445316643e7SJed Brown 
1446e1244c69SJed Brown /*@
1447e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1448e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1449e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1450e1244c69SJed Brown    not been changed by the TS.
1451e1244c69SJed Brown 
1452e1244c69SJed Brown    Logically Collective
1453e1244c69SJed Brown 
14544165533cSJose E. Roman    Input Parameters:
1455e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1456e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1457e1244c69SJed Brown 
1458e1244c69SJed Brown    Level: intermediate
1459e1244c69SJed Brown 
1460e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1461e1244c69SJed Brown @*/
1462e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1463e1244c69SJed Brown {
1464e1244c69SJed Brown   PetscFunctionBegin;
1465e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1466e1244c69SJed Brown   PetscFunctionReturn(0);
1467e1244c69SJed Brown }
1468e1244c69SJed Brown 
1469efe9872eSLisandro Dalcin /*@C
1470efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1471efe9872eSLisandro Dalcin 
1472efe9872eSLisandro Dalcin    Logically Collective on TS
1473efe9872eSLisandro Dalcin 
1474efe9872eSLisandro Dalcin    Input Parameters:
1475efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1476efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1477efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1478efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1479efe9872eSLisandro Dalcin 
1480efe9872eSLisandro Dalcin    Calling sequence of fun:
14816bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1482efe9872eSLisandro Dalcin 
1483efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1484efe9872eSLisandro Dalcin .  U    - state vector
1485efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1486efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1487efe9872eSLisandro Dalcin .  F    - function vector
1488efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin    Level: beginner
1491efe9872eSLisandro Dalcin 
1492a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1493efe9872eSLisandro Dalcin @*/
1494efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1495efe9872eSLisandro Dalcin {
1496efe9872eSLisandro Dalcin   DM             dm;
1497efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1498efe9872eSLisandro Dalcin 
1499efe9872eSLisandro Dalcin   PetscFunctionBegin;
1500efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1501efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1502efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1503efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1504efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1505efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1506efe9872eSLisandro Dalcin }
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin /*@C
1509a5b23f4aSJose E. Roman   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it.
1510efe9872eSLisandro Dalcin 
1511efe9872eSLisandro Dalcin   Not Collective
1512efe9872eSLisandro Dalcin 
1513efe9872eSLisandro Dalcin   Input Parameter:
1514efe9872eSLisandro Dalcin . ts - the TS context
1515efe9872eSLisandro Dalcin 
1516d8d19677SJose E. Roman   Output Parameters:
1517efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1518efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1519efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin   Level: advanced
1522efe9872eSLisandro Dalcin 
1523a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1524efe9872eSLisandro Dalcin @*/
1525efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1526efe9872eSLisandro Dalcin {
1527efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1528efe9872eSLisandro Dalcin   SNES           snes;
1529efe9872eSLisandro Dalcin   DM             dm;
1530efe9872eSLisandro Dalcin 
1531efe9872eSLisandro Dalcin   PetscFunctionBegin;
1532efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1533efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1534efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1535efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1536efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1537efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1538efe9872eSLisandro Dalcin }
1539efe9872eSLisandro Dalcin 
1540efe9872eSLisandro Dalcin /*@C
1541bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1542efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1543efe9872eSLisandro Dalcin 
1544efe9872eSLisandro Dalcin    Logically Collective on TS
1545efe9872eSLisandro Dalcin 
1546efe9872eSLisandro Dalcin    Input Parameters:
1547efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1548efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1549efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1550efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1551efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1552efe9872eSLisandro Dalcin 
1553efe9872eSLisandro Dalcin    Calling sequence of jac:
15546bc98fa9SBarry 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);
1555efe9872eSLisandro Dalcin 
1556efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1557efe9872eSLisandro Dalcin .  U    - state vector
1558efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1559efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1560efe9872eSLisandro Dalcin .  v    - shift for U_t
1561efe9872eSLisandro Dalcin .  a    - shift for U_tt
1562efe9872eSLisandro 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
1563efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1564efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1565efe9872eSLisandro Dalcin 
1566efe9872eSLisandro Dalcin    Notes:
1567efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1568efe9872eSLisandro Dalcin 
1569efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1570efe9872eSLisandro 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.
1571efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1572bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1573efe9872eSLisandro Dalcin 
1574efe9872eSLisandro Dalcin    Level: beginner
1575efe9872eSLisandro Dalcin 
1576a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1577efe9872eSLisandro Dalcin @*/
1578efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1579efe9872eSLisandro Dalcin {
1580efe9872eSLisandro Dalcin   DM             dm;
1581efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1582efe9872eSLisandro Dalcin 
1583efe9872eSLisandro Dalcin   PetscFunctionBegin;
1584efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1585efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1586efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1587efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1588efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1589efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1590efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1591efe9872eSLisandro Dalcin }
1592efe9872eSLisandro Dalcin 
1593efe9872eSLisandro Dalcin /*@C
1594efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin   Input Parameter:
1599efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1600efe9872eSLisandro Dalcin 
1601efe9872eSLisandro Dalcin   Output Parameters:
1602efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1603efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1604efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1605efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1606efe9872eSLisandro Dalcin 
160795452b02SPatrick Sanan   Notes:
160895452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1609efe9872eSLisandro Dalcin 
1610efe9872eSLisandro Dalcin   Level: advanced
1611efe9872eSLisandro Dalcin 
1612a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1613efe9872eSLisandro Dalcin 
1614efe9872eSLisandro Dalcin @*/
1615efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1616efe9872eSLisandro Dalcin {
1617efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1618efe9872eSLisandro Dalcin   SNES           snes;
1619efe9872eSLisandro Dalcin   DM             dm;
1620efe9872eSLisandro Dalcin 
1621efe9872eSLisandro Dalcin   PetscFunctionBegin;
1622efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1623efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1626efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1627efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1628efe9872eSLisandro Dalcin }
1629efe9872eSLisandro Dalcin 
1630efe9872eSLisandro Dalcin /*@
1631efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1632efe9872eSLisandro Dalcin 
1633d083f849SBarry Smith   Collective on TS
1634efe9872eSLisandro Dalcin 
1635efe9872eSLisandro Dalcin   Input Parameters:
1636efe9872eSLisandro Dalcin + ts - the TS context
1637efe9872eSLisandro Dalcin . t - current time
1638efe9872eSLisandro Dalcin . U - state vector
1639efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1640efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1641efe9872eSLisandro Dalcin 
1642efe9872eSLisandro Dalcin   Output Parameter:
1643efe9872eSLisandro Dalcin . F - the residual vector
1644efe9872eSLisandro Dalcin 
1645efe9872eSLisandro Dalcin   Note:
1646efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1647efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin   Level: developer
1650efe9872eSLisandro Dalcin 
1651a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1652efe9872eSLisandro Dalcin @*/
1653efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1654efe9872eSLisandro Dalcin {
1655efe9872eSLisandro Dalcin   DM             dm;
1656efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1657efe9872eSLisandro Dalcin   void           *ctx;
1658efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1659efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1660efe9872eSLisandro Dalcin 
1661efe9872eSLisandro Dalcin   PetscFunctionBegin;
1662efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1663efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1664efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1665efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1666efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1667efe9872eSLisandro Dalcin 
1668efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1669efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1670efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1671efe9872eSLisandro Dalcin 
1672efe9872eSLisandro Dalcin   if (!I2Function) {
1673efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1674efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1675efe9872eSLisandro Dalcin   }
1676efe9872eSLisandro Dalcin 
1677efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1680efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1681efe9872eSLisandro Dalcin   PetscStackPop;
1682efe9872eSLisandro Dalcin 
1683efe9872eSLisandro Dalcin   if (rhsfunction) {
1684efe9872eSLisandro Dalcin     Vec Frhs;
1685efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1686efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1687efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1688efe9872eSLisandro Dalcin   }
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1691efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1692efe9872eSLisandro Dalcin }
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin /*@
1695efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1696efe9872eSLisandro Dalcin 
1697d083f849SBarry Smith   Collective on TS
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin   Input Parameters:
1700efe9872eSLisandro Dalcin + ts - the TS context
1701efe9872eSLisandro Dalcin . t - current timestep
1702efe9872eSLisandro Dalcin . U - state vector
1703efe9872eSLisandro Dalcin . V - time derivative of state vector
1704efe9872eSLisandro Dalcin . A - second time derivative of state vector
1705efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1706efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin   Output Parameters:
1709efe9872eSLisandro Dalcin + J - Jacobian matrix
1710efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin   Notes:
1713efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1714efe9872eSLisandro Dalcin 
1715efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1716efe9872eSLisandro Dalcin 
1717efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1718efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1719efe9872eSLisandro Dalcin 
1720efe9872eSLisandro Dalcin   Level: developer
1721efe9872eSLisandro Dalcin 
1722efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1723efe9872eSLisandro Dalcin @*/
1724efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1725efe9872eSLisandro Dalcin {
1726efe9872eSLisandro Dalcin   DM             dm;
1727efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1728efe9872eSLisandro Dalcin   void           *ctx;
1729efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1730efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1731efe9872eSLisandro Dalcin 
1732efe9872eSLisandro Dalcin   PetscFunctionBegin;
1733efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1734efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1735efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1736efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1737efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1738efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1741efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1742efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1743efe9872eSLisandro Dalcin 
1744efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1745efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1746efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1747efe9872eSLisandro Dalcin   }
1748efe9872eSLisandro Dalcin 
1749efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1750efe9872eSLisandro Dalcin 
1751efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1752efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1753efe9872eSLisandro Dalcin   PetscStackPop;
1754efe9872eSLisandro Dalcin 
1755efe9872eSLisandro Dalcin   if (rhsjacobian) {
1756d60b7d5cSBarry Smith     Mat Jrhs,Prhs;
1757efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1758efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1759d60b7d5cSBarry Smith     ierr = MatAXPY(J,-1,Jrhs,ts->axpy_pattern);CHKERRQ(ierr);
1760d60b7d5cSBarry Smith     if (P != J) {ierr = MatAXPY(P,-1,Prhs,ts->axpy_pattern);CHKERRQ(ierr);}
1761efe9872eSLisandro Dalcin   }
1762efe9872eSLisandro Dalcin 
1763efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1764efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1765efe9872eSLisandro Dalcin }
1766efe9872eSLisandro Dalcin 
1767438f35afSJed Brown /*@C
1768438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1769438f35afSJed Brown 
1770438f35afSJed Brown    Logically Collective
1771438f35afSJed Brown 
17724165533cSJose E. Roman    Input Parameters:
1773438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1774a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1775438f35afSJed Brown -  ctx - a context for tvar
1776438f35afSJed Brown 
1777a96d6ef6SBarry Smith     Calling sequence of tvar:
1778a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1779a96d6ef6SBarry Smith 
1780a96d6ef6SBarry Smith +   ts - timestep context
1781a96d6ef6SBarry Smith .   p - input vector (primative form)
1782a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1783a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1784a96d6ef6SBarry Smith 
1785438f35afSJed Brown    Level: advanced
1786438f35afSJed Brown 
1787438f35afSJed Brown    Notes:
1788438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1789438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1790438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1791438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1792438f35afSJed Brown    evaluated via the chain rule, as in
1793438f35afSJed Brown 
1794438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1795438f35afSJed Brown 
1796438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1797438f35afSJed Brown @*/
1798438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1799438f35afSJed Brown {
1800438f35afSJed Brown   PetscErrorCode ierr;
1801438f35afSJed Brown   DM             dm;
1802438f35afSJed Brown 
1803438f35afSJed Brown   PetscFunctionBegin;
1804438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1805438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1806438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1807438f35afSJed Brown   PetscFunctionReturn(0);
1808438f35afSJed Brown }
1809438f35afSJed Brown 
1810efe9872eSLisandro Dalcin /*@
1811e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1812e3c11fc1SJed Brown 
1813e3c11fc1SJed Brown    Logically Collective
1814e3c11fc1SJed Brown 
1815e3c11fc1SJed Brown    Input Parameters:
1816e3c11fc1SJed Brown +  ts - TS on which to compute
1817e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1818e3c11fc1SJed Brown 
1819e3c11fc1SJed Brown    Output Parameters:
1820e3c11fc1SJed Brown .  C - transient (conservative) variable
1821e3c11fc1SJed Brown 
1822e3c11fc1SJed Brown    Developer Notes:
1823e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1824e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1825e3c11fc1SJed Brown    being used.
1826e3c11fc1SJed Brown 
1827e3c11fc1SJed Brown    Level: developer
1828e3c11fc1SJed Brown 
1829e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1830e3c11fc1SJed Brown @*/
1831e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1832e3c11fc1SJed Brown {
1833e3c11fc1SJed Brown   PetscErrorCode ierr;
1834e3c11fc1SJed Brown   DM             dm;
1835e3c11fc1SJed Brown   DMTS           dmts;
1836e3c11fc1SJed Brown 
1837e3c11fc1SJed Brown   PetscFunctionBegin;
1838e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1839e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1840e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1841e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1842e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1843e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1844e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1845e3c11fc1SJed Brown   }
1846e3c11fc1SJed Brown   PetscFunctionReturn(0);
1847e3c11fc1SJed Brown }
1848e3c11fc1SJed Brown 
1849e3c11fc1SJed Brown /*@
1850e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1851e3c11fc1SJed Brown 
1852e3c11fc1SJed Brown    Logically Collective
1853e3c11fc1SJed Brown 
1854e3c11fc1SJed Brown    Input Parameters:
1855e3c11fc1SJed Brown .  ts - TS on which to compute
1856e3c11fc1SJed Brown 
1857e3c11fc1SJed Brown    Output Parameters:
1858e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1859e3c11fc1SJed Brown 
1860e3c11fc1SJed Brown    Level: developer
1861e3c11fc1SJed Brown 
1862e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1863e3c11fc1SJed Brown @*/
1864e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1865e3c11fc1SJed Brown {
1866e3c11fc1SJed Brown   PetscErrorCode ierr;
1867e3c11fc1SJed Brown   DM             dm;
1868e3c11fc1SJed Brown   DMTS           dmts;
1869e3c11fc1SJed Brown 
1870e3c11fc1SJed Brown   PetscFunctionBegin;
1871e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1872e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1873e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1874e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1875e3c11fc1SJed Brown   PetscFunctionReturn(0);
1876e3c11fc1SJed Brown }
1877e3c11fc1SJed Brown 
1878e3c11fc1SJed Brown /*@
1879efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1880efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1881efe9872eSLisandro Dalcin 
1882d083f849SBarry Smith    Logically Collective on TS
1883efe9872eSLisandro Dalcin 
1884efe9872eSLisandro Dalcin    Input Parameters:
1885efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1886efe9872eSLisandro Dalcin .  u - the solution vector
1887efe9872eSLisandro Dalcin -  v - the time derivative vector
1888efe9872eSLisandro Dalcin 
1889efe9872eSLisandro Dalcin    Level: beginner
1890efe9872eSLisandro Dalcin 
1891efe9872eSLisandro Dalcin @*/
1892efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1893efe9872eSLisandro Dalcin {
1894efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1895efe9872eSLisandro Dalcin 
1896efe9872eSLisandro Dalcin   PetscFunctionBegin;
1897efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1898efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1899efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1900efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1901efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1902efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1903efe9872eSLisandro Dalcin   ts->vec_dot = v;
1904efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1905efe9872eSLisandro Dalcin }
1906efe9872eSLisandro Dalcin 
1907efe9872eSLisandro Dalcin /*@
1908efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1909efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1910efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1911efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1912efe9872eSLisandro Dalcin 
1913efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1914efe9872eSLisandro Dalcin 
1915efe9872eSLisandro Dalcin    Input Parameter:
1916efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1917efe9872eSLisandro Dalcin 
1918d8d19677SJose E. Roman    Output Parameters:
1919efe9872eSLisandro Dalcin +  u - the vector containing the solution
1920efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1921efe9872eSLisandro Dalcin 
1922efe9872eSLisandro Dalcin    Level: intermediate
1923efe9872eSLisandro Dalcin 
1924efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1925efe9872eSLisandro Dalcin 
1926efe9872eSLisandro Dalcin @*/
1927efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1928efe9872eSLisandro Dalcin {
1929efe9872eSLisandro Dalcin   PetscFunctionBegin;
1930efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1931efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1932efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1933efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1934efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1935efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1936efe9872eSLisandro Dalcin }
1937efe9872eSLisandro Dalcin 
193855849f57SBarry Smith /*@C
193955849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
194055849f57SBarry Smith 
194155849f57SBarry Smith   Collective on PetscViewer
194255849f57SBarry Smith 
194355849f57SBarry Smith   Input Parameters:
194455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
194555849f57SBarry Smith            some related function before a call to TSLoad().
194655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
194755849f57SBarry Smith 
194855849f57SBarry Smith    Level: intermediate
194955849f57SBarry Smith 
195055849f57SBarry Smith   Notes:
195155849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
195255849f57SBarry Smith 
195355849f57SBarry Smith   Notes for advanced users:
195455849f57SBarry Smith   Most users should not need to know the details of the binary storage
195555849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
195655849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
195755849f57SBarry Smith   format is
195855849f57SBarry Smith .vb
195955849f57SBarry Smith      has not yet been determined
196055849f57SBarry Smith .ve
196155849f57SBarry Smith 
196255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
196355849f57SBarry Smith @*/
1964f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
196555849f57SBarry Smith {
196655849f57SBarry Smith   PetscErrorCode ierr;
196755849f57SBarry Smith   PetscBool      isbinary;
196855849f57SBarry Smith   PetscInt       classid;
196955849f57SBarry Smith   char           type[256];
19702d53ad75SBarry Smith   DMTS           sdm;
1971ad6bc421SBarry Smith   DM             dm;
197255849f57SBarry Smith 
197355849f57SBarry Smith   PetscFunctionBegin;
1974f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
197555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
197655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19772c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!isbinary,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
197855849f57SBarry Smith 
1979060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
19802c71b3e2SJacob Faibussowitsch   PetscCheckFalse(classid != TS_FILE_CLASSID,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1981060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1982f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1983f2c2a1b9SBarry Smith   if (ts->ops->load) {
1984f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1985f2c2a1b9SBarry Smith   }
1986ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1987ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1988ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1989f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1990f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19912d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19922d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
199355849f57SBarry Smith   PetscFunctionReturn(0);
199455849f57SBarry Smith }
199555849f57SBarry Smith 
19969804daf3SBarry Smith #include <petscdraw.h>
1997e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1998e04113cfSBarry Smith #include <petscviewersaws.h>
1999f05ece33SBarry Smith #endif
2000fe2efc57SMark 
2001fe2efc57SMark /*@C
2002fe2efc57SMark    TSViewFromOptions - View from Options
2003fe2efc57SMark 
2004fe2efc57SMark    Collective on TS
2005fe2efc57SMark 
2006fe2efc57SMark    Input Parameters:
2007fe2efc57SMark +  A - the application ordering context
2008736c3998SJose E. Roman .  obj - Optional object
2009736c3998SJose E. Roman -  name - command line option
2010fe2efc57SMark 
2011fe2efc57SMark    Level: intermediate
2012fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
2013fe2efc57SMark @*/
2014fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
2015fe2efc57SMark {
2016fe2efc57SMark   PetscErrorCode ierr;
2017fe2efc57SMark 
2018fe2efc57SMark   PetscFunctionBegin;
2019fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
2020fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
2021fe2efc57SMark   PetscFunctionReturn(0);
2022fe2efc57SMark }
2023fe2efc57SMark 
20247e2c5f70SBarry Smith /*@C
2025d763cef2SBarry Smith     TSView - Prints the TS data structure.
2026d763cef2SBarry Smith 
20274c49b128SBarry Smith     Collective on TS
2028d763cef2SBarry Smith 
2029d763cef2SBarry Smith     Input Parameters:
2030d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2031d763cef2SBarry Smith -   viewer - visualization context
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith     Options Database Key:
2034d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2035d763cef2SBarry Smith 
2036d763cef2SBarry Smith     Notes:
2037d763cef2SBarry Smith     The available visualization contexts include
2038b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2039b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2040d763cef2SBarry Smith          output where only the first processor opens
2041d763cef2SBarry Smith          the file.  All other processors send their
2042d763cef2SBarry Smith          data to the first processor to print.
2043d763cef2SBarry Smith 
2044d763cef2SBarry Smith     The user can open an alternative visualization context with
2045b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2046d763cef2SBarry Smith 
2047595c91d4SBarry Smith     In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer).
2048595c91d4SBarry Smith 
2049d763cef2SBarry Smith     Level: beginner
2050d763cef2SBarry Smith 
2051b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2052d763cef2SBarry Smith @*/
20537087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2054d763cef2SBarry Smith {
2055dfbe8321SBarry Smith   PetscErrorCode ierr;
205619fd82e9SBarry Smith   TSType         type;
20572b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20582d53ad75SBarry Smith   DMTS           sdm;
2059e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2060536b137fSBarry Smith   PetscBool      issaws;
2061f05ece33SBarry Smith #endif
2062d763cef2SBarry Smith 
2063d763cef2SBarry Smith   PetscFunctionBegin;
20640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20653050cee2SBarry Smith   if (!viewer) {
2066ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20673050cee2SBarry Smith   }
20680700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2069c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2070fd16b177SBarry Smith 
2071251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2072251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
207355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20742b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2075e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2076536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2077f05ece33SBarry Smith #endif
207832077d6dSBarry Smith   if (iascii) {
2079dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2080efd4aadfSBarry Smith     if (ts->ops->view) {
2081efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2082efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2083efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2084efd4aadfSBarry Smith     }
2085ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
208677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2087ef85077eSLisandro Dalcin     }
2088ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20897c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2090ef85077eSLisandro Dalcin     }
2091a6ab3590SBarry Smith     if (ts->ifuncs) {
2092a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of I function evaluations=%D\n",ts->ifuncs);CHKERRQ(ierr);
2093a6ab3590SBarry Smith     }
2094a6ab3590SBarry Smith     if (ts->ijacs) {
2095a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of I Jacobian evaluations=%D\n",ts->ijacs);CHKERRQ(ierr);
2096a6ab3590SBarry Smith     }
2097a6ab3590SBarry Smith     if (ts->rhsfuncs) {
2098a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of RHS function evaluations=%D\n",ts->rhsfuncs);CHKERRQ(ierr);
2099a6ab3590SBarry Smith     }
2100a6ab3590SBarry Smith     if (ts->rhsjacs) {
2101a6ab3590SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of RHS Jacobian evaluations=%D\n",ts->rhsjacs);CHKERRQ(ierr);
2102a6ab3590SBarry Smith     }
2103efd4aadfSBarry Smith     if (ts->usessnes) {
2104efd4aadfSBarry Smith       PetscBool lin;
2105d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
21065ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2107d763cef2SBarry Smith       }
21085ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
21091ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2110efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2111efd4aadfSBarry Smith     }
2112193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2113a0af407cSBarry Smith     if (ts->vrtol) {
2114a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2115a0af407cSBarry Smith     } else {
2116a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2117a0af407cSBarry Smith     }
2118a0af407cSBarry Smith     if (ts->vatol) {
2119a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2120a0af407cSBarry Smith     } else {
2121a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2122a0af407cSBarry Smith     }
2123825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2124efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2125825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21260f5bd95cSBarry Smith   } else if (isstring) {
2127a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
212836a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
212936a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
213055849f57SBarry Smith   } else if (isbinary) {
213155849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
213255849f57SBarry Smith     MPI_Comm    comm;
213355849f57SBarry Smith     PetscMPIInt rank;
213455849f57SBarry Smith     char        type[256];
213555849f57SBarry Smith 
213655849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
2137ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
2138dd400576SPatrick Sanan     if (rank == 0) {
2139f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
214055849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2141f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
214255849f57SBarry Smith     }
214355849f57SBarry Smith     if (ts->ops->view) {
214455849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
214555849f57SBarry Smith     }
2146efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2147f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2148f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21492d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21502d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21512b0a91c0SBarry Smith   } else if (isdraw) {
21522b0a91c0SBarry Smith     PetscDraw draw;
21532b0a91c0SBarry Smith     char      str[36];
215489fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21552b0a91c0SBarry Smith 
21562b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21572b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21582b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21592b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
216051fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
216189fd9fafSBarry Smith     bottom = y - h;
21622b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21632b0a91c0SBarry Smith     if (ts->ops->view) {
21642b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21652b0a91c0SBarry Smith     }
2166efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2167efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21682b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2169e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2170536b137fSBarry Smith   } else if (issaws) {
2171d45a07a7SBarry Smith     PetscMPIInt rank;
21722657e9d9SBarry Smith     const char  *name;
21732657e9d9SBarry Smith 
21742657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2175ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
2176dd400576SPatrick Sanan     if (!((PetscObject)ts)->amsmem && rank == 0) {
2177d45a07a7SBarry Smith       char       dir[1024];
2178d45a07a7SBarry Smith 
2179e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2180a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21812657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21822657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21832657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2184d763cef2SBarry Smith     }
21850acecf5bSBarry Smith     if (ts->ops->view) {
21860acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21870acecf5bSBarry Smith     }
2188f05ece33SBarry Smith #endif
2189f05ece33SBarry Smith   }
219036a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
219136a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
219236a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
219336a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
219436a9e3b9SBarry Smith   }
219536a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
219636a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2197f05ece33SBarry Smith 
2198b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2199251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2200b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2201d763cef2SBarry Smith   PetscFunctionReturn(0);
2202d763cef2SBarry Smith }
2203d763cef2SBarry Smith 
2204b07ff414SBarry Smith /*@
2205d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2206d763cef2SBarry Smith    the timesteppers.
2207d763cef2SBarry Smith 
22083f9fe445SBarry Smith    Logically Collective on TS
2209d763cef2SBarry Smith 
2210d763cef2SBarry Smith    Input Parameters:
2211d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2212d763cef2SBarry Smith -  usrP - optional user context
2213d763cef2SBarry Smith 
221495452b02SPatrick Sanan    Fortran Notes:
221595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2216daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2217daf670e6SBarry Smith 
2218d763cef2SBarry Smith    Level: intermediate
2219d763cef2SBarry Smith 
2220d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2221d763cef2SBarry Smith @*/
22227087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2223d763cef2SBarry Smith {
2224d763cef2SBarry Smith   PetscFunctionBegin;
22250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2226d763cef2SBarry Smith   ts->user = usrP;
2227d763cef2SBarry Smith   PetscFunctionReturn(0);
2228d763cef2SBarry Smith }
2229d763cef2SBarry Smith 
2230b07ff414SBarry Smith /*@
2231d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2232d763cef2SBarry Smith     timestepper.
2233d763cef2SBarry Smith 
2234d763cef2SBarry Smith     Not Collective
2235d763cef2SBarry Smith 
2236d763cef2SBarry Smith     Input Parameter:
2237d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2238d763cef2SBarry Smith 
2239d763cef2SBarry Smith     Output Parameter:
2240d763cef2SBarry Smith .   usrP - user context
2241d763cef2SBarry Smith 
224295452b02SPatrick Sanan    Fortran Notes:
224395452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2244daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2245daf670e6SBarry Smith 
2246d763cef2SBarry Smith     Level: intermediate
2247d763cef2SBarry Smith 
2248d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2249d763cef2SBarry Smith @*/
2250e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2251d763cef2SBarry Smith {
2252d763cef2SBarry Smith   PetscFunctionBegin;
22530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2254e71120c6SJed Brown   *(void**)usrP = ts->user;
2255d763cef2SBarry Smith   PetscFunctionReturn(0);
2256d763cef2SBarry Smith }
2257d763cef2SBarry Smith 
2258d763cef2SBarry Smith /*@
225980275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2260d763cef2SBarry Smith 
2261d763cef2SBarry Smith    Not Collective
2262d763cef2SBarry Smith 
2263d763cef2SBarry Smith    Input Parameter:
2264d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2265d763cef2SBarry Smith 
2266d763cef2SBarry Smith    Output Parameter:
226780275a0aSLisandro Dalcin .  steps - number of steps completed so far
2268d763cef2SBarry Smith 
2269d763cef2SBarry Smith    Level: intermediate
2270d763cef2SBarry Smith 
22719be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2272d763cef2SBarry Smith @*/
227380275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2274d763cef2SBarry Smith {
2275d763cef2SBarry Smith   PetscFunctionBegin;
22760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227780275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
227880275a0aSLisandro Dalcin   *steps = ts->steps;
227980275a0aSLisandro Dalcin   PetscFunctionReturn(0);
228080275a0aSLisandro Dalcin }
228180275a0aSLisandro Dalcin 
228280275a0aSLisandro Dalcin /*@
228380275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
228480275a0aSLisandro Dalcin 
228580275a0aSLisandro Dalcin    Logically Collective on TS
228680275a0aSLisandro Dalcin 
228780275a0aSLisandro Dalcin    Input Parameters:
228880275a0aSLisandro Dalcin +  ts - the TS context
228980275a0aSLisandro Dalcin -  steps - number of steps completed so far
229080275a0aSLisandro Dalcin 
229180275a0aSLisandro Dalcin    Notes:
229280275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
229380275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
229480275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
229580275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
229680275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
229780275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
229880275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
229980275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
230080275a0aSLisandro Dalcin 
230180275a0aSLisandro Dalcin    Level: advanced
230280275a0aSLisandro Dalcin 
230380275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
230480275a0aSLisandro Dalcin @*/
230580275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
230680275a0aSLisandro Dalcin {
230780275a0aSLisandro Dalcin   PetscFunctionBegin;
230880275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
230980275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
23102c71b3e2SJacob Faibussowitsch   PetscCheckFalse(steps < 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
231180275a0aSLisandro Dalcin   ts->steps = steps;
2312d763cef2SBarry Smith   PetscFunctionReturn(0);
2313d763cef2SBarry Smith }
2314d763cef2SBarry Smith 
2315d763cef2SBarry Smith /*@
2316d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2317d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2318d763cef2SBarry Smith 
23193f9fe445SBarry Smith    Logically Collective on TS
2320d763cef2SBarry Smith 
2321d763cef2SBarry Smith    Input Parameters:
2322d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2323d763cef2SBarry Smith -  time_step - the size of the timestep
2324d763cef2SBarry Smith 
2325d763cef2SBarry Smith    Level: intermediate
2326d763cef2SBarry Smith 
2327aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2328d763cef2SBarry Smith 
2329d763cef2SBarry Smith @*/
23307087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2331d763cef2SBarry Smith {
2332d763cef2SBarry Smith   PetscFunctionBegin;
23330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2334c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2335d763cef2SBarry Smith   ts->time_step = time_step;
2336d763cef2SBarry Smith   PetscFunctionReturn(0);
2337d763cef2SBarry Smith }
2338d763cef2SBarry Smith 
2339a43b19c4SJed Brown /*@
234049354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
234149354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
234249354f04SShri Abhyankar      or just return at the final time TS computed.
2343a43b19c4SJed Brown 
2344a43b19c4SJed Brown   Logically Collective on TS
2345a43b19c4SJed Brown 
2346d8d19677SJose E. Roman    Input Parameters:
2347a43b19c4SJed Brown +   ts - the time-step context
234849354f04SShri Abhyankar -   eftopt - exact final time option
2349a43b19c4SJed Brown 
2350feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2351feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2352feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2353feed9e9dSBarry Smith 
2354feed9e9dSBarry Smith    Options Database:
2355feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2356feed9e9dSBarry Smith 
2357ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2358ee346746SBarry Smith     then the final time you selected.
2359ee346746SBarry Smith 
2360a43b19c4SJed Brown    Level: beginner
2361a43b19c4SJed Brown 
2362f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2363a43b19c4SJed Brown @*/
236449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2365a43b19c4SJed Brown {
2366a43b19c4SJed Brown   PetscFunctionBegin;
2367a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
236849354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
236949354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2370a43b19c4SJed Brown   PetscFunctionReturn(0);
2371a43b19c4SJed Brown }
2372a43b19c4SJed Brown 
2373d763cef2SBarry Smith /*@
2374f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2375f6953c82SLisandro Dalcin 
2376f6953c82SLisandro Dalcin    Not Collective
2377f6953c82SLisandro Dalcin 
2378f6953c82SLisandro Dalcin    Input Parameter:
2379f6953c82SLisandro Dalcin .  ts - the TS context
2380f6953c82SLisandro Dalcin 
2381f6953c82SLisandro Dalcin    Output Parameter:
2382f6953c82SLisandro Dalcin .  eftopt - exact final time option
2383f6953c82SLisandro Dalcin 
2384f6953c82SLisandro Dalcin    Level: beginner
2385f6953c82SLisandro Dalcin 
2386f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2387f6953c82SLisandro Dalcin @*/
2388f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2389f6953c82SLisandro Dalcin {
2390f6953c82SLisandro Dalcin   PetscFunctionBegin;
2391f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2392f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2393f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2394f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2395f6953c82SLisandro Dalcin }
2396f6953c82SLisandro Dalcin 
2397f6953c82SLisandro Dalcin /*@
2398d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2399d763cef2SBarry Smith 
2400d763cef2SBarry Smith    Not Collective
2401d763cef2SBarry Smith 
2402d763cef2SBarry Smith    Input Parameter:
2403d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2404d763cef2SBarry Smith 
2405d763cef2SBarry Smith    Output Parameter:
2406d763cef2SBarry Smith .  dt - the current timestep size
2407d763cef2SBarry Smith 
2408d763cef2SBarry Smith    Level: intermediate
2409d763cef2SBarry Smith 
2410aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2411d763cef2SBarry Smith 
2412d763cef2SBarry Smith @*/
24137087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2414d763cef2SBarry Smith {
2415d763cef2SBarry Smith   PetscFunctionBegin;
24160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2417f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2418d763cef2SBarry Smith   *dt = ts->time_step;
2419d763cef2SBarry Smith   PetscFunctionReturn(0);
2420d763cef2SBarry Smith }
2421d763cef2SBarry Smith 
2422d8e5e3e6SSatish Balay /*@
2423d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2424d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2425d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2426d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2427d763cef2SBarry Smith 
2428d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2429d763cef2SBarry Smith 
2430d763cef2SBarry Smith    Input Parameter:
2431d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2432d763cef2SBarry Smith 
2433d763cef2SBarry Smith    Output Parameter:
2434d763cef2SBarry Smith .  v - the vector containing the solution
2435d763cef2SBarry Smith 
243663e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
243763e21af5SBarry Smith    final time. It returns the solution at the next timestep.
243863e21af5SBarry Smith 
2439d763cef2SBarry Smith    Level: intermediate
2440d763cef2SBarry Smith 
24410ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2442d763cef2SBarry Smith 
2443d763cef2SBarry Smith @*/
24447087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2445d763cef2SBarry Smith {
2446d763cef2SBarry Smith   PetscFunctionBegin;
24470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24484482741eSBarry Smith   PetscValidPointer(v,2);
24498737fe31SLisandro Dalcin   *v = ts->vec_sol;
2450d763cef2SBarry Smith   PetscFunctionReturn(0);
2451d763cef2SBarry Smith }
2452d763cef2SBarry Smith 
245303fe5f5eSDebojyoti Ghosh /*@
2454b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
245503fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2456b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
245703fe5f5eSDebojyoti Ghosh    structure as the solution vector.
245803fe5f5eSDebojyoti Ghosh 
245903fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
246003fe5f5eSDebojyoti Ghosh 
246103fe5f5eSDebojyoti Ghosh    Parameters :
2462a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2463b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2464b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
246503fe5f5eSDebojyoti Ghosh        returned in v.
2466a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
246703fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2468b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
246903fe5f5eSDebojyoti Ghosh 
24704cdd57e5SDebojyoti Ghosh    Level: advanced
247103fe5f5eSDebojyoti Ghosh 
247203fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
247303fe5f5eSDebojyoti Ghosh 
247403fe5f5eSDebojyoti Ghosh @*/
2475b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
247603fe5f5eSDebojyoti Ghosh {
247703fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
247803fe5f5eSDebojyoti Ghosh 
247903fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
248003fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2481b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
248203fe5f5eSDebojyoti Ghosh   else {
2483b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
248403fe5f5eSDebojyoti Ghosh   }
248503fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
248603fe5f5eSDebojyoti Ghosh }
248703fe5f5eSDebojyoti Ghosh 
24884cdd57e5SDebojyoti Ghosh /*@
24894cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24904cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24914cdd57e5SDebojyoti Ghosh 
24924cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24934cdd57e5SDebojyoti Ghosh 
24944cdd57e5SDebojyoti Ghosh    Parameters :
2495a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2496a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24974cdd57e5SDebojyoti Ghosh 
24984cdd57e5SDebojyoti Ghosh    Level: intermediate
24994cdd57e5SDebojyoti Ghosh 
25004cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
25014cdd57e5SDebojyoti Ghosh 
25024cdd57e5SDebojyoti Ghosh @*/
25034cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
25044cdd57e5SDebojyoti Ghosh {
25054cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25064cdd57e5SDebojyoti Ghosh 
25074cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25084cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2509f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
25104cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2511f6356ec7SDebojyoti Ghosh   } else {
2512f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2513f6356ec7SDebojyoti Ghosh   }
25144cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25154cdd57e5SDebojyoti Ghosh }
25164cdd57e5SDebojyoti Ghosh 
25174cdd57e5SDebojyoti Ghosh /*@
25184cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
25194cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
25204cdd57e5SDebojyoti Ghosh 
25214cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25224cdd57e5SDebojyoti Ghosh 
25239657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
25249657682dSDebojyoti Ghosh 
25254cdd57e5SDebojyoti Ghosh    Parameters :
2526a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2527657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2528a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
25294cdd57e5SDebojyoti Ghosh 
25304cdd57e5SDebojyoti Ghosh    Level: intermediate
25314cdd57e5SDebojyoti Ghosh 
253257df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25334cdd57e5SDebojyoti Ghosh 
25344cdd57e5SDebojyoti Ghosh @*/
25350a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25364cdd57e5SDebojyoti Ghosh {
25374cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25384cdd57e5SDebojyoti Ghosh 
25394cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25404cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2541f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25420a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2543f6356ec7SDebojyoti Ghosh   } else {
2544f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2545f6356ec7SDebojyoti Ghosh   }
25464cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25474cdd57e5SDebojyoti Ghosh }
25484cdd57e5SDebojyoti Ghosh 
254957df6a1bSDebojyoti Ghosh /*@
255057df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
255157df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
255257df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
255357df6a1bSDebojyoti Ghosh 
255457df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
255557df6a1bSDebojyoti Ghosh 
255657df6a1bSDebojyoti Ghosh    Parameters :
2557a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2558a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
255957df6a1bSDebojyoti Ghosh 
256057df6a1bSDebojyoti Ghosh    Level: intermediate
256157df6a1bSDebojyoti Ghosh 
256257df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
256357df6a1bSDebojyoti Ghosh 
256457df6a1bSDebojyoti Ghosh @*/
256557df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
256657df6a1bSDebojyoti Ghosh {
256757df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
256857df6a1bSDebojyoti Ghosh 
256957df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
257057df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25712c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!ts->setupcalled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
257257df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
257357df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
257457df6a1bSDebojyoti Ghosh   }
257557df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
257657df6a1bSDebojyoti Ghosh }
257757df6a1bSDebojyoti Ghosh 
2578bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2579d8e5e3e6SSatish Balay /*@
2580bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2581d763cef2SBarry Smith 
2582bdad233fSMatthew Knepley   Not collective
2583d763cef2SBarry Smith 
2584bdad233fSMatthew Knepley   Input Parameters:
2585bdad233fSMatthew Knepley + ts   - The TS
2586bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2587d763cef2SBarry Smith .vb
25880910c330SBarry Smith          U_t - A U = 0      (linear)
25890910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25900910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2591d763cef2SBarry Smith .ve
2592d763cef2SBarry Smith 
2593d763cef2SBarry Smith    Level: beginner
2594d763cef2SBarry Smith 
2595bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2596d763cef2SBarry Smith @*/
25977087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2598a7cc72afSBarry Smith {
25999e2a6581SJed Brown   PetscErrorCode ierr;
26009e2a6581SJed Brown 
2601d763cef2SBarry Smith   PetscFunctionBegin;
26020700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2603bdad233fSMatthew Knepley   ts->problem_type = type;
26049e2a6581SJed Brown   if (type == TS_LINEAR) {
26059e2a6581SJed Brown     SNES snes;
26069e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
26079e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
26089e2a6581SJed Brown   }
2609d763cef2SBarry Smith   PetscFunctionReturn(0);
2610d763cef2SBarry Smith }
2611d763cef2SBarry Smith 
2612bdad233fSMatthew Knepley /*@C
2613bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2614bdad233fSMatthew Knepley 
2615bdad233fSMatthew Knepley   Not collective
2616bdad233fSMatthew Knepley 
2617bdad233fSMatthew Knepley   Input Parameter:
2618bdad233fSMatthew Knepley . ts   - The TS
2619bdad233fSMatthew Knepley 
2620bdad233fSMatthew Knepley   Output Parameter:
2621bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2622bdad233fSMatthew Knepley .vb
2623089b2837SJed Brown          M U_t = A U
2624089b2837SJed Brown          M(t) U_t = A(t) U
2625b5abc632SBarry Smith          F(t,U,U_t)
2626bdad233fSMatthew Knepley .ve
2627bdad233fSMatthew Knepley 
2628bdad233fSMatthew Knepley    Level: beginner
2629bdad233fSMatthew Knepley 
2630bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2631bdad233fSMatthew Knepley @*/
26327087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2633a7cc72afSBarry Smith {
2634bdad233fSMatthew Knepley   PetscFunctionBegin;
26350700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26364482741eSBarry Smith   PetscValidIntPointer(type,2);
2637bdad233fSMatthew Knepley   *type = ts->problem_type;
2638bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2639bdad233fSMatthew Knepley }
2640d763cef2SBarry Smith 
2641303a5415SBarry Smith /*
2642303a5415SBarry 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()
2643303a5415SBarry Smith */
2644303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2645303a5415SBarry Smith {
2646303a5415SBarry Smith   PetscErrorCode ierr;
2647303a5415SBarry Smith   PetscBool      isnone;
2648303a5415SBarry Smith 
2649303a5415SBarry Smith   PetscFunctionBegin;
2650303a5415SBarry Smith   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
2651303a5415SBarry Smith   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
2652303a5415SBarry Smith 
2653303a5415SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
2654303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2655303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2656303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2657303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2658303a5415SBarry Smith   }
2659303a5415SBarry Smith   PetscFunctionReturn(0);
2660303a5415SBarry Smith }
2661303a5415SBarry Smith 
2662d763cef2SBarry Smith /*@
2663303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2664d763cef2SBarry Smith 
2665d763cef2SBarry Smith    Collective on TS
2666d763cef2SBarry Smith 
2667d763cef2SBarry Smith    Input Parameter:
2668d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2669d763cef2SBarry Smith 
2670d763cef2SBarry Smith    Notes:
2671d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2672d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2673141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2674d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2675141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2676d763cef2SBarry Smith 
2677d763cef2SBarry Smith    Level: advanced
2678d763cef2SBarry Smith 
2679141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2680d763cef2SBarry Smith @*/
26817087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2682d763cef2SBarry Smith {
2683dfbe8321SBarry Smith   PetscErrorCode ierr;
26846c6b9e74SPeter Brune   DM             dm;
26856c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2686d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2687cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26886c6b9e74SPeter Brune   TSIJacobian    ijac;
2689efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26906c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2691d763cef2SBarry Smith 
2692d763cef2SBarry Smith   PetscFunctionBegin;
26930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2694277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2695277b19d0SLisandro Dalcin 
26967adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2697cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2698cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2699d763cef2SBarry Smith   }
2700277b19d0SLisandro Dalcin 
27011a638600SStefano Zampini   if (!ts->vec_sol) {
27021a638600SStefano Zampini     if (ts->dm) {
27031a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
27041a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
27051a638600SStefano Zampini   }
2706277b19d0SLisandro Dalcin 
2707298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2708298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2709298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2710298bade4SHong Zhang   }
2711298bade4SHong Zhang 
2712cd4cee2dSHong Zhang   if (ts->quadraturets) {
2713cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2714ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2715cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2716cd4cee2dSHong Zhang   }
2717cd4cee2dSHong Zhang 
2718971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2719f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2720e1244c69SJed Brown     Mat Amat,Pmat;
2721e1244c69SJed Brown     SNES snes;
2722e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2723e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2724e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2725e1244c69SJed Brown      * have displaced the RHS matrix */
2726971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2727abc0d4abSBarry 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 */
2728abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2729e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2730e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2731e1244c69SJed Brown     }
2732971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2733abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2734e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2735e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2736e1244c69SJed Brown     }
2737e1244c69SJed Brown   }
27382ffb9264SLisandro Dalcin 
27392ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27402ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27412ffb9264SLisandro Dalcin 
2742277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2743000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2744277b19d0SLisandro Dalcin   }
2745277b19d0SLisandro Dalcin 
2746303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
27472ffb9264SLisandro Dalcin 
2748a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27496c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27506c6b9e74SPeter Brune    */
27516c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27520298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27536c6b9e74SPeter Brune   if (!func) {
27546c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27556c6b9e74SPeter Brune   }
2756a6772fa2SLisandro 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.
27576c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27586c6b9e74SPeter Brune    */
27590298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27600298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2761efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27620298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2763efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27646c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27656c6b9e74SPeter Brune   }
2766c0517034SDebojyoti Ghosh 
2767c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2768c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2769c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2770c0517034SDebojyoti Ghosh   }
2771c0517034SDebojyoti Ghosh 
2772277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2773277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2774277b19d0SLisandro Dalcin }
2775277b19d0SLisandro Dalcin 
2776f6a906c0SBarry Smith /*@
2777277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2778277b19d0SLisandro Dalcin 
2779277b19d0SLisandro Dalcin    Collective on TS
2780277b19d0SLisandro Dalcin 
2781277b19d0SLisandro Dalcin    Input Parameter:
2782277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2783277b19d0SLisandro Dalcin 
2784277b19d0SLisandro Dalcin    Level: beginner
2785277b19d0SLisandro Dalcin 
2786277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2787277b19d0SLisandro Dalcin @*/
2788277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2789277b19d0SLisandro Dalcin {
27901d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2791277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2792277b19d0SLisandro Dalcin 
2793277b19d0SLisandro Dalcin   PetscFunctionBegin;
2794277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2795b18ea86cSHong Zhang 
2796277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2797277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2798277b19d0SLisandro Dalcin   }
2799277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2800e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2801bbd56ea5SKarl Rupp 
28024e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
28034e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2804214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
28056bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2806efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2807e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2808e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
280938637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2810bbd56ea5SKarl Rupp 
2811cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2812ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2813ecf68647SHong Zhang   if (ts->forward_solve) {
2814ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2815ecf68647SHong Zhang   }
2816cd4cee2dSHong Zhang   if (ts->quadraturets) {
2817cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
281836eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2819cd4cee2dSHong Zhang   }
28201d06f6b3SHong Zhang   while (ilink) {
28211d06f6b3SHong Zhang     next = ilink->next;
28221d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
28231d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
28241d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
28251d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
28261d06f6b3SHong Zhang     ilink = next;
282787f4e208SHong Zhang   }
28286bf673ebSJoe Pusztay   ts->tsrhssplit = NULL;
2829545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2830277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2831d763cef2SBarry Smith   PetscFunctionReturn(0);
2832d763cef2SBarry Smith }
2833d763cef2SBarry Smith 
28341fb7b255SJunchao Zhang /*@C
2835d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2836d763cef2SBarry Smith    with TSCreate().
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith    Collective on TS
2839d763cef2SBarry Smith 
2840d763cef2SBarry Smith    Input Parameter:
2841d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2842d763cef2SBarry Smith 
2843d763cef2SBarry Smith    Level: beginner
2844d763cef2SBarry Smith 
2845d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2846d763cef2SBarry Smith @*/
28476bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2848d763cef2SBarry Smith {
28496849ba73SBarry Smith   PetscErrorCode ierr;
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith   PetscFunctionBegin;
28526bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2853ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2854c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2855d763cef2SBarry Smith 
2856ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2857ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2858ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2859ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2860ecf68647SHong Zhang   }
2861e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2862e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28636bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28646d4c513bSLisandro Dalcin 
2865bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2866bc952696SBarry Smith 
286784df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28686427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28696427ac75SLisandro Dalcin 
28706bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28716bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28726bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28730dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28746d4c513bSLisandro Dalcin 
2875cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2876a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2877d763cef2SBarry Smith   PetscFunctionReturn(0);
2878d763cef2SBarry Smith }
2879d763cef2SBarry Smith 
2880d8e5e3e6SSatish Balay /*@
2881d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2882d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2883d763cef2SBarry Smith 
2884d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2885d763cef2SBarry Smith 
2886d763cef2SBarry Smith    Input Parameter:
2887d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2888d763cef2SBarry Smith 
2889d763cef2SBarry Smith    Output Parameter:
2890d763cef2SBarry Smith .  snes - the nonlinear solver context
2891d763cef2SBarry Smith 
2892d763cef2SBarry Smith    Notes:
2893d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2894d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
289594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2896d763cef2SBarry Smith 
2897d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28980298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2899d763cef2SBarry Smith 
2900d763cef2SBarry Smith    Level: beginner
2901d763cef2SBarry Smith 
2902d763cef2SBarry Smith @*/
29037087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2904d763cef2SBarry Smith {
2905d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2906d372ba47SLisandro Dalcin 
2907d763cef2SBarry Smith   PetscFunctionBegin;
29080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29094482741eSBarry Smith   PetscValidPointer(snes,2);
2910d372ba47SLisandro Dalcin   if (!ts->snes) {
2911ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
291216413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
29130298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29143bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2915d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2916496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
29179e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
29189e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
29199e2a6581SJed Brown     }
2920d372ba47SLisandro Dalcin   }
2921d763cef2SBarry Smith   *snes = ts->snes;
2922d763cef2SBarry Smith   PetscFunctionReturn(0);
2923d763cef2SBarry Smith }
2924d763cef2SBarry Smith 
2925deb2cd25SJed Brown /*@
2926deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2927deb2cd25SJed Brown 
2928deb2cd25SJed Brown    Collective
2929deb2cd25SJed Brown 
2930d8d19677SJose E. Roman    Input Parameters:
2931deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2932deb2cd25SJed Brown -  snes - the nonlinear solver context
2933deb2cd25SJed Brown 
2934deb2cd25SJed Brown    Notes:
2935deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2936deb2cd25SJed Brown 
2937deb2cd25SJed Brown    Level: developer
2938deb2cd25SJed Brown 
2939deb2cd25SJed Brown @*/
2940deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2941deb2cd25SJed Brown {
2942deb2cd25SJed Brown   PetscErrorCode ierr;
2943d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2944deb2cd25SJed Brown 
2945deb2cd25SJed Brown   PetscFunctionBegin;
2946deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2947deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2948deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2949deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2950bbd56ea5SKarl Rupp 
2951deb2cd25SJed Brown   ts->snes = snes;
2952bbd56ea5SKarl Rupp 
29530298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29540298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2955740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29560298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2957740132f1SEmil Constantinescu   }
2958deb2cd25SJed Brown   PetscFunctionReturn(0);
2959deb2cd25SJed Brown }
2960deb2cd25SJed Brown 
2961d8e5e3e6SSatish Balay /*@
296294b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2963d763cef2SBarry Smith    a TS (timestepper) context.
2964d763cef2SBarry Smith 
296594b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2966d763cef2SBarry Smith 
2967d763cef2SBarry Smith    Input Parameter:
2968d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2969d763cef2SBarry Smith 
2970d763cef2SBarry Smith    Output Parameter:
297194b7f48cSBarry Smith .  ksp - the nonlinear solver context
2972d763cef2SBarry Smith 
2973d763cef2SBarry Smith    Notes:
297494b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2975d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2976d763cef2SBarry Smith    KSP and PC contexts as well.
2977d763cef2SBarry Smith 
297894b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29790298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2980d763cef2SBarry Smith 
2981d763cef2SBarry Smith    Level: beginner
2982d763cef2SBarry Smith 
2983d763cef2SBarry Smith @*/
29847087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2985d763cef2SBarry Smith {
2986d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2987089b2837SJed Brown   SNES           snes;
2988d372ba47SLisandro Dalcin 
2989d763cef2SBarry Smith   PetscFunctionBegin;
29900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29914482741eSBarry Smith   PetscValidPointer(ksp,2);
29922c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!((PetscObject)ts)->type_name,PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
29932c71b3e2SJacob Faibussowitsch   PetscCheckFalse(ts->problem_type != TS_LINEAR,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2994089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2995089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2996d763cef2SBarry Smith   PetscFunctionReturn(0);
2997d763cef2SBarry Smith }
2998d763cef2SBarry Smith 
2999d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
3000d763cef2SBarry Smith 
3001adb62b0dSMatthew Knepley /*@
3002618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
3003618ce8baSLisandro Dalcin 
3004618ce8baSLisandro Dalcin    Logically Collective on TS
3005618ce8baSLisandro Dalcin 
3006618ce8baSLisandro Dalcin    Input Parameters:
3007618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3008618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
3009618ce8baSLisandro Dalcin 
3010618ce8baSLisandro Dalcin    Options Database Keys:
3011618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
3012618ce8baSLisandro Dalcin 
3013618ce8baSLisandro Dalcin    Notes:
3014618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
3015618ce8baSLisandro Dalcin 
3016618ce8baSLisandro Dalcin    Level: intermediate
3017618ce8baSLisandro Dalcin 
3018618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
3019618ce8baSLisandro Dalcin @*/
3020618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
3021618ce8baSLisandro Dalcin {
3022618ce8baSLisandro Dalcin   PetscFunctionBegin;
3023618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3024618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
30252c71b3e2SJacob Faibussowitsch   PetscCheckFalse(maxsteps < 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
3026618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
3027618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3028618ce8baSLisandro Dalcin }
3029618ce8baSLisandro Dalcin 
3030618ce8baSLisandro Dalcin /*@
3031618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
3032618ce8baSLisandro Dalcin 
3033618ce8baSLisandro Dalcin    Not Collective
3034618ce8baSLisandro Dalcin 
3035618ce8baSLisandro Dalcin    Input Parameters:
3036618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3037618ce8baSLisandro Dalcin 
3038618ce8baSLisandro Dalcin    Output Parameter:
3039618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3040618ce8baSLisandro Dalcin 
3041618ce8baSLisandro Dalcin    Level: advanced
3042618ce8baSLisandro Dalcin 
3043618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3044618ce8baSLisandro Dalcin @*/
3045618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3046618ce8baSLisandro Dalcin {
3047618ce8baSLisandro Dalcin   PetscFunctionBegin;
3048618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3049618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3050618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3051618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3052618ce8baSLisandro Dalcin }
3053618ce8baSLisandro Dalcin 
3054618ce8baSLisandro Dalcin /*@
3055618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3056618ce8baSLisandro Dalcin 
3057618ce8baSLisandro Dalcin    Logically Collective on TS
3058618ce8baSLisandro Dalcin 
3059618ce8baSLisandro Dalcin    Input Parameters:
3060618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3061618ce8baSLisandro Dalcin -  maxtime - final time to step to
3062618ce8baSLisandro Dalcin 
3063618ce8baSLisandro Dalcin    Options Database Keys:
3064ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3065618ce8baSLisandro Dalcin 
3066618ce8baSLisandro Dalcin    Notes:
3067618ce8baSLisandro Dalcin    The default maximum time is 5.0
3068618ce8baSLisandro Dalcin 
3069618ce8baSLisandro Dalcin    Level: intermediate
3070618ce8baSLisandro Dalcin 
3071618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3072618ce8baSLisandro Dalcin @*/
3073618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3074618ce8baSLisandro Dalcin {
3075618ce8baSLisandro Dalcin   PetscFunctionBegin;
3076618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3077618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3078618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3079618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3080618ce8baSLisandro Dalcin }
3081618ce8baSLisandro Dalcin 
3082618ce8baSLisandro Dalcin /*@
3083618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3084618ce8baSLisandro Dalcin 
3085618ce8baSLisandro Dalcin    Not Collective
3086618ce8baSLisandro Dalcin 
3087618ce8baSLisandro Dalcin    Input Parameters:
3088618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3089618ce8baSLisandro Dalcin 
3090618ce8baSLisandro Dalcin    Output Parameter:
3091618ce8baSLisandro Dalcin .  maxtime - final time to step to
3092618ce8baSLisandro Dalcin 
3093618ce8baSLisandro Dalcin    Level: advanced
3094618ce8baSLisandro Dalcin 
3095618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3096618ce8baSLisandro Dalcin @*/
3097618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3098618ce8baSLisandro Dalcin {
3099618ce8baSLisandro Dalcin   PetscFunctionBegin;
3100618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3101618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3102618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3103618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3104618ce8baSLisandro Dalcin }
3105618ce8baSLisandro Dalcin 
3106618ce8baSLisandro Dalcin /*@
3107aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3108edc382c3SSatish Balay 
3109edc382c3SSatish Balay    Level: deprecated
3110edc382c3SSatish Balay 
3111aaa6c58dSLisandro Dalcin @*/
3112aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3113aaa6c58dSLisandro Dalcin {
3114aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3115aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3116aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3117aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3118aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3119aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3120aaa6c58dSLisandro Dalcin }
3121aaa6c58dSLisandro Dalcin 
3122aaa6c58dSLisandro Dalcin /*@
312319eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3124edc382c3SSatish Balay 
3125edc382c3SSatish Balay    Level: deprecated
3126edc382c3SSatish Balay 
3127adb62b0dSMatthew Knepley @*/
31287087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3129adb62b0dSMatthew Knepley {
3130adb62b0dSMatthew Knepley   PetscFunctionBegin;
31310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3132abc0a331SBarry Smith   if (maxsteps) {
31334482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3134adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3135adb62b0dSMatthew Knepley   }
3136abc0a331SBarry Smith   if (maxtime) {
3137064a246eSJacob Faibussowitsch     PetscValidRealPointer(maxtime,3);
3138adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3139adb62b0dSMatthew Knepley   }
3140adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3141adb62b0dSMatthew Knepley }
3142adb62b0dSMatthew Knepley 
3143d763cef2SBarry Smith /*@
314419eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3145edc382c3SSatish Balay 
3146edc382c3SSatish Balay    Level: deprecated
3147edc382c3SSatish Balay 
3148d763cef2SBarry Smith @*/
31497087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3150d763cef2SBarry Smith {
3151d763cef2SBarry Smith   PetscFunctionBegin;
31520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3153c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3154064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveReal(ts,maxtime,3);
315539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
315639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3157d763cef2SBarry Smith   PetscFunctionReturn(0);
3158d763cef2SBarry Smith }
3159d763cef2SBarry Smith 
3160d763cef2SBarry Smith /*@
31615c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3162edc382c3SSatish Balay 
3163edc382c3SSatish Balay    Level: deprecated
3164edc382c3SSatish Balay 
31655c5f5948SLisandro Dalcin @*/
3166e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31675c5f5948SLisandro Dalcin 
316819eac22cSLisandro Dalcin /*@
31694f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3170edc382c3SSatish Balay 
3171edc382c3SSatish Balay    Level: deprecated
3172edc382c3SSatish Balay 
31734f4e0956SLisandro Dalcin @*/
31744f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31754f4e0956SLisandro Dalcin 
31764f4e0956SLisandro Dalcin /*@
3177d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3178d763cef2SBarry Smith    for use by the TS routines.
3179d763cef2SBarry Smith 
3180d083f849SBarry Smith    Logically Collective on TS
3181d763cef2SBarry Smith 
3182d763cef2SBarry Smith    Input Parameters:
3183d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31840910c330SBarry Smith -  u - the solution vector
3185d763cef2SBarry Smith 
3186d763cef2SBarry Smith    Level: beginner
3187d763cef2SBarry Smith 
31880ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3189d763cef2SBarry Smith @*/
31900910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3191d763cef2SBarry Smith {
31928737fe31SLisandro Dalcin   PetscErrorCode ierr;
3193496e6a7aSJed Brown   DM             dm;
31948737fe31SLisandro Dalcin 
3195d763cef2SBarry Smith   PetscFunctionBegin;
31960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31970910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31980910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31996bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
32000910c330SBarry Smith   ts->vec_sol = u;
3201bbd56ea5SKarl Rupp 
3202496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
32030910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3204d763cef2SBarry Smith   PetscFunctionReturn(0);
3205d763cef2SBarry Smith }
3206d763cef2SBarry Smith 
3207ac226902SBarry Smith /*@C
3208000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32093f2090d5SJed Brown   called once at the beginning of each time step.
3210000e7ae3SMatthew Knepley 
32113f9fe445SBarry Smith   Logically Collective on TS
3212000e7ae3SMatthew Knepley 
3213000e7ae3SMatthew Knepley   Input Parameters:
3214000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3215000e7ae3SMatthew Knepley - func - The function
3216000e7ae3SMatthew Knepley 
3217000e7ae3SMatthew Knepley   Calling sequence of func:
3218*67b8a455SSatish Balay .vb
3219*67b8a455SSatish Balay   PetscErrorCode func (TS ts);
3220*67b8a455SSatish Balay .ve
3221000e7ae3SMatthew Knepley 
3222000e7ae3SMatthew Knepley   Level: intermediate
3223000e7ae3SMatthew Knepley 
3224dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3225000e7ae3SMatthew Knepley @*/
32267087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3227000e7ae3SMatthew Knepley {
3228000e7ae3SMatthew Knepley   PetscFunctionBegin;
32290700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3230ae60f76fSBarry Smith   ts->prestep = func;
3231000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3232000e7ae3SMatthew Knepley }
3233000e7ae3SMatthew Knepley 
323409ee8438SJed Brown /*@
32353f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32363f2090d5SJed Brown 
32373f2090d5SJed Brown   Collective on TS
32383f2090d5SJed Brown 
32393f2090d5SJed Brown   Input Parameters:
32403f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32413f2090d5SJed Brown 
32423f2090d5SJed Brown   Notes:
32433f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32443f2090d5SJed Brown   so most users would not generally call this routine themselves.
32453f2090d5SJed Brown 
32463f2090d5SJed Brown   Level: developer
32473f2090d5SJed Brown 
32489be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32493f2090d5SJed Brown @*/
32507087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32513f2090d5SJed Brown {
32523f2090d5SJed Brown   PetscErrorCode ierr;
32533f2090d5SJed Brown 
32543f2090d5SJed Brown   PetscFunctionBegin;
32550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3256ae60f76fSBarry Smith   if (ts->prestep) {
32575efd42a4SStefano Zampini     Vec              U;
3258ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3259ef8d1ce0SJohann Rudi     PetscBool        sameObject;
32605efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32615efd42a4SStefano Zampini 
32625efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3263ef8d1ce0SJohann Rudi     ierr = PetscObjectGetId((PetscObject)U,&idprev);CHKERRQ(ierr);
32645efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3265a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->prestep),ts);
3266ef8d1ce0SJohann Rudi     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3267ef8d1ce0SJohann Rudi     ierr = PetscObjectCompareId((PetscObject)U,idprev,&sameObject);CHKERRQ(ierr);
32685efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3269ef8d1ce0SJohann Rudi     if (!sameObject || sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3270312ce896SJed Brown   }
32713f2090d5SJed Brown   PetscFunctionReturn(0);
32723f2090d5SJed Brown }
32733f2090d5SJed Brown 
3274b8123daeSJed Brown /*@C
3275b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3276b8123daeSJed Brown   called once at the beginning of each stage.
3277b8123daeSJed Brown 
3278b8123daeSJed Brown   Logically Collective on TS
3279b8123daeSJed Brown 
3280b8123daeSJed Brown   Input Parameters:
3281b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3282b8123daeSJed Brown - func - The function
3283b8123daeSJed Brown 
3284b8123daeSJed Brown   Calling sequence of func:
3285*67b8a455SSatish Balay .vb
3286*67b8a455SSatish Balay   PetscErrorCode func(TS ts, PetscReal stagetime);
3287*67b8a455SSatish Balay .ve
3288b8123daeSJed Brown 
3289b8123daeSJed Brown   Level: intermediate
3290b8123daeSJed Brown 
3291b8123daeSJed Brown   Note:
3292b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
329380275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3294b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3295b8123daeSJed Brown 
32969be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3297b8123daeSJed Brown @*/
3298b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3299b8123daeSJed Brown {
3300b8123daeSJed Brown   PetscFunctionBegin;
3301b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3302ae60f76fSBarry Smith   ts->prestage = func;
3303b8123daeSJed Brown   PetscFunctionReturn(0);
3304b8123daeSJed Brown }
3305b8123daeSJed Brown 
33069be3e283SDebojyoti Ghosh /*@C
33079be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33089be3e283SDebojyoti Ghosh   called once at the end of each stage.
33099be3e283SDebojyoti Ghosh 
33109be3e283SDebojyoti Ghosh   Logically Collective on TS
33119be3e283SDebojyoti Ghosh 
33129be3e283SDebojyoti Ghosh   Input Parameters:
33139be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33149be3e283SDebojyoti Ghosh - func - The function
33159be3e283SDebojyoti Ghosh 
33169be3e283SDebojyoti Ghosh   Calling sequence of func:
3317*67b8a455SSatish Balay .vb
3318*67b8a455SSatish Balay   PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
3319*67b8a455SSatish Balay .ve
33209be3e283SDebojyoti Ghosh 
33219be3e283SDebojyoti Ghosh   Level: intermediate
33229be3e283SDebojyoti Ghosh 
33239be3e283SDebojyoti Ghosh   Note:
33249be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
332580275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
33269be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33279be3e283SDebojyoti Ghosh 
33289be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33299be3e283SDebojyoti Ghosh @*/
33309be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33319be3e283SDebojyoti Ghosh {
33329be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33339be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33349be3e283SDebojyoti Ghosh   ts->poststage = func;
33359be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33369be3e283SDebojyoti Ghosh }
33379be3e283SDebojyoti Ghosh 
3338c688d042SShri Abhyankar /*@C
3339c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3340c688d042SShri Abhyankar   called once at the end of each step evaluation.
3341c688d042SShri Abhyankar 
3342c688d042SShri Abhyankar   Logically Collective on TS
3343c688d042SShri Abhyankar 
3344c688d042SShri Abhyankar   Input Parameters:
3345c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3346c688d042SShri Abhyankar - func - The function
3347c688d042SShri Abhyankar 
3348c688d042SShri Abhyankar   Calling sequence of func:
3349*67b8a455SSatish Balay .vb
3350*67b8a455SSatish Balay   PetscErrorCode func(TS ts);
3351*67b8a455SSatish Balay .ve
3352c688d042SShri Abhyankar 
3353c688d042SShri Abhyankar   Level: intermediate
3354c688d042SShri Abhyankar 
3355c688d042SShri Abhyankar   Note:
33561785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33571785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3358e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3359e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3360e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3361c688d042SShri Abhyankar 
3362c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3363c688d042SShri Abhyankar @*/
3364c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3365c688d042SShri Abhyankar {
3366c688d042SShri Abhyankar   PetscFunctionBegin;
3367c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3368c688d042SShri Abhyankar   ts->postevaluate = func;
3369c688d042SShri Abhyankar   PetscFunctionReturn(0);
3370c688d042SShri Abhyankar }
3371c688d042SShri Abhyankar 
3372b8123daeSJed Brown /*@
3373b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3374b8123daeSJed Brown 
3375b8123daeSJed Brown   Collective on TS
3376b8123daeSJed Brown 
3377b8123daeSJed Brown   Input Parameters:
3378b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33799be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3380b8123daeSJed Brown 
3381b8123daeSJed Brown   Notes:
3382b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3383b8123daeSJed Brown   most users would not generally call this routine themselves.
3384b8123daeSJed Brown 
3385b8123daeSJed Brown   Level: developer
3386b8123daeSJed Brown 
33879be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3388b8123daeSJed Brown @*/
3389b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3390b8123daeSJed Brown {
3391b8123daeSJed Brown   PetscFunctionBegin;
3392b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3393ae60f76fSBarry Smith   if (ts->prestage) {
3394a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->prestage),ts,stagetime);
3395b8123daeSJed Brown   }
3396b8123daeSJed Brown   PetscFunctionReturn(0);
3397b8123daeSJed Brown }
3398b8123daeSJed Brown 
33999be3e283SDebojyoti Ghosh /*@
34009be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34019be3e283SDebojyoti Ghosh 
34029be3e283SDebojyoti Ghosh   Collective on TS
34039be3e283SDebojyoti Ghosh 
34049be3e283SDebojyoti Ghosh   Input Parameters:
34059be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34069be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34079be3e283SDebojyoti Ghosh   stageindex  - Stage number
34089be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34099be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34109be3e283SDebojyoti Ghosh 
34119be3e283SDebojyoti Ghosh   Notes:
34129be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34139be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34149be3e283SDebojyoti Ghosh 
34159be3e283SDebojyoti Ghosh   Level: developer
34169be3e283SDebojyoti Ghosh 
34179be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34189be3e283SDebojyoti Ghosh @*/
34199be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34209be3e283SDebojyoti Ghosh {
34219be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34229be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34234beae5d8SLisandro Dalcin   if (ts->poststage) {
3424a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->poststage),ts,stagetime,stageindex,Y);
34259be3e283SDebojyoti Ghosh   }
34269be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34279be3e283SDebojyoti Ghosh }
34289be3e283SDebojyoti Ghosh 
3429c688d042SShri Abhyankar /*@
3430c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3431c688d042SShri Abhyankar 
3432c688d042SShri Abhyankar   Collective on TS
3433c688d042SShri Abhyankar 
3434c688d042SShri Abhyankar   Input Parameters:
3435c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3436c688d042SShri Abhyankar 
3437c688d042SShri Abhyankar   Notes:
3438c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3439c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3440c688d042SShri Abhyankar 
3441c688d042SShri Abhyankar   Level: developer
3442c688d042SShri Abhyankar 
3443c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3444c688d042SShri Abhyankar @*/
3445c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3446c688d042SShri Abhyankar {
3447c688d042SShri Abhyankar   PetscErrorCode ierr;
3448c688d042SShri Abhyankar 
3449c688d042SShri Abhyankar   PetscFunctionBegin;
3450c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3451c688d042SShri Abhyankar   if (ts->postevaluate) {
3452dcb233daSLisandro Dalcin     Vec              U;
3453dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3454dcb233daSLisandro Dalcin 
3455dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3456dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3457a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->postevaluate),ts);
3458dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3459dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3460c688d042SShri Abhyankar   }
3461c688d042SShri Abhyankar   PetscFunctionReturn(0);
3462c688d042SShri Abhyankar }
3463c688d042SShri Abhyankar 
3464ac226902SBarry Smith /*@C
3465000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34663f2090d5SJed Brown   called once at the end of each time step.
3467000e7ae3SMatthew Knepley 
34683f9fe445SBarry Smith   Logically Collective on TS
3469000e7ae3SMatthew Knepley 
3470000e7ae3SMatthew Knepley   Input Parameters:
3471000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3472000e7ae3SMatthew Knepley - func - The function
3473000e7ae3SMatthew Knepley 
3474000e7ae3SMatthew Knepley   Calling sequence of func:
3475b8123daeSJed Brown $ func (TS ts);
3476000e7ae3SMatthew Knepley 
34771785ff2aSShri Abhyankar   Notes:
34781785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34791785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34801785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34811785ff2aSShri Abhyankar 
3482000e7ae3SMatthew Knepley   Level: intermediate
3483000e7ae3SMatthew Knepley 
3484dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3485000e7ae3SMatthew Knepley @*/
34867087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3487000e7ae3SMatthew Knepley {
3488000e7ae3SMatthew Knepley   PetscFunctionBegin;
34890700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3490ae60f76fSBarry Smith   ts->poststep = func;
3491000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3492000e7ae3SMatthew Knepley }
3493000e7ae3SMatthew Knepley 
349409ee8438SJed Brown /*@
34953f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34963f2090d5SJed Brown 
34973f2090d5SJed Brown   Collective on TS
34983f2090d5SJed Brown 
34993f2090d5SJed Brown   Input Parameters:
35003f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35013f2090d5SJed Brown 
35023f2090d5SJed Brown   Notes:
35033f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35043f2090d5SJed Brown   so most users would not generally call this routine themselves.
35053f2090d5SJed Brown 
35063f2090d5SJed Brown   Level: developer
35073f2090d5SJed Brown 
35083f2090d5SJed Brown @*/
35097087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35103f2090d5SJed Brown {
35113f2090d5SJed Brown   PetscErrorCode ierr;
35123f2090d5SJed Brown 
35133f2090d5SJed Brown   PetscFunctionBegin;
35140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3515ae60f76fSBarry Smith   if (ts->poststep) {
35165efd42a4SStefano Zampini     Vec              U;
3517ef8d1ce0SJohann Rudi     PetscObjectId    idprev;
3518ef8d1ce0SJohann Rudi     PetscBool        sameObject;
35195efd42a4SStefano Zampini     PetscObjectState sprev,spost;
35205efd42a4SStefano Zampini 
35215efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3522ef8d1ce0SJohann Rudi     ierr = PetscObjectGetId((PetscObject)U,&idprev);CHKERRQ(ierr);
35235efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3524a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->poststep),ts);
3525ef8d1ce0SJohann Rudi     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3526ef8d1ce0SJohann Rudi     ierr = PetscObjectCompareId((PetscObject)U,idprev,&sameObject);CHKERRQ(ierr);
35275efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3528ef8d1ce0SJohann Rudi     if (!sameObject || sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
352972ac3e02SJed Brown   }
35303f2090d5SJed Brown   PetscFunctionReturn(0);
35313f2090d5SJed Brown }
35323f2090d5SJed Brown 
3533cd652676SJed Brown /*@
3534cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3535cd652676SJed Brown 
3536cd652676SJed Brown    Collective on TS
3537cd652676SJed Brown 
35384165533cSJose E. Roman    Input Parameters:
3539cd652676SJed Brown +  ts - time stepping context
3540cd652676SJed Brown -  t - time to interpolate to
3541cd652676SJed Brown 
35424165533cSJose E. Roman    Output Parameter:
35430910c330SBarry Smith .  U - state at given time
3544cd652676SJed Brown 
3545cd652676SJed Brown    Level: intermediate
3546cd652676SJed Brown 
3547cd652676SJed Brown    Developer Notes:
3548cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3549cd652676SJed Brown 
3550874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3551cd652676SJed Brown @*/
35520910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3553cd652676SJed Brown {
3554cd652676SJed Brown   PetscErrorCode ierr;
3555cd652676SJed Brown 
3556cd652676SJed Brown   PetscFunctionBegin;
3557cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3558b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
35592c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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);
35602c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!ts->ops->interpolate,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35610910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3562cd652676SJed Brown   PetscFunctionReturn(0);
3563cd652676SJed Brown }
3564cd652676SJed Brown 
3565d763cef2SBarry Smith /*@
35666d9e5789SSean Farley    TSStep - Steps one time step
3567d763cef2SBarry Smith 
3568d763cef2SBarry Smith    Collective on TS
3569d763cef2SBarry Smith 
3570d763cef2SBarry Smith    Input Parameter:
3571d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3572d763cef2SBarry Smith 
357327829d71SBarry Smith    Level: developer
3574d763cef2SBarry Smith 
3575b8123daeSJed Brown    Notes:
357627829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
357727829d71SBarry Smith 
3578b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3579b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3580b8123daeSJed Brown 
358119eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
358219eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
358325cb2221SBarry Smith 
35849be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3585d763cef2SBarry Smith @*/
3586193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3587d763cef2SBarry Smith {
3588dfbe8321SBarry Smith   PetscErrorCode   ierr;
3589fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3590be5899b3SLisandro Dalcin   PetscReal        ptime;
3591d763cef2SBarry Smith 
3592d763cef2SBarry Smith   PetscFunctionBegin;
35930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3594f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3595fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3596f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3597f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3598f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3599f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3600f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3601fffbeea8SBarry Smith 
3602d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
360368bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3604d405a339SMatthew Knepley 
36052c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!ts->ops->step,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
36062c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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>");
36072c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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()");
36082c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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");
3609a6772fa2SLisandro Dalcin 
3610be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3611be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
36122808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3613fc8dbba5SLisandro Dalcin 
3614d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3615193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3616d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3617fc8dbba5SLisandro Dalcin 
3618fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3619be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
36202808aa04SLisandro Dalcin     ts->steps++;
3621be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
362228d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3623d2daff3dSHong Zhang   }
3624fc8dbba5SLisandro Dalcin 
3625fc8dbba5SLisandro Dalcin   if (!ts->reason) {
362608c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
362708c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
362808c7845fSBarry Smith   }
3629fc8dbba5SLisandro Dalcin 
36302c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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]);
36312c71b3e2SJacob Faibussowitsch   PetscCheckFalse(ts->reason < 0 && ts->errorifstepfailed,PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
363208c7845fSBarry Smith   PetscFunctionReturn(0);
363308c7845fSBarry Smith }
363408c7845fSBarry Smith 
363508c7845fSBarry Smith /*@
36367cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36377cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36387cbde773SLisandro Dalcin 
36397cbde773SLisandro Dalcin    Collective on TS
36407cbde773SLisandro Dalcin 
36414165533cSJose E. Roman    Input Parameters:
36427cbde773SLisandro Dalcin +  ts - time stepping context
364397bb3fdcSJose E. Roman -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36447cbde773SLisandro Dalcin 
364597bb3fdcSJose E. Roman    Input/Output Parameter:
364697bb3fdcSJose E. Roman .  order - optional, desired order for the error evaluation or PETSC_DECIDE;
364797bb3fdcSJose E. Roman            on output, the actual order of the error evaluation
364897bb3fdcSJose E. Roman 
364997bb3fdcSJose E. Roman    Output Parameter:
365097bb3fdcSJose E. Roman .  wlte - the weighted local truncation error norm
36517cbde773SLisandro Dalcin 
36527cbde773SLisandro Dalcin    Level: advanced
36537cbde773SLisandro Dalcin 
36547cbde773SLisandro Dalcin    Notes:
36557cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36567cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36577cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36587cbde773SLisandro Dalcin 
36597cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36607cbde773SLisandro Dalcin @*/
36617cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36627cbde773SLisandro Dalcin {
36637cbde773SLisandro Dalcin   PetscErrorCode ierr;
36647cbde773SLisandro Dalcin 
36657cbde773SLisandro Dalcin   PetscFunctionBegin;
36667cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36677cbde773SLisandro Dalcin   PetscValidType(ts,1);
3668064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveEnum(ts,wnormtype,2);
36697cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36707cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36717cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36722c71b3e2SJacob Faibussowitsch   PetscCheckFalse(wnormtype != NORM_2 && wnormtype != NORM_INFINITY,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36732c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!ts->ops->evaluatewlte,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36747cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
36757cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36767cbde773SLisandro Dalcin }
36777cbde773SLisandro Dalcin 
367805175c85SJed Brown /*@
367905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
368005175c85SJed Brown 
36811c3436cfSJed Brown    Collective on TS
368205175c85SJed Brown 
36834165533cSJose E. Roman    Input Parameters:
36841c3436cfSJed Brown +  ts - time stepping context
36851c3436cfSJed Brown .  order - desired order of accuracy
36860298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
368705175c85SJed Brown 
36884165533cSJose E. Roman    Output Parameter:
36890910c330SBarry Smith .  U - state at the end of the current step
369005175c85SJed Brown 
369105175c85SJed Brown    Level: advanced
369205175c85SJed Brown 
3693108c343cSJed Brown    Notes:
3694108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3695108c343cSJed 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.
3696108c343cSJed Brown 
36971c3436cfSJed Brown .seealso: TSStep(), TSAdapt
369805175c85SJed Brown @*/
36990910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
370005175c85SJed Brown {
370105175c85SJed Brown   PetscErrorCode ierr;
370205175c85SJed Brown 
370305175c85SJed Brown   PetscFunctionBegin;
370405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
370505175c85SJed Brown   PetscValidType(ts,1);
37060910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
37072c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!ts->ops->evaluatestep,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
37080910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
370905175c85SJed Brown   PetscFunctionReturn(0);
371005175c85SJed Brown }
371105175c85SJed Brown 
3712aad739acSMatthew G. Knepley /*@C
37132e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3714aad739acSMatthew G. Knepley 
3715aad739acSMatthew G. Knepley   Not collective
3716aad739acSMatthew G. Knepley 
37174165533cSJose E. Roman   Input Parameter:
3718aad739acSMatthew G. Knepley . ts        - time stepping context
3719aad739acSMatthew G. Knepley 
37204165533cSJose E. Roman   Output Parameter:
37212e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3722aad739acSMatthew G. Knepley 
3723aad739acSMatthew G. Knepley    Level: advanced
3724aad739acSMatthew G. Knepley 
3725aad739acSMatthew G. Knepley    Notes:
3726aad739acSMatthew G. Knepley    The calling sequence for the function is
37272e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3728aad739acSMatthew G. Knepley $ ts - The timestepping context
37292e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3730aad739acSMatthew G. Knepley 
37312e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3732aad739acSMatthew G. Knepley @*/
37332e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3734aad739acSMatthew G. Knepley {
3735aad739acSMatthew G. Knepley   PetscFunctionBegin;
3736aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
37372e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
37382e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3739aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3740aad739acSMatthew G. Knepley }
3741aad739acSMatthew G. Knepley 
3742aad739acSMatthew G. Knepley /*@C
37432e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3744aad739acSMatthew G. Knepley 
3745aad739acSMatthew G. Knepley   Logically collective on ts
3746aad739acSMatthew G. Knepley 
37474165533cSJose E. Roman   Input Parameters:
3748aad739acSMatthew G. Knepley + ts        - time stepping context
37492e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3750aad739acSMatthew G. Knepley 
3751aad739acSMatthew G. Knepley   Level: advanced
3752aad739acSMatthew G. Knepley 
3753a96d6ef6SBarry Smith   Calling sequence for initCondition:
3754a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3755a96d6ef6SBarry Smith 
3756a96d6ef6SBarry Smith + ts - The timestepping context
3757a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3758aad739acSMatthew G. Knepley 
37592e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3760aad739acSMatthew G. Knepley @*/
37612e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3762aad739acSMatthew G. Knepley {
3763aad739acSMatthew G. Knepley   PetscFunctionBegin;
3764aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
37652e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
37662e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3767aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3768aad739acSMatthew G. Knepley }
3769aad739acSMatthew G. Knepley 
3770aad739acSMatthew G. Knepley /*@
37712e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3772aad739acSMatthew G. Knepley 
3773aad739acSMatthew G. Knepley   Collective on ts
3774aad739acSMatthew G. Knepley 
37754165533cSJose E. Roman   Input Parameters:
3776aad739acSMatthew G. Knepley + ts - time stepping context
37772e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3778aad739acSMatthew G. Knepley 
3779aad739acSMatthew G. Knepley   Level: advanced
3780aad739acSMatthew G. Knepley 
37812e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3782aad739acSMatthew G. Knepley @*/
37832e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3784aad739acSMatthew G. Knepley {
3785aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3786aad739acSMatthew G. Knepley 
3787aad739acSMatthew G. Knepley   PetscFunctionBegin;
3788aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3789aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
37902e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3791aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3792aad739acSMatthew G. Knepley }
3793aad739acSMatthew G. Knepley 
3794aad739acSMatthew G. Knepley /*@C
3795aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3796aad739acSMatthew G. Knepley 
3797aad739acSMatthew G. Knepley   Not collective
3798aad739acSMatthew G. Knepley 
37994165533cSJose E. Roman   Input Parameter:
3800aad739acSMatthew G. Knepley . ts         - time stepping context
3801aad739acSMatthew G. Knepley 
38024165533cSJose E. Roman   Output Parameter:
3803aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3804aad739acSMatthew G. Knepley 
3805aad739acSMatthew G. Knepley   Level: advanced
3806aad739acSMatthew G. Knepley 
3807a96d6ef6SBarry Smith   Calling sequence for exactError:
3808a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3809a96d6ef6SBarry Smith 
3810a96d6ef6SBarry Smith + ts - The timestepping context
3811a96d6ef6SBarry Smith . u  - The approximate solution vector
3812a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3813aad739acSMatthew G. Knepley 
3814aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3815aad739acSMatthew G. Knepley @*/
3816aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3817aad739acSMatthew G. Knepley {
3818aad739acSMatthew G. Knepley   PetscFunctionBegin;
3819aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3820aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3821aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3822aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3823aad739acSMatthew G. Knepley }
3824aad739acSMatthew G. Knepley 
3825aad739acSMatthew G. Knepley /*@C
3826aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3827aad739acSMatthew G. Knepley 
3828aad739acSMatthew G. Knepley   Logically collective on ts
3829aad739acSMatthew G. Knepley 
38304165533cSJose E. Roman   Input Parameters:
3831aad739acSMatthew G. Knepley + ts         - time stepping context
3832aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3833aad739acSMatthew G. Knepley 
3834aad739acSMatthew G. Knepley   Level: advanced
3835aad739acSMatthew G. Knepley 
3836a96d6ef6SBarry Smith   Calling sequence for exactError:
3837a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3838a96d6ef6SBarry Smith 
3839a96d6ef6SBarry Smith + ts - The timestepping context
3840a96d6ef6SBarry Smith . u  - The approximate solution vector
3841a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3842aad739acSMatthew G. Knepley 
3843aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3844aad739acSMatthew G. Knepley @*/
3845aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3846aad739acSMatthew G. Knepley {
3847aad739acSMatthew G. Knepley   PetscFunctionBegin;
3848aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3849f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3850aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3851aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3852aad739acSMatthew G. Knepley }
3853aad739acSMatthew G. Knepley 
3854aad739acSMatthew G. Knepley /*@
3855aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3856aad739acSMatthew G. Knepley 
3857aad739acSMatthew G. Knepley   Collective on ts
3858aad739acSMatthew G. Knepley 
38594165533cSJose E. Roman   Input Parameters:
3860aad739acSMatthew G. Knepley + ts - time stepping context
3861aad739acSMatthew G. Knepley . u  - The approximate solution
3862aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3863aad739acSMatthew G. Knepley 
3864aad739acSMatthew G. Knepley   Level: advanced
3865aad739acSMatthew G. Knepley 
38662e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3867aad739acSMatthew G. Knepley @*/
3868aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3869aad739acSMatthew G. Knepley {
3870aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3871aad739acSMatthew G. Knepley 
3872aad739acSMatthew G. Knepley   PetscFunctionBegin;
3873aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3874aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3875aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
3876aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
3877aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3878aad739acSMatthew G. Knepley }
3879aad739acSMatthew G. Knepley 
3880b1cb36f3SHong Zhang /*@
38816a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
38826a4d4014SLisandro Dalcin 
38836a4d4014SLisandro Dalcin    Collective on TS
38846a4d4014SLisandro Dalcin 
3885d8d19677SJose E. Roman    Input Parameters:
38866a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
388763e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
388863e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
38895a3a76d0SJed Brown 
38906a4d4014SLisandro Dalcin    Level: beginner
38916a4d4014SLisandro Dalcin 
38925a3a76d0SJed Brown    Notes:
38935a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
38945a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
38955a3a76d0SJed Brown    stepped over the final time.
38965a3a76d0SJed Brown 
389763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
38986a4d4014SLisandro Dalcin @*/
3899cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
39006a4d4014SLisandro Dalcin {
3901b06615a5SLisandro Dalcin   Vec               solution;
39026a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3903f22f69f0SBarry Smith 
39046a4d4014SLisandro Dalcin   PetscFunctionBegin;
39050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3906f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3907303a5415SBarry Smith 
3908303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
3909ee41a567SStefano 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 */
39100910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3911b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3912b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3913b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
39145a3a76d0SJed Brown     }
39150910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
39162c71b3e2SJacob Faibussowitsch     PetscCheckFalse(ts->forward_solve,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3917bbd56ea5SKarl Rupp   } else if (u) {
39180910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
39195a3a76d0SJed Brown   }
3920b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
392168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3922a6772fa2SLisandro Dalcin 
39232c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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>");
39242c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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()");
39252c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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");
3926a6772fa2SLisandro Dalcin 
3927715f1b00SHong Zhang   if (ts->forward_solve) {
3928715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3929715f1b00SHong Zhang   }
3930715f1b00SHong Zhang 
3931e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3932715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3933e7069c78SShri      TSTrajectory to incorrectly save the output files
3934e7069c78SShri   */
3935715f1b00SHong Zhang   /* reset time step and iteration counters */
39362808aa04SLisandro Dalcin   if (!ts->steps) {
39375ef26d82SJed Brown     ts->ksp_its           = 0;
39385ef26d82SJed Brown     ts->snes_its          = 0;
3939c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3940c610991cSLisandro Dalcin     ts->reject            = 0;
39412808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
39422808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
39437d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
39442808aa04SLisandro Dalcin   }
3945e97c63d7SStefano Zampini 
3946e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
3947e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
3948e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
3949e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
3950e97c63d7SStefano Zampini 
3951e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
3952e97c63d7SStefano Zampini   }
3953193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3954193ac0bcSJed Brown 
3955900f6b5bSMatthew G. Knepley   {
3956900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
3957900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
3958900f6b5bSMatthew G. Knepley     PetscBool         flg;
3959900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
3960900f6b5bSMatthew G. Knepley 
3961900f6b5bSMatthew G. Knepley     if (!incall) {
3962900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
3963900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
3964900f6b5bSMatthew G. Knepley       if (flg) {
3965900f6b5bSMatthew G. Knepley         PetscConvEst conv;
3966900f6b5bSMatthew G. Knepley         DM           dm;
3967900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
3968900f6b5bSMatthew G. Knepley         PetscInt     Nf;
3969f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
3970900f6b5bSMatthew G. Knepley 
3971900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
3972f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
3973900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3974900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3975900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
3976900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
3977f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
3978900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
3979900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
3980900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
3981900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
3982900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
3983900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
3984900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3985900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3986900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
3987900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
3988900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
3989900f6b5bSMatthew G. Knepley       }
3990900f6b5bSMatthew G. Knepley     }
3991900f6b5bSMatthew G. Knepley   }
3992900f6b5bSMatthew G. Knepley 
3993ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3994f05ece33SBarry Smith 
3995193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3996193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3997a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3998cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3999a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4000be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4001db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4002db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4003e7069c78SShri 
40042808aa04SLisandro Dalcin     if (!ts->steps) {
40052808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40066427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40072808aa04SLisandro Dalcin     }
40086427ac75SLisandro Dalcin 
4009e1a7a14fSJed Brown     while (!ts->reason) {
40102808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40119687d888SLisandro Dalcin       if (!ts->steprollback) {
40129687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
40139687d888SLisandro Dalcin       }
4014193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4015f3b1f45cSBarry Smith       if (ts->testjacobian) {
4016f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4017f3b1f45cSBarry Smith       }
4018f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4019f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4020f3b1f45cSBarry Smith       }
4021cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
40227b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4023b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
40247b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4025b1cb36f3SHong Zhang       }
402658818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
40277b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4028715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
40297b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4030715f1b00SHong Zhang       }
403110b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4032e783b05fSHong Zhang       ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
403358818c2dSLisandro Dalcin       if (ts->steprollback) {
403458818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
403558818c2dSLisandro Dalcin       }
4036e783b05fSHong Zhang       if (!ts->steprollback) {
40372808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40381eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4039aeb4809dSShri Abhyankar       }
4040193ac0bcSJed Brown     }
40412808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
40426427ac75SLisandro Dalcin 
404349354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
40440910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4045cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4046b06615a5SLisandro Dalcin       solution = u;
404763e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
40480574a7fbSJed Brown     } else {
4049ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4050cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4051b06615a5SLisandro Dalcin       solution = ts->vec_sol;
40520574a7fbSJed Brown     }
4053193ac0bcSJed Brown   }
4054d2daff3dSHong Zhang 
4055ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
4056aee7a9fbSMatthew G. Knepley   ierr = VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
405756f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4058ef222394SBarry Smith   if (ts->adjoint_solve) {
4059ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
40602b0a91c0SBarry Smith   }
40616a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40626a4d4014SLisandro Dalcin }
40636a4d4014SLisandro Dalcin 
4064d763cef2SBarry Smith /*@
4065b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4066d763cef2SBarry Smith 
4067d763cef2SBarry Smith    Not Collective
4068d763cef2SBarry Smith 
4069d763cef2SBarry Smith    Input Parameter:
4070d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4071d763cef2SBarry Smith 
4072d763cef2SBarry Smith    Output Parameter:
407319eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4074d763cef2SBarry Smith 
4075d763cef2SBarry Smith    Level: beginner
4076d763cef2SBarry Smith 
4077b8123daeSJed Brown    Note:
4078b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40799be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4080b8123daeSJed Brown 
40818f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4082d763cef2SBarry Smith 
4083d763cef2SBarry Smith @*/
40847087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4085d763cef2SBarry Smith {
4086d763cef2SBarry Smith   PetscFunctionBegin;
40870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4088f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4089d763cef2SBarry Smith   *t = ts->ptime;
4090d763cef2SBarry Smith   PetscFunctionReturn(0);
4091d763cef2SBarry Smith }
4092d763cef2SBarry Smith 
4093e5e524a1SHong Zhang /*@
4094e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4095e5e524a1SHong Zhang 
4096e5e524a1SHong Zhang    Not Collective
4097e5e524a1SHong Zhang 
4098e5e524a1SHong Zhang    Input Parameter:
4099e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4100e5e524a1SHong Zhang 
4101e5e524a1SHong Zhang    Output Parameter:
4102e5e524a1SHong Zhang .  t  - the previous time
4103e5e524a1SHong Zhang 
4104e5e524a1SHong Zhang    Level: beginner
4105e5e524a1SHong Zhang 
4106aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4107e5e524a1SHong Zhang 
4108e5e524a1SHong Zhang @*/
4109e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4110e5e524a1SHong Zhang {
4111e5e524a1SHong Zhang   PetscFunctionBegin;
4112e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4113e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4114e5e524a1SHong Zhang   *t = ts->ptime_prev;
4115e5e524a1SHong Zhang   PetscFunctionReturn(0);
4116e5e524a1SHong Zhang }
4117e5e524a1SHong Zhang 
41186a4d4014SLisandro Dalcin /*@
41196a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
41206a4d4014SLisandro Dalcin 
41213f9fe445SBarry Smith    Logically Collective on TS
41226a4d4014SLisandro Dalcin 
41236a4d4014SLisandro Dalcin    Input Parameters:
41246a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
41256a4d4014SLisandro Dalcin -  time - the time
41266a4d4014SLisandro Dalcin 
41276a4d4014SLisandro Dalcin    Level: intermediate
41286a4d4014SLisandro Dalcin 
412919eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
41306a4d4014SLisandro Dalcin 
41316a4d4014SLisandro Dalcin @*/
41327087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
41336a4d4014SLisandro Dalcin {
41346a4d4014SLisandro Dalcin   PetscFunctionBegin;
41350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4136c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
41376a4d4014SLisandro Dalcin   ts->ptime = t;
41386a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41396a4d4014SLisandro Dalcin }
41406a4d4014SLisandro Dalcin 
4141d763cef2SBarry Smith /*@C
4142d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4143d763cef2SBarry Smith    TS options in the database.
4144d763cef2SBarry Smith 
41453f9fe445SBarry Smith    Logically Collective on TS
4146d763cef2SBarry Smith 
4147d8d19677SJose E. Roman    Input Parameters:
4148d763cef2SBarry Smith +  ts     - The TS context
4149d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4150d763cef2SBarry Smith 
4151d763cef2SBarry Smith    Notes:
4152d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4153d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4154d763cef2SBarry Smith    hyphen.
4155d763cef2SBarry Smith 
4156d763cef2SBarry Smith    Level: advanced
4157d763cef2SBarry Smith 
4158d763cef2SBarry Smith .seealso: TSSetFromOptions()
4159d763cef2SBarry Smith 
4160d763cef2SBarry Smith @*/
41617087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4162d763cef2SBarry Smith {
4163dfbe8321SBarry Smith   PetscErrorCode ierr;
4164089b2837SJed Brown   SNES           snes;
4165d763cef2SBarry Smith 
4166d763cef2SBarry Smith   PetscFunctionBegin;
41670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4168d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4169089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4170089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4171d763cef2SBarry Smith   PetscFunctionReturn(0);
4172d763cef2SBarry Smith }
4173d763cef2SBarry Smith 
4174d763cef2SBarry Smith /*@C
4175d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4176d763cef2SBarry Smith    TS options in the database.
4177d763cef2SBarry Smith 
41783f9fe445SBarry Smith    Logically Collective on TS
4179d763cef2SBarry Smith 
4180d8d19677SJose E. Roman    Input Parameters:
4181d763cef2SBarry Smith +  ts     - The TS context
4182d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4183d763cef2SBarry Smith 
4184d763cef2SBarry Smith    Notes:
4185d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4186d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4187d763cef2SBarry Smith    hyphen.
4188d763cef2SBarry Smith 
4189d763cef2SBarry Smith    Level: advanced
4190d763cef2SBarry Smith 
4191d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4192d763cef2SBarry Smith 
4193d763cef2SBarry Smith @*/
41947087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4195d763cef2SBarry Smith {
4196dfbe8321SBarry Smith   PetscErrorCode ierr;
4197089b2837SJed Brown   SNES           snes;
4198d763cef2SBarry Smith 
4199d763cef2SBarry Smith   PetscFunctionBegin;
42000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4201d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4202089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4203089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4204d763cef2SBarry Smith   PetscFunctionReturn(0);
4205d763cef2SBarry Smith }
4206d763cef2SBarry Smith 
4207d763cef2SBarry Smith /*@C
4208d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4209d763cef2SBarry Smith    TS options in the database.
4210d763cef2SBarry Smith 
4211d763cef2SBarry Smith    Not Collective
4212d763cef2SBarry Smith 
4213d763cef2SBarry Smith    Input Parameter:
4214d763cef2SBarry Smith .  ts - The TS context
4215d763cef2SBarry Smith 
4216d763cef2SBarry Smith    Output Parameter:
4217d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4218d763cef2SBarry Smith 
421995452b02SPatrick Sanan    Notes:
422095452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4221d763cef2SBarry Smith    sufficient length to hold the prefix.
4222d763cef2SBarry Smith 
4223d763cef2SBarry Smith    Level: intermediate
4224d763cef2SBarry Smith 
4225d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4226d763cef2SBarry Smith @*/
42277087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4228d763cef2SBarry Smith {
4229dfbe8321SBarry Smith   PetscErrorCode ierr;
4230d763cef2SBarry Smith 
4231d763cef2SBarry Smith   PetscFunctionBegin;
42320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42334482741eSBarry Smith   PetscValidPointer(prefix,2);
4234d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4235d763cef2SBarry Smith   PetscFunctionReturn(0);
4236d763cef2SBarry Smith }
4237d763cef2SBarry Smith 
4238d763cef2SBarry Smith /*@C
4239d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4240d763cef2SBarry Smith 
4241d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4242d763cef2SBarry Smith 
4243d763cef2SBarry Smith    Input Parameter:
4244d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4245d763cef2SBarry Smith 
4246d763cef2SBarry Smith    Output Parameters:
4247e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4248e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4249e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4250e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4251d763cef2SBarry Smith 
425295452b02SPatrick Sanan    Notes:
425395452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4254d763cef2SBarry Smith 
4255d763cef2SBarry Smith    Level: intermediate
4256d763cef2SBarry Smith 
425780275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4258d763cef2SBarry Smith 
4259d763cef2SBarry Smith @*/
4260e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4261d763cef2SBarry Smith {
4262089b2837SJed Brown   PetscErrorCode ierr;
426324989b8cSPeter Brune   DM             dm;
4264089b2837SJed Brown 
4265d763cef2SBarry Smith   PetscFunctionBegin;
426623a57915SBarry Smith   if (Amat || Pmat) {
426723a57915SBarry Smith     SNES snes;
4268089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
426923a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4270e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
427123a57915SBarry Smith   }
427224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
427324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4274d763cef2SBarry Smith   PetscFunctionReturn(0);
4275d763cef2SBarry Smith }
4276d763cef2SBarry Smith 
42772eca1d9cSJed Brown /*@C
42782eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42792eca1d9cSJed Brown 
42802eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42812eca1d9cSJed Brown 
42822eca1d9cSJed Brown    Input Parameter:
42832eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42842eca1d9cSJed Brown 
42852eca1d9cSJed Brown    Output Parameters:
4286e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4287e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42882eca1d9cSJed Brown .  f   - The function to compute the matrices
42892eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42902eca1d9cSJed Brown 
429195452b02SPatrick Sanan    Notes:
429295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42932eca1d9cSJed Brown 
42942eca1d9cSJed Brown    Level: advanced
42952eca1d9cSJed Brown 
429680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42972eca1d9cSJed Brown 
42982eca1d9cSJed Brown @*/
4299e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
43002eca1d9cSJed Brown {
4301089b2837SJed Brown   PetscErrorCode ierr;
430224989b8cSPeter Brune   DM             dm;
43030910c330SBarry Smith 
43042eca1d9cSJed Brown   PetscFunctionBegin;
4305c0aab802Sstefano_zampini   if (Amat || Pmat) {
4306c0aab802Sstefano_zampini     SNES snes;
4307089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4308f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4309e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4310c0aab802Sstefano_zampini   }
431124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
431224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
43132eca1d9cSJed Brown   PetscFunctionReturn(0);
43142eca1d9cSJed Brown }
43152eca1d9cSJed Brown 
4316af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
43176c699258SBarry Smith /*@
43182a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
43196c699258SBarry Smith 
4320d083f849SBarry Smith    Logically Collective on ts
43216c699258SBarry Smith 
43226c699258SBarry Smith    Input Parameters:
43232a808120SBarry Smith +  ts - the ODE integrator object
43242a808120SBarry Smith -  dm - the dm, cannot be NULL
43256c699258SBarry Smith 
4326e03a659cSJed Brown    Notes:
4327e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4328e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4329e03a659cSJed Brown    different problems using the same function space.
4330e03a659cSJed Brown 
43316c699258SBarry Smith    Level: intermediate
43326c699258SBarry Smith 
43336c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
43346c699258SBarry Smith @*/
43357087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
43366c699258SBarry Smith {
43376c699258SBarry Smith   PetscErrorCode ierr;
4338089b2837SJed Brown   SNES           snes;
4339942e3340SBarry Smith   DMTS           tsdm;
43406c699258SBarry Smith 
43416c699258SBarry Smith   PetscFunctionBegin;
43420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43432a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
434470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4345942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
43462a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4347942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4348942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
434924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
435024989b8cSPeter Brune         tsdm->originaldm = dm;
435124989b8cSPeter Brune       }
435224989b8cSPeter Brune     }
43536bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
435424989b8cSPeter Brune   }
43556c699258SBarry Smith   ts->dm = dm;
4356bbd56ea5SKarl Rupp 
4357089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4358089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
43596c699258SBarry Smith   PetscFunctionReturn(0);
43606c699258SBarry Smith }
43616c699258SBarry Smith 
43626c699258SBarry Smith /*@
43636c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
43646c699258SBarry Smith 
43653f9fe445SBarry Smith    Not Collective
43666c699258SBarry Smith 
43676c699258SBarry Smith    Input Parameter:
43686c699258SBarry Smith . ts - the preconditioner context
43696c699258SBarry Smith 
43706c699258SBarry Smith    Output Parameter:
43716c699258SBarry Smith .  dm - the dm
43726c699258SBarry Smith 
43736c699258SBarry Smith    Level: intermediate
43746c699258SBarry Smith 
43756c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
43766c699258SBarry Smith @*/
43777087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
43786c699258SBarry Smith {
4379496e6a7aSJed Brown   PetscErrorCode ierr;
4380496e6a7aSJed Brown 
43816c699258SBarry Smith   PetscFunctionBegin;
43820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4383496e6a7aSJed Brown   if (!ts->dm) {
4384ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4385496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4386496e6a7aSJed Brown   }
43876c699258SBarry Smith   *dm = ts->dm;
43886c699258SBarry Smith   PetscFunctionReturn(0);
43896c699258SBarry Smith }
43901713a123SBarry Smith 
43910f5c6efeSJed Brown /*@
43920f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
43930f5c6efeSJed Brown 
43943f9fe445SBarry Smith    Logically Collective on SNES
43950f5c6efeSJed Brown 
4396d8d19677SJose E. Roman    Input Parameters:
4397d42a1c89SJed Brown + snes - nonlinear solver
43980910c330SBarry Smith . U - the current state at which to evaluate the residual
4399d42a1c89SJed Brown - ctx - user context, must be a TS
44000f5c6efeSJed Brown 
44010f5c6efeSJed Brown    Output Parameter:
44020f5c6efeSJed Brown . F - the nonlinear residual
44030f5c6efeSJed Brown 
44040f5c6efeSJed Brown    Notes:
44050f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
44060f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
44070f5c6efeSJed Brown 
44080f5c6efeSJed Brown    Level: advanced
44090f5c6efeSJed Brown 
44100f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
44110f5c6efeSJed Brown @*/
44120910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
44130f5c6efeSJed Brown {
44140f5c6efeSJed Brown   TS             ts = (TS)ctx;
44150f5c6efeSJed Brown   PetscErrorCode ierr;
44160f5c6efeSJed Brown 
44170f5c6efeSJed Brown   PetscFunctionBegin;
44180f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
44190910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
44200f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
44210f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
44220910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
44230f5c6efeSJed Brown   PetscFunctionReturn(0);
44240f5c6efeSJed Brown }
44250f5c6efeSJed Brown 
44260f5c6efeSJed Brown /*@
44270f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
44280f5c6efeSJed Brown 
44290f5c6efeSJed Brown    Collective on SNES
44300f5c6efeSJed Brown 
4431d8d19677SJose E. Roman    Input Parameters:
44320f5c6efeSJed Brown + snes - nonlinear solver
44330910c330SBarry Smith . U - the current state at which to evaluate the residual
44340f5c6efeSJed Brown - ctx - user context, must be a TS
44350f5c6efeSJed Brown 
4436d8d19677SJose E. Roman    Output Parameters:
44370f5c6efeSJed Brown + A - the Jacobian
44386b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A)
44390f5c6efeSJed Brown 
44400f5c6efeSJed Brown    Notes:
44410f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
44420f5c6efeSJed Brown 
44430f5c6efeSJed Brown    Level: developer
44440f5c6efeSJed Brown 
44450f5c6efeSJed Brown .seealso: SNESSetJacobian()
44460f5c6efeSJed Brown @*/
4447d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
44480f5c6efeSJed Brown {
44490f5c6efeSJed Brown   TS             ts = (TS)ctx;
44500f5c6efeSJed Brown   PetscErrorCode ierr;
44510f5c6efeSJed Brown 
44520f5c6efeSJed Brown   PetscFunctionBegin;
44530f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
44540910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
44550f5c6efeSJed Brown   PetscValidPointer(A,3);
445694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
44570f5c6efeSJed Brown   PetscValidPointer(B,4);
445894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
4459064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ts,TS_CLASSID,5);
4460d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
44610f5c6efeSJed Brown   PetscFunctionReturn(0);
44620f5c6efeSJed Brown }
4463325fc9f4SBarry Smith 
44640e4ef248SJed Brown /*@C
44659ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
44660e4ef248SJed Brown 
44670e4ef248SJed Brown    Collective on TS
44680e4ef248SJed Brown 
44694165533cSJose E. Roman    Input Parameters:
44700e4ef248SJed Brown +  ts - time stepping context
44710e4ef248SJed Brown .  t - time at which to evaluate
44720910c330SBarry Smith .  U - state at which to evaluate
44730e4ef248SJed Brown -  ctx - context
44740e4ef248SJed Brown 
44754165533cSJose E. Roman    Output Parameter:
44760e4ef248SJed Brown .  F - right hand side
44770e4ef248SJed Brown 
44780e4ef248SJed Brown    Level: intermediate
44790e4ef248SJed Brown 
44800e4ef248SJed Brown    Notes:
44810e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
44820e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
44830e4ef248SJed Brown 
44840e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
44850e4ef248SJed Brown @*/
44860910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
44870e4ef248SJed Brown {
44880e4ef248SJed Brown   PetscErrorCode ierr;
44890e4ef248SJed Brown   Mat            Arhs,Brhs;
44900e4ef248SJed Brown 
44910e4ef248SJed Brown   PetscFunctionBegin;
44920e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
44932663174eSHong Zhang   /* undo the damage caused by shifting */
4494cfa8a9a2SHong Zhang   ierr = TSRecoverRHSJacobian(ts,Arhs,Brhs);CHKERRQ(ierr);
4495d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
44960910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
44970e4ef248SJed Brown   PetscFunctionReturn(0);
44980e4ef248SJed Brown }
44990e4ef248SJed Brown 
45000e4ef248SJed Brown /*@C
45010e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
45020e4ef248SJed Brown 
45030e4ef248SJed Brown    Collective on TS
45040e4ef248SJed Brown 
45054165533cSJose E. Roman    Input Parameters:
45060e4ef248SJed Brown +  ts - time stepping context
45070e4ef248SJed Brown .  t - time at which to evaluate
45080910c330SBarry Smith .  U - state at which to evaluate
45090e4ef248SJed Brown -  ctx - context
45100e4ef248SJed Brown 
45114165533cSJose E. Roman    Output Parameters:
45120e4ef248SJed Brown +  A - pointer to operator
451397bb3fdcSJose E. Roman -  B - pointer to preconditioning matrix
45140e4ef248SJed Brown 
45150e4ef248SJed Brown    Level: intermediate
45160e4ef248SJed Brown 
45170e4ef248SJed Brown    Notes:
45180e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
45190e4ef248SJed Brown 
45200e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
45210e4ef248SJed Brown @*/
4522d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
45230e4ef248SJed Brown {
45240e4ef248SJed Brown   PetscFunctionBegin;
45250e4ef248SJed Brown   PetscFunctionReturn(0);
45260e4ef248SJed Brown }
45270e4ef248SJed Brown 
45280026cea9SSean Farley /*@C
45290026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
45300026cea9SSean Farley 
45310026cea9SSean Farley    Collective on TS
45320026cea9SSean Farley 
45334165533cSJose E. Roman    Input Parameters:
45340026cea9SSean Farley +  ts - time stepping context
45350026cea9SSean Farley .  t - time at which to evaluate
45360910c330SBarry Smith .  U - state at which to evaluate
45370910c330SBarry Smith .  Udot - time derivative of state vector
45380026cea9SSean Farley -  ctx - context
45390026cea9SSean Farley 
45404165533cSJose E. Roman    Output Parameter:
45410026cea9SSean Farley .  F - left hand side
45420026cea9SSean Farley 
45430026cea9SSean Farley    Level: intermediate
45440026cea9SSean Farley 
45450026cea9SSean Farley    Notes:
45460910c330SBarry 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
45470026cea9SSean Farley    user is required to write their own TSComputeIFunction.
45480026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
45490026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
45500026cea9SSean Farley 
45519ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
45529ae8fd06SBarry Smith 
45539ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
45540026cea9SSean Farley @*/
45550910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
45560026cea9SSean Farley {
45570026cea9SSean Farley   PetscErrorCode ierr;
45580026cea9SSean Farley   Mat            A,B;
45590026cea9SSean Farley 
45600026cea9SSean Farley   PetscFunctionBegin;
45610298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4562d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
45630910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
45640026cea9SSean Farley   PetscFunctionReturn(0);
45650026cea9SSean Farley }
45660026cea9SSean Farley 
45670026cea9SSean Farley /*@C
4568b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
45690026cea9SSean Farley 
45700026cea9SSean Farley    Collective on TS
45710026cea9SSean Farley 
45724165533cSJose E. Roman    Input Parameters:
45730026cea9SSean Farley +  ts - time stepping context
45740026cea9SSean Farley .  t - time at which to evaluate
45750910c330SBarry Smith .  U - state at which to evaluate
45760910c330SBarry Smith .  Udot - time derivative of state vector
45770026cea9SSean Farley .  shift - shift to apply
45780026cea9SSean Farley -  ctx - context
45790026cea9SSean Farley 
45804165533cSJose E. Roman    Output Parameters:
45810026cea9SSean Farley +  A - pointer to operator
458297bb3fdcSJose E. Roman -  B - pointer to preconditioning matrix
45830026cea9SSean Farley 
4584b41af12eSJed Brown    Level: advanced
45850026cea9SSean Farley 
45860026cea9SSean Farley    Notes:
45870026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
45880026cea9SSean Farley 
4589b41af12eSJed Brown    It is only appropriate for problems of the form
4590b41af12eSJed Brown 
4591b41af12eSJed Brown $     M Udot = F(U,t)
4592b41af12eSJed Brown 
4593b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4594b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4595b41af12eSJed Brown   an implicit operator of the form
4596b41af12eSJed Brown 
4597b41af12eSJed Brown $    shift*M + J
4598b41af12eSJed Brown 
4599b41af12eSJed 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
4600b41af12eSJed Brown   a copy of M or reassemble it when requested.
4601b41af12eSJed Brown 
46020026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
46030026cea9SSean Farley @*/
4604d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
46050026cea9SSean Farley {
4606b41af12eSJed Brown   PetscErrorCode ierr;
4607b41af12eSJed Brown 
46080026cea9SSean Farley   PetscFunctionBegin;
460994ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4610b41af12eSJed Brown   ts->ijacobian.shift = shift;
46110026cea9SSean Farley   PetscFunctionReturn(0);
46120026cea9SSean Farley }
4613b41af12eSJed Brown 
4614e817cc15SEmil Constantinescu /*@
4615e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4616e817cc15SEmil Constantinescu 
4617e817cc15SEmil Constantinescu    Not Collective
4618e817cc15SEmil Constantinescu 
4619e817cc15SEmil Constantinescu    Input Parameter:
4620e817cc15SEmil Constantinescu .  ts - the TS context
4621e817cc15SEmil Constantinescu 
4622e817cc15SEmil Constantinescu    Output Parameter:
46234e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4624e817cc15SEmil Constantinescu 
4625e817cc15SEmil Constantinescu    Level: beginner
4626e817cc15SEmil Constantinescu 
4627e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4628e817cc15SEmil Constantinescu @*/
4629e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4630e817cc15SEmil Constantinescu {
4631e817cc15SEmil Constantinescu   PetscFunctionBegin;
4632e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4633e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4634e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4635e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4636e817cc15SEmil Constantinescu }
4637e817cc15SEmil Constantinescu 
4638e817cc15SEmil Constantinescu /*@
4639e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4640e817cc15SEmil Constantinescu 
4641e817cc15SEmil Constantinescu    Not Collective
4642e817cc15SEmil Constantinescu 
4643d8d19677SJose E. Roman    Input Parameters:
4644e817cc15SEmil Constantinescu +  ts - the TS context
46451297b224SEmil Constantinescu -  equation_type - see TSEquationType
4646e817cc15SEmil Constantinescu 
4647e817cc15SEmil Constantinescu    Level: advanced
4648e817cc15SEmil Constantinescu 
4649e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4650e817cc15SEmil Constantinescu @*/
4651e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4652e817cc15SEmil Constantinescu {
4653e817cc15SEmil Constantinescu   PetscFunctionBegin;
4654e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4655e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4656e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4657e817cc15SEmil Constantinescu }
46580026cea9SSean Farley 
46594af1b03aSJed Brown /*@
46604af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
46614af1b03aSJed Brown 
46624af1b03aSJed Brown    Not Collective
46634af1b03aSJed Brown 
46644af1b03aSJed Brown    Input Parameter:
46654af1b03aSJed Brown .  ts - the TS context
46664af1b03aSJed Brown 
46674af1b03aSJed Brown    Output Parameter:
46684af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
46694af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
46704af1b03aSJed Brown 
4671487e0bb9SJed Brown    Level: beginner
46724af1b03aSJed Brown 
4673cd652676SJed Brown    Notes:
4674cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
46754af1b03aSJed Brown 
46764af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
46774af1b03aSJed Brown @*/
46784af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
46794af1b03aSJed Brown {
46804af1b03aSJed Brown   PetscFunctionBegin;
46814af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46824af1b03aSJed Brown   PetscValidPointer(reason,2);
46834af1b03aSJed Brown   *reason = ts->reason;
46844af1b03aSJed Brown   PetscFunctionReturn(0);
46854af1b03aSJed Brown }
46864af1b03aSJed Brown 
4687d6ad946cSShri Abhyankar /*@
4688d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4689d6ad946cSShri Abhyankar 
46906b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
4691d6ad946cSShri Abhyankar 
46926b221cbeSPatrick Sanan    Input Parameters:
4693d6ad946cSShri Abhyankar +  ts - the TS context
46946b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4695d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4696d6ad946cSShri Abhyankar 
4697f5abba47SShri Abhyankar    Level: advanced
4698d6ad946cSShri Abhyankar 
4699d6ad946cSShri Abhyankar    Notes:
47006b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
4701d6ad946cSShri Abhyankar 
4702d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4703d6ad946cSShri Abhyankar @*/
4704d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4705d6ad946cSShri Abhyankar {
4706d6ad946cSShri Abhyankar   PetscFunctionBegin;
4707d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4708d6ad946cSShri Abhyankar   ts->reason = reason;
4709d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4710d6ad946cSShri Abhyankar }
4711d6ad946cSShri Abhyankar 
4712cc708dedSBarry Smith /*@
4713cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4714cc708dedSBarry Smith 
4715cc708dedSBarry Smith    Not Collective
4716cc708dedSBarry Smith 
4717cc708dedSBarry Smith    Input Parameter:
4718cc708dedSBarry Smith .  ts - the TS context
4719cc708dedSBarry Smith 
4720cc708dedSBarry Smith    Output Parameter:
472119eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4722cc708dedSBarry Smith 
4723487e0bb9SJed Brown    Level: beginner
4724cc708dedSBarry Smith 
4725cc708dedSBarry Smith    Notes:
4726cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4727cc708dedSBarry Smith 
4728cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4729cc708dedSBarry Smith @*/
4730cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4731cc708dedSBarry Smith {
4732cc708dedSBarry Smith   PetscFunctionBegin;
4733cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4734cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4735cc708dedSBarry Smith   *ftime = ts->solvetime;
4736cc708dedSBarry Smith   PetscFunctionReturn(0);
4737cc708dedSBarry Smith }
4738cc708dedSBarry Smith 
47392c18e0fdSBarry Smith /*@
47405ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
47419f67acb7SJed Brown    used by the time integrator.
47429f67acb7SJed Brown 
47439f67acb7SJed Brown    Not Collective
47449f67acb7SJed Brown 
47459f67acb7SJed Brown    Input Parameter:
47469f67acb7SJed Brown .  ts - TS context
47479f67acb7SJed Brown 
47489f67acb7SJed Brown    Output Parameter:
47499f67acb7SJed Brown .  nits - number of nonlinear iterations
47509f67acb7SJed Brown 
47519f67acb7SJed Brown    Notes:
47529f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
47539f67acb7SJed Brown 
47549f67acb7SJed Brown    Level: intermediate
47559f67acb7SJed Brown 
47565ef26d82SJed Brown .seealso:  TSGetKSPIterations()
47579f67acb7SJed Brown @*/
47585ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
47599f67acb7SJed Brown {
47609f67acb7SJed Brown   PetscFunctionBegin;
47619f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47629f67acb7SJed Brown   PetscValidIntPointer(nits,2);
47635ef26d82SJed Brown   *nits = ts->snes_its;
47649f67acb7SJed Brown   PetscFunctionReturn(0);
47659f67acb7SJed Brown }
47669f67acb7SJed Brown 
47679f67acb7SJed Brown /*@
47685ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
47699f67acb7SJed Brown    used by the time integrator.
47709f67acb7SJed Brown 
47719f67acb7SJed Brown    Not Collective
47729f67acb7SJed Brown 
47739f67acb7SJed Brown    Input Parameter:
47749f67acb7SJed Brown .  ts - TS context
47759f67acb7SJed Brown 
47769f67acb7SJed Brown    Output Parameter:
47779f67acb7SJed Brown .  lits - number of linear iterations
47789f67acb7SJed Brown 
47799f67acb7SJed Brown    Notes:
47809f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
47819f67acb7SJed Brown 
47829f67acb7SJed Brown    Level: intermediate
47839f67acb7SJed Brown 
47845ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
47859f67acb7SJed Brown @*/
47865ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
47879f67acb7SJed Brown {
47889f67acb7SJed Brown   PetscFunctionBegin;
47899f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47909f67acb7SJed Brown   PetscValidIntPointer(lits,2);
47915ef26d82SJed Brown   *lits = ts->ksp_its;
47929f67acb7SJed Brown   PetscFunctionReturn(0);
47939f67acb7SJed Brown }
47949f67acb7SJed Brown 
4795cef5090cSJed Brown /*@
4796cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4797cef5090cSJed Brown 
4798cef5090cSJed Brown    Not Collective
4799cef5090cSJed Brown 
4800cef5090cSJed Brown    Input Parameter:
4801cef5090cSJed Brown .  ts - TS context
4802cef5090cSJed Brown 
4803cef5090cSJed Brown    Output Parameter:
4804cef5090cSJed Brown .  rejects - number of steps rejected
4805cef5090cSJed Brown 
4806cef5090cSJed Brown    Notes:
4807cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4808cef5090cSJed Brown 
4809cef5090cSJed Brown    Level: intermediate
4810cef5090cSJed Brown 
48115ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4812cef5090cSJed Brown @*/
4813cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4814cef5090cSJed Brown {
4815cef5090cSJed Brown   PetscFunctionBegin;
4816cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4817cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4818cef5090cSJed Brown   *rejects = ts->reject;
4819cef5090cSJed Brown   PetscFunctionReturn(0);
4820cef5090cSJed Brown }
4821cef5090cSJed Brown 
4822cef5090cSJed Brown /*@
4823cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4824cef5090cSJed Brown 
4825cef5090cSJed Brown    Not Collective
4826cef5090cSJed Brown 
4827cef5090cSJed Brown    Input Parameter:
4828cef5090cSJed Brown .  ts - TS context
4829cef5090cSJed Brown 
4830cef5090cSJed Brown    Output Parameter:
4831cef5090cSJed Brown .  fails - number of failed nonlinear solves
4832cef5090cSJed Brown 
4833cef5090cSJed Brown    Notes:
4834cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4835cef5090cSJed Brown 
4836cef5090cSJed Brown    Level: intermediate
4837cef5090cSJed Brown 
48385ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4839cef5090cSJed Brown @*/
4840cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4841cef5090cSJed Brown {
4842cef5090cSJed Brown   PetscFunctionBegin;
4843cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4844cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4845cef5090cSJed Brown   *fails = ts->num_snes_failures;
4846cef5090cSJed Brown   PetscFunctionReturn(0);
4847cef5090cSJed Brown }
4848cef5090cSJed Brown 
4849cef5090cSJed Brown /*@
4850cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4851cef5090cSJed Brown 
4852cef5090cSJed Brown    Not Collective
4853cef5090cSJed Brown 
4854d8d19677SJose E. Roman    Input Parameters:
4855cef5090cSJed Brown +  ts - TS context
4856cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4857cef5090cSJed Brown 
4858cef5090cSJed Brown    Notes:
4859cef5090cSJed Brown    The counter is reset to zero for each step
4860cef5090cSJed Brown 
4861cef5090cSJed Brown    Options Database Key:
4862cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4863cef5090cSJed Brown 
4864cef5090cSJed Brown    Level: intermediate
4865cef5090cSJed Brown 
48665ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4867cef5090cSJed Brown @*/
4868cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4869cef5090cSJed Brown {
4870cef5090cSJed Brown   PetscFunctionBegin;
4871cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4872cef5090cSJed Brown   ts->max_reject = rejects;
4873cef5090cSJed Brown   PetscFunctionReturn(0);
4874cef5090cSJed Brown }
4875cef5090cSJed Brown 
4876cef5090cSJed Brown /*@
4877cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4878cef5090cSJed Brown 
4879cef5090cSJed Brown    Not Collective
4880cef5090cSJed Brown 
4881d8d19677SJose E. Roman    Input Parameters:
4882cef5090cSJed Brown +  ts - TS context
4883cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4884cef5090cSJed Brown 
4885cef5090cSJed Brown    Notes:
4886cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4887cef5090cSJed Brown 
4888cef5090cSJed Brown    Options Database Key:
4889cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4890cef5090cSJed Brown 
4891cef5090cSJed Brown    Level: intermediate
4892cef5090cSJed Brown 
48935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4894cef5090cSJed Brown @*/
4895cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4896cef5090cSJed Brown {
4897cef5090cSJed Brown   PetscFunctionBegin;
4898cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4899cef5090cSJed Brown   ts->max_snes_failures = fails;
4900cef5090cSJed Brown   PetscFunctionReturn(0);
4901cef5090cSJed Brown }
4902cef5090cSJed Brown 
4903cef5090cSJed Brown /*@
4904cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4905cef5090cSJed Brown 
4906cef5090cSJed Brown    Not Collective
4907cef5090cSJed Brown 
4908d8d19677SJose E. Roman    Input Parameters:
4909cef5090cSJed Brown +  ts - TS context
4910cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4911cef5090cSJed Brown 
4912cef5090cSJed Brown    Options Database Key:
4913cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4914cef5090cSJed Brown 
4915cef5090cSJed Brown    Level: intermediate
4916cef5090cSJed Brown 
49175ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4918cef5090cSJed Brown @*/
4919cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4920cef5090cSJed Brown {
4921cef5090cSJed Brown   PetscFunctionBegin;
4922cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4923cef5090cSJed Brown   ts->errorifstepfailed = err;
4924cef5090cSJed Brown   PetscFunctionReturn(0);
4925cef5090cSJed Brown }
4926cef5090cSJed Brown 
492784df9cb4SJed Brown /*@
4928552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
492984df9cb4SJed Brown 
4930ed81e22dSJed Brown    Collective on TS if controller has not been created yet
493184df9cb4SJed Brown 
49324165533cSJose E. Roman    Input Parameter:
4933ed81e22dSJed Brown .  ts - time stepping context
493484df9cb4SJed Brown 
49354165533cSJose E. Roman    Output Parameter:
4936ed81e22dSJed Brown .  adapt - adaptive controller
493784df9cb4SJed Brown 
493884df9cb4SJed Brown    Level: intermediate
493984df9cb4SJed Brown 
4940ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
494184df9cb4SJed Brown @*/
4942552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
494384df9cb4SJed Brown {
494484df9cb4SJed Brown   PetscErrorCode ierr;
494584df9cb4SJed Brown 
494684df9cb4SJed Brown   PetscFunctionBegin;
494784df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4948bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
494984df9cb4SJed Brown   if (!ts->adapt) {
4950ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
49513bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
49521c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
495384df9cb4SJed Brown   }
4954bec58848SLisandro Dalcin   *adapt = ts->adapt;
495584df9cb4SJed Brown   PetscFunctionReturn(0);
495684df9cb4SJed Brown }
4957d6ebe24aSShri Abhyankar 
49581c3436cfSJed Brown /*@
49591c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
49601c3436cfSJed Brown 
49611c3436cfSJed Brown    Logically Collective
49621c3436cfSJed Brown 
49634165533cSJose E. Roman    Input Parameters:
49641c3436cfSJed Brown +  ts - time integration context
49651c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
49660298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
49671c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
49680298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
49691c3436cfSJed Brown 
4970a3cdaa26SBarry Smith    Options Database keys:
4971a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4972*67b8a455SSatish Balay -  -ts_atol <atol> - Absolute tolerance for local truncation error
4973a3cdaa26SBarry Smith 
49743ff766beSShri Abhyankar    Notes:
49753ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
49763ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
49773ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
49783ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
49793ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
49803ff766beSShri Abhyankar    differential variables.
49813ff766beSShri Abhyankar 
49821c3436cfSJed Brown    Level: beginner
49831c3436cfSJed Brown 
49845c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
49851c3436cfSJed Brown @*/
49861c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
49871c3436cfSJed Brown {
49881c3436cfSJed Brown   PetscErrorCode ierr;
49891c3436cfSJed Brown 
49901c3436cfSJed Brown   PetscFunctionBegin;
4991c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
49921c3436cfSJed Brown   if (vatol) {
49931c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
49941c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
49951c3436cfSJed Brown     ts->vatol = vatol;
49961c3436cfSJed Brown   }
4997c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
49981c3436cfSJed Brown   if (vrtol) {
49991c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
50001c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
50011c3436cfSJed Brown     ts->vrtol = vrtol;
50021c3436cfSJed Brown   }
50031c3436cfSJed Brown   PetscFunctionReturn(0);
50041c3436cfSJed Brown }
50051c3436cfSJed Brown 
5006c5033834SJed Brown /*@
5007c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5008c5033834SJed Brown 
5009c5033834SJed Brown    Logically Collective
5010c5033834SJed Brown 
50114165533cSJose E. Roman    Input Parameter:
5012c5033834SJed Brown .  ts - time integration context
5013c5033834SJed Brown 
50144165533cSJose E. Roman    Output Parameters:
50150298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
50160298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
50170298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
50180298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5019c5033834SJed Brown 
5020c5033834SJed Brown    Level: beginner
5021c5033834SJed Brown 
50225c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5023c5033834SJed Brown @*/
5024c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5025c5033834SJed Brown {
5026c5033834SJed Brown   PetscFunctionBegin;
5027c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5028c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5029c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5030c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5031c5033834SJed Brown   PetscFunctionReturn(0);
5032c5033834SJed Brown }
5033c5033834SJed Brown 
50349c6b16b5SShri Abhyankar /*@
5035a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
50369c6b16b5SShri Abhyankar 
50379c6b16b5SShri Abhyankar    Collective on TS
50389c6b16b5SShri Abhyankar 
50394165533cSJose E. Roman    Input Parameters:
50409c6b16b5SShri Abhyankar +  ts - time stepping context
5041a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5042a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
50439c6b16b5SShri Abhyankar 
50444165533cSJose E. Roman    Output Parameters:
5045a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
50467453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5047a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
50489c6b16b5SShri Abhyankar 
50499c6b16b5SShri Abhyankar    Level: developer
50509c6b16b5SShri Abhyankar 
5051deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
50529c6b16b5SShri Abhyankar @*/
50537453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
50549c6b16b5SShri Abhyankar {
50559c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
50569c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
50577453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
50587453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
50599c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
50607453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
50617453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
50627453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
50639c6b16b5SShri Abhyankar 
50649c6b16b5SShri Abhyankar   PetscFunctionBegin;
50659c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5066a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5067a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5068a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5069a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5070a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5071a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
50728a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
50738a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
50742c71b3e2SJacob Faibussowitsch   PetscCheckFalse(U == Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
50759c6b16b5SShri Abhyankar 
50769c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
50779c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
50789c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
50799c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
50809c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
50817453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
50827453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
50837453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
50849c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
50859c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
50869c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50879c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50889c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
508976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
50907453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
50917453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
50927453f775SEmil Constantinescu       if (tola>0.) {
50937453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
50947453f775SEmil Constantinescu         na_loc++;
50957453f775SEmil Constantinescu       }
50967453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50977453f775SEmil Constantinescu       if (tolr>0.) {
50987453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
50997453f775SEmil Constantinescu         nr_loc++;
51007453f775SEmil Constantinescu       }
51017453f775SEmil Constantinescu       tol=tola+tolr;
51027453f775SEmil Constantinescu       if (tol>0.) {
51037453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
51047453f775SEmil Constantinescu         n_loc++;
51057453f775SEmil Constantinescu       }
51069c6b16b5SShri Abhyankar     }
51079c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
51089c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
51099c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
51109c6b16b5SShri Abhyankar     const PetscScalar *atol;
51119c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
51129c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
511376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
51147453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
51157453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
51167453f775SEmil Constantinescu       if (tola>0.) {
51177453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
51187453f775SEmil Constantinescu         na_loc++;
51197453f775SEmil Constantinescu       }
51207453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51217453f775SEmil Constantinescu       if (tolr>0.) {
51227453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
51237453f775SEmil Constantinescu         nr_loc++;
51247453f775SEmil Constantinescu       }
51257453f775SEmil Constantinescu       tol=tola+tolr;
51267453f775SEmil Constantinescu       if (tol>0.) {
51277453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
51287453f775SEmil Constantinescu         n_loc++;
51297453f775SEmil Constantinescu       }
51309c6b16b5SShri Abhyankar     }
51319c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
51329c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
51339c6b16b5SShri Abhyankar     const PetscScalar *rtol;
51349c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
51359c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
513676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
51377453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
51387453f775SEmil Constantinescu       tola = ts->atol;
51397453f775SEmil Constantinescu       if (tola>0.) {
51407453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
51417453f775SEmil Constantinescu         na_loc++;
51427453f775SEmil Constantinescu       }
51437453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51447453f775SEmil Constantinescu       if (tolr>0.) {
51457453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
51467453f775SEmil Constantinescu         nr_loc++;
51477453f775SEmil Constantinescu       }
51487453f775SEmil Constantinescu       tol=tola+tolr;
51497453f775SEmil Constantinescu       if (tol>0.) {
51507453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
51517453f775SEmil Constantinescu         n_loc++;
51527453f775SEmil Constantinescu       }
51539c6b16b5SShri Abhyankar     }
51549c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
51559c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
51569c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
515776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
51587453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
51597453f775SEmil Constantinescu       tola = ts->atol;
51607453f775SEmil Constantinescu       if (tola>0.) {
51617453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
51627453f775SEmil Constantinescu         na_loc++;
51637453f775SEmil Constantinescu       }
51647453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51657453f775SEmil Constantinescu       if (tolr>0.) {
51667453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
51677453f775SEmil Constantinescu         nr_loc++;
51687453f775SEmil Constantinescu       }
51697453f775SEmil Constantinescu       tol=tola+tolr;
51707453f775SEmil Constantinescu       if (tol>0.) {
51717453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
51727453f775SEmil Constantinescu         n_loc++;
51737453f775SEmil Constantinescu       }
51749c6b16b5SShri Abhyankar     }
51759c6b16b5SShri Abhyankar   }
51769c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
51779c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
51789c6b16b5SShri Abhyankar 
51797453f775SEmil Constantinescu   err_loc[0] = sum;
51807453f775SEmil Constantinescu   err_loc[1] = suma;
51817453f775SEmil Constantinescu   err_loc[2] = sumr;
51827453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
51837453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
51847453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
51857453f775SEmil Constantinescu 
5186820f2d46SBarry Smith   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr);
51877453f775SEmil Constantinescu 
51887453f775SEmil Constantinescu   gsum   = err_glb[0];
51897453f775SEmil Constantinescu   gsuma  = err_glb[1];
51907453f775SEmil Constantinescu   gsumr  = err_glb[2];
51917453f775SEmil Constantinescu   n_glb  = err_glb[3];
51927453f775SEmil Constantinescu   na_glb = err_glb[4];
51937453f775SEmil Constantinescu   nr_glb = err_glb[5];
51947453f775SEmil Constantinescu 
5195b1316ef9SEmil Constantinescu   *norm  = 0.;
5196b1316ef9SEmil Constantinescu   if (n_glb>0.) {*norm  = PetscSqrtReal(gsum  / n_glb);}
5197b1316ef9SEmil Constantinescu   *norma = 0.;
5198b1316ef9SEmil Constantinescu   if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);}
5199b1316ef9SEmil Constantinescu   *normr = 0.;
5200b1316ef9SEmil Constantinescu   if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);}
52019c6b16b5SShri Abhyankar 
52022c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
52032c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
52042c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
52059c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
52069c6b16b5SShri Abhyankar }
52079c6b16b5SShri Abhyankar 
52089c6b16b5SShri Abhyankar /*@
5209a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
52109c6b16b5SShri Abhyankar 
52119c6b16b5SShri Abhyankar    Collective on TS
52129c6b16b5SShri Abhyankar 
52134165533cSJose E. Roman    Input Parameters:
52149c6b16b5SShri Abhyankar +  ts - time stepping context
5215a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5216a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
52179c6b16b5SShri Abhyankar 
52184165533cSJose E. Roman    Output Parameters:
5219a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
52207453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5221a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
52229c6b16b5SShri Abhyankar 
52239c6b16b5SShri Abhyankar    Level: developer
52249c6b16b5SShri Abhyankar 
5225deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
52269c6b16b5SShri Abhyankar @*/
52277453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
52289c6b16b5SShri Abhyankar {
52299c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
52307453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
52319c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
52327453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
52337453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
52347453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
52359c6b16b5SShri Abhyankar 
52369c6b16b5SShri Abhyankar   PetscFunctionBegin;
52379c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5238a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5239a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5240a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5241a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5242a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5243a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
52448a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
52458a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
52462c71b3e2SJacob Faibussowitsch   PetscCheckFalse(U == Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
52479c6b16b5SShri Abhyankar 
52489c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
52499c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
52509c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
52519c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
52529c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
52537453f775SEmil Constantinescu 
52547453f775SEmil Constantinescu   max=0.;
52557453f775SEmil Constantinescu   maxa=0.;
52567453f775SEmil Constantinescu   maxr=0.;
52577453f775SEmil Constantinescu 
52587453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
52599c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
52609c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52619c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52627453f775SEmil Constantinescu 
52637453f775SEmil Constantinescu     for (i=0; i<n; i++) {
526476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52657453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52667453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
52677453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52687453f775SEmil Constantinescu       tol  = tola+tolr;
52697453f775SEmil Constantinescu       if (tola>0.) {
52707453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52717453f775SEmil Constantinescu       }
52727453f775SEmil Constantinescu       if (tolr>0.) {
52737453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52747453f775SEmil Constantinescu       }
52757453f775SEmil Constantinescu       if (tol>0.) {
52767453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52777453f775SEmil Constantinescu       }
52789c6b16b5SShri Abhyankar     }
52799c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52809c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52819c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
52829c6b16b5SShri Abhyankar     const PetscScalar *atol;
52839c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52847453f775SEmil Constantinescu     for (i=0; i<n; i++) {
528576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
52867453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
52877453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
52887453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52897453f775SEmil Constantinescu       tol  = tola+tolr;
52907453f775SEmil Constantinescu       if (tola>0.) {
52917453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
52927453f775SEmil Constantinescu       }
52937453f775SEmil Constantinescu       if (tolr>0.) {
52947453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
52957453f775SEmil Constantinescu       }
52967453f775SEmil Constantinescu       if (tol>0.) {
52977453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
52987453f775SEmil Constantinescu       }
52999c6b16b5SShri Abhyankar     }
53009c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53019c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
53029c6b16b5SShri Abhyankar     const PetscScalar *rtol;
53039c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53047453f775SEmil Constantinescu 
53057453f775SEmil Constantinescu     for (i=0; i<n; i++) {
530676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
53077453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
53087453f775SEmil Constantinescu       tola = ts->atol;
53097453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53107453f775SEmil Constantinescu       tol  = tola+tolr;
53117453f775SEmil Constantinescu       if (tola>0.) {
53127453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
53137453f775SEmil Constantinescu       }
53147453f775SEmil Constantinescu       if (tolr>0.) {
53157453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
53167453f775SEmil Constantinescu       }
53177453f775SEmil Constantinescu       if (tol>0.) {
53187453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
53197453f775SEmil Constantinescu       }
53209c6b16b5SShri Abhyankar     }
53219c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53229c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
53237453f775SEmil Constantinescu 
53247453f775SEmil Constantinescu     for (i=0; i<n; i++) {
532576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
53267453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
53277453f775SEmil Constantinescu       tola = ts->atol;
53287453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53297453f775SEmil Constantinescu       tol  = tola+tolr;
53307453f775SEmil Constantinescu       if (tola>0.) {
53317453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
53327453f775SEmil Constantinescu       }
53337453f775SEmil Constantinescu       if (tolr>0.) {
53347453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
53357453f775SEmil Constantinescu       }
53367453f775SEmil Constantinescu       if (tol>0.) {
53377453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
53387453f775SEmil Constantinescu       }
53399c6b16b5SShri Abhyankar     }
53409c6b16b5SShri Abhyankar   }
53419c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
53429c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
53437453f775SEmil Constantinescu   err_loc[0] = max;
53447453f775SEmil Constantinescu   err_loc[1] = maxa;
53457453f775SEmil Constantinescu   err_loc[2] = maxr;
5346820f2d46SBarry Smith   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr);
53477453f775SEmil Constantinescu   gmax   = err_glb[0];
53487453f775SEmil Constantinescu   gmaxa  = err_glb[1];
53497453f775SEmil Constantinescu   gmaxr  = err_glb[2];
53509c6b16b5SShri Abhyankar 
53519c6b16b5SShri Abhyankar   *norm = gmax;
53527453f775SEmil Constantinescu   *norma = gmaxa;
53537453f775SEmil Constantinescu   *normr = gmaxr;
53542c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
53552c71b3e2SJacob Faibussowitsch     PetscCheckFalse(PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
53562c71b3e2SJacob Faibussowitsch     PetscCheckFalse(PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
53579c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
53589c6b16b5SShri Abhyankar }
53599c6b16b5SShri Abhyankar 
53601c3436cfSJed Brown /*@
53618a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
53621c3436cfSJed Brown 
53631c3436cfSJed Brown    Collective on TS
53641c3436cfSJed Brown 
53654165533cSJose E. Roman    Input Parameters:
53661c3436cfSJed Brown +  ts - time stepping context
5367a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5368a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5369a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
53707619abb3SShri 
53714165533cSJose E. Roman    Output Parameters:
5372a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
53738a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5374a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5375a4868fbcSLisandro Dalcin 
5376a4868fbcSLisandro Dalcin    Options Database Keys:
5377a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5378a4868fbcSLisandro Dalcin 
53791c3436cfSJed Brown    Level: developer
53801c3436cfSJed Brown 
53818a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
53821c3436cfSJed Brown @*/
53837453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53841c3436cfSJed Brown {
53858beabaa1SBarry Smith   PetscErrorCode ierr;
53861c3436cfSJed Brown 
53871c3436cfSJed Brown   PetscFunctionBegin;
5388a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
53897453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5390a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
53917453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
539298921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
53931c3436cfSJed Brown   PetscFunctionReturn(0);
53941c3436cfSJed Brown }
53951c3436cfSJed Brown 
53968a175baeSEmil Constantinescu /*@
53978a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
53988a175baeSEmil Constantinescu 
53998a175baeSEmil Constantinescu    Collective on TS
54008a175baeSEmil Constantinescu 
54014165533cSJose E. Roman    Input Parameters:
54028a175baeSEmil Constantinescu +  ts - time stepping context
54038a175baeSEmil Constantinescu .  E - error vector
54048a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
54058a175baeSEmil Constantinescu -  Y - state vector, previous time step
54068a175baeSEmil Constantinescu 
54074165533cSJose E. Roman    Output Parameters:
5408a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
54098a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5410a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
54118a175baeSEmil Constantinescu 
54128a175baeSEmil Constantinescu    Level: developer
54138a175baeSEmil Constantinescu 
54148a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
54158a175baeSEmil Constantinescu @*/
54168a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
54178a175baeSEmil Constantinescu {
54188a175baeSEmil Constantinescu   PetscErrorCode    ierr;
54198a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
54208a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
54218a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
54228a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
54238a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
54248a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
54258a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
54268a175baeSEmil Constantinescu 
54278a175baeSEmil Constantinescu   PetscFunctionBegin;
54288a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54298a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
54308a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
54318a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
54328a175baeSEmil Constantinescu   PetscValidType(E,2);
54338a175baeSEmil Constantinescu   PetscValidType(U,3);
54348a175baeSEmil Constantinescu   PetscValidType(Y,4);
54358a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
5436064a246eSJacob Faibussowitsch   PetscCheckSameComm(U,3,Y,4);
54378a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
54388a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
54398a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
54408a175baeSEmil Constantinescu 
54418a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
54428a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
54438a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
54448a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
54458a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54468a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54478a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
54488a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
54498a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
54508a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
54518a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
54528a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54538a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54548a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
545576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54568a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54578a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
54588a175baeSEmil Constantinescu       if (tola>0.) {
54598a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54608a175baeSEmil Constantinescu         na_loc++;
54618a175baeSEmil Constantinescu       }
54628a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54638a175baeSEmil Constantinescu       if (tolr>0.) {
54648a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54658a175baeSEmil Constantinescu         nr_loc++;
54668a175baeSEmil Constantinescu       }
54678a175baeSEmil Constantinescu       tol=tola+tolr;
54688a175baeSEmil Constantinescu       if (tol>0.) {
54698a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54708a175baeSEmil Constantinescu         n_loc++;
54718a175baeSEmil Constantinescu       }
54728a175baeSEmil Constantinescu     }
54738a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54748a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54758a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54768a175baeSEmil Constantinescu     const PetscScalar *atol;
54778a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54788a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
547976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54808a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
54818a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
54828a175baeSEmil Constantinescu       if (tola>0.) {
54838a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
54848a175baeSEmil Constantinescu         na_loc++;
54858a175baeSEmil Constantinescu       }
54868a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54878a175baeSEmil Constantinescu       if (tolr>0.) {
54888a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
54898a175baeSEmil Constantinescu         nr_loc++;
54908a175baeSEmil Constantinescu       }
54918a175baeSEmil Constantinescu       tol=tola+tolr;
54928a175baeSEmil Constantinescu       if (tol>0.) {
54938a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
54948a175baeSEmil Constantinescu         n_loc++;
54958a175baeSEmil Constantinescu       }
54968a175baeSEmil Constantinescu     }
54978a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54988a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54998a175baeSEmil Constantinescu     const PetscScalar *rtol;
55008a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55018a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
550276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55038a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
55048a175baeSEmil Constantinescu       tola = ts->atol;
55058a175baeSEmil Constantinescu       if (tola>0.) {
55068a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
55078a175baeSEmil Constantinescu         na_loc++;
55088a175baeSEmil Constantinescu       }
55098a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55108a175baeSEmil Constantinescu       if (tolr>0.) {
55118a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
55128a175baeSEmil Constantinescu         nr_loc++;
55138a175baeSEmil Constantinescu       }
55148a175baeSEmil Constantinescu       tol=tola+tolr;
55158a175baeSEmil Constantinescu       if (tol>0.) {
55168a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
55178a175baeSEmil Constantinescu         n_loc++;
55188a175baeSEmil Constantinescu       }
55198a175baeSEmil Constantinescu     }
55208a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55218a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
55228a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
552376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55248a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
55258a175baeSEmil Constantinescu       tola = ts->atol;
55268a175baeSEmil Constantinescu       if (tola>0.) {
55278a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
55288a175baeSEmil Constantinescu         na_loc++;
55298a175baeSEmil Constantinescu       }
55308a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55318a175baeSEmil Constantinescu       if (tolr>0.) {
55328a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
55338a175baeSEmil Constantinescu         nr_loc++;
55348a175baeSEmil Constantinescu       }
55358a175baeSEmil Constantinescu       tol=tola+tolr;
55368a175baeSEmil Constantinescu       if (tol>0.) {
55378a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
55388a175baeSEmil Constantinescu         n_loc++;
55398a175baeSEmil Constantinescu       }
55408a175baeSEmil Constantinescu     }
55418a175baeSEmil Constantinescu   }
55428a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
55438a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
55448a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
55458a175baeSEmil Constantinescu 
55468a175baeSEmil Constantinescu   err_loc[0] = sum;
55478a175baeSEmil Constantinescu   err_loc[1] = suma;
55488a175baeSEmil Constantinescu   err_loc[2] = sumr;
55498a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
55508a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
55518a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
55528a175baeSEmil Constantinescu 
5553820f2d46SBarry Smith   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr);
55548a175baeSEmil Constantinescu 
55558a175baeSEmil Constantinescu   gsum   = err_glb[0];
55568a175baeSEmil Constantinescu   gsuma  = err_glb[1];
55578a175baeSEmil Constantinescu   gsumr  = err_glb[2];
55588a175baeSEmil Constantinescu   n_glb  = err_glb[3];
55598a175baeSEmil Constantinescu   na_glb = err_glb[4];
55608a175baeSEmil Constantinescu   nr_glb = err_glb[5];
55618a175baeSEmil Constantinescu 
55628a175baeSEmil Constantinescu   *norm  = 0.;
55638a175baeSEmil Constantinescu   if (n_glb>0.) {*norm  = PetscSqrtReal(gsum  / n_glb);}
55648a175baeSEmil Constantinescu   *norma = 0.;
55658a175baeSEmil Constantinescu   if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);}
55668a175baeSEmil Constantinescu   *normr = 0.;
55678a175baeSEmil Constantinescu   if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);}
55688a175baeSEmil Constantinescu 
55692c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55702c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
55712c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
55728a175baeSEmil Constantinescu   PetscFunctionReturn(0);
55738a175baeSEmil Constantinescu }
55748a175baeSEmil Constantinescu 
55758a175baeSEmil Constantinescu /*@
55768a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
55778a175baeSEmil Constantinescu    Collective on TS
55788a175baeSEmil Constantinescu 
55794165533cSJose E. Roman    Input Parameters:
55808a175baeSEmil Constantinescu +  ts - time stepping context
55818a175baeSEmil Constantinescu .  E - error vector
55828a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
55838a175baeSEmil Constantinescu -  Y - state vector, previous time step
55848a175baeSEmil Constantinescu 
55854165533cSJose E. Roman    Output Parameters:
5586a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55878a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5588a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55898a175baeSEmil Constantinescu 
55908a175baeSEmil Constantinescu    Level: developer
55918a175baeSEmil Constantinescu 
55928a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
55938a175baeSEmil Constantinescu @*/
55948a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55958a175baeSEmil Constantinescu {
55968a175baeSEmil Constantinescu   PetscErrorCode    ierr;
55978a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
55988a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
55998a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
56008a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
56018a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
56028a175baeSEmil Constantinescu 
56038a175baeSEmil Constantinescu   PetscFunctionBegin;
56048a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56058a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
56068a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
56078a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
56088a175baeSEmil Constantinescu   PetscValidType(E,2);
56098a175baeSEmil Constantinescu   PetscValidType(U,3);
56108a175baeSEmil Constantinescu   PetscValidType(Y,4);
56118a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
5612064a246eSJacob Faibussowitsch   PetscCheckSameComm(U,3,Y,4);
56138a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
56148a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
56158a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
56168a175baeSEmil Constantinescu 
56178a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
56188a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
56198a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
56208a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
56218a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
56228a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
56238a175baeSEmil Constantinescu 
56248a175baeSEmil Constantinescu   max=0.;
56258a175baeSEmil Constantinescu   maxa=0.;
56268a175baeSEmil Constantinescu   maxr=0.;
56278a175baeSEmil Constantinescu 
56288a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
56298a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
56308a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56318a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56328a175baeSEmil Constantinescu 
56338a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
563476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56358a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
56368a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
56378a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56388a175baeSEmil Constantinescu       tol  = tola+tolr;
56398a175baeSEmil Constantinescu       if (tola>0.) {
56408a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
56418a175baeSEmil Constantinescu       }
56428a175baeSEmil Constantinescu       if (tolr>0.) {
56438a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
56448a175baeSEmil Constantinescu       }
56458a175baeSEmil Constantinescu       if (tol>0.) {
56468a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
56478a175baeSEmil Constantinescu       }
56488a175baeSEmil Constantinescu     }
56498a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56508a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56518a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
56528a175baeSEmil Constantinescu     const PetscScalar *atol;
56538a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56548a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
565576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56568a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
56578a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
56588a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56598a175baeSEmil Constantinescu       tol  = tola+tolr;
56608a175baeSEmil Constantinescu       if (tola>0.) {
56618a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
56628a175baeSEmil Constantinescu       }
56638a175baeSEmil Constantinescu       if (tolr>0.) {
56648a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
56658a175baeSEmil Constantinescu       }
56668a175baeSEmil Constantinescu       if (tol>0.) {
56678a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
56688a175baeSEmil Constantinescu       }
56698a175baeSEmil Constantinescu     }
56708a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56718a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
56728a175baeSEmil Constantinescu     const PetscScalar *rtol;
56738a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56748a175baeSEmil Constantinescu 
56758a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
567676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56778a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
56788a175baeSEmil Constantinescu       tola = ts->atol;
56798a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56808a175baeSEmil Constantinescu       tol  = tola+tolr;
56818a175baeSEmil Constantinescu       if (tola>0.) {
56828a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
56838a175baeSEmil Constantinescu       }
56848a175baeSEmil Constantinescu       if (tolr>0.) {
56858a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
56868a175baeSEmil Constantinescu       }
56878a175baeSEmil Constantinescu       if (tol>0.) {
56888a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
56898a175baeSEmil Constantinescu       }
56908a175baeSEmil Constantinescu     }
56918a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56928a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
56938a175baeSEmil Constantinescu 
56948a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
569576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56968a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
56978a175baeSEmil Constantinescu       tola = ts->atol;
56988a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56998a175baeSEmil Constantinescu       tol  = tola+tolr;
57008a175baeSEmil Constantinescu       if (tola>0.) {
57018a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
57028a175baeSEmil Constantinescu       }
57038a175baeSEmil Constantinescu       if (tolr>0.) {
57048a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
57058a175baeSEmil Constantinescu       }
57068a175baeSEmil Constantinescu       if (tol>0.) {
57078a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
57088a175baeSEmil Constantinescu       }
57098a175baeSEmil Constantinescu     }
57108a175baeSEmil Constantinescu   }
57118a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
57128a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
57138a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
57148a175baeSEmil Constantinescu   err_loc[0] = max;
57158a175baeSEmil Constantinescu   err_loc[1] = maxa;
57168a175baeSEmil Constantinescu   err_loc[2] = maxr;
5717820f2d46SBarry Smith   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr);
57188a175baeSEmil Constantinescu   gmax   = err_glb[0];
57198a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
57208a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
57218a175baeSEmil Constantinescu 
57228a175baeSEmil Constantinescu   *norm = gmax;
57238a175baeSEmil Constantinescu   *norma = gmaxa;
57248a175baeSEmil Constantinescu   *normr = gmaxr;
57252c71b3e2SJacob Faibussowitsch   PetscCheckFalse(PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
57262c71b3e2SJacob Faibussowitsch     PetscCheckFalse(PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
57272c71b3e2SJacob Faibussowitsch     PetscCheckFalse(PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
57288a175baeSEmil Constantinescu   PetscFunctionReturn(0);
57298a175baeSEmil Constantinescu }
57308a175baeSEmil Constantinescu 
57318a175baeSEmil Constantinescu /*@
57328a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
57338a175baeSEmil Constantinescu 
57348a175baeSEmil Constantinescu    Collective on TS
57358a175baeSEmil Constantinescu 
57364165533cSJose E. Roman    Input Parameters:
57378a175baeSEmil Constantinescu +  ts - time stepping context
57388a175baeSEmil Constantinescu .  E - error vector
57398a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
57408a175baeSEmil Constantinescu .  Y - state vector, previous time step
57418a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
57428a175baeSEmil Constantinescu 
57434165533cSJose E. Roman    Output Parameters:
5744a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
57458a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
5746a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
57478a175baeSEmil Constantinescu 
57488a175baeSEmil Constantinescu    Options Database Keys:
57498a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
57508a175baeSEmil Constantinescu 
57518a175baeSEmil Constantinescu    Level: developer
57528a175baeSEmil Constantinescu 
57538a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
57548a175baeSEmil Constantinescu @*/
57558a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57568a175baeSEmil Constantinescu {
57578a175baeSEmil Constantinescu   PetscErrorCode ierr;
57588a175baeSEmil Constantinescu 
57598a175baeSEmil Constantinescu   PetscFunctionBegin;
57608a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
57618a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
57628a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
57638a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
576498921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
57658a175baeSEmil Constantinescu   PetscFunctionReturn(0);
57668a175baeSEmil Constantinescu }
57678a175baeSEmil Constantinescu 
57688d59e960SJed Brown /*@
57698d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
57708d59e960SJed Brown 
57718d59e960SJed Brown    Logically Collective on TS
57728d59e960SJed Brown 
57734165533cSJose E. Roman    Input Parameters:
57748d59e960SJed Brown +  ts - time stepping context
57758d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
57768d59e960SJed Brown 
57778d59e960SJed Brown    Note:
57788d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
57798d59e960SJed Brown 
57808d59e960SJed Brown    Level: intermediate
57818d59e960SJed Brown 
57828d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
57838d59e960SJed Brown @*/
57848d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
57858d59e960SJed Brown {
57868d59e960SJed Brown   PetscFunctionBegin;
57878d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57888d59e960SJed Brown   ts->cfltime_local = cfltime;
57898d59e960SJed Brown   ts->cfltime       = -1.;
57908d59e960SJed Brown   PetscFunctionReturn(0);
57918d59e960SJed Brown }
57928d59e960SJed Brown 
57938d59e960SJed Brown /*@
57948d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
57958d59e960SJed Brown 
57968d59e960SJed Brown    Collective on TS
57978d59e960SJed Brown 
57984165533cSJose E. Roman    Input Parameter:
57998d59e960SJed Brown .  ts - time stepping context
58008d59e960SJed Brown 
58014165533cSJose E. Roman    Output Parameter:
58028d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
58038d59e960SJed Brown 
58048d59e960SJed Brown    Level: advanced
58058d59e960SJed Brown 
58068d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
58078d59e960SJed Brown @*/
58088d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
58098d59e960SJed Brown {
58108d59e960SJed Brown   PetscErrorCode ierr;
58118d59e960SJed Brown 
58128d59e960SJed Brown   PetscFunctionBegin;
58138d59e960SJed Brown   if (ts->cfltime < 0) {
5814820f2d46SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr);
58158d59e960SJed Brown   }
58168d59e960SJed Brown   *cfltime = ts->cfltime;
58178d59e960SJed Brown   PetscFunctionReturn(0);
58188d59e960SJed Brown }
58198d59e960SJed Brown 
5820d6ebe24aSShri Abhyankar /*@
5821d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5822d6ebe24aSShri Abhyankar 
5823d6ebe24aSShri Abhyankar    Input Parameters:
5824a2b725a8SWilliam Gropp +  ts   - the TS context.
5825d6ebe24aSShri Abhyankar .  xl   - lower bound.
5826a2b725a8SWilliam Gropp -  xu   - upper bound.
5827d6ebe24aSShri Abhyankar 
5828d6ebe24aSShri Abhyankar    Notes:
5829d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5830e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5831d6ebe24aSShri Abhyankar 
58322bd2b0e6SSatish Balay    Level: advanced
58332bd2b0e6SSatish Balay 
5834d6ebe24aSShri Abhyankar @*/
5835d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5836d6ebe24aSShri Abhyankar {
5837d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5838d6ebe24aSShri Abhyankar   SNES           snes;
5839d6ebe24aSShri Abhyankar 
5840d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5841d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5842d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5843d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5844d6ebe24aSShri Abhyankar }
5845d6ebe24aSShri Abhyankar 
5846f9c1d6abSBarry Smith /*@
5847f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5848f9c1d6abSBarry Smith 
5849d083f849SBarry Smith    Collective on TS
5850f9c1d6abSBarry Smith 
5851f9c1d6abSBarry Smith    Input Parameters:
5852f9c1d6abSBarry Smith +  ts - the TS context
5853f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5854f9c1d6abSBarry Smith 
5855f9c1d6abSBarry Smith    Output Parameters:
5856f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5857f9c1d6abSBarry Smith 
5858f9c1d6abSBarry Smith    Level: developer
5859f9c1d6abSBarry Smith 
5860f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5861f9c1d6abSBarry Smith @*/
5862f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5863f9c1d6abSBarry Smith {
5864f9c1d6abSBarry Smith   PetscErrorCode ierr;
5865f9c1d6abSBarry Smith 
5866f9c1d6abSBarry Smith   PetscFunctionBegin;
5867f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58682c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!ts->ops->linearstability,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5869f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5870f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5871f9c1d6abSBarry Smith }
587224655328SShri 
587324655328SShri /*@
5874dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
5875dcb233daSLisandro Dalcin 
5876dcb233daSLisandro Dalcin    Collective on TS
5877dcb233daSLisandro Dalcin 
5878dcb233daSLisandro Dalcin    Input Parameter:
5879dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
5880dcb233daSLisandro Dalcin 
5881dcb233daSLisandro Dalcin    Level: advanced
5882dcb233daSLisandro Dalcin 
5883dcb233daSLisandro Dalcin    Notes:
5884dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
5885dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
5886dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
5887dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
5888dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
5889dcb233daSLisandro Dalcin    discontinuous source terms).
5890dcb233daSLisandro Dalcin 
5891dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
5892dcb233daSLisandro Dalcin @*/
5893dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
5894dcb233daSLisandro Dalcin {
5895dcb233daSLisandro Dalcin   PetscFunctionBegin;
5896dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5897dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
5898dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
5899dcb233daSLisandro Dalcin }
5900dcb233daSLisandro Dalcin 
5901dcb233daSLisandro Dalcin /*@
590224655328SShri    TSRollBack - Rolls back one time step
590324655328SShri 
590424655328SShri    Collective on TS
590524655328SShri 
590624655328SShri    Input Parameter:
590724655328SShri .  ts - the TS context obtained from TSCreate()
590824655328SShri 
590924655328SShri    Level: advanced
591024655328SShri 
591124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
591224655328SShri @*/
591324655328SShri PetscErrorCode  TSRollBack(TS ts)
591424655328SShri {
591524655328SShri   PetscErrorCode ierr;
591624655328SShri 
591724655328SShri   PetscFunctionBegin;
591824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
59192c71b3e2SJacob Faibussowitsch   PetscCheckFalse(ts->steprollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
59202c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!ts->ops->rollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
592124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
592224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
592324655328SShri   ts->ptime = ts->ptime_prev;
5924be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
59252808aa04SLisandro Dalcin   ts->steps--;
5926b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
592724655328SShri   PetscFunctionReturn(0);
592824655328SShri }
5929aeb4809dSShri Abhyankar 
5930ff22ae23SHong Zhang /*@
5931ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5932ff22ae23SHong Zhang 
5933ff22ae23SHong Zhang    Input Parameter:
5934ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5935ff22ae23SHong Zhang 
59360429704eSStefano Zampini    Output Parameters:
59370429704eSStefano Zampini +  ns - the number of stages
59380429704eSStefano Zampini -  Y - the current stage vectors
59390429704eSStefano Zampini 
5940ff22ae23SHong Zhang    Level: advanced
5941ff22ae23SHong Zhang 
59420429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
59430429704eSStefano Zampini 
5944ff22ae23SHong Zhang .seealso: TSCreate()
5945ff22ae23SHong Zhang @*/
5946ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
5947ff22ae23SHong Zhang {
5948ff22ae23SHong Zhang   PetscErrorCode ierr;
5949ff22ae23SHong Zhang 
5950ff22ae23SHong Zhang   PetscFunctionBegin;
5951ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
59520429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
59530429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
59540429704eSStefano Zampini   if (!ts->ops->getstages) {
59550429704eSStefano Zampini     if (ns) *ns = 0;
59560429704eSStefano Zampini     if (Y) *Y = NULL;
59570429704eSStefano Zampini   } else {
5958ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
5959ff22ae23SHong Zhang   }
5960ff22ae23SHong Zhang   PetscFunctionReturn(0);
5961ff22ae23SHong Zhang }
5962ff22ae23SHong Zhang 
5963847ff0e1SMatthew G. Knepley /*@C
5964847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
5965847ff0e1SMatthew G. Knepley 
5966847ff0e1SMatthew G. Knepley   Collective on SNES
5967847ff0e1SMatthew G. Knepley 
5968847ff0e1SMatthew G. Knepley   Input Parameters:
5969847ff0e1SMatthew G. Knepley + ts - the TS context
5970847ff0e1SMatthew G. Knepley . t - current timestep
5971847ff0e1SMatthew G. Knepley . U - state vector
5972847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
5973847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
5974847ff0e1SMatthew G. Knepley - ctx - an optional user context
5975847ff0e1SMatthew G. Knepley 
5976847ff0e1SMatthew G. Knepley   Output Parameters:
5977847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
5978847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
5979847ff0e1SMatthew G. Knepley 
5980847ff0e1SMatthew G. Knepley   Level: intermediate
5981847ff0e1SMatthew G. Knepley 
5982847ff0e1SMatthew G. Knepley   Notes:
5983847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
5984847ff0e1SMatthew G. Knepley 
5985847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
5986847ff0e1SMatthew G. Knepley 
5987847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
5988847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
5989847ff0e1SMatthew G. Knepley 
5990847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
5991847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
5992847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
5993847ff0e1SMatthew G. Knepley 
5994847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
5995847ff0e1SMatthew G. Knepley @*/
5996847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
5997847ff0e1SMatthew G. Knepley {
5998847ff0e1SMatthew G. Knepley   SNES           snes;
5999847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6000847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6001847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6002847ff0e1SMatthew G. Knepley 
6003847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6004c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6005847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6006847ff0e1SMatthew G. Knepley   if (!color) {
6007847ff0e1SMatthew G. Knepley     DM         dm;
6008847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6009847ff0e1SMatthew G. Knepley 
6010847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6011847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6012847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6013847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6014847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6015847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6016847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6017847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6018847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6019847ff0e1SMatthew G. Knepley     } else {
6020847ff0e1SMatthew G. Knepley       MatColoring mc;
6021847ff0e1SMatthew G. Knepley 
6022847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
6023847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
6024847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
6025847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
6026847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
6027847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
6028847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6029847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6030847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6031847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6032847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6033847ff0e1SMatthew G. Knepley     }
6034847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
6035847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
6036847ff0e1SMatthew G. Knepley   }
6037847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
6038847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
6039847ff0e1SMatthew G. Knepley   if (J != B) {
6040847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6041847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6042847ff0e1SMatthew G. Knepley   }
6043847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
6044847ff0e1SMatthew G. Knepley }
604593b34091SDebojyoti Ghosh 
6046cb9d8021SPierre Barbier de Reuille /*@
60476bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
6048cb9d8021SPierre Barbier de Reuille 
6049cb9d8021SPierre Barbier de Reuille     Input Parameters:
60506bc98fa9SBarry Smith +    ts - the TS context
60516bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
60526bc98fa9SBarry Smith 
60536bc98fa9SBarry Smith     Calling sequence of func:
60546bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
60556bc98fa9SBarry Smith 
60566bc98fa9SBarry Smith +   ts - the TS context
60576bc98fa9SBarry Smith .   time - the current time (of the stage)
60586bc98fa9SBarry Smith .   state - the state to check if it is valid
60596bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
6060cb9d8021SPierre Barbier de Reuille 
6061cb9d8021SPierre Barbier de Reuille     Level: intermediate
6062cb9d8021SPierre Barbier de Reuille 
60636bc98fa9SBarry Smith     Notes:
60646bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
60656bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
60666bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
60676bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
60686bc98fa9SBarry Smith 
60696bc98fa9SBarry Smith     Developer Notes:
60706bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
60716bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
60726bc98fa9SBarry Smith 
60736bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
6074cb9d8021SPierre Barbier de Reuille @*/
6075cb9d8021SPierre Barbier de Reuille 
6076d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
6077cb9d8021SPierre Barbier de Reuille {
6078cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6079cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6080cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
6081cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6082cb9d8021SPierre Barbier de Reuille }
6083cb9d8021SPierre Barbier de Reuille 
6084cb9d8021SPierre Barbier de Reuille /*@
60856bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
6086cb9d8021SPierre Barbier de Reuille 
6087cb9d8021SPierre Barbier de Reuille     Input Parameters:
60886bc98fa9SBarry Smith +    ts - the TS context
60896bc98fa9SBarry Smith .    stagetime - time of the simulation
60906bc98fa9SBarry Smith -    Y - state vector to check.
6091cb9d8021SPierre Barbier de Reuille 
6092cb9d8021SPierre Barbier de Reuille     Output Parameter:
60936bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
6094cb9d8021SPierre Barbier de Reuille 
6095cb9d8021SPierre Barbier de Reuille     Note:
60966bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
60976bc98fa9SBarry Smith     to check if the current state is valid.
609896a0c994SBarry Smith 
60996bc98fa9SBarry Smith     Level: developer
61006bc98fa9SBarry Smith 
61016bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
6102cb9d8021SPierre Barbier de Reuille @*/
6103d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
6104cb9d8021SPierre Barbier de Reuille {
6105cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6106cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6107cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
6108cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
6109a74df02fSJacob Faibussowitsch     PetscStackCallStandard((*ts->functiondomainerror),ts,stagetime,Y,accept);
6110cb9d8021SPierre Barbier de Reuille   }
6111cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6112cb9d8021SPierre Barbier de Reuille }
61131ceb14c0SBarry Smith 
611493b34091SDebojyoti Ghosh /*@C
6115e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
611693b34091SDebojyoti Ghosh 
6117d083f849SBarry Smith   Collective
611893b34091SDebojyoti Ghosh 
611993b34091SDebojyoti Ghosh   Input Parameter:
612093b34091SDebojyoti Ghosh . tsin    - The input TS
612193b34091SDebojyoti Ghosh 
612293b34091SDebojyoti Ghosh   Output Parameter:
6123e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
612493b34091SDebojyoti Ghosh 
61255eca1a21SEmil Constantinescu   Notes:
61265eca1a21SEmil 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.
61275eca1a21SEmil Constantinescu 
6128928bb9adSStefano 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);
61295eca1a21SEmil Constantinescu 
61305eca1a21SEmil Constantinescu   Level: developer
613193b34091SDebojyoti Ghosh 
6132e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
613393b34091SDebojyoti Ghosh @*/
6134baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
613593b34091SDebojyoti Ghosh {
613693b34091SDebojyoti Ghosh   TS             t;
613793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
6138dc846ba4SSatish Balay   SNES           snes_start;
6139dc846ba4SSatish Balay   DM             dm;
6140dc846ba4SSatish Balay   TSType         type;
614193b34091SDebojyoti Ghosh 
614293b34091SDebojyoti Ghosh   PetscFunctionBegin;
614393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
614493b34091SDebojyoti Ghosh   *tsout = NULL;
614593b34091SDebojyoti Ghosh 
61467a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
614793b34091SDebojyoti Ghosh 
614893b34091SDebojyoti Ghosh   /* General TS description */
614993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
6150d0c080abSJoseph Pusztay   t->monitorFrequency  = 1;
615193b34091SDebojyoti Ghosh   t->setupcalled       = 0;
615293b34091SDebojyoti Ghosh   t->ksp_its           = 0;
615393b34091SDebojyoti Ghosh   t->snes_its          = 0;
615493b34091SDebojyoti Ghosh   t->nwork             = 0;
61557d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
615693b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
615793b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
615893b34091SDebojyoti Ghosh 
615934561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
616034561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
6161d15a3a53SEmil Constantinescu 
616293b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
616393b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
616493b34091SDebojyoti Ghosh 
616593b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
616651699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
616793b34091SDebojyoti Ghosh 
6168e7069c78SShri   t->trajectory = tsin->trajectory;
6169e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
6170e7069c78SShri 
6171e7069c78SShri   t->event = tsin->event;
61726b10a48eSSatish Balay   if (t->event) t->event->refct++;
6173e7069c78SShri 
617493b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
617593b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
6176e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
617793b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
617893b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
617993b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
618093b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
618193b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
618293b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
618393b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
618493b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
618593b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
618693b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
618793b34091SDebojyoti Ghosh 
618893b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
618993b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
619093b34091SDebojyoti Ghosh 
619193b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
619293b34091SDebojyoti Ghosh 
619393b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
619493b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
619593b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
619693b34091SDebojyoti Ghosh 
619793b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
619893b34091SDebojyoti Ghosh 
61990d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
62000d4fed19SBarry Smith     PetscInt i;
62010d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
62020d4fed19SBarry Smith     for (i=0; i<10; i++) {
62030d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
62040d4fed19SBarry Smith     }
62050d4fed19SBarry Smith   }
620693b34091SDebojyoti Ghosh   *tsout = t;
620793b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
620893b34091SDebojyoti Ghosh }
6209f3b1f45cSBarry Smith 
6210f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
6211f3b1f45cSBarry Smith {
6212f3b1f45cSBarry Smith   PetscErrorCode ierr;
6213f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
6214f3b1f45cSBarry Smith 
6215f3b1f45cSBarry Smith   PetscFunctionBegin;
6216f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
6217f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6218f3b1f45cSBarry Smith }
6219f3b1f45cSBarry Smith 
6220f3b1f45cSBarry Smith /*@
6221f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
6222f3b1f45cSBarry Smith 
6223d083f849SBarry Smith    Logically Collective on TS
6224f3b1f45cSBarry Smith 
6225f3b1f45cSBarry Smith     Input Parameters:
6226f3b1f45cSBarry Smith     TS - the time stepping routine
6227f3b1f45cSBarry Smith 
6228f3b1f45cSBarry Smith    Output Parameter:
6229f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
6230f3b1f45cSBarry Smith 
6231f3b1f45cSBarry Smith    Options Database:
6232f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
6233f3b1f45cSBarry Smith 
6234f3b1f45cSBarry Smith    Level: advanced
6235f3b1f45cSBarry Smith 
623695452b02SPatrick Sanan    Notes:
623795452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
6238f3b1f45cSBarry Smith 
6239f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
6240f3b1f45cSBarry Smith @*/
6241f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
6242f3b1f45cSBarry Smith {
6243f3b1f45cSBarry Smith   Mat            J,B;
6244f3b1f45cSBarry Smith   PetscErrorCode ierr;
6245f3b1f45cSBarry Smith   TSRHSJacobian  func;
6246f3b1f45cSBarry Smith   void*          ctx;
6247f3b1f45cSBarry Smith 
6248f3b1f45cSBarry Smith   PetscFunctionBegin;
6249f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
6250f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
6251f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
6252f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6253f3b1f45cSBarry Smith }
6254f3b1f45cSBarry Smith 
6255f3b1f45cSBarry Smith /*@C
6256f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
6257f3b1f45cSBarry Smith 
6258d083f849SBarry Smith    Logically Collective on TS
6259f3b1f45cSBarry Smith 
6260f3b1f45cSBarry Smith     Input Parameters:
6261f3b1f45cSBarry Smith     TS - the time stepping routine
6262f3b1f45cSBarry Smith 
6263f3b1f45cSBarry Smith    Output Parameter:
6264f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
6265f3b1f45cSBarry Smith 
6266f3b1f45cSBarry Smith    Options Database:
6267f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
6268f3b1f45cSBarry Smith 
626995452b02SPatrick Sanan    Notes:
627095452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
6271f3b1f45cSBarry Smith 
6272f3b1f45cSBarry Smith    Level: advanced
6273f3b1f45cSBarry Smith 
6274f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
6275f3b1f45cSBarry Smith @*/
6276f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
6277f3b1f45cSBarry Smith {
6278f3b1f45cSBarry Smith   Mat            J,B;
6279f3b1f45cSBarry Smith   PetscErrorCode ierr;
6280f3b1f45cSBarry Smith   void           *ctx;
6281f3b1f45cSBarry Smith   TSRHSJacobian  func;
6282f3b1f45cSBarry Smith 
6283f3b1f45cSBarry Smith   PetscFunctionBegin;
6284f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
6285f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
6286f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
6287f3b1f45cSBarry Smith   PetscFunctionReturn(0);
6288f3b1f45cSBarry Smith }
62890fe4d17eSHong Zhang 
62900fe4d17eSHong Zhang /*@
62910fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
62920fe4d17eSHong Zhang 
62930fe4d17eSHong Zhang   Logically collective
62940fe4d17eSHong Zhang 
6295d8d19677SJose E. Roman   Input Parameters:
62960fe4d17eSHong Zhang +  ts - timestepping context
62970fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
62980fe4d17eSHong Zhang 
62990fe4d17eSHong Zhang   Options Database:
63000fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
63010fe4d17eSHong Zhang 
63020fe4d17eSHong Zhang   Notes:
63030fe4d17eSHong Zhang     This is only useful for multirate methods
63040fe4d17eSHong Zhang 
63050fe4d17eSHong Zhang   Level: intermediate
63060fe4d17eSHong Zhang 
63070fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
63080fe4d17eSHong Zhang @*/
63090fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
63100fe4d17eSHong Zhang {
63110fe4d17eSHong Zhang   PetscFunctionBegin;
63120fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63130fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
63140fe4d17eSHong Zhang   PetscFunctionReturn(0);
63150fe4d17eSHong Zhang }
63160fe4d17eSHong Zhang 
63170fe4d17eSHong Zhang /*@
63180fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
63190fe4d17eSHong Zhang 
63200fe4d17eSHong Zhang   Not collective
63210fe4d17eSHong Zhang 
63220fe4d17eSHong Zhang   Input Parameter:
63230fe4d17eSHong Zhang .  ts - timestepping context
63240fe4d17eSHong Zhang 
63250fe4d17eSHong Zhang   Output Parameter:
63260fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
63270fe4d17eSHong Zhang 
63280fe4d17eSHong Zhang   Level: intermediate
63290fe4d17eSHong Zhang 
63300fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
63310fe4d17eSHong Zhang @*/
63320fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
63330fe4d17eSHong Zhang {
63340fe4d17eSHong Zhang   PetscFunctionBegin;
63350fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63360fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
63370fe4d17eSHong Zhang   PetscFunctionReturn(0);
63380fe4d17eSHong Zhang }
6339d60b7d5cSBarry Smith 
6340d60b7d5cSBarry Smith /*@
6341d60b7d5cSBarry Smith     TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix.
6342d60b7d5cSBarry Smith 
6343d60b7d5cSBarry Smith    Logically  Collective on ts
6344d60b7d5cSBarry Smith 
6345d60b7d5cSBarry Smith    Input Parameters:
6346d60b7d5cSBarry Smith +  ts - the time-stepper
6347d60b7d5cSBarry Smith -  str - the structure (the default is UNKNOWN_NONZERO_PATTERN)
6348d60b7d5cSBarry Smith 
6349d60b7d5cSBarry Smith    Level: intermediate
6350d60b7d5cSBarry Smith 
6351d60b7d5cSBarry Smith    Notes:
6352d60b7d5cSBarry Smith      When the relationship between the nonzero structures is known and supplied the solution process can be much faster
6353d60b7d5cSBarry Smith 
6354d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure
6355d60b7d5cSBarry Smith  @*/
6356d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str)
6357d60b7d5cSBarry Smith {
6358d60b7d5cSBarry Smith   PetscFunctionBegin;
6359d60b7d5cSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6360d60b7d5cSBarry Smith   ts->axpy_pattern = str;
6361d60b7d5cSBarry Smith   PetscFunctionReturn(0);
6362d60b7d5cSBarry Smith }
6363