xref: /petsc/src/ts/interface/ts.c (revision ae692cf2f1f94047cc87437e1050bcbf53f67188)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6900f6b5bSMatthew G. Knepley #include <petscconvest.h>
7d763cef2SBarry Smith 
81c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if (PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;
91c167fc2SEmil Constantinescu 
10d5ba7fb7SMatthew Knepley /* Logging support */
11d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
13d405a339SMatthew Knepley 
14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",NULL};
1549354f04SShri Abhyankar 
161c167fc2SEmil Constantinescu 
17fde5950dSBarry Smith /*@C
18fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
19fde5950dSBarry Smith 
20fde5950dSBarry Smith    Collective on TS
21fde5950dSBarry Smith 
22fde5950dSBarry Smith    Input Parameters:
23fde5950dSBarry Smith +  ts - TS object you wish to monitor
24fde5950dSBarry Smith .  name - the monitor type one is seeking
25fde5950dSBarry Smith .  help - message indicating what monitoring is done
26fde5950dSBarry Smith .  manual - manual page for the monitor
27fde5950dSBarry Smith .  monitor - the monitor function
28fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
29fde5950dSBarry Smith 
30fde5950dSBarry Smith    Level: developer
31fde5950dSBarry Smith 
32fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
33fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
34fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
35fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
36fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
37fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
38fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
39fde5950dSBarry Smith @*/
40721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
41fde5950dSBarry Smith {
42fde5950dSBarry Smith   PetscErrorCode    ierr;
43fde5950dSBarry Smith   PetscViewer       viewer;
44fde5950dSBarry Smith   PetscViewerFormat format;
45fde5950dSBarry Smith   PetscBool         flg;
46fde5950dSBarry Smith 
47fde5950dSBarry Smith   PetscFunctionBegin;
4816413a6aSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
49fde5950dSBarry Smith   if (flg) {
50721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
51721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
52721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
53fde5950dSBarry Smith     if (monitorsetup) {
54721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
55fde5950dSBarry Smith     }
56721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
57fde5950dSBarry Smith   }
58fde5950dSBarry Smith   PetscFunctionReturn(0);
59fde5950dSBarry Smith }
60fde5950dSBarry Smith 
612ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
622ffb9264SLisandro Dalcin {
632ffb9264SLisandro Dalcin   PetscErrorCode ierr;
642ffb9264SLisandro Dalcin 
652ffb9264SLisandro Dalcin   PetscFunctionBegin;
66b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
67b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
682ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
692ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
702ffb9264SLisandro Dalcin   }
712ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
722ffb9264SLisandro Dalcin }
732ffb9264SLisandro Dalcin 
74bdad233fSMatthew Knepley /*@
75bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
76bdad233fSMatthew Knepley 
77bdad233fSMatthew Knepley    Collective on TS
78bdad233fSMatthew Knepley 
79bdad233fSMatthew Knepley    Input Parameter:
80bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
81bdad233fSMatthew Knepley 
82bdad233fSMatthew Knepley    Options Database Keys:
83e49d4f37SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
84ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
85ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
86ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
87ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
884dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
893e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
906c51df0eSBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> - whether to stop at the exact given final time and how to compute the solution at that ti,e
91a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
92a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
93a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
94a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
95a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
96f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
97f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
98ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
99847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
100bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
101de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
102de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1037cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1046934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1058b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
106de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
107de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
108de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
109de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1103e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1113e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
112fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
113e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
1149e336e28SPatrick Sanan -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11553ea634cSHong Zhang 
1163d5a8a6aSBarry Smith    Notes:
1173d5a8a6aSBarry Smith      See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper.
1183d5a8a6aSBarry Smith 
1193d5a8a6aSBarry Smith      Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order
1203d5a8a6aSBarry Smith      to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and
1213d5a8a6aSBarry Smith      -snes_lag_preconditioner_persists true
1223d5a8a6aSBarry Smith 
1239e336e28SPatrick Sanan    Developer Note:
1249e336e28SPatrick Sanan      We should unify all the -ts_monitor options in the way that -xxx_view has been unified
125bdad233fSMatthew Knepley 
126bdad233fSMatthew Knepley    Level: beginner
127bdad233fSMatthew Knepley 
128a313700dSBarry Smith .seealso: TSGetType()
129bdad233fSMatthew Knepley @*/
1307087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
131bdad233fSMatthew Knepley {
132bc952696SBarry Smith   PetscBool              opt,flg,tflg;
133dfbe8321SBarry Smith   PetscErrorCode         ierr;
134eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
13531748224SBarry Smith   PetscReal              time_step;
13649354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
137d1212d36SBarry Smith   char                   dir[16];
138cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1396991f827SBarry Smith   const char             *defaultType;
1406991f827SBarry Smith   char                   typeName[256];
141bdad233fSMatthew Knepley 
142bdad233fSMatthew Knepley   PetscFunctionBegin;
1430700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1446991f827SBarry Smith 
1456991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
146cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
147cd11d68dSLisandro Dalcin 
148cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1491ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1501ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1516991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1526991f827SBarry Smith   if (opt) {
1536991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1546991f827SBarry Smith   } else {
1556991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1566991f827SBarry Smith   }
157bdad233fSMatthew Knepley 
158bdad233fSMatthew Knepley   /* Handle generic TS options */
1594dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
160ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
16119eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
16331748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
164cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
16549354f04SShri 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);
16649354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1670298fd71SBarry 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);
1680298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1690298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1700298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1710298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
172bdad233fSMatthew Knepley 
173f3b1f45cSBarry 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);
174f3b1f45cSBarry 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);
1750fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr);
17656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
17756f85f32SBarry Smith   {
17856f85f32SBarry Smith     PetscBool set;
17956f85f32SBarry Smith     flg  = PETSC_FALSE;
18056f85f32SBarry Smith     ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
18156f85f32SBarry Smith     if (set) {
18256f85f32SBarry Smith       ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
18356f85f32SBarry Smith     }
18456f85f32SBarry Smith   }
18556f85f32SBarry Smith #endif
18656f85f32SBarry Smith 
187bdad233fSMatthew Knepley   /* Monitor options */
188cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
189cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
190fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
191fde5950dSBarry Smith 
192589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr);
1935180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1945180491cSLisandro Dalcin 
1954f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
196b3603a34SBarry Smith   if (opt) {
1970b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1983923b477SBarry Smith     PetscInt       howoften = 1;
199b3603a34SBarry Smith 
2000298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
201c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2024f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
203bdad233fSMatthew Knepley   }
2046ba87a44SLisandro Dalcin 
2054f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
206ef20d060SBarry Smith   if (opt) {
2070b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2083923b477SBarry Smith     PetscInt       howoften = 1;
209ef20d060SBarry Smith 
2100298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
211c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2124f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
213ef20d060SBarry Smith   }
214edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2157cf37e64SBarry Smith 
2166934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2176934998bSLisandro Dalcin   if (opt) {
2186934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2196934998bSLisandro Dalcin     PetscInt       howoften = 1;
2206934998bSLisandro Dalcin 
2216934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2226ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2236934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2246934998bSLisandro Dalcin   }
2258b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2268b668821SLisandro Dalcin   if (opt) {
2278b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2288b668821SLisandro Dalcin     PetscInt       howoften = 1;
2298b668821SLisandro Dalcin 
2308b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2318b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2328b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2338b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2348b668821SLisandro Dalcin   }
2358b668821SLisandro Dalcin 
236201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
237201da799SBarry Smith   if (opt) {
238201da799SBarry Smith     TSMonitorLGCtx ctx;
239201da799SBarry Smith     PetscInt       howoften = 1;
240201da799SBarry Smith 
2410298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2426ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
243201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
244201da799SBarry Smith   }
245201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
246201da799SBarry Smith   if (opt) {
247201da799SBarry Smith     TSMonitorLGCtx ctx;
248201da799SBarry Smith     PetscInt       howoften = 1;
249201da799SBarry Smith 
2500298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2516ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
252201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
253201da799SBarry Smith   }
2548189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2558189c53fSBarry Smith   if (opt) {
2568189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2578189c53fSBarry Smith     PetscInt          howoften = 1;
2588189c53fSBarry Smith 
2590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
260c793f718SLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2618189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2628189c53fSBarry Smith   }
2630ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2641b575b74SJoseph Pusztay   if (opt) {
2651b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2661b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2670ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2681b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2690ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2701b575b74SJoseph Pusztay   }
271ef20d060SBarry Smith   opt  = PETSC_FALSE;
2720dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
273a7cc72afSBarry Smith   if (opt) {
27483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
27583a4ac43SBarry Smith     PetscInt         howoften = 1;
276a80ad3e0SBarry Smith 
2770298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
278c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
27983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
280bdad233fSMatthew Knepley   }
281fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2822d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2832d5ee99bSBarry Smith   if (opt) {
2842d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2852d5ee99bSBarry Smith     PetscReal        bounds[4];
2862d5ee99bSBarry Smith     PetscInt         n = 4;
2872d5ee99bSBarry Smith     PetscDraw        draw;
2886934998bSLisandro Dalcin     PetscDrawAxis    axis;
2892d5ee99bSBarry Smith 
2902d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2912d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
292c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2932d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2946934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2956934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2966934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2972d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2982d5ee99bSBarry Smith   }
2992d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3000dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3013a471f94SBarry Smith   if (opt) {
30283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
30383a4ac43SBarry Smith     PetscInt         howoften = 1;
3043a471f94SBarry Smith 
3050298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
306c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
30783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3083a471f94SBarry Smith   }
3090ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3100ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3110ed3bfb6SBarry Smith   if (opt) {
3120ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3130ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3140ed3bfb6SBarry Smith 
3150ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
316c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3170ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3180ed3bfb6SBarry Smith   }
319fde5950dSBarry Smith 
320ed81e22dSJed Brown   opt  = PETSC_FALSE;
321589a23caSBarry 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);
322ed81e22dSJed Brown   if (flg) {
323ed81e22dSJed Brown     const char *ptr,*ptr2;
324ed81e22dSJed Brown     char       *filetemplate;
325ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
326ed81e22dSJed Brown     /* Do some cursory validation of the input. */
327ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
328ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
329ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
330ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
331ce94432eSBarry Smith       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
332ed81e22dSJed Brown       if (ptr2) break;
333ed81e22dSJed Brown     }
334ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
335ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
336ed81e22dSJed Brown   }
337bdad233fSMatthew Knepley 
338589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
339d1212d36SBarry Smith   if (flg) {
340d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
341d1212d36SBarry Smith     int                  ray = 0;
3423ee9839eSMatthew G. Knepley     DMDirection          ddir;
343d1212d36SBarry Smith     DM                   da;
344d1212d36SBarry Smith     PetscMPIInt          rank;
345d1212d36SBarry Smith 
346ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3473ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3483ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
349ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
350d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
351d1212d36SBarry Smith 
3525bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr);
353b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
354d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
355d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
356ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
357d1212d36SBarry Smith     if (!rank) {
358c793f718SLisandro Dalcin       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
359d1212d36SBarry Smith     }
36051b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
361d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
362d1212d36SBarry Smith   }
363589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
36451b4a12fSMatthew G. Knepley   if (flg) {
36551b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
36651b4a12fSMatthew G. Knepley     int                 ray = 0;
3673ee9839eSMatthew G. Knepley     DMDirection         ddir;
36851b4a12fSMatthew G. Knepley     DM                  da;
36951b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
370d1212d36SBarry Smith 
37151b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3723ee9839eSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DM_X;
3733ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
37451b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
37551b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3761c3436cfSJed Brown 
3775bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr);
378b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
37951b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
38051b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
381c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
38251b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
38351b4a12fSMatthew G. Knepley   }
384a7a1495cSBarry Smith 
385b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
386b3d3934dSBarry Smith   if (opt) {
387b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
388b3d3934dSBarry Smith 
389b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
390b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
391b3d3934dSBarry Smith   }
392b3d3934dSBarry Smith 
393847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
394847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
395847ff0e1SMatthew G. Knepley   if (flg) {
396847ff0e1SMatthew G. Knepley     DM   dm;
397847ff0e1SMatthew G. Knepley     DMTS tdm;
398847ff0e1SMatthew G. Knepley 
399847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
400847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
401847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
402c793f718SLisandro Dalcin     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL);CHKERRQ(ierr);
403847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
404847ff0e1SMatthew G. Knepley   }
405847ff0e1SMatthew G. Knepley 
406d763cef2SBarry Smith   /* Handle specific TS options */
407d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4086991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
409d763cef2SBarry Smith   }
410fbc52257SHong Zhang 
411a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
412a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
413a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
414a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
415a7bdc993SLisandro Dalcin 
41668bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4174f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
41868bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
41968bece0bSHong Zhang   if (tflg) {
42068bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
42168bece0bSHong Zhang   }
422a05bf03eSHong Zhang 
423a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
424d763cef2SBarry Smith 
425d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4260633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
427fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
428d763cef2SBarry Smith 
4294f122a70SLisandro Dalcin   if (ts->trajectory) {
4304f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
431d763cef2SBarry Smith   }
43268bece0bSHong Zhang 
4331ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4344f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4351ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4361ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4371ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4381ef27442SStefano Zampini   }
4394f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
440d763cef2SBarry Smith   PetscFunctionReturn(0);
441d763cef2SBarry Smith }
442d763cef2SBarry Smith 
443d2daff3dSHong Zhang /*@
44478fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
44578fbdcc8SBarry Smith 
44678fbdcc8SBarry Smith    Collective on TS
44778fbdcc8SBarry Smith 
44878fbdcc8SBarry Smith    Input Parameters:
44978fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
45078fbdcc8SBarry Smith 
4517a7aea1fSJed Brown    Output Parameters:
45278fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
45378fbdcc8SBarry Smith 
45478fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
45578fbdcc8SBarry Smith 
45678fbdcc8SBarry Smith    Level: advanced
45778fbdcc8SBarry Smith 
45878fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
45978fbdcc8SBarry Smith 
46078fbdcc8SBarry Smith @*/
46178fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
46278fbdcc8SBarry Smith {
46378fbdcc8SBarry Smith   PetscFunctionBegin;
46478fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46578fbdcc8SBarry Smith   *tr = ts->trajectory;
46678fbdcc8SBarry Smith   PetscFunctionReturn(0);
46778fbdcc8SBarry Smith }
46878fbdcc8SBarry Smith 
46978fbdcc8SBarry Smith /*@
470bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
471d2daff3dSHong Zhang 
472d2daff3dSHong Zhang    Collective on TS
473d2daff3dSHong Zhang 
474d2daff3dSHong Zhang    Input Parameters:
475bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
476bc952696SBarry Smith 
47778fbdcc8SBarry Smith    Options Database:
47878fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
47978fbdcc8SBarry Smith -  -ts_trajectory_type type
48078fbdcc8SBarry Smith 
48168bece0bSHong Zhang Note: This routine should be called after all TS options have been set
482d2daff3dSHong Zhang 
483c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
48478fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
48578fbdcc8SBarry Smith 
486d2daff3dSHong Zhang    Level: intermediate
487d2daff3dSHong Zhang 
4882d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
489d2daff3dSHong Zhang 
490d2daff3dSHong Zhang @*/
491bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
492d2daff3dSHong Zhang {
493bc952696SBarry Smith   PetscErrorCode ierr;
494bc952696SBarry Smith 
495d2daff3dSHong Zhang   PetscFunctionBegin;
496d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
497bc952696SBarry Smith   if (!ts->trajectory) {
498bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
499bc952696SBarry Smith   }
500d2daff3dSHong Zhang   PetscFunctionReturn(0);
501d2daff3dSHong Zhang }
502d2daff3dSHong Zhang 
503a7a1495cSBarry Smith /*@
5042d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
5052d29f1f2SStefano Zampini 
5062d29f1f2SStefano Zampini    Collective on TS
5072d29f1f2SStefano Zampini 
5082d29f1f2SStefano Zampini    Input Parameters:
5092d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5102d29f1f2SStefano Zampini 
5112d29f1f2SStefano Zampini    Level: intermediate
5122d29f1f2SStefano Zampini 
5132d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5142d29f1f2SStefano Zampini 
5152d29f1f2SStefano Zampini @*/
5162d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5172d29f1f2SStefano Zampini {
5182d29f1f2SStefano Zampini   PetscErrorCode ierr;
5192d29f1f2SStefano Zampini 
5202d29f1f2SStefano Zampini   PetscFunctionBegin;
5212d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5222d29f1f2SStefano Zampini   if (ts->trajectory) {
5232d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5242d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5252d29f1f2SStefano Zampini   }
5262d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5272d29f1f2SStefano Zampini }
5282d29f1f2SStefano Zampini 
5292d29f1f2SStefano Zampini /*@
530a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
531a7a1495cSBarry Smith       set with TSSetRHSJacobian().
532a7a1495cSBarry Smith 
533d083f849SBarry Smith    Collective on TS
534a7a1495cSBarry Smith 
535a7a1495cSBarry Smith    Input Parameters:
536316643e7SJed Brown +  ts - the TS context
537a7a1495cSBarry Smith .  t - current timestep
5380910c330SBarry Smith -  U - input vector
539a7a1495cSBarry Smith 
540a7a1495cSBarry Smith    Output Parameters:
541a7a1495cSBarry Smith +  A - Jacobian matrix
542a7a1495cSBarry Smith .  B - optional preconditioning matrix
543a7a1495cSBarry Smith -  flag - flag indicating matrix structure
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 
54994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
550a7a1495cSBarry Smith    flag parameter.
551a7a1495cSBarry Smith 
552a7a1495cSBarry Smith    Level: developer
553a7a1495cSBarry Smith 
55494b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
555a7a1495cSBarry Smith @*/
556d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
557a7a1495cSBarry Smith {
558dfbe8321SBarry Smith   PetscErrorCode   ierr;
559270bf2e7SJed Brown   PetscObjectState Ustate;
5606c1e1eecSBarry Smith   PetscObjectId    Uid;
56124989b8cSPeter Brune   DM               dm;
562942e3340SBarry Smith   DMTS             tsdm;
56324989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
56424989b8cSPeter Brune   void             *ctx;
565b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
566a7a1495cSBarry Smith 
567a7a1495cSBarry Smith   PetscFunctionBegin;
5680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5690910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5700910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
57124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
572942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
5736374d434SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
5742663174eSHong Zhang   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
57559e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5766c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
577971015bcSStefano Zampini 
5782663174eSHong Zhang   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0);
579e1244c69SJed Brown 
580*ae692cf2SHong Zhang   if (ts->rhsjacobian.shift && ts->rhsjacobian.reuse) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Should not call TSComputeRHSJacobian() on a shifted matrix (shift=%lf) when RHSJacobian is reusable.",ts->rhsjacobian.shift);
58124989b8cSPeter Brune   if (rhsjacobianfunc) {
58294ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
583a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
584d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
585a7a1495cSBarry Smith     PetscStackPop;
58694ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
587ef66eb69SBarry Smith   } else {
58894ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
589971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
590ef66eb69SBarry Smith   }
5910e4ef248SJed Brown   ts->rhsjacobian.time  = t;
592971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
593971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
5947f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
59559e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
596a7a1495cSBarry Smith   PetscFunctionReturn(0);
597a7a1495cSBarry Smith }
598a7a1495cSBarry Smith 
599316643e7SJed Brown /*@
600d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
601d763cef2SBarry Smith 
602d083f849SBarry Smith    Collective on TS
603316643e7SJed Brown 
604316643e7SJed Brown    Input Parameters:
605316643e7SJed Brown +  ts - the TS context
606316643e7SJed Brown .  t - current time
6070910c330SBarry Smith -  U - state vector
608316643e7SJed Brown 
609316643e7SJed Brown    Output Parameter:
610316643e7SJed Brown .  y - right hand side
611316643e7SJed Brown 
612316643e7SJed Brown    Note:
613316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
614316643e7SJed Brown    is used internally within the nonlinear solvers.
615316643e7SJed Brown 
616316643e7SJed Brown    Level: developer
617316643e7SJed Brown 
618316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
619316643e7SJed Brown @*/
6200910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
621d763cef2SBarry Smith {
622dfbe8321SBarry Smith   PetscErrorCode ierr;
62324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
62424989b8cSPeter Brune   TSIFunction    ifunction;
62524989b8cSPeter Brune   void           *ctx;
62624989b8cSPeter Brune   DM             dm;
62724989b8cSPeter Brune 
628d763cef2SBarry Smith   PetscFunctionBegin;
6290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6300910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6310700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
63224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
63324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6340298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
635d763cef2SBarry Smith 
636ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
637d763cef2SBarry Smith 
6380910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
63924989b8cSPeter Brune   if (rhsfunction) {
6401be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
641d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6420910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
643d763cef2SBarry Smith     PetscStackPop;
6441be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
645214bc6a2SJed Brown   } else {
646214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
647b2cd27e8SJed Brown   }
64844a41b28SSean Farley 
6490910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
650d763cef2SBarry Smith   PetscFunctionReturn(0);
651d763cef2SBarry Smith }
652d763cef2SBarry Smith 
653ef20d060SBarry Smith /*@
654ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
655ef20d060SBarry Smith 
656d083f849SBarry Smith    Collective on TS
657ef20d060SBarry Smith 
658ef20d060SBarry Smith    Input Parameters:
659ef20d060SBarry Smith +  ts - the TS context
660ef20d060SBarry Smith -  t - current time
661ef20d060SBarry Smith 
662ef20d060SBarry Smith    Output Parameter:
6630910c330SBarry Smith .  U - the solution
664ef20d060SBarry Smith 
665ef20d060SBarry Smith    Note:
666ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
667ef20d060SBarry Smith    is used internally within the nonlinear solvers.
668ef20d060SBarry Smith 
669ef20d060SBarry Smith    Level: developer
670ef20d060SBarry Smith 
671abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
672ef20d060SBarry Smith @*/
6730910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
674ef20d060SBarry Smith {
675ef20d060SBarry Smith   PetscErrorCode     ierr;
676ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
677ef20d060SBarry Smith   void               *ctx;
678ef20d060SBarry Smith   DM                 dm;
679ef20d060SBarry Smith 
680ef20d060SBarry Smith   PetscFunctionBegin;
681ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6820910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
683ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
684ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
685ef20d060SBarry Smith 
686ef20d060SBarry Smith   if (solutionfunction) {
6879b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6880910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
689ef20d060SBarry Smith     PetscStackPop;
690ef20d060SBarry Smith   }
691ef20d060SBarry Smith   PetscFunctionReturn(0);
692ef20d060SBarry Smith }
6939b7cd975SBarry Smith /*@
6949b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6959b7cd975SBarry Smith 
696d083f849SBarry Smith    Collective on TS
6979b7cd975SBarry Smith 
6989b7cd975SBarry Smith    Input Parameters:
6999b7cd975SBarry Smith +  ts - the TS context
7009b7cd975SBarry Smith -  t - current time
7019b7cd975SBarry Smith 
7029b7cd975SBarry Smith    Output Parameter:
7039b7cd975SBarry Smith .  U - the function value
7049b7cd975SBarry Smith 
7059b7cd975SBarry Smith    Note:
7069b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7079b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7089b7cd975SBarry Smith 
7099b7cd975SBarry Smith    Level: developer
7109b7cd975SBarry Smith 
7119b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7129b7cd975SBarry Smith @*/
7139b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7149b7cd975SBarry Smith {
7159b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7169b7cd975SBarry Smith   void               *ctx;
7179b7cd975SBarry Smith   DM                 dm;
7189b7cd975SBarry Smith 
7199b7cd975SBarry Smith   PetscFunctionBegin;
7209b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7219b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7229b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7239b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7249b7cd975SBarry Smith 
7259b7cd975SBarry Smith   if (forcing) {
7269b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7279b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7289b7cd975SBarry Smith     PetscStackPop;
7299b7cd975SBarry Smith   }
7309b7cd975SBarry Smith   PetscFunctionReturn(0);
7319b7cd975SBarry Smith }
732ef20d060SBarry Smith 
733214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
734214bc6a2SJed Brown {
7352dd45cf8SJed Brown   Vec            F;
736214bc6a2SJed Brown   PetscErrorCode ierr;
737214bc6a2SJed Brown 
738214bc6a2SJed Brown   PetscFunctionBegin;
7390298fd71SBarry Smith   *Frhs = NULL;
7400298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
741214bc6a2SJed Brown   if (!ts->Frhs) {
7422dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
743214bc6a2SJed Brown   }
744214bc6a2SJed Brown   *Frhs = ts->Frhs;
745214bc6a2SJed Brown   PetscFunctionReturn(0);
746214bc6a2SJed Brown }
747214bc6a2SJed Brown 
748971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
749214bc6a2SJed Brown {
750214bc6a2SJed Brown   Mat            A,B;
7512dd45cf8SJed Brown   PetscErrorCode ierr;
75241a1d4d2SBarry Smith   TSIJacobian    ijacobian;
753214bc6a2SJed Brown 
754214bc6a2SJed Brown   PetscFunctionBegin;
755c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
756c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
75741a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
758214bc6a2SJed Brown   if (Arhs) {
759214bc6a2SJed Brown     if (!ts->Arhs) {
76041a1d4d2SBarry Smith       if (ijacobian) {
761214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
76241a1d4d2SBarry Smith       } else {
76341a1d4d2SBarry Smith         ts->Arhs = A;
76441a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
76541a1d4d2SBarry Smith       }
7663565c898SBarry Smith     } else {
7673565c898SBarry Smith       PetscBool flg;
7683565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7693565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7703565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7713565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7723565c898SBarry Smith         ts->Arhs = A;
7733565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7743565c898SBarry Smith       }
775214bc6a2SJed Brown     }
776214bc6a2SJed Brown     *Arhs = ts->Arhs;
777214bc6a2SJed Brown   }
778214bc6a2SJed Brown   if (Brhs) {
779214bc6a2SJed Brown     if (!ts->Brhs) {
780bdb70873SJed Brown       if (A != B) {
78141a1d4d2SBarry Smith         if (ijacobian) {
782214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
783bdb70873SJed Brown         } else {
78441a1d4d2SBarry Smith           ts->Brhs = B;
78541a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
78641a1d4d2SBarry Smith         }
78741a1d4d2SBarry Smith       } else {
788bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
78951699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
790bdb70873SJed Brown       }
791214bc6a2SJed Brown     }
792214bc6a2SJed Brown     *Brhs = ts->Brhs;
793214bc6a2SJed Brown   }
794214bc6a2SJed Brown   PetscFunctionReturn(0);
795214bc6a2SJed Brown }
796214bc6a2SJed Brown 
797316643e7SJed Brown /*@
7980910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
799316643e7SJed Brown 
800d083f849SBarry Smith    Collective on TS
801316643e7SJed Brown 
802316643e7SJed Brown    Input Parameters:
803316643e7SJed Brown +  ts - the TS context
804316643e7SJed Brown .  t - current time
8050910c330SBarry Smith .  U - state vector
8060910c330SBarry Smith .  Udot - time derivative of state vector
807214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
808316643e7SJed Brown 
809316643e7SJed Brown    Output Parameter:
810316643e7SJed Brown .  Y - right hand side
811316643e7SJed Brown 
812316643e7SJed Brown    Note:
813316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
814316643e7SJed Brown    is used internally within the nonlinear solvers.
815316643e7SJed Brown 
816316643e7SJed Brown    If the user did did not write their equations in implicit form, this
817316643e7SJed Brown    function recasts them in implicit form.
818316643e7SJed Brown 
819316643e7SJed Brown    Level: developer
820316643e7SJed Brown 
821316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
822316643e7SJed Brown @*/
8230910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
824316643e7SJed Brown {
825316643e7SJed Brown   PetscErrorCode ierr;
82624989b8cSPeter Brune   TSIFunction    ifunction;
82724989b8cSPeter Brune   TSRHSFunction  rhsfunction;
82824989b8cSPeter Brune   void           *ctx;
82924989b8cSPeter Brune   DM             dm;
830316643e7SJed Brown 
831316643e7SJed Brown   PetscFunctionBegin;
8320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8330910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8340910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8350700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
836316643e7SJed Brown 
83724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
83824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8390298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
84024989b8cSPeter Brune 
841ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
842d90be118SSean Farley 
8430910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
84424989b8cSPeter Brune   if (ifunction) {
845316643e7SJed Brown     PetscStackPush("TS user implicit function");
8460910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
847316643e7SJed Brown     PetscStackPop;
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);
881cfa8a9a2SHong Zhang 
882cfa8a9a2SHong Zhang   if (ts->rhsjacobian.shift) {
883cfa8a9a2SHong Zhang     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
884cfa8a9a2SHong Zhang   }
885cfa8a9a2SHong Zhang   if (ts->rhsjacobian.scale == -1.) {
886cfa8a9a2SHong Zhang     ierr = MatScale(A,-1);CHKERRQ(ierr);
887cfa8a9a2SHong Zhang   }
888cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
889cfa8a9a2SHong Zhang     if (ts->rhsjacobian.shift) {
890cfa8a9a2SHong Zhang       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
891cfa8a9a2SHong Zhang     }
892cfa8a9a2SHong Zhang     if (ts->rhsjacobian.scale == -1.) {
893cfa8a9a2SHong Zhang       ierr = MatScale(B,-1);CHKERRQ(ierr);
894cfa8a9a2SHong Zhang     }
895cfa8a9a2SHong Zhang   }
896cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
897cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
898cfa8a9a2SHong Zhang   PetscFunctionReturn(0);
899cfa8a9a2SHong Zhang }
900cfa8a9a2SHong Zhang 
901316643e7SJed Brown /*@
902316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
903316643e7SJed Brown 
904d083f849SBarry Smith    Collective on TS
905316643e7SJed Brown 
906316643e7SJed Brown    Input
907316643e7SJed Brown       Input Parameters:
908316643e7SJed Brown +  ts - the TS context
909316643e7SJed Brown .  t - current timestep
9100910c330SBarry Smith .  U - state vector
9110910c330SBarry Smith .  Udot - time derivative of state vector
912214bc6a2SJed Brown .  shift - shift to apply, see note below
913214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
914316643e7SJed Brown 
915316643e7SJed Brown    Output Parameters:
916316643e7SJed Brown +  A - Jacobian matrix
9173565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
918316643e7SJed Brown 
919316643e7SJed Brown    Notes:
9200910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
921316643e7SJed Brown 
9220910c330SBarry Smith    dF/dU + shift*dF/dUdot
923316643e7SJed Brown 
924316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
925316643e7SJed Brown    is used internally within the nonlinear solvers.
926316643e7SJed Brown 
927316643e7SJed Brown    Level: developer
928316643e7SJed Brown 
929316643e7SJed Brown .seealso:  TSSetIJacobian()
93063495f91SJed Brown @*/
931d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
932316643e7SJed Brown {
933316643e7SJed Brown   PetscErrorCode ierr;
93424989b8cSPeter Brune   TSIJacobian    ijacobian;
93524989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
93624989b8cSPeter Brune   DM             dm;
93724989b8cSPeter Brune   void           *ctx;
938316643e7SJed Brown 
939316643e7SJed Brown   PetscFunctionBegin;
9400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9410910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9420910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
943316643e7SJed Brown   PetscValidPointer(A,6);
94494ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
945316643e7SJed Brown   PetscValidPointer(B,7);
94694ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
94724989b8cSPeter Brune 
94824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
94924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9500298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
95124989b8cSPeter Brune 
952ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
953316643e7SJed Brown 
95494ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
95524989b8cSPeter Brune   if (ijacobian) {
956316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
957d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
958316643e7SJed Brown     PetscStackPop;
9594a6899ffSJed Brown   }
960214bc6a2SJed Brown   if (imex) {
961b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9624c26be97Sstefano_zampini       PetscBool assembled;
963971015bcSStefano Zampini       if (rhsjacobian) {
964971015bcSStefano Zampini         Mat Arhs = NULL;
965971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
966971015bcSStefano Zampini         if (A == Arhs) {
967e8b1e424SHong Zhang           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant"); /* there is no way to reconstruct shift*M-J since J cannot be reevaluated */
968971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
969971015bcSStefano Zampini         }
970971015bcSStefano Zampini       }
97194ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9724c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9734c26be97Sstefano_zampini       if (!assembled) {
9744c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9754c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9764c26be97Sstefano_zampini       }
97794ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
97894ab13aaSBarry Smith       if (A != B) {
97994ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9804c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9814c26be97Sstefano_zampini         if (!assembled) {
9824c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9834c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9844c26be97Sstefano_zampini         }
98594ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
986214bc6a2SJed Brown       }
987214bc6a2SJed Brown     }
988214bc6a2SJed Brown   } else {
989e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
990e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
991214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
992e1244c69SJed Brown     }
993e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
994e8b1e424SHong Zhang       PetscObjectState Ustate;
995e8b1e424SHong Zhang       PetscObjectId    Uid;
996e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
997e8b1e424SHong Zhang 
998e8b1e424SHong Zhang       ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
999e8b1e424SHong Zhang       ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
1000e8b1e424SHong Zhang       ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
1001097a96a2SHong 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 */
1002e8b1e424SHong 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 */
1003e8b1e424SHong Zhang       } else {
10043565c898SBarry Smith         PetscBool flg;
1005e8b1e424SHong Zhang 
1006e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1007e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1008e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1009e8b1e424SHong Zhang              and the matrices have not been assembled yet */
1010cfa8a9a2SHong Zhang           ierr = TSRecoverRHSJacobian(ts,A,B);CHKERRQ(ierr);
1011e8b1e424SHong Zhang         }
1012e8b1e424SHong Zhang         ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr);
10133565c898SBarry Smith         ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10143565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10153565c898SBarry Smith         if (!flg) {
101694ab13aaSBarry Smith           ierr = MatScale(A,-1);CHKERRQ(ierr);
101794ab13aaSBarry Smith           ierr = MatShift(A,shift);CHKERRQ(ierr);
10183565c898SBarry Smith         }
101994ab13aaSBarry Smith         if (A != B) {
102094ab13aaSBarry Smith           ierr = MatScale(B,-1);CHKERRQ(ierr);
102194ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1022316643e7SJed Brown         }
1023e8b1e424SHong Zhang       }
1024e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1025e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1026e8b1e424SHong Zhang     } else if (Arhs) {  /* Both IJacobian and RHSJacobian exist or the RHS matrix provided (A) is different from the internal RHS matrix (Arhs) */
1027e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1028e8b1e424SHong Zhang 
1029e1244c69SJed Brown       if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */
103094ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
103194ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
103294ab13aaSBarry Smith         if (A != B) {
103394ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
103494ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1035214bc6a2SJed Brown         }
1036316643e7SJed Brown       }
1037e8b1e424SHong Zhang       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
103894ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
103994ab13aaSBarry Smith       if (A != B) {
104094ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1041316643e7SJed Brown       }
1042316643e7SJed Brown     }
1043316643e7SJed Brown   }
104494ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1045316643e7SJed Brown   PetscFunctionReturn(0);
1046316643e7SJed Brown }
1047316643e7SJed Brown 
1048d763cef2SBarry Smith /*@C
1049d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1050b5abc632SBarry Smith     where U_t = G(t,u).
1051d763cef2SBarry Smith 
10523f9fe445SBarry Smith     Logically Collective on TS
1053d763cef2SBarry Smith 
1054d763cef2SBarry Smith     Input Parameters:
1055d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10560298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1057d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1058d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10590298fd71SBarry Smith           function evaluation routine (may be NULL)
1060d763cef2SBarry Smith 
1061a96d6ef6SBarry Smith     Calling sequence of f:
1062a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1063d763cef2SBarry Smith 
1064a96d6ef6SBarry Smith +   ts - timestep context
1065a96d6ef6SBarry Smith .   t - current timestep
1066d763cef2SBarry Smith .   u - input vector
1067d763cef2SBarry Smith .   F - function vector
1068d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1069d763cef2SBarry Smith 
1070d763cef2SBarry Smith     Level: beginner
1071d763cef2SBarry Smith 
107295452b02SPatrick Sanan     Notes:
107395452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10742bbac0d3SBarry Smith 
1075ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1076d763cef2SBarry Smith @*/
1077089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1078d763cef2SBarry Smith {
1079089b2837SJed Brown   PetscErrorCode ierr;
1080089b2837SJed Brown   SNES           snes;
10810298fd71SBarry Smith   Vec            ralloc = NULL;
108224989b8cSPeter Brune   DM             dm;
1083d763cef2SBarry Smith 
1084089b2837SJed Brown   PetscFunctionBegin;
10850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1086ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
108724989b8cSPeter Brune 
108824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108924989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1090089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1091e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1092e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1093e856ceecSJed Brown     r = ralloc;
1094e856ceecSJed Brown   }
1095089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1096e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1097d763cef2SBarry Smith   PetscFunctionReturn(0);
1098d763cef2SBarry Smith }
1099d763cef2SBarry Smith 
1100ef20d060SBarry Smith /*@C
1101abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1102ef20d060SBarry Smith 
1103ef20d060SBarry Smith     Logically Collective on TS
1104ef20d060SBarry Smith 
1105ef20d060SBarry Smith     Input Parameters:
1106ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1107ef20d060SBarry Smith .   f - routine for evaluating the solution
1108ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11090298fd71SBarry Smith           function evaluation routine (may be NULL)
1110ef20d060SBarry Smith 
1111a96d6ef6SBarry Smith     Calling sequence of f:
1112a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1113ef20d060SBarry Smith 
1114ef20d060SBarry Smith +   t - current timestep
1115ef20d060SBarry Smith .   u - output vector
1116ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1117ef20d060SBarry Smith 
11180ed3bfb6SBarry Smith     Options Database:
11190ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11200ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11210ed3bfb6SBarry Smith 
1122abd5a294SJed Brown     Notes:
1123abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1124abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1125abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1126abd5a294SJed Brown 
11274f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1128abd5a294SJed Brown 
1129ef20d060SBarry Smith     Level: beginner
1130ef20d060SBarry Smith 
11310ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1132ef20d060SBarry Smith @*/
1133ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1134ef20d060SBarry Smith {
1135ef20d060SBarry Smith   PetscErrorCode ierr;
1136ef20d060SBarry Smith   DM             dm;
1137ef20d060SBarry Smith 
1138ef20d060SBarry Smith   PetscFunctionBegin;
1139ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1140ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1141ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1142ef20d060SBarry Smith   PetscFunctionReturn(0);
1143ef20d060SBarry Smith }
1144ef20d060SBarry Smith 
11459b7cd975SBarry Smith /*@C
11469b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11479b7cd975SBarry Smith 
11489b7cd975SBarry Smith     Logically Collective on TS
11499b7cd975SBarry Smith 
11509b7cd975SBarry Smith     Input Parameters:
11519b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1152e162b725SBarry Smith .   func - routine for evaluating the forcing function
11539b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11540298fd71SBarry Smith           function evaluation routine (may be NULL)
11559b7cd975SBarry Smith 
11569b7cd975SBarry Smith     Calling sequence of func:
11576bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11589b7cd975SBarry Smith 
11599b7cd975SBarry Smith +   t - current timestep
1160e162b725SBarry Smith .   f - output vector
11619b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11629b7cd975SBarry Smith 
11639b7cd975SBarry Smith     Notes:
11649b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1165e162b725SBarry 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
1166e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1167e162b725SBarry Smith 
1168e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1169e162b725SBarry Smith 
1170e162b725SBarry 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
1171e162b725SBarry Smith     parameters can be passed in the ctx variable.
11729b7cd975SBarry Smith 
11739b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11749b7cd975SBarry Smith 
11759b7cd975SBarry Smith     Level: beginner
11769b7cd975SBarry Smith 
11779b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11789b7cd975SBarry Smith @*/
1179e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11809b7cd975SBarry Smith {
11819b7cd975SBarry Smith   PetscErrorCode ierr;
11829b7cd975SBarry Smith   DM             dm;
11839b7cd975SBarry Smith 
11849b7cd975SBarry Smith   PetscFunctionBegin;
11859b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11869b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1187e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11889b7cd975SBarry Smith   PetscFunctionReturn(0);
11899b7cd975SBarry Smith }
11909b7cd975SBarry Smith 
1191d763cef2SBarry Smith /*@C
1192f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1193b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1194d763cef2SBarry Smith 
11953f9fe445SBarry Smith    Logically Collective on TS
1196d763cef2SBarry Smith 
1197d763cef2SBarry Smith    Input Parameters:
1198d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1199e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1200e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1201d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1202d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12030298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1204d763cef2SBarry Smith 
1205f7ab8db6SBarry Smith    Calling sequence of f:
1206a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1207d763cef2SBarry Smith 
1208d763cef2SBarry Smith +  t - current timestep
1209d763cef2SBarry Smith .  u - input vector
1210e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1211e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1212d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1213d763cef2SBarry Smith 
12146cd88445SBarry Smith    Notes:
12156cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12166cd88445SBarry Smith 
12176cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1218ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1219d763cef2SBarry Smith 
1220d763cef2SBarry Smith    Level: beginner
1221d763cef2SBarry Smith 
1222ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1223d763cef2SBarry Smith 
1224d763cef2SBarry Smith @*/
1225e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1226d763cef2SBarry Smith {
1227277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1228089b2837SJed Brown   SNES           snes;
122924989b8cSPeter Brune   DM             dm;
123024989b8cSPeter Brune   TSIJacobian    ijacobian;
1231277b19d0SLisandro Dalcin 
1232d763cef2SBarry Smith   PetscFunctionBegin;
12330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1234e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1235e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1236e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1237e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1238d763cef2SBarry Smith 
123924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124024989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
12410298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1242089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12435f659677SPeter Brune   if (!ijacobian) {
1244e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12450e4ef248SJed Brown   }
1246e5d3d808SBarry Smith   if (Amat) {
1247e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12480e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1249e5d3d808SBarry Smith     ts->Arhs = Amat;
12500e4ef248SJed Brown   }
1251e5d3d808SBarry Smith   if (Pmat) {
1252e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12530e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1254e5d3d808SBarry Smith     ts->Brhs = Pmat;
12550e4ef248SJed Brown   }
1256d763cef2SBarry Smith   PetscFunctionReturn(0);
1257d763cef2SBarry Smith }
1258d763cef2SBarry Smith 
1259316643e7SJed Brown /*@C
1260b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1261316643e7SJed Brown 
12623f9fe445SBarry Smith    Logically Collective on TS
1263316643e7SJed Brown 
1264316643e7SJed Brown    Input Parameters:
1265316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12660298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1267316643e7SJed Brown .  f   - the function evaluation routine
12680298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1269316643e7SJed Brown 
1270316643e7SJed Brown    Calling sequence of f:
12716bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1272316643e7SJed Brown 
1273316643e7SJed Brown +  t   - time at step/stage being solved
1274316643e7SJed Brown .  u   - state vector
1275316643e7SJed Brown .  u_t - time derivative of state vector
1276316643e7SJed Brown .  F   - function vector
1277316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1278316643e7SJed Brown 
1279316643e7SJed Brown    Important:
12802bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1281316643e7SJed Brown 
1282316643e7SJed Brown    Level: beginner
1283316643e7SJed Brown 
1284d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1285316643e7SJed Brown @*/
128651699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1287316643e7SJed Brown {
1288089b2837SJed Brown   PetscErrorCode ierr;
1289089b2837SJed Brown   SNES           snes;
129051699248SLisandro Dalcin   Vec            ralloc = NULL;
129124989b8cSPeter Brune   DM             dm;
1292316643e7SJed Brown 
1293316643e7SJed Brown   PetscFunctionBegin;
12940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
129551699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
129624989b8cSPeter Brune 
129724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
129824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
129924989b8cSPeter Brune 
1300089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
130151699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
130251699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
130351699248SLisandro Dalcin     r  = ralloc;
1304e856ceecSJed Brown   }
130551699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
130651699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1307089b2837SJed Brown   PetscFunctionReturn(0);
1308089b2837SJed Brown }
1309089b2837SJed Brown 
1310089b2837SJed Brown /*@C
1311089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1312089b2837SJed Brown 
1313089b2837SJed Brown    Not Collective
1314089b2837SJed Brown 
1315089b2837SJed Brown    Input Parameter:
1316089b2837SJed Brown .  ts - the TS context
1317089b2837SJed Brown 
1318089b2837SJed Brown    Output Parameter:
13190298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13200298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13210298fd71SBarry Smith -  ctx - the function context (or NULL)
1322089b2837SJed Brown 
1323089b2837SJed Brown    Level: advanced
1324089b2837SJed Brown 
1325089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1326089b2837SJed Brown @*/
1327089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1328089b2837SJed Brown {
1329089b2837SJed Brown   PetscErrorCode ierr;
1330089b2837SJed Brown   SNES           snes;
133124989b8cSPeter Brune   DM             dm;
1332089b2837SJed Brown 
1333089b2837SJed Brown   PetscFunctionBegin;
1334089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1335089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13360298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
133724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
133824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1339089b2837SJed Brown   PetscFunctionReturn(0);
1340089b2837SJed Brown }
1341089b2837SJed Brown 
1342089b2837SJed Brown /*@C
1343089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1344089b2837SJed Brown 
1345089b2837SJed Brown    Not Collective
1346089b2837SJed Brown 
1347089b2837SJed Brown    Input Parameter:
1348089b2837SJed Brown .  ts - the TS context
1349089b2837SJed Brown 
1350089b2837SJed Brown    Output Parameter:
13510298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13520298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13530298fd71SBarry Smith -  ctx - the function context (or NULL)
1354089b2837SJed Brown 
1355089b2837SJed Brown    Level: advanced
1356089b2837SJed Brown 
13572bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1358089b2837SJed Brown @*/
1359089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1360089b2837SJed Brown {
1361089b2837SJed Brown   PetscErrorCode ierr;
1362089b2837SJed Brown   SNES           snes;
136324989b8cSPeter Brune   DM             dm;
1364089b2837SJed Brown 
1365089b2837SJed Brown   PetscFunctionBegin;
1366089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1367089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13680298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
136924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1371316643e7SJed Brown   PetscFunctionReturn(0);
1372316643e7SJed Brown }
1373316643e7SJed Brown 
1374316643e7SJed Brown /*@C
1375a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1376ae8867d6SBarry Smith         provided with TSSetIFunction().
1377316643e7SJed Brown 
13783f9fe445SBarry Smith    Logically Collective on TS
1379316643e7SJed Brown 
1380316643e7SJed Brown    Input Parameters:
1381316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1382e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1383e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1384316643e7SJed Brown .  f   - the Jacobian evaluation routine
13850298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1386316643e7SJed Brown 
1387316643e7SJed Brown    Calling sequence of f:
13886bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1389316643e7SJed Brown 
1390316643e7SJed Brown +  t    - time at step/stage being solved
13911b4a444bSJed Brown .  U    - state vector
13921b4a444bSJed Brown .  U_t  - time derivative of state vector
1393316643e7SJed Brown .  a    - shift
1394e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1395e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1396316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1397316643e7SJed Brown 
1398316643e7SJed Brown    Notes:
1399e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1400316643e7SJed Brown 
1401895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1402895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1403895c21f2SBarry Smith 
1404a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1405b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1406a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1407a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1408a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1409a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1410a4f0a591SBarry Smith 
14116cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14126cd88445SBarry Smith 
14136cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1414ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1415ca5f011dSBarry Smith 
1416316643e7SJed Brown    Level: beginner
1417316643e7SJed Brown 
1418ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1419316643e7SJed Brown 
1420316643e7SJed Brown @*/
1421e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1422316643e7SJed Brown {
1423316643e7SJed Brown   PetscErrorCode ierr;
1424089b2837SJed Brown   SNES           snes;
142524989b8cSPeter Brune   DM             dm;
1426316643e7SJed Brown 
1427316643e7SJed Brown   PetscFunctionBegin;
14280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1429e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1430e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1431e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1432e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
143324989b8cSPeter Brune 
143424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
143524989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
143624989b8cSPeter Brune 
1437089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1438e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1439316643e7SJed Brown   PetscFunctionReturn(0);
1440316643e7SJed Brown }
1441316643e7SJed Brown 
1442e1244c69SJed Brown /*@
1443e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1444e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1445e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1446e1244c69SJed Brown    not been changed by the TS.
1447e1244c69SJed Brown 
1448e1244c69SJed Brown    Logically Collective
1449e1244c69SJed Brown 
1450e1244c69SJed Brown    Input Arguments:
1451e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1452e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1453e1244c69SJed Brown 
1454e1244c69SJed Brown    Level: intermediate
1455e1244c69SJed Brown 
1456e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1457e1244c69SJed Brown @*/
1458e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1459e1244c69SJed Brown {
1460e1244c69SJed Brown   PetscFunctionBegin;
1461e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1462e1244c69SJed Brown   PetscFunctionReturn(0);
1463e1244c69SJed Brown }
1464e1244c69SJed Brown 
1465efe9872eSLisandro Dalcin /*@C
1466efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1467efe9872eSLisandro Dalcin 
1468efe9872eSLisandro Dalcin    Logically Collective on TS
1469efe9872eSLisandro Dalcin 
1470efe9872eSLisandro Dalcin    Input Parameters:
1471efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1472efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1473efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1474efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1475efe9872eSLisandro Dalcin 
1476efe9872eSLisandro Dalcin    Calling sequence of fun:
14776bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1478efe9872eSLisandro Dalcin 
1479efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1480efe9872eSLisandro Dalcin .  U    - state vector
1481efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1482efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1483efe9872eSLisandro Dalcin .  F    - function vector
1484efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1485efe9872eSLisandro Dalcin 
1486efe9872eSLisandro Dalcin    Level: beginner
1487efe9872eSLisandro Dalcin 
1488a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1489efe9872eSLisandro Dalcin @*/
1490efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1491efe9872eSLisandro Dalcin {
1492efe9872eSLisandro Dalcin   DM             dm;
1493efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1494efe9872eSLisandro Dalcin 
1495efe9872eSLisandro Dalcin   PetscFunctionBegin;
1496efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1497efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1498efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1499efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1500efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1501efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1502efe9872eSLisandro Dalcin }
1503efe9872eSLisandro Dalcin 
1504efe9872eSLisandro Dalcin /*@C
1505efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1506efe9872eSLisandro Dalcin 
1507efe9872eSLisandro Dalcin   Not Collective
1508efe9872eSLisandro Dalcin 
1509efe9872eSLisandro Dalcin   Input Parameter:
1510efe9872eSLisandro Dalcin . ts - the TS context
1511efe9872eSLisandro Dalcin 
1512efe9872eSLisandro Dalcin   Output Parameter:
1513efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1514efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1515efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin   Level: advanced
1518efe9872eSLisandro Dalcin 
1519a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1520efe9872eSLisandro Dalcin @*/
1521efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1522efe9872eSLisandro Dalcin {
1523efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1524efe9872eSLisandro Dalcin   SNES           snes;
1525efe9872eSLisandro Dalcin   DM             dm;
1526efe9872eSLisandro Dalcin 
1527efe9872eSLisandro Dalcin   PetscFunctionBegin;
1528efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1529efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1530efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1531efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1532efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1533efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1534efe9872eSLisandro Dalcin }
1535efe9872eSLisandro Dalcin 
1536efe9872eSLisandro Dalcin /*@C
1537bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1538efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1539efe9872eSLisandro Dalcin 
1540efe9872eSLisandro Dalcin    Logically Collective on TS
1541efe9872eSLisandro Dalcin 
1542efe9872eSLisandro Dalcin    Input Parameters:
1543efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1544efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1545efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1546efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1547efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1548efe9872eSLisandro Dalcin 
1549efe9872eSLisandro Dalcin    Calling sequence of jac:
15506bc98fa9SBarry 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);
1551efe9872eSLisandro Dalcin 
1552efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1553efe9872eSLisandro Dalcin .  U    - state vector
1554efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1555efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1556efe9872eSLisandro Dalcin .  v    - shift for U_t
1557efe9872eSLisandro Dalcin .  a    - shift for U_tt
1558efe9872eSLisandro 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
1559efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1560efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1561efe9872eSLisandro Dalcin 
1562efe9872eSLisandro Dalcin    Notes:
1563efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1564efe9872eSLisandro Dalcin 
1565efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1566efe9872eSLisandro 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.
1567efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1568bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1569efe9872eSLisandro Dalcin 
1570efe9872eSLisandro Dalcin    Level: beginner
1571efe9872eSLisandro Dalcin 
1572a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1573efe9872eSLisandro Dalcin @*/
1574efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1575efe9872eSLisandro Dalcin {
1576efe9872eSLisandro Dalcin   DM             dm;
1577efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1578efe9872eSLisandro Dalcin 
1579efe9872eSLisandro Dalcin   PetscFunctionBegin;
1580efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1581efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1582efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1583efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1584efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1585efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1586efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1587efe9872eSLisandro Dalcin }
1588efe9872eSLisandro Dalcin 
1589efe9872eSLisandro Dalcin /*@C
1590efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1591efe9872eSLisandro Dalcin 
1592efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1593efe9872eSLisandro Dalcin 
1594efe9872eSLisandro Dalcin   Input Parameter:
1595efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin   Output Parameters:
1598efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1599efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1600efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1601efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1602efe9872eSLisandro Dalcin 
160395452b02SPatrick Sanan   Notes:
160495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1605efe9872eSLisandro Dalcin 
1606efe9872eSLisandro Dalcin   Level: advanced
1607efe9872eSLisandro Dalcin 
1608a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1609efe9872eSLisandro Dalcin 
1610efe9872eSLisandro Dalcin @*/
1611efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1612efe9872eSLisandro Dalcin {
1613efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1614efe9872eSLisandro Dalcin   SNES           snes;
1615efe9872eSLisandro Dalcin   DM             dm;
1616efe9872eSLisandro Dalcin 
1617efe9872eSLisandro Dalcin   PetscFunctionBegin;
1618efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1619efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1620efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1621efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1622efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1623efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1624efe9872eSLisandro Dalcin }
1625efe9872eSLisandro Dalcin 
1626efe9872eSLisandro Dalcin /*@
1627efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1628efe9872eSLisandro Dalcin 
1629d083f849SBarry Smith   Collective on TS
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin   Input Parameters:
1632efe9872eSLisandro Dalcin + ts - the TS context
1633efe9872eSLisandro Dalcin . t - current time
1634efe9872eSLisandro Dalcin . U - state vector
1635efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1636efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1637efe9872eSLisandro Dalcin 
1638efe9872eSLisandro Dalcin   Output Parameter:
1639efe9872eSLisandro Dalcin . F - the residual vector
1640efe9872eSLisandro Dalcin 
1641efe9872eSLisandro Dalcin   Note:
1642efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1643efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1644efe9872eSLisandro Dalcin 
1645efe9872eSLisandro Dalcin   Level: developer
1646efe9872eSLisandro Dalcin 
1647a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1648efe9872eSLisandro Dalcin @*/
1649efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1650efe9872eSLisandro Dalcin {
1651efe9872eSLisandro Dalcin   DM             dm;
1652efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1653efe9872eSLisandro Dalcin   void           *ctx;
1654efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1655efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1656efe9872eSLisandro Dalcin 
1657efe9872eSLisandro Dalcin   PetscFunctionBegin;
1658efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1659efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1660efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1661efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1662efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1665efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1666efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1667efe9872eSLisandro Dalcin 
1668efe9872eSLisandro Dalcin   if (!I2Function) {
1669efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1670efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1671efe9872eSLisandro Dalcin   }
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1674efe9872eSLisandro Dalcin 
1675efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1676efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1677efe9872eSLisandro Dalcin   PetscStackPop;
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   if (rhsfunction) {
1680efe9872eSLisandro Dalcin     Vec Frhs;
1681efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1682efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1683efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1684efe9872eSLisandro Dalcin   }
1685efe9872eSLisandro Dalcin 
1686efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1687efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1688efe9872eSLisandro Dalcin }
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin /*@
1691efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1692efe9872eSLisandro Dalcin 
1693d083f849SBarry Smith   Collective on TS
1694efe9872eSLisandro Dalcin 
1695efe9872eSLisandro Dalcin   Input Parameters:
1696efe9872eSLisandro Dalcin + ts - the TS context
1697efe9872eSLisandro Dalcin . t - current timestep
1698efe9872eSLisandro Dalcin . U - state vector
1699efe9872eSLisandro Dalcin . V - time derivative of state vector
1700efe9872eSLisandro Dalcin . A - second time derivative of state vector
1701efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1702efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1703efe9872eSLisandro Dalcin 
1704efe9872eSLisandro Dalcin   Output Parameters:
1705efe9872eSLisandro Dalcin + J - Jacobian matrix
1706efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin   Notes:
1709efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1710efe9872eSLisandro Dalcin 
1711efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1714efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1715efe9872eSLisandro Dalcin 
1716efe9872eSLisandro Dalcin   Level: developer
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1719efe9872eSLisandro Dalcin @*/
1720efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1721efe9872eSLisandro Dalcin {
1722efe9872eSLisandro Dalcin   DM             dm;
1723efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1724efe9872eSLisandro Dalcin   void           *ctx;
1725efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1726efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1727efe9872eSLisandro Dalcin 
1728efe9872eSLisandro Dalcin   PetscFunctionBegin;
1729efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1730efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1731efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1732efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1733efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1734efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1737efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1738efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1741efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1742efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1743efe9872eSLisandro Dalcin   }
1744efe9872eSLisandro Dalcin 
1745efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1746efe9872eSLisandro Dalcin 
1747efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1748efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1749efe9872eSLisandro Dalcin   PetscStackPop;
1750efe9872eSLisandro Dalcin 
1751efe9872eSLisandro Dalcin   if (rhsjacobian) {
1752efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1753efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1754efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1755efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1756efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1757efe9872eSLisandro Dalcin   }
1758efe9872eSLisandro Dalcin 
1759efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1760efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1761efe9872eSLisandro Dalcin }
1762efe9872eSLisandro Dalcin 
1763438f35afSJed Brown /*@C
1764438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1765438f35afSJed Brown 
1766438f35afSJed Brown    Logically Collective
1767438f35afSJed Brown 
1768438f35afSJed Brown    Input Arguments:
1769438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1770a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1771438f35afSJed Brown -  ctx - a context for tvar
1772438f35afSJed Brown 
1773a96d6ef6SBarry Smith     Calling sequence of tvar:
1774a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1775a96d6ef6SBarry Smith 
1776a96d6ef6SBarry Smith +   ts - timestep context
1777a96d6ef6SBarry Smith .   p - input vector (primative form)
1778a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1779a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1780a96d6ef6SBarry Smith 
1781438f35afSJed Brown    Level: advanced
1782438f35afSJed Brown 
1783438f35afSJed Brown    Notes:
1784438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1785438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1786438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1787438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1788438f35afSJed Brown    evaluated via the chain rule, as in
1789438f35afSJed Brown 
1790438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1791438f35afSJed Brown 
1792438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1793438f35afSJed Brown @*/
1794438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1795438f35afSJed Brown {
1796438f35afSJed Brown   PetscErrorCode ierr;
1797438f35afSJed Brown   DM             dm;
1798438f35afSJed Brown 
1799438f35afSJed Brown   PetscFunctionBegin;
1800438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1801438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1802438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1803438f35afSJed Brown   PetscFunctionReturn(0);
1804438f35afSJed Brown }
1805438f35afSJed Brown 
1806efe9872eSLisandro Dalcin /*@
1807e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1808e3c11fc1SJed Brown 
1809e3c11fc1SJed Brown    Logically Collective
1810e3c11fc1SJed Brown 
1811e3c11fc1SJed Brown    Input Parameters:
1812e3c11fc1SJed Brown +  ts - TS on which to compute
1813e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1814e3c11fc1SJed Brown 
1815e3c11fc1SJed Brown    Output Parameters:
1816e3c11fc1SJed Brown .  C - transient (conservative) variable
1817e3c11fc1SJed Brown 
1818e3c11fc1SJed Brown    Developer Notes:
1819e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1820e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1821e3c11fc1SJed Brown    being used.
1822e3c11fc1SJed Brown 
1823e3c11fc1SJed Brown    Level: developer
1824e3c11fc1SJed Brown 
1825e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1826e3c11fc1SJed Brown @*/
1827e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1828e3c11fc1SJed Brown {
1829e3c11fc1SJed Brown   PetscErrorCode ierr;
1830e3c11fc1SJed Brown   DM             dm;
1831e3c11fc1SJed Brown   DMTS           dmts;
1832e3c11fc1SJed Brown 
1833e3c11fc1SJed Brown   PetscFunctionBegin;
1834e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1835e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1836e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1837e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1838e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1839e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1840e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1841e3c11fc1SJed Brown   }
1842e3c11fc1SJed Brown   PetscFunctionReturn(0);
1843e3c11fc1SJed Brown }
1844e3c11fc1SJed Brown 
1845e3c11fc1SJed Brown /*@
1846e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1847e3c11fc1SJed Brown 
1848e3c11fc1SJed Brown    Logically Collective
1849e3c11fc1SJed Brown 
1850e3c11fc1SJed Brown    Input Parameters:
1851e3c11fc1SJed Brown .  ts - TS on which to compute
1852e3c11fc1SJed Brown 
1853e3c11fc1SJed Brown    Output Parameters:
1854e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1855e3c11fc1SJed Brown 
1856e3c11fc1SJed Brown    Level: developer
1857e3c11fc1SJed Brown 
1858e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1859e3c11fc1SJed Brown @*/
1860e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1861e3c11fc1SJed Brown {
1862e3c11fc1SJed Brown   PetscErrorCode ierr;
1863e3c11fc1SJed Brown   DM             dm;
1864e3c11fc1SJed Brown   DMTS           dmts;
1865e3c11fc1SJed Brown 
1866e3c11fc1SJed Brown   PetscFunctionBegin;
1867e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1868e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1869e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1870e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1871e3c11fc1SJed Brown   PetscFunctionReturn(0);
1872e3c11fc1SJed Brown }
1873e3c11fc1SJed Brown 
1874e3c11fc1SJed Brown /*@
1875efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1876efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1877efe9872eSLisandro Dalcin 
1878d083f849SBarry Smith    Logically Collective on TS
1879efe9872eSLisandro Dalcin 
1880efe9872eSLisandro Dalcin    Input Parameters:
1881efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1882efe9872eSLisandro Dalcin .  u - the solution vector
1883efe9872eSLisandro Dalcin -  v - the time derivative vector
1884efe9872eSLisandro Dalcin 
1885efe9872eSLisandro Dalcin    Level: beginner
1886efe9872eSLisandro Dalcin 
1887efe9872eSLisandro Dalcin @*/
1888efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1889efe9872eSLisandro Dalcin {
1890efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1891efe9872eSLisandro Dalcin 
1892efe9872eSLisandro Dalcin   PetscFunctionBegin;
1893efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1894efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1895efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1896efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1897efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1898efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1899efe9872eSLisandro Dalcin   ts->vec_dot = v;
1900efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1901efe9872eSLisandro Dalcin }
1902efe9872eSLisandro Dalcin 
1903efe9872eSLisandro Dalcin /*@
1904efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1905efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1906efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1907efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1908efe9872eSLisandro Dalcin 
1909efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1910efe9872eSLisandro Dalcin 
1911efe9872eSLisandro Dalcin    Input Parameter:
1912efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1913efe9872eSLisandro Dalcin 
1914efe9872eSLisandro Dalcin    Output Parameter:
1915efe9872eSLisandro Dalcin +  u - the vector containing the solution
1916efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1917efe9872eSLisandro Dalcin 
1918efe9872eSLisandro Dalcin    Level: intermediate
1919efe9872eSLisandro Dalcin 
1920efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1921efe9872eSLisandro Dalcin 
1922efe9872eSLisandro Dalcin @*/
1923efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1924efe9872eSLisandro Dalcin {
1925efe9872eSLisandro Dalcin   PetscFunctionBegin;
1926efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1927efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1928efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1929efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1930efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1931efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1932efe9872eSLisandro Dalcin }
1933efe9872eSLisandro Dalcin 
193455849f57SBarry Smith /*@C
193555849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
193655849f57SBarry Smith 
193755849f57SBarry Smith   Collective on PetscViewer
193855849f57SBarry Smith 
193955849f57SBarry Smith   Input Parameters:
194055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
194155849f57SBarry Smith            some related function before a call to TSLoad().
194255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
194355849f57SBarry Smith 
194455849f57SBarry Smith    Level: intermediate
194555849f57SBarry Smith 
194655849f57SBarry Smith   Notes:
194755849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
194855849f57SBarry Smith 
194955849f57SBarry Smith   Notes for advanced users:
195055849f57SBarry Smith   Most users should not need to know the details of the binary storage
195155849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
195255849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
195355849f57SBarry Smith   format is
195455849f57SBarry Smith .vb
195555849f57SBarry Smith      has not yet been determined
195655849f57SBarry Smith .ve
195755849f57SBarry Smith 
195855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
195955849f57SBarry Smith @*/
1960f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
196155849f57SBarry Smith {
196255849f57SBarry Smith   PetscErrorCode ierr;
196355849f57SBarry Smith   PetscBool      isbinary;
196455849f57SBarry Smith   PetscInt       classid;
196555849f57SBarry Smith   char           type[256];
19662d53ad75SBarry Smith   DMTS           sdm;
1967ad6bc421SBarry Smith   DM             dm;
196855849f57SBarry Smith 
196955849f57SBarry Smith   PetscFunctionBegin;
1970f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
197155849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
197255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
197355849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
197455849f57SBarry Smith 
1975060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1976ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1977060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1978f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1979f2c2a1b9SBarry Smith   if (ts->ops->load) {
1980f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1981f2c2a1b9SBarry Smith   }
1982ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1983ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1984ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1985f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1986f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19872d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19882d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
198955849f57SBarry Smith   PetscFunctionReturn(0);
199055849f57SBarry Smith }
199155849f57SBarry Smith 
19929804daf3SBarry Smith #include <petscdraw.h>
1993e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1994e04113cfSBarry Smith #include <petscviewersaws.h>
1995f05ece33SBarry Smith #endif
1996fe2efc57SMark 
1997fe2efc57SMark /*@C
1998fe2efc57SMark    TSViewFromOptions - View from Options
1999fe2efc57SMark 
2000fe2efc57SMark    Collective on TS
2001fe2efc57SMark 
2002fe2efc57SMark    Input Parameters:
2003fe2efc57SMark +  A - the application ordering context
2004736c3998SJose E. Roman .  obj - Optional object
2005736c3998SJose E. Roman -  name - command line option
2006fe2efc57SMark 
2007fe2efc57SMark    Level: intermediate
2008fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
2009fe2efc57SMark @*/
2010fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
2011fe2efc57SMark {
2012fe2efc57SMark   PetscErrorCode ierr;
2013fe2efc57SMark 
2014fe2efc57SMark   PetscFunctionBegin;
2015fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
2016fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
2017fe2efc57SMark   PetscFunctionReturn(0);
2018fe2efc57SMark }
2019fe2efc57SMark 
20207e2c5f70SBarry Smith /*@C
2021d763cef2SBarry Smith     TSView - Prints the TS data structure.
2022d763cef2SBarry Smith 
20234c49b128SBarry Smith     Collective on TS
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith     Input Parameters:
2026d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2027d763cef2SBarry Smith -   viewer - visualization context
2028d763cef2SBarry Smith 
2029d763cef2SBarry Smith     Options Database Key:
2030d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith     Notes:
2033d763cef2SBarry Smith     The available visualization contexts include
2034b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2035b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2036d763cef2SBarry Smith          output where only the first processor opens
2037d763cef2SBarry Smith          the file.  All other processors send their
2038d763cef2SBarry Smith          data to the first processor to print.
2039d763cef2SBarry Smith 
2040d763cef2SBarry Smith     The user can open an alternative visualization context with
2041b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith     Level: beginner
2044d763cef2SBarry Smith 
2045b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2046d763cef2SBarry Smith @*/
20477087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2048d763cef2SBarry Smith {
2049dfbe8321SBarry Smith   PetscErrorCode ierr;
205019fd82e9SBarry Smith   TSType         type;
20512b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20522d53ad75SBarry Smith   DMTS           sdm;
2053e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2054536b137fSBarry Smith   PetscBool      issaws;
2055f05ece33SBarry Smith #endif
2056d763cef2SBarry Smith 
2057d763cef2SBarry Smith   PetscFunctionBegin;
20580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20593050cee2SBarry Smith   if (!viewer) {
2060ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20613050cee2SBarry Smith   }
20620700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2063c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2064fd16b177SBarry Smith 
2065251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2066251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
206755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20682b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2069e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2070536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2071f05ece33SBarry Smith #endif
207232077d6dSBarry Smith   if (iascii) {
2073dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2074efd4aadfSBarry Smith     if (ts->ops->view) {
2075efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2076efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2077efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2078efd4aadfSBarry Smith     }
2079ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
208077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2081ef85077eSLisandro Dalcin     }
2082ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20837c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2084ef85077eSLisandro Dalcin     }
2085efd4aadfSBarry Smith     if (ts->usessnes) {
2086efd4aadfSBarry Smith       PetscBool lin;
2087d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20885ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2089d763cef2SBarry Smith       }
20905ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
20911ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2092efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2093efd4aadfSBarry Smith     }
2094193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2095a0af407cSBarry Smith     if (ts->vrtol) {
2096a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2097a0af407cSBarry Smith     } else {
2098a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2099a0af407cSBarry Smith     }
2100a0af407cSBarry Smith     if (ts->vatol) {
2101a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2102a0af407cSBarry Smith     } else {
2103a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2104a0af407cSBarry Smith     }
2105825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2106efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2107825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21080f5bd95cSBarry Smith   } else if (isstring) {
2109a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
211036a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
211136a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
211255849f57SBarry Smith   } else if (isbinary) {
211355849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
211455849f57SBarry Smith     MPI_Comm    comm;
211555849f57SBarry Smith     PetscMPIInt rank;
211655849f57SBarry Smith     char        type[256];
211755849f57SBarry Smith 
211855849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
211955849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
212055849f57SBarry Smith     if (!rank) {
2121f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
212255849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2123f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
212455849f57SBarry Smith     }
212555849f57SBarry Smith     if (ts->ops->view) {
212655849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
212755849f57SBarry Smith     }
2128efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2129f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2130f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21312d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21322d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21332b0a91c0SBarry Smith   } else if (isdraw) {
21342b0a91c0SBarry Smith     PetscDraw draw;
21352b0a91c0SBarry Smith     char      str[36];
213689fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21372b0a91c0SBarry Smith 
21382b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21392b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21402b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21412b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
214251fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
214389fd9fafSBarry Smith     bottom = y - h;
21442b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21452b0a91c0SBarry Smith     if (ts->ops->view) {
21462b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21472b0a91c0SBarry Smith     }
2148efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2149efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21502b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2151e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2152536b137fSBarry Smith   } else if (issaws) {
2153d45a07a7SBarry Smith     PetscMPIInt rank;
21542657e9d9SBarry Smith     const char  *name;
21552657e9d9SBarry Smith 
21562657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2157d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2158d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2159d45a07a7SBarry Smith       char       dir[1024];
2160d45a07a7SBarry Smith 
2161e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2162a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21632657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21642657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21652657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2166d763cef2SBarry Smith     }
21670acecf5bSBarry Smith     if (ts->ops->view) {
21680acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21690acecf5bSBarry Smith     }
2170f05ece33SBarry Smith #endif
2171f05ece33SBarry Smith   }
217236a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
217336a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
217436a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
217536a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
217636a9e3b9SBarry Smith   }
217736a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
217836a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2179f05ece33SBarry Smith 
2180b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2181251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2182b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2183d763cef2SBarry Smith   PetscFunctionReturn(0);
2184d763cef2SBarry Smith }
2185d763cef2SBarry Smith 
2186b07ff414SBarry Smith /*@
2187d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2188d763cef2SBarry Smith    the timesteppers.
2189d763cef2SBarry Smith 
21903f9fe445SBarry Smith    Logically Collective on TS
2191d763cef2SBarry Smith 
2192d763cef2SBarry Smith    Input Parameters:
2193d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2194d763cef2SBarry Smith -  usrP - optional user context
2195d763cef2SBarry Smith 
219695452b02SPatrick Sanan    Fortran Notes:
219795452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2198daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2199daf670e6SBarry Smith 
2200d763cef2SBarry Smith    Level: intermediate
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2203d763cef2SBarry Smith @*/
22047087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2205d763cef2SBarry Smith {
2206d763cef2SBarry Smith   PetscFunctionBegin;
22070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2208d763cef2SBarry Smith   ts->user = usrP;
2209d763cef2SBarry Smith   PetscFunctionReturn(0);
2210d763cef2SBarry Smith }
2211d763cef2SBarry Smith 
2212b07ff414SBarry Smith /*@
2213d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2214d763cef2SBarry Smith     timestepper.
2215d763cef2SBarry Smith 
2216d763cef2SBarry Smith     Not Collective
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith     Input Parameter:
2219d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2220d763cef2SBarry Smith 
2221d763cef2SBarry Smith     Output Parameter:
2222d763cef2SBarry Smith .   usrP - user context
2223d763cef2SBarry Smith 
222495452b02SPatrick Sanan    Fortran Notes:
222595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2226daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2227daf670e6SBarry Smith 
2228d763cef2SBarry Smith     Level: intermediate
2229d763cef2SBarry Smith 
2230d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2231d763cef2SBarry Smith @*/
2232e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2233d763cef2SBarry Smith {
2234d763cef2SBarry Smith   PetscFunctionBegin;
22350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2236e71120c6SJed Brown   *(void**)usrP = ts->user;
2237d763cef2SBarry Smith   PetscFunctionReturn(0);
2238d763cef2SBarry Smith }
2239d763cef2SBarry Smith 
2240d763cef2SBarry Smith /*@
224180275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2242d763cef2SBarry Smith 
2243d763cef2SBarry Smith    Not Collective
2244d763cef2SBarry Smith 
2245d763cef2SBarry Smith    Input Parameter:
2246d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2247d763cef2SBarry Smith 
2248d763cef2SBarry Smith    Output Parameter:
224980275a0aSLisandro Dalcin .  steps - number of steps completed so far
2250d763cef2SBarry Smith 
2251d763cef2SBarry Smith    Level: intermediate
2252d763cef2SBarry Smith 
22539be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2254d763cef2SBarry Smith @*/
225580275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2256d763cef2SBarry Smith {
2257d763cef2SBarry Smith   PetscFunctionBegin;
22580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
225980275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
226080275a0aSLisandro Dalcin   *steps = ts->steps;
226180275a0aSLisandro Dalcin   PetscFunctionReturn(0);
226280275a0aSLisandro Dalcin }
226380275a0aSLisandro Dalcin 
226480275a0aSLisandro Dalcin /*@
226580275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
226680275a0aSLisandro Dalcin 
226780275a0aSLisandro Dalcin    Logically Collective on TS
226880275a0aSLisandro Dalcin 
226980275a0aSLisandro Dalcin    Input Parameters:
227080275a0aSLisandro Dalcin +  ts - the TS context
227180275a0aSLisandro Dalcin -  steps - number of steps completed so far
227280275a0aSLisandro Dalcin 
227380275a0aSLisandro Dalcin    Notes:
227480275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
227580275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
227680275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
227780275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
227880275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
227980275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
228080275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
228180275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
228280275a0aSLisandro Dalcin 
228380275a0aSLisandro Dalcin    Level: advanced
228480275a0aSLisandro Dalcin 
228580275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
228680275a0aSLisandro Dalcin @*/
228780275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
228880275a0aSLisandro Dalcin {
228980275a0aSLisandro Dalcin   PetscFunctionBegin;
229080275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
229180275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
229280275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
229380275a0aSLisandro Dalcin   ts->steps = steps;
2294d763cef2SBarry Smith   PetscFunctionReturn(0);
2295d763cef2SBarry Smith }
2296d763cef2SBarry Smith 
2297d763cef2SBarry Smith /*@
2298d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2299d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2300d763cef2SBarry Smith 
23013f9fe445SBarry Smith    Logically Collective on TS
2302d763cef2SBarry Smith 
2303d763cef2SBarry Smith    Input Parameters:
2304d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2305d763cef2SBarry Smith -  time_step - the size of the timestep
2306d763cef2SBarry Smith 
2307d763cef2SBarry Smith    Level: intermediate
2308d763cef2SBarry Smith 
2309aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2310d763cef2SBarry Smith 
2311d763cef2SBarry Smith @*/
23127087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2313d763cef2SBarry Smith {
2314d763cef2SBarry Smith   PetscFunctionBegin;
23150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2316c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2317d763cef2SBarry Smith   ts->time_step = time_step;
2318d763cef2SBarry Smith   PetscFunctionReturn(0);
2319d763cef2SBarry Smith }
2320d763cef2SBarry Smith 
2321a43b19c4SJed Brown /*@
232249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
232349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
232449354f04SShri Abhyankar      or just return at the final time TS computed.
2325a43b19c4SJed Brown 
2326a43b19c4SJed Brown   Logically Collective on TS
2327a43b19c4SJed Brown 
2328a43b19c4SJed Brown    Input Parameter:
2329a43b19c4SJed Brown +   ts - the time-step context
233049354f04SShri Abhyankar -   eftopt - exact final time option
2331a43b19c4SJed Brown 
2332feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2333feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2334feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2335feed9e9dSBarry Smith 
2336feed9e9dSBarry Smith    Options Database:
2337feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2338feed9e9dSBarry Smith 
2339ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2340ee346746SBarry Smith     then the final time you selected.
2341ee346746SBarry Smith 
2342a43b19c4SJed Brown    Level: beginner
2343a43b19c4SJed Brown 
2344f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2345a43b19c4SJed Brown @*/
234649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2347a43b19c4SJed Brown {
2348a43b19c4SJed Brown   PetscFunctionBegin;
2349a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
235049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
235149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2352a43b19c4SJed Brown   PetscFunctionReturn(0);
2353a43b19c4SJed Brown }
2354a43b19c4SJed Brown 
2355d763cef2SBarry Smith /*@
2356f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2357f6953c82SLisandro Dalcin 
2358f6953c82SLisandro Dalcin    Not Collective
2359f6953c82SLisandro Dalcin 
2360f6953c82SLisandro Dalcin    Input Parameter:
2361f6953c82SLisandro Dalcin .  ts - the TS context
2362f6953c82SLisandro Dalcin 
2363f6953c82SLisandro Dalcin    Output Parameter:
2364f6953c82SLisandro Dalcin .  eftopt - exact final time option
2365f6953c82SLisandro Dalcin 
2366f6953c82SLisandro Dalcin    Level: beginner
2367f6953c82SLisandro Dalcin 
2368f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2369f6953c82SLisandro Dalcin @*/
2370f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2371f6953c82SLisandro Dalcin {
2372f6953c82SLisandro Dalcin   PetscFunctionBegin;
2373f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2374f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2375f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2376f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2377f6953c82SLisandro Dalcin }
2378f6953c82SLisandro Dalcin 
2379f6953c82SLisandro Dalcin /*@
2380d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2381d763cef2SBarry Smith 
2382d763cef2SBarry Smith    Not Collective
2383d763cef2SBarry Smith 
2384d763cef2SBarry Smith    Input Parameter:
2385d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2386d763cef2SBarry Smith 
2387d763cef2SBarry Smith    Output Parameter:
2388d763cef2SBarry Smith .  dt - the current timestep size
2389d763cef2SBarry Smith 
2390d763cef2SBarry Smith    Level: intermediate
2391d763cef2SBarry Smith 
2392aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2393d763cef2SBarry Smith 
2394d763cef2SBarry Smith @*/
23957087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2396d763cef2SBarry Smith {
2397d763cef2SBarry Smith   PetscFunctionBegin;
23980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2399f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2400d763cef2SBarry Smith   *dt = ts->time_step;
2401d763cef2SBarry Smith   PetscFunctionReturn(0);
2402d763cef2SBarry Smith }
2403d763cef2SBarry Smith 
2404d8e5e3e6SSatish Balay /*@
2405d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2406d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2407d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2408d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2409d763cef2SBarry Smith 
2410d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2411d763cef2SBarry Smith 
2412d763cef2SBarry Smith    Input Parameter:
2413d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith    Output Parameter:
2416d763cef2SBarry Smith .  v - the vector containing the solution
2417d763cef2SBarry Smith 
241863e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
241963e21af5SBarry Smith    final time. It returns the solution at the next timestep.
242063e21af5SBarry Smith 
2421d763cef2SBarry Smith    Level: intermediate
2422d763cef2SBarry Smith 
24230ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2424d763cef2SBarry Smith 
2425d763cef2SBarry Smith @*/
24267087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2427d763cef2SBarry Smith {
2428d763cef2SBarry Smith   PetscFunctionBegin;
24290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24304482741eSBarry Smith   PetscValidPointer(v,2);
24318737fe31SLisandro Dalcin   *v = ts->vec_sol;
2432d763cef2SBarry Smith   PetscFunctionReturn(0);
2433d763cef2SBarry Smith }
2434d763cef2SBarry Smith 
243503fe5f5eSDebojyoti Ghosh /*@
2436b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
243703fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2438b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
243903fe5f5eSDebojyoti Ghosh    structure as the solution vector.
244003fe5f5eSDebojyoti Ghosh 
244103fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
244203fe5f5eSDebojyoti Ghosh 
244303fe5f5eSDebojyoti Ghosh    Parameters :
2444a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2445b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2446b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
244703fe5f5eSDebojyoti Ghosh        returned in v.
2448a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
244903fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2450b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
245103fe5f5eSDebojyoti Ghosh 
24524cdd57e5SDebojyoti Ghosh    Level: advanced
245303fe5f5eSDebojyoti Ghosh 
245403fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
245503fe5f5eSDebojyoti Ghosh 
245603fe5f5eSDebojyoti Ghosh @*/
2457b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
245803fe5f5eSDebojyoti Ghosh {
245903fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
246003fe5f5eSDebojyoti Ghosh 
246103fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
246203fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2463b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
246403fe5f5eSDebojyoti Ghosh   else {
2465b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
246603fe5f5eSDebojyoti Ghosh   }
246703fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
246803fe5f5eSDebojyoti Ghosh }
246903fe5f5eSDebojyoti Ghosh 
24704cdd57e5SDebojyoti Ghosh /*@
24714cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24724cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24734cdd57e5SDebojyoti Ghosh 
24744cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24754cdd57e5SDebojyoti Ghosh 
24764cdd57e5SDebojyoti Ghosh    Parameters :
2477a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2478a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24794cdd57e5SDebojyoti Ghosh 
24804cdd57e5SDebojyoti Ghosh    Level: intermediate
24814cdd57e5SDebojyoti Ghosh 
24824cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24834cdd57e5SDebojyoti Ghosh 
24844cdd57e5SDebojyoti Ghosh @*/
24854cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24864cdd57e5SDebojyoti Ghosh {
24874cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24884cdd57e5SDebojyoti Ghosh 
24894cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24904cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2491f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24924cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2493f6356ec7SDebojyoti Ghosh   } else {
2494f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2495f6356ec7SDebojyoti Ghosh   }
24964cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24974cdd57e5SDebojyoti Ghosh }
24984cdd57e5SDebojyoti Ghosh 
24994cdd57e5SDebojyoti Ghosh /*@
25004cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
25014cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
25024cdd57e5SDebojyoti Ghosh 
25034cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25044cdd57e5SDebojyoti Ghosh 
25059657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
25069657682dSDebojyoti Ghosh 
25074cdd57e5SDebojyoti Ghosh    Parameters :
2508a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2509657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2510a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
25114cdd57e5SDebojyoti Ghosh 
25124cdd57e5SDebojyoti Ghosh    Level: intermediate
25134cdd57e5SDebojyoti Ghosh 
251457df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25154cdd57e5SDebojyoti Ghosh 
25164cdd57e5SDebojyoti Ghosh @*/
25170a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25184cdd57e5SDebojyoti Ghosh {
25194cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25204cdd57e5SDebojyoti Ghosh 
25214cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25224cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2523f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25240a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2525f6356ec7SDebojyoti Ghosh   } else {
2526f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2527f6356ec7SDebojyoti Ghosh   }
25284cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25294cdd57e5SDebojyoti Ghosh }
25304cdd57e5SDebojyoti Ghosh 
253157df6a1bSDebojyoti Ghosh /*@
253257df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
253357df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
253457df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
253557df6a1bSDebojyoti Ghosh 
253657df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
253757df6a1bSDebojyoti Ghosh 
253857df6a1bSDebojyoti Ghosh    Parameters :
2539a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2540a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
254157df6a1bSDebojyoti Ghosh 
254257df6a1bSDebojyoti Ghosh    Level: intermediate
254357df6a1bSDebojyoti Ghosh 
254457df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
254557df6a1bSDebojyoti Ghosh 
254657df6a1bSDebojyoti Ghosh @*/
254757df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
254857df6a1bSDebojyoti Ghosh {
254957df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
255057df6a1bSDebojyoti Ghosh 
255157df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
255257df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25539657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
255457df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
255557df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
255657df6a1bSDebojyoti Ghosh   }
255757df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
255857df6a1bSDebojyoti Ghosh }
255957df6a1bSDebojyoti Ghosh 
2560bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2561d8e5e3e6SSatish Balay /*@
2562bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2563d763cef2SBarry Smith 
2564bdad233fSMatthew Knepley   Not collective
2565d763cef2SBarry Smith 
2566bdad233fSMatthew Knepley   Input Parameters:
2567bdad233fSMatthew Knepley + ts   - The TS
2568bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2569d763cef2SBarry Smith .vb
25700910c330SBarry Smith          U_t - A U = 0      (linear)
25710910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25720910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2573d763cef2SBarry Smith .ve
2574d763cef2SBarry Smith 
2575d763cef2SBarry Smith    Level: beginner
2576d763cef2SBarry Smith 
2577bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2578d763cef2SBarry Smith @*/
25797087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2580a7cc72afSBarry Smith {
25819e2a6581SJed Brown   PetscErrorCode ierr;
25829e2a6581SJed Brown 
2583d763cef2SBarry Smith   PetscFunctionBegin;
25840700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2585bdad233fSMatthew Knepley   ts->problem_type = type;
25869e2a6581SJed Brown   if (type == TS_LINEAR) {
25879e2a6581SJed Brown     SNES snes;
25889e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25899e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25909e2a6581SJed Brown   }
2591d763cef2SBarry Smith   PetscFunctionReturn(0);
2592d763cef2SBarry Smith }
2593d763cef2SBarry Smith 
2594bdad233fSMatthew Knepley /*@C
2595bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2596bdad233fSMatthew Knepley 
2597bdad233fSMatthew Knepley   Not collective
2598bdad233fSMatthew Knepley 
2599bdad233fSMatthew Knepley   Input Parameter:
2600bdad233fSMatthew Knepley . ts   - The TS
2601bdad233fSMatthew Knepley 
2602bdad233fSMatthew Knepley   Output Parameter:
2603bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2604bdad233fSMatthew Knepley .vb
2605089b2837SJed Brown          M U_t = A U
2606089b2837SJed Brown          M(t) U_t = A(t) U
2607b5abc632SBarry Smith          F(t,U,U_t)
2608bdad233fSMatthew Knepley .ve
2609bdad233fSMatthew Knepley 
2610bdad233fSMatthew Knepley    Level: beginner
2611bdad233fSMatthew Knepley 
2612bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2613bdad233fSMatthew Knepley @*/
26147087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2615a7cc72afSBarry Smith {
2616bdad233fSMatthew Knepley   PetscFunctionBegin;
26170700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26184482741eSBarry Smith   PetscValidIntPointer(type,2);
2619bdad233fSMatthew Knepley   *type = ts->problem_type;
2620bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2621bdad233fSMatthew Knepley }
2622d763cef2SBarry Smith 
2623303a5415SBarry Smith /*
2624303a5415SBarry 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()
2625303a5415SBarry Smith */
2626303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2627303a5415SBarry Smith {
2628303a5415SBarry Smith   PetscErrorCode ierr;
2629303a5415SBarry Smith   PetscBool      isnone;
2630303a5415SBarry Smith 
2631303a5415SBarry Smith   PetscFunctionBegin;
2632303a5415SBarry Smith   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
2633303a5415SBarry Smith   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
2634303a5415SBarry Smith 
2635303a5415SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
2636303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2637303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2638303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2639303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2640303a5415SBarry Smith   }
2641303a5415SBarry Smith   PetscFunctionReturn(0);
2642303a5415SBarry Smith }
2643303a5415SBarry Smith 
2644303a5415SBarry Smith 
2645d763cef2SBarry Smith /*@
2646303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2647d763cef2SBarry Smith 
2648d763cef2SBarry Smith    Collective on TS
2649d763cef2SBarry Smith 
2650d763cef2SBarry Smith    Input Parameter:
2651d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2652d763cef2SBarry Smith 
2653d763cef2SBarry Smith    Notes:
2654d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2655d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2656141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2657d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2658141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2659d763cef2SBarry Smith 
2660d763cef2SBarry Smith    Level: advanced
2661d763cef2SBarry Smith 
2662141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2663d763cef2SBarry Smith @*/
26647087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2665d763cef2SBarry Smith {
2666dfbe8321SBarry Smith   PetscErrorCode ierr;
26676c6b9e74SPeter Brune   DM             dm;
26686c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2669d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2670cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26716c6b9e74SPeter Brune   TSIJacobian    ijac;
2672efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26736c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2674d763cef2SBarry Smith 
2675d763cef2SBarry Smith   PetscFunctionBegin;
26760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2677277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2678277b19d0SLisandro Dalcin 
26797adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2680cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2681cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2682d763cef2SBarry Smith   }
2683277b19d0SLisandro Dalcin 
26841a638600SStefano Zampini   if (!ts->vec_sol) {
26851a638600SStefano Zampini     if (ts->dm) {
26861a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
26871a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
26881a638600SStefano Zampini   }
2689277b19d0SLisandro Dalcin 
2690298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2691298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2692298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2693298bade4SHong Zhang   }
2694298bade4SHong Zhang 
2695cd4cee2dSHong Zhang   if (ts->quadraturets) {
2696cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2697ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2698cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2699cd4cee2dSHong Zhang   }
2700cd4cee2dSHong Zhang 
2701971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2702f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2703e1244c69SJed Brown     Mat Amat,Pmat;
2704e1244c69SJed Brown     SNES snes;
2705e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2706e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2707e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2708e1244c69SJed Brown      * have displaced the RHS matrix */
2709971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2710abc0d4abSBarry 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 */
2711abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2712e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2713e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2714e1244c69SJed Brown     }
2715971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2716abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2717e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2718e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2719e1244c69SJed Brown     }
2720e1244c69SJed Brown   }
27212ffb9264SLisandro Dalcin 
27222ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27232ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27242ffb9264SLisandro Dalcin 
2725277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2726000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2727277b19d0SLisandro Dalcin   }
2728277b19d0SLisandro Dalcin 
2729303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
27302ffb9264SLisandro Dalcin 
2731a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27326c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27336c6b9e74SPeter Brune    */
27346c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27350298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27366c6b9e74SPeter Brune   if (!func) {
27376c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27386c6b9e74SPeter Brune   }
2739a6772fa2SLisandro 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.
27406c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27416c6b9e74SPeter Brune    */
27420298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27430298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2744efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27450298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2746efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27476c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27486c6b9e74SPeter Brune   }
2749c0517034SDebojyoti Ghosh 
2750c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2751c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2752c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2753c0517034SDebojyoti Ghosh   }
2754c0517034SDebojyoti Ghosh 
2755277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2756277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2757277b19d0SLisandro Dalcin }
2758277b19d0SLisandro Dalcin 
2759f6a906c0SBarry Smith /*@
2760277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2761277b19d0SLisandro Dalcin 
2762277b19d0SLisandro Dalcin    Collective on TS
2763277b19d0SLisandro Dalcin 
2764277b19d0SLisandro Dalcin    Input Parameter:
2765277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2766277b19d0SLisandro Dalcin 
2767277b19d0SLisandro Dalcin    Level: beginner
2768277b19d0SLisandro Dalcin 
2769277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2770277b19d0SLisandro Dalcin @*/
2771277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2772277b19d0SLisandro Dalcin {
27731d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2774277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2775277b19d0SLisandro Dalcin 
2776277b19d0SLisandro Dalcin   PetscFunctionBegin;
2777277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2778b18ea86cSHong Zhang 
2779277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2780277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2781277b19d0SLisandro Dalcin   }
2782277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2783e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2784bbd56ea5SKarl Rupp 
27854e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27864e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2787214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27886bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2789efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2790e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2791e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
279238637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2793bbd56ea5SKarl Rupp 
2794cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2795ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2796ecf68647SHong Zhang   if (ts->forward_solve) {
2797ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2798ecf68647SHong Zhang   }
2799cd4cee2dSHong Zhang   if (ts->quadraturets) {
2800cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
280136eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2802cd4cee2dSHong Zhang   }
28031d06f6b3SHong Zhang   while (ilink) {
28041d06f6b3SHong Zhang     next = ilink->next;
28051d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
28061d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
28071d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
28081d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
28091d06f6b3SHong Zhang     ilink = next;
281087f4e208SHong Zhang   }
2811545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2812277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2813d763cef2SBarry Smith   PetscFunctionReturn(0);
2814d763cef2SBarry Smith }
2815d763cef2SBarry Smith 
2816d8e5e3e6SSatish Balay /*@
2817d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2818d763cef2SBarry Smith    with TSCreate().
2819d763cef2SBarry Smith 
2820d763cef2SBarry Smith    Collective on TS
2821d763cef2SBarry Smith 
2822d763cef2SBarry Smith    Input Parameter:
2823d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2824d763cef2SBarry Smith 
2825d763cef2SBarry Smith    Level: beginner
2826d763cef2SBarry Smith 
2827d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2828d763cef2SBarry Smith @*/
28296bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2830d763cef2SBarry Smith {
28316849ba73SBarry Smith   PetscErrorCode ierr;
2832d763cef2SBarry Smith 
2833d763cef2SBarry Smith   PetscFunctionBegin;
28346bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2835ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2836c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2837d763cef2SBarry Smith 
2838ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2839ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2840ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2841ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2842ecf68647SHong Zhang   }
2843e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2844e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28456bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28466d4c513bSLisandro Dalcin 
2847bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2848bc952696SBarry Smith 
284984df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28506427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28516427ac75SLisandro Dalcin 
28526bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28536bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28546bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28550dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28566d4c513bSLisandro Dalcin 
2857cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2858a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2859d763cef2SBarry Smith   PetscFunctionReturn(0);
2860d763cef2SBarry Smith }
2861d763cef2SBarry Smith 
2862d8e5e3e6SSatish Balay /*@
2863d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2864d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2865d763cef2SBarry Smith 
2866d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2867d763cef2SBarry Smith 
2868d763cef2SBarry Smith    Input Parameter:
2869d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2870d763cef2SBarry Smith 
2871d763cef2SBarry Smith    Output Parameter:
2872d763cef2SBarry Smith .  snes - the nonlinear solver context
2873d763cef2SBarry Smith 
2874d763cef2SBarry Smith    Notes:
2875d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2876d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
287794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2878d763cef2SBarry Smith 
2879d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28800298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2881d763cef2SBarry Smith 
2882d763cef2SBarry Smith    Level: beginner
2883d763cef2SBarry Smith 
2884d763cef2SBarry Smith @*/
28857087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2886d763cef2SBarry Smith {
2887d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2888d372ba47SLisandro Dalcin 
2889d763cef2SBarry Smith   PetscFunctionBegin;
28900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28914482741eSBarry Smith   PetscValidPointer(snes,2);
2892d372ba47SLisandro Dalcin   if (!ts->snes) {
2893ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
289416413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
28950298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28963bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2897d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2898496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28999e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
29009e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
29019e2a6581SJed Brown     }
2902d372ba47SLisandro Dalcin   }
2903d763cef2SBarry Smith   *snes = ts->snes;
2904d763cef2SBarry Smith   PetscFunctionReturn(0);
2905d763cef2SBarry Smith }
2906d763cef2SBarry Smith 
2907deb2cd25SJed Brown /*@
2908deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2909deb2cd25SJed Brown 
2910deb2cd25SJed Brown    Collective
2911deb2cd25SJed Brown 
2912deb2cd25SJed Brown    Input Parameter:
2913deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2914deb2cd25SJed Brown -  snes - the nonlinear solver context
2915deb2cd25SJed Brown 
2916deb2cd25SJed Brown    Notes:
2917deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2918deb2cd25SJed Brown 
2919deb2cd25SJed Brown    Level: developer
2920deb2cd25SJed Brown 
2921deb2cd25SJed Brown @*/
2922deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2923deb2cd25SJed Brown {
2924deb2cd25SJed Brown   PetscErrorCode ierr;
2925d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2926deb2cd25SJed Brown 
2927deb2cd25SJed Brown   PetscFunctionBegin;
2928deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2929deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2930deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2931deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2932bbd56ea5SKarl Rupp 
2933deb2cd25SJed Brown   ts->snes = snes;
2934bbd56ea5SKarl Rupp 
29350298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29360298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2937740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29380298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2939740132f1SEmil Constantinescu   }
2940deb2cd25SJed Brown   PetscFunctionReturn(0);
2941deb2cd25SJed Brown }
2942deb2cd25SJed Brown 
2943d8e5e3e6SSatish Balay /*@
294494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2945d763cef2SBarry Smith    a TS (timestepper) context.
2946d763cef2SBarry Smith 
294794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2948d763cef2SBarry Smith 
2949d763cef2SBarry Smith    Input Parameter:
2950d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2951d763cef2SBarry Smith 
2952d763cef2SBarry Smith    Output Parameter:
295394b7f48cSBarry Smith .  ksp - the nonlinear solver context
2954d763cef2SBarry Smith 
2955d763cef2SBarry Smith    Notes:
295694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2957d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2958d763cef2SBarry Smith    KSP and PC contexts as well.
2959d763cef2SBarry Smith 
296094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29610298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2962d763cef2SBarry Smith 
2963d763cef2SBarry Smith    Level: beginner
2964d763cef2SBarry Smith 
2965d763cef2SBarry Smith @*/
29667087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2967d763cef2SBarry Smith {
2968d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2969089b2837SJed Brown   SNES           snes;
2970d372ba47SLisandro Dalcin 
2971d763cef2SBarry Smith   PetscFunctionBegin;
29720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29734482741eSBarry Smith   PetscValidPointer(ksp,2);
297417186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2975e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2976089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2977089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2978d763cef2SBarry Smith   PetscFunctionReturn(0);
2979d763cef2SBarry Smith }
2980d763cef2SBarry Smith 
2981d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2982d763cef2SBarry Smith 
2983adb62b0dSMatthew Knepley /*@
2984618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2985618ce8baSLisandro Dalcin 
2986618ce8baSLisandro Dalcin    Logically Collective on TS
2987618ce8baSLisandro Dalcin 
2988618ce8baSLisandro Dalcin    Input Parameters:
2989618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2990618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2991618ce8baSLisandro Dalcin 
2992618ce8baSLisandro Dalcin    Options Database Keys:
2993618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2994618ce8baSLisandro Dalcin 
2995618ce8baSLisandro Dalcin    Notes:
2996618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2997618ce8baSLisandro Dalcin 
2998618ce8baSLisandro Dalcin    Level: intermediate
2999618ce8baSLisandro Dalcin 
3000618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
3001618ce8baSLisandro Dalcin @*/
3002618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
3003618ce8baSLisandro Dalcin {
3004618ce8baSLisandro Dalcin   PetscFunctionBegin;
3005618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3006618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3007618ce8baSLisandro Dalcin   if (maxsteps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
3008618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
3009618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3010618ce8baSLisandro Dalcin }
3011618ce8baSLisandro Dalcin 
3012618ce8baSLisandro Dalcin /*@
3013618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
3014618ce8baSLisandro Dalcin 
3015618ce8baSLisandro Dalcin    Not Collective
3016618ce8baSLisandro Dalcin 
3017618ce8baSLisandro Dalcin    Input Parameters:
3018618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3019618ce8baSLisandro Dalcin 
3020618ce8baSLisandro Dalcin    Output Parameter:
3021618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3022618ce8baSLisandro Dalcin 
3023618ce8baSLisandro Dalcin    Level: advanced
3024618ce8baSLisandro Dalcin 
3025618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3026618ce8baSLisandro Dalcin @*/
3027618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3028618ce8baSLisandro Dalcin {
3029618ce8baSLisandro Dalcin   PetscFunctionBegin;
3030618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3031618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3032618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3033618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3034618ce8baSLisandro Dalcin }
3035618ce8baSLisandro Dalcin 
3036618ce8baSLisandro Dalcin /*@
3037618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3038618ce8baSLisandro Dalcin 
3039618ce8baSLisandro Dalcin    Logically Collective on TS
3040618ce8baSLisandro Dalcin 
3041618ce8baSLisandro Dalcin    Input Parameters:
3042618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3043618ce8baSLisandro Dalcin -  maxtime - final time to step to
3044618ce8baSLisandro Dalcin 
3045618ce8baSLisandro Dalcin    Options Database Keys:
3046ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3047618ce8baSLisandro Dalcin 
3048618ce8baSLisandro Dalcin    Notes:
3049618ce8baSLisandro Dalcin    The default maximum time is 5.0
3050618ce8baSLisandro Dalcin 
3051618ce8baSLisandro Dalcin    Level: intermediate
3052618ce8baSLisandro Dalcin 
3053618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3054618ce8baSLisandro Dalcin @*/
3055618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3056618ce8baSLisandro Dalcin {
3057618ce8baSLisandro Dalcin   PetscFunctionBegin;
3058618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3059618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3060618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3061618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3062618ce8baSLisandro Dalcin }
3063618ce8baSLisandro Dalcin 
3064618ce8baSLisandro Dalcin /*@
3065618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3066618ce8baSLisandro Dalcin 
3067618ce8baSLisandro Dalcin    Not Collective
3068618ce8baSLisandro Dalcin 
3069618ce8baSLisandro Dalcin    Input Parameters:
3070618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3071618ce8baSLisandro Dalcin 
3072618ce8baSLisandro Dalcin    Output Parameter:
3073618ce8baSLisandro Dalcin .  maxtime - final time to step to
3074618ce8baSLisandro Dalcin 
3075618ce8baSLisandro Dalcin    Level: advanced
3076618ce8baSLisandro Dalcin 
3077618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3078618ce8baSLisandro Dalcin @*/
3079618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3080618ce8baSLisandro Dalcin {
3081618ce8baSLisandro Dalcin   PetscFunctionBegin;
3082618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3083618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3084618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3085618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3086618ce8baSLisandro Dalcin }
3087618ce8baSLisandro Dalcin 
3088618ce8baSLisandro Dalcin /*@
3089aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3090edc382c3SSatish Balay 
3091edc382c3SSatish Balay    Level: deprecated
3092edc382c3SSatish Balay 
3093aaa6c58dSLisandro Dalcin @*/
3094aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3095aaa6c58dSLisandro Dalcin {
3096aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3097aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3098aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3099aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3100aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3101aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3102aaa6c58dSLisandro Dalcin }
3103aaa6c58dSLisandro Dalcin 
3104aaa6c58dSLisandro Dalcin /*@
310519eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3106edc382c3SSatish Balay 
3107edc382c3SSatish Balay    Level: deprecated
3108edc382c3SSatish Balay 
3109adb62b0dSMatthew Knepley @*/
31107087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3111adb62b0dSMatthew Knepley {
3112adb62b0dSMatthew Knepley   PetscFunctionBegin;
31130700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3114abc0a331SBarry Smith   if (maxsteps) {
31154482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3116adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3117adb62b0dSMatthew Knepley   }
3118abc0a331SBarry Smith   if (maxtime) {
31194482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3120adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3121adb62b0dSMatthew Knepley   }
3122adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3123adb62b0dSMatthew Knepley }
3124adb62b0dSMatthew Knepley 
3125d763cef2SBarry Smith /*@
312619eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3127edc382c3SSatish Balay 
3128edc382c3SSatish Balay    Level: deprecated
3129edc382c3SSatish Balay 
3130d763cef2SBarry Smith @*/
31317087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3132d763cef2SBarry Smith {
3133d763cef2SBarry Smith   PetscFunctionBegin;
31340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3135c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3136c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
313739b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
313839b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3139d763cef2SBarry Smith   PetscFunctionReturn(0);
3140d763cef2SBarry Smith }
3141d763cef2SBarry Smith 
3142d763cef2SBarry Smith /*@
31435c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3144edc382c3SSatish Balay 
3145edc382c3SSatish Balay    Level: deprecated
3146edc382c3SSatish Balay 
31475c5f5948SLisandro Dalcin @*/
3148e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31495c5f5948SLisandro Dalcin 
315019eac22cSLisandro Dalcin /*@
31514f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3152edc382c3SSatish Balay 
3153edc382c3SSatish Balay    Level: deprecated
3154edc382c3SSatish Balay 
31554f4e0956SLisandro Dalcin @*/
31564f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31574f4e0956SLisandro Dalcin 
31584f4e0956SLisandro Dalcin /*@
3159d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3160d763cef2SBarry Smith    for use by the TS routines.
3161d763cef2SBarry Smith 
3162d083f849SBarry Smith    Logically Collective on TS
3163d763cef2SBarry Smith 
3164d763cef2SBarry Smith    Input Parameters:
3165d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31660910c330SBarry Smith -  u - the solution vector
3167d763cef2SBarry Smith 
3168d763cef2SBarry Smith    Level: beginner
3169d763cef2SBarry Smith 
31700ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3171d763cef2SBarry Smith @*/
31720910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3173d763cef2SBarry Smith {
31748737fe31SLisandro Dalcin   PetscErrorCode ierr;
3175496e6a7aSJed Brown   DM             dm;
31768737fe31SLisandro Dalcin 
3177d763cef2SBarry Smith   PetscFunctionBegin;
31780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31790910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31800910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31816bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31820910c330SBarry Smith   ts->vec_sol = u;
3183bbd56ea5SKarl Rupp 
3184496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
31850910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3186d763cef2SBarry Smith   PetscFunctionReturn(0);
3187d763cef2SBarry Smith }
3188d763cef2SBarry Smith 
3189ac226902SBarry Smith /*@C
3190000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31913f2090d5SJed Brown   called once at the beginning of each time step.
3192000e7ae3SMatthew Knepley 
31933f9fe445SBarry Smith   Logically Collective on TS
3194000e7ae3SMatthew Knepley 
3195000e7ae3SMatthew Knepley   Input Parameters:
3196000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3197000e7ae3SMatthew Knepley - func - The function
3198000e7ae3SMatthew Knepley 
3199000e7ae3SMatthew Knepley   Calling sequence of func:
32006bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3201000e7ae3SMatthew Knepley 
3202000e7ae3SMatthew Knepley   Level: intermediate
3203000e7ae3SMatthew Knepley 
3204dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3205000e7ae3SMatthew Knepley @*/
32067087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3207000e7ae3SMatthew Knepley {
3208000e7ae3SMatthew Knepley   PetscFunctionBegin;
32090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3210ae60f76fSBarry Smith   ts->prestep = func;
3211000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3212000e7ae3SMatthew Knepley }
3213000e7ae3SMatthew Knepley 
321409ee8438SJed Brown /*@
32153f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32163f2090d5SJed Brown 
32173f2090d5SJed Brown   Collective on TS
32183f2090d5SJed Brown 
32193f2090d5SJed Brown   Input Parameters:
32203f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32213f2090d5SJed Brown 
32223f2090d5SJed Brown   Notes:
32233f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32243f2090d5SJed Brown   so most users would not generally call this routine themselves.
32253f2090d5SJed Brown 
32263f2090d5SJed Brown   Level: developer
32273f2090d5SJed Brown 
32289be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32293f2090d5SJed Brown @*/
32307087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32313f2090d5SJed Brown {
32323f2090d5SJed Brown   PetscErrorCode ierr;
32333f2090d5SJed Brown 
32343f2090d5SJed Brown   PetscFunctionBegin;
32350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3236ae60f76fSBarry Smith   if (ts->prestep) {
32375efd42a4SStefano Zampini     Vec              U;
32385efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32395efd42a4SStefano Zampini 
32405efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32415efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3242ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32435efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3244dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3245312ce896SJed Brown   }
32463f2090d5SJed Brown   PetscFunctionReturn(0);
32473f2090d5SJed Brown }
32483f2090d5SJed Brown 
3249b8123daeSJed Brown /*@C
3250b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3251b8123daeSJed Brown   called once at the beginning of each stage.
3252b8123daeSJed Brown 
3253b8123daeSJed Brown   Logically Collective on TS
3254b8123daeSJed Brown 
3255b8123daeSJed Brown   Input Parameters:
3256b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3257b8123daeSJed Brown - func - The function
3258b8123daeSJed Brown 
3259b8123daeSJed Brown   Calling sequence of func:
3260b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3261b8123daeSJed Brown 
3262b8123daeSJed Brown   Level: intermediate
3263b8123daeSJed Brown 
3264b8123daeSJed Brown   Note:
3265b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
326680275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3267b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3268b8123daeSJed Brown 
32699be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3270b8123daeSJed Brown @*/
3271b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3272b8123daeSJed Brown {
3273b8123daeSJed Brown   PetscFunctionBegin;
3274b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3275ae60f76fSBarry Smith   ts->prestage = func;
3276b8123daeSJed Brown   PetscFunctionReturn(0);
3277b8123daeSJed Brown }
3278b8123daeSJed Brown 
32799be3e283SDebojyoti Ghosh /*@C
32809be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32819be3e283SDebojyoti Ghosh   called once at the end of each stage.
32829be3e283SDebojyoti Ghosh 
32839be3e283SDebojyoti Ghosh   Logically Collective on TS
32849be3e283SDebojyoti Ghosh 
32859be3e283SDebojyoti Ghosh   Input Parameters:
32869be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32879be3e283SDebojyoti Ghosh - func - The function
32889be3e283SDebojyoti Ghosh 
32899be3e283SDebojyoti Ghosh   Calling sequence of func:
32909be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32919be3e283SDebojyoti Ghosh 
32929be3e283SDebojyoti Ghosh   Level: intermediate
32939be3e283SDebojyoti Ghosh 
32949be3e283SDebojyoti Ghosh   Note:
32959be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
329680275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32979be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32989be3e283SDebojyoti Ghosh 
32999be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33009be3e283SDebojyoti Ghosh @*/
33019be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33029be3e283SDebojyoti Ghosh {
33039be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33049be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33059be3e283SDebojyoti Ghosh   ts->poststage = func;
33069be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33079be3e283SDebojyoti Ghosh }
33089be3e283SDebojyoti Ghosh 
3309c688d042SShri Abhyankar /*@C
3310c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3311c688d042SShri Abhyankar   called once at the end of each step evaluation.
3312c688d042SShri Abhyankar 
3313c688d042SShri Abhyankar   Logically Collective on TS
3314c688d042SShri Abhyankar 
3315c688d042SShri Abhyankar   Input Parameters:
3316c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3317c688d042SShri Abhyankar - func - The function
3318c688d042SShri Abhyankar 
3319c688d042SShri Abhyankar   Calling sequence of func:
3320c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3321c688d042SShri Abhyankar 
3322c688d042SShri Abhyankar   Level: intermediate
3323c688d042SShri Abhyankar 
3324c688d042SShri Abhyankar   Note:
33251785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33261785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3327e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3328e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3329e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3330c688d042SShri Abhyankar 
3331c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3332c688d042SShri Abhyankar @*/
3333c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3334c688d042SShri Abhyankar {
3335c688d042SShri Abhyankar   PetscFunctionBegin;
3336c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3337c688d042SShri Abhyankar   ts->postevaluate = func;
3338c688d042SShri Abhyankar   PetscFunctionReturn(0);
3339c688d042SShri Abhyankar }
3340c688d042SShri Abhyankar 
3341b8123daeSJed Brown /*@
3342b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3343b8123daeSJed Brown 
3344b8123daeSJed Brown   Collective on TS
3345b8123daeSJed Brown 
3346b8123daeSJed Brown   Input Parameters:
3347b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33489be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3349b8123daeSJed Brown 
3350b8123daeSJed Brown   Notes:
3351b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3352b8123daeSJed Brown   most users would not generally call this routine themselves.
3353b8123daeSJed Brown 
3354b8123daeSJed Brown   Level: developer
3355b8123daeSJed Brown 
33569be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3357b8123daeSJed Brown @*/
3358b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3359b8123daeSJed Brown {
3360b8123daeSJed Brown   PetscFunctionBegin;
3361b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3362ae60f76fSBarry Smith   if (ts->prestage) {
3363ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3364b8123daeSJed Brown   }
3365b8123daeSJed Brown   PetscFunctionReturn(0);
3366b8123daeSJed Brown }
3367b8123daeSJed Brown 
33689be3e283SDebojyoti Ghosh /*@
33699be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33709be3e283SDebojyoti Ghosh 
33719be3e283SDebojyoti Ghosh   Collective on TS
33729be3e283SDebojyoti Ghosh 
33739be3e283SDebojyoti Ghosh   Input Parameters:
33749be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33759be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33769be3e283SDebojyoti Ghosh   stageindex  - Stage number
33779be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33789be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33799be3e283SDebojyoti Ghosh 
33809be3e283SDebojyoti Ghosh   Notes:
33819be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33829be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33839be3e283SDebojyoti Ghosh 
33849be3e283SDebojyoti Ghosh   Level: developer
33859be3e283SDebojyoti Ghosh 
33869be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33879be3e283SDebojyoti Ghosh @*/
33889be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33899be3e283SDebojyoti Ghosh {
33909be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33919be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33924beae5d8SLisandro Dalcin   if (ts->poststage) {
33939be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33949be3e283SDebojyoti Ghosh   }
33959be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33969be3e283SDebojyoti Ghosh }
33979be3e283SDebojyoti Ghosh 
3398c688d042SShri Abhyankar /*@
3399c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3400c688d042SShri Abhyankar 
3401c688d042SShri Abhyankar   Collective on TS
3402c688d042SShri Abhyankar 
3403c688d042SShri Abhyankar   Input Parameters:
3404c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3405c688d042SShri Abhyankar 
3406c688d042SShri Abhyankar   Notes:
3407c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3408c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3409c688d042SShri Abhyankar 
3410c688d042SShri Abhyankar   Level: developer
3411c688d042SShri Abhyankar 
3412c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3413c688d042SShri Abhyankar @*/
3414c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3415c688d042SShri Abhyankar {
3416c688d042SShri Abhyankar   PetscErrorCode ierr;
3417c688d042SShri Abhyankar 
3418c688d042SShri Abhyankar   PetscFunctionBegin;
3419c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3420c688d042SShri Abhyankar   if (ts->postevaluate) {
3421dcb233daSLisandro Dalcin     Vec              U;
3422dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3423dcb233daSLisandro Dalcin 
3424dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3425dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3426c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3427dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3428dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3429c688d042SShri Abhyankar   }
3430c688d042SShri Abhyankar   PetscFunctionReturn(0);
3431c688d042SShri Abhyankar }
3432c688d042SShri Abhyankar 
3433ac226902SBarry Smith /*@C
3434000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34353f2090d5SJed Brown   called once at the end of each time step.
3436000e7ae3SMatthew Knepley 
34373f9fe445SBarry Smith   Logically Collective on TS
3438000e7ae3SMatthew Knepley 
3439000e7ae3SMatthew Knepley   Input Parameters:
3440000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3441000e7ae3SMatthew Knepley - func - The function
3442000e7ae3SMatthew Knepley 
3443000e7ae3SMatthew Knepley   Calling sequence of func:
3444b8123daeSJed Brown $ func (TS ts);
3445000e7ae3SMatthew Knepley 
34461785ff2aSShri Abhyankar   Notes:
34471785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34481785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34491785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34501785ff2aSShri Abhyankar 
3451000e7ae3SMatthew Knepley   Level: intermediate
3452000e7ae3SMatthew Knepley 
3453dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3454000e7ae3SMatthew Knepley @*/
34557087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3456000e7ae3SMatthew Knepley {
3457000e7ae3SMatthew Knepley   PetscFunctionBegin;
34580700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3459ae60f76fSBarry Smith   ts->poststep = func;
3460000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3461000e7ae3SMatthew Knepley }
3462000e7ae3SMatthew Knepley 
346309ee8438SJed Brown /*@
34643f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34653f2090d5SJed Brown 
34663f2090d5SJed Brown   Collective on TS
34673f2090d5SJed Brown 
34683f2090d5SJed Brown   Input Parameters:
34693f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34703f2090d5SJed Brown 
34713f2090d5SJed Brown   Notes:
34723f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34733f2090d5SJed Brown   so most users would not generally call this routine themselves.
34743f2090d5SJed Brown 
34753f2090d5SJed Brown   Level: developer
34763f2090d5SJed Brown 
34773f2090d5SJed Brown @*/
34787087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34793f2090d5SJed Brown {
34803f2090d5SJed Brown   PetscErrorCode ierr;
34813f2090d5SJed Brown 
34823f2090d5SJed Brown   PetscFunctionBegin;
34830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3484ae60f76fSBarry Smith   if (ts->poststep) {
34855efd42a4SStefano Zampini     Vec              U;
34865efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34875efd42a4SStefano Zampini 
34885efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
34895efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3490ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
34915efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3492dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
349372ac3e02SJed Brown   }
34943f2090d5SJed Brown   PetscFunctionReturn(0);
34953f2090d5SJed Brown }
34963f2090d5SJed Brown 
3497d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3498d763cef2SBarry Smith 
3499d763cef2SBarry Smith /*@C
3500a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3501d763cef2SBarry Smith    timestep to display the iteration's  progress.
3502d763cef2SBarry Smith 
35033f9fe445SBarry Smith    Logically Collective on TS
3504d763cef2SBarry Smith 
3505d763cef2SBarry Smith    Input Parameters:
3506d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3507e213d8f1SJed Brown .  monitor - monitoring routine
3508329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35090298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3510b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35110298fd71SBarry Smith           (may be NULL)
3512d763cef2SBarry Smith 
3513e213d8f1SJed Brown    Calling sequence of monitor:
3514dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3515d763cef2SBarry Smith 
3516d763cef2SBarry Smith +    ts - the TS context
351763e21af5SBarry Smith .    steps - iteration number (after the final time step the monitor routine may be called with a step of -1, this indicates the solution has been interpolated to this time)
35181f06c33eSBarry Smith .    time - current time
35190910c330SBarry Smith .    u - current iterate
3520d763cef2SBarry Smith -    mctx - [optional] monitoring context
3521d763cef2SBarry Smith 
3522d763cef2SBarry Smith    Notes:
3523d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3524d763cef2SBarry Smith    already has been loaded.
3525d763cef2SBarry Smith 
352695452b02SPatrick Sanan    Fortran Notes:
352795452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3528025f1a04SBarry Smith 
3529d763cef2SBarry Smith    Level: intermediate
3530d763cef2SBarry Smith 
3531a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3532d763cef2SBarry Smith @*/
3533c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3534d763cef2SBarry Smith {
353578064530SBarry Smith   PetscErrorCode ierr;
353678064530SBarry Smith   PetscInt       i;
353778064530SBarry Smith   PetscBool      identical;
353878064530SBarry Smith 
3539d763cef2SBarry Smith   PetscFunctionBegin;
35400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
354178064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
354278064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
354378064530SBarry Smith     if (identical) PetscFunctionReturn(0);
354478064530SBarry Smith   }
354517186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3546d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35478704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3548d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3549d763cef2SBarry Smith   PetscFunctionReturn(0);
3550d763cef2SBarry Smith }
3551d763cef2SBarry Smith 
3552d763cef2SBarry Smith /*@C
3553a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3554d763cef2SBarry Smith 
35553f9fe445SBarry Smith    Logically Collective on TS
3556d763cef2SBarry Smith 
3557d763cef2SBarry Smith    Input Parameters:
3558d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3559d763cef2SBarry Smith 
3560d763cef2SBarry Smith    Notes:
3561d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3562d763cef2SBarry Smith 
3563d763cef2SBarry Smith    Level: intermediate
3564d763cef2SBarry Smith 
3565a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3566d763cef2SBarry Smith @*/
35677087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3568d763cef2SBarry Smith {
3569d952e501SBarry Smith   PetscErrorCode ierr;
3570d952e501SBarry Smith   PetscInt       i;
3571d952e501SBarry Smith 
3572d763cef2SBarry Smith   PetscFunctionBegin;
35730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3574d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35758704b422SBarry Smith     if (ts->monitordestroy[i]) {
35768704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3577d952e501SBarry Smith     }
3578d952e501SBarry Smith   }
3579d763cef2SBarry Smith   ts->numbermonitors = 0;
3580d763cef2SBarry Smith   PetscFunctionReturn(0);
3581d763cef2SBarry Smith }
3582d763cef2SBarry Smith 
3583721cd6eeSBarry Smith /*@C
3584721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35855516499fSSatish Balay 
35865516499fSSatish Balay    Level: intermediate
358741251cbbSSatish Balay 
358863e21af5SBarry Smith .seealso:  TSMonitorSet()
358941251cbbSSatish Balay @*/
3590721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3591d763cef2SBarry Smith {
3592dfbe8321SBarry Smith   PetscErrorCode ierr;
3593721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
359441aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3595d132466eSBarry Smith 
3596d763cef2SBarry Smith   PetscFunctionBegin;
35974d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
359841aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
359941aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3600721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
360141aca3d6SBarry Smith   if (iascii) {
3602649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
360363e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
360463e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
360563e21af5SBarry Smith     } else {
36068392e04aSShri Abhyankar       ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr);
360763e21af5SBarry Smith     }
3608649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
360941aca3d6SBarry Smith   } else if (ibinary) {
361041aca3d6SBarry Smith     PetscMPIInt rank;
361141aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
361241aca3d6SBarry Smith     if (!rank) {
3613450a797fSBarry Smith       PetscBool skipHeader;
3614450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3615450a797fSBarry Smith 
3616450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3617450a797fSBarry Smith       if (!skipHeader) {
3618f253e43cSLisandro Dalcin          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
3619450a797fSBarry Smith        }
362041aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
362141aca3d6SBarry Smith     } else {
362241aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
362341aca3d6SBarry Smith     }
362441aca3d6SBarry Smith   }
3625721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3626d763cef2SBarry Smith   PetscFunctionReturn(0);
3627d763cef2SBarry Smith }
3628d763cef2SBarry Smith 
3629cc9c3a59SBarry Smith /*@C
3630cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3631cc9c3a59SBarry Smith 
3632cc9c3a59SBarry Smith    Level: intermediate
3633cc9c3a59SBarry Smith 
3634cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3635cc9c3a59SBarry Smith @*/
3636cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3637cc9c3a59SBarry Smith {
3638cc9c3a59SBarry Smith   PetscErrorCode ierr;
3639cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3640cc9c3a59SBarry Smith   PetscBool      iascii;
3641cc9c3a59SBarry Smith   PetscReal      max,min;
3642cc9c3a59SBarry Smith 
3643cc9c3a59SBarry Smith 
3644cc9c3a59SBarry Smith   PetscFunctionBegin;
3645cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3646cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3647cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3648cc9c3a59SBarry Smith   if (iascii) {
3649cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3650cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3651cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
36525132926bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s max %g min %g\n",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)" : "",(double)max,(double)min);CHKERRQ(ierr);
3653cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3654cc9c3a59SBarry Smith   }
3655cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3656cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3657cc9c3a59SBarry Smith }
3658cc9c3a59SBarry Smith 
3659cd652676SJed Brown /*@
3660cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3661cd652676SJed Brown 
3662cd652676SJed Brown    Collective on TS
3663cd652676SJed Brown 
3664cd652676SJed Brown    Input Argument:
3665cd652676SJed Brown +  ts - time stepping context
3666cd652676SJed Brown -  t - time to interpolate to
3667cd652676SJed Brown 
3668cd652676SJed Brown    Output Argument:
36690910c330SBarry Smith .  U - state at given time
3670cd652676SJed Brown 
3671cd652676SJed Brown    Level: intermediate
3672cd652676SJed Brown 
3673cd652676SJed Brown    Developer Notes:
3674cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3675cd652676SJed Brown 
3676874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3677cd652676SJed Brown @*/
36780910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3679cd652676SJed Brown {
3680cd652676SJed Brown   PetscErrorCode ierr;
3681cd652676SJed Brown 
3682cd652676SJed Brown   PetscFunctionBegin;
3683cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3684b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3685be5899b3SLisandro Dalcin   if (t < ts->ptime_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)ts->ptime_prev,(double)ts->ptime);
3686ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36870910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3688cd652676SJed Brown   PetscFunctionReturn(0);
3689cd652676SJed Brown }
3690cd652676SJed Brown 
3691d763cef2SBarry Smith /*@
36926d9e5789SSean Farley    TSStep - Steps one time step
3693d763cef2SBarry Smith 
3694d763cef2SBarry Smith    Collective on TS
3695d763cef2SBarry Smith 
3696d763cef2SBarry Smith    Input Parameter:
3697d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3698d763cef2SBarry Smith 
369927829d71SBarry Smith    Level: developer
3700d763cef2SBarry Smith 
3701b8123daeSJed Brown    Notes:
370227829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
370327829d71SBarry Smith 
3704b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3705b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3706b8123daeSJed Brown 
370719eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
370819eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
370925cb2221SBarry Smith 
37109be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3711d763cef2SBarry Smith @*/
3712193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3713d763cef2SBarry Smith {
3714dfbe8321SBarry Smith   PetscErrorCode   ierr;
3715fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3716be5899b3SLisandro Dalcin   PetscReal        ptime;
3717d763cef2SBarry Smith 
3718d763cef2SBarry Smith   PetscFunctionBegin;
37190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3720f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3721fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3722f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3723f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3724f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3725f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3726f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3727fffbeea8SBarry Smith 
3728d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
372968bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3730d405a339SMatthew Knepley 
3731fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3732ef85077eSLisandro Dalcin   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
3733a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()");
3734be5899b3SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
3735a6772fa2SLisandro Dalcin 
3736be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3737be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37382808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3739fc8dbba5SLisandro Dalcin 
3740d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3741193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3742d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3743fc8dbba5SLisandro Dalcin 
3744fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3745be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
37462808aa04SLisandro Dalcin     ts->steps++;
3747be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
374828d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3749d2daff3dSHong Zhang   }
3750fc8dbba5SLisandro Dalcin 
3751fc8dbba5SLisandro Dalcin   if (!ts->reason) {
375208c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
375308c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
375408c7845fSBarry Smith   }
3755fc8dbba5SLisandro Dalcin 
3756fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed && ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
3757fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
375808c7845fSBarry Smith   PetscFunctionReturn(0);
375908c7845fSBarry Smith }
376008c7845fSBarry Smith 
376108c7845fSBarry Smith /*@
37627cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37637cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37647cbde773SLisandro Dalcin 
37657cbde773SLisandro Dalcin    Collective on TS
37667cbde773SLisandro Dalcin 
37677cbde773SLisandro Dalcin    Input Arguments:
37687cbde773SLisandro Dalcin +  ts - time stepping context
37697cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37707cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37717cbde773SLisandro Dalcin 
37727cbde773SLisandro Dalcin    Output Arguments:
37737cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37747cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37757cbde773SLisandro Dalcin 
37767cbde773SLisandro Dalcin    Level: advanced
37777cbde773SLisandro Dalcin 
37787cbde773SLisandro Dalcin    Notes:
37797cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37807cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37817cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37827cbde773SLisandro Dalcin 
37837cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37847cbde773SLisandro Dalcin @*/
37857cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37867cbde773SLisandro Dalcin {
37877cbde773SLisandro Dalcin   PetscErrorCode ierr;
37887cbde773SLisandro Dalcin 
37897cbde773SLisandro Dalcin   PetscFunctionBegin;
37907cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37917cbde773SLisandro Dalcin   PetscValidType(ts,1);
37927cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37937cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37947cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37957cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37967cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37977cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37987cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37997cbde773SLisandro Dalcin   PetscFunctionReturn(0);
38007cbde773SLisandro Dalcin }
38017cbde773SLisandro Dalcin 
380205175c85SJed Brown /*@
380305175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
380405175c85SJed Brown 
38051c3436cfSJed Brown    Collective on TS
380605175c85SJed Brown 
380705175c85SJed Brown    Input Arguments:
38081c3436cfSJed Brown +  ts - time stepping context
38091c3436cfSJed Brown .  order - desired order of accuracy
38100298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
381105175c85SJed Brown 
381205175c85SJed Brown    Output Arguments:
38130910c330SBarry Smith .  U - state at the end of the current step
381405175c85SJed Brown 
381505175c85SJed Brown    Level: advanced
381605175c85SJed Brown 
3817108c343cSJed Brown    Notes:
3818108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3819108c343cSJed 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.
3820108c343cSJed Brown 
38211c3436cfSJed Brown .seealso: TSStep(), TSAdapt
382205175c85SJed Brown @*/
38230910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
382405175c85SJed Brown {
382505175c85SJed Brown   PetscErrorCode ierr;
382605175c85SJed Brown 
382705175c85SJed Brown   PetscFunctionBegin;
382805175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
382905175c85SJed Brown   PetscValidType(ts,1);
38300910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3831ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38320910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
383305175c85SJed Brown   PetscFunctionReturn(0);
383405175c85SJed Brown }
383505175c85SJed Brown 
3836aad739acSMatthew G. Knepley /*@C
38372e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3838aad739acSMatthew G. Knepley 
3839aad739acSMatthew G. Knepley   Not collective
3840aad739acSMatthew G. Knepley 
3841aad739acSMatthew G. Knepley   Input Argument:
3842aad739acSMatthew G. Knepley . ts        - time stepping context
3843aad739acSMatthew G. Knepley 
3844aad739acSMatthew G. Knepley   Output Argument:
38452e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3846aad739acSMatthew G. Knepley 
3847aad739acSMatthew G. Knepley    Level: advanced
3848aad739acSMatthew G. Knepley 
3849aad739acSMatthew G. Knepley    Notes:
3850aad739acSMatthew G. Knepley    The calling sequence for the function is
38512e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3852aad739acSMatthew G. Knepley $ ts - The timestepping context
38532e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3854aad739acSMatthew G. Knepley 
38552e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3856aad739acSMatthew G. Knepley @*/
38572e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3858aad739acSMatthew G. Knepley {
3859aad739acSMatthew G. Knepley   PetscFunctionBegin;
3860aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38612e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38622e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3863aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3864aad739acSMatthew G. Knepley }
3865aad739acSMatthew G. Knepley 
3866aad739acSMatthew G. Knepley /*@C
38672e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3868aad739acSMatthew G. Knepley 
3869aad739acSMatthew G. Knepley   Logically collective on ts
3870aad739acSMatthew G. Knepley 
3871aad739acSMatthew G. Knepley   Input Arguments:
3872aad739acSMatthew G. Knepley + ts        - time stepping context
38732e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3874aad739acSMatthew G. Knepley 
3875aad739acSMatthew G. Knepley   Level: advanced
3876aad739acSMatthew G. Knepley 
3877a96d6ef6SBarry Smith   Calling sequence for initCondition:
3878a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3879a96d6ef6SBarry Smith 
3880a96d6ef6SBarry Smith + ts - The timestepping context
3881a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3882aad739acSMatthew G. Knepley 
38832e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3884aad739acSMatthew G. Knepley @*/
38852e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3886aad739acSMatthew G. Knepley {
3887aad739acSMatthew G. Knepley   PetscFunctionBegin;
3888aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38892e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
38902e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3891aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3892aad739acSMatthew G. Knepley }
3893aad739acSMatthew G. Knepley 
3894aad739acSMatthew G. Knepley /*@
38952e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3896aad739acSMatthew G. Knepley 
3897aad739acSMatthew G. Knepley   Collective on ts
3898aad739acSMatthew G. Knepley 
3899aad739acSMatthew G. Knepley   Input Arguments:
3900aad739acSMatthew G. Knepley + ts - time stepping context
39012e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3902aad739acSMatthew G. Knepley 
3903aad739acSMatthew G. Knepley   Level: advanced
3904aad739acSMatthew G. Knepley 
39052e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3906aad739acSMatthew G. Knepley @*/
39072e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3908aad739acSMatthew G. Knepley {
3909aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3910aad739acSMatthew G. Knepley 
3911aad739acSMatthew G. Knepley   PetscFunctionBegin;
3912aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3913aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
39142e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3915aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3916aad739acSMatthew G. Knepley }
3917aad739acSMatthew G. Knepley 
3918aad739acSMatthew G. Knepley /*@C
3919aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3920aad739acSMatthew G. Knepley 
3921aad739acSMatthew G. Knepley   Not collective
3922aad739acSMatthew G. Knepley 
3923aad739acSMatthew G. Knepley   Input Argument:
3924aad739acSMatthew G. Knepley . ts         - time stepping context
3925aad739acSMatthew G. Knepley 
3926aad739acSMatthew G. Knepley   Output Argument:
3927aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3928aad739acSMatthew G. Knepley 
3929aad739acSMatthew G. Knepley   Level: advanced
3930aad739acSMatthew G. Knepley 
3931a96d6ef6SBarry Smith   Calling sequence for exactError:
3932a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3933a96d6ef6SBarry Smith 
3934a96d6ef6SBarry Smith + ts - The timestepping context
3935a96d6ef6SBarry Smith . u  - The approximate solution vector
3936a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3937aad739acSMatthew G. Knepley 
3938aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3939aad739acSMatthew G. Knepley @*/
3940aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3941aad739acSMatthew G. Knepley {
3942aad739acSMatthew G. Knepley   PetscFunctionBegin;
3943aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3944aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3945aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3946aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3947aad739acSMatthew G. Knepley }
3948aad739acSMatthew G. Knepley 
3949aad739acSMatthew G. Knepley /*@C
3950aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3951aad739acSMatthew G. Knepley 
3952aad739acSMatthew G. Knepley   Logically collective on ts
3953aad739acSMatthew G. Knepley 
3954aad739acSMatthew G. Knepley   Input Arguments:
3955aad739acSMatthew G. Knepley + ts         - time stepping context
3956aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3957aad739acSMatthew G. Knepley 
3958aad739acSMatthew G. Knepley   Level: advanced
3959aad739acSMatthew G. Knepley 
3960a96d6ef6SBarry Smith   Calling sequence for exactError:
3961a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3962a96d6ef6SBarry Smith 
3963a96d6ef6SBarry Smith + ts - The timestepping context
3964a96d6ef6SBarry Smith . u  - The approximate solution vector
3965a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3966aad739acSMatthew G. Knepley 
3967aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3968aad739acSMatthew G. Knepley @*/
3969aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3970aad739acSMatthew G. Knepley {
3971aad739acSMatthew G. Knepley   PetscFunctionBegin;
3972aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3973f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3974aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3975aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3976aad739acSMatthew G. Knepley }
3977aad739acSMatthew G. Knepley 
3978aad739acSMatthew G. Knepley /*@
3979aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3980aad739acSMatthew G. Knepley 
3981aad739acSMatthew G. Knepley   Collective on ts
3982aad739acSMatthew G. Knepley 
3983aad739acSMatthew G. Knepley   Input Arguments:
3984aad739acSMatthew G. Knepley + ts - time stepping context
3985aad739acSMatthew G. Knepley . u  - The approximate solution
3986aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3987aad739acSMatthew G. Knepley 
3988aad739acSMatthew G. Knepley   Level: advanced
3989aad739acSMatthew G. Knepley 
39902e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3991aad739acSMatthew G. Knepley @*/
3992aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3993aad739acSMatthew G. Knepley {
3994aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3995aad739acSMatthew G. Knepley 
3996aad739acSMatthew G. Knepley   PetscFunctionBegin;
3997aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3998aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3999aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
4000aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
4001aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4002aad739acSMatthew G. Knepley }
4003aad739acSMatthew G. Knepley 
4004b1cb36f3SHong Zhang /*@
40056a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
40066a4d4014SLisandro Dalcin 
40076a4d4014SLisandro Dalcin    Collective on TS
40086a4d4014SLisandro Dalcin 
40096a4d4014SLisandro Dalcin    Input Parameter:
40106a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
401163e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
401263e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
40135a3a76d0SJed Brown 
40146a4d4014SLisandro Dalcin    Level: beginner
40156a4d4014SLisandro Dalcin 
40165a3a76d0SJed Brown    Notes:
40175a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
40185a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40195a3a76d0SJed Brown    stepped over the final time.
40205a3a76d0SJed Brown 
402163e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
40226a4d4014SLisandro Dalcin @*/
4023cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
40246a4d4014SLisandro Dalcin {
4025b06615a5SLisandro Dalcin   Vec               solution;
40266a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
4027f22f69f0SBarry Smith 
40286a4d4014SLisandro Dalcin   PetscFunctionBegin;
40290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4030f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4031303a5415SBarry Smith 
4032303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
4033ee41a567SStefano 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 */
40340910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4035b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4036b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4037b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40385a3a76d0SJed Brown     }
40390910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4040715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4041bbd56ea5SKarl Rupp   } else if (u) {
40420910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40435a3a76d0SJed Brown   }
4044b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
404568bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4046a6772fa2SLisandro Dalcin 
4047ef85077eSLisandro Dalcin   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
4048a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()");
4049a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
4050a6772fa2SLisandro Dalcin 
4051715f1b00SHong Zhang   if (ts->forward_solve) {
4052715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4053715f1b00SHong Zhang   }
4054715f1b00SHong Zhang 
4055e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4056715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4057e7069c78SShri      TSTrajectory to incorrectly save the output files
4058e7069c78SShri   */
4059715f1b00SHong Zhang   /* reset time step and iteration counters */
40602808aa04SLisandro Dalcin   if (!ts->steps) {
40615ef26d82SJed Brown     ts->ksp_its           = 0;
40625ef26d82SJed Brown     ts->snes_its          = 0;
4063c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4064c610991cSLisandro Dalcin     ts->reject            = 0;
40652808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40662808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40677d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
40682808aa04SLisandro Dalcin   }
4069e97c63d7SStefano Zampini 
4070e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4071e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4072e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4073e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4074e97c63d7SStefano Zampini 
4075e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4076e97c63d7SStefano Zampini   }
4077193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4078193ac0bcSJed Brown 
4079900f6b5bSMatthew G. Knepley   {
4080900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4081900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4082900f6b5bSMatthew G. Knepley     PetscBool         flg;
4083900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4084900f6b5bSMatthew G. Knepley 
4085900f6b5bSMatthew G. Knepley     if (!incall) {
4086900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4087900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4088900f6b5bSMatthew G. Knepley       if (flg) {
4089900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4090900f6b5bSMatthew G. Knepley         DM           dm;
4091900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4092900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4093f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4094900f6b5bSMatthew G. Knepley 
4095900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4096f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
4097900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4098900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4099900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4100900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4101f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
4102900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4103900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4104900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4105900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4106900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4107900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4108900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4109900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4110900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4111900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4112900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4113900f6b5bSMatthew G. Knepley       }
4114900f6b5bSMatthew G. Knepley     }
4115900f6b5bSMatthew G. Knepley   }
4116900f6b5bSMatthew G. Knepley 
4117ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4118f05ece33SBarry Smith 
4119193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4120193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4121a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4122cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4123a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4124be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4125db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4126db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4127e7069c78SShri 
41282808aa04SLisandro Dalcin     if (!ts->steps) {
41292808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41306427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41312808aa04SLisandro Dalcin     }
41326427ac75SLisandro Dalcin 
4133e1a7a14fSJed Brown     while (!ts->reason) {
41342808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41359687d888SLisandro Dalcin       if (!ts->steprollback) {
41369687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41379687d888SLisandro Dalcin       }
4138193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4139f3b1f45cSBarry Smith       if (ts->testjacobian) {
4140f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4141f3b1f45cSBarry Smith       }
4142f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4143f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4144f3b1f45cSBarry Smith       }
4145cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
41467b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4147b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
41487b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4149b1cb36f3SHong Zhang       }
415058818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41517b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4152715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
41537b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4154715f1b00SHong Zhang       }
415510b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4156e783b05fSHong 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. */
415758818c2dSLisandro Dalcin       if (ts->steprollback) {
415858818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
415958818c2dSLisandro Dalcin       }
4160e783b05fSHong Zhang       if (!ts->steprollback) {
41612808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41621eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4163aeb4809dSShri Abhyankar       }
4164193ac0bcSJed Brown     }
41652808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41666427ac75SLisandro Dalcin 
416749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41680910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4169cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4170b06615a5SLisandro Dalcin       solution = u;
417163e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41720574a7fbSJed Brown     } else {
4173ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4174cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4175b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41760574a7fbSJed Brown     }
4177193ac0bcSJed Brown   }
4178d2daff3dSHong Zhang 
4179ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
418063e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
418156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4182ef222394SBarry Smith   if (ts->adjoint_solve) {
4183ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41842b0a91c0SBarry Smith   }
41856a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41866a4d4014SLisandro Dalcin }
41876a4d4014SLisandro Dalcin 
41887db568b7SBarry Smith /*@C
4189228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4190228d79bcSJed Brown 
4191228d79bcSJed Brown    Collective on TS
4192228d79bcSJed Brown 
4193228d79bcSJed Brown    Input Parameters:
4194228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4195228d79bcSJed Brown .  step - step number that has just completed
4196228d79bcSJed Brown .  ptime - model time of the state
41970910c330SBarry Smith -  u - state at the current model time
4198228d79bcSJed Brown 
4199228d79bcSJed Brown    Notes:
42007db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
42017db568b7SBarry Smith    Users would almost never call this routine directly.
4202228d79bcSJed Brown 
420363e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
420463e21af5SBarry Smith 
42057db568b7SBarry Smith    Level: developer
4206228d79bcSJed Brown 
4207228d79bcSJed Brown @*/
42080910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4209d763cef2SBarry Smith {
4210d6152f81SLisandro Dalcin   DM             dm;
4211a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4212d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4213d763cef2SBarry Smith 
4214d763cef2SBarry Smith   PetscFunctionBegin;
4215b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4216b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4217d6152f81SLisandro Dalcin 
4218d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4219d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4220d6152f81SLisandro Dalcin 
42218860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4222d763cef2SBarry Smith   for (i=0; i<n; i++) {
42230910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4224d763cef2SBarry Smith   }
42258860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4226d763cef2SBarry Smith   PetscFunctionReturn(0);
4227d763cef2SBarry Smith }
4228d763cef2SBarry Smith 
4229d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4230d763cef2SBarry Smith /*@C
42317db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4232a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4233d763cef2SBarry Smith 
4234d763cef2SBarry Smith    Collective on TS
4235d763cef2SBarry Smith 
4236d763cef2SBarry Smith    Input Parameters:
4237d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4238d763cef2SBarry Smith .  label - the title to put in the title bar
42397c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4240a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4241a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4242d763cef2SBarry Smith 
4243d763cef2SBarry Smith    Output Parameter:
42440b039ecaSBarry Smith .  ctx - the context
4245d763cef2SBarry Smith 
4246d763cef2SBarry Smith    Options Database Key:
42474f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42488b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
42497db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42507db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42517db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42527db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4253b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4254d763cef2SBarry Smith 
4255d763cef2SBarry Smith    Notes:
4256a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4257d763cef2SBarry Smith 
42587db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42597db568b7SBarry Smith 
42607db568b7SBarry Smith    Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the
42617db568b7SBarry Smith    first argument (if that TS object does not have a TSMonitorLGCtx associated with it the function call is ignored) and the second takes a TSMonitorLGCtx object
42627db568b7SBarry Smith    as the first argument.
42637db568b7SBarry Smith 
42647db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42657db568b7SBarry Smith 
4266d763cef2SBarry Smith    Level: intermediate
4267d763cef2SBarry Smith 
42687db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
42697db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
42707db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
42717db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
42727db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
42737c922b88SBarry Smith 
4274d763cef2SBarry Smith @*/
4275a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4276d763cef2SBarry Smith {
42777f52daa2SLisandro Dalcin   PetscDraw      draw;
4278dfbe8321SBarry Smith   PetscErrorCode ierr;
4279d763cef2SBarry Smith 
4280d763cef2SBarry Smith   PetscFunctionBegin;
4281b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
42827f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
42837f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
42847f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4285287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
42867f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4287a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4288d763cef2SBarry Smith   PetscFunctionReturn(0);
4289d763cef2SBarry Smith }
4290d763cef2SBarry Smith 
4291b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4292d763cef2SBarry Smith {
42930b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4294c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4295dfbe8321SBarry Smith   PetscErrorCode ierr;
4296d763cef2SBarry Smith 
4297d763cef2SBarry Smith   PetscFunctionBegin;
429863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4299b06615a5SLisandro Dalcin   if (!step) {
4300a9f9c1f6SBarry Smith     PetscDrawAxis axis;
43018b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4302a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43038b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4304a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4305a9f9c1f6SBarry Smith   }
4306c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43078b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
43080b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4309b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43100b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43116934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
43123923b477SBarry Smith   }
4313d763cef2SBarry Smith   PetscFunctionReturn(0);
4314d763cef2SBarry Smith }
4315d763cef2SBarry Smith 
4316d763cef2SBarry Smith /*@C
4317a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4318a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4319d763cef2SBarry Smith 
43200b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4321d763cef2SBarry Smith 
4322d763cef2SBarry Smith    Input Parameter:
43230b039ecaSBarry Smith .  ctx - the monitor context
4324d763cef2SBarry Smith 
4325d763cef2SBarry Smith    Level: intermediate
4326d763cef2SBarry Smith 
43274f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4328d763cef2SBarry Smith @*/
4329a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4330d763cef2SBarry Smith {
4331dfbe8321SBarry Smith   PetscErrorCode ierr;
4332d763cef2SBarry Smith 
4333d763cef2SBarry Smith   PetscFunctionBegin;
43347684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43357684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43367684fa3eSBarry Smith   }
43370b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
433831152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4339387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4340387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4341387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43420b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4343d763cef2SBarry Smith   PetscFunctionReturn(0);
4344d763cef2SBarry Smith }
4345d763cef2SBarry Smith 
43461b575b74SJoseph Pusztay /*
43471b575b74SJoseph Pusztay 
43481b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
43491b575b74SJoseph Pusztay 
43501b575b74SJoseph Pusztay */
43511b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43521b575b74SJoseph Pusztay {
43531b575b74SJoseph Pusztay   PetscDraw      draw;
43541b575b74SJoseph Pusztay   PetscErrorCode ierr;
43551b575b74SJoseph Pusztay 
43561b575b74SJoseph Pusztay   PetscFunctionBegin;
43571b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43581b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43591b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43601b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43611b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43621b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43631b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43641b575b74SJoseph Pusztay 
43651b575b74SJoseph Pusztay }
43661b575b74SJoseph Pusztay 
43671b575b74SJoseph Pusztay /*
43681b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
43691b575b74SJoseph Pusztay */
43701b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
43711b575b74SJoseph Pusztay {
43721b575b74SJoseph Pusztay   PetscErrorCode ierr;
43731b575b74SJoseph Pusztay 
43741b575b74SJoseph Pusztay   PetscFunctionBegin;
43751b575b74SJoseph Pusztay 
43761b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
43771b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
43781b575b74SJoseph Pusztay 
43791b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43801b575b74SJoseph Pusztay 
43811b575b74SJoseph Pusztay }
43821b575b74SJoseph Pusztay 
4383d763cef2SBarry Smith /*@
4384b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4385d763cef2SBarry Smith 
4386d763cef2SBarry Smith    Not Collective
4387d763cef2SBarry Smith 
4388d763cef2SBarry Smith    Input Parameter:
4389d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4390d763cef2SBarry Smith 
4391d763cef2SBarry Smith    Output Parameter:
439219eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4393d763cef2SBarry Smith 
4394d763cef2SBarry Smith    Level: beginner
4395d763cef2SBarry Smith 
4396b8123daeSJed Brown    Note:
4397b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
43989be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4399b8123daeSJed Brown 
44008f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4401d763cef2SBarry Smith 
4402d763cef2SBarry Smith @*/
44037087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4404d763cef2SBarry Smith {
4405d763cef2SBarry Smith   PetscFunctionBegin;
44060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4407f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4408d763cef2SBarry Smith   *t = ts->ptime;
4409d763cef2SBarry Smith   PetscFunctionReturn(0);
4410d763cef2SBarry Smith }
4411d763cef2SBarry Smith 
4412e5e524a1SHong Zhang /*@
4413e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4414e5e524a1SHong Zhang 
4415e5e524a1SHong Zhang    Not Collective
4416e5e524a1SHong Zhang 
4417e5e524a1SHong Zhang    Input Parameter:
4418e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4419e5e524a1SHong Zhang 
4420e5e524a1SHong Zhang    Output Parameter:
4421e5e524a1SHong Zhang .  t  - the previous time
4422e5e524a1SHong Zhang 
4423e5e524a1SHong Zhang    Level: beginner
4424e5e524a1SHong Zhang 
4425aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4426e5e524a1SHong Zhang 
4427e5e524a1SHong Zhang @*/
4428e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4429e5e524a1SHong Zhang {
4430e5e524a1SHong Zhang   PetscFunctionBegin;
4431e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4432e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4433e5e524a1SHong Zhang   *t = ts->ptime_prev;
4434e5e524a1SHong Zhang   PetscFunctionReturn(0);
4435e5e524a1SHong Zhang }
4436e5e524a1SHong Zhang 
44376a4d4014SLisandro Dalcin /*@
44386a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44396a4d4014SLisandro Dalcin 
44403f9fe445SBarry Smith    Logically Collective on TS
44416a4d4014SLisandro Dalcin 
44426a4d4014SLisandro Dalcin    Input Parameters:
44436a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44446a4d4014SLisandro Dalcin -  time - the time
44456a4d4014SLisandro Dalcin 
44466a4d4014SLisandro Dalcin    Level: intermediate
44476a4d4014SLisandro Dalcin 
444819eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
44496a4d4014SLisandro Dalcin 
44506a4d4014SLisandro Dalcin @*/
44517087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44526a4d4014SLisandro Dalcin {
44536a4d4014SLisandro Dalcin   PetscFunctionBegin;
44540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4455c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44566a4d4014SLisandro Dalcin   ts->ptime = t;
44576a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44586a4d4014SLisandro Dalcin }
44596a4d4014SLisandro Dalcin 
4460d763cef2SBarry Smith /*@C
4461d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4462d763cef2SBarry Smith    TS options in the database.
4463d763cef2SBarry Smith 
44643f9fe445SBarry Smith    Logically Collective on TS
4465d763cef2SBarry Smith 
4466d763cef2SBarry Smith    Input Parameter:
4467d763cef2SBarry Smith +  ts     - The TS context
4468d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4469d763cef2SBarry Smith 
4470d763cef2SBarry Smith    Notes:
4471d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4472d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4473d763cef2SBarry Smith    hyphen.
4474d763cef2SBarry Smith 
4475d763cef2SBarry Smith    Level: advanced
4476d763cef2SBarry Smith 
4477d763cef2SBarry Smith .seealso: TSSetFromOptions()
4478d763cef2SBarry Smith 
4479d763cef2SBarry Smith @*/
44807087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4481d763cef2SBarry Smith {
4482dfbe8321SBarry Smith   PetscErrorCode ierr;
4483089b2837SJed Brown   SNES           snes;
4484d763cef2SBarry Smith 
4485d763cef2SBarry Smith   PetscFunctionBegin;
44860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4487d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4488089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4489089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4490d763cef2SBarry Smith   PetscFunctionReturn(0);
4491d763cef2SBarry Smith }
4492d763cef2SBarry Smith 
4493d763cef2SBarry Smith /*@C
4494d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4495d763cef2SBarry Smith    TS options in the database.
4496d763cef2SBarry Smith 
44973f9fe445SBarry Smith    Logically Collective on TS
4498d763cef2SBarry Smith 
4499d763cef2SBarry Smith    Input Parameter:
4500d763cef2SBarry Smith +  ts     - The TS context
4501d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4502d763cef2SBarry Smith 
4503d763cef2SBarry Smith    Notes:
4504d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4505d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4506d763cef2SBarry Smith    hyphen.
4507d763cef2SBarry Smith 
4508d763cef2SBarry Smith    Level: advanced
4509d763cef2SBarry Smith 
4510d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4511d763cef2SBarry Smith 
4512d763cef2SBarry Smith @*/
45137087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4514d763cef2SBarry Smith {
4515dfbe8321SBarry Smith   PetscErrorCode ierr;
4516089b2837SJed Brown   SNES           snes;
4517d763cef2SBarry Smith 
4518d763cef2SBarry Smith   PetscFunctionBegin;
45190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4520d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4521089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4522089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4523d763cef2SBarry Smith   PetscFunctionReturn(0);
4524d763cef2SBarry Smith }
4525d763cef2SBarry Smith 
4526d763cef2SBarry Smith /*@C
4527d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4528d763cef2SBarry Smith    TS options in the database.
4529d763cef2SBarry Smith 
4530d763cef2SBarry Smith    Not Collective
4531d763cef2SBarry Smith 
4532d763cef2SBarry Smith    Input Parameter:
4533d763cef2SBarry Smith .  ts - The TS context
4534d763cef2SBarry Smith 
4535d763cef2SBarry Smith    Output Parameter:
4536d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4537d763cef2SBarry Smith 
453895452b02SPatrick Sanan    Notes:
453995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4540d763cef2SBarry Smith    sufficient length to hold the prefix.
4541d763cef2SBarry Smith 
4542d763cef2SBarry Smith    Level: intermediate
4543d763cef2SBarry Smith 
4544d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4545d763cef2SBarry Smith @*/
45467087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4547d763cef2SBarry Smith {
4548dfbe8321SBarry Smith   PetscErrorCode ierr;
4549d763cef2SBarry Smith 
4550d763cef2SBarry Smith   PetscFunctionBegin;
45510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45524482741eSBarry Smith   PetscValidPointer(prefix,2);
4553d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4554d763cef2SBarry Smith   PetscFunctionReturn(0);
4555d763cef2SBarry Smith }
4556d763cef2SBarry Smith 
4557d763cef2SBarry Smith /*@C
4558d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4559d763cef2SBarry Smith 
4560d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4561d763cef2SBarry Smith 
4562d763cef2SBarry Smith    Input Parameter:
4563d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4564d763cef2SBarry Smith 
4565d763cef2SBarry Smith    Output Parameters:
4566e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4567e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4568e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4569e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4570d763cef2SBarry Smith 
457195452b02SPatrick Sanan    Notes:
457295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4573d763cef2SBarry Smith 
4574d763cef2SBarry Smith    Level: intermediate
4575d763cef2SBarry Smith 
457680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4577d763cef2SBarry Smith 
4578d763cef2SBarry Smith @*/
4579e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4580d763cef2SBarry Smith {
4581089b2837SJed Brown   PetscErrorCode ierr;
458224989b8cSPeter Brune   DM             dm;
4583089b2837SJed Brown 
4584d763cef2SBarry Smith   PetscFunctionBegin;
458523a57915SBarry Smith   if (Amat || Pmat) {
458623a57915SBarry Smith     SNES snes;
4587089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
458823a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4589e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
459023a57915SBarry Smith   }
459124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
459224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4593d763cef2SBarry Smith   PetscFunctionReturn(0);
4594d763cef2SBarry Smith }
4595d763cef2SBarry Smith 
45962eca1d9cSJed Brown /*@C
45972eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
45982eca1d9cSJed Brown 
45992eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46002eca1d9cSJed Brown 
46012eca1d9cSJed Brown    Input Parameter:
46022eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46032eca1d9cSJed Brown 
46042eca1d9cSJed Brown    Output Parameters:
4605e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4606e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46072eca1d9cSJed Brown .  f   - The function to compute the matrices
46082eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46092eca1d9cSJed Brown 
461095452b02SPatrick Sanan    Notes:
461195452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
46122eca1d9cSJed Brown 
46132eca1d9cSJed Brown    Level: advanced
46142eca1d9cSJed Brown 
461580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
46162eca1d9cSJed Brown 
46172eca1d9cSJed Brown @*/
4618e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46192eca1d9cSJed Brown {
4620089b2837SJed Brown   PetscErrorCode ierr;
462124989b8cSPeter Brune   DM             dm;
46220910c330SBarry Smith 
46232eca1d9cSJed Brown   PetscFunctionBegin;
4624c0aab802Sstefano_zampini   if (Amat || Pmat) {
4625c0aab802Sstefano_zampini     SNES snes;
4626089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4627f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4628e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4629c0aab802Sstefano_zampini   }
463024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
463124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46322eca1d9cSJed Brown   PetscFunctionReturn(0);
46332eca1d9cSJed Brown }
46342eca1d9cSJed Brown 
46351713a123SBarry Smith /*@C
463683a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46371713a123SBarry Smith    VecView() for the solution at each timestep
46381713a123SBarry Smith 
46391713a123SBarry Smith    Collective on TS
46401713a123SBarry Smith 
46411713a123SBarry Smith    Input Parameters:
46421713a123SBarry Smith +  ts - the TS context
46431713a123SBarry Smith .  step - current time-step
4644142b95e3SSatish Balay .  ptime - current time
46450298fd71SBarry Smith -  dummy - either a viewer or NULL
46461713a123SBarry Smith 
464799fdda47SBarry Smith    Options Database:
464899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
464999fdda47SBarry Smith 
465095452b02SPatrick Sanan    Notes:
465195452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
465299fdda47SBarry Smith        will look bad
465399fdda47SBarry Smith 
46541713a123SBarry Smith    Level: intermediate
46551713a123SBarry Smith 
4656a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46571713a123SBarry Smith @*/
46580910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46591713a123SBarry Smith {
4660dfbe8321SBarry Smith   PetscErrorCode   ierr;
466183a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4662473a3ab2SBarry Smith   PetscDraw        draw;
46631713a123SBarry Smith 
46641713a123SBarry Smith   PetscFunctionBegin;
46656083293cSBarry Smith   if (!step && ictx->showinitial) {
46666083293cSBarry Smith     if (!ictx->initialsolution) {
46670910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46681713a123SBarry Smith     }
46690910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
46706083293cSBarry Smith   }
4671b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46720dcf80beSBarry Smith 
46736083293cSBarry Smith   if (ictx->showinitial) {
46746083293cSBarry Smith     PetscReal pause;
46756083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
46766083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
46776083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
46786083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
46796083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
46806083293cSBarry Smith   }
46810910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4682473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
468351fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4684473a3ab2SBarry Smith     char      time[32];
4685473a3ab2SBarry Smith 
4686473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
468785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4688473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4689473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
469051fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4691473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4692473a3ab2SBarry Smith   }
4693473a3ab2SBarry Smith 
46946083293cSBarry Smith   if (ictx->showinitial) {
46956083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
46966083293cSBarry Smith   }
46971713a123SBarry Smith   PetscFunctionReturn(0);
46981713a123SBarry Smith }
46991713a123SBarry Smith 
47009110b2e7SHong Zhang /*@C
47012d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
47022d5ee99bSBarry Smith 
47032d5ee99bSBarry Smith    Collective on TS
47042d5ee99bSBarry Smith 
47052d5ee99bSBarry Smith    Input Parameters:
47062d5ee99bSBarry Smith +  ts - the TS context
47072d5ee99bSBarry Smith .  step - current time-step
47082d5ee99bSBarry Smith .  ptime - current time
47092d5ee99bSBarry Smith -  dummy - either a viewer or NULL
47102d5ee99bSBarry Smith 
47112d5ee99bSBarry Smith    Level: intermediate
47122d5ee99bSBarry Smith 
47132d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47142d5ee99bSBarry Smith @*/
47152d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47162d5ee99bSBarry Smith {
47172d5ee99bSBarry Smith   PetscErrorCode    ierr;
47182d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47192d5ee99bSBarry Smith   PetscDraw         draw;
47206934998bSLisandro Dalcin   PetscDrawAxis     axis;
47212d5ee99bSBarry Smith   PetscInt          n;
47222d5ee99bSBarry Smith   PetscMPIInt       size;
47236934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47242d5ee99bSBarry Smith   char              time[32];
47252d5ee99bSBarry Smith   const PetscScalar *U;
47262d5ee99bSBarry Smith 
47272d5ee99bSBarry Smith   PetscFunctionBegin;
47286934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
47296934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47302d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47316934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47322d5ee99bSBarry Smith 
47332d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47346934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47356934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47366934998bSLisandro Dalcin   if (!step) {
47376934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47386934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47396934998bSLisandro Dalcin   }
47402d5ee99bSBarry Smith 
47412d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47426934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47436934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4744a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47456934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47462d5ee99bSBarry Smith 
47476934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47486934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47492d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47504b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
475185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47522d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
475351fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47542d5ee99bSBarry Smith   }
47556934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47562d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
475756d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47586934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47592d5ee99bSBarry Smith   PetscFunctionReturn(0);
47602d5ee99bSBarry Smith }
47612d5ee99bSBarry Smith 
47626083293cSBarry Smith /*@C
476383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
47646083293cSBarry Smith 
47656083293cSBarry Smith    Collective on TS
47666083293cSBarry Smith 
47676083293cSBarry Smith    Input Parameters:
47686083293cSBarry Smith .    ctx - the monitor context
47696083293cSBarry Smith 
47706083293cSBarry Smith    Level: intermediate
47716083293cSBarry Smith 
477283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
47736083293cSBarry Smith @*/
477483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
47756083293cSBarry Smith {
47766083293cSBarry Smith   PetscErrorCode ierr;
47776083293cSBarry Smith 
47786083293cSBarry Smith   PetscFunctionBegin;
477983a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
478083a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
478183a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
47826083293cSBarry Smith   PetscFunctionReturn(0);
47836083293cSBarry Smith }
47846083293cSBarry Smith 
47856083293cSBarry Smith /*@C
478683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
47876083293cSBarry Smith 
47886083293cSBarry Smith    Collective on TS
47896083293cSBarry Smith 
47906083293cSBarry Smith    Input Parameter:
47916083293cSBarry Smith .    ts - time-step context
47926083293cSBarry Smith 
47936083293cSBarry Smith    Output Patameter:
47946083293cSBarry Smith .    ctx - the monitor context
47956083293cSBarry Smith 
479699fdda47SBarry Smith    Options Database:
479799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
479899fdda47SBarry Smith 
47996083293cSBarry Smith    Level: intermediate
48006083293cSBarry Smith 
480183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
48026083293cSBarry Smith @*/
480383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
48046083293cSBarry Smith {
48056083293cSBarry Smith   PetscErrorCode   ierr;
48066083293cSBarry Smith 
48076083293cSBarry Smith   PetscFunctionBegin;
4808b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
480983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4810e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4811bbd56ea5SKarl Rupp 
481283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4813473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4814c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4815473a3ab2SBarry Smith 
4816473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4817c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
48186083293cSBarry Smith   PetscFunctionReturn(0);
48196083293cSBarry Smith }
48206083293cSBarry Smith 
48213a471f94SBarry Smith /*@C
48220ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
48230ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
48240ed3bfb6SBarry Smith 
48250ed3bfb6SBarry Smith    Collective on TS
48260ed3bfb6SBarry Smith 
48270ed3bfb6SBarry Smith    Input Parameters:
48280ed3bfb6SBarry Smith +  ts - the TS context
48290ed3bfb6SBarry Smith .  step - current time-step
48300ed3bfb6SBarry Smith .  ptime - current time
48310ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
48320ed3bfb6SBarry Smith 
48330ed3bfb6SBarry Smith    Options Database:
48340ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48350ed3bfb6SBarry Smith 
48360ed3bfb6SBarry Smith    Level: intermediate
48370ed3bfb6SBarry Smith 
48380ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48390ed3bfb6SBarry Smith @*/
48400ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48410ed3bfb6SBarry Smith {
48420ed3bfb6SBarry Smith   PetscErrorCode   ierr;
48430ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
48440ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
48450ed3bfb6SBarry Smith   Vec              work;
48460ed3bfb6SBarry Smith 
48470ed3bfb6SBarry Smith   PetscFunctionBegin;
48480ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48490ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48500ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48510ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48520ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48530ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48540ed3bfb6SBarry Smith }
48550ed3bfb6SBarry Smith 
48560ed3bfb6SBarry Smith /*@C
485783a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48583a471f94SBarry Smith    VecView() for the error at each timestep
48593a471f94SBarry Smith 
48603a471f94SBarry Smith    Collective on TS
48613a471f94SBarry Smith 
48623a471f94SBarry Smith    Input Parameters:
48633a471f94SBarry Smith +  ts - the TS context
48643a471f94SBarry Smith .  step - current time-step
48653a471f94SBarry Smith .  ptime - current time
48660298fd71SBarry Smith -  dummy - either a viewer or NULL
48673a471f94SBarry Smith 
48680ed3bfb6SBarry Smith    Options Database:
48690ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48700ed3bfb6SBarry Smith 
48713a471f94SBarry Smith    Level: intermediate
48723a471f94SBarry Smith 
48730ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48743a471f94SBarry Smith @*/
48750910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48763a471f94SBarry Smith {
48773a471f94SBarry Smith   PetscErrorCode   ierr;
487883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
487983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
48803a471f94SBarry Smith   Vec              work;
48813a471f94SBarry Smith 
48823a471f94SBarry Smith   PetscFunctionBegin;
4883b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48840910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48853a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48860910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
48873a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48883a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48893a471f94SBarry Smith   PetscFunctionReturn(0);
48903a471f94SBarry Smith }
48913a471f94SBarry Smith 
4892af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
48936c699258SBarry Smith /*@
48942a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
48956c699258SBarry Smith 
4896d083f849SBarry Smith    Logically Collective on ts
48976c699258SBarry Smith 
48986c699258SBarry Smith    Input Parameters:
48992a808120SBarry Smith +  ts - the ODE integrator object
49002a808120SBarry Smith -  dm - the dm, cannot be NULL
49016c699258SBarry Smith 
4902e03a659cSJed Brown    Notes:
4903e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4904e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4905e03a659cSJed Brown    different problems using the same function space.
4906e03a659cSJed Brown 
49076c699258SBarry Smith    Level: intermediate
49086c699258SBarry Smith 
49096c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49106c699258SBarry Smith @*/
49117087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49126c699258SBarry Smith {
49136c699258SBarry Smith   PetscErrorCode ierr;
4914089b2837SJed Brown   SNES           snes;
4915942e3340SBarry Smith   DMTS           tsdm;
49166c699258SBarry Smith 
49176c699258SBarry Smith   PetscFunctionBegin;
49180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49192a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
492070663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4921942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49222a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4923942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4924942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
492524989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
492624989b8cSPeter Brune         tsdm->originaldm = dm;
492724989b8cSPeter Brune       }
492824989b8cSPeter Brune     }
49296bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
493024989b8cSPeter Brune   }
49316c699258SBarry Smith   ts->dm = dm;
4932bbd56ea5SKarl Rupp 
4933089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4934089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49356c699258SBarry Smith   PetscFunctionReturn(0);
49366c699258SBarry Smith }
49376c699258SBarry Smith 
49386c699258SBarry Smith /*@
49396c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49406c699258SBarry Smith 
49413f9fe445SBarry Smith    Not Collective
49426c699258SBarry Smith 
49436c699258SBarry Smith    Input Parameter:
49446c699258SBarry Smith . ts - the preconditioner context
49456c699258SBarry Smith 
49466c699258SBarry Smith    Output Parameter:
49476c699258SBarry Smith .  dm - the dm
49486c699258SBarry Smith 
49496c699258SBarry Smith    Level: intermediate
49506c699258SBarry Smith 
49516c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49526c699258SBarry Smith @*/
49537087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49546c699258SBarry Smith {
4955496e6a7aSJed Brown   PetscErrorCode ierr;
4956496e6a7aSJed Brown 
49576c699258SBarry Smith   PetscFunctionBegin;
49580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4959496e6a7aSJed Brown   if (!ts->dm) {
4960ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4961496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4962496e6a7aSJed Brown   }
49636c699258SBarry Smith   *dm = ts->dm;
49646c699258SBarry Smith   PetscFunctionReturn(0);
49656c699258SBarry Smith }
49661713a123SBarry Smith 
49670f5c6efeSJed Brown /*@
49680f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49690f5c6efeSJed Brown 
49703f9fe445SBarry Smith    Logically Collective on SNES
49710f5c6efeSJed Brown 
49720f5c6efeSJed Brown    Input Parameter:
4973d42a1c89SJed Brown + snes - nonlinear solver
49740910c330SBarry Smith . U - the current state at which to evaluate the residual
4975d42a1c89SJed Brown - ctx - user context, must be a TS
49760f5c6efeSJed Brown 
49770f5c6efeSJed Brown    Output Parameter:
49780f5c6efeSJed Brown . F - the nonlinear residual
49790f5c6efeSJed Brown 
49800f5c6efeSJed Brown    Notes:
49810f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49820f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
49830f5c6efeSJed Brown 
49840f5c6efeSJed Brown    Level: advanced
49850f5c6efeSJed Brown 
49860f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
49870f5c6efeSJed Brown @*/
49880910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
49890f5c6efeSJed Brown {
49900f5c6efeSJed Brown   TS             ts = (TS)ctx;
49910f5c6efeSJed Brown   PetscErrorCode ierr;
49920f5c6efeSJed Brown 
49930f5c6efeSJed Brown   PetscFunctionBegin;
49940f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
49950910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
49960f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
49970f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
49980910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
49990f5c6efeSJed Brown   PetscFunctionReturn(0);
50000f5c6efeSJed Brown }
50010f5c6efeSJed Brown 
50020f5c6efeSJed Brown /*@
50030f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50040f5c6efeSJed Brown 
50050f5c6efeSJed Brown    Collective on SNES
50060f5c6efeSJed Brown 
50070f5c6efeSJed Brown    Input Parameter:
50080f5c6efeSJed Brown + snes - nonlinear solver
50090910c330SBarry Smith . U - the current state at which to evaluate the residual
50100f5c6efeSJed Brown - ctx - user context, must be a TS
50110f5c6efeSJed Brown 
50120f5c6efeSJed Brown    Output Parameter:
50130f5c6efeSJed Brown + A - the Jacobian
50140f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50150f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50160f5c6efeSJed Brown 
50170f5c6efeSJed Brown    Notes:
50180f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50190f5c6efeSJed Brown 
50200f5c6efeSJed Brown    Level: developer
50210f5c6efeSJed Brown 
50220f5c6efeSJed Brown .seealso: SNESSetJacobian()
50230f5c6efeSJed Brown @*/
5024d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50250f5c6efeSJed Brown {
50260f5c6efeSJed Brown   TS             ts = (TS)ctx;
50270f5c6efeSJed Brown   PetscErrorCode ierr;
50280f5c6efeSJed Brown 
50290f5c6efeSJed Brown   PetscFunctionBegin;
50300f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50310910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50320f5c6efeSJed Brown   PetscValidPointer(A,3);
503394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50340f5c6efeSJed Brown   PetscValidPointer(B,4);
503594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50360f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5037d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50380f5c6efeSJed Brown   PetscFunctionReturn(0);
50390f5c6efeSJed Brown }
5040325fc9f4SBarry Smith 
50410e4ef248SJed Brown /*@C
50429ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50430e4ef248SJed Brown 
50440e4ef248SJed Brown    Collective on TS
50450e4ef248SJed Brown 
50460e4ef248SJed Brown    Input Arguments:
50470e4ef248SJed Brown +  ts - time stepping context
50480e4ef248SJed Brown .  t - time at which to evaluate
50490910c330SBarry Smith .  U - state at which to evaluate
50500e4ef248SJed Brown -  ctx - context
50510e4ef248SJed Brown 
50520e4ef248SJed Brown    Output Arguments:
50530e4ef248SJed Brown .  F - right hand side
50540e4ef248SJed Brown 
50550e4ef248SJed Brown    Level: intermediate
50560e4ef248SJed Brown 
50570e4ef248SJed Brown    Notes:
50580e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50590e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50600e4ef248SJed Brown 
50610e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50620e4ef248SJed Brown @*/
50630910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50640e4ef248SJed Brown {
50650e4ef248SJed Brown   PetscErrorCode ierr;
50660e4ef248SJed Brown   Mat            Arhs,Brhs;
50670e4ef248SJed Brown 
50680e4ef248SJed Brown   PetscFunctionBegin;
50690e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
50702663174eSHong Zhang   /* undo the damage caused by shifting */
5071cfa8a9a2SHong Zhang   ierr = TSRecoverRHSJacobian(ts,Arhs,Brhs);CHKERRQ(ierr);
5072d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
50730910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
50740e4ef248SJed Brown   PetscFunctionReturn(0);
50750e4ef248SJed Brown }
50760e4ef248SJed Brown 
50770e4ef248SJed Brown /*@C
50780e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
50790e4ef248SJed Brown 
50800e4ef248SJed Brown    Collective on TS
50810e4ef248SJed Brown 
50820e4ef248SJed Brown    Input Arguments:
50830e4ef248SJed Brown +  ts - time stepping context
50840e4ef248SJed Brown .  t - time at which to evaluate
50850910c330SBarry Smith .  U - state at which to evaluate
50860e4ef248SJed Brown -  ctx - context
50870e4ef248SJed Brown 
50880e4ef248SJed Brown    Output Arguments:
50890e4ef248SJed Brown +  A - pointer to operator
50900e4ef248SJed Brown .  B - pointer to preconditioning matrix
50910e4ef248SJed Brown -  flg - matrix structure flag
50920e4ef248SJed Brown 
50930e4ef248SJed Brown    Level: intermediate
50940e4ef248SJed Brown 
50950e4ef248SJed Brown    Notes:
50960e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
50970e4ef248SJed Brown 
50980e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
50990e4ef248SJed Brown @*/
5100d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51010e4ef248SJed Brown {
51020e4ef248SJed Brown   PetscFunctionBegin;
51030e4ef248SJed Brown   PetscFunctionReturn(0);
51040e4ef248SJed Brown }
51050e4ef248SJed Brown 
51060026cea9SSean Farley /*@C
51070026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51080026cea9SSean Farley 
51090026cea9SSean Farley    Collective on TS
51100026cea9SSean Farley 
51110026cea9SSean Farley    Input Arguments:
51120026cea9SSean Farley +  ts - time stepping context
51130026cea9SSean Farley .  t - time at which to evaluate
51140910c330SBarry Smith .  U - state at which to evaluate
51150910c330SBarry Smith .  Udot - time derivative of state vector
51160026cea9SSean Farley -  ctx - context
51170026cea9SSean Farley 
51180026cea9SSean Farley    Output Arguments:
51190026cea9SSean Farley .  F - left hand side
51200026cea9SSean Farley 
51210026cea9SSean Farley    Level: intermediate
51220026cea9SSean Farley 
51230026cea9SSean Farley    Notes:
51240910c330SBarry 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
51250026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51260026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51270026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51280026cea9SSean Farley 
51299ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51309ae8fd06SBarry Smith 
51319ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51320026cea9SSean Farley @*/
51330910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51340026cea9SSean Farley {
51350026cea9SSean Farley   PetscErrorCode ierr;
51360026cea9SSean Farley   Mat            A,B;
51370026cea9SSean Farley 
51380026cea9SSean Farley   PetscFunctionBegin;
51390298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5140d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51410910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51420026cea9SSean Farley   PetscFunctionReturn(0);
51430026cea9SSean Farley }
51440026cea9SSean Farley 
51450026cea9SSean Farley /*@C
5146b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51470026cea9SSean Farley 
51480026cea9SSean Farley    Collective on TS
51490026cea9SSean Farley 
51500026cea9SSean Farley    Input Arguments:
51510026cea9SSean Farley +  ts - time stepping context
51520026cea9SSean Farley .  t - time at which to evaluate
51530910c330SBarry Smith .  U - state at which to evaluate
51540910c330SBarry Smith .  Udot - time derivative of state vector
51550026cea9SSean Farley .  shift - shift to apply
51560026cea9SSean Farley -  ctx - context
51570026cea9SSean Farley 
51580026cea9SSean Farley    Output Arguments:
51590026cea9SSean Farley +  A - pointer to operator
51600026cea9SSean Farley .  B - pointer to preconditioning matrix
51610026cea9SSean Farley -  flg - matrix structure flag
51620026cea9SSean Farley 
5163b41af12eSJed Brown    Level: advanced
51640026cea9SSean Farley 
51650026cea9SSean Farley    Notes:
51660026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51670026cea9SSean Farley 
5168b41af12eSJed Brown    It is only appropriate for problems of the form
5169b41af12eSJed Brown 
5170b41af12eSJed Brown $     M Udot = F(U,t)
5171b41af12eSJed Brown 
5172b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5173b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5174b41af12eSJed Brown   an implicit operator of the form
5175b41af12eSJed Brown 
5176b41af12eSJed Brown $    shift*M + J
5177b41af12eSJed Brown 
5178b41af12eSJed 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
5179b41af12eSJed Brown   a copy of M or reassemble it when requested.
5180b41af12eSJed Brown 
51810026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
51820026cea9SSean Farley @*/
5183d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
51840026cea9SSean Farley {
5185b41af12eSJed Brown   PetscErrorCode ierr;
5186b41af12eSJed Brown 
51870026cea9SSean Farley   PetscFunctionBegin;
518894ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5189b41af12eSJed Brown   ts->ijacobian.shift = shift;
51900026cea9SSean Farley   PetscFunctionReturn(0);
51910026cea9SSean Farley }
5192b41af12eSJed Brown 
5193e817cc15SEmil Constantinescu /*@
5194e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5195e817cc15SEmil Constantinescu 
5196e817cc15SEmil Constantinescu    Not Collective
5197e817cc15SEmil Constantinescu 
5198e817cc15SEmil Constantinescu    Input Parameter:
5199e817cc15SEmil Constantinescu .  ts - the TS context
5200e817cc15SEmil Constantinescu 
5201e817cc15SEmil Constantinescu    Output Parameter:
52024e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5203e817cc15SEmil Constantinescu 
5204e817cc15SEmil Constantinescu    Level: beginner
5205e817cc15SEmil Constantinescu 
5206e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5207e817cc15SEmil Constantinescu @*/
5208e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5209e817cc15SEmil Constantinescu {
5210e817cc15SEmil Constantinescu   PetscFunctionBegin;
5211e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5212e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5213e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5214e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5215e817cc15SEmil Constantinescu }
5216e817cc15SEmil Constantinescu 
5217e817cc15SEmil Constantinescu /*@
5218e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5219e817cc15SEmil Constantinescu 
5220e817cc15SEmil Constantinescu    Not Collective
5221e817cc15SEmil Constantinescu 
5222e817cc15SEmil Constantinescu    Input Parameter:
5223e817cc15SEmil Constantinescu +  ts - the TS context
52241297b224SEmil Constantinescu -  equation_type - see TSEquationType
5225e817cc15SEmil Constantinescu 
5226e817cc15SEmil Constantinescu    Level: advanced
5227e817cc15SEmil Constantinescu 
5228e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5229e817cc15SEmil Constantinescu @*/
5230e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5231e817cc15SEmil Constantinescu {
5232e817cc15SEmil Constantinescu   PetscFunctionBegin;
5233e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5234e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5235e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5236e817cc15SEmil Constantinescu }
52370026cea9SSean Farley 
52384af1b03aSJed Brown /*@
52394af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52404af1b03aSJed Brown 
52414af1b03aSJed Brown    Not Collective
52424af1b03aSJed Brown 
52434af1b03aSJed Brown    Input Parameter:
52444af1b03aSJed Brown .  ts - the TS context
52454af1b03aSJed Brown 
52464af1b03aSJed Brown    Output Parameter:
52474af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52484af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52494af1b03aSJed Brown 
5250487e0bb9SJed Brown    Level: beginner
52514af1b03aSJed Brown 
5252cd652676SJed Brown    Notes:
5253cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52544af1b03aSJed Brown 
52554af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52564af1b03aSJed Brown @*/
52574af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52584af1b03aSJed Brown {
52594af1b03aSJed Brown   PetscFunctionBegin;
52604af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52614af1b03aSJed Brown   PetscValidPointer(reason,2);
52624af1b03aSJed Brown   *reason = ts->reason;
52634af1b03aSJed Brown   PetscFunctionReturn(0);
52644af1b03aSJed Brown }
52654af1b03aSJed Brown 
5266d6ad946cSShri Abhyankar /*@
5267d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5268d6ad946cSShri Abhyankar 
52696b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5270d6ad946cSShri Abhyankar 
52716b221cbeSPatrick Sanan    Input Parameters:
5272d6ad946cSShri Abhyankar +  ts - the TS context
52736b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5274d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5275d6ad946cSShri Abhyankar 
5276f5abba47SShri Abhyankar    Level: advanced
5277d6ad946cSShri Abhyankar 
5278d6ad946cSShri Abhyankar    Notes:
52796b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5280d6ad946cSShri Abhyankar 
5281d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5282d6ad946cSShri Abhyankar @*/
5283d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5284d6ad946cSShri Abhyankar {
5285d6ad946cSShri Abhyankar   PetscFunctionBegin;
5286d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5287d6ad946cSShri Abhyankar   ts->reason = reason;
5288d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5289d6ad946cSShri Abhyankar }
5290d6ad946cSShri Abhyankar 
5291cc708dedSBarry Smith /*@
5292cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5293cc708dedSBarry Smith 
5294cc708dedSBarry Smith    Not Collective
5295cc708dedSBarry Smith 
5296cc708dedSBarry Smith    Input Parameter:
5297cc708dedSBarry Smith .  ts - the TS context
5298cc708dedSBarry Smith 
5299cc708dedSBarry Smith    Output Parameter:
530019eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5301cc708dedSBarry Smith 
5302487e0bb9SJed Brown    Level: beginner
5303cc708dedSBarry Smith 
5304cc708dedSBarry Smith    Notes:
5305cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5306cc708dedSBarry Smith 
5307cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5308cc708dedSBarry Smith @*/
5309cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5310cc708dedSBarry Smith {
5311cc708dedSBarry Smith   PetscFunctionBegin;
5312cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5313cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5314cc708dedSBarry Smith   *ftime = ts->solvetime;
5315cc708dedSBarry Smith   PetscFunctionReturn(0);
5316cc708dedSBarry Smith }
5317cc708dedSBarry Smith 
53182c18e0fdSBarry Smith /*@
53195ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53209f67acb7SJed Brown    used by the time integrator.
53219f67acb7SJed Brown 
53229f67acb7SJed Brown    Not Collective
53239f67acb7SJed Brown 
53249f67acb7SJed Brown    Input Parameter:
53259f67acb7SJed Brown .  ts - TS context
53269f67acb7SJed Brown 
53279f67acb7SJed Brown    Output Parameter:
53289f67acb7SJed Brown .  nits - number of nonlinear iterations
53299f67acb7SJed Brown 
53309f67acb7SJed Brown    Notes:
53319f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53329f67acb7SJed Brown 
53339f67acb7SJed Brown    Level: intermediate
53349f67acb7SJed Brown 
53355ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53369f67acb7SJed Brown @*/
53375ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53389f67acb7SJed Brown {
53399f67acb7SJed Brown   PetscFunctionBegin;
53409f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53419f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53425ef26d82SJed Brown   *nits = ts->snes_its;
53439f67acb7SJed Brown   PetscFunctionReturn(0);
53449f67acb7SJed Brown }
53459f67acb7SJed Brown 
53469f67acb7SJed Brown /*@
53475ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53489f67acb7SJed Brown    used by the time integrator.
53499f67acb7SJed Brown 
53509f67acb7SJed Brown    Not Collective
53519f67acb7SJed Brown 
53529f67acb7SJed Brown    Input Parameter:
53539f67acb7SJed Brown .  ts - TS context
53549f67acb7SJed Brown 
53559f67acb7SJed Brown    Output Parameter:
53569f67acb7SJed Brown .  lits - number of linear iterations
53579f67acb7SJed Brown 
53589f67acb7SJed Brown    Notes:
53599f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53609f67acb7SJed Brown 
53619f67acb7SJed Brown    Level: intermediate
53629f67acb7SJed Brown 
53635ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
53649f67acb7SJed Brown @*/
53655ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
53669f67acb7SJed Brown {
53679f67acb7SJed Brown   PetscFunctionBegin;
53689f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53699f67acb7SJed Brown   PetscValidIntPointer(lits,2);
53705ef26d82SJed Brown   *lits = ts->ksp_its;
53719f67acb7SJed Brown   PetscFunctionReturn(0);
53729f67acb7SJed Brown }
53739f67acb7SJed Brown 
5374cef5090cSJed Brown /*@
5375cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5376cef5090cSJed Brown 
5377cef5090cSJed Brown    Not Collective
5378cef5090cSJed Brown 
5379cef5090cSJed Brown    Input Parameter:
5380cef5090cSJed Brown .  ts - TS context
5381cef5090cSJed Brown 
5382cef5090cSJed Brown    Output Parameter:
5383cef5090cSJed Brown .  rejects - number of steps rejected
5384cef5090cSJed Brown 
5385cef5090cSJed Brown    Notes:
5386cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5387cef5090cSJed Brown 
5388cef5090cSJed Brown    Level: intermediate
5389cef5090cSJed Brown 
53905ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5391cef5090cSJed Brown @*/
5392cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5393cef5090cSJed Brown {
5394cef5090cSJed Brown   PetscFunctionBegin;
5395cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5396cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5397cef5090cSJed Brown   *rejects = ts->reject;
5398cef5090cSJed Brown   PetscFunctionReturn(0);
5399cef5090cSJed Brown }
5400cef5090cSJed Brown 
5401cef5090cSJed Brown /*@
5402cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5403cef5090cSJed Brown 
5404cef5090cSJed Brown    Not Collective
5405cef5090cSJed Brown 
5406cef5090cSJed Brown    Input Parameter:
5407cef5090cSJed Brown .  ts - TS context
5408cef5090cSJed Brown 
5409cef5090cSJed Brown    Output Parameter:
5410cef5090cSJed Brown .  fails - number of failed nonlinear solves
5411cef5090cSJed Brown 
5412cef5090cSJed Brown    Notes:
5413cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5414cef5090cSJed Brown 
5415cef5090cSJed Brown    Level: intermediate
5416cef5090cSJed Brown 
54175ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5418cef5090cSJed Brown @*/
5419cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5420cef5090cSJed Brown {
5421cef5090cSJed Brown   PetscFunctionBegin;
5422cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5423cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5424cef5090cSJed Brown   *fails = ts->num_snes_failures;
5425cef5090cSJed Brown   PetscFunctionReturn(0);
5426cef5090cSJed Brown }
5427cef5090cSJed Brown 
5428cef5090cSJed Brown /*@
5429cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5430cef5090cSJed Brown 
5431cef5090cSJed Brown    Not Collective
5432cef5090cSJed Brown 
5433cef5090cSJed Brown    Input Parameter:
5434cef5090cSJed Brown +  ts - TS context
5435cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5436cef5090cSJed Brown 
5437cef5090cSJed Brown    Notes:
5438cef5090cSJed Brown    The counter is reset to zero for each step
5439cef5090cSJed Brown 
5440cef5090cSJed Brown    Options Database Key:
5441cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5442cef5090cSJed Brown 
5443cef5090cSJed Brown    Level: intermediate
5444cef5090cSJed Brown 
54455ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5446cef5090cSJed Brown @*/
5447cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5448cef5090cSJed Brown {
5449cef5090cSJed Brown   PetscFunctionBegin;
5450cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5451cef5090cSJed Brown   ts->max_reject = rejects;
5452cef5090cSJed Brown   PetscFunctionReturn(0);
5453cef5090cSJed Brown }
5454cef5090cSJed Brown 
5455cef5090cSJed Brown /*@
5456cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5457cef5090cSJed Brown 
5458cef5090cSJed Brown    Not Collective
5459cef5090cSJed Brown 
5460cef5090cSJed Brown    Input Parameter:
5461cef5090cSJed Brown +  ts - TS context
5462cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5463cef5090cSJed Brown 
5464cef5090cSJed Brown    Notes:
5465cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5466cef5090cSJed Brown 
5467cef5090cSJed Brown    Options Database Key:
5468cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5469cef5090cSJed Brown 
5470cef5090cSJed Brown    Level: intermediate
5471cef5090cSJed Brown 
54725ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5473cef5090cSJed Brown @*/
5474cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5475cef5090cSJed Brown {
5476cef5090cSJed Brown   PetscFunctionBegin;
5477cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5478cef5090cSJed Brown   ts->max_snes_failures = fails;
5479cef5090cSJed Brown   PetscFunctionReturn(0);
5480cef5090cSJed Brown }
5481cef5090cSJed Brown 
5482cef5090cSJed Brown /*@
5483cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5484cef5090cSJed Brown 
5485cef5090cSJed Brown    Not Collective
5486cef5090cSJed Brown 
5487cef5090cSJed Brown    Input Parameter:
5488cef5090cSJed Brown +  ts - TS context
5489cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5490cef5090cSJed Brown 
5491cef5090cSJed Brown    Options Database Key:
5492cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5493cef5090cSJed Brown 
5494cef5090cSJed Brown    Level: intermediate
5495cef5090cSJed Brown 
54965ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5497cef5090cSJed Brown @*/
5498cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5499cef5090cSJed Brown {
5500cef5090cSJed Brown   PetscFunctionBegin;
5501cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5502cef5090cSJed Brown   ts->errorifstepfailed = err;
5503cef5090cSJed Brown   PetscFunctionReturn(0);
5504cef5090cSJed Brown }
5505cef5090cSJed Brown 
5506fb1732b5SBarry Smith /*@C
5507fde5950dSBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file or a PetscDraw object
5508fb1732b5SBarry Smith 
5509fb1732b5SBarry Smith    Collective on TS
5510fb1732b5SBarry Smith 
5511fb1732b5SBarry Smith    Input Parameters:
5512fb1732b5SBarry Smith +  ts - the TS context
5513fb1732b5SBarry Smith .  step - current time-step
5514fb1732b5SBarry Smith .  ptime - current time
55150910c330SBarry Smith .  u - current state
5516721cd6eeSBarry Smith -  vf - viewer and its format
5517fb1732b5SBarry Smith 
5518fb1732b5SBarry Smith    Level: intermediate
5519fb1732b5SBarry Smith 
5520fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5521fb1732b5SBarry Smith @*/
5522721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5523fb1732b5SBarry Smith {
5524fb1732b5SBarry Smith   PetscErrorCode ierr;
5525fb1732b5SBarry Smith 
5526fb1732b5SBarry Smith   PetscFunctionBegin;
5527721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5528721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5529721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5530ed81e22dSJed Brown   PetscFunctionReturn(0);
5531ed81e22dSJed Brown }
5532ed81e22dSJed Brown 
5533ed81e22dSJed Brown /*@C
5534ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5535ed81e22dSJed Brown 
5536ed81e22dSJed Brown    Collective on TS
5537ed81e22dSJed Brown 
5538ed81e22dSJed Brown    Input Parameters:
5539ed81e22dSJed Brown +  ts - the TS context
5540ed81e22dSJed Brown .  step - current time-step
5541ed81e22dSJed Brown .  ptime - current time
55420910c330SBarry Smith .  u - current state
5543ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5544ed81e22dSJed Brown 
5545ed81e22dSJed Brown    Level: intermediate
5546ed81e22dSJed Brown 
5547ed81e22dSJed Brown    Notes:
5548ed81e22dSJed Brown    The VTK format does not allow writing multiple time steps in the same file, therefore a different file will be written for each time step.
5549ed81e22dSJed Brown    These are named according to the file name template.
5550ed81e22dSJed Brown 
5551ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5552ed81e22dSJed Brown 
5553ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5554ed81e22dSJed Brown @*/
55550910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5556ed81e22dSJed Brown {
5557ed81e22dSJed Brown   PetscErrorCode ierr;
5558ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5559ed81e22dSJed Brown   PetscViewer    viewer;
5560ed81e22dSJed Brown 
5561ed81e22dSJed Brown   PetscFunctionBegin;
556263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55638caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5564ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
55650910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5566ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5567ed81e22dSJed Brown   PetscFunctionReturn(0);
5568ed81e22dSJed Brown }
5569ed81e22dSJed Brown 
5570ed81e22dSJed Brown /*@C
5571ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5572ed81e22dSJed Brown 
5573ed81e22dSJed Brown    Collective on TS
5574ed81e22dSJed Brown 
5575ed81e22dSJed Brown    Input Parameters:
5576ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5577ed81e22dSJed Brown 
5578ed81e22dSJed Brown    Level: intermediate
5579ed81e22dSJed Brown 
5580ed81e22dSJed Brown    Note:
5581ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5582ed81e22dSJed Brown 
5583ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5584ed81e22dSJed Brown @*/
5585ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5586ed81e22dSJed Brown {
5587ed81e22dSJed Brown   PetscErrorCode ierr;
5588ed81e22dSJed Brown 
5589ed81e22dSJed Brown   PetscFunctionBegin;
5590ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5591fb1732b5SBarry Smith   PetscFunctionReturn(0);
5592fb1732b5SBarry Smith }
5593fb1732b5SBarry Smith 
559484df9cb4SJed Brown /*@
5595552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
559684df9cb4SJed Brown 
5597ed81e22dSJed Brown    Collective on TS if controller has not been created yet
559884df9cb4SJed Brown 
559984df9cb4SJed Brown    Input Arguments:
5600ed81e22dSJed Brown .  ts - time stepping context
560184df9cb4SJed Brown 
560284df9cb4SJed Brown    Output Arguments:
5603ed81e22dSJed Brown .  adapt - adaptive controller
560484df9cb4SJed Brown 
560584df9cb4SJed Brown    Level: intermediate
560684df9cb4SJed Brown 
5607ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
560884df9cb4SJed Brown @*/
5609552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
561084df9cb4SJed Brown {
561184df9cb4SJed Brown   PetscErrorCode ierr;
561284df9cb4SJed Brown 
561384df9cb4SJed Brown   PetscFunctionBegin;
561484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5615bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
561684df9cb4SJed Brown   if (!ts->adapt) {
5617ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56183bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56191c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
562084df9cb4SJed Brown   }
5621bec58848SLisandro Dalcin   *adapt = ts->adapt;
562284df9cb4SJed Brown   PetscFunctionReturn(0);
562384df9cb4SJed Brown }
5624d6ebe24aSShri Abhyankar 
56251c3436cfSJed Brown /*@
56261c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
56271c3436cfSJed Brown 
56281c3436cfSJed Brown    Logically Collective
56291c3436cfSJed Brown 
56301c3436cfSJed Brown    Input Arguments:
56311c3436cfSJed Brown +  ts - time integration context
56321c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56330298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56341c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56350298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
56361c3436cfSJed Brown 
5637a3cdaa26SBarry Smith    Options Database keys:
5638a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5639a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5640a3cdaa26SBarry Smith 
56413ff766beSShri Abhyankar    Notes:
56423ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
56433ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
56443ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
56453ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
56463ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
56473ff766beSShri Abhyankar    differential variables.
56483ff766beSShri Abhyankar 
56491c3436cfSJed Brown    Level: beginner
56501c3436cfSJed Brown 
56515c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
56521c3436cfSJed Brown @*/
56531c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56541c3436cfSJed Brown {
56551c3436cfSJed Brown   PetscErrorCode ierr;
56561c3436cfSJed Brown 
56571c3436cfSJed Brown   PetscFunctionBegin;
5658c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56591c3436cfSJed Brown   if (vatol) {
56601c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56611c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56621c3436cfSJed Brown     ts->vatol = vatol;
56631c3436cfSJed Brown   }
5664c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
56651c3436cfSJed Brown   if (vrtol) {
56661c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
56671c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
56681c3436cfSJed Brown     ts->vrtol = vrtol;
56691c3436cfSJed Brown   }
56701c3436cfSJed Brown   PetscFunctionReturn(0);
56711c3436cfSJed Brown }
56721c3436cfSJed Brown 
5673c5033834SJed Brown /*@
5674c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5675c5033834SJed Brown 
5676c5033834SJed Brown    Logically Collective
5677c5033834SJed Brown 
5678c5033834SJed Brown    Input Arguments:
5679c5033834SJed Brown .  ts - time integration context
5680c5033834SJed Brown 
5681c5033834SJed Brown    Output Arguments:
56820298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
56830298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
56840298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
56850298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5686c5033834SJed Brown 
5687c5033834SJed Brown    Level: beginner
5688c5033834SJed Brown 
56895c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5690c5033834SJed Brown @*/
5691c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5692c5033834SJed Brown {
5693c5033834SJed Brown   PetscFunctionBegin;
5694c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5695c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5696c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5697c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5698c5033834SJed Brown   PetscFunctionReturn(0);
5699c5033834SJed Brown }
5700c5033834SJed Brown 
57019c6b16b5SShri Abhyankar /*@
5702a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57039c6b16b5SShri Abhyankar 
57049c6b16b5SShri Abhyankar    Collective on TS
57059c6b16b5SShri Abhyankar 
57069c6b16b5SShri Abhyankar    Input Arguments:
57079c6b16b5SShri Abhyankar +  ts - time stepping context
5708a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5709a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57109c6b16b5SShri Abhyankar 
57119c6b16b5SShri Abhyankar    Output Arguments:
5712a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57137453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5714a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57159c6b16b5SShri Abhyankar 
57169c6b16b5SShri Abhyankar    Level: developer
57179c6b16b5SShri Abhyankar 
5718deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57199c6b16b5SShri Abhyankar @*/
57207453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57219c6b16b5SShri Abhyankar {
57229c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57239c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57247453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57257453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57269c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57277453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
57287453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
57297453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57309c6b16b5SShri Abhyankar 
57319c6b16b5SShri Abhyankar   PetscFunctionBegin;
57329c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5733a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5734a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5735a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5736a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5737a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5738a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57398a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57408a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5741a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57429c6b16b5SShri Abhyankar 
57439c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57449c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57459c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57469c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57479c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57487453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
57497453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
57507453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
57519c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57529c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57539c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57549c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57559c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
575676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57577453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57587453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57597453f775SEmil Constantinescu       if (tola>0.){
57607453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57617453f775SEmil Constantinescu         na_loc++;
57627453f775SEmil Constantinescu       }
57637453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57647453f775SEmil Constantinescu       if (tolr>0.){
57657453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57667453f775SEmil Constantinescu         nr_loc++;
57677453f775SEmil Constantinescu       }
57687453f775SEmil Constantinescu       tol=tola+tolr;
57697453f775SEmil Constantinescu       if (tol>0.){
57707453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57717453f775SEmil Constantinescu         n_loc++;
57727453f775SEmil Constantinescu       }
57739c6b16b5SShri Abhyankar     }
57749c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57759c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57769c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57779c6b16b5SShri Abhyankar     const PetscScalar *atol;
57789c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57799c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
578076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57817453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57827453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57837453f775SEmil Constantinescu       if (tola>0.){
57847453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57857453f775SEmil Constantinescu         na_loc++;
57867453f775SEmil Constantinescu       }
57877453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57887453f775SEmil Constantinescu       if (tolr>0.){
57897453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57907453f775SEmil Constantinescu         nr_loc++;
57917453f775SEmil Constantinescu       }
57927453f775SEmil Constantinescu       tol=tola+tolr;
57937453f775SEmil Constantinescu       if (tol>0.){
57947453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57957453f775SEmil Constantinescu         n_loc++;
57967453f775SEmil Constantinescu       }
57979c6b16b5SShri Abhyankar     }
57989c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57999c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58009c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58019c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58029c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
580376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58047453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58057453f775SEmil Constantinescu       tola = ts->atol;
58067453f775SEmil Constantinescu       if (tola>0.){
58077453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58087453f775SEmil Constantinescu         na_loc++;
58097453f775SEmil Constantinescu       }
58107453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58117453f775SEmil Constantinescu       if (tolr>0.){
58127453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58137453f775SEmil Constantinescu         nr_loc++;
58147453f775SEmil Constantinescu       }
58157453f775SEmil Constantinescu       tol=tola+tolr;
58167453f775SEmil Constantinescu       if (tol>0.){
58177453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58187453f775SEmil Constantinescu         n_loc++;
58197453f775SEmil Constantinescu       }
58209c6b16b5SShri Abhyankar     }
58219c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58229c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58239c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
582476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58257453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58267453f775SEmil Constantinescu       tola = ts->atol;
58277453f775SEmil Constantinescu       if (tola>0.){
58287453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58297453f775SEmil Constantinescu         na_loc++;
58307453f775SEmil Constantinescu       }
58317453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58327453f775SEmil Constantinescu       if (tolr>0.){
58337453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58347453f775SEmil Constantinescu         nr_loc++;
58357453f775SEmil Constantinescu       }
58367453f775SEmil Constantinescu       tol=tola+tolr;
58377453f775SEmil Constantinescu       if (tol>0.){
58387453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58397453f775SEmil Constantinescu         n_loc++;
58407453f775SEmil Constantinescu       }
58419c6b16b5SShri Abhyankar     }
58429c6b16b5SShri Abhyankar   }
58439c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58449c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58459c6b16b5SShri Abhyankar 
58467453f775SEmil Constantinescu   err_loc[0] = sum;
58477453f775SEmil Constantinescu   err_loc[1] = suma;
58487453f775SEmil Constantinescu   err_loc[2] = sumr;
58497453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58507453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58517453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58527453f775SEmil Constantinescu 
5853a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58547453f775SEmil Constantinescu 
58557453f775SEmil Constantinescu   gsum   = err_glb[0];
58567453f775SEmil Constantinescu   gsuma  = err_glb[1];
58577453f775SEmil Constantinescu   gsumr  = err_glb[2];
58587453f775SEmil Constantinescu   n_glb  = err_glb[3];
58597453f775SEmil Constantinescu   na_glb = err_glb[4];
58607453f775SEmil Constantinescu   nr_glb = err_glb[5];
58617453f775SEmil Constantinescu 
5862b1316ef9SEmil Constantinescu   *norm  = 0.;
5863b1316ef9SEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
5864b1316ef9SEmil Constantinescu   *norma = 0.;
5865b1316ef9SEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5866b1316ef9SEmil Constantinescu   *normr = 0.;
5867b1316ef9SEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58689c6b16b5SShri Abhyankar 
58699c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58707453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58717453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58729c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
58739c6b16b5SShri Abhyankar }
58749c6b16b5SShri Abhyankar 
58759c6b16b5SShri Abhyankar /*@
5876a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
58779c6b16b5SShri Abhyankar 
58789c6b16b5SShri Abhyankar    Collective on TS
58799c6b16b5SShri Abhyankar 
58809c6b16b5SShri Abhyankar    Input Arguments:
58819c6b16b5SShri Abhyankar +  ts - time stepping context
5882a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5883a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
58849c6b16b5SShri Abhyankar 
58859c6b16b5SShri Abhyankar    Output Arguments:
5886a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58877453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5888a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58899c6b16b5SShri Abhyankar 
58909c6b16b5SShri Abhyankar    Level: developer
58919c6b16b5SShri Abhyankar 
5892deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
58939c6b16b5SShri Abhyankar @*/
58947453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58959c6b16b5SShri Abhyankar {
58969c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
58977453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
58989c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
58997453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
59007453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
59017453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59029c6b16b5SShri Abhyankar 
59039c6b16b5SShri Abhyankar   PetscFunctionBegin;
59049c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5905a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5906a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5907a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5908a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5909a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5910a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59118a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59128a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5913a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59149c6b16b5SShri Abhyankar 
59159c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59169c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59179c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59189c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59199c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59207453f775SEmil Constantinescu 
59217453f775SEmil Constantinescu   max=0.;
59227453f775SEmil Constantinescu   maxa=0.;
59237453f775SEmil Constantinescu   maxr=0.;
59247453f775SEmil Constantinescu 
59257453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59269c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59279c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59289c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59297453f775SEmil Constantinescu 
59307453f775SEmil Constantinescu     for (i=0; i<n; i++) {
593176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59327453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59337453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59347453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59357453f775SEmil Constantinescu       tol  = tola+tolr;
59367453f775SEmil Constantinescu       if (tola>0.){
59377453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59387453f775SEmil Constantinescu       }
59397453f775SEmil Constantinescu       if (tolr>0.){
59407453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59417453f775SEmil Constantinescu       }
59427453f775SEmil Constantinescu       if (tol>0.){
59437453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59447453f775SEmil Constantinescu       }
59459c6b16b5SShri Abhyankar     }
59469c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59479c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59489c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59499c6b16b5SShri Abhyankar     const PetscScalar *atol;
59509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59517453f775SEmil Constantinescu     for (i=0; i<n; i++) {
595276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59537453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59547453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59557453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59567453f775SEmil Constantinescu       tol  = tola+tolr;
59577453f775SEmil Constantinescu       if (tola>0.){
59587453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59597453f775SEmil Constantinescu       }
59607453f775SEmil Constantinescu       if (tolr>0.){
59617453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59627453f775SEmil Constantinescu       }
59637453f775SEmil Constantinescu       if (tol>0.){
59647453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59657453f775SEmil Constantinescu       }
59669c6b16b5SShri Abhyankar     }
59679c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59689c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59699c6b16b5SShri Abhyankar     const PetscScalar *rtol;
59709c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59717453f775SEmil Constantinescu 
59727453f775SEmil Constantinescu     for (i=0; i<n; i++) {
597376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59747453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59757453f775SEmil Constantinescu       tola = ts->atol;
59767453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59777453f775SEmil Constantinescu       tol  = tola+tolr;
59787453f775SEmil Constantinescu       if (tola>0.){
59797453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59807453f775SEmil Constantinescu       }
59817453f775SEmil Constantinescu       if (tolr>0.){
59827453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59837453f775SEmil Constantinescu       }
59847453f775SEmil Constantinescu       if (tol>0.){
59857453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59867453f775SEmil Constantinescu       }
59879c6b16b5SShri Abhyankar     }
59889c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59899c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
59907453f775SEmil Constantinescu 
59917453f775SEmil Constantinescu     for (i=0; i<n; i++) {
599276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59937453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59947453f775SEmil Constantinescu       tola = ts->atol;
59957453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59967453f775SEmil Constantinescu       tol  = tola+tolr;
59977453f775SEmil Constantinescu       if (tola>0.){
59987453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59997453f775SEmil Constantinescu       }
60007453f775SEmil Constantinescu       if (tolr>0.){
60017453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60027453f775SEmil Constantinescu       }
60037453f775SEmil Constantinescu       if (tol>0.){
60047453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60057453f775SEmil Constantinescu       }
60069c6b16b5SShri Abhyankar     }
60079c6b16b5SShri Abhyankar   }
60089c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60099c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60107453f775SEmil Constantinescu   err_loc[0] = max;
60117453f775SEmil Constantinescu   err_loc[1] = maxa;
60127453f775SEmil Constantinescu   err_loc[2] = maxr;
6013a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60147453f775SEmil Constantinescu   gmax   = err_glb[0];
60157453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60167453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60179c6b16b5SShri Abhyankar 
60189c6b16b5SShri Abhyankar   *norm = gmax;
60197453f775SEmil Constantinescu   *norma = gmaxa;
60207453f775SEmil Constantinescu   *normr = gmaxr;
60219c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60227453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60237453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60249c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60259c6b16b5SShri Abhyankar }
60269c6b16b5SShri Abhyankar 
60271c3436cfSJed Brown /*@
60288a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60291c3436cfSJed Brown 
60301c3436cfSJed Brown    Collective on TS
60311c3436cfSJed Brown 
60321c3436cfSJed Brown    Input Arguments:
60331c3436cfSJed Brown +  ts - time stepping context
6034a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6035a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6036a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60377619abb3SShri 
60381c3436cfSJed Brown    Output Arguments:
6039a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60408a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6041a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6042a4868fbcSLisandro Dalcin 
6043a4868fbcSLisandro Dalcin    Options Database Keys:
6044a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6045a4868fbcSLisandro Dalcin 
60461c3436cfSJed Brown    Level: developer
60471c3436cfSJed Brown 
60488a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
60491c3436cfSJed Brown @*/
60507453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60511c3436cfSJed Brown {
60528beabaa1SBarry Smith   PetscErrorCode ierr;
60531c3436cfSJed Brown 
60541c3436cfSJed Brown   PetscFunctionBegin;
6055a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60567453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6057a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
60587453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6059a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60601c3436cfSJed Brown   PetscFunctionReturn(0);
60611c3436cfSJed Brown }
60621c3436cfSJed Brown 
60638a175baeSEmil Constantinescu 
60648a175baeSEmil Constantinescu /*@
60658a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
60668a175baeSEmil Constantinescu 
60678a175baeSEmil Constantinescu    Collective on TS
60688a175baeSEmil Constantinescu 
60698a175baeSEmil Constantinescu    Input Arguments:
60708a175baeSEmil Constantinescu +  ts - time stepping context
60718a175baeSEmil Constantinescu .  E - error vector
60728a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60738a175baeSEmil Constantinescu -  Y - state vector, previous time step
60748a175baeSEmil Constantinescu 
60758a175baeSEmil Constantinescu    Output Arguments:
6076a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60778a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6078a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60798a175baeSEmil Constantinescu 
60808a175baeSEmil Constantinescu    Level: developer
60818a175baeSEmil Constantinescu 
60828a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
60838a175baeSEmil Constantinescu @*/
60848a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60858a175baeSEmil Constantinescu {
60868a175baeSEmil Constantinescu   PetscErrorCode    ierr;
60878a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
60888a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
60898a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
60908a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
60918a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
60928a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
60938a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
60948a175baeSEmil Constantinescu 
60958a175baeSEmil Constantinescu   PetscFunctionBegin;
60968a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60978a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
60988a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
60998a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61008a175baeSEmil Constantinescu   PetscValidType(E,2);
61018a175baeSEmil Constantinescu   PetscValidType(U,3);
61028a175baeSEmil Constantinescu   PetscValidType(Y,4);
61038a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61048a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61058a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61068a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61078a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61088a175baeSEmil Constantinescu 
61098a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61108a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61118a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61128a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61138a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61148a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61158a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61168a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61178a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61188a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61198a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61208a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61218a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61228a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
612376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61248a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61258a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61268a175baeSEmil Constantinescu       if (tola>0.){
61278a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61288a175baeSEmil Constantinescu         na_loc++;
61298a175baeSEmil Constantinescu       }
61308a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61318a175baeSEmil Constantinescu       if (tolr>0.){
61328a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61338a175baeSEmil Constantinescu         nr_loc++;
61348a175baeSEmil Constantinescu       }
61358a175baeSEmil Constantinescu       tol=tola+tolr;
61368a175baeSEmil Constantinescu       if (tol>0.){
61378a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61388a175baeSEmil Constantinescu         n_loc++;
61398a175baeSEmil Constantinescu       }
61408a175baeSEmil Constantinescu     }
61418a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61428a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61438a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61448a175baeSEmil Constantinescu     const PetscScalar *atol;
61458a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61468a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
614776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61488a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61498a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61508a175baeSEmil Constantinescu       if (tola>0.){
61518a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61528a175baeSEmil Constantinescu         na_loc++;
61538a175baeSEmil Constantinescu       }
61548a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61558a175baeSEmil Constantinescu       if (tolr>0.){
61568a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61578a175baeSEmil Constantinescu         nr_loc++;
61588a175baeSEmil Constantinescu       }
61598a175baeSEmil Constantinescu       tol=tola+tolr;
61608a175baeSEmil Constantinescu       if (tol>0.){
61618a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61628a175baeSEmil Constantinescu         n_loc++;
61638a175baeSEmil Constantinescu       }
61648a175baeSEmil Constantinescu     }
61658a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61668a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61678a175baeSEmil Constantinescu     const PetscScalar *rtol;
61688a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61698a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
617076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61718a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61728a175baeSEmil Constantinescu       tola = ts->atol;
61738a175baeSEmil Constantinescu       if (tola>0.){
61748a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61758a175baeSEmil Constantinescu         na_loc++;
61768a175baeSEmil Constantinescu       }
61778a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61788a175baeSEmil Constantinescu       if (tolr>0.){
61798a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61808a175baeSEmil Constantinescu         nr_loc++;
61818a175baeSEmil Constantinescu       }
61828a175baeSEmil Constantinescu       tol=tola+tolr;
61838a175baeSEmil Constantinescu       if (tol>0.){
61848a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61858a175baeSEmil Constantinescu         n_loc++;
61868a175baeSEmil Constantinescu       }
61878a175baeSEmil Constantinescu     }
61888a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61898a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
61908a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
619176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61928a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61938a175baeSEmil Constantinescu       tola = ts->atol;
61948a175baeSEmil Constantinescu       if (tola>0.){
61958a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61968a175baeSEmil Constantinescu         na_loc++;
61978a175baeSEmil Constantinescu       }
61988a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61998a175baeSEmil Constantinescu       if (tolr>0.){
62008a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62018a175baeSEmil Constantinescu         nr_loc++;
62028a175baeSEmil Constantinescu       }
62038a175baeSEmil Constantinescu       tol=tola+tolr;
62048a175baeSEmil Constantinescu       if (tol>0.){
62058a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62068a175baeSEmil Constantinescu         n_loc++;
62078a175baeSEmil Constantinescu       }
62088a175baeSEmil Constantinescu     }
62098a175baeSEmil Constantinescu   }
62108a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62118a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62128a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62138a175baeSEmil Constantinescu 
62148a175baeSEmil Constantinescu   err_loc[0] = sum;
62158a175baeSEmil Constantinescu   err_loc[1] = suma;
62168a175baeSEmil Constantinescu   err_loc[2] = sumr;
62178a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62188a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62198a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62208a175baeSEmil Constantinescu 
6221a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62228a175baeSEmil Constantinescu 
62238a175baeSEmil Constantinescu   gsum   = err_glb[0];
62248a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62258a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62268a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62278a175baeSEmil Constantinescu   na_glb = err_glb[4];
62288a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62298a175baeSEmil Constantinescu 
62308a175baeSEmil Constantinescu   *norm  = 0.;
62318a175baeSEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
62328a175baeSEmil Constantinescu   *norma = 0.;
62338a175baeSEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62348a175baeSEmil Constantinescu   *normr = 0.;
62358a175baeSEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62368a175baeSEmil Constantinescu 
62378a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62388a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62398a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62408a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62418a175baeSEmil Constantinescu }
62428a175baeSEmil Constantinescu 
62438a175baeSEmil Constantinescu /*@
62448a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62458a175baeSEmil Constantinescu    Collective on TS
62468a175baeSEmil Constantinescu 
62478a175baeSEmil Constantinescu    Input Arguments:
62488a175baeSEmil Constantinescu +  ts - time stepping context
62498a175baeSEmil Constantinescu .  E - error vector
62508a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62518a175baeSEmil Constantinescu -  Y - state vector, previous time step
62528a175baeSEmil Constantinescu 
62538a175baeSEmil Constantinescu    Output Arguments:
6254a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62558a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6256a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62578a175baeSEmil Constantinescu 
62588a175baeSEmil Constantinescu    Level: developer
62598a175baeSEmil Constantinescu 
62608a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62618a175baeSEmil Constantinescu @*/
62628a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62638a175baeSEmil Constantinescu {
62648a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62658a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62668a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62678a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
62688a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62698a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
62708a175baeSEmil Constantinescu 
62718a175baeSEmil Constantinescu   PetscFunctionBegin;
62728a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62738a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
62748a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
62758a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
62768a175baeSEmil Constantinescu   PetscValidType(E,2);
62778a175baeSEmil Constantinescu   PetscValidType(U,3);
62788a175baeSEmil Constantinescu   PetscValidType(Y,4);
62798a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
62808a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
62818a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
62828a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
62838a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
62848a175baeSEmil Constantinescu 
62858a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
62868a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
62878a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
62888a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
62898a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62908a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62918a175baeSEmil Constantinescu 
62928a175baeSEmil Constantinescu   max=0.;
62938a175baeSEmil Constantinescu   maxa=0.;
62948a175baeSEmil Constantinescu   maxr=0.;
62958a175baeSEmil Constantinescu 
62968a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
62978a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
62988a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62998a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63008a175baeSEmil Constantinescu 
63018a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
630276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63038a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63048a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63058a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63068a175baeSEmil Constantinescu       tol  = tola+tolr;
63078a175baeSEmil Constantinescu       if (tola>0.){
63088a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63098a175baeSEmil Constantinescu       }
63108a175baeSEmil Constantinescu       if (tolr>0.){
63118a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63128a175baeSEmil Constantinescu       }
63138a175baeSEmil Constantinescu       if (tol>0.){
63148a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63158a175baeSEmil Constantinescu       }
63168a175baeSEmil Constantinescu     }
63178a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63188a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63198a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63208a175baeSEmil Constantinescu     const PetscScalar *atol;
63218a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63228a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
632376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63248a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63258a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63268a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63278a175baeSEmil Constantinescu       tol  = tola+tolr;
63288a175baeSEmil Constantinescu       if (tola>0.){
63298a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63308a175baeSEmil Constantinescu       }
63318a175baeSEmil Constantinescu       if (tolr>0.){
63328a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63338a175baeSEmil Constantinescu       }
63348a175baeSEmil Constantinescu       if (tol>0.){
63358a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63368a175baeSEmil Constantinescu       }
63378a175baeSEmil Constantinescu     }
63388a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63398a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63408a175baeSEmil Constantinescu     const PetscScalar *rtol;
63418a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63428a175baeSEmil Constantinescu 
63438a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
634476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63458a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63468a175baeSEmil Constantinescu       tola = ts->atol;
63478a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63488a175baeSEmil Constantinescu       tol  = tola+tolr;
63498a175baeSEmil Constantinescu       if (tola>0.){
63508a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63518a175baeSEmil Constantinescu       }
63528a175baeSEmil Constantinescu       if (tolr>0.){
63538a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63548a175baeSEmil Constantinescu       }
63558a175baeSEmil Constantinescu       if (tol>0.){
63568a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63578a175baeSEmil Constantinescu       }
63588a175baeSEmil Constantinescu     }
63598a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63608a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63618a175baeSEmil Constantinescu 
63628a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
636376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63648a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63658a175baeSEmil Constantinescu       tola = ts->atol;
63668a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63678a175baeSEmil Constantinescu       tol  = tola+tolr;
63688a175baeSEmil Constantinescu       if (tola>0.){
63698a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63708a175baeSEmil Constantinescu       }
63718a175baeSEmil Constantinescu       if (tolr>0.){
63728a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63738a175baeSEmil Constantinescu       }
63748a175baeSEmil Constantinescu       if (tol>0.){
63758a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63768a175baeSEmil Constantinescu       }
63778a175baeSEmil Constantinescu     }
63788a175baeSEmil Constantinescu   }
63798a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
63808a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63818a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63828a175baeSEmil Constantinescu   err_loc[0] = max;
63838a175baeSEmil Constantinescu   err_loc[1] = maxa;
63848a175baeSEmil Constantinescu   err_loc[2] = maxr;
6385a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63868a175baeSEmil Constantinescu   gmax   = err_glb[0];
63878a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
63888a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
63898a175baeSEmil Constantinescu 
63908a175baeSEmil Constantinescu   *norm = gmax;
63918a175baeSEmil Constantinescu   *norma = gmaxa;
63928a175baeSEmil Constantinescu   *normr = gmaxr;
63938a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63948a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63958a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63968a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63978a175baeSEmil Constantinescu }
63988a175baeSEmil Constantinescu 
63998a175baeSEmil Constantinescu /*@
64008a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
64018a175baeSEmil Constantinescu 
64028a175baeSEmil Constantinescu    Collective on TS
64038a175baeSEmil Constantinescu 
64048a175baeSEmil Constantinescu    Input Arguments:
64058a175baeSEmil Constantinescu +  ts - time stepping context
64068a175baeSEmil Constantinescu .  E - error vector
64078a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64088a175baeSEmil Constantinescu .  Y - state vector, previous time step
64098a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64108a175baeSEmil Constantinescu 
64118a175baeSEmil Constantinescu    Output Arguments:
6412a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64138a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6414a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64158a175baeSEmil Constantinescu 
64168a175baeSEmil Constantinescu    Options Database Keys:
64178a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64188a175baeSEmil Constantinescu 
64198a175baeSEmil Constantinescu    Level: developer
64208a175baeSEmil Constantinescu 
64218a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64228a175baeSEmil Constantinescu @*/
64238a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64248a175baeSEmil Constantinescu {
64258a175baeSEmil Constantinescu   PetscErrorCode ierr;
64268a175baeSEmil Constantinescu 
64278a175baeSEmil Constantinescu   PetscFunctionBegin;
64288a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64298a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64308a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
64318a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64328a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64338a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64348a175baeSEmil Constantinescu }
64358a175baeSEmil Constantinescu 
64368a175baeSEmil Constantinescu 
64378d59e960SJed Brown /*@
64388d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64398d59e960SJed Brown 
64408d59e960SJed Brown    Logically Collective on TS
64418d59e960SJed Brown 
64428d59e960SJed Brown    Input Arguments:
64438d59e960SJed Brown +  ts - time stepping context
64448d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64458d59e960SJed Brown 
64468d59e960SJed Brown    Note:
64478d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64488d59e960SJed Brown 
64498d59e960SJed Brown    Level: intermediate
64508d59e960SJed Brown 
64518d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64528d59e960SJed Brown @*/
64538d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64548d59e960SJed Brown {
64558d59e960SJed Brown   PetscFunctionBegin;
64568d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64578d59e960SJed Brown   ts->cfltime_local = cfltime;
64588d59e960SJed Brown   ts->cfltime       = -1.;
64598d59e960SJed Brown   PetscFunctionReturn(0);
64608d59e960SJed Brown }
64618d59e960SJed Brown 
64628d59e960SJed Brown /*@
64638d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
64648d59e960SJed Brown 
64658d59e960SJed Brown    Collective on TS
64668d59e960SJed Brown 
64678d59e960SJed Brown    Input Arguments:
64688d59e960SJed Brown .  ts - time stepping context
64698d59e960SJed Brown 
64708d59e960SJed Brown    Output Arguments:
64718d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
64728d59e960SJed Brown 
64738d59e960SJed Brown    Level: advanced
64748d59e960SJed Brown 
64758d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
64768d59e960SJed Brown @*/
64778d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
64788d59e960SJed Brown {
64798d59e960SJed Brown   PetscErrorCode ierr;
64808d59e960SJed Brown 
64818d59e960SJed Brown   PetscFunctionBegin;
64828d59e960SJed Brown   if (ts->cfltime < 0) {
6483b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64848d59e960SJed Brown   }
64858d59e960SJed Brown   *cfltime = ts->cfltime;
64868d59e960SJed Brown   PetscFunctionReturn(0);
64878d59e960SJed Brown }
64888d59e960SJed Brown 
6489d6ebe24aSShri Abhyankar /*@
6490d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6491d6ebe24aSShri Abhyankar 
6492d6ebe24aSShri Abhyankar    Input Parameters:
6493a2b725a8SWilliam Gropp +  ts   - the TS context.
6494d6ebe24aSShri Abhyankar .  xl   - lower bound.
6495a2b725a8SWilliam Gropp -  xu   - upper bound.
6496d6ebe24aSShri Abhyankar 
6497d6ebe24aSShri Abhyankar    Notes:
6498d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6499e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6500d6ebe24aSShri Abhyankar 
65012bd2b0e6SSatish Balay    Level: advanced
65022bd2b0e6SSatish Balay 
6503d6ebe24aSShri Abhyankar @*/
6504d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6505d6ebe24aSShri Abhyankar {
6506d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6507d6ebe24aSShri Abhyankar   SNES           snes;
6508d6ebe24aSShri Abhyankar 
6509d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6510d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6511d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6512d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6513d6ebe24aSShri Abhyankar }
6514d6ebe24aSShri Abhyankar 
6515b3603a34SBarry Smith /*@C
65164f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6517b3603a34SBarry Smith        in a time based line graph
6518b3603a34SBarry Smith 
6519b3603a34SBarry Smith    Collective on TS
6520b3603a34SBarry Smith 
6521b3603a34SBarry Smith    Input Parameters:
6522b3603a34SBarry Smith +  ts - the TS context
6523b3603a34SBarry Smith .  step - current time-step
6524b3603a34SBarry Smith .  ptime - current time
65257db568b7SBarry Smith .  u - current solution
65267db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6527b3603a34SBarry Smith 
6528b3d3934dSBarry Smith    Options Database:
65299ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6530b3d3934dSBarry Smith 
6531b3603a34SBarry Smith    Level: intermediate
6532b3603a34SBarry Smith 
653395452b02SPatrick Sanan    Notes:
653495452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6535b3603a34SBarry Smith 
65367db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65377db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65387db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65397db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6540b3603a34SBarry Smith @*/
65417db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6542b3603a34SBarry Smith {
6543b3603a34SBarry Smith   PetscErrorCode    ierr;
65447db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6545b3603a34SBarry Smith   const PetscScalar *yy;
654680666b62SBarry Smith   Vec               v;
6547b3603a34SBarry Smith 
6548b3603a34SBarry Smith   PetscFunctionBegin;
654963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
655058ff32f7SBarry Smith   if (!step) {
6551a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65526934998bSLisandro Dalcin     PetscInt      dim;
6553a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65540ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6555bab0a581SBarry Smith     if (!ctx->names) {
6556bab0a581SBarry Smith       PetscBool flg;
6557bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6558bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6559bab0a581SBarry Smith       if (flg) {
6560bab0a581SBarry Smith         PetscInt i,n;
6561bab0a581SBarry Smith         char     **names;
6562bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6563bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6564bab0a581SBarry Smith         for (i=0; i<n; i++) {
6565bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6566bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6567bab0a581SBarry Smith         }
6568bab0a581SBarry Smith         names[n] = NULL;
6569bab0a581SBarry Smith         ctx->names = names;
6570bab0a581SBarry Smith       }
6571bab0a581SBarry Smith     }
6572387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6573387f4636SBarry Smith       char      **displaynames;
6574387f4636SBarry Smith       PetscBool flg;
6575387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6576580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6577c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6578387f4636SBarry Smith       if (flg) {
6579a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6580387f4636SBarry Smith       }
6581387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6582387f4636SBarry Smith     }
6583387f4636SBarry Smith     if (ctx->displaynames) {
6584387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6585387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6586387f4636SBarry Smith     } else if (ctx->names) {
65870910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65880b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6589387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6590b0bc92c6SBarry Smith     } else {
6591b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6592b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6593387f4636SBarry Smith     }
65940b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
659558ff32f7SBarry Smith   }
65966934998bSLisandro Dalcin 
65976934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65986934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
659980666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66006934998bSLisandro Dalcin   if (ctx->displaynames) {
66016934998bSLisandro Dalcin     PetscInt i;
66026934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66036934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66046934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66056934998bSLisandro Dalcin   } else {
6606e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6607e3efe391SJed Brown     PetscInt  i,n;
66086934998bSLisandro Dalcin     PetscReal *yreal;
660980666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6610785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6611e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66120b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6613e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6614e3efe391SJed Brown #else
66150b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6616e3efe391SJed Brown #endif
661780666b62SBarry Smith   }
66186934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66196934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66206934998bSLisandro Dalcin 
6621b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66220b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66236934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66243923b477SBarry Smith   }
6625b3603a34SBarry Smith   PetscFunctionReturn(0);
6626b3603a34SBarry Smith }
6627b3603a34SBarry Smith 
6628b037adc7SBarry Smith /*@C
662931152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6630b037adc7SBarry Smith 
6631b037adc7SBarry Smith    Collective on TS
6632b037adc7SBarry Smith 
6633b037adc7SBarry Smith    Input Parameters:
6634b037adc7SBarry Smith +  ts - the TS context
6635b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6636b037adc7SBarry Smith 
6637b037adc7SBarry Smith    Level: intermediate
6638b037adc7SBarry Smith 
663995452b02SPatrick Sanan    Notes:
664095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66417db568b7SBarry Smith 
6642a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6643b037adc7SBarry Smith @*/
664431152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6645b037adc7SBarry Smith {
6646b037adc7SBarry Smith   PetscErrorCode    ierr;
6647b037adc7SBarry Smith   PetscInt          i;
6648b037adc7SBarry Smith 
6649b037adc7SBarry Smith   PetscFunctionBegin;
6650b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6651b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66525537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6653b3d3934dSBarry Smith       break;
6654b3d3934dSBarry Smith     }
6655b3d3934dSBarry Smith   }
6656b3d3934dSBarry Smith   PetscFunctionReturn(0);
6657b3d3934dSBarry Smith }
6658b3d3934dSBarry Smith 
6659e673d494SBarry Smith /*@C
6660e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6661e673d494SBarry Smith 
6662e673d494SBarry Smith    Collective on TS
6663e673d494SBarry Smith 
6664e673d494SBarry Smith    Input Parameters:
6665e673d494SBarry Smith +  ts - the TS context
6666e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6667e673d494SBarry Smith 
6668e673d494SBarry Smith    Level: intermediate
6669e673d494SBarry Smith 
6670a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6671e673d494SBarry Smith @*/
6672e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6673e673d494SBarry Smith {
6674e673d494SBarry Smith   PetscErrorCode    ierr;
6675e673d494SBarry Smith 
6676e673d494SBarry Smith   PetscFunctionBegin;
6677e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6678e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6679e673d494SBarry Smith   PetscFunctionReturn(0);
6680e673d494SBarry Smith }
6681e673d494SBarry Smith 
6682b3d3934dSBarry Smith /*@C
6683b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6684b3d3934dSBarry Smith 
6685b3d3934dSBarry Smith    Collective on TS
6686b3d3934dSBarry Smith 
6687b3d3934dSBarry Smith    Input Parameter:
6688b3d3934dSBarry Smith .  ts - the TS context
6689b3d3934dSBarry Smith 
6690b3d3934dSBarry Smith    Output Parameter:
6691b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6692b3d3934dSBarry Smith 
6693b3d3934dSBarry Smith    Level: intermediate
6694b3d3934dSBarry Smith 
669595452b02SPatrick Sanan    Notes:
669695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66977db568b7SBarry Smith 
6698b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6699b3d3934dSBarry Smith @*/
6700b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6701b3d3934dSBarry Smith {
6702b3d3934dSBarry Smith   PetscInt       i;
6703b3d3934dSBarry Smith 
6704b3d3934dSBarry Smith   PetscFunctionBegin;
6705b3d3934dSBarry Smith   *names = NULL;
6706b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6707b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67085537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6709b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6710b3d3934dSBarry Smith       break;
6711387f4636SBarry Smith     }
6712387f4636SBarry Smith   }
6713387f4636SBarry Smith   PetscFunctionReturn(0);
6714387f4636SBarry Smith }
6715387f4636SBarry Smith 
6716a66092f1SBarry Smith /*@C
6717a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6718a66092f1SBarry Smith 
6719a66092f1SBarry Smith    Collective on TS
6720a66092f1SBarry Smith 
6721a66092f1SBarry Smith    Input Parameters:
6722a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6723a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6724a66092f1SBarry Smith 
6725a66092f1SBarry Smith    Level: intermediate
6726a66092f1SBarry Smith 
6727a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6728a66092f1SBarry Smith @*/
6729a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6730a66092f1SBarry Smith {
6731a66092f1SBarry Smith   PetscInt          j = 0,k;
6732a66092f1SBarry Smith   PetscErrorCode    ierr;
6733a66092f1SBarry Smith 
6734a66092f1SBarry Smith   PetscFunctionBegin;
6735a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6736a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6737a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6738a66092f1SBarry Smith   while (displaynames[j]) j++;
6739a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6740a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6741a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6742a66092f1SBarry Smith   j = 0;
6743a66092f1SBarry Smith   while (displaynames[j]) {
6744a66092f1SBarry Smith     k = 0;
6745a66092f1SBarry Smith     while (ctx->names[k]) {
6746a66092f1SBarry Smith       PetscBool flg;
6747a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6748a66092f1SBarry Smith       if (flg) {
6749a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6750a66092f1SBarry Smith         break;
6751a66092f1SBarry Smith       }
6752a66092f1SBarry Smith       k++;
6753a66092f1SBarry Smith     }
6754a66092f1SBarry Smith     j++;
6755a66092f1SBarry Smith   }
6756a66092f1SBarry Smith   PetscFunctionReturn(0);
6757a66092f1SBarry Smith }
6758a66092f1SBarry Smith 
6759387f4636SBarry Smith /*@C
6760387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6761387f4636SBarry Smith 
6762387f4636SBarry Smith    Collective on TS
6763387f4636SBarry Smith 
6764387f4636SBarry Smith    Input Parameters:
6765387f4636SBarry Smith +  ts - the TS context
6766a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6767387f4636SBarry Smith 
676895452b02SPatrick Sanan    Notes:
676995452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67707db568b7SBarry Smith 
6771387f4636SBarry Smith    Level: intermediate
6772387f4636SBarry Smith 
6773387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6774387f4636SBarry Smith @*/
6775387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6776387f4636SBarry Smith {
6777a66092f1SBarry Smith   PetscInt          i;
6778387f4636SBarry Smith   PetscErrorCode    ierr;
6779387f4636SBarry Smith 
6780387f4636SBarry Smith   PetscFunctionBegin;
6781387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6782387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67835537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6784b3d3934dSBarry Smith       break;
6785b037adc7SBarry Smith     }
6786b037adc7SBarry Smith   }
6787b037adc7SBarry Smith   PetscFunctionReturn(0);
6788b037adc7SBarry Smith }
6789b037adc7SBarry Smith 
679080666b62SBarry Smith /*@C
679180666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
679280666b62SBarry Smith 
679380666b62SBarry Smith    Collective on TS
679480666b62SBarry Smith 
679580666b62SBarry Smith    Input Parameters:
679680666b62SBarry Smith +  ts - the TS context
679780666b62SBarry Smith .  transform - the transform function
67987684fa3eSBarry Smith .  destroy - function to destroy the optional context
679980666b62SBarry Smith -  ctx - optional context used by transform function
680080666b62SBarry Smith 
680195452b02SPatrick Sanan    Notes:
680295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68037db568b7SBarry Smith 
680480666b62SBarry Smith    Level: intermediate
680580666b62SBarry Smith 
6806a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
680780666b62SBarry Smith @*/
68087684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
680980666b62SBarry Smith {
681080666b62SBarry Smith   PetscInt          i;
6811a66092f1SBarry Smith   PetscErrorCode    ierr;
681280666b62SBarry Smith 
681380666b62SBarry Smith   PetscFunctionBegin;
681480666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
681580666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68165537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
681780666b62SBarry Smith     }
681880666b62SBarry Smith   }
681980666b62SBarry Smith   PetscFunctionReturn(0);
682080666b62SBarry Smith }
682180666b62SBarry Smith 
6822e673d494SBarry Smith /*@C
6823e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6824e673d494SBarry Smith 
6825e673d494SBarry Smith    Collective on TSLGCtx
6826e673d494SBarry Smith 
6827e673d494SBarry Smith    Input Parameters:
6828e673d494SBarry Smith +  ts - the TS context
6829e673d494SBarry Smith .  transform - the transform function
68307684fa3eSBarry Smith .  destroy - function to destroy the optional context
6831e673d494SBarry Smith -  ctx - optional context used by transform function
6832e673d494SBarry Smith 
6833e673d494SBarry Smith    Level: intermediate
6834e673d494SBarry Smith 
6835a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6836e673d494SBarry Smith @*/
68377684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6838e673d494SBarry Smith {
6839e673d494SBarry Smith   PetscFunctionBegin;
6840e673d494SBarry Smith   ctx->transform    = transform;
68417684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6842e673d494SBarry Smith   ctx->transformctx = tctx;
6843e673d494SBarry Smith   PetscFunctionReturn(0);
6844e673d494SBarry Smith }
6845e673d494SBarry Smith 
6846ef20d060SBarry Smith /*@C
68478b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6848ef20d060SBarry Smith        in a time based line graph
6849ef20d060SBarry Smith 
6850ef20d060SBarry Smith    Collective on TS
6851ef20d060SBarry Smith 
6852ef20d060SBarry Smith    Input Parameters:
6853ef20d060SBarry Smith +  ts - the TS context
6854ef20d060SBarry Smith .  step - current time-step
6855ef20d060SBarry Smith .  ptime - current time
68567db568b7SBarry Smith .  u - current solution
68577db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6858ef20d060SBarry Smith 
6859ef20d060SBarry Smith    Level: intermediate
6860ef20d060SBarry Smith 
686195452b02SPatrick Sanan    Notes:
686295452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6863abd5a294SJed Brown 
6864abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6865abd5a294SJed Brown 
6866abd5a294SJed Brown    Options Database Keys:
68674f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6868ef20d060SBarry Smith 
6869abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6870ef20d060SBarry Smith @*/
68710910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6872ef20d060SBarry Smith {
6873ef20d060SBarry Smith   PetscErrorCode    ierr;
68740b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6875ef20d060SBarry Smith   const PetscScalar *yy;
6876ef20d060SBarry Smith   Vec               y;
6877ef20d060SBarry Smith 
6878ef20d060SBarry Smith   PetscFunctionBegin;
6879a9f9c1f6SBarry Smith   if (!step) {
6880a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68816934998bSLisandro Dalcin     PetscInt      dim;
6882a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68838b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68840910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6885a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6886a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6887a9f9c1f6SBarry Smith   }
68880910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6889ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68900910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6891ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6892e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6893e3efe391SJed Brown   {
6894e3efe391SJed Brown     PetscReal *yreal;
6895e3efe391SJed Brown     PetscInt  i,n;
6896e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6897785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6898e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
68990b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6900e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6901e3efe391SJed Brown   }
6902e3efe391SJed Brown #else
69030b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6904e3efe391SJed Brown #endif
6905ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6906ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6907b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69080b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69096934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69103923b477SBarry Smith   }
6911ef20d060SBarry Smith   PetscFunctionReturn(0);
6912ef20d060SBarry Smith }
6913ef20d060SBarry Smith 
69145e3b7effSJoseph Pusztay /*@C
69155e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
69165e3b7effSJoseph Pusztay 
69175e3b7effSJoseph Pusztay    Input Parameters:
69185e3b7effSJoseph Pusztay +  ts - the TS context
69195e3b7effSJoseph Pusztay .  step - current time-step
69205e3b7effSJoseph Pusztay .  ptime - current time
69215e3b7effSJoseph Pusztay .  u - current solution
69225e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
69235e3b7effSJoseph Pusztay 
69245e3b7effSJoseph Pusztay    Options Database:
6925918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
69265e3b7effSJoseph Pusztay 
69275e3b7effSJoseph Pusztay    Level: intermediate
69285e3b7effSJoseph Pusztay 
69295e3b7effSJoseph Pusztay @*/
69300ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
69311b575b74SJoseph Pusztay {
69321b575b74SJoseph Pusztay   PetscErrorCode    ierr;
69331b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
69341b575b74SJoseph Pusztay   const PetscScalar *yy;
6935b1670a61SJoseph Pusztay   PetscReal       *y,*x;
69361b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
69371b575b74SJoseph Pusztay   DM                dm;
69381b575b74SJoseph Pusztay 
69391b575b74SJoseph Pusztay   PetscFunctionBegin;
69401b575b74SJoseph Pusztay 
69411b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
69421b575b74SJoseph Pusztay   if (!step) {
69431b575b74SJoseph Pusztay     PetscDrawAxis axis;
69441b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
69451b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6946895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6947895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
69481b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
69491b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
69501b575b74SJoseph Pusztay     if (dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
69511b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69521b575b74SJoseph Pusztay     Np /= 2*dim;
69531b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69541b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69551b575b74SJoseph Pusztay   }
69561b575b74SJoseph Pusztay 
69571b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69581b575b74SJoseph Pusztay   Np /= 2*dim;
69591b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6960895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69611b575b74SJoseph Pusztay   /* get points from solution vector */
69621b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6963b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6964b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6965895f37d5SJoseph Pusztay   }
69661b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
69671b575b74SJoseph Pusztay 
69681b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69691b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
69701b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
69711b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
69721b575b74SJoseph Pusztay   }
69731b575b74SJoseph Pusztay 
6974918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6975918b1d10SJoseph Pusztay 
69761b575b74SJoseph Pusztay   PetscFunctionReturn(0);
69771b575b74SJoseph Pusztay }
69781b575b74SJoseph Pusztay 
69791b575b74SJoseph Pusztay 
69801b575b74SJoseph Pusztay 
69817cf37e64SBarry Smith /*@C
69827cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69837cf37e64SBarry Smith 
69847cf37e64SBarry Smith    Collective on TS
69857cf37e64SBarry Smith 
69867cf37e64SBarry Smith    Input Parameters:
69877cf37e64SBarry Smith +  ts - the TS context
69887cf37e64SBarry Smith .  step - current time-step
69897cf37e64SBarry Smith .  ptime - current time
69907cf37e64SBarry Smith .  u - current solution
69917cf37e64SBarry Smith -  dctx - unused context
69927cf37e64SBarry Smith 
69937cf37e64SBarry Smith    Level: intermediate
69947cf37e64SBarry Smith 
69957cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
69967cf37e64SBarry Smith 
69977cf37e64SBarry Smith    Options Database Keys:
69987cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
69997cf37e64SBarry Smith 
70007cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
70017cf37e64SBarry Smith @*/
7002edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70037cf37e64SBarry Smith {
70047cf37e64SBarry Smith   PetscErrorCode    ierr;
70057cf37e64SBarry Smith   Vec               y;
70067cf37e64SBarry Smith   PetscReal         nrm;
7007edbaebb3SBarry Smith   PetscBool         flg;
70087cf37e64SBarry Smith 
70097cf37e64SBarry Smith   PetscFunctionBegin;
70107cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70117cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70127cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7013edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7014edbaebb3SBarry Smith   if (flg) {
70157cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7016edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7017edbaebb3SBarry Smith   }
7018edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7019edbaebb3SBarry Smith   if (flg) {
7020edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7021edbaebb3SBarry Smith   }
7022edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70237cf37e64SBarry Smith   PetscFunctionReturn(0);
70247cf37e64SBarry Smith }
70257cf37e64SBarry Smith 
7026201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7027201da799SBarry Smith {
7028201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7029201da799SBarry Smith   PetscReal      x   = ptime,y;
7030201da799SBarry Smith   PetscErrorCode ierr;
7031201da799SBarry Smith   PetscInt       its;
7032201da799SBarry Smith 
7033201da799SBarry Smith   PetscFunctionBegin;
703463e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7035201da799SBarry Smith   if (!n) {
7036201da799SBarry Smith     PetscDrawAxis axis;
7037201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7038201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7039201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7040201da799SBarry Smith     ctx->snes_its = 0;
7041201da799SBarry Smith   }
7042201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7043201da799SBarry Smith   y    = its - ctx->snes_its;
7044201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70453fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7046201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70476934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7048201da799SBarry Smith   }
7049201da799SBarry Smith   ctx->snes_its = its;
7050201da799SBarry Smith   PetscFunctionReturn(0);
7051201da799SBarry Smith }
7052201da799SBarry Smith 
7053201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7054201da799SBarry Smith {
7055201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7056201da799SBarry Smith   PetscReal      x   = ptime,y;
7057201da799SBarry Smith   PetscErrorCode ierr;
7058201da799SBarry Smith   PetscInt       its;
7059201da799SBarry Smith 
7060201da799SBarry Smith   PetscFunctionBegin;
706163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7062201da799SBarry Smith   if (!n) {
7063201da799SBarry Smith     PetscDrawAxis axis;
7064201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7065201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7066201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7067201da799SBarry Smith     ctx->ksp_its = 0;
7068201da799SBarry Smith   }
7069201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7070201da799SBarry Smith   y    = its - ctx->ksp_its;
7071201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
707299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7073201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70746934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7075201da799SBarry Smith   }
7076201da799SBarry Smith   ctx->ksp_its = its;
7077201da799SBarry Smith   PetscFunctionReturn(0);
7078201da799SBarry Smith }
7079f9c1d6abSBarry Smith 
7080f9c1d6abSBarry Smith /*@
7081f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7082f9c1d6abSBarry Smith 
7083d083f849SBarry Smith    Collective on TS
7084f9c1d6abSBarry Smith 
7085f9c1d6abSBarry Smith    Input Parameters:
7086f9c1d6abSBarry Smith +  ts - the TS context
7087f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7088f9c1d6abSBarry Smith 
7089f9c1d6abSBarry Smith    Output Parameters:
7090f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7091f9c1d6abSBarry Smith 
7092f9c1d6abSBarry Smith    Level: developer
7093f9c1d6abSBarry Smith 
7094f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7095f9c1d6abSBarry Smith @*/
7096f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7097f9c1d6abSBarry Smith {
7098f9c1d6abSBarry Smith   PetscErrorCode ierr;
7099f9c1d6abSBarry Smith 
7100f9c1d6abSBarry Smith   PetscFunctionBegin;
7101f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7102ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7103f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7104f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7105f9c1d6abSBarry Smith }
710624655328SShri 
7107b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7108b3d3934dSBarry Smith /*@C
7109b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7110b3d3934dSBarry Smith 
7111b3d3934dSBarry Smith    Collective on TS
7112b3d3934dSBarry Smith 
7113b3d3934dSBarry Smith    Input Parameters:
7114b3d3934dSBarry Smith .  ts  - the ODE solver object
7115b3d3934dSBarry Smith 
7116b3d3934dSBarry Smith    Output Parameter:
7117b3d3934dSBarry Smith .  ctx - the context
7118b3d3934dSBarry Smith 
7119b3d3934dSBarry Smith    Level: intermediate
7120b3d3934dSBarry Smith 
7121b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7122b3d3934dSBarry Smith 
7123b3d3934dSBarry Smith @*/
7124b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7125b3d3934dSBarry Smith {
7126b3d3934dSBarry Smith   PetscErrorCode ierr;
7127b3d3934dSBarry Smith 
7128b3d3934dSBarry Smith   PetscFunctionBegin;
7129a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7130b3d3934dSBarry Smith   PetscFunctionReturn(0);
7131b3d3934dSBarry Smith }
7132b3d3934dSBarry Smith 
7133b3d3934dSBarry Smith /*@C
7134b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7135b3d3934dSBarry Smith 
7136b3d3934dSBarry Smith    Collective on TS
7137b3d3934dSBarry Smith 
7138b3d3934dSBarry Smith    Input Parameters:
7139b3d3934dSBarry Smith +  ts - the TS context
7140b3d3934dSBarry Smith .  step - current time-step
7141b3d3934dSBarry Smith .  ptime - current time
71427db568b7SBarry Smith .  u  - current solution
71437db568b7SBarry Smith -  dctx - the envelope context
7144b3d3934dSBarry Smith 
7145b3d3934dSBarry Smith    Options Database:
7146b3d3934dSBarry Smith .  -ts_monitor_envelope
7147b3d3934dSBarry Smith 
7148b3d3934dSBarry Smith    Level: intermediate
7149b3d3934dSBarry Smith 
715095452b02SPatrick Sanan    Notes:
715195452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7152b3d3934dSBarry Smith 
71537db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7154b3d3934dSBarry Smith @*/
71557db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7156b3d3934dSBarry Smith {
7157b3d3934dSBarry Smith   PetscErrorCode       ierr;
71587db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7159b3d3934dSBarry Smith 
7160b3d3934dSBarry Smith   PetscFunctionBegin;
7161b3d3934dSBarry Smith   if (!ctx->max) {
7162b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7163b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7164b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7165b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7166b3d3934dSBarry Smith   } else {
7167b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7168b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7169b3d3934dSBarry Smith   }
7170b3d3934dSBarry Smith   PetscFunctionReturn(0);
7171b3d3934dSBarry Smith }
7172b3d3934dSBarry Smith 
7173b3d3934dSBarry Smith /*@C
7174b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7175b3d3934dSBarry Smith 
7176b3d3934dSBarry Smith    Collective on TS
7177b3d3934dSBarry Smith 
7178b3d3934dSBarry Smith    Input Parameter:
7179b3d3934dSBarry Smith .  ts - the TS context
7180b3d3934dSBarry Smith 
7181b3d3934dSBarry Smith    Output Parameter:
7182b3d3934dSBarry Smith +  max - the maximum values
7183b3d3934dSBarry Smith -  min - the minimum values
7184b3d3934dSBarry Smith 
718595452b02SPatrick Sanan    Notes:
718695452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71877db568b7SBarry Smith 
7188b3d3934dSBarry Smith    Level: intermediate
7189b3d3934dSBarry Smith 
7190b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7191b3d3934dSBarry Smith @*/
7192b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7193b3d3934dSBarry Smith {
7194b3d3934dSBarry Smith   PetscInt i;
7195b3d3934dSBarry Smith 
7196b3d3934dSBarry Smith   PetscFunctionBegin;
7197b3d3934dSBarry Smith   if (max) *max = NULL;
7198b3d3934dSBarry Smith   if (min) *min = NULL;
7199b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7200b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
72015537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7202b3d3934dSBarry Smith       if (max) *max = ctx->max;
7203b3d3934dSBarry Smith       if (min) *min = ctx->min;
7204b3d3934dSBarry Smith       break;
7205b3d3934dSBarry Smith     }
7206b3d3934dSBarry Smith   }
7207b3d3934dSBarry Smith   PetscFunctionReturn(0);
7208b3d3934dSBarry Smith }
7209b3d3934dSBarry Smith 
7210b3d3934dSBarry Smith /*@C
7211b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7212b3d3934dSBarry Smith 
7213b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7214b3d3934dSBarry Smith 
7215b3d3934dSBarry Smith    Input Parameter:
7216b3d3934dSBarry Smith .  ctx - the monitor context
7217b3d3934dSBarry Smith 
7218b3d3934dSBarry Smith    Level: intermediate
7219b3d3934dSBarry Smith 
72207db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7221b3d3934dSBarry Smith @*/
7222b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7223b3d3934dSBarry Smith {
7224b3d3934dSBarry Smith   PetscErrorCode ierr;
7225b3d3934dSBarry Smith 
7226b3d3934dSBarry Smith   PetscFunctionBegin;
7227b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7228b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7229b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7230b3d3934dSBarry Smith   PetscFunctionReturn(0);
7231b3d3934dSBarry Smith }
7232f2dee214SBarry Smith 
723324655328SShri /*@
7234dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7235dcb233daSLisandro Dalcin 
7236dcb233daSLisandro Dalcin    Collective on TS
7237dcb233daSLisandro Dalcin 
7238dcb233daSLisandro Dalcin    Input Parameter:
7239dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7240dcb233daSLisandro Dalcin 
7241dcb233daSLisandro Dalcin    Level: advanced
7242dcb233daSLisandro Dalcin 
7243dcb233daSLisandro Dalcin    Notes:
7244dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7245dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7246dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7247dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7248dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7249dcb233daSLisandro Dalcin    discontinuous source terms).
7250dcb233daSLisandro Dalcin 
7251dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7252dcb233daSLisandro Dalcin @*/
7253dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7254dcb233daSLisandro Dalcin {
7255dcb233daSLisandro Dalcin   PetscFunctionBegin;
7256dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7257dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7258dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7259dcb233daSLisandro Dalcin }
7260dcb233daSLisandro Dalcin 
7261dcb233daSLisandro Dalcin /*@
726224655328SShri    TSRollBack - Rolls back one time step
726324655328SShri 
726424655328SShri    Collective on TS
726524655328SShri 
726624655328SShri    Input Parameter:
726724655328SShri .  ts - the TS context obtained from TSCreate()
726824655328SShri 
726924655328SShri    Level: advanced
727024655328SShri 
727124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
727224655328SShri @*/
727324655328SShri PetscErrorCode  TSRollBack(TS ts)
727424655328SShri {
727524655328SShri   PetscErrorCode ierr;
727624655328SShri 
727724655328SShri   PetscFunctionBegin;
727824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7279b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
728024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
728124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
728224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
728324655328SShri   ts->ptime = ts->ptime_prev;
7284be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
72852808aa04SLisandro Dalcin   ts->steps--;
7286b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
728724655328SShri   PetscFunctionReturn(0);
728824655328SShri }
7289aeb4809dSShri Abhyankar 
7290ff22ae23SHong Zhang /*@
7291ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7292ff22ae23SHong Zhang 
7293ff22ae23SHong Zhang    Input Parameter:
7294ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7295ff22ae23SHong Zhang 
72960429704eSStefano Zampini    Output Parameters:
72970429704eSStefano Zampini +  ns - the number of stages
72980429704eSStefano Zampini -  Y - the current stage vectors
72990429704eSStefano Zampini 
7300ff22ae23SHong Zhang    Level: advanced
7301ff22ae23SHong Zhang 
73020429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
73030429704eSStefano Zampini 
7304ff22ae23SHong Zhang .seealso: TSCreate()
7305ff22ae23SHong Zhang @*/
7306ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7307ff22ae23SHong Zhang {
7308ff22ae23SHong Zhang   PetscErrorCode ierr;
7309ff22ae23SHong Zhang 
7310ff22ae23SHong Zhang   PetscFunctionBegin;
7311ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
73120429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
73130429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
73140429704eSStefano Zampini   if (!ts->ops->getstages) {
73150429704eSStefano Zampini     if (ns) *ns = 0;
73160429704eSStefano Zampini     if (Y) *Y = NULL;
73170429704eSStefano Zampini   } else {
7318ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7319ff22ae23SHong Zhang   }
7320ff22ae23SHong Zhang   PetscFunctionReturn(0);
7321ff22ae23SHong Zhang }
7322ff22ae23SHong Zhang 
7323847ff0e1SMatthew G. Knepley /*@C
7324847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7325847ff0e1SMatthew G. Knepley 
7326847ff0e1SMatthew G. Knepley   Collective on SNES
7327847ff0e1SMatthew G. Knepley 
7328847ff0e1SMatthew G. Knepley   Input Parameters:
7329847ff0e1SMatthew G. Knepley + ts - the TS context
7330847ff0e1SMatthew G. Knepley . t - current timestep
7331847ff0e1SMatthew G. Knepley . U - state vector
7332847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7333847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7334847ff0e1SMatthew G. Knepley - ctx - an optional user context
7335847ff0e1SMatthew G. Knepley 
7336847ff0e1SMatthew G. Knepley   Output Parameters:
7337847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7338847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7339847ff0e1SMatthew G. Knepley 
7340847ff0e1SMatthew G. Knepley   Level: intermediate
7341847ff0e1SMatthew G. Knepley 
7342847ff0e1SMatthew G. Knepley   Notes:
7343847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7344847ff0e1SMatthew G. Knepley 
7345847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7346847ff0e1SMatthew G. Knepley 
7347847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7348847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7349847ff0e1SMatthew G. Knepley 
7350847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7351847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7352847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7353847ff0e1SMatthew G. Knepley 
7354847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7355847ff0e1SMatthew G. Knepley @*/
7356847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7357847ff0e1SMatthew G. Knepley {
7358847ff0e1SMatthew G. Knepley   SNES           snes;
7359847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7360847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7361847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7362847ff0e1SMatthew G. Knepley 
7363847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7364c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7365847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7366847ff0e1SMatthew G. Knepley   if (!color) {
7367847ff0e1SMatthew G. Knepley     DM         dm;
7368847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7369847ff0e1SMatthew G. Knepley 
7370847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7371847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7372847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7373847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7374847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7375847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7376847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7377847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7378847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7379847ff0e1SMatthew G. Knepley     } else {
7380847ff0e1SMatthew G. Knepley       MatColoring mc;
7381847ff0e1SMatthew G. Knepley 
7382847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7383847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7384847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7385847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7386847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7387847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7388847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7389847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7390847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7391847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7392847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7393847ff0e1SMatthew G. Knepley     }
7394847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7395847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7396847ff0e1SMatthew G. Knepley   }
7397847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7398847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7399847ff0e1SMatthew G. Knepley   if (J != B) {
7400847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7401847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7402847ff0e1SMatthew G. Knepley   }
7403847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7404847ff0e1SMatthew G. Knepley }
740593b34091SDebojyoti Ghosh 
7406cb9d8021SPierre Barbier de Reuille /*@
74076bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7408cb9d8021SPierre Barbier de Reuille 
7409cb9d8021SPierre Barbier de Reuille     Input Parameters:
74106bc98fa9SBarry Smith +    ts - the TS context
74116bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
74126bc98fa9SBarry Smith 
74136bc98fa9SBarry Smith     Calling sequence of func:
74146bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
74156bc98fa9SBarry Smith 
74166bc98fa9SBarry Smith +   ts - the TS context
74176bc98fa9SBarry Smith .   time - the current time (of the stage)
74186bc98fa9SBarry Smith .   state - the state to check if it is valid
74196bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7420cb9d8021SPierre Barbier de Reuille 
7421cb9d8021SPierre Barbier de Reuille     Level: intermediate
7422cb9d8021SPierre Barbier de Reuille 
74236bc98fa9SBarry Smith     Notes:
74246bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
74256bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
74266bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
74276bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
74286bc98fa9SBarry Smith 
74296bc98fa9SBarry Smith     Developer Notes:
74306bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
74316bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
74326bc98fa9SBarry Smith 
74336bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7434cb9d8021SPierre Barbier de Reuille @*/
7435cb9d8021SPierre Barbier de Reuille 
7436d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7437cb9d8021SPierre Barbier de Reuille {
7438cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7439cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7440cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7441cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7442cb9d8021SPierre Barbier de Reuille }
7443cb9d8021SPierre Barbier de Reuille 
7444cb9d8021SPierre Barbier de Reuille /*@
74456bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7446cb9d8021SPierre Barbier de Reuille 
7447cb9d8021SPierre Barbier de Reuille     Input Parameters:
74486bc98fa9SBarry Smith +    ts - the TS context
74496bc98fa9SBarry Smith .    stagetime - time of the simulation
74506bc98fa9SBarry Smith -    Y - state vector to check.
7451cb9d8021SPierre Barbier de Reuille 
7452cb9d8021SPierre Barbier de Reuille     Output Parameter:
74536bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7454cb9d8021SPierre Barbier de Reuille 
7455cb9d8021SPierre Barbier de Reuille     Note:
74566bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74576bc98fa9SBarry Smith     to check if the current state is valid.
745896a0c994SBarry Smith 
74596bc98fa9SBarry Smith     Level: developer
74606bc98fa9SBarry Smith 
74616bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7462cb9d8021SPierre Barbier de Reuille @*/
7463d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7464cb9d8021SPierre Barbier de Reuille {
7465cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7466cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7467cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7468cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7469d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7470cb9d8021SPierre Barbier de Reuille   }
7471cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7472cb9d8021SPierre Barbier de Reuille }
74731ceb14c0SBarry Smith 
747493b34091SDebojyoti Ghosh /*@C
7475e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
747693b34091SDebojyoti Ghosh 
7477d083f849SBarry Smith   Collective
747893b34091SDebojyoti Ghosh 
747993b34091SDebojyoti Ghosh   Input Parameter:
748093b34091SDebojyoti Ghosh . tsin    - The input TS
748193b34091SDebojyoti Ghosh 
748293b34091SDebojyoti Ghosh   Output Parameter:
7483e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
748493b34091SDebojyoti Ghosh 
74855eca1a21SEmil Constantinescu   Notes:
74865eca1a21SEmil 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.
74875eca1a21SEmil Constantinescu 
7488928bb9adSStefano 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);
74895eca1a21SEmil Constantinescu 
74905eca1a21SEmil Constantinescu   Level: developer
749193b34091SDebojyoti Ghosh 
7492e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
749393b34091SDebojyoti Ghosh @*/
7494baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
749593b34091SDebojyoti Ghosh {
749693b34091SDebojyoti Ghosh   TS             t;
749793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7498dc846ba4SSatish Balay   SNES           snes_start;
7499dc846ba4SSatish Balay   DM             dm;
7500dc846ba4SSatish Balay   TSType         type;
750193b34091SDebojyoti Ghosh 
750293b34091SDebojyoti Ghosh   PetscFunctionBegin;
750393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
750493b34091SDebojyoti Ghosh   *tsout = NULL;
750593b34091SDebojyoti Ghosh 
75067a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
750793b34091SDebojyoti Ghosh 
750893b34091SDebojyoti Ghosh   /* General TS description */
750993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
751093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
751193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
751293b34091SDebojyoti Ghosh   t->snes_its          = 0;
751393b34091SDebojyoti Ghosh   t->nwork             = 0;
75147d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
751593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
751693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
751793b34091SDebojyoti Ghosh 
751834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
751934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7520d15a3a53SEmil Constantinescu 
752193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
752293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
752393b34091SDebojyoti Ghosh 
752493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
752551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
752693b34091SDebojyoti Ghosh 
7527e7069c78SShri   t->trajectory = tsin->trajectory;
7528e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7529e7069c78SShri 
7530e7069c78SShri   t->event = tsin->event;
75316b10a48eSSatish Balay   if (t->event) t->event->refct++;
7532e7069c78SShri 
753393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
753493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7535e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
753693b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
753793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
753893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
753993b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
754093b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
754193b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
754293b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
754393b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
754493b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
754593b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
754693b34091SDebojyoti Ghosh 
754793b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
754893b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
754993b34091SDebojyoti Ghosh 
755093b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
755193b34091SDebojyoti Ghosh 
755293b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
755393b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
755493b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
755593b34091SDebojyoti Ghosh 
755693b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
755793b34091SDebojyoti Ghosh 
75580d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75590d4fed19SBarry Smith     PetscInt i;
75600d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75610d4fed19SBarry Smith     for (i=0; i<10; i++) {
75620d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75630d4fed19SBarry Smith     }
75640d4fed19SBarry Smith   }
756593b34091SDebojyoti Ghosh   *tsout = t;
756693b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
756793b34091SDebojyoti Ghosh }
7568f3b1f45cSBarry Smith 
7569f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7570f3b1f45cSBarry Smith {
7571f3b1f45cSBarry Smith   PetscErrorCode ierr;
7572f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7573f3b1f45cSBarry Smith 
7574f3b1f45cSBarry Smith   PetscFunctionBegin;
7575f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7576f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7577f3b1f45cSBarry Smith }
7578f3b1f45cSBarry Smith 
7579f3b1f45cSBarry Smith /*@
7580f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7581f3b1f45cSBarry Smith 
7582d083f849SBarry Smith    Logically Collective on TS
7583f3b1f45cSBarry Smith 
7584f3b1f45cSBarry Smith     Input Parameters:
7585f3b1f45cSBarry Smith     TS - the time stepping routine
7586f3b1f45cSBarry Smith 
7587f3b1f45cSBarry Smith    Output Parameter:
7588f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7589f3b1f45cSBarry Smith 
7590f3b1f45cSBarry Smith    Options Database:
7591f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7592f3b1f45cSBarry Smith 
7593f3b1f45cSBarry Smith    Level: advanced
7594f3b1f45cSBarry Smith 
759595452b02SPatrick Sanan    Notes:
759695452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7597f3b1f45cSBarry Smith 
7598f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7599f3b1f45cSBarry Smith @*/
7600f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7601f3b1f45cSBarry Smith {
7602f3b1f45cSBarry Smith   Mat            J,B;
7603f3b1f45cSBarry Smith   PetscErrorCode ierr;
7604f3b1f45cSBarry Smith   TSRHSJacobian  func;
7605f3b1f45cSBarry Smith   void*          ctx;
7606f3b1f45cSBarry Smith 
7607f3b1f45cSBarry Smith   PetscFunctionBegin;
7608f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7609f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7610f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7611f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7612f3b1f45cSBarry Smith }
7613f3b1f45cSBarry Smith 
7614f3b1f45cSBarry Smith /*@C
7615f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7616f3b1f45cSBarry Smith 
7617d083f849SBarry Smith    Logically Collective on TS
7618f3b1f45cSBarry Smith 
7619f3b1f45cSBarry Smith     Input Parameters:
7620f3b1f45cSBarry Smith     TS - the time stepping routine
7621f3b1f45cSBarry Smith 
7622f3b1f45cSBarry Smith    Output Parameter:
7623f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7624f3b1f45cSBarry Smith 
7625f3b1f45cSBarry Smith    Options Database:
7626f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7627f3b1f45cSBarry Smith 
762895452b02SPatrick Sanan    Notes:
762995452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7630f3b1f45cSBarry Smith 
7631f3b1f45cSBarry Smith    Level: advanced
7632f3b1f45cSBarry Smith 
7633f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7634f3b1f45cSBarry Smith @*/
7635f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7636f3b1f45cSBarry Smith {
7637f3b1f45cSBarry Smith   Mat            J,B;
7638f3b1f45cSBarry Smith   PetscErrorCode ierr;
7639f3b1f45cSBarry Smith   void           *ctx;
7640f3b1f45cSBarry Smith   TSRHSJacobian  func;
7641f3b1f45cSBarry Smith 
7642f3b1f45cSBarry Smith   PetscFunctionBegin;
7643f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7644f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7645f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7646f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7647f3b1f45cSBarry Smith }
76480fe4d17eSHong Zhang 
76490fe4d17eSHong Zhang /*@
76500fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
76510fe4d17eSHong Zhang 
76520fe4d17eSHong Zhang   Logically collective
76530fe4d17eSHong Zhang 
76540fe4d17eSHong Zhang   Input Parameter:
76550fe4d17eSHong Zhang +  ts - timestepping context
76560fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76570fe4d17eSHong Zhang 
76580fe4d17eSHong Zhang   Options Database:
76590fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76600fe4d17eSHong Zhang 
76610fe4d17eSHong Zhang   Notes:
76620fe4d17eSHong Zhang     This is only useful for multirate methods
76630fe4d17eSHong Zhang 
76640fe4d17eSHong Zhang   Level: intermediate
76650fe4d17eSHong Zhang 
76660fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
76670fe4d17eSHong Zhang @*/
76680fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
76690fe4d17eSHong Zhang {
76700fe4d17eSHong Zhang   PetscFunctionBegin;
76710fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76720fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
76730fe4d17eSHong Zhang   PetscFunctionReturn(0);
76740fe4d17eSHong Zhang }
76750fe4d17eSHong Zhang 
76760fe4d17eSHong Zhang /*@
76770fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
76780fe4d17eSHong Zhang 
76790fe4d17eSHong Zhang   Not collective
76800fe4d17eSHong Zhang 
76810fe4d17eSHong Zhang   Input Parameter:
76820fe4d17eSHong Zhang .  ts - timestepping context
76830fe4d17eSHong Zhang 
76840fe4d17eSHong Zhang   Output Parameter:
76850fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76860fe4d17eSHong Zhang 
76870fe4d17eSHong Zhang   Level: intermediate
76880fe4d17eSHong Zhang 
76890fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
76900fe4d17eSHong Zhang @*/
76910fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
76920fe4d17eSHong Zhang {
76930fe4d17eSHong Zhang   PetscFunctionBegin;
76940fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76950fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
76960fe4d17eSHong Zhang   PetscFunctionReturn(0);
76970fe4d17eSHong Zhang }
7698