xref: /petsc/src/ts/interface/ts.c (revision 097a96a2fa22e45be4b68edf9cd0d7f7e6287615)
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 
58024989b8cSPeter Brune   if (rhsjacobianfunc) {
58194ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
582a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
583d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
584a7a1495cSBarry Smith     PetscStackPop;
58594ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
586ef66eb69SBarry Smith   } else {
58794ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
588971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
589ef66eb69SBarry Smith   }
5900e4ef248SJed Brown   ts->rhsjacobian.time  = t;
591971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
592971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
5937f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
59459e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
595a7a1495cSBarry Smith   PetscFunctionReturn(0);
596a7a1495cSBarry Smith }
597a7a1495cSBarry Smith 
598316643e7SJed Brown /*@
599d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
600d763cef2SBarry Smith 
601d083f849SBarry Smith    Collective on TS
602316643e7SJed Brown 
603316643e7SJed Brown    Input Parameters:
604316643e7SJed Brown +  ts - the TS context
605316643e7SJed Brown .  t - current time
6060910c330SBarry Smith -  U - state vector
607316643e7SJed Brown 
608316643e7SJed Brown    Output Parameter:
609316643e7SJed Brown .  y - right hand side
610316643e7SJed Brown 
611316643e7SJed Brown    Note:
612316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
613316643e7SJed Brown    is used internally within the nonlinear solvers.
614316643e7SJed Brown 
615316643e7SJed Brown    Level: developer
616316643e7SJed Brown 
617316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
618316643e7SJed Brown @*/
6190910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
620d763cef2SBarry Smith {
621dfbe8321SBarry Smith   PetscErrorCode ierr;
62224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
62324989b8cSPeter Brune   TSIFunction    ifunction;
62424989b8cSPeter Brune   void           *ctx;
62524989b8cSPeter Brune   DM             dm;
62624989b8cSPeter Brune 
627d763cef2SBarry Smith   PetscFunctionBegin;
6280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6290910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6300700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
63124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
63224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6330298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
634d763cef2SBarry Smith 
635ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
636d763cef2SBarry Smith 
6370910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
63824989b8cSPeter Brune   if (rhsfunction) {
6391be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
640d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6410910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
642d763cef2SBarry Smith     PetscStackPop;
6431be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
644214bc6a2SJed Brown   } else {
645214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
646b2cd27e8SJed Brown   }
64744a41b28SSean Farley 
6480910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
649d763cef2SBarry Smith   PetscFunctionReturn(0);
650d763cef2SBarry Smith }
651d763cef2SBarry Smith 
652ef20d060SBarry Smith /*@
653ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
654ef20d060SBarry Smith 
655d083f849SBarry Smith    Collective on TS
656ef20d060SBarry Smith 
657ef20d060SBarry Smith    Input Parameters:
658ef20d060SBarry Smith +  ts - the TS context
659ef20d060SBarry Smith -  t - current time
660ef20d060SBarry Smith 
661ef20d060SBarry Smith    Output Parameter:
6620910c330SBarry Smith .  U - the solution
663ef20d060SBarry Smith 
664ef20d060SBarry Smith    Note:
665ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
666ef20d060SBarry Smith    is used internally within the nonlinear solvers.
667ef20d060SBarry Smith 
668ef20d060SBarry Smith    Level: developer
669ef20d060SBarry Smith 
670abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
671ef20d060SBarry Smith @*/
6720910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
673ef20d060SBarry Smith {
674ef20d060SBarry Smith   PetscErrorCode     ierr;
675ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
676ef20d060SBarry Smith   void               *ctx;
677ef20d060SBarry Smith   DM                 dm;
678ef20d060SBarry Smith 
679ef20d060SBarry Smith   PetscFunctionBegin;
680ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6810910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
682ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
683ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
684ef20d060SBarry Smith 
685ef20d060SBarry Smith   if (solutionfunction) {
6869b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6870910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
688ef20d060SBarry Smith     PetscStackPop;
689ef20d060SBarry Smith   }
690ef20d060SBarry Smith   PetscFunctionReturn(0);
691ef20d060SBarry Smith }
6929b7cd975SBarry Smith /*@
6939b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6949b7cd975SBarry Smith 
695d083f849SBarry Smith    Collective on TS
6969b7cd975SBarry Smith 
6979b7cd975SBarry Smith    Input Parameters:
6989b7cd975SBarry Smith +  ts - the TS context
6999b7cd975SBarry Smith -  t - current time
7009b7cd975SBarry Smith 
7019b7cd975SBarry Smith    Output Parameter:
7029b7cd975SBarry Smith .  U - the function value
7039b7cd975SBarry Smith 
7049b7cd975SBarry Smith    Note:
7059b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7069b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7079b7cd975SBarry Smith 
7089b7cd975SBarry Smith    Level: developer
7099b7cd975SBarry Smith 
7109b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7119b7cd975SBarry Smith @*/
7129b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7139b7cd975SBarry Smith {
7149b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7159b7cd975SBarry Smith   void               *ctx;
7169b7cd975SBarry Smith   DM                 dm;
7179b7cd975SBarry Smith 
7189b7cd975SBarry Smith   PetscFunctionBegin;
7199b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7209b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7219b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7229b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7239b7cd975SBarry Smith 
7249b7cd975SBarry Smith   if (forcing) {
7259b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7269b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7279b7cd975SBarry Smith     PetscStackPop;
7289b7cd975SBarry Smith   }
7299b7cd975SBarry Smith   PetscFunctionReturn(0);
7309b7cd975SBarry Smith }
731ef20d060SBarry Smith 
732214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
733214bc6a2SJed Brown {
7342dd45cf8SJed Brown   Vec            F;
735214bc6a2SJed Brown   PetscErrorCode ierr;
736214bc6a2SJed Brown 
737214bc6a2SJed Brown   PetscFunctionBegin;
7380298fd71SBarry Smith   *Frhs = NULL;
7390298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
740214bc6a2SJed Brown   if (!ts->Frhs) {
7412dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
742214bc6a2SJed Brown   }
743214bc6a2SJed Brown   *Frhs = ts->Frhs;
744214bc6a2SJed Brown   PetscFunctionReturn(0);
745214bc6a2SJed Brown }
746214bc6a2SJed Brown 
747971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
748214bc6a2SJed Brown {
749214bc6a2SJed Brown   Mat            A,B;
7502dd45cf8SJed Brown   PetscErrorCode ierr;
75141a1d4d2SBarry Smith   TSIJacobian    ijacobian;
752214bc6a2SJed Brown 
753214bc6a2SJed Brown   PetscFunctionBegin;
754c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
755c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
75641a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
757214bc6a2SJed Brown   if (Arhs) {
758214bc6a2SJed Brown     if (!ts->Arhs) {
75941a1d4d2SBarry Smith       if (ijacobian) {
760214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
76141a1d4d2SBarry Smith       } else {
76241a1d4d2SBarry Smith         ts->Arhs = A;
76341a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
76441a1d4d2SBarry Smith       }
7653565c898SBarry Smith     } else {
7663565c898SBarry Smith       PetscBool flg;
7673565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7683565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7693565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7703565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7713565c898SBarry Smith         ts->Arhs = A;
7723565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7733565c898SBarry Smith       }
774214bc6a2SJed Brown     }
775214bc6a2SJed Brown     *Arhs = ts->Arhs;
776214bc6a2SJed Brown   }
777214bc6a2SJed Brown   if (Brhs) {
778214bc6a2SJed Brown     if (!ts->Brhs) {
779bdb70873SJed Brown       if (A != B) {
78041a1d4d2SBarry Smith         if (ijacobian) {
781214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
782bdb70873SJed Brown         } else {
78341a1d4d2SBarry Smith           ts->Brhs = B;
78441a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
78541a1d4d2SBarry Smith         }
78641a1d4d2SBarry Smith       } else {
787bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
78851699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
789bdb70873SJed Brown       }
790214bc6a2SJed Brown     }
791214bc6a2SJed Brown     *Brhs = ts->Brhs;
792214bc6a2SJed Brown   }
793214bc6a2SJed Brown   PetscFunctionReturn(0);
794214bc6a2SJed Brown }
795214bc6a2SJed Brown 
796316643e7SJed Brown /*@
7970910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
798316643e7SJed Brown 
799d083f849SBarry Smith    Collective on TS
800316643e7SJed Brown 
801316643e7SJed Brown    Input Parameters:
802316643e7SJed Brown +  ts - the TS context
803316643e7SJed Brown .  t - current time
8040910c330SBarry Smith .  U - state vector
8050910c330SBarry Smith .  Udot - time derivative of state vector
806214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
807316643e7SJed Brown 
808316643e7SJed Brown    Output Parameter:
809316643e7SJed Brown .  Y - right hand side
810316643e7SJed Brown 
811316643e7SJed Brown    Note:
812316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
813316643e7SJed Brown    is used internally within the nonlinear solvers.
814316643e7SJed Brown 
815316643e7SJed Brown    If the user did did not write their equations in implicit form, this
816316643e7SJed Brown    function recasts them in implicit form.
817316643e7SJed Brown 
818316643e7SJed Brown    Level: developer
819316643e7SJed Brown 
820316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
821316643e7SJed Brown @*/
8220910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
823316643e7SJed Brown {
824316643e7SJed Brown   PetscErrorCode ierr;
82524989b8cSPeter Brune   TSIFunction    ifunction;
82624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
82724989b8cSPeter Brune   void           *ctx;
82824989b8cSPeter Brune   DM             dm;
829316643e7SJed Brown 
830316643e7SJed Brown   PetscFunctionBegin;
8310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8320910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8330910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8340700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
835316643e7SJed Brown 
83624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
83724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8380298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
83924989b8cSPeter Brune 
840ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
841d90be118SSean Farley 
8420910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
84324989b8cSPeter Brune   if (ifunction) {
844316643e7SJed Brown     PetscStackPush("TS user implicit function");
8450910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
846316643e7SJed Brown     PetscStackPop;
847214bc6a2SJed Brown   }
848214bc6a2SJed Brown   if (imex) {
84924989b8cSPeter Brune     if (!ifunction) {
8500910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8512dd45cf8SJed Brown     }
85224989b8cSPeter Brune   } else if (rhsfunction) {
85324989b8cSPeter Brune     if (ifunction) {
854214bc6a2SJed Brown       Vec Frhs;
855214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8560910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
857214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8582dd45cf8SJed Brown     } else {
8590910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8600910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
861316643e7SJed Brown     }
8624a6899ffSJed Brown   }
8630910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
864316643e7SJed Brown   PetscFunctionReturn(0);
865316643e7SJed Brown }
866316643e7SJed Brown 
867316643e7SJed Brown /*@
868316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
869316643e7SJed Brown 
870d083f849SBarry Smith    Collective on TS
871316643e7SJed Brown 
872316643e7SJed Brown    Input
873316643e7SJed Brown       Input Parameters:
874316643e7SJed Brown +  ts - the TS context
875316643e7SJed Brown .  t - current timestep
8760910c330SBarry Smith .  U - state vector
8770910c330SBarry Smith .  Udot - time derivative of state vector
878214bc6a2SJed Brown .  shift - shift to apply, see note below
879214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
880316643e7SJed Brown 
881316643e7SJed Brown    Output Parameters:
882316643e7SJed Brown +  A - Jacobian matrix
8833565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
884316643e7SJed Brown 
885316643e7SJed Brown    Notes:
8860910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
887316643e7SJed Brown 
8880910c330SBarry Smith    dF/dU + shift*dF/dUdot
889316643e7SJed Brown 
890316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
891316643e7SJed Brown    is used internally within the nonlinear solvers.
892316643e7SJed Brown 
893316643e7SJed Brown    Level: developer
894316643e7SJed Brown 
895316643e7SJed Brown .seealso:  TSSetIJacobian()
89663495f91SJed Brown @*/
897d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
898316643e7SJed Brown {
899316643e7SJed Brown   PetscErrorCode ierr;
90024989b8cSPeter Brune   TSIJacobian    ijacobian;
90124989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
90224989b8cSPeter Brune   DM             dm;
90324989b8cSPeter Brune   void           *ctx;
904316643e7SJed Brown 
905316643e7SJed Brown   PetscFunctionBegin;
9060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9070910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9080910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
909316643e7SJed Brown   PetscValidPointer(A,6);
91094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
911316643e7SJed Brown   PetscValidPointer(B,7);
91294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
91324989b8cSPeter Brune 
91424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9160298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
91724989b8cSPeter Brune 
918ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
919316643e7SJed Brown 
92094ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
92124989b8cSPeter Brune   if (ijacobian) {
922316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
923d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
924316643e7SJed Brown     PetscStackPop;
9254a6899ffSJed Brown   }
926214bc6a2SJed Brown   if (imex) {
927b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9284c26be97Sstefano_zampini       PetscBool assembled;
929971015bcSStefano Zampini       if (rhsjacobian) {
930971015bcSStefano Zampini         Mat Arhs = NULL;
931971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
932971015bcSStefano Zampini         if (A == Arhs) {
933e8b1e424SHong 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 */
934971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
935971015bcSStefano Zampini         }
936971015bcSStefano Zampini       }
93794ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9384c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9394c26be97Sstefano_zampini       if (!assembled) {
9404c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9414c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9424c26be97Sstefano_zampini       }
94394ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
94494ab13aaSBarry Smith       if (A != B) {
94594ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9464c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9474c26be97Sstefano_zampini         if (!assembled) {
9484c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9494c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9504c26be97Sstefano_zampini         }
95194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
952214bc6a2SJed Brown       }
953214bc6a2SJed Brown     }
954214bc6a2SJed Brown   } else {
955e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
956e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
957214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
958e1244c69SJed Brown     }
959e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
960e8b1e424SHong Zhang       PetscObjectState Ustate;
961e8b1e424SHong Zhang       PetscObjectId    Uid;
962e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
963e8b1e424SHong Zhang 
964e8b1e424SHong Zhang       ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
965e8b1e424SHong Zhang       ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
966e8b1e424SHong Zhang       ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
967*097a96a2SHong 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 */
968e8b1e424SHong 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 */
969e8b1e424SHong Zhang       } else {
9703565c898SBarry Smith         PetscBool flg;
971e8b1e424SHong Zhang 
972e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
973e8b1e424SHong Zhang           /* MatScale has a short path for this case.
974e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
975e8b1e424SHong Zhang              and the matrices have not been assembled yet */
976e8b1e424SHong Zhang           if (ts->rhsjacobian.shift) {
977e8b1e424SHong Zhang             ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
978e8b1e424SHong Zhang           }
979e8b1e424SHong Zhang           if (ts->rhsjacobian.scale == -1.) {
980e8b1e424SHong Zhang             ierr = MatScale(A,-1);CHKERRQ(ierr);
981e8b1e424SHong Zhang           }
982e8b1e424SHong Zhang           if (B && B == ts->Brhs && A != B) {
983e8b1e424SHong Zhang             if (ts->rhsjacobian.shift) {
984e8b1e424SHong Zhang               ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
985e8b1e424SHong Zhang             }
986e8b1e424SHong Zhang             if (ts->rhsjacobian.scale == -1.) {
987e8b1e424SHong Zhang               ierr = MatScale(B,-1);CHKERRQ(ierr);
988e8b1e424SHong Zhang             }
989e8b1e424SHong Zhang           }
990e8b1e424SHong Zhang           ts->rhsjacobian.shift = 0;
991e8b1e424SHong Zhang           ts->rhsjacobian.scale = 1.;
992e8b1e424SHong Zhang         }
993e8b1e424SHong Zhang         ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr);
9943565c898SBarry Smith         ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
9953565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
9963565c898SBarry Smith         if (!flg) {
99794ab13aaSBarry Smith           ierr = MatScale(A,-1);CHKERRQ(ierr);
99894ab13aaSBarry Smith           ierr = MatShift(A,shift);CHKERRQ(ierr);
9993565c898SBarry Smith         }
100094ab13aaSBarry Smith         if (A != B) {
100194ab13aaSBarry Smith           ierr = MatScale(B,-1);CHKERRQ(ierr);
100294ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1003316643e7SJed Brown         }
1004e8b1e424SHong Zhang       }
1005e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1006e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1007e8b1e424SHong Zhang     } else if (Arhs) {  /* Both IJacobian and RHSJacobian exist or the RHS matrix provided (A) is different from the internal RHS matrix (Arhs) */
1008e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1009e8b1e424SHong Zhang 
1010e1244c69SJed Brown       if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */
101194ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
101294ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
101394ab13aaSBarry Smith         if (A != B) {
101494ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
101594ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1016214bc6a2SJed Brown         }
1017316643e7SJed Brown       }
1018e8b1e424SHong Zhang       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
101994ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
102094ab13aaSBarry Smith       if (A != B) {
102194ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1022316643e7SJed Brown       }
1023316643e7SJed Brown     }
1024316643e7SJed Brown   }
102594ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1026316643e7SJed Brown   PetscFunctionReturn(0);
1027316643e7SJed Brown }
1028316643e7SJed Brown 
1029d763cef2SBarry Smith /*@C
1030d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1031b5abc632SBarry Smith     where U_t = G(t,u).
1032d763cef2SBarry Smith 
10333f9fe445SBarry Smith     Logically Collective on TS
1034d763cef2SBarry Smith 
1035d763cef2SBarry Smith     Input Parameters:
1036d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10370298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1038d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1039d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10400298fd71SBarry Smith           function evaluation routine (may be NULL)
1041d763cef2SBarry Smith 
1042a96d6ef6SBarry Smith     Calling sequence of f:
1043a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1044d763cef2SBarry Smith 
1045a96d6ef6SBarry Smith +   ts - timestep context
1046a96d6ef6SBarry Smith .   t - current timestep
1047d763cef2SBarry Smith .   u - input vector
1048d763cef2SBarry Smith .   F - function vector
1049d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1050d763cef2SBarry Smith 
1051d763cef2SBarry Smith     Level: beginner
1052d763cef2SBarry Smith 
105395452b02SPatrick Sanan     Notes:
105495452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10552bbac0d3SBarry Smith 
1056ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1057d763cef2SBarry Smith @*/
1058089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1059d763cef2SBarry Smith {
1060089b2837SJed Brown   PetscErrorCode ierr;
1061089b2837SJed Brown   SNES           snes;
10620298fd71SBarry Smith   Vec            ralloc = NULL;
106324989b8cSPeter Brune   DM             dm;
1064d763cef2SBarry Smith 
1065089b2837SJed Brown   PetscFunctionBegin;
10660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1067ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
106824989b8cSPeter Brune 
106924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
107024989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1071089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1072e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1073e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1074e856ceecSJed Brown     r = ralloc;
1075e856ceecSJed Brown   }
1076089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1077e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1078d763cef2SBarry Smith   PetscFunctionReturn(0);
1079d763cef2SBarry Smith }
1080d763cef2SBarry Smith 
1081ef20d060SBarry Smith /*@C
1082abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1083ef20d060SBarry Smith 
1084ef20d060SBarry Smith     Logically Collective on TS
1085ef20d060SBarry Smith 
1086ef20d060SBarry Smith     Input Parameters:
1087ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1088ef20d060SBarry Smith .   f - routine for evaluating the solution
1089ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10900298fd71SBarry Smith           function evaluation routine (may be NULL)
1091ef20d060SBarry Smith 
1092a96d6ef6SBarry Smith     Calling sequence of f:
1093a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1094ef20d060SBarry Smith 
1095ef20d060SBarry Smith +   t - current timestep
1096ef20d060SBarry Smith .   u - output vector
1097ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1098ef20d060SBarry Smith 
10990ed3bfb6SBarry Smith     Options Database:
11000ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11010ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11020ed3bfb6SBarry Smith 
1103abd5a294SJed Brown     Notes:
1104abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1105abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1106abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1107abd5a294SJed Brown 
11084f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1109abd5a294SJed Brown 
1110ef20d060SBarry Smith     Level: beginner
1111ef20d060SBarry Smith 
11120ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1113ef20d060SBarry Smith @*/
1114ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1115ef20d060SBarry Smith {
1116ef20d060SBarry Smith   PetscErrorCode ierr;
1117ef20d060SBarry Smith   DM             dm;
1118ef20d060SBarry Smith 
1119ef20d060SBarry Smith   PetscFunctionBegin;
1120ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1121ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1122ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1123ef20d060SBarry Smith   PetscFunctionReturn(0);
1124ef20d060SBarry Smith }
1125ef20d060SBarry Smith 
11269b7cd975SBarry Smith /*@C
11279b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11289b7cd975SBarry Smith 
11299b7cd975SBarry Smith     Logically Collective on TS
11309b7cd975SBarry Smith 
11319b7cd975SBarry Smith     Input Parameters:
11329b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1133e162b725SBarry Smith .   func - routine for evaluating the forcing function
11349b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11350298fd71SBarry Smith           function evaluation routine (may be NULL)
11369b7cd975SBarry Smith 
11379b7cd975SBarry Smith     Calling sequence of func:
11386bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11399b7cd975SBarry Smith 
11409b7cd975SBarry Smith +   t - current timestep
1141e162b725SBarry Smith .   f - output vector
11429b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11439b7cd975SBarry Smith 
11449b7cd975SBarry Smith     Notes:
11459b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1146e162b725SBarry 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
1147e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1148e162b725SBarry Smith 
1149e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1150e162b725SBarry Smith 
1151e162b725SBarry 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
1152e162b725SBarry Smith     parameters can be passed in the ctx variable.
11539b7cd975SBarry Smith 
11549b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11559b7cd975SBarry Smith 
11569b7cd975SBarry Smith     Level: beginner
11579b7cd975SBarry Smith 
11589b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11599b7cd975SBarry Smith @*/
1160e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11619b7cd975SBarry Smith {
11629b7cd975SBarry Smith   PetscErrorCode ierr;
11639b7cd975SBarry Smith   DM             dm;
11649b7cd975SBarry Smith 
11659b7cd975SBarry Smith   PetscFunctionBegin;
11669b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11679b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1168e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11699b7cd975SBarry Smith   PetscFunctionReturn(0);
11709b7cd975SBarry Smith }
11719b7cd975SBarry Smith 
1172d763cef2SBarry Smith /*@C
1173f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1174b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1175d763cef2SBarry Smith 
11763f9fe445SBarry Smith    Logically Collective on TS
1177d763cef2SBarry Smith 
1178d763cef2SBarry Smith    Input Parameters:
1179d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1180e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1181e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1182d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1183d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11840298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1185d763cef2SBarry Smith 
1186f7ab8db6SBarry Smith    Calling sequence of f:
1187a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1188d763cef2SBarry Smith 
1189d763cef2SBarry Smith +  t - current timestep
1190d763cef2SBarry Smith .  u - input vector
1191e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1192e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1193d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1194d763cef2SBarry Smith 
11956cd88445SBarry Smith    Notes:
11966cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11976cd88445SBarry Smith 
11986cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1199ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1200d763cef2SBarry Smith 
1201d763cef2SBarry Smith    Level: beginner
1202d763cef2SBarry Smith 
1203ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1204d763cef2SBarry Smith 
1205d763cef2SBarry Smith @*/
1206e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1207d763cef2SBarry Smith {
1208277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1209089b2837SJed Brown   SNES           snes;
121024989b8cSPeter Brune   DM             dm;
121124989b8cSPeter Brune   TSIJacobian    ijacobian;
1212277b19d0SLisandro Dalcin 
1213d763cef2SBarry Smith   PetscFunctionBegin;
12140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1215e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1216e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1217e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1218e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1219d763cef2SBarry Smith 
122024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122124989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
12220298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1223089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12245f659677SPeter Brune   if (!ijacobian) {
1225e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12260e4ef248SJed Brown   }
1227e5d3d808SBarry Smith   if (Amat) {
1228e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12290e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1230e5d3d808SBarry Smith     ts->Arhs = Amat;
12310e4ef248SJed Brown   }
1232e5d3d808SBarry Smith   if (Pmat) {
1233e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12340e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1235e5d3d808SBarry Smith     ts->Brhs = Pmat;
12360e4ef248SJed Brown   }
1237d763cef2SBarry Smith   PetscFunctionReturn(0);
1238d763cef2SBarry Smith }
1239d763cef2SBarry Smith 
1240316643e7SJed Brown /*@C
1241b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1242316643e7SJed Brown 
12433f9fe445SBarry Smith    Logically Collective on TS
1244316643e7SJed Brown 
1245316643e7SJed Brown    Input Parameters:
1246316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12470298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1248316643e7SJed Brown .  f   - the function evaluation routine
12490298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1250316643e7SJed Brown 
1251316643e7SJed Brown    Calling sequence of f:
12526bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1253316643e7SJed Brown 
1254316643e7SJed Brown +  t   - time at step/stage being solved
1255316643e7SJed Brown .  u   - state vector
1256316643e7SJed Brown .  u_t - time derivative of state vector
1257316643e7SJed Brown .  F   - function vector
1258316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1259316643e7SJed Brown 
1260316643e7SJed Brown    Important:
12612bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1262316643e7SJed Brown 
1263316643e7SJed Brown    Level: beginner
1264316643e7SJed Brown 
1265d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1266316643e7SJed Brown @*/
126751699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1268316643e7SJed Brown {
1269089b2837SJed Brown   PetscErrorCode ierr;
1270089b2837SJed Brown   SNES           snes;
127151699248SLisandro Dalcin   Vec            ralloc = NULL;
127224989b8cSPeter Brune   DM             dm;
1273316643e7SJed Brown 
1274316643e7SJed Brown   PetscFunctionBegin;
12750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
127651699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
127724989b8cSPeter Brune 
127824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
127924989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
128024989b8cSPeter Brune 
1281089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
128251699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
128351699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
128451699248SLisandro Dalcin     r  = ralloc;
1285e856ceecSJed Brown   }
128651699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
128751699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1288089b2837SJed Brown   PetscFunctionReturn(0);
1289089b2837SJed Brown }
1290089b2837SJed Brown 
1291089b2837SJed Brown /*@C
1292089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1293089b2837SJed Brown 
1294089b2837SJed Brown    Not Collective
1295089b2837SJed Brown 
1296089b2837SJed Brown    Input Parameter:
1297089b2837SJed Brown .  ts - the TS context
1298089b2837SJed Brown 
1299089b2837SJed Brown    Output Parameter:
13000298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13010298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13020298fd71SBarry Smith -  ctx - the function context (or NULL)
1303089b2837SJed Brown 
1304089b2837SJed Brown    Level: advanced
1305089b2837SJed Brown 
1306089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1307089b2837SJed Brown @*/
1308089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1309089b2837SJed Brown {
1310089b2837SJed Brown   PetscErrorCode ierr;
1311089b2837SJed Brown   SNES           snes;
131224989b8cSPeter Brune   DM             dm;
1313089b2837SJed Brown 
1314089b2837SJed Brown   PetscFunctionBegin;
1315089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1316089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13170298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
131824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
131924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1320089b2837SJed Brown   PetscFunctionReturn(0);
1321089b2837SJed Brown }
1322089b2837SJed Brown 
1323089b2837SJed Brown /*@C
1324089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1325089b2837SJed Brown 
1326089b2837SJed Brown    Not Collective
1327089b2837SJed Brown 
1328089b2837SJed Brown    Input Parameter:
1329089b2837SJed Brown .  ts - the TS context
1330089b2837SJed Brown 
1331089b2837SJed Brown    Output Parameter:
13320298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13330298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13340298fd71SBarry Smith -  ctx - the function context (or NULL)
1335089b2837SJed Brown 
1336089b2837SJed Brown    Level: advanced
1337089b2837SJed Brown 
13382bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1339089b2837SJed Brown @*/
1340089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1341089b2837SJed Brown {
1342089b2837SJed Brown   PetscErrorCode ierr;
1343089b2837SJed Brown   SNES           snes;
134424989b8cSPeter Brune   DM             dm;
1345089b2837SJed Brown 
1346089b2837SJed Brown   PetscFunctionBegin;
1347089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1348089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13490298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
135024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
135124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1352316643e7SJed Brown   PetscFunctionReturn(0);
1353316643e7SJed Brown }
1354316643e7SJed Brown 
1355316643e7SJed Brown /*@C
1356a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1357ae8867d6SBarry Smith         provided with TSSetIFunction().
1358316643e7SJed Brown 
13593f9fe445SBarry Smith    Logically Collective on TS
1360316643e7SJed Brown 
1361316643e7SJed Brown    Input Parameters:
1362316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1363e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1364e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1365316643e7SJed Brown .  f   - the Jacobian evaluation routine
13660298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1367316643e7SJed Brown 
1368316643e7SJed Brown    Calling sequence of f:
13696bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1370316643e7SJed Brown 
1371316643e7SJed Brown +  t    - time at step/stage being solved
13721b4a444bSJed Brown .  U    - state vector
13731b4a444bSJed Brown .  U_t  - time derivative of state vector
1374316643e7SJed Brown .  a    - shift
1375e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1376e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1377316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1378316643e7SJed Brown 
1379316643e7SJed Brown    Notes:
1380e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1381316643e7SJed Brown 
1382895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1383895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1384895c21f2SBarry Smith 
1385a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1386b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1387a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1388a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1389a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1390a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1391a4f0a591SBarry Smith 
13926cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13936cd88445SBarry Smith 
13946cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1395ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1396ca5f011dSBarry Smith 
1397316643e7SJed Brown    Level: beginner
1398316643e7SJed Brown 
1399ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1400316643e7SJed Brown 
1401316643e7SJed Brown @*/
1402e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1403316643e7SJed Brown {
1404316643e7SJed Brown   PetscErrorCode ierr;
1405089b2837SJed Brown   SNES           snes;
140624989b8cSPeter Brune   DM             dm;
1407316643e7SJed Brown 
1408316643e7SJed Brown   PetscFunctionBegin;
14090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1410e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1411e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1412e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1413e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
141424989b8cSPeter Brune 
141524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
141624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
141724989b8cSPeter Brune 
1418089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1419e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1420316643e7SJed Brown   PetscFunctionReturn(0);
1421316643e7SJed Brown }
1422316643e7SJed Brown 
1423e1244c69SJed Brown /*@
1424e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1425e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1426e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1427e1244c69SJed Brown    not been changed by the TS.
1428e1244c69SJed Brown 
1429e1244c69SJed Brown    Logically Collective
1430e1244c69SJed Brown 
1431e1244c69SJed Brown    Input Arguments:
1432e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1433e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1434e1244c69SJed Brown 
1435e1244c69SJed Brown    Level: intermediate
1436e1244c69SJed Brown 
1437e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1438e1244c69SJed Brown @*/
1439e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1440e1244c69SJed Brown {
1441e1244c69SJed Brown   PetscFunctionBegin;
1442e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1443e1244c69SJed Brown   PetscFunctionReturn(0);
1444e1244c69SJed Brown }
1445e1244c69SJed Brown 
1446efe9872eSLisandro Dalcin /*@C
1447efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1448efe9872eSLisandro Dalcin 
1449efe9872eSLisandro Dalcin    Logically Collective on TS
1450efe9872eSLisandro Dalcin 
1451efe9872eSLisandro Dalcin    Input Parameters:
1452efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1453efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1454efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1455efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1456efe9872eSLisandro Dalcin 
1457efe9872eSLisandro Dalcin    Calling sequence of fun:
14586bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1459efe9872eSLisandro Dalcin 
1460efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1461efe9872eSLisandro Dalcin .  U    - state vector
1462efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1463efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1464efe9872eSLisandro Dalcin .  F    - function vector
1465efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1466efe9872eSLisandro Dalcin 
1467efe9872eSLisandro Dalcin    Level: beginner
1468efe9872eSLisandro Dalcin 
1469a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1470efe9872eSLisandro Dalcin @*/
1471efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1472efe9872eSLisandro Dalcin {
1473efe9872eSLisandro Dalcin   DM             dm;
1474efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1475efe9872eSLisandro Dalcin 
1476efe9872eSLisandro Dalcin   PetscFunctionBegin;
1477efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1478efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1479efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1480efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1481efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1482efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1483efe9872eSLisandro Dalcin }
1484efe9872eSLisandro Dalcin 
1485efe9872eSLisandro Dalcin /*@C
1486efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1487efe9872eSLisandro Dalcin 
1488efe9872eSLisandro Dalcin   Not Collective
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin   Input Parameter:
1491efe9872eSLisandro Dalcin . ts - the TS context
1492efe9872eSLisandro Dalcin 
1493efe9872eSLisandro Dalcin   Output Parameter:
1494efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1495efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1496efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1497efe9872eSLisandro Dalcin 
1498efe9872eSLisandro Dalcin   Level: advanced
1499efe9872eSLisandro Dalcin 
1500a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1501efe9872eSLisandro Dalcin @*/
1502efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1503efe9872eSLisandro Dalcin {
1504efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1505efe9872eSLisandro Dalcin   SNES           snes;
1506efe9872eSLisandro Dalcin   DM             dm;
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin   PetscFunctionBegin;
1509efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1510efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1511efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1512efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1513efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1514efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1515efe9872eSLisandro Dalcin }
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin /*@C
1518bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1519efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin    Logically Collective on TS
1522efe9872eSLisandro Dalcin 
1523efe9872eSLisandro Dalcin    Input Parameters:
1524efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1525efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1526efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1527efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1528efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin    Calling sequence of jac:
15316bc98fa9SBarry Smith $    PetscErrorCode jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1532efe9872eSLisandro Dalcin 
1533efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1534efe9872eSLisandro Dalcin .  U    - state vector
1535efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1536efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1537efe9872eSLisandro Dalcin .  v    - shift for U_t
1538efe9872eSLisandro Dalcin .  a    - shift for U_tt
1539efe9872eSLisandro Dalcin .  J    - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t  + a*dF/dU_tt
1540efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1541efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1542efe9872eSLisandro Dalcin 
1543efe9872eSLisandro Dalcin    Notes:
1544efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1545efe9872eSLisandro Dalcin 
1546efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1547efe9872eSLisandro Dalcin    the Jacobian of G(U) = F(t,U,W+v*U,W'+a*U) where F(t,U,U_t,U_tt) = 0 is the DAE to be solved.
1548efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1549bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin    Level: beginner
1552efe9872eSLisandro Dalcin 
1553a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1554efe9872eSLisandro Dalcin @*/
1555efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1556efe9872eSLisandro Dalcin {
1557efe9872eSLisandro Dalcin   DM             dm;
1558efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1559efe9872eSLisandro Dalcin 
1560efe9872eSLisandro Dalcin   PetscFunctionBegin;
1561efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1562efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1563efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1564efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1565efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1566efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1567efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1568efe9872eSLisandro Dalcin }
1569efe9872eSLisandro Dalcin 
1570efe9872eSLisandro Dalcin /*@C
1571efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1572efe9872eSLisandro Dalcin 
1573efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1574efe9872eSLisandro Dalcin 
1575efe9872eSLisandro Dalcin   Input Parameter:
1576efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1577efe9872eSLisandro Dalcin 
1578efe9872eSLisandro Dalcin   Output Parameters:
1579efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1580efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1581efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1582efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1583efe9872eSLisandro Dalcin 
158495452b02SPatrick Sanan   Notes:
158595452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin   Level: advanced
1588efe9872eSLisandro Dalcin 
1589a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1590efe9872eSLisandro Dalcin 
1591efe9872eSLisandro Dalcin @*/
1592efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1593efe9872eSLisandro Dalcin {
1594efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1595efe9872eSLisandro Dalcin   SNES           snes;
1596efe9872eSLisandro Dalcin   DM             dm;
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin   PetscFunctionBegin;
1599efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1600efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1601efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1602efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1603efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1604efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1605efe9872eSLisandro Dalcin }
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin /*@
1608efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1609efe9872eSLisandro Dalcin 
1610d083f849SBarry Smith   Collective on TS
1611efe9872eSLisandro Dalcin 
1612efe9872eSLisandro Dalcin   Input Parameters:
1613efe9872eSLisandro Dalcin + ts - the TS context
1614efe9872eSLisandro Dalcin . t - current time
1615efe9872eSLisandro Dalcin . U - state vector
1616efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1617efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1618efe9872eSLisandro Dalcin 
1619efe9872eSLisandro Dalcin   Output Parameter:
1620efe9872eSLisandro Dalcin . F - the residual vector
1621efe9872eSLisandro Dalcin 
1622efe9872eSLisandro Dalcin   Note:
1623efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1624efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1625efe9872eSLisandro Dalcin 
1626efe9872eSLisandro Dalcin   Level: developer
1627efe9872eSLisandro Dalcin 
1628a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1629efe9872eSLisandro Dalcin @*/
1630efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1631efe9872eSLisandro Dalcin {
1632efe9872eSLisandro Dalcin   DM             dm;
1633efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1634efe9872eSLisandro Dalcin   void           *ctx;
1635efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1636efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1637efe9872eSLisandro Dalcin 
1638efe9872eSLisandro Dalcin   PetscFunctionBegin;
1639efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1640efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1641efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1642efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1643efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1644efe9872eSLisandro Dalcin 
1645efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1646efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1647efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin   if (!I2Function) {
1650efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1651efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1652efe9872eSLisandro Dalcin   }
1653efe9872eSLisandro Dalcin 
1654efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1655efe9872eSLisandro Dalcin 
1656efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1657efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1658efe9872eSLisandro Dalcin   PetscStackPop;
1659efe9872eSLisandro Dalcin 
1660efe9872eSLisandro Dalcin   if (rhsfunction) {
1661efe9872eSLisandro Dalcin     Vec Frhs;
1662efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1663efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1664efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1665efe9872eSLisandro Dalcin   }
1666efe9872eSLisandro Dalcin 
1667efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1668efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1669efe9872eSLisandro Dalcin }
1670efe9872eSLisandro Dalcin 
1671efe9872eSLisandro Dalcin /*@
1672efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1673efe9872eSLisandro Dalcin 
1674d083f849SBarry Smith   Collective on TS
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   Input Parameters:
1677efe9872eSLisandro Dalcin + ts - the TS context
1678efe9872eSLisandro Dalcin . t - current timestep
1679efe9872eSLisandro Dalcin . U - state vector
1680efe9872eSLisandro Dalcin . V - time derivative of state vector
1681efe9872eSLisandro Dalcin . A - second time derivative of state vector
1682efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1683efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1684efe9872eSLisandro Dalcin 
1685efe9872eSLisandro Dalcin   Output Parameters:
1686efe9872eSLisandro Dalcin + J - Jacobian matrix
1687efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1688efe9872eSLisandro Dalcin 
1689efe9872eSLisandro Dalcin   Notes:
1690efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1691efe9872eSLisandro Dalcin 
1692efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1695efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1696efe9872eSLisandro Dalcin 
1697efe9872eSLisandro Dalcin   Level: developer
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1700efe9872eSLisandro Dalcin @*/
1701efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1702efe9872eSLisandro Dalcin {
1703efe9872eSLisandro Dalcin   DM             dm;
1704efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1705efe9872eSLisandro Dalcin   void           *ctx;
1706efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1707efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin   PetscFunctionBegin;
1710efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1711efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1712efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1713efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1714efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1715efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1716efe9872eSLisandro Dalcin 
1717efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1718efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1719efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1720efe9872eSLisandro Dalcin 
1721efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1722efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1723efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1724efe9872eSLisandro Dalcin   }
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1727efe9872eSLisandro Dalcin 
1728efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1729efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1730efe9872eSLisandro Dalcin   PetscStackPop;
1731efe9872eSLisandro Dalcin 
1732efe9872eSLisandro Dalcin   if (rhsjacobian) {
1733efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1734efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1735efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1736efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1737efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1738efe9872eSLisandro Dalcin   }
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1741efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1742efe9872eSLisandro Dalcin }
1743efe9872eSLisandro Dalcin 
1744438f35afSJed Brown /*@C
1745438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1746438f35afSJed Brown 
1747438f35afSJed Brown    Logically Collective
1748438f35afSJed Brown 
1749438f35afSJed Brown    Input Arguments:
1750438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1751a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1752438f35afSJed Brown -  ctx - a context for tvar
1753438f35afSJed Brown 
1754a96d6ef6SBarry Smith     Calling sequence of tvar:
1755a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1756a96d6ef6SBarry Smith 
1757a96d6ef6SBarry Smith +   ts - timestep context
1758a96d6ef6SBarry Smith .   p - input vector (primative form)
1759a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1760a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1761a96d6ef6SBarry Smith 
1762438f35afSJed Brown    Level: advanced
1763438f35afSJed Brown 
1764438f35afSJed Brown    Notes:
1765438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1766438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1767438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1768438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1769438f35afSJed Brown    evaluated via the chain rule, as in
1770438f35afSJed Brown 
1771438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1772438f35afSJed Brown 
1773438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1774438f35afSJed Brown @*/
1775438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1776438f35afSJed Brown {
1777438f35afSJed Brown   PetscErrorCode ierr;
1778438f35afSJed Brown   DM             dm;
1779438f35afSJed Brown 
1780438f35afSJed Brown   PetscFunctionBegin;
1781438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1782438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1783438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1784438f35afSJed Brown   PetscFunctionReturn(0);
1785438f35afSJed Brown }
1786438f35afSJed Brown 
1787efe9872eSLisandro Dalcin /*@
1788e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1789e3c11fc1SJed Brown 
1790e3c11fc1SJed Brown    Logically Collective
1791e3c11fc1SJed Brown 
1792e3c11fc1SJed Brown    Input Parameters:
1793e3c11fc1SJed Brown +  ts - TS on which to compute
1794e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1795e3c11fc1SJed Brown 
1796e3c11fc1SJed Brown    Output Parameters:
1797e3c11fc1SJed Brown .  C - transient (conservative) variable
1798e3c11fc1SJed Brown 
1799e3c11fc1SJed Brown    Developer Notes:
1800e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1801e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1802e3c11fc1SJed Brown    being used.
1803e3c11fc1SJed Brown 
1804e3c11fc1SJed Brown    Level: developer
1805e3c11fc1SJed Brown 
1806e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1807e3c11fc1SJed Brown @*/
1808e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1809e3c11fc1SJed Brown {
1810e3c11fc1SJed Brown   PetscErrorCode ierr;
1811e3c11fc1SJed Brown   DM             dm;
1812e3c11fc1SJed Brown   DMTS           dmts;
1813e3c11fc1SJed Brown 
1814e3c11fc1SJed Brown   PetscFunctionBegin;
1815e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1816e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1817e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1818e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1819e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1820e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1821e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1822e3c11fc1SJed Brown   }
1823e3c11fc1SJed Brown   PetscFunctionReturn(0);
1824e3c11fc1SJed Brown }
1825e3c11fc1SJed Brown 
1826e3c11fc1SJed Brown /*@
1827e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1828e3c11fc1SJed Brown 
1829e3c11fc1SJed Brown    Logically Collective
1830e3c11fc1SJed Brown 
1831e3c11fc1SJed Brown    Input Parameters:
1832e3c11fc1SJed Brown .  ts - TS on which to compute
1833e3c11fc1SJed Brown 
1834e3c11fc1SJed Brown    Output Parameters:
1835e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1836e3c11fc1SJed Brown 
1837e3c11fc1SJed Brown    Level: developer
1838e3c11fc1SJed Brown 
1839e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1840e3c11fc1SJed Brown @*/
1841e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1842e3c11fc1SJed Brown {
1843e3c11fc1SJed Brown   PetscErrorCode ierr;
1844e3c11fc1SJed Brown   DM             dm;
1845e3c11fc1SJed Brown   DMTS           dmts;
1846e3c11fc1SJed Brown 
1847e3c11fc1SJed Brown   PetscFunctionBegin;
1848e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1849e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1850e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1851e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1852e3c11fc1SJed Brown   PetscFunctionReturn(0);
1853e3c11fc1SJed Brown }
1854e3c11fc1SJed Brown 
1855e3c11fc1SJed Brown /*@
1856efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1857efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1858efe9872eSLisandro Dalcin 
1859d083f849SBarry Smith    Logically Collective on TS
1860efe9872eSLisandro Dalcin 
1861efe9872eSLisandro Dalcin    Input Parameters:
1862efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1863efe9872eSLisandro Dalcin .  u - the solution vector
1864efe9872eSLisandro Dalcin -  v - the time derivative vector
1865efe9872eSLisandro Dalcin 
1866efe9872eSLisandro Dalcin    Level: beginner
1867efe9872eSLisandro Dalcin 
1868efe9872eSLisandro Dalcin @*/
1869efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1870efe9872eSLisandro Dalcin {
1871efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1872efe9872eSLisandro Dalcin 
1873efe9872eSLisandro Dalcin   PetscFunctionBegin;
1874efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1875efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1876efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1877efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1878efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1879efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1880efe9872eSLisandro Dalcin   ts->vec_dot = v;
1881efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1882efe9872eSLisandro Dalcin }
1883efe9872eSLisandro Dalcin 
1884efe9872eSLisandro Dalcin /*@
1885efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1886efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1887efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1888efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1889efe9872eSLisandro Dalcin 
1890efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1891efe9872eSLisandro Dalcin 
1892efe9872eSLisandro Dalcin    Input Parameter:
1893efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1894efe9872eSLisandro Dalcin 
1895efe9872eSLisandro Dalcin    Output Parameter:
1896efe9872eSLisandro Dalcin +  u - the vector containing the solution
1897efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1898efe9872eSLisandro Dalcin 
1899efe9872eSLisandro Dalcin    Level: intermediate
1900efe9872eSLisandro Dalcin 
1901efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1902efe9872eSLisandro Dalcin 
1903efe9872eSLisandro Dalcin @*/
1904efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1905efe9872eSLisandro Dalcin {
1906efe9872eSLisandro Dalcin   PetscFunctionBegin;
1907efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1908efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1909efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1910efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1911efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1912efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1913efe9872eSLisandro Dalcin }
1914efe9872eSLisandro Dalcin 
191555849f57SBarry Smith /*@C
191655849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
191755849f57SBarry Smith 
191855849f57SBarry Smith   Collective on PetscViewer
191955849f57SBarry Smith 
192055849f57SBarry Smith   Input Parameters:
192155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
192255849f57SBarry Smith            some related function before a call to TSLoad().
192355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
192455849f57SBarry Smith 
192555849f57SBarry Smith    Level: intermediate
192655849f57SBarry Smith 
192755849f57SBarry Smith   Notes:
192855849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
192955849f57SBarry Smith 
193055849f57SBarry Smith   Notes for advanced users:
193155849f57SBarry Smith   Most users should not need to know the details of the binary storage
193255849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
193355849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
193455849f57SBarry Smith   format is
193555849f57SBarry Smith .vb
193655849f57SBarry Smith      has not yet been determined
193755849f57SBarry Smith .ve
193855849f57SBarry Smith 
193955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
194055849f57SBarry Smith @*/
1941f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
194255849f57SBarry Smith {
194355849f57SBarry Smith   PetscErrorCode ierr;
194455849f57SBarry Smith   PetscBool      isbinary;
194555849f57SBarry Smith   PetscInt       classid;
194655849f57SBarry Smith   char           type[256];
19472d53ad75SBarry Smith   DMTS           sdm;
1948ad6bc421SBarry Smith   DM             dm;
194955849f57SBarry Smith 
195055849f57SBarry Smith   PetscFunctionBegin;
1951f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
195255849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
195355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
195455849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
195555849f57SBarry Smith 
1956060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1957ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1958060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1959f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1960f2c2a1b9SBarry Smith   if (ts->ops->load) {
1961f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1962f2c2a1b9SBarry Smith   }
1963ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1964ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1965ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1966f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1967f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19682d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19692d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
197055849f57SBarry Smith   PetscFunctionReturn(0);
197155849f57SBarry Smith }
197255849f57SBarry Smith 
19739804daf3SBarry Smith #include <petscdraw.h>
1974e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1975e04113cfSBarry Smith #include <petscviewersaws.h>
1976f05ece33SBarry Smith #endif
1977fe2efc57SMark 
1978fe2efc57SMark /*@C
1979fe2efc57SMark    TSViewFromOptions - View from Options
1980fe2efc57SMark 
1981fe2efc57SMark    Collective on TS
1982fe2efc57SMark 
1983fe2efc57SMark    Input Parameters:
1984fe2efc57SMark +  A - the application ordering context
1985736c3998SJose E. Roman .  obj - Optional object
1986736c3998SJose E. Roman -  name - command line option
1987fe2efc57SMark 
1988fe2efc57SMark    Level: intermediate
1989fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
1990fe2efc57SMark @*/
1991fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
1992fe2efc57SMark {
1993fe2efc57SMark   PetscErrorCode ierr;
1994fe2efc57SMark 
1995fe2efc57SMark   PetscFunctionBegin;
1996fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
1997fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
1998fe2efc57SMark   PetscFunctionReturn(0);
1999fe2efc57SMark }
2000fe2efc57SMark 
20017e2c5f70SBarry Smith /*@C
2002d763cef2SBarry Smith     TSView - Prints the TS data structure.
2003d763cef2SBarry Smith 
20044c49b128SBarry Smith     Collective on TS
2005d763cef2SBarry Smith 
2006d763cef2SBarry Smith     Input Parameters:
2007d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2008d763cef2SBarry Smith -   viewer - visualization context
2009d763cef2SBarry Smith 
2010d763cef2SBarry Smith     Options Database Key:
2011d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2012d763cef2SBarry Smith 
2013d763cef2SBarry Smith     Notes:
2014d763cef2SBarry Smith     The available visualization contexts include
2015b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2016b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2017d763cef2SBarry Smith          output where only the first processor opens
2018d763cef2SBarry Smith          the file.  All other processors send their
2019d763cef2SBarry Smith          data to the first processor to print.
2020d763cef2SBarry Smith 
2021d763cef2SBarry Smith     The user can open an alternative visualization context with
2022b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith     Level: beginner
2025d763cef2SBarry Smith 
2026b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2027d763cef2SBarry Smith @*/
20287087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2029d763cef2SBarry Smith {
2030dfbe8321SBarry Smith   PetscErrorCode ierr;
203119fd82e9SBarry Smith   TSType         type;
20322b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20332d53ad75SBarry Smith   DMTS           sdm;
2034e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2035536b137fSBarry Smith   PetscBool      issaws;
2036f05ece33SBarry Smith #endif
2037d763cef2SBarry Smith 
2038d763cef2SBarry Smith   PetscFunctionBegin;
20390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20403050cee2SBarry Smith   if (!viewer) {
2041ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20423050cee2SBarry Smith   }
20430700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2044c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2045fd16b177SBarry Smith 
2046251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2047251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
204855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20492b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2050e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2051536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2052f05ece33SBarry Smith #endif
205332077d6dSBarry Smith   if (iascii) {
2054dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2055efd4aadfSBarry Smith     if (ts->ops->view) {
2056efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2057efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2058efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2059efd4aadfSBarry Smith     }
2060ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
206177431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2062ef85077eSLisandro Dalcin     }
2063ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20647c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2065ef85077eSLisandro Dalcin     }
2066efd4aadfSBarry Smith     if (ts->usessnes) {
2067efd4aadfSBarry Smith       PetscBool lin;
2068d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20695ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2070d763cef2SBarry Smith       }
20715ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
20721ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2073efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2074efd4aadfSBarry Smith     }
2075193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2076a0af407cSBarry Smith     if (ts->vrtol) {
2077a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2078a0af407cSBarry Smith     } else {
2079a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2080a0af407cSBarry Smith     }
2081a0af407cSBarry Smith     if (ts->vatol) {
2082a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2083a0af407cSBarry Smith     } else {
2084a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2085a0af407cSBarry Smith     }
2086825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2087efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2088825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
20890f5bd95cSBarry Smith   } else if (isstring) {
2090a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
209136a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
209236a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
209355849f57SBarry Smith   } else if (isbinary) {
209455849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
209555849f57SBarry Smith     MPI_Comm    comm;
209655849f57SBarry Smith     PetscMPIInt rank;
209755849f57SBarry Smith     char        type[256];
209855849f57SBarry Smith 
209955849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
210055849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
210155849f57SBarry Smith     if (!rank) {
2102f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
210355849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2104f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
210555849f57SBarry Smith     }
210655849f57SBarry Smith     if (ts->ops->view) {
210755849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
210855849f57SBarry Smith     }
2109efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2110f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2111f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21122d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21132d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21142b0a91c0SBarry Smith   } else if (isdraw) {
21152b0a91c0SBarry Smith     PetscDraw draw;
21162b0a91c0SBarry Smith     char      str[36];
211789fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21182b0a91c0SBarry Smith 
21192b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21202b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21212b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21222b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
212351fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
212489fd9fafSBarry Smith     bottom = y - h;
21252b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21262b0a91c0SBarry Smith     if (ts->ops->view) {
21272b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21282b0a91c0SBarry Smith     }
2129efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2130efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21312b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2132e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2133536b137fSBarry Smith   } else if (issaws) {
2134d45a07a7SBarry Smith     PetscMPIInt rank;
21352657e9d9SBarry Smith     const char  *name;
21362657e9d9SBarry Smith 
21372657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2138d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2139d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2140d45a07a7SBarry Smith       char       dir[1024];
2141d45a07a7SBarry Smith 
2142e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2143a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21442657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21452657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21462657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2147d763cef2SBarry Smith     }
21480acecf5bSBarry Smith     if (ts->ops->view) {
21490acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21500acecf5bSBarry Smith     }
2151f05ece33SBarry Smith #endif
2152f05ece33SBarry Smith   }
215336a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
215436a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
215536a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
215636a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
215736a9e3b9SBarry Smith   }
215836a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
215936a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2160f05ece33SBarry Smith 
2161b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2162251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2163b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2164d763cef2SBarry Smith   PetscFunctionReturn(0);
2165d763cef2SBarry Smith }
2166d763cef2SBarry Smith 
2167b07ff414SBarry Smith /*@
2168d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2169d763cef2SBarry Smith    the timesteppers.
2170d763cef2SBarry Smith 
21713f9fe445SBarry Smith    Logically Collective on TS
2172d763cef2SBarry Smith 
2173d763cef2SBarry Smith    Input Parameters:
2174d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2175d763cef2SBarry Smith -  usrP - optional user context
2176d763cef2SBarry Smith 
217795452b02SPatrick Sanan    Fortran Notes:
217895452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2179daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2180daf670e6SBarry Smith 
2181d763cef2SBarry Smith    Level: intermediate
2182d763cef2SBarry Smith 
2183d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2184d763cef2SBarry Smith @*/
21857087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2186d763cef2SBarry Smith {
2187d763cef2SBarry Smith   PetscFunctionBegin;
21880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2189d763cef2SBarry Smith   ts->user = usrP;
2190d763cef2SBarry Smith   PetscFunctionReturn(0);
2191d763cef2SBarry Smith }
2192d763cef2SBarry Smith 
2193b07ff414SBarry Smith /*@
2194d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2195d763cef2SBarry Smith     timestepper.
2196d763cef2SBarry Smith 
2197d763cef2SBarry Smith     Not Collective
2198d763cef2SBarry Smith 
2199d763cef2SBarry Smith     Input Parameter:
2200d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith     Output Parameter:
2203d763cef2SBarry Smith .   usrP - user context
2204d763cef2SBarry Smith 
220595452b02SPatrick Sanan    Fortran Notes:
220695452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2207daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2208daf670e6SBarry Smith 
2209d763cef2SBarry Smith     Level: intermediate
2210d763cef2SBarry Smith 
2211d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2212d763cef2SBarry Smith @*/
2213e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2214d763cef2SBarry Smith {
2215d763cef2SBarry Smith   PetscFunctionBegin;
22160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2217e71120c6SJed Brown   *(void**)usrP = ts->user;
2218d763cef2SBarry Smith   PetscFunctionReturn(0);
2219d763cef2SBarry Smith }
2220d763cef2SBarry Smith 
2221d763cef2SBarry Smith /*@
222280275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith    Not Collective
2225d763cef2SBarry Smith 
2226d763cef2SBarry Smith    Input Parameter:
2227d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2228d763cef2SBarry Smith 
2229d763cef2SBarry Smith    Output Parameter:
223080275a0aSLisandro Dalcin .  steps - number of steps completed so far
2231d763cef2SBarry Smith 
2232d763cef2SBarry Smith    Level: intermediate
2233d763cef2SBarry Smith 
22349be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2235d763cef2SBarry Smith @*/
223680275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2237d763cef2SBarry Smith {
2238d763cef2SBarry Smith   PetscFunctionBegin;
22390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
224080275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
224180275a0aSLisandro Dalcin   *steps = ts->steps;
224280275a0aSLisandro Dalcin   PetscFunctionReturn(0);
224380275a0aSLisandro Dalcin }
224480275a0aSLisandro Dalcin 
224580275a0aSLisandro Dalcin /*@
224680275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
224780275a0aSLisandro Dalcin 
224880275a0aSLisandro Dalcin    Logically Collective on TS
224980275a0aSLisandro Dalcin 
225080275a0aSLisandro Dalcin    Input Parameters:
225180275a0aSLisandro Dalcin +  ts - the TS context
225280275a0aSLisandro Dalcin -  steps - number of steps completed so far
225380275a0aSLisandro Dalcin 
225480275a0aSLisandro Dalcin    Notes:
225580275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
225680275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
225780275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
225880275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
225980275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
226080275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
226180275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
226280275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
226380275a0aSLisandro Dalcin 
226480275a0aSLisandro Dalcin    Level: advanced
226580275a0aSLisandro Dalcin 
226680275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
226780275a0aSLisandro Dalcin @*/
226880275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
226980275a0aSLisandro Dalcin {
227080275a0aSLisandro Dalcin   PetscFunctionBegin;
227180275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227280275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
227380275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
227480275a0aSLisandro Dalcin   ts->steps = steps;
2275d763cef2SBarry Smith   PetscFunctionReturn(0);
2276d763cef2SBarry Smith }
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith /*@
2279d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2280d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2281d763cef2SBarry Smith 
22823f9fe445SBarry Smith    Logically Collective on TS
2283d763cef2SBarry Smith 
2284d763cef2SBarry Smith    Input Parameters:
2285d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2286d763cef2SBarry Smith -  time_step - the size of the timestep
2287d763cef2SBarry Smith 
2288d763cef2SBarry Smith    Level: intermediate
2289d763cef2SBarry Smith 
2290aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2291d763cef2SBarry Smith 
2292d763cef2SBarry Smith @*/
22937087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2294d763cef2SBarry Smith {
2295d763cef2SBarry Smith   PetscFunctionBegin;
22960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2297c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2298d763cef2SBarry Smith   ts->time_step = time_step;
2299d763cef2SBarry Smith   PetscFunctionReturn(0);
2300d763cef2SBarry Smith }
2301d763cef2SBarry Smith 
2302a43b19c4SJed Brown /*@
230349354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
230449354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
230549354f04SShri Abhyankar      or just return at the final time TS computed.
2306a43b19c4SJed Brown 
2307a43b19c4SJed Brown   Logically Collective on TS
2308a43b19c4SJed Brown 
2309a43b19c4SJed Brown    Input Parameter:
2310a43b19c4SJed Brown +   ts - the time-step context
231149354f04SShri Abhyankar -   eftopt - exact final time option
2312a43b19c4SJed Brown 
2313feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2314feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2315feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2316feed9e9dSBarry Smith 
2317feed9e9dSBarry Smith    Options Database:
2318feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2319feed9e9dSBarry Smith 
2320ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2321ee346746SBarry Smith     then the final time you selected.
2322ee346746SBarry Smith 
2323a43b19c4SJed Brown    Level: beginner
2324a43b19c4SJed Brown 
2325f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2326a43b19c4SJed Brown @*/
232749354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2328a43b19c4SJed Brown {
2329a43b19c4SJed Brown   PetscFunctionBegin;
2330a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
233149354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
233249354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2333a43b19c4SJed Brown   PetscFunctionReturn(0);
2334a43b19c4SJed Brown }
2335a43b19c4SJed Brown 
2336d763cef2SBarry Smith /*@
2337f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2338f6953c82SLisandro Dalcin 
2339f6953c82SLisandro Dalcin    Not Collective
2340f6953c82SLisandro Dalcin 
2341f6953c82SLisandro Dalcin    Input Parameter:
2342f6953c82SLisandro Dalcin .  ts - the TS context
2343f6953c82SLisandro Dalcin 
2344f6953c82SLisandro Dalcin    Output Parameter:
2345f6953c82SLisandro Dalcin .  eftopt - exact final time option
2346f6953c82SLisandro Dalcin 
2347f6953c82SLisandro Dalcin    Level: beginner
2348f6953c82SLisandro Dalcin 
2349f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2350f6953c82SLisandro Dalcin @*/
2351f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2352f6953c82SLisandro Dalcin {
2353f6953c82SLisandro Dalcin   PetscFunctionBegin;
2354f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2355f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2356f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2357f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2358f6953c82SLisandro Dalcin }
2359f6953c82SLisandro Dalcin 
2360f6953c82SLisandro Dalcin /*@
2361d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2362d763cef2SBarry Smith 
2363d763cef2SBarry Smith    Not Collective
2364d763cef2SBarry Smith 
2365d763cef2SBarry Smith    Input Parameter:
2366d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2367d763cef2SBarry Smith 
2368d763cef2SBarry Smith    Output Parameter:
2369d763cef2SBarry Smith .  dt - the current timestep size
2370d763cef2SBarry Smith 
2371d763cef2SBarry Smith    Level: intermediate
2372d763cef2SBarry Smith 
2373aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2374d763cef2SBarry Smith 
2375d763cef2SBarry Smith @*/
23767087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2377d763cef2SBarry Smith {
2378d763cef2SBarry Smith   PetscFunctionBegin;
23790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2380f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2381d763cef2SBarry Smith   *dt = ts->time_step;
2382d763cef2SBarry Smith   PetscFunctionReturn(0);
2383d763cef2SBarry Smith }
2384d763cef2SBarry Smith 
2385d8e5e3e6SSatish Balay /*@
2386d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2387d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2388d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2389d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2390d763cef2SBarry Smith 
2391d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2392d763cef2SBarry Smith 
2393d763cef2SBarry Smith    Input Parameter:
2394d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2395d763cef2SBarry Smith 
2396d763cef2SBarry Smith    Output Parameter:
2397d763cef2SBarry Smith .  v - the vector containing the solution
2398d763cef2SBarry Smith 
239963e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
240063e21af5SBarry Smith    final time. It returns the solution at the next timestep.
240163e21af5SBarry Smith 
2402d763cef2SBarry Smith    Level: intermediate
2403d763cef2SBarry Smith 
24040ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2405d763cef2SBarry Smith 
2406d763cef2SBarry Smith @*/
24077087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2408d763cef2SBarry Smith {
2409d763cef2SBarry Smith   PetscFunctionBegin;
24100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24114482741eSBarry Smith   PetscValidPointer(v,2);
24128737fe31SLisandro Dalcin   *v = ts->vec_sol;
2413d763cef2SBarry Smith   PetscFunctionReturn(0);
2414d763cef2SBarry Smith }
2415d763cef2SBarry Smith 
241603fe5f5eSDebojyoti Ghosh /*@
2417b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
241803fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2419b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
242003fe5f5eSDebojyoti Ghosh    structure as the solution vector.
242103fe5f5eSDebojyoti Ghosh 
242203fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
242303fe5f5eSDebojyoti Ghosh 
242403fe5f5eSDebojyoti Ghosh    Parameters :
2425a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2426b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2427b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
242803fe5f5eSDebojyoti Ghosh        returned in v.
2429a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
243003fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2431b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
243203fe5f5eSDebojyoti Ghosh 
24334cdd57e5SDebojyoti Ghosh    Level: advanced
243403fe5f5eSDebojyoti Ghosh 
243503fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
243603fe5f5eSDebojyoti Ghosh 
243703fe5f5eSDebojyoti Ghosh @*/
2438b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
243903fe5f5eSDebojyoti Ghosh {
244003fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
244103fe5f5eSDebojyoti Ghosh 
244203fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
244303fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2444b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
244503fe5f5eSDebojyoti Ghosh   else {
2446b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
244703fe5f5eSDebojyoti Ghosh   }
244803fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
244903fe5f5eSDebojyoti Ghosh }
245003fe5f5eSDebojyoti Ghosh 
24514cdd57e5SDebojyoti Ghosh /*@
24524cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24534cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24544cdd57e5SDebojyoti Ghosh 
24554cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24564cdd57e5SDebojyoti Ghosh 
24574cdd57e5SDebojyoti Ghosh    Parameters :
2458a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2459a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24604cdd57e5SDebojyoti Ghosh 
24614cdd57e5SDebojyoti Ghosh    Level: intermediate
24624cdd57e5SDebojyoti Ghosh 
24634cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24644cdd57e5SDebojyoti Ghosh 
24654cdd57e5SDebojyoti Ghosh @*/
24664cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24674cdd57e5SDebojyoti Ghosh {
24684cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24694cdd57e5SDebojyoti Ghosh 
24704cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24714cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2472f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24734cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2474f6356ec7SDebojyoti Ghosh   } else {
2475f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2476f6356ec7SDebojyoti Ghosh   }
24774cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24784cdd57e5SDebojyoti Ghosh }
24794cdd57e5SDebojyoti Ghosh 
24804cdd57e5SDebojyoti Ghosh /*@
24814cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24824cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24834cdd57e5SDebojyoti Ghosh 
24844cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24854cdd57e5SDebojyoti Ghosh 
24869657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24879657682dSDebojyoti Ghosh 
24884cdd57e5SDebojyoti Ghosh    Parameters :
2489a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2490657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2491a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
24924cdd57e5SDebojyoti Ghosh 
24934cdd57e5SDebojyoti Ghosh    Level: intermediate
24944cdd57e5SDebojyoti Ghosh 
249557df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24964cdd57e5SDebojyoti Ghosh 
24974cdd57e5SDebojyoti Ghosh @*/
24980a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24994cdd57e5SDebojyoti Ghosh {
25004cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25014cdd57e5SDebojyoti Ghosh 
25024cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25034cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2504f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25050a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2506f6356ec7SDebojyoti Ghosh   } else {
2507f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2508f6356ec7SDebojyoti Ghosh   }
25094cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25104cdd57e5SDebojyoti Ghosh }
25114cdd57e5SDebojyoti Ghosh 
251257df6a1bSDebojyoti Ghosh /*@
251357df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
251457df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
251557df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
251657df6a1bSDebojyoti Ghosh 
251757df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
251857df6a1bSDebojyoti Ghosh 
251957df6a1bSDebojyoti Ghosh    Parameters :
2520a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2521a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
252257df6a1bSDebojyoti Ghosh 
252357df6a1bSDebojyoti Ghosh    Level: intermediate
252457df6a1bSDebojyoti Ghosh 
252557df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
252657df6a1bSDebojyoti Ghosh 
252757df6a1bSDebojyoti Ghosh @*/
252857df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
252957df6a1bSDebojyoti Ghosh {
253057df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
253157df6a1bSDebojyoti Ghosh 
253257df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
253357df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25349657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
253557df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
253657df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
253757df6a1bSDebojyoti Ghosh   }
253857df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
253957df6a1bSDebojyoti Ghosh }
254057df6a1bSDebojyoti Ghosh 
2541bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2542d8e5e3e6SSatish Balay /*@
2543bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2544d763cef2SBarry Smith 
2545bdad233fSMatthew Knepley   Not collective
2546d763cef2SBarry Smith 
2547bdad233fSMatthew Knepley   Input Parameters:
2548bdad233fSMatthew Knepley + ts   - The TS
2549bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2550d763cef2SBarry Smith .vb
25510910c330SBarry Smith          U_t - A U = 0      (linear)
25520910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25530910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2554d763cef2SBarry Smith .ve
2555d763cef2SBarry Smith 
2556d763cef2SBarry Smith    Level: beginner
2557d763cef2SBarry Smith 
2558bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2559d763cef2SBarry Smith @*/
25607087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2561a7cc72afSBarry Smith {
25629e2a6581SJed Brown   PetscErrorCode ierr;
25639e2a6581SJed Brown 
2564d763cef2SBarry Smith   PetscFunctionBegin;
25650700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2566bdad233fSMatthew Knepley   ts->problem_type = type;
25679e2a6581SJed Brown   if (type == TS_LINEAR) {
25689e2a6581SJed Brown     SNES snes;
25699e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25709e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25719e2a6581SJed Brown   }
2572d763cef2SBarry Smith   PetscFunctionReturn(0);
2573d763cef2SBarry Smith }
2574d763cef2SBarry Smith 
2575bdad233fSMatthew Knepley /*@C
2576bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2577bdad233fSMatthew Knepley 
2578bdad233fSMatthew Knepley   Not collective
2579bdad233fSMatthew Knepley 
2580bdad233fSMatthew Knepley   Input Parameter:
2581bdad233fSMatthew Knepley . ts   - The TS
2582bdad233fSMatthew Knepley 
2583bdad233fSMatthew Knepley   Output Parameter:
2584bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2585bdad233fSMatthew Knepley .vb
2586089b2837SJed Brown          M U_t = A U
2587089b2837SJed Brown          M(t) U_t = A(t) U
2588b5abc632SBarry Smith          F(t,U,U_t)
2589bdad233fSMatthew Knepley .ve
2590bdad233fSMatthew Knepley 
2591bdad233fSMatthew Knepley    Level: beginner
2592bdad233fSMatthew Knepley 
2593bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2594bdad233fSMatthew Knepley @*/
25957087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2596a7cc72afSBarry Smith {
2597bdad233fSMatthew Knepley   PetscFunctionBegin;
25980700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25994482741eSBarry Smith   PetscValidIntPointer(type,2);
2600bdad233fSMatthew Knepley   *type = ts->problem_type;
2601bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2602bdad233fSMatthew Knepley }
2603d763cef2SBarry Smith 
2604303a5415SBarry Smith /*
2605303a5415SBarry 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()
2606303a5415SBarry Smith */
2607303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2608303a5415SBarry Smith {
2609303a5415SBarry Smith   PetscErrorCode ierr;
2610303a5415SBarry Smith   PetscBool      isnone;
2611303a5415SBarry Smith 
2612303a5415SBarry Smith   PetscFunctionBegin;
2613303a5415SBarry Smith   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
2614303a5415SBarry Smith   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
2615303a5415SBarry Smith 
2616303a5415SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
2617303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2618303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2619303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2620303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2621303a5415SBarry Smith   }
2622303a5415SBarry Smith   PetscFunctionReturn(0);
2623303a5415SBarry Smith }
2624303a5415SBarry Smith 
2625303a5415SBarry Smith 
2626d763cef2SBarry Smith /*@
2627303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2628d763cef2SBarry Smith 
2629d763cef2SBarry Smith    Collective on TS
2630d763cef2SBarry Smith 
2631d763cef2SBarry Smith    Input Parameter:
2632d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2633d763cef2SBarry Smith 
2634d763cef2SBarry Smith    Notes:
2635d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2636d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2637141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2638d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2639141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2640d763cef2SBarry Smith 
2641d763cef2SBarry Smith    Level: advanced
2642d763cef2SBarry Smith 
2643141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2644d763cef2SBarry Smith @*/
26457087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2646d763cef2SBarry Smith {
2647dfbe8321SBarry Smith   PetscErrorCode ierr;
26486c6b9e74SPeter Brune   DM             dm;
26496c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2650d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2651cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26526c6b9e74SPeter Brune   TSIJacobian    ijac;
2653efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26546c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2655d763cef2SBarry Smith 
2656d763cef2SBarry Smith   PetscFunctionBegin;
26570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2658277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2659277b19d0SLisandro Dalcin 
26607adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2661cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2662cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2663d763cef2SBarry Smith   }
2664277b19d0SLisandro Dalcin 
26651a638600SStefano Zampini   if (!ts->vec_sol) {
26661a638600SStefano Zampini     if (ts->dm) {
26671a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
26681a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
26691a638600SStefano Zampini   }
2670277b19d0SLisandro Dalcin 
2671298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2672298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2673298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2674298bade4SHong Zhang   }
2675298bade4SHong Zhang 
2676cd4cee2dSHong Zhang   if (ts->quadraturets) {
2677cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2678ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2679cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2680cd4cee2dSHong Zhang   }
2681cd4cee2dSHong Zhang 
2682971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2683f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2684e1244c69SJed Brown     Mat Amat,Pmat;
2685e1244c69SJed Brown     SNES snes;
2686e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2687e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2688e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2689e1244c69SJed Brown      * have displaced the RHS matrix */
2690971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2691abc0d4abSBarry 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 */
2692abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2693e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2694e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2695e1244c69SJed Brown     }
2696971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2697abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2698e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2699e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2700e1244c69SJed Brown     }
2701e1244c69SJed Brown   }
27022ffb9264SLisandro Dalcin 
27032ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27042ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27052ffb9264SLisandro Dalcin 
2706277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2707000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2708277b19d0SLisandro Dalcin   }
2709277b19d0SLisandro Dalcin 
2710303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
27112ffb9264SLisandro Dalcin 
2712a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27136c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27146c6b9e74SPeter Brune    */
27156c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27160298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27176c6b9e74SPeter Brune   if (!func) {
27186c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27196c6b9e74SPeter Brune   }
2720a6772fa2SLisandro 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.
27216c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27226c6b9e74SPeter Brune    */
27230298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27240298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2725efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27260298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2727efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27286c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27296c6b9e74SPeter Brune   }
2730c0517034SDebojyoti Ghosh 
2731c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2732c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2733c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2734c0517034SDebojyoti Ghosh   }
2735c0517034SDebojyoti Ghosh 
2736277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2737277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2738277b19d0SLisandro Dalcin }
2739277b19d0SLisandro Dalcin 
2740f6a906c0SBarry Smith /*@
2741277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2742277b19d0SLisandro Dalcin 
2743277b19d0SLisandro Dalcin    Collective on TS
2744277b19d0SLisandro Dalcin 
2745277b19d0SLisandro Dalcin    Input Parameter:
2746277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2747277b19d0SLisandro Dalcin 
2748277b19d0SLisandro Dalcin    Level: beginner
2749277b19d0SLisandro Dalcin 
2750277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2751277b19d0SLisandro Dalcin @*/
2752277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2753277b19d0SLisandro Dalcin {
27541d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2755277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2756277b19d0SLisandro Dalcin 
2757277b19d0SLisandro Dalcin   PetscFunctionBegin;
2758277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2759b18ea86cSHong Zhang 
2760277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2761277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2762277b19d0SLisandro Dalcin   }
2763277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2764e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2765bbd56ea5SKarl Rupp 
27664e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27674e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2768214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27696bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2770efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2771e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2772e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
277338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2774bbd56ea5SKarl Rupp 
2775cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2776ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2777ecf68647SHong Zhang   if (ts->forward_solve) {
2778ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2779ecf68647SHong Zhang   }
2780cd4cee2dSHong Zhang   if (ts->quadraturets) {
2781cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
278236eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2783cd4cee2dSHong Zhang   }
27841d06f6b3SHong Zhang   while (ilink) {
27851d06f6b3SHong Zhang     next = ilink->next;
27861d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
27871d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
27881d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
27891d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
27901d06f6b3SHong Zhang     ilink = next;
279187f4e208SHong Zhang   }
2792545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2793277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2794d763cef2SBarry Smith   PetscFunctionReturn(0);
2795d763cef2SBarry Smith }
2796d763cef2SBarry Smith 
2797d8e5e3e6SSatish Balay /*@
2798d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2799d763cef2SBarry Smith    with TSCreate().
2800d763cef2SBarry Smith 
2801d763cef2SBarry Smith    Collective on TS
2802d763cef2SBarry Smith 
2803d763cef2SBarry Smith    Input Parameter:
2804d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2805d763cef2SBarry Smith 
2806d763cef2SBarry Smith    Level: beginner
2807d763cef2SBarry Smith 
2808d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2809d763cef2SBarry Smith @*/
28106bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2811d763cef2SBarry Smith {
28126849ba73SBarry Smith   PetscErrorCode ierr;
2813d763cef2SBarry Smith 
2814d763cef2SBarry Smith   PetscFunctionBegin;
28156bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2816ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2817c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2818d763cef2SBarry Smith 
2819ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2820ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2821ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2822ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2823ecf68647SHong Zhang   }
2824e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2825e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28266bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28276d4c513bSLisandro Dalcin 
2828bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2829bc952696SBarry Smith 
283084df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28316427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28326427ac75SLisandro Dalcin 
28336bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28346bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28356bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28360dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28376d4c513bSLisandro Dalcin 
2838cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2839a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2840d763cef2SBarry Smith   PetscFunctionReturn(0);
2841d763cef2SBarry Smith }
2842d763cef2SBarry Smith 
2843d8e5e3e6SSatish Balay /*@
2844d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2845d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2846d763cef2SBarry Smith 
2847d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2848d763cef2SBarry Smith 
2849d763cef2SBarry Smith    Input Parameter:
2850d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2851d763cef2SBarry Smith 
2852d763cef2SBarry Smith    Output Parameter:
2853d763cef2SBarry Smith .  snes - the nonlinear solver context
2854d763cef2SBarry Smith 
2855d763cef2SBarry Smith    Notes:
2856d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2857d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
285894b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2859d763cef2SBarry Smith 
2860d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28610298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2862d763cef2SBarry Smith 
2863d763cef2SBarry Smith    Level: beginner
2864d763cef2SBarry Smith 
2865d763cef2SBarry Smith @*/
28667087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2867d763cef2SBarry Smith {
2868d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2869d372ba47SLisandro Dalcin 
2870d763cef2SBarry Smith   PetscFunctionBegin;
28710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28724482741eSBarry Smith   PetscValidPointer(snes,2);
2873d372ba47SLisandro Dalcin   if (!ts->snes) {
2874ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
287516413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
28760298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28773bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2878d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2879496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28809e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28819e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
28829e2a6581SJed Brown     }
2883d372ba47SLisandro Dalcin   }
2884d763cef2SBarry Smith   *snes = ts->snes;
2885d763cef2SBarry Smith   PetscFunctionReturn(0);
2886d763cef2SBarry Smith }
2887d763cef2SBarry Smith 
2888deb2cd25SJed Brown /*@
2889deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2890deb2cd25SJed Brown 
2891deb2cd25SJed Brown    Collective
2892deb2cd25SJed Brown 
2893deb2cd25SJed Brown    Input Parameter:
2894deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2895deb2cd25SJed Brown -  snes - the nonlinear solver context
2896deb2cd25SJed Brown 
2897deb2cd25SJed Brown    Notes:
2898deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2899deb2cd25SJed Brown 
2900deb2cd25SJed Brown    Level: developer
2901deb2cd25SJed Brown 
2902deb2cd25SJed Brown @*/
2903deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2904deb2cd25SJed Brown {
2905deb2cd25SJed Brown   PetscErrorCode ierr;
2906d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2907deb2cd25SJed Brown 
2908deb2cd25SJed Brown   PetscFunctionBegin;
2909deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2910deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2911deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2912deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2913bbd56ea5SKarl Rupp 
2914deb2cd25SJed Brown   ts->snes = snes;
2915bbd56ea5SKarl Rupp 
29160298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29170298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2918740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29190298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2920740132f1SEmil Constantinescu   }
2921deb2cd25SJed Brown   PetscFunctionReturn(0);
2922deb2cd25SJed Brown }
2923deb2cd25SJed Brown 
2924d8e5e3e6SSatish Balay /*@
292594b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2926d763cef2SBarry Smith    a TS (timestepper) context.
2927d763cef2SBarry Smith 
292894b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2929d763cef2SBarry Smith 
2930d763cef2SBarry Smith    Input Parameter:
2931d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2932d763cef2SBarry Smith 
2933d763cef2SBarry Smith    Output Parameter:
293494b7f48cSBarry Smith .  ksp - the nonlinear solver context
2935d763cef2SBarry Smith 
2936d763cef2SBarry Smith    Notes:
293794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2938d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2939d763cef2SBarry Smith    KSP and PC contexts as well.
2940d763cef2SBarry Smith 
294194b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29420298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2943d763cef2SBarry Smith 
2944d763cef2SBarry Smith    Level: beginner
2945d763cef2SBarry Smith 
2946d763cef2SBarry Smith @*/
29477087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2948d763cef2SBarry Smith {
2949d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2950089b2837SJed Brown   SNES           snes;
2951d372ba47SLisandro Dalcin 
2952d763cef2SBarry Smith   PetscFunctionBegin;
29530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29544482741eSBarry Smith   PetscValidPointer(ksp,2);
295517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2956e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2957089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2958089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2959d763cef2SBarry Smith   PetscFunctionReturn(0);
2960d763cef2SBarry Smith }
2961d763cef2SBarry Smith 
2962d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2963d763cef2SBarry Smith 
2964adb62b0dSMatthew Knepley /*@
2965618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2966618ce8baSLisandro Dalcin 
2967618ce8baSLisandro Dalcin    Logically Collective on TS
2968618ce8baSLisandro Dalcin 
2969618ce8baSLisandro Dalcin    Input Parameters:
2970618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2971618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2972618ce8baSLisandro Dalcin 
2973618ce8baSLisandro Dalcin    Options Database Keys:
2974618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2975618ce8baSLisandro Dalcin 
2976618ce8baSLisandro Dalcin    Notes:
2977618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2978618ce8baSLisandro Dalcin 
2979618ce8baSLisandro Dalcin    Level: intermediate
2980618ce8baSLisandro Dalcin 
2981618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2982618ce8baSLisandro Dalcin @*/
2983618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2984618ce8baSLisandro Dalcin {
2985618ce8baSLisandro Dalcin   PetscFunctionBegin;
2986618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2987618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2988618ce8baSLisandro Dalcin   if (maxsteps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2989618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2990618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2991618ce8baSLisandro Dalcin }
2992618ce8baSLisandro Dalcin 
2993618ce8baSLisandro Dalcin /*@
2994618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2995618ce8baSLisandro Dalcin 
2996618ce8baSLisandro Dalcin    Not Collective
2997618ce8baSLisandro Dalcin 
2998618ce8baSLisandro Dalcin    Input Parameters:
2999618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3000618ce8baSLisandro Dalcin 
3001618ce8baSLisandro Dalcin    Output Parameter:
3002618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3003618ce8baSLisandro Dalcin 
3004618ce8baSLisandro Dalcin    Level: advanced
3005618ce8baSLisandro Dalcin 
3006618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3007618ce8baSLisandro Dalcin @*/
3008618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3009618ce8baSLisandro Dalcin {
3010618ce8baSLisandro Dalcin   PetscFunctionBegin;
3011618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3012618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3013618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3014618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3015618ce8baSLisandro Dalcin }
3016618ce8baSLisandro Dalcin 
3017618ce8baSLisandro Dalcin /*@
3018618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3019618ce8baSLisandro Dalcin 
3020618ce8baSLisandro Dalcin    Logically Collective on TS
3021618ce8baSLisandro Dalcin 
3022618ce8baSLisandro Dalcin    Input Parameters:
3023618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3024618ce8baSLisandro Dalcin -  maxtime - final time to step to
3025618ce8baSLisandro Dalcin 
3026618ce8baSLisandro Dalcin    Options Database Keys:
3027ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3028618ce8baSLisandro Dalcin 
3029618ce8baSLisandro Dalcin    Notes:
3030618ce8baSLisandro Dalcin    The default maximum time is 5.0
3031618ce8baSLisandro Dalcin 
3032618ce8baSLisandro Dalcin    Level: intermediate
3033618ce8baSLisandro Dalcin 
3034618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3035618ce8baSLisandro Dalcin @*/
3036618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3037618ce8baSLisandro Dalcin {
3038618ce8baSLisandro Dalcin   PetscFunctionBegin;
3039618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3040618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3041618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3042618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3043618ce8baSLisandro Dalcin }
3044618ce8baSLisandro Dalcin 
3045618ce8baSLisandro Dalcin /*@
3046618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3047618ce8baSLisandro Dalcin 
3048618ce8baSLisandro Dalcin    Not Collective
3049618ce8baSLisandro Dalcin 
3050618ce8baSLisandro Dalcin    Input Parameters:
3051618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3052618ce8baSLisandro Dalcin 
3053618ce8baSLisandro Dalcin    Output Parameter:
3054618ce8baSLisandro Dalcin .  maxtime - final time to step to
3055618ce8baSLisandro Dalcin 
3056618ce8baSLisandro Dalcin    Level: advanced
3057618ce8baSLisandro Dalcin 
3058618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3059618ce8baSLisandro Dalcin @*/
3060618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3061618ce8baSLisandro Dalcin {
3062618ce8baSLisandro Dalcin   PetscFunctionBegin;
3063618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3064618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3065618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3066618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3067618ce8baSLisandro Dalcin }
3068618ce8baSLisandro Dalcin 
3069618ce8baSLisandro Dalcin /*@
3070aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3071edc382c3SSatish Balay 
3072edc382c3SSatish Balay    Level: deprecated
3073edc382c3SSatish Balay 
3074aaa6c58dSLisandro Dalcin @*/
3075aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3076aaa6c58dSLisandro Dalcin {
3077aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3078aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3079aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3080aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3081aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3082aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3083aaa6c58dSLisandro Dalcin }
3084aaa6c58dSLisandro Dalcin 
3085aaa6c58dSLisandro Dalcin /*@
308619eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3087edc382c3SSatish Balay 
3088edc382c3SSatish Balay    Level: deprecated
3089edc382c3SSatish Balay 
3090adb62b0dSMatthew Knepley @*/
30917087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3092adb62b0dSMatthew Knepley {
3093adb62b0dSMatthew Knepley   PetscFunctionBegin;
30940700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3095abc0a331SBarry Smith   if (maxsteps) {
30964482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3097adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3098adb62b0dSMatthew Knepley   }
3099abc0a331SBarry Smith   if (maxtime) {
31004482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3101adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3102adb62b0dSMatthew Knepley   }
3103adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3104adb62b0dSMatthew Knepley }
3105adb62b0dSMatthew Knepley 
3106d763cef2SBarry Smith /*@
310719eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3108edc382c3SSatish Balay 
3109edc382c3SSatish Balay    Level: deprecated
3110edc382c3SSatish Balay 
3111d763cef2SBarry Smith @*/
31127087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3113d763cef2SBarry Smith {
3114d763cef2SBarry Smith   PetscFunctionBegin;
31150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3116c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3117c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
311839b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
311939b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3120d763cef2SBarry Smith   PetscFunctionReturn(0);
3121d763cef2SBarry Smith }
3122d763cef2SBarry Smith 
3123d763cef2SBarry Smith /*@
31245c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3125edc382c3SSatish Balay 
3126edc382c3SSatish Balay    Level: deprecated
3127edc382c3SSatish Balay 
31285c5f5948SLisandro Dalcin @*/
3129e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31305c5f5948SLisandro Dalcin 
313119eac22cSLisandro Dalcin /*@
31324f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3133edc382c3SSatish Balay 
3134edc382c3SSatish Balay    Level: deprecated
3135edc382c3SSatish Balay 
31364f4e0956SLisandro Dalcin @*/
31374f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31384f4e0956SLisandro Dalcin 
31394f4e0956SLisandro Dalcin /*@
3140d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3141d763cef2SBarry Smith    for use by the TS routines.
3142d763cef2SBarry Smith 
3143d083f849SBarry Smith    Logically Collective on TS
3144d763cef2SBarry Smith 
3145d763cef2SBarry Smith    Input Parameters:
3146d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31470910c330SBarry Smith -  u - the solution vector
3148d763cef2SBarry Smith 
3149d763cef2SBarry Smith    Level: beginner
3150d763cef2SBarry Smith 
31510ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3152d763cef2SBarry Smith @*/
31530910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3154d763cef2SBarry Smith {
31558737fe31SLisandro Dalcin   PetscErrorCode ierr;
3156496e6a7aSJed Brown   DM             dm;
31578737fe31SLisandro Dalcin 
3158d763cef2SBarry Smith   PetscFunctionBegin;
31590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31600910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31610910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31626bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31630910c330SBarry Smith   ts->vec_sol = u;
3164bbd56ea5SKarl Rupp 
3165496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
31660910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3167d763cef2SBarry Smith   PetscFunctionReturn(0);
3168d763cef2SBarry Smith }
3169d763cef2SBarry Smith 
3170ac226902SBarry Smith /*@C
3171000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31723f2090d5SJed Brown   called once at the beginning of each time step.
3173000e7ae3SMatthew Knepley 
31743f9fe445SBarry Smith   Logically Collective on TS
3175000e7ae3SMatthew Knepley 
3176000e7ae3SMatthew Knepley   Input Parameters:
3177000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3178000e7ae3SMatthew Knepley - func - The function
3179000e7ae3SMatthew Knepley 
3180000e7ae3SMatthew Knepley   Calling sequence of func:
31816bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3182000e7ae3SMatthew Knepley 
3183000e7ae3SMatthew Knepley   Level: intermediate
3184000e7ae3SMatthew Knepley 
3185dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3186000e7ae3SMatthew Knepley @*/
31877087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3188000e7ae3SMatthew Knepley {
3189000e7ae3SMatthew Knepley   PetscFunctionBegin;
31900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3191ae60f76fSBarry Smith   ts->prestep = func;
3192000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3193000e7ae3SMatthew Knepley }
3194000e7ae3SMatthew Knepley 
319509ee8438SJed Brown /*@
31963f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31973f2090d5SJed Brown 
31983f2090d5SJed Brown   Collective on TS
31993f2090d5SJed Brown 
32003f2090d5SJed Brown   Input Parameters:
32013f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32023f2090d5SJed Brown 
32033f2090d5SJed Brown   Notes:
32043f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32053f2090d5SJed Brown   so most users would not generally call this routine themselves.
32063f2090d5SJed Brown 
32073f2090d5SJed Brown   Level: developer
32083f2090d5SJed Brown 
32099be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32103f2090d5SJed Brown @*/
32117087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32123f2090d5SJed Brown {
32133f2090d5SJed Brown   PetscErrorCode ierr;
32143f2090d5SJed Brown 
32153f2090d5SJed Brown   PetscFunctionBegin;
32160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3217ae60f76fSBarry Smith   if (ts->prestep) {
32185efd42a4SStefano Zampini     Vec              U;
32195efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32205efd42a4SStefano Zampini 
32215efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32225efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3223ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32245efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3225dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3226312ce896SJed Brown   }
32273f2090d5SJed Brown   PetscFunctionReturn(0);
32283f2090d5SJed Brown }
32293f2090d5SJed Brown 
3230b8123daeSJed Brown /*@C
3231b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3232b8123daeSJed Brown   called once at the beginning of each stage.
3233b8123daeSJed Brown 
3234b8123daeSJed Brown   Logically Collective on TS
3235b8123daeSJed Brown 
3236b8123daeSJed Brown   Input Parameters:
3237b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3238b8123daeSJed Brown - func - The function
3239b8123daeSJed Brown 
3240b8123daeSJed Brown   Calling sequence of func:
3241b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3242b8123daeSJed Brown 
3243b8123daeSJed Brown   Level: intermediate
3244b8123daeSJed Brown 
3245b8123daeSJed Brown   Note:
3246b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
324780275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3248b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3249b8123daeSJed Brown 
32509be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3251b8123daeSJed Brown @*/
3252b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3253b8123daeSJed Brown {
3254b8123daeSJed Brown   PetscFunctionBegin;
3255b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3256ae60f76fSBarry Smith   ts->prestage = func;
3257b8123daeSJed Brown   PetscFunctionReturn(0);
3258b8123daeSJed Brown }
3259b8123daeSJed Brown 
32609be3e283SDebojyoti Ghosh /*@C
32619be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32629be3e283SDebojyoti Ghosh   called once at the end of each stage.
32639be3e283SDebojyoti Ghosh 
32649be3e283SDebojyoti Ghosh   Logically Collective on TS
32659be3e283SDebojyoti Ghosh 
32669be3e283SDebojyoti Ghosh   Input Parameters:
32679be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32689be3e283SDebojyoti Ghosh - func - The function
32699be3e283SDebojyoti Ghosh 
32709be3e283SDebojyoti Ghosh   Calling sequence of func:
32719be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32729be3e283SDebojyoti Ghosh 
32739be3e283SDebojyoti Ghosh   Level: intermediate
32749be3e283SDebojyoti Ghosh 
32759be3e283SDebojyoti Ghosh   Note:
32769be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
327780275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32789be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32799be3e283SDebojyoti Ghosh 
32809be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32819be3e283SDebojyoti Ghosh @*/
32829be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32839be3e283SDebojyoti Ghosh {
32849be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32859be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32869be3e283SDebojyoti Ghosh   ts->poststage = func;
32879be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32889be3e283SDebojyoti Ghosh }
32899be3e283SDebojyoti Ghosh 
3290c688d042SShri Abhyankar /*@C
3291c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3292c688d042SShri Abhyankar   called once at the end of each step evaluation.
3293c688d042SShri Abhyankar 
3294c688d042SShri Abhyankar   Logically Collective on TS
3295c688d042SShri Abhyankar 
3296c688d042SShri Abhyankar   Input Parameters:
3297c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3298c688d042SShri Abhyankar - func - The function
3299c688d042SShri Abhyankar 
3300c688d042SShri Abhyankar   Calling sequence of func:
3301c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3302c688d042SShri Abhyankar 
3303c688d042SShri Abhyankar   Level: intermediate
3304c688d042SShri Abhyankar 
3305c688d042SShri Abhyankar   Note:
33061785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33071785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3308e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3309e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3310e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3311c688d042SShri Abhyankar 
3312c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3313c688d042SShri Abhyankar @*/
3314c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3315c688d042SShri Abhyankar {
3316c688d042SShri Abhyankar   PetscFunctionBegin;
3317c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3318c688d042SShri Abhyankar   ts->postevaluate = func;
3319c688d042SShri Abhyankar   PetscFunctionReturn(0);
3320c688d042SShri Abhyankar }
3321c688d042SShri Abhyankar 
3322b8123daeSJed Brown /*@
3323b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3324b8123daeSJed Brown 
3325b8123daeSJed Brown   Collective on TS
3326b8123daeSJed Brown 
3327b8123daeSJed Brown   Input Parameters:
3328b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33299be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3330b8123daeSJed Brown 
3331b8123daeSJed Brown   Notes:
3332b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3333b8123daeSJed Brown   most users would not generally call this routine themselves.
3334b8123daeSJed Brown 
3335b8123daeSJed Brown   Level: developer
3336b8123daeSJed Brown 
33379be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3338b8123daeSJed Brown @*/
3339b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3340b8123daeSJed Brown {
3341b8123daeSJed Brown   PetscFunctionBegin;
3342b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3343ae60f76fSBarry Smith   if (ts->prestage) {
3344ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3345b8123daeSJed Brown   }
3346b8123daeSJed Brown   PetscFunctionReturn(0);
3347b8123daeSJed Brown }
3348b8123daeSJed Brown 
33499be3e283SDebojyoti Ghosh /*@
33509be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33519be3e283SDebojyoti Ghosh 
33529be3e283SDebojyoti Ghosh   Collective on TS
33539be3e283SDebojyoti Ghosh 
33549be3e283SDebojyoti Ghosh   Input Parameters:
33559be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33569be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33579be3e283SDebojyoti Ghosh   stageindex  - Stage number
33589be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33599be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33609be3e283SDebojyoti Ghosh 
33619be3e283SDebojyoti Ghosh   Notes:
33629be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33639be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33649be3e283SDebojyoti Ghosh 
33659be3e283SDebojyoti Ghosh   Level: developer
33669be3e283SDebojyoti Ghosh 
33679be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33689be3e283SDebojyoti Ghosh @*/
33699be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33709be3e283SDebojyoti Ghosh {
33719be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33729be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33734beae5d8SLisandro Dalcin   if (ts->poststage) {
33749be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33759be3e283SDebojyoti Ghosh   }
33769be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33779be3e283SDebojyoti Ghosh }
33789be3e283SDebojyoti Ghosh 
3379c688d042SShri Abhyankar /*@
3380c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3381c688d042SShri Abhyankar 
3382c688d042SShri Abhyankar   Collective on TS
3383c688d042SShri Abhyankar 
3384c688d042SShri Abhyankar   Input Parameters:
3385c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3386c688d042SShri Abhyankar 
3387c688d042SShri Abhyankar   Notes:
3388c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3389c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3390c688d042SShri Abhyankar 
3391c688d042SShri Abhyankar   Level: developer
3392c688d042SShri Abhyankar 
3393c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3394c688d042SShri Abhyankar @*/
3395c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3396c688d042SShri Abhyankar {
3397c688d042SShri Abhyankar   PetscErrorCode ierr;
3398c688d042SShri Abhyankar 
3399c688d042SShri Abhyankar   PetscFunctionBegin;
3400c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3401c688d042SShri Abhyankar   if (ts->postevaluate) {
3402dcb233daSLisandro Dalcin     Vec              U;
3403dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3404dcb233daSLisandro Dalcin 
3405dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3406dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3407c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3408dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3409dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3410c688d042SShri Abhyankar   }
3411c688d042SShri Abhyankar   PetscFunctionReturn(0);
3412c688d042SShri Abhyankar }
3413c688d042SShri Abhyankar 
3414ac226902SBarry Smith /*@C
3415000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34163f2090d5SJed Brown   called once at the end of each time step.
3417000e7ae3SMatthew Knepley 
34183f9fe445SBarry Smith   Logically Collective on TS
3419000e7ae3SMatthew Knepley 
3420000e7ae3SMatthew Knepley   Input Parameters:
3421000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3422000e7ae3SMatthew Knepley - func - The function
3423000e7ae3SMatthew Knepley 
3424000e7ae3SMatthew Knepley   Calling sequence of func:
3425b8123daeSJed Brown $ func (TS ts);
3426000e7ae3SMatthew Knepley 
34271785ff2aSShri Abhyankar   Notes:
34281785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34291785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34301785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34311785ff2aSShri Abhyankar 
3432000e7ae3SMatthew Knepley   Level: intermediate
3433000e7ae3SMatthew Knepley 
3434dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3435000e7ae3SMatthew Knepley @*/
34367087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3437000e7ae3SMatthew Knepley {
3438000e7ae3SMatthew Knepley   PetscFunctionBegin;
34390700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3440ae60f76fSBarry Smith   ts->poststep = func;
3441000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3442000e7ae3SMatthew Knepley }
3443000e7ae3SMatthew Knepley 
344409ee8438SJed Brown /*@
34453f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34463f2090d5SJed Brown 
34473f2090d5SJed Brown   Collective on TS
34483f2090d5SJed Brown 
34493f2090d5SJed Brown   Input Parameters:
34503f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34513f2090d5SJed Brown 
34523f2090d5SJed Brown   Notes:
34533f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34543f2090d5SJed Brown   so most users would not generally call this routine themselves.
34553f2090d5SJed Brown 
34563f2090d5SJed Brown   Level: developer
34573f2090d5SJed Brown 
34583f2090d5SJed Brown @*/
34597087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34603f2090d5SJed Brown {
34613f2090d5SJed Brown   PetscErrorCode ierr;
34623f2090d5SJed Brown 
34633f2090d5SJed Brown   PetscFunctionBegin;
34640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3465ae60f76fSBarry Smith   if (ts->poststep) {
34665efd42a4SStefano Zampini     Vec              U;
34675efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34685efd42a4SStefano Zampini 
34695efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
34705efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3471ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
34725efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3473dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
347472ac3e02SJed Brown   }
34753f2090d5SJed Brown   PetscFunctionReturn(0);
34763f2090d5SJed Brown }
34773f2090d5SJed Brown 
3478d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3479d763cef2SBarry Smith 
3480d763cef2SBarry Smith /*@C
3481a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3482d763cef2SBarry Smith    timestep to display the iteration's  progress.
3483d763cef2SBarry Smith 
34843f9fe445SBarry Smith    Logically Collective on TS
3485d763cef2SBarry Smith 
3486d763cef2SBarry Smith    Input Parameters:
3487d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3488e213d8f1SJed Brown .  monitor - monitoring routine
3489329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34900298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3491b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34920298fd71SBarry Smith           (may be NULL)
3493d763cef2SBarry Smith 
3494e213d8f1SJed Brown    Calling sequence of monitor:
3495dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3496d763cef2SBarry Smith 
3497d763cef2SBarry Smith +    ts - the TS context
349863e21af5SBarry 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)
34991f06c33eSBarry Smith .    time - current time
35000910c330SBarry Smith .    u - current iterate
3501d763cef2SBarry Smith -    mctx - [optional] monitoring context
3502d763cef2SBarry Smith 
3503d763cef2SBarry Smith    Notes:
3504d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3505d763cef2SBarry Smith    already has been loaded.
3506d763cef2SBarry Smith 
350795452b02SPatrick Sanan    Fortran Notes:
350895452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3509025f1a04SBarry Smith 
3510d763cef2SBarry Smith    Level: intermediate
3511d763cef2SBarry Smith 
3512a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3513d763cef2SBarry Smith @*/
3514c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3515d763cef2SBarry Smith {
351678064530SBarry Smith   PetscErrorCode ierr;
351778064530SBarry Smith   PetscInt       i;
351878064530SBarry Smith   PetscBool      identical;
351978064530SBarry Smith 
3520d763cef2SBarry Smith   PetscFunctionBegin;
35210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
352278064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
352378064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
352478064530SBarry Smith     if (identical) PetscFunctionReturn(0);
352578064530SBarry Smith   }
352617186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3527d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35288704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3529d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3530d763cef2SBarry Smith   PetscFunctionReturn(0);
3531d763cef2SBarry Smith }
3532d763cef2SBarry Smith 
3533d763cef2SBarry Smith /*@C
3534a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3535d763cef2SBarry Smith 
35363f9fe445SBarry Smith    Logically Collective on TS
3537d763cef2SBarry Smith 
3538d763cef2SBarry Smith    Input Parameters:
3539d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3540d763cef2SBarry Smith 
3541d763cef2SBarry Smith    Notes:
3542d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3543d763cef2SBarry Smith 
3544d763cef2SBarry Smith    Level: intermediate
3545d763cef2SBarry Smith 
3546a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3547d763cef2SBarry Smith @*/
35487087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3549d763cef2SBarry Smith {
3550d952e501SBarry Smith   PetscErrorCode ierr;
3551d952e501SBarry Smith   PetscInt       i;
3552d952e501SBarry Smith 
3553d763cef2SBarry Smith   PetscFunctionBegin;
35540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3555d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35568704b422SBarry Smith     if (ts->monitordestroy[i]) {
35578704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3558d952e501SBarry Smith     }
3559d952e501SBarry Smith   }
3560d763cef2SBarry Smith   ts->numbermonitors = 0;
3561d763cef2SBarry Smith   PetscFunctionReturn(0);
3562d763cef2SBarry Smith }
3563d763cef2SBarry Smith 
3564721cd6eeSBarry Smith /*@C
3565721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35665516499fSSatish Balay 
35675516499fSSatish Balay    Level: intermediate
356841251cbbSSatish Balay 
356963e21af5SBarry Smith .seealso:  TSMonitorSet()
357041251cbbSSatish Balay @*/
3571721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3572d763cef2SBarry Smith {
3573dfbe8321SBarry Smith   PetscErrorCode ierr;
3574721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
357541aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3576d132466eSBarry Smith 
3577d763cef2SBarry Smith   PetscFunctionBegin;
35784d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
357941aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
358041aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3581721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
358241aca3d6SBarry Smith   if (iascii) {
3583649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
358463e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
358563e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
358663e21af5SBarry Smith     } else {
35878392e04aSShri 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);
358863e21af5SBarry Smith     }
3589649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
359041aca3d6SBarry Smith   } else if (ibinary) {
359141aca3d6SBarry Smith     PetscMPIInt rank;
359241aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
359341aca3d6SBarry Smith     if (!rank) {
3594450a797fSBarry Smith       PetscBool skipHeader;
3595450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3596450a797fSBarry Smith 
3597450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3598450a797fSBarry Smith       if (!skipHeader) {
3599f253e43cSLisandro Dalcin          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
3600450a797fSBarry Smith        }
360141aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
360241aca3d6SBarry Smith     } else {
360341aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
360441aca3d6SBarry Smith     }
360541aca3d6SBarry Smith   }
3606721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3607d763cef2SBarry Smith   PetscFunctionReturn(0);
3608d763cef2SBarry Smith }
3609d763cef2SBarry Smith 
3610cc9c3a59SBarry Smith /*@C
3611cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3612cc9c3a59SBarry Smith 
3613cc9c3a59SBarry Smith    Level: intermediate
3614cc9c3a59SBarry Smith 
3615cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3616cc9c3a59SBarry Smith @*/
3617cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3618cc9c3a59SBarry Smith {
3619cc9c3a59SBarry Smith   PetscErrorCode ierr;
3620cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3621cc9c3a59SBarry Smith   PetscBool      iascii;
3622cc9c3a59SBarry Smith   PetscReal      max,min;
3623cc9c3a59SBarry Smith 
3624cc9c3a59SBarry Smith 
3625cc9c3a59SBarry Smith   PetscFunctionBegin;
3626cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3627cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3628cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3629cc9c3a59SBarry Smith   if (iascii) {
3630cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3631cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3632cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
36335132926bSBarry 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);
3634cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3635cc9c3a59SBarry Smith   }
3636cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3637cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3638cc9c3a59SBarry Smith }
3639cc9c3a59SBarry Smith 
3640cd652676SJed Brown /*@
3641cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3642cd652676SJed Brown 
3643cd652676SJed Brown    Collective on TS
3644cd652676SJed Brown 
3645cd652676SJed Brown    Input Argument:
3646cd652676SJed Brown +  ts - time stepping context
3647cd652676SJed Brown -  t - time to interpolate to
3648cd652676SJed Brown 
3649cd652676SJed Brown    Output Argument:
36500910c330SBarry Smith .  U - state at given time
3651cd652676SJed Brown 
3652cd652676SJed Brown    Level: intermediate
3653cd652676SJed Brown 
3654cd652676SJed Brown    Developer Notes:
3655cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3656cd652676SJed Brown 
3657874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3658cd652676SJed Brown @*/
36590910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3660cd652676SJed Brown {
3661cd652676SJed Brown   PetscErrorCode ierr;
3662cd652676SJed Brown 
3663cd652676SJed Brown   PetscFunctionBegin;
3664cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3665b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3666be5899b3SLisandro 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);
3667ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36680910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3669cd652676SJed Brown   PetscFunctionReturn(0);
3670cd652676SJed Brown }
3671cd652676SJed Brown 
3672d763cef2SBarry Smith /*@
36736d9e5789SSean Farley    TSStep - Steps one time step
3674d763cef2SBarry Smith 
3675d763cef2SBarry Smith    Collective on TS
3676d763cef2SBarry Smith 
3677d763cef2SBarry Smith    Input Parameter:
3678d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3679d763cef2SBarry Smith 
368027829d71SBarry Smith    Level: developer
3681d763cef2SBarry Smith 
3682b8123daeSJed Brown    Notes:
368327829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
368427829d71SBarry Smith 
3685b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3686b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3687b8123daeSJed Brown 
368819eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
368919eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
369025cb2221SBarry Smith 
36919be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3692d763cef2SBarry Smith @*/
3693193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3694d763cef2SBarry Smith {
3695dfbe8321SBarry Smith   PetscErrorCode   ierr;
3696fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3697be5899b3SLisandro Dalcin   PetscReal        ptime;
3698d763cef2SBarry Smith 
3699d763cef2SBarry Smith   PetscFunctionBegin;
37000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3701f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3702fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3703f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3704f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3705f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3706f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3707f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3708fffbeea8SBarry Smith 
3709d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
371068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3711d405a339SMatthew Knepley 
3712fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3713ef85077eSLisandro 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>");
3714a6772fa2SLisandro 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()");
3715be5899b3SLisandro 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");
3716a6772fa2SLisandro Dalcin 
3717be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3718be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37192808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3720fc8dbba5SLisandro Dalcin 
3721d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3722193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3723d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3724fc8dbba5SLisandro Dalcin 
3725fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3726be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
37272808aa04SLisandro Dalcin     ts->steps++;
3728be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
372928d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3730d2daff3dSHong Zhang   }
3731fc8dbba5SLisandro Dalcin 
3732fc8dbba5SLisandro Dalcin   if (!ts->reason) {
373308c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
373408c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
373508c7845fSBarry Smith   }
3736fc8dbba5SLisandro Dalcin 
3737fc8dbba5SLisandro 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]);
3738fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
373908c7845fSBarry Smith   PetscFunctionReturn(0);
374008c7845fSBarry Smith }
374108c7845fSBarry Smith 
374208c7845fSBarry Smith /*@
37437cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37447cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37457cbde773SLisandro Dalcin 
37467cbde773SLisandro Dalcin    Collective on TS
37477cbde773SLisandro Dalcin 
37487cbde773SLisandro Dalcin    Input Arguments:
37497cbde773SLisandro Dalcin +  ts - time stepping context
37507cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37517cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37527cbde773SLisandro Dalcin 
37537cbde773SLisandro Dalcin    Output Arguments:
37547cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37557cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37567cbde773SLisandro Dalcin 
37577cbde773SLisandro Dalcin    Level: advanced
37587cbde773SLisandro Dalcin 
37597cbde773SLisandro Dalcin    Notes:
37607cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37617cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37627cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37637cbde773SLisandro Dalcin 
37647cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37657cbde773SLisandro Dalcin @*/
37667cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37677cbde773SLisandro Dalcin {
37687cbde773SLisandro Dalcin   PetscErrorCode ierr;
37697cbde773SLisandro Dalcin 
37707cbde773SLisandro Dalcin   PetscFunctionBegin;
37717cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37727cbde773SLisandro Dalcin   PetscValidType(ts,1);
37737cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37747cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37757cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37767cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37777cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37787cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37797cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37807cbde773SLisandro Dalcin   PetscFunctionReturn(0);
37817cbde773SLisandro Dalcin }
37827cbde773SLisandro Dalcin 
378305175c85SJed Brown /*@
378405175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
378505175c85SJed Brown 
37861c3436cfSJed Brown    Collective on TS
378705175c85SJed Brown 
378805175c85SJed Brown    Input Arguments:
37891c3436cfSJed Brown +  ts - time stepping context
37901c3436cfSJed Brown .  order - desired order of accuracy
37910298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
379205175c85SJed Brown 
379305175c85SJed Brown    Output Arguments:
37940910c330SBarry Smith .  U - state at the end of the current step
379505175c85SJed Brown 
379605175c85SJed Brown    Level: advanced
379705175c85SJed Brown 
3798108c343cSJed Brown    Notes:
3799108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3800108c343cSJed 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.
3801108c343cSJed Brown 
38021c3436cfSJed Brown .seealso: TSStep(), TSAdapt
380305175c85SJed Brown @*/
38040910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
380505175c85SJed Brown {
380605175c85SJed Brown   PetscErrorCode ierr;
380705175c85SJed Brown 
380805175c85SJed Brown   PetscFunctionBegin;
380905175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
381005175c85SJed Brown   PetscValidType(ts,1);
38110910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3812ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38130910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
381405175c85SJed Brown   PetscFunctionReturn(0);
381505175c85SJed Brown }
381605175c85SJed Brown 
3817aad739acSMatthew G. Knepley /*@C
38182e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3819aad739acSMatthew G. Knepley 
3820aad739acSMatthew G. Knepley   Not collective
3821aad739acSMatthew G. Knepley 
3822aad739acSMatthew G. Knepley   Input Argument:
3823aad739acSMatthew G. Knepley . ts        - time stepping context
3824aad739acSMatthew G. Knepley 
3825aad739acSMatthew G. Knepley   Output Argument:
38262e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3827aad739acSMatthew G. Knepley 
3828aad739acSMatthew G. Knepley    Level: advanced
3829aad739acSMatthew G. Knepley 
3830aad739acSMatthew G. Knepley    Notes:
3831aad739acSMatthew G. Knepley    The calling sequence for the function is
38322e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3833aad739acSMatthew G. Knepley $ ts - The timestepping context
38342e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3835aad739acSMatthew G. Knepley 
38362e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3837aad739acSMatthew G. Knepley @*/
38382e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3839aad739acSMatthew G. Knepley {
3840aad739acSMatthew G. Knepley   PetscFunctionBegin;
3841aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38422e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38432e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3844aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3845aad739acSMatthew G. Knepley }
3846aad739acSMatthew G. Knepley 
3847aad739acSMatthew G. Knepley /*@C
38482e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3849aad739acSMatthew G. Knepley 
3850aad739acSMatthew G. Knepley   Logically collective on ts
3851aad739acSMatthew G. Knepley 
3852aad739acSMatthew G. Knepley   Input Arguments:
3853aad739acSMatthew G. Knepley + ts        - time stepping context
38542e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3855aad739acSMatthew G. Knepley 
3856aad739acSMatthew G. Knepley   Level: advanced
3857aad739acSMatthew G. Knepley 
3858a96d6ef6SBarry Smith   Calling sequence for initCondition:
3859a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3860a96d6ef6SBarry Smith 
3861a96d6ef6SBarry Smith + ts - The timestepping context
3862a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3863aad739acSMatthew G. Knepley 
38642e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3865aad739acSMatthew G. Knepley @*/
38662e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3867aad739acSMatthew G. Knepley {
3868aad739acSMatthew G. Knepley   PetscFunctionBegin;
3869aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38702e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
38712e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3872aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3873aad739acSMatthew G. Knepley }
3874aad739acSMatthew G. Knepley 
3875aad739acSMatthew G. Knepley /*@
38762e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3877aad739acSMatthew G. Knepley 
3878aad739acSMatthew G. Knepley   Collective on ts
3879aad739acSMatthew G. Knepley 
3880aad739acSMatthew G. Knepley   Input Arguments:
3881aad739acSMatthew G. Knepley + ts - time stepping context
38822e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3883aad739acSMatthew G. Knepley 
3884aad739acSMatthew G. Knepley   Level: advanced
3885aad739acSMatthew G. Knepley 
38862e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3887aad739acSMatthew G. Knepley @*/
38882e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3889aad739acSMatthew G. Knepley {
3890aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3891aad739acSMatthew G. Knepley 
3892aad739acSMatthew G. Knepley   PetscFunctionBegin;
3893aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3894aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
38952e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3896aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3897aad739acSMatthew G. Knepley }
3898aad739acSMatthew G. Knepley 
3899aad739acSMatthew G. Knepley /*@C
3900aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3901aad739acSMatthew G. Knepley 
3902aad739acSMatthew G. Knepley   Not collective
3903aad739acSMatthew G. Knepley 
3904aad739acSMatthew G. Knepley   Input Argument:
3905aad739acSMatthew G. Knepley . ts         - time stepping context
3906aad739acSMatthew G. Knepley 
3907aad739acSMatthew G. Knepley   Output Argument:
3908aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3909aad739acSMatthew G. Knepley 
3910aad739acSMatthew G. Knepley   Level: advanced
3911aad739acSMatthew G. Knepley 
3912a96d6ef6SBarry Smith   Calling sequence for exactError:
3913a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3914a96d6ef6SBarry Smith 
3915a96d6ef6SBarry Smith + ts - The timestepping context
3916a96d6ef6SBarry Smith . u  - The approximate solution vector
3917a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3918aad739acSMatthew G. Knepley 
3919aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3920aad739acSMatthew G. Knepley @*/
3921aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3922aad739acSMatthew G. Knepley {
3923aad739acSMatthew G. Knepley   PetscFunctionBegin;
3924aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3925aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3926aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3927aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3928aad739acSMatthew G. Knepley }
3929aad739acSMatthew G. Knepley 
3930aad739acSMatthew G. Knepley /*@C
3931aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3932aad739acSMatthew G. Knepley 
3933aad739acSMatthew G. Knepley   Logically collective on ts
3934aad739acSMatthew G. Knepley 
3935aad739acSMatthew G. Knepley   Input Arguments:
3936aad739acSMatthew G. Knepley + ts         - time stepping context
3937aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3938aad739acSMatthew G. Knepley 
3939aad739acSMatthew G. Knepley   Level: advanced
3940aad739acSMatthew G. Knepley 
3941a96d6ef6SBarry Smith   Calling sequence for exactError:
3942a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3943a96d6ef6SBarry Smith 
3944a96d6ef6SBarry Smith + ts - The timestepping context
3945a96d6ef6SBarry Smith . u  - The approximate solution vector
3946a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3947aad739acSMatthew G. Knepley 
3948aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3949aad739acSMatthew G. Knepley @*/
3950aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3951aad739acSMatthew G. Knepley {
3952aad739acSMatthew G. Knepley   PetscFunctionBegin;
3953aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3954f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3955aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3956aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3957aad739acSMatthew G. Knepley }
3958aad739acSMatthew G. Knepley 
3959aad739acSMatthew G. Knepley /*@
3960aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3961aad739acSMatthew G. Knepley 
3962aad739acSMatthew G. Knepley   Collective on ts
3963aad739acSMatthew G. Knepley 
3964aad739acSMatthew G. Knepley   Input Arguments:
3965aad739acSMatthew G. Knepley + ts - time stepping context
3966aad739acSMatthew G. Knepley . u  - The approximate solution
3967aad739acSMatthew G. Knepley - e  - The Vec used to store the error
3968aad739acSMatthew G. Knepley 
3969aad739acSMatthew G. Knepley   Level: advanced
3970aad739acSMatthew G. Knepley 
39712e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3972aad739acSMatthew G. Knepley @*/
3973aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
3974aad739acSMatthew G. Knepley {
3975aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3976aad739acSMatthew G. Knepley 
3977aad739acSMatthew G. Knepley   PetscFunctionBegin;
3978aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3979aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
3980aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
3981aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
3982aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3983aad739acSMatthew G. Knepley }
3984aad739acSMatthew G. Knepley 
3985b1cb36f3SHong Zhang /*@
39866a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
39876a4d4014SLisandro Dalcin 
39886a4d4014SLisandro Dalcin    Collective on TS
39896a4d4014SLisandro Dalcin 
39906a4d4014SLisandro Dalcin    Input Parameter:
39916a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
399263e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
399363e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
39945a3a76d0SJed Brown 
39956a4d4014SLisandro Dalcin    Level: beginner
39966a4d4014SLisandro Dalcin 
39975a3a76d0SJed Brown    Notes:
39985a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
39995a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40005a3a76d0SJed Brown    stepped over the final time.
40015a3a76d0SJed Brown 
400263e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
40036a4d4014SLisandro Dalcin @*/
4004cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
40056a4d4014SLisandro Dalcin {
4006b06615a5SLisandro Dalcin   Vec               solution;
40076a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
4008f22f69f0SBarry Smith 
40096a4d4014SLisandro Dalcin   PetscFunctionBegin;
40100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4011f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4012303a5415SBarry Smith 
4013303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
4014ee41a567SStefano 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 */
40150910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4016b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4017b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4018b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40195a3a76d0SJed Brown     }
40200910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4021715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4022bbd56ea5SKarl Rupp   } else if (u) {
40230910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40245a3a76d0SJed Brown   }
4025b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
402668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4027a6772fa2SLisandro Dalcin 
4028ef85077eSLisandro 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>");
4029a6772fa2SLisandro 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()");
4030a6772fa2SLisandro 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");
4031a6772fa2SLisandro Dalcin 
4032715f1b00SHong Zhang   if (ts->forward_solve) {
4033715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4034715f1b00SHong Zhang   }
4035715f1b00SHong Zhang 
4036e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4037715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4038e7069c78SShri      TSTrajectory to incorrectly save the output files
4039e7069c78SShri   */
4040715f1b00SHong Zhang   /* reset time step and iteration counters */
40412808aa04SLisandro Dalcin   if (!ts->steps) {
40425ef26d82SJed Brown     ts->ksp_its           = 0;
40435ef26d82SJed Brown     ts->snes_its          = 0;
4044c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4045c610991cSLisandro Dalcin     ts->reject            = 0;
40462808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40472808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40487d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
40492808aa04SLisandro Dalcin   }
4050e97c63d7SStefano Zampini 
4051e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4052e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4053e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4054e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4055e97c63d7SStefano Zampini 
4056e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4057e97c63d7SStefano Zampini   }
4058193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4059193ac0bcSJed Brown 
4060900f6b5bSMatthew G. Knepley   {
4061900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4062900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4063900f6b5bSMatthew G. Knepley     PetscBool         flg;
4064900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4065900f6b5bSMatthew G. Knepley 
4066900f6b5bSMatthew G. Knepley     if (!incall) {
4067900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4068900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4069900f6b5bSMatthew G. Knepley       if (flg) {
4070900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4071900f6b5bSMatthew G. Knepley         DM           dm;
4072900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4073900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4074f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4075900f6b5bSMatthew G. Knepley 
4076900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4077f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
4078900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4079900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4080900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4081900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4082f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
4083900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4084900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4085900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4086900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4087900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4088900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4089900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4090900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4091900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4092900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4093900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4094900f6b5bSMatthew G. Knepley       }
4095900f6b5bSMatthew G. Knepley     }
4096900f6b5bSMatthew G. Knepley   }
4097900f6b5bSMatthew G. Knepley 
4098ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4099f05ece33SBarry Smith 
4100193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4101193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4102a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4103cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4104a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4105be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4106db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4107db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4108e7069c78SShri 
41092808aa04SLisandro Dalcin     if (!ts->steps) {
41102808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41116427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41122808aa04SLisandro Dalcin     }
41136427ac75SLisandro Dalcin 
4114e1a7a14fSJed Brown     while (!ts->reason) {
41152808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41169687d888SLisandro Dalcin       if (!ts->steprollback) {
41179687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41189687d888SLisandro Dalcin       }
4119193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4120f3b1f45cSBarry Smith       if (ts->testjacobian) {
4121f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4122f3b1f45cSBarry Smith       }
4123f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4124f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4125f3b1f45cSBarry Smith       }
4126cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
41277b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4128b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
41297b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4130b1cb36f3SHong Zhang       }
413158818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41327b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4133715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
41347b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4135715f1b00SHong Zhang       }
413610b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4137e783b05fSHong 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. */
413858818c2dSLisandro Dalcin       if (ts->steprollback) {
413958818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
414058818c2dSLisandro Dalcin       }
4141e783b05fSHong Zhang       if (!ts->steprollback) {
41422808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41431eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4144aeb4809dSShri Abhyankar       }
4145193ac0bcSJed Brown     }
41462808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41476427ac75SLisandro Dalcin 
414849354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41490910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4150cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4151b06615a5SLisandro Dalcin       solution = u;
415263e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41530574a7fbSJed Brown     } else {
4154ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4155cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4156b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41570574a7fbSJed Brown     }
4158193ac0bcSJed Brown   }
4159d2daff3dSHong Zhang 
4160ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
416163e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
416256f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4163ef222394SBarry Smith   if (ts->adjoint_solve) {
4164ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41652b0a91c0SBarry Smith   }
41666a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41676a4d4014SLisandro Dalcin }
41686a4d4014SLisandro Dalcin 
41697db568b7SBarry Smith /*@C
4170228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4171228d79bcSJed Brown 
4172228d79bcSJed Brown    Collective on TS
4173228d79bcSJed Brown 
4174228d79bcSJed Brown    Input Parameters:
4175228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4176228d79bcSJed Brown .  step - step number that has just completed
4177228d79bcSJed Brown .  ptime - model time of the state
41780910c330SBarry Smith -  u - state at the current model time
4179228d79bcSJed Brown 
4180228d79bcSJed Brown    Notes:
41817db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
41827db568b7SBarry Smith    Users would almost never call this routine directly.
4183228d79bcSJed Brown 
418463e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
418563e21af5SBarry Smith 
41867db568b7SBarry Smith    Level: developer
4187228d79bcSJed Brown 
4188228d79bcSJed Brown @*/
41890910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4190d763cef2SBarry Smith {
4191d6152f81SLisandro Dalcin   DM             dm;
4192a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4193d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4194d763cef2SBarry Smith 
4195d763cef2SBarry Smith   PetscFunctionBegin;
4196b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4197b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4198d6152f81SLisandro Dalcin 
4199d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4200d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4201d6152f81SLisandro Dalcin 
42028860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4203d763cef2SBarry Smith   for (i=0; i<n; i++) {
42040910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4205d763cef2SBarry Smith   }
42068860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4207d763cef2SBarry Smith   PetscFunctionReturn(0);
4208d763cef2SBarry Smith }
4209d763cef2SBarry Smith 
4210d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4211d763cef2SBarry Smith /*@C
42127db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4213a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4214d763cef2SBarry Smith 
4215d763cef2SBarry Smith    Collective on TS
4216d763cef2SBarry Smith 
4217d763cef2SBarry Smith    Input Parameters:
4218d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4219d763cef2SBarry Smith .  label - the title to put in the title bar
42207c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4221a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4222a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4223d763cef2SBarry Smith 
4224d763cef2SBarry Smith    Output Parameter:
42250b039ecaSBarry Smith .  ctx - the context
4226d763cef2SBarry Smith 
4227d763cef2SBarry Smith    Options Database Key:
42284f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42298b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
42307db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42317db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42327db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42337db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4234b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4235d763cef2SBarry Smith 
4236d763cef2SBarry Smith    Notes:
4237a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4238d763cef2SBarry Smith 
42397db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42407db568b7SBarry Smith 
42417db568b7SBarry 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
42427db568b7SBarry 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
42437db568b7SBarry Smith    as the first argument.
42447db568b7SBarry Smith 
42457db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42467db568b7SBarry Smith 
4247d763cef2SBarry Smith    Level: intermediate
4248d763cef2SBarry Smith 
42497db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
42507db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
42517db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
42527db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
42537db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
42547c922b88SBarry Smith 
4255d763cef2SBarry Smith @*/
4256a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4257d763cef2SBarry Smith {
42587f52daa2SLisandro Dalcin   PetscDraw      draw;
4259dfbe8321SBarry Smith   PetscErrorCode ierr;
4260d763cef2SBarry Smith 
4261d763cef2SBarry Smith   PetscFunctionBegin;
4262b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
42637f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
42647f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
42657f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4266287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
42677f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4268a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4269d763cef2SBarry Smith   PetscFunctionReturn(0);
4270d763cef2SBarry Smith }
4271d763cef2SBarry Smith 
4272b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4273d763cef2SBarry Smith {
42740b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4275c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4276dfbe8321SBarry Smith   PetscErrorCode ierr;
4277d763cef2SBarry Smith 
4278d763cef2SBarry Smith   PetscFunctionBegin;
427963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4280b06615a5SLisandro Dalcin   if (!step) {
4281a9f9c1f6SBarry Smith     PetscDrawAxis axis;
42828b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4283a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
42848b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4285a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4286a9f9c1f6SBarry Smith   }
4287c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
42888b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
42890b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4290b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
42910b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
42926934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
42933923b477SBarry Smith   }
4294d763cef2SBarry Smith   PetscFunctionReturn(0);
4295d763cef2SBarry Smith }
4296d763cef2SBarry Smith 
4297d763cef2SBarry Smith /*@C
4298a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4299a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4300d763cef2SBarry Smith 
43010b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4302d763cef2SBarry Smith 
4303d763cef2SBarry Smith    Input Parameter:
43040b039ecaSBarry Smith .  ctx - the monitor context
4305d763cef2SBarry Smith 
4306d763cef2SBarry Smith    Level: intermediate
4307d763cef2SBarry Smith 
43084f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4309d763cef2SBarry Smith @*/
4310a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4311d763cef2SBarry Smith {
4312dfbe8321SBarry Smith   PetscErrorCode ierr;
4313d763cef2SBarry Smith 
4314d763cef2SBarry Smith   PetscFunctionBegin;
43157684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43167684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43177684fa3eSBarry Smith   }
43180b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
431931152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4320387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4321387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4322387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43230b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4324d763cef2SBarry Smith   PetscFunctionReturn(0);
4325d763cef2SBarry Smith }
4326d763cef2SBarry Smith 
43271b575b74SJoseph Pusztay /*
43281b575b74SJoseph Pusztay 
43291b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
43301b575b74SJoseph Pusztay 
43311b575b74SJoseph Pusztay */
43321b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43331b575b74SJoseph Pusztay {
43341b575b74SJoseph Pusztay   PetscDraw      draw;
43351b575b74SJoseph Pusztay   PetscErrorCode ierr;
43361b575b74SJoseph Pusztay 
43371b575b74SJoseph Pusztay   PetscFunctionBegin;
43381b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43391b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43401b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43411b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43421b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43431b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43441b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43451b575b74SJoseph Pusztay 
43461b575b74SJoseph Pusztay }
43471b575b74SJoseph Pusztay 
43481b575b74SJoseph Pusztay /*
43491b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
43501b575b74SJoseph Pusztay */
43511b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
43521b575b74SJoseph Pusztay {
43531b575b74SJoseph Pusztay   PetscErrorCode ierr;
43541b575b74SJoseph Pusztay 
43551b575b74SJoseph Pusztay   PetscFunctionBegin;
43561b575b74SJoseph Pusztay 
43571b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
43581b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
43591b575b74SJoseph Pusztay 
43601b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43611b575b74SJoseph Pusztay 
43621b575b74SJoseph Pusztay }
43631b575b74SJoseph Pusztay 
4364d763cef2SBarry Smith /*@
4365b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4366d763cef2SBarry Smith 
4367d763cef2SBarry Smith    Not Collective
4368d763cef2SBarry Smith 
4369d763cef2SBarry Smith    Input Parameter:
4370d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4371d763cef2SBarry Smith 
4372d763cef2SBarry Smith    Output Parameter:
437319eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4374d763cef2SBarry Smith 
4375d763cef2SBarry Smith    Level: beginner
4376d763cef2SBarry Smith 
4377b8123daeSJed Brown    Note:
4378b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
43799be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4380b8123daeSJed Brown 
43818f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4382d763cef2SBarry Smith 
4383d763cef2SBarry Smith @*/
43847087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4385d763cef2SBarry Smith {
4386d763cef2SBarry Smith   PetscFunctionBegin;
43870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4388f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4389d763cef2SBarry Smith   *t = ts->ptime;
4390d763cef2SBarry Smith   PetscFunctionReturn(0);
4391d763cef2SBarry Smith }
4392d763cef2SBarry Smith 
4393e5e524a1SHong Zhang /*@
4394e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4395e5e524a1SHong Zhang 
4396e5e524a1SHong Zhang    Not Collective
4397e5e524a1SHong Zhang 
4398e5e524a1SHong Zhang    Input Parameter:
4399e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4400e5e524a1SHong Zhang 
4401e5e524a1SHong Zhang    Output Parameter:
4402e5e524a1SHong Zhang .  t  - the previous time
4403e5e524a1SHong Zhang 
4404e5e524a1SHong Zhang    Level: beginner
4405e5e524a1SHong Zhang 
4406aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4407e5e524a1SHong Zhang 
4408e5e524a1SHong Zhang @*/
4409e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4410e5e524a1SHong Zhang {
4411e5e524a1SHong Zhang   PetscFunctionBegin;
4412e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4413e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4414e5e524a1SHong Zhang   *t = ts->ptime_prev;
4415e5e524a1SHong Zhang   PetscFunctionReturn(0);
4416e5e524a1SHong Zhang }
4417e5e524a1SHong Zhang 
44186a4d4014SLisandro Dalcin /*@
44196a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44206a4d4014SLisandro Dalcin 
44213f9fe445SBarry Smith    Logically Collective on TS
44226a4d4014SLisandro Dalcin 
44236a4d4014SLisandro Dalcin    Input Parameters:
44246a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44256a4d4014SLisandro Dalcin -  time - the time
44266a4d4014SLisandro Dalcin 
44276a4d4014SLisandro Dalcin    Level: intermediate
44286a4d4014SLisandro Dalcin 
442919eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
44306a4d4014SLisandro Dalcin 
44316a4d4014SLisandro Dalcin @*/
44327087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44336a4d4014SLisandro Dalcin {
44346a4d4014SLisandro Dalcin   PetscFunctionBegin;
44350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4436c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44376a4d4014SLisandro Dalcin   ts->ptime = t;
44386a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44396a4d4014SLisandro Dalcin }
44406a4d4014SLisandro Dalcin 
4441d763cef2SBarry Smith /*@C
4442d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4443d763cef2SBarry Smith    TS options in the database.
4444d763cef2SBarry Smith 
44453f9fe445SBarry Smith    Logically Collective on TS
4446d763cef2SBarry Smith 
4447d763cef2SBarry Smith    Input Parameter:
4448d763cef2SBarry Smith +  ts     - The TS context
4449d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4450d763cef2SBarry Smith 
4451d763cef2SBarry Smith    Notes:
4452d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4453d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4454d763cef2SBarry Smith    hyphen.
4455d763cef2SBarry Smith 
4456d763cef2SBarry Smith    Level: advanced
4457d763cef2SBarry Smith 
4458d763cef2SBarry Smith .seealso: TSSetFromOptions()
4459d763cef2SBarry Smith 
4460d763cef2SBarry Smith @*/
44617087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4462d763cef2SBarry Smith {
4463dfbe8321SBarry Smith   PetscErrorCode ierr;
4464089b2837SJed Brown   SNES           snes;
4465d763cef2SBarry Smith 
4466d763cef2SBarry Smith   PetscFunctionBegin;
44670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4468d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4469089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4470089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4471d763cef2SBarry Smith   PetscFunctionReturn(0);
4472d763cef2SBarry Smith }
4473d763cef2SBarry Smith 
4474d763cef2SBarry Smith /*@C
4475d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4476d763cef2SBarry Smith    TS options in the database.
4477d763cef2SBarry Smith 
44783f9fe445SBarry Smith    Logically Collective on TS
4479d763cef2SBarry Smith 
4480d763cef2SBarry Smith    Input Parameter:
4481d763cef2SBarry Smith +  ts     - The TS context
4482d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4483d763cef2SBarry Smith 
4484d763cef2SBarry Smith    Notes:
4485d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4486d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4487d763cef2SBarry Smith    hyphen.
4488d763cef2SBarry Smith 
4489d763cef2SBarry Smith    Level: advanced
4490d763cef2SBarry Smith 
4491d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4492d763cef2SBarry Smith 
4493d763cef2SBarry Smith @*/
44947087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4495d763cef2SBarry Smith {
4496dfbe8321SBarry Smith   PetscErrorCode ierr;
4497089b2837SJed Brown   SNES           snes;
4498d763cef2SBarry Smith 
4499d763cef2SBarry Smith   PetscFunctionBegin;
45000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4501d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4502089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4503089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4504d763cef2SBarry Smith   PetscFunctionReturn(0);
4505d763cef2SBarry Smith }
4506d763cef2SBarry Smith 
4507d763cef2SBarry Smith /*@C
4508d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4509d763cef2SBarry Smith    TS options in the database.
4510d763cef2SBarry Smith 
4511d763cef2SBarry Smith    Not Collective
4512d763cef2SBarry Smith 
4513d763cef2SBarry Smith    Input Parameter:
4514d763cef2SBarry Smith .  ts - The TS context
4515d763cef2SBarry Smith 
4516d763cef2SBarry Smith    Output Parameter:
4517d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4518d763cef2SBarry Smith 
451995452b02SPatrick Sanan    Notes:
452095452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4521d763cef2SBarry Smith    sufficient length to hold the prefix.
4522d763cef2SBarry Smith 
4523d763cef2SBarry Smith    Level: intermediate
4524d763cef2SBarry Smith 
4525d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4526d763cef2SBarry Smith @*/
45277087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4528d763cef2SBarry Smith {
4529dfbe8321SBarry Smith   PetscErrorCode ierr;
4530d763cef2SBarry Smith 
4531d763cef2SBarry Smith   PetscFunctionBegin;
45320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45334482741eSBarry Smith   PetscValidPointer(prefix,2);
4534d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4535d763cef2SBarry Smith   PetscFunctionReturn(0);
4536d763cef2SBarry Smith }
4537d763cef2SBarry Smith 
4538d763cef2SBarry Smith /*@C
4539d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4540d763cef2SBarry Smith 
4541d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4542d763cef2SBarry Smith 
4543d763cef2SBarry Smith    Input Parameter:
4544d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4545d763cef2SBarry Smith 
4546d763cef2SBarry Smith    Output Parameters:
4547e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4548e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4549e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4550e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4551d763cef2SBarry Smith 
455295452b02SPatrick Sanan    Notes:
455395452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4554d763cef2SBarry Smith 
4555d763cef2SBarry Smith    Level: intermediate
4556d763cef2SBarry Smith 
455780275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4558d763cef2SBarry Smith 
4559d763cef2SBarry Smith @*/
4560e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4561d763cef2SBarry Smith {
4562089b2837SJed Brown   PetscErrorCode ierr;
456324989b8cSPeter Brune   DM             dm;
4564089b2837SJed Brown 
4565d763cef2SBarry Smith   PetscFunctionBegin;
456623a57915SBarry Smith   if (Amat || Pmat) {
456723a57915SBarry Smith     SNES snes;
4568089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
456923a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4570e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
457123a57915SBarry Smith   }
457224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
457324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4574d763cef2SBarry Smith   PetscFunctionReturn(0);
4575d763cef2SBarry Smith }
4576d763cef2SBarry Smith 
45772eca1d9cSJed Brown /*@C
45782eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
45792eca1d9cSJed Brown 
45802eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
45812eca1d9cSJed Brown 
45822eca1d9cSJed Brown    Input Parameter:
45832eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
45842eca1d9cSJed Brown 
45852eca1d9cSJed Brown    Output Parameters:
4586e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4587e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
45882eca1d9cSJed Brown .  f   - The function to compute the matrices
45892eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
45902eca1d9cSJed Brown 
459195452b02SPatrick Sanan    Notes:
459295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
45932eca1d9cSJed Brown 
45942eca1d9cSJed Brown    Level: advanced
45952eca1d9cSJed Brown 
459680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
45972eca1d9cSJed Brown 
45982eca1d9cSJed Brown @*/
4599e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46002eca1d9cSJed Brown {
4601089b2837SJed Brown   PetscErrorCode ierr;
460224989b8cSPeter Brune   DM             dm;
46030910c330SBarry Smith 
46042eca1d9cSJed Brown   PetscFunctionBegin;
4605c0aab802Sstefano_zampini   if (Amat || Pmat) {
4606c0aab802Sstefano_zampini     SNES snes;
4607089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4608f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4609e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4610c0aab802Sstefano_zampini   }
461124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
461224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46132eca1d9cSJed Brown   PetscFunctionReturn(0);
46142eca1d9cSJed Brown }
46152eca1d9cSJed Brown 
46161713a123SBarry Smith /*@C
461783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46181713a123SBarry Smith    VecView() for the solution at each timestep
46191713a123SBarry Smith 
46201713a123SBarry Smith    Collective on TS
46211713a123SBarry Smith 
46221713a123SBarry Smith    Input Parameters:
46231713a123SBarry Smith +  ts - the TS context
46241713a123SBarry Smith .  step - current time-step
4625142b95e3SSatish Balay .  ptime - current time
46260298fd71SBarry Smith -  dummy - either a viewer or NULL
46271713a123SBarry Smith 
462899fdda47SBarry Smith    Options Database:
462999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
463099fdda47SBarry Smith 
463195452b02SPatrick Sanan    Notes:
463295452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
463399fdda47SBarry Smith        will look bad
463499fdda47SBarry Smith 
46351713a123SBarry Smith    Level: intermediate
46361713a123SBarry Smith 
4637a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46381713a123SBarry Smith @*/
46390910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46401713a123SBarry Smith {
4641dfbe8321SBarry Smith   PetscErrorCode   ierr;
464283a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4643473a3ab2SBarry Smith   PetscDraw        draw;
46441713a123SBarry Smith 
46451713a123SBarry Smith   PetscFunctionBegin;
46466083293cSBarry Smith   if (!step && ictx->showinitial) {
46476083293cSBarry Smith     if (!ictx->initialsolution) {
46480910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46491713a123SBarry Smith     }
46500910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
46516083293cSBarry Smith   }
4652b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46530dcf80beSBarry Smith 
46546083293cSBarry Smith   if (ictx->showinitial) {
46556083293cSBarry Smith     PetscReal pause;
46566083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
46576083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
46586083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
46596083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
46606083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
46616083293cSBarry Smith   }
46620910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4663473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
466451fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4665473a3ab2SBarry Smith     char      time[32];
4666473a3ab2SBarry Smith 
4667473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
466885b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4669473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4670473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
467151fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4672473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4673473a3ab2SBarry Smith   }
4674473a3ab2SBarry Smith 
46756083293cSBarry Smith   if (ictx->showinitial) {
46766083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
46776083293cSBarry Smith   }
46781713a123SBarry Smith   PetscFunctionReturn(0);
46791713a123SBarry Smith }
46801713a123SBarry Smith 
46819110b2e7SHong Zhang /*@C
46822d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
46832d5ee99bSBarry Smith 
46842d5ee99bSBarry Smith    Collective on TS
46852d5ee99bSBarry Smith 
46862d5ee99bSBarry Smith    Input Parameters:
46872d5ee99bSBarry Smith +  ts - the TS context
46882d5ee99bSBarry Smith .  step - current time-step
46892d5ee99bSBarry Smith .  ptime - current time
46902d5ee99bSBarry Smith -  dummy - either a viewer or NULL
46912d5ee99bSBarry Smith 
46922d5ee99bSBarry Smith    Level: intermediate
46932d5ee99bSBarry Smith 
46942d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46952d5ee99bSBarry Smith @*/
46962d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46972d5ee99bSBarry Smith {
46982d5ee99bSBarry Smith   PetscErrorCode    ierr;
46992d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47002d5ee99bSBarry Smith   PetscDraw         draw;
47016934998bSLisandro Dalcin   PetscDrawAxis     axis;
47022d5ee99bSBarry Smith   PetscInt          n;
47032d5ee99bSBarry Smith   PetscMPIInt       size;
47046934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47052d5ee99bSBarry Smith   char              time[32];
47062d5ee99bSBarry Smith   const PetscScalar *U;
47072d5ee99bSBarry Smith 
47082d5ee99bSBarry Smith   PetscFunctionBegin;
47096934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
47106934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47112d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47126934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47132d5ee99bSBarry Smith 
47142d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47156934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47166934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47176934998bSLisandro Dalcin   if (!step) {
47186934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47196934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47206934998bSLisandro Dalcin   }
47212d5ee99bSBarry Smith 
47222d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47236934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47246934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4725a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47266934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47272d5ee99bSBarry Smith 
47286934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47296934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47302d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47314b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
473285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47332d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
473451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47352d5ee99bSBarry Smith   }
47366934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47372d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
473856d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47396934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47402d5ee99bSBarry Smith   PetscFunctionReturn(0);
47412d5ee99bSBarry Smith }
47422d5ee99bSBarry Smith 
47436083293cSBarry Smith /*@C
474483a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
47456083293cSBarry Smith 
47466083293cSBarry Smith    Collective on TS
47476083293cSBarry Smith 
47486083293cSBarry Smith    Input Parameters:
47496083293cSBarry Smith .    ctx - the monitor context
47506083293cSBarry Smith 
47516083293cSBarry Smith    Level: intermediate
47526083293cSBarry Smith 
475383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
47546083293cSBarry Smith @*/
475583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
47566083293cSBarry Smith {
47576083293cSBarry Smith   PetscErrorCode ierr;
47586083293cSBarry Smith 
47596083293cSBarry Smith   PetscFunctionBegin;
476083a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
476183a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
476283a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
47636083293cSBarry Smith   PetscFunctionReturn(0);
47646083293cSBarry Smith }
47656083293cSBarry Smith 
47666083293cSBarry Smith /*@C
476783a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
47686083293cSBarry Smith 
47696083293cSBarry Smith    Collective on TS
47706083293cSBarry Smith 
47716083293cSBarry Smith    Input Parameter:
47726083293cSBarry Smith .    ts - time-step context
47736083293cSBarry Smith 
47746083293cSBarry Smith    Output Patameter:
47756083293cSBarry Smith .    ctx - the monitor context
47766083293cSBarry Smith 
477799fdda47SBarry Smith    Options Database:
477899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
477999fdda47SBarry Smith 
47806083293cSBarry Smith    Level: intermediate
47816083293cSBarry Smith 
478283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
47836083293cSBarry Smith @*/
478483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
47856083293cSBarry Smith {
47866083293cSBarry Smith   PetscErrorCode   ierr;
47876083293cSBarry Smith 
47886083293cSBarry Smith   PetscFunctionBegin;
4789b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
479083a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4791e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4792bbd56ea5SKarl Rupp 
479383a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4794473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4795c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4796473a3ab2SBarry Smith 
4797473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4798c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
47996083293cSBarry Smith   PetscFunctionReturn(0);
48006083293cSBarry Smith }
48016083293cSBarry Smith 
48023a471f94SBarry Smith /*@C
48030ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
48040ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
48050ed3bfb6SBarry Smith 
48060ed3bfb6SBarry Smith    Collective on TS
48070ed3bfb6SBarry Smith 
48080ed3bfb6SBarry Smith    Input Parameters:
48090ed3bfb6SBarry Smith +  ts - the TS context
48100ed3bfb6SBarry Smith .  step - current time-step
48110ed3bfb6SBarry Smith .  ptime - current time
48120ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
48130ed3bfb6SBarry Smith 
48140ed3bfb6SBarry Smith    Options Database:
48150ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48160ed3bfb6SBarry Smith 
48170ed3bfb6SBarry Smith    Level: intermediate
48180ed3bfb6SBarry Smith 
48190ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48200ed3bfb6SBarry Smith @*/
48210ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48220ed3bfb6SBarry Smith {
48230ed3bfb6SBarry Smith   PetscErrorCode   ierr;
48240ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
48250ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
48260ed3bfb6SBarry Smith   Vec              work;
48270ed3bfb6SBarry Smith 
48280ed3bfb6SBarry Smith   PetscFunctionBegin;
48290ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48300ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48310ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48320ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48330ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48340ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48350ed3bfb6SBarry Smith }
48360ed3bfb6SBarry Smith 
48370ed3bfb6SBarry Smith /*@C
483883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48393a471f94SBarry Smith    VecView() for the error at each timestep
48403a471f94SBarry Smith 
48413a471f94SBarry Smith    Collective on TS
48423a471f94SBarry Smith 
48433a471f94SBarry Smith    Input Parameters:
48443a471f94SBarry Smith +  ts - the TS context
48453a471f94SBarry Smith .  step - current time-step
48463a471f94SBarry Smith .  ptime - current time
48470298fd71SBarry Smith -  dummy - either a viewer or NULL
48483a471f94SBarry Smith 
48490ed3bfb6SBarry Smith    Options Database:
48500ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48510ed3bfb6SBarry Smith 
48523a471f94SBarry Smith    Level: intermediate
48533a471f94SBarry Smith 
48540ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48553a471f94SBarry Smith @*/
48560910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48573a471f94SBarry Smith {
48583a471f94SBarry Smith   PetscErrorCode   ierr;
485983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
486083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
48613a471f94SBarry Smith   Vec              work;
48623a471f94SBarry Smith 
48633a471f94SBarry Smith   PetscFunctionBegin;
4864b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48650910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48663a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48670910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
48683a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48693a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48703a471f94SBarry Smith   PetscFunctionReturn(0);
48713a471f94SBarry Smith }
48723a471f94SBarry Smith 
4873af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
48746c699258SBarry Smith /*@
48752a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
48766c699258SBarry Smith 
4877d083f849SBarry Smith    Logically Collective on ts
48786c699258SBarry Smith 
48796c699258SBarry Smith    Input Parameters:
48802a808120SBarry Smith +  ts - the ODE integrator object
48812a808120SBarry Smith -  dm - the dm, cannot be NULL
48826c699258SBarry Smith 
4883e03a659cSJed Brown    Notes:
4884e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4885e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4886e03a659cSJed Brown    different problems using the same function space.
4887e03a659cSJed Brown 
48886c699258SBarry Smith    Level: intermediate
48896c699258SBarry Smith 
48906c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
48916c699258SBarry Smith @*/
48927087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
48936c699258SBarry Smith {
48946c699258SBarry Smith   PetscErrorCode ierr;
4895089b2837SJed Brown   SNES           snes;
4896942e3340SBarry Smith   DMTS           tsdm;
48976c699258SBarry Smith 
48986c699258SBarry Smith   PetscFunctionBegin;
48990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49002a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
490170663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4902942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49032a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4904942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4905942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
490624989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
490724989b8cSPeter Brune         tsdm->originaldm = dm;
490824989b8cSPeter Brune       }
490924989b8cSPeter Brune     }
49106bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
491124989b8cSPeter Brune   }
49126c699258SBarry Smith   ts->dm = dm;
4913bbd56ea5SKarl Rupp 
4914089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4915089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49166c699258SBarry Smith   PetscFunctionReturn(0);
49176c699258SBarry Smith }
49186c699258SBarry Smith 
49196c699258SBarry Smith /*@
49206c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49216c699258SBarry Smith 
49223f9fe445SBarry Smith    Not Collective
49236c699258SBarry Smith 
49246c699258SBarry Smith    Input Parameter:
49256c699258SBarry Smith . ts - the preconditioner context
49266c699258SBarry Smith 
49276c699258SBarry Smith    Output Parameter:
49286c699258SBarry Smith .  dm - the dm
49296c699258SBarry Smith 
49306c699258SBarry Smith    Level: intermediate
49316c699258SBarry Smith 
49326c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49336c699258SBarry Smith @*/
49347087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49356c699258SBarry Smith {
4936496e6a7aSJed Brown   PetscErrorCode ierr;
4937496e6a7aSJed Brown 
49386c699258SBarry Smith   PetscFunctionBegin;
49390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4940496e6a7aSJed Brown   if (!ts->dm) {
4941ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4942496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4943496e6a7aSJed Brown   }
49446c699258SBarry Smith   *dm = ts->dm;
49456c699258SBarry Smith   PetscFunctionReturn(0);
49466c699258SBarry Smith }
49471713a123SBarry Smith 
49480f5c6efeSJed Brown /*@
49490f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49500f5c6efeSJed Brown 
49513f9fe445SBarry Smith    Logically Collective on SNES
49520f5c6efeSJed Brown 
49530f5c6efeSJed Brown    Input Parameter:
4954d42a1c89SJed Brown + snes - nonlinear solver
49550910c330SBarry Smith . U - the current state at which to evaluate the residual
4956d42a1c89SJed Brown - ctx - user context, must be a TS
49570f5c6efeSJed Brown 
49580f5c6efeSJed Brown    Output Parameter:
49590f5c6efeSJed Brown . F - the nonlinear residual
49600f5c6efeSJed Brown 
49610f5c6efeSJed Brown    Notes:
49620f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49630f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
49640f5c6efeSJed Brown 
49650f5c6efeSJed Brown    Level: advanced
49660f5c6efeSJed Brown 
49670f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
49680f5c6efeSJed Brown @*/
49690910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
49700f5c6efeSJed Brown {
49710f5c6efeSJed Brown   TS             ts = (TS)ctx;
49720f5c6efeSJed Brown   PetscErrorCode ierr;
49730f5c6efeSJed Brown 
49740f5c6efeSJed Brown   PetscFunctionBegin;
49750f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
49760910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
49770f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
49780f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
49790910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
49800f5c6efeSJed Brown   PetscFunctionReturn(0);
49810f5c6efeSJed Brown }
49820f5c6efeSJed Brown 
49830f5c6efeSJed Brown /*@
49840f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
49850f5c6efeSJed Brown 
49860f5c6efeSJed Brown    Collective on SNES
49870f5c6efeSJed Brown 
49880f5c6efeSJed Brown    Input Parameter:
49890f5c6efeSJed Brown + snes - nonlinear solver
49900910c330SBarry Smith . U - the current state at which to evaluate the residual
49910f5c6efeSJed Brown - ctx - user context, must be a TS
49920f5c6efeSJed Brown 
49930f5c6efeSJed Brown    Output Parameter:
49940f5c6efeSJed Brown + A - the Jacobian
49950f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
49960f5c6efeSJed Brown - flag - indicates any structure change in the matrix
49970f5c6efeSJed Brown 
49980f5c6efeSJed Brown    Notes:
49990f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50000f5c6efeSJed Brown 
50010f5c6efeSJed Brown    Level: developer
50020f5c6efeSJed Brown 
50030f5c6efeSJed Brown .seealso: SNESSetJacobian()
50040f5c6efeSJed Brown @*/
5005d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50060f5c6efeSJed Brown {
50070f5c6efeSJed Brown   TS             ts = (TS)ctx;
50080f5c6efeSJed Brown   PetscErrorCode ierr;
50090f5c6efeSJed Brown 
50100f5c6efeSJed Brown   PetscFunctionBegin;
50110f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50120910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50130f5c6efeSJed Brown   PetscValidPointer(A,3);
501494ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50150f5c6efeSJed Brown   PetscValidPointer(B,4);
501694ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50170f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5018d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50190f5c6efeSJed Brown   PetscFunctionReturn(0);
50200f5c6efeSJed Brown }
5021325fc9f4SBarry Smith 
50220e4ef248SJed Brown /*@C
50239ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50240e4ef248SJed Brown 
50250e4ef248SJed Brown    Collective on TS
50260e4ef248SJed Brown 
50270e4ef248SJed Brown    Input Arguments:
50280e4ef248SJed Brown +  ts - time stepping context
50290e4ef248SJed Brown .  t - time at which to evaluate
50300910c330SBarry Smith .  U - state at which to evaluate
50310e4ef248SJed Brown -  ctx - context
50320e4ef248SJed Brown 
50330e4ef248SJed Brown    Output Arguments:
50340e4ef248SJed Brown .  F - right hand side
50350e4ef248SJed Brown 
50360e4ef248SJed Brown    Level: intermediate
50370e4ef248SJed Brown 
50380e4ef248SJed Brown    Notes:
50390e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50400e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50410e4ef248SJed Brown 
50420e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50430e4ef248SJed Brown @*/
50440910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50450e4ef248SJed Brown {
50460e4ef248SJed Brown   PetscErrorCode ierr;
50470e4ef248SJed Brown   Mat            Arhs,Brhs;
50480e4ef248SJed Brown 
50490e4ef248SJed Brown   PetscFunctionBegin;
50500e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
50512663174eSHong Zhang 
50522663174eSHong Zhang   /* undo the damage caused by shifting */
50532663174eSHong Zhang   if (ts->rhsjacobian.shift) {
50542663174eSHong Zhang     ierr = MatShift(Arhs,-ts->rhsjacobian.shift);CHKERRQ(ierr);
50552663174eSHong Zhang    }
50562663174eSHong Zhang   if (ts->rhsjacobian.scale == -1.) {
50572663174eSHong Zhang     ierr = MatScale(Arhs,-1);CHKERRQ(ierr);
50582663174eSHong Zhang   }
50592663174eSHong Zhang   if (Arhs != Brhs) {
50602663174eSHong Zhang     if (ts->rhsjacobian.shift) {
50612663174eSHong Zhang       ierr = MatShift(Brhs,-ts->rhsjacobian.shift);CHKERRQ(ierr);
50622663174eSHong Zhang     }
50632663174eSHong Zhang     if (ts->rhsjacobian.scale == -1.) {
50642663174eSHong Zhang       ierr = MatScale(Brhs,-1);CHKERRQ(ierr);
50652663174eSHong Zhang     }
50662663174eSHong Zhang   }
50672663174eSHong Zhang   ts->rhsjacobian.shift = 0;
50682663174eSHong Zhang   ts->rhsjacobian.scale = 1;
5069d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
50700910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
50710e4ef248SJed Brown   PetscFunctionReturn(0);
50720e4ef248SJed Brown }
50730e4ef248SJed Brown 
50740e4ef248SJed Brown /*@C
50750e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
50760e4ef248SJed Brown 
50770e4ef248SJed Brown    Collective on TS
50780e4ef248SJed Brown 
50790e4ef248SJed Brown    Input Arguments:
50800e4ef248SJed Brown +  ts - time stepping context
50810e4ef248SJed Brown .  t - time at which to evaluate
50820910c330SBarry Smith .  U - state at which to evaluate
50830e4ef248SJed Brown -  ctx - context
50840e4ef248SJed Brown 
50850e4ef248SJed Brown    Output Arguments:
50860e4ef248SJed Brown +  A - pointer to operator
50870e4ef248SJed Brown .  B - pointer to preconditioning matrix
50880e4ef248SJed Brown -  flg - matrix structure flag
50890e4ef248SJed Brown 
50900e4ef248SJed Brown    Level: intermediate
50910e4ef248SJed Brown 
50920e4ef248SJed Brown    Notes:
50930e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
50940e4ef248SJed Brown 
50950e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
50960e4ef248SJed Brown @*/
5097d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
50980e4ef248SJed Brown {
50990e4ef248SJed Brown   PetscFunctionBegin;
51000e4ef248SJed Brown   PetscFunctionReturn(0);
51010e4ef248SJed Brown }
51020e4ef248SJed Brown 
51030026cea9SSean Farley /*@C
51040026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51050026cea9SSean Farley 
51060026cea9SSean Farley    Collective on TS
51070026cea9SSean Farley 
51080026cea9SSean Farley    Input Arguments:
51090026cea9SSean Farley +  ts - time stepping context
51100026cea9SSean Farley .  t - time at which to evaluate
51110910c330SBarry Smith .  U - state at which to evaluate
51120910c330SBarry Smith .  Udot - time derivative of state vector
51130026cea9SSean Farley -  ctx - context
51140026cea9SSean Farley 
51150026cea9SSean Farley    Output Arguments:
51160026cea9SSean Farley .  F - left hand side
51170026cea9SSean Farley 
51180026cea9SSean Farley    Level: intermediate
51190026cea9SSean Farley 
51200026cea9SSean Farley    Notes:
51210910c330SBarry 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
51220026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51230026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51240026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51250026cea9SSean Farley 
51269ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51279ae8fd06SBarry Smith 
51289ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51290026cea9SSean Farley @*/
51300910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51310026cea9SSean Farley {
51320026cea9SSean Farley   PetscErrorCode ierr;
51330026cea9SSean Farley   Mat            A,B;
51340026cea9SSean Farley 
51350026cea9SSean Farley   PetscFunctionBegin;
51360298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5137d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51380910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51390026cea9SSean Farley   PetscFunctionReturn(0);
51400026cea9SSean Farley }
51410026cea9SSean Farley 
51420026cea9SSean Farley /*@C
5143b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51440026cea9SSean Farley 
51450026cea9SSean Farley    Collective on TS
51460026cea9SSean Farley 
51470026cea9SSean Farley    Input Arguments:
51480026cea9SSean Farley +  ts - time stepping context
51490026cea9SSean Farley .  t - time at which to evaluate
51500910c330SBarry Smith .  U - state at which to evaluate
51510910c330SBarry Smith .  Udot - time derivative of state vector
51520026cea9SSean Farley .  shift - shift to apply
51530026cea9SSean Farley -  ctx - context
51540026cea9SSean Farley 
51550026cea9SSean Farley    Output Arguments:
51560026cea9SSean Farley +  A - pointer to operator
51570026cea9SSean Farley .  B - pointer to preconditioning matrix
51580026cea9SSean Farley -  flg - matrix structure flag
51590026cea9SSean Farley 
5160b41af12eSJed Brown    Level: advanced
51610026cea9SSean Farley 
51620026cea9SSean Farley    Notes:
51630026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51640026cea9SSean Farley 
5165b41af12eSJed Brown    It is only appropriate for problems of the form
5166b41af12eSJed Brown 
5167b41af12eSJed Brown $     M Udot = F(U,t)
5168b41af12eSJed Brown 
5169b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5170b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5171b41af12eSJed Brown   an implicit operator of the form
5172b41af12eSJed Brown 
5173b41af12eSJed Brown $    shift*M + J
5174b41af12eSJed Brown 
5175b41af12eSJed 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
5176b41af12eSJed Brown   a copy of M or reassemble it when requested.
5177b41af12eSJed Brown 
51780026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
51790026cea9SSean Farley @*/
5180d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
51810026cea9SSean Farley {
5182b41af12eSJed Brown   PetscErrorCode ierr;
5183b41af12eSJed Brown 
51840026cea9SSean Farley   PetscFunctionBegin;
518594ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5186b41af12eSJed Brown   ts->ijacobian.shift = shift;
51870026cea9SSean Farley   PetscFunctionReturn(0);
51880026cea9SSean Farley }
5189b41af12eSJed Brown 
5190e817cc15SEmil Constantinescu /*@
5191e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5192e817cc15SEmil Constantinescu 
5193e817cc15SEmil Constantinescu    Not Collective
5194e817cc15SEmil Constantinescu 
5195e817cc15SEmil Constantinescu    Input Parameter:
5196e817cc15SEmil Constantinescu .  ts - the TS context
5197e817cc15SEmil Constantinescu 
5198e817cc15SEmil Constantinescu    Output Parameter:
51994e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5200e817cc15SEmil Constantinescu 
5201e817cc15SEmil Constantinescu    Level: beginner
5202e817cc15SEmil Constantinescu 
5203e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5204e817cc15SEmil Constantinescu @*/
5205e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5206e817cc15SEmil Constantinescu {
5207e817cc15SEmil Constantinescu   PetscFunctionBegin;
5208e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5209e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5210e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5211e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5212e817cc15SEmil Constantinescu }
5213e817cc15SEmil Constantinescu 
5214e817cc15SEmil Constantinescu /*@
5215e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5216e817cc15SEmil Constantinescu 
5217e817cc15SEmil Constantinescu    Not Collective
5218e817cc15SEmil Constantinescu 
5219e817cc15SEmil Constantinescu    Input Parameter:
5220e817cc15SEmil Constantinescu +  ts - the TS context
52211297b224SEmil Constantinescu -  equation_type - see TSEquationType
5222e817cc15SEmil Constantinescu 
5223e817cc15SEmil Constantinescu    Level: advanced
5224e817cc15SEmil Constantinescu 
5225e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5226e817cc15SEmil Constantinescu @*/
5227e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5228e817cc15SEmil Constantinescu {
5229e817cc15SEmil Constantinescu   PetscFunctionBegin;
5230e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5231e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5232e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5233e817cc15SEmil Constantinescu }
52340026cea9SSean Farley 
52354af1b03aSJed Brown /*@
52364af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52374af1b03aSJed Brown 
52384af1b03aSJed Brown    Not Collective
52394af1b03aSJed Brown 
52404af1b03aSJed Brown    Input Parameter:
52414af1b03aSJed Brown .  ts - the TS context
52424af1b03aSJed Brown 
52434af1b03aSJed Brown    Output Parameter:
52444af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52454af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52464af1b03aSJed Brown 
5247487e0bb9SJed Brown    Level: beginner
52484af1b03aSJed Brown 
5249cd652676SJed Brown    Notes:
5250cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52514af1b03aSJed Brown 
52524af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52534af1b03aSJed Brown @*/
52544af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52554af1b03aSJed Brown {
52564af1b03aSJed Brown   PetscFunctionBegin;
52574af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52584af1b03aSJed Brown   PetscValidPointer(reason,2);
52594af1b03aSJed Brown   *reason = ts->reason;
52604af1b03aSJed Brown   PetscFunctionReturn(0);
52614af1b03aSJed Brown }
52624af1b03aSJed Brown 
5263d6ad946cSShri Abhyankar /*@
5264d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5265d6ad946cSShri Abhyankar 
52666b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5267d6ad946cSShri Abhyankar 
52686b221cbeSPatrick Sanan    Input Parameters:
5269d6ad946cSShri Abhyankar +  ts - the TS context
52706b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5271d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5272d6ad946cSShri Abhyankar 
5273f5abba47SShri Abhyankar    Level: advanced
5274d6ad946cSShri Abhyankar 
5275d6ad946cSShri Abhyankar    Notes:
52766b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5277d6ad946cSShri Abhyankar 
5278d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5279d6ad946cSShri Abhyankar @*/
5280d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5281d6ad946cSShri Abhyankar {
5282d6ad946cSShri Abhyankar   PetscFunctionBegin;
5283d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5284d6ad946cSShri Abhyankar   ts->reason = reason;
5285d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5286d6ad946cSShri Abhyankar }
5287d6ad946cSShri Abhyankar 
5288cc708dedSBarry Smith /*@
5289cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5290cc708dedSBarry Smith 
5291cc708dedSBarry Smith    Not Collective
5292cc708dedSBarry Smith 
5293cc708dedSBarry Smith    Input Parameter:
5294cc708dedSBarry Smith .  ts - the TS context
5295cc708dedSBarry Smith 
5296cc708dedSBarry Smith    Output Parameter:
529719eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5298cc708dedSBarry Smith 
5299487e0bb9SJed Brown    Level: beginner
5300cc708dedSBarry Smith 
5301cc708dedSBarry Smith    Notes:
5302cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5303cc708dedSBarry Smith 
5304cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5305cc708dedSBarry Smith @*/
5306cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5307cc708dedSBarry Smith {
5308cc708dedSBarry Smith   PetscFunctionBegin;
5309cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5310cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5311cc708dedSBarry Smith   *ftime = ts->solvetime;
5312cc708dedSBarry Smith   PetscFunctionReturn(0);
5313cc708dedSBarry Smith }
5314cc708dedSBarry Smith 
53152c18e0fdSBarry Smith /*@
53165ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53179f67acb7SJed Brown    used by the time integrator.
53189f67acb7SJed Brown 
53199f67acb7SJed Brown    Not Collective
53209f67acb7SJed Brown 
53219f67acb7SJed Brown    Input Parameter:
53229f67acb7SJed Brown .  ts - TS context
53239f67acb7SJed Brown 
53249f67acb7SJed Brown    Output Parameter:
53259f67acb7SJed Brown .  nits - number of nonlinear iterations
53269f67acb7SJed Brown 
53279f67acb7SJed Brown    Notes:
53289f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53299f67acb7SJed Brown 
53309f67acb7SJed Brown    Level: intermediate
53319f67acb7SJed Brown 
53325ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53339f67acb7SJed Brown @*/
53345ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53359f67acb7SJed Brown {
53369f67acb7SJed Brown   PetscFunctionBegin;
53379f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53389f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53395ef26d82SJed Brown   *nits = ts->snes_its;
53409f67acb7SJed Brown   PetscFunctionReturn(0);
53419f67acb7SJed Brown }
53429f67acb7SJed Brown 
53439f67acb7SJed Brown /*@
53445ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53459f67acb7SJed Brown    used by the time integrator.
53469f67acb7SJed Brown 
53479f67acb7SJed Brown    Not Collective
53489f67acb7SJed Brown 
53499f67acb7SJed Brown    Input Parameter:
53509f67acb7SJed Brown .  ts - TS context
53519f67acb7SJed Brown 
53529f67acb7SJed Brown    Output Parameter:
53539f67acb7SJed Brown .  lits - number of linear iterations
53549f67acb7SJed Brown 
53559f67acb7SJed Brown    Notes:
53569f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53579f67acb7SJed Brown 
53589f67acb7SJed Brown    Level: intermediate
53599f67acb7SJed Brown 
53605ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
53619f67acb7SJed Brown @*/
53625ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
53639f67acb7SJed Brown {
53649f67acb7SJed Brown   PetscFunctionBegin;
53659f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53669f67acb7SJed Brown   PetscValidIntPointer(lits,2);
53675ef26d82SJed Brown   *lits = ts->ksp_its;
53689f67acb7SJed Brown   PetscFunctionReturn(0);
53699f67acb7SJed Brown }
53709f67acb7SJed Brown 
5371cef5090cSJed Brown /*@
5372cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5373cef5090cSJed Brown 
5374cef5090cSJed Brown    Not Collective
5375cef5090cSJed Brown 
5376cef5090cSJed Brown    Input Parameter:
5377cef5090cSJed Brown .  ts - TS context
5378cef5090cSJed Brown 
5379cef5090cSJed Brown    Output Parameter:
5380cef5090cSJed Brown .  rejects - number of steps rejected
5381cef5090cSJed Brown 
5382cef5090cSJed Brown    Notes:
5383cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5384cef5090cSJed Brown 
5385cef5090cSJed Brown    Level: intermediate
5386cef5090cSJed Brown 
53875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5388cef5090cSJed Brown @*/
5389cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5390cef5090cSJed Brown {
5391cef5090cSJed Brown   PetscFunctionBegin;
5392cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5393cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5394cef5090cSJed Brown   *rejects = ts->reject;
5395cef5090cSJed Brown   PetscFunctionReturn(0);
5396cef5090cSJed Brown }
5397cef5090cSJed Brown 
5398cef5090cSJed Brown /*@
5399cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5400cef5090cSJed Brown 
5401cef5090cSJed Brown    Not Collective
5402cef5090cSJed Brown 
5403cef5090cSJed Brown    Input Parameter:
5404cef5090cSJed Brown .  ts - TS context
5405cef5090cSJed Brown 
5406cef5090cSJed Brown    Output Parameter:
5407cef5090cSJed Brown .  fails - number of failed nonlinear solves
5408cef5090cSJed Brown 
5409cef5090cSJed Brown    Notes:
5410cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5411cef5090cSJed Brown 
5412cef5090cSJed Brown    Level: intermediate
5413cef5090cSJed Brown 
54145ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5415cef5090cSJed Brown @*/
5416cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5417cef5090cSJed Brown {
5418cef5090cSJed Brown   PetscFunctionBegin;
5419cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5420cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5421cef5090cSJed Brown   *fails = ts->num_snes_failures;
5422cef5090cSJed Brown   PetscFunctionReturn(0);
5423cef5090cSJed Brown }
5424cef5090cSJed Brown 
5425cef5090cSJed Brown /*@
5426cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5427cef5090cSJed Brown 
5428cef5090cSJed Brown    Not Collective
5429cef5090cSJed Brown 
5430cef5090cSJed Brown    Input Parameter:
5431cef5090cSJed Brown +  ts - TS context
5432cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5433cef5090cSJed Brown 
5434cef5090cSJed Brown    Notes:
5435cef5090cSJed Brown    The counter is reset to zero for each step
5436cef5090cSJed Brown 
5437cef5090cSJed Brown    Options Database Key:
5438cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5439cef5090cSJed Brown 
5440cef5090cSJed Brown    Level: intermediate
5441cef5090cSJed Brown 
54425ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5443cef5090cSJed Brown @*/
5444cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5445cef5090cSJed Brown {
5446cef5090cSJed Brown   PetscFunctionBegin;
5447cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5448cef5090cSJed Brown   ts->max_reject = rejects;
5449cef5090cSJed Brown   PetscFunctionReturn(0);
5450cef5090cSJed Brown }
5451cef5090cSJed Brown 
5452cef5090cSJed Brown /*@
5453cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5454cef5090cSJed Brown 
5455cef5090cSJed Brown    Not Collective
5456cef5090cSJed Brown 
5457cef5090cSJed Brown    Input Parameter:
5458cef5090cSJed Brown +  ts - TS context
5459cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5460cef5090cSJed Brown 
5461cef5090cSJed Brown    Notes:
5462cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5463cef5090cSJed Brown 
5464cef5090cSJed Brown    Options Database Key:
5465cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5466cef5090cSJed Brown 
5467cef5090cSJed Brown    Level: intermediate
5468cef5090cSJed Brown 
54695ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5470cef5090cSJed Brown @*/
5471cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5472cef5090cSJed Brown {
5473cef5090cSJed Brown   PetscFunctionBegin;
5474cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5475cef5090cSJed Brown   ts->max_snes_failures = fails;
5476cef5090cSJed Brown   PetscFunctionReturn(0);
5477cef5090cSJed Brown }
5478cef5090cSJed Brown 
5479cef5090cSJed Brown /*@
5480cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5481cef5090cSJed Brown 
5482cef5090cSJed Brown    Not Collective
5483cef5090cSJed Brown 
5484cef5090cSJed Brown    Input Parameter:
5485cef5090cSJed Brown +  ts - TS context
5486cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5487cef5090cSJed Brown 
5488cef5090cSJed Brown    Options Database Key:
5489cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5490cef5090cSJed Brown 
5491cef5090cSJed Brown    Level: intermediate
5492cef5090cSJed Brown 
54935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5494cef5090cSJed Brown @*/
5495cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5496cef5090cSJed Brown {
5497cef5090cSJed Brown   PetscFunctionBegin;
5498cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5499cef5090cSJed Brown   ts->errorifstepfailed = err;
5500cef5090cSJed Brown   PetscFunctionReturn(0);
5501cef5090cSJed Brown }
5502cef5090cSJed Brown 
5503fb1732b5SBarry Smith /*@C
5504fde5950dSBarry 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
5505fb1732b5SBarry Smith 
5506fb1732b5SBarry Smith    Collective on TS
5507fb1732b5SBarry Smith 
5508fb1732b5SBarry Smith    Input Parameters:
5509fb1732b5SBarry Smith +  ts - the TS context
5510fb1732b5SBarry Smith .  step - current time-step
5511fb1732b5SBarry Smith .  ptime - current time
55120910c330SBarry Smith .  u - current state
5513721cd6eeSBarry Smith -  vf - viewer and its format
5514fb1732b5SBarry Smith 
5515fb1732b5SBarry Smith    Level: intermediate
5516fb1732b5SBarry Smith 
5517fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5518fb1732b5SBarry Smith @*/
5519721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5520fb1732b5SBarry Smith {
5521fb1732b5SBarry Smith   PetscErrorCode ierr;
5522fb1732b5SBarry Smith 
5523fb1732b5SBarry Smith   PetscFunctionBegin;
5524721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5525721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5526721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5527ed81e22dSJed Brown   PetscFunctionReturn(0);
5528ed81e22dSJed Brown }
5529ed81e22dSJed Brown 
5530ed81e22dSJed Brown /*@C
5531ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5532ed81e22dSJed Brown 
5533ed81e22dSJed Brown    Collective on TS
5534ed81e22dSJed Brown 
5535ed81e22dSJed Brown    Input Parameters:
5536ed81e22dSJed Brown +  ts - the TS context
5537ed81e22dSJed Brown .  step - current time-step
5538ed81e22dSJed Brown .  ptime - current time
55390910c330SBarry Smith .  u - current state
5540ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5541ed81e22dSJed Brown 
5542ed81e22dSJed Brown    Level: intermediate
5543ed81e22dSJed Brown 
5544ed81e22dSJed Brown    Notes:
5545ed81e22dSJed 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.
5546ed81e22dSJed Brown    These are named according to the file name template.
5547ed81e22dSJed Brown 
5548ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5549ed81e22dSJed Brown 
5550ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5551ed81e22dSJed Brown @*/
55520910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5553ed81e22dSJed Brown {
5554ed81e22dSJed Brown   PetscErrorCode ierr;
5555ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5556ed81e22dSJed Brown   PetscViewer    viewer;
5557ed81e22dSJed Brown 
5558ed81e22dSJed Brown   PetscFunctionBegin;
555963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55608caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5561ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
55620910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5563ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5564ed81e22dSJed Brown   PetscFunctionReturn(0);
5565ed81e22dSJed Brown }
5566ed81e22dSJed Brown 
5567ed81e22dSJed Brown /*@C
5568ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5569ed81e22dSJed Brown 
5570ed81e22dSJed Brown    Collective on TS
5571ed81e22dSJed Brown 
5572ed81e22dSJed Brown    Input Parameters:
5573ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5574ed81e22dSJed Brown 
5575ed81e22dSJed Brown    Level: intermediate
5576ed81e22dSJed Brown 
5577ed81e22dSJed Brown    Note:
5578ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5579ed81e22dSJed Brown 
5580ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5581ed81e22dSJed Brown @*/
5582ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5583ed81e22dSJed Brown {
5584ed81e22dSJed Brown   PetscErrorCode ierr;
5585ed81e22dSJed Brown 
5586ed81e22dSJed Brown   PetscFunctionBegin;
5587ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5588fb1732b5SBarry Smith   PetscFunctionReturn(0);
5589fb1732b5SBarry Smith }
5590fb1732b5SBarry Smith 
559184df9cb4SJed Brown /*@
5592552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
559384df9cb4SJed Brown 
5594ed81e22dSJed Brown    Collective on TS if controller has not been created yet
559584df9cb4SJed Brown 
559684df9cb4SJed Brown    Input Arguments:
5597ed81e22dSJed Brown .  ts - time stepping context
559884df9cb4SJed Brown 
559984df9cb4SJed Brown    Output Arguments:
5600ed81e22dSJed Brown .  adapt - adaptive controller
560184df9cb4SJed Brown 
560284df9cb4SJed Brown    Level: intermediate
560384df9cb4SJed Brown 
5604ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
560584df9cb4SJed Brown @*/
5606552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
560784df9cb4SJed Brown {
560884df9cb4SJed Brown   PetscErrorCode ierr;
560984df9cb4SJed Brown 
561084df9cb4SJed Brown   PetscFunctionBegin;
561184df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5612bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
561384df9cb4SJed Brown   if (!ts->adapt) {
5614ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56153bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56161c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
561784df9cb4SJed Brown   }
5618bec58848SLisandro Dalcin   *adapt = ts->adapt;
561984df9cb4SJed Brown   PetscFunctionReturn(0);
562084df9cb4SJed Brown }
5621d6ebe24aSShri Abhyankar 
56221c3436cfSJed Brown /*@
56231c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
56241c3436cfSJed Brown 
56251c3436cfSJed Brown    Logically Collective
56261c3436cfSJed Brown 
56271c3436cfSJed Brown    Input Arguments:
56281c3436cfSJed Brown +  ts - time integration context
56291c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56300298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56311c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56320298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
56331c3436cfSJed Brown 
5634a3cdaa26SBarry Smith    Options Database keys:
5635a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5636a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5637a3cdaa26SBarry Smith 
56383ff766beSShri Abhyankar    Notes:
56393ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
56403ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
56413ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
56423ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
56433ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
56443ff766beSShri Abhyankar    differential variables.
56453ff766beSShri Abhyankar 
56461c3436cfSJed Brown    Level: beginner
56471c3436cfSJed Brown 
56485c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
56491c3436cfSJed Brown @*/
56501c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56511c3436cfSJed Brown {
56521c3436cfSJed Brown   PetscErrorCode ierr;
56531c3436cfSJed Brown 
56541c3436cfSJed Brown   PetscFunctionBegin;
5655c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56561c3436cfSJed Brown   if (vatol) {
56571c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56581c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56591c3436cfSJed Brown     ts->vatol = vatol;
56601c3436cfSJed Brown   }
5661c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
56621c3436cfSJed Brown   if (vrtol) {
56631c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
56641c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
56651c3436cfSJed Brown     ts->vrtol = vrtol;
56661c3436cfSJed Brown   }
56671c3436cfSJed Brown   PetscFunctionReturn(0);
56681c3436cfSJed Brown }
56691c3436cfSJed Brown 
5670c5033834SJed Brown /*@
5671c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5672c5033834SJed Brown 
5673c5033834SJed Brown    Logically Collective
5674c5033834SJed Brown 
5675c5033834SJed Brown    Input Arguments:
5676c5033834SJed Brown .  ts - time integration context
5677c5033834SJed Brown 
5678c5033834SJed Brown    Output Arguments:
56790298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
56800298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
56810298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
56820298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5683c5033834SJed Brown 
5684c5033834SJed Brown    Level: beginner
5685c5033834SJed Brown 
56865c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5687c5033834SJed Brown @*/
5688c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5689c5033834SJed Brown {
5690c5033834SJed Brown   PetscFunctionBegin;
5691c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5692c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5693c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5694c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5695c5033834SJed Brown   PetscFunctionReturn(0);
5696c5033834SJed Brown }
5697c5033834SJed Brown 
56989c6b16b5SShri Abhyankar /*@
5699a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57009c6b16b5SShri Abhyankar 
57019c6b16b5SShri Abhyankar    Collective on TS
57029c6b16b5SShri Abhyankar 
57039c6b16b5SShri Abhyankar    Input Arguments:
57049c6b16b5SShri Abhyankar +  ts - time stepping context
5705a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5706a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57079c6b16b5SShri Abhyankar 
57089c6b16b5SShri Abhyankar    Output Arguments:
5709a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57107453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5711a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57129c6b16b5SShri Abhyankar 
57139c6b16b5SShri Abhyankar    Level: developer
57149c6b16b5SShri Abhyankar 
5715deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57169c6b16b5SShri Abhyankar @*/
57177453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57189c6b16b5SShri Abhyankar {
57199c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57209c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57217453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57227453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57239c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57247453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
57257453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
57267453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57279c6b16b5SShri Abhyankar 
57289c6b16b5SShri Abhyankar   PetscFunctionBegin;
57299c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5730a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5731a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5732a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5733a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5734a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5735a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57368a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57378a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5738a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57399c6b16b5SShri Abhyankar 
57409c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57419c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57429c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57439c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57449c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57457453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
57467453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
57477453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
57489c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57499c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57519c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57529c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
575376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57547453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57557453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57567453f775SEmil Constantinescu       if (tola>0.){
57577453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57587453f775SEmil Constantinescu         na_loc++;
57597453f775SEmil Constantinescu       }
57607453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57617453f775SEmil Constantinescu       if (tolr>0.){
57627453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57637453f775SEmil Constantinescu         nr_loc++;
57647453f775SEmil Constantinescu       }
57657453f775SEmil Constantinescu       tol=tola+tolr;
57667453f775SEmil Constantinescu       if (tol>0.){
57677453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57687453f775SEmil Constantinescu         n_loc++;
57697453f775SEmil Constantinescu       }
57709c6b16b5SShri Abhyankar     }
57719c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57729c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57739c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57749c6b16b5SShri Abhyankar     const PetscScalar *atol;
57759c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57769c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
577776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57787453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57797453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57807453f775SEmil Constantinescu       if (tola>0.){
57817453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57827453f775SEmil Constantinescu         na_loc++;
57837453f775SEmil Constantinescu       }
57847453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57857453f775SEmil Constantinescu       if (tolr>0.){
57867453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57877453f775SEmil Constantinescu         nr_loc++;
57887453f775SEmil Constantinescu       }
57897453f775SEmil Constantinescu       tol=tola+tolr;
57907453f775SEmil Constantinescu       if (tol>0.){
57917453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57927453f775SEmil Constantinescu         n_loc++;
57937453f775SEmil Constantinescu       }
57949c6b16b5SShri Abhyankar     }
57959c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57969c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57979c6b16b5SShri Abhyankar     const PetscScalar *rtol;
57989c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57999c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
580076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58017453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58027453f775SEmil Constantinescu       tola = ts->atol;
58037453f775SEmil Constantinescu       if (tola>0.){
58047453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58057453f775SEmil Constantinescu         na_loc++;
58067453f775SEmil Constantinescu       }
58077453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58087453f775SEmil Constantinescu       if (tolr>0.){
58097453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58107453f775SEmil Constantinescu         nr_loc++;
58117453f775SEmil Constantinescu       }
58127453f775SEmil Constantinescu       tol=tola+tolr;
58137453f775SEmil Constantinescu       if (tol>0.){
58147453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58157453f775SEmil Constantinescu         n_loc++;
58167453f775SEmil Constantinescu       }
58179c6b16b5SShri Abhyankar     }
58189c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58199c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58209c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
582176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58237453f775SEmil Constantinescu       tola = ts->atol;
58247453f775SEmil Constantinescu       if (tola>0.){
58257453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58267453f775SEmil Constantinescu         na_loc++;
58277453f775SEmil Constantinescu       }
58287453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58297453f775SEmil Constantinescu       if (tolr>0.){
58307453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58317453f775SEmil Constantinescu         nr_loc++;
58327453f775SEmil Constantinescu       }
58337453f775SEmil Constantinescu       tol=tola+tolr;
58347453f775SEmil Constantinescu       if (tol>0.){
58357453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58367453f775SEmil Constantinescu         n_loc++;
58377453f775SEmil Constantinescu       }
58389c6b16b5SShri Abhyankar     }
58399c6b16b5SShri Abhyankar   }
58409c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58419c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58429c6b16b5SShri Abhyankar 
58437453f775SEmil Constantinescu   err_loc[0] = sum;
58447453f775SEmil Constantinescu   err_loc[1] = suma;
58457453f775SEmil Constantinescu   err_loc[2] = sumr;
58467453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58477453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58487453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58497453f775SEmil Constantinescu 
5850a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58517453f775SEmil Constantinescu 
58527453f775SEmil Constantinescu   gsum   = err_glb[0];
58537453f775SEmil Constantinescu   gsuma  = err_glb[1];
58547453f775SEmil Constantinescu   gsumr  = err_glb[2];
58557453f775SEmil Constantinescu   n_glb  = err_glb[3];
58567453f775SEmil Constantinescu   na_glb = err_glb[4];
58577453f775SEmil Constantinescu   nr_glb = err_glb[5];
58587453f775SEmil Constantinescu 
5859b1316ef9SEmil Constantinescu   *norm  = 0.;
5860b1316ef9SEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
5861b1316ef9SEmil Constantinescu   *norma = 0.;
5862b1316ef9SEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5863b1316ef9SEmil Constantinescu   *normr = 0.;
5864b1316ef9SEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58659c6b16b5SShri Abhyankar 
58669c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58677453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58687453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58699c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
58709c6b16b5SShri Abhyankar }
58719c6b16b5SShri Abhyankar 
58729c6b16b5SShri Abhyankar /*@
5873a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
58749c6b16b5SShri Abhyankar 
58759c6b16b5SShri Abhyankar    Collective on TS
58769c6b16b5SShri Abhyankar 
58779c6b16b5SShri Abhyankar    Input Arguments:
58789c6b16b5SShri Abhyankar +  ts - time stepping context
5879a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5880a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
58819c6b16b5SShri Abhyankar 
58829c6b16b5SShri Abhyankar    Output Arguments:
5883a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58847453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5885a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58869c6b16b5SShri Abhyankar 
58879c6b16b5SShri Abhyankar    Level: developer
58889c6b16b5SShri Abhyankar 
5889deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
58909c6b16b5SShri Abhyankar @*/
58917453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58929c6b16b5SShri Abhyankar {
58939c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
58947453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
58959c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
58967453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
58977453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
58987453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
58999c6b16b5SShri Abhyankar 
59009c6b16b5SShri Abhyankar   PetscFunctionBegin;
59019c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5902a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5903a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5904a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5905a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5906a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5907a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59088a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59098a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5910a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59119c6b16b5SShri Abhyankar 
59129c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59139c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59149c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59159c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59169c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59177453f775SEmil Constantinescu 
59187453f775SEmil Constantinescu   max=0.;
59197453f775SEmil Constantinescu   maxa=0.;
59207453f775SEmil Constantinescu   maxr=0.;
59217453f775SEmil Constantinescu 
59227453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59239c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59249c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59259c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59267453f775SEmil Constantinescu 
59277453f775SEmil Constantinescu     for (i=0; i<n; i++) {
592876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59297453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59307453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59317453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59327453f775SEmil Constantinescu       tol  = tola+tolr;
59337453f775SEmil Constantinescu       if (tola>0.){
59347453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59357453f775SEmil Constantinescu       }
59367453f775SEmil Constantinescu       if (tolr>0.){
59377453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59387453f775SEmil Constantinescu       }
59397453f775SEmil Constantinescu       if (tol>0.){
59407453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59417453f775SEmil Constantinescu       }
59429c6b16b5SShri Abhyankar     }
59439c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59449c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59459c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59469c6b16b5SShri Abhyankar     const PetscScalar *atol;
59479c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59487453f775SEmil Constantinescu     for (i=0; i<n; i++) {
594976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59507453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59517453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59527453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59537453f775SEmil Constantinescu       tol  = tola+tolr;
59547453f775SEmil Constantinescu       if (tola>0.){
59557453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59567453f775SEmil Constantinescu       }
59577453f775SEmil Constantinescu       if (tolr>0.){
59587453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59597453f775SEmil Constantinescu       }
59607453f775SEmil Constantinescu       if (tol>0.){
59617453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59627453f775SEmil Constantinescu       }
59639c6b16b5SShri Abhyankar     }
59649c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59659c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59669c6b16b5SShri Abhyankar     const PetscScalar *rtol;
59679c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59687453f775SEmil Constantinescu 
59697453f775SEmil Constantinescu     for (i=0; i<n; i++) {
597076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59717453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59727453f775SEmil Constantinescu       tola = ts->atol;
59737453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59747453f775SEmil Constantinescu       tol  = tola+tolr;
59757453f775SEmil Constantinescu       if (tola>0.){
59767453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59777453f775SEmil Constantinescu       }
59787453f775SEmil Constantinescu       if (tolr>0.){
59797453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59807453f775SEmil Constantinescu       }
59817453f775SEmil Constantinescu       if (tol>0.){
59827453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59837453f775SEmil Constantinescu       }
59849c6b16b5SShri Abhyankar     }
59859c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59869c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
59877453f775SEmil Constantinescu 
59887453f775SEmil Constantinescu     for (i=0; i<n; i++) {
598976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59907453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59917453f775SEmil Constantinescu       tola = ts->atol;
59927453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59937453f775SEmil Constantinescu       tol  = tola+tolr;
59947453f775SEmil Constantinescu       if (tola>0.){
59957453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59967453f775SEmil Constantinescu       }
59977453f775SEmil Constantinescu       if (tolr>0.){
59987453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59997453f775SEmil Constantinescu       }
60007453f775SEmil Constantinescu       if (tol>0.){
60017453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60027453f775SEmil Constantinescu       }
60039c6b16b5SShri Abhyankar     }
60049c6b16b5SShri Abhyankar   }
60059c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60069c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60077453f775SEmil Constantinescu   err_loc[0] = max;
60087453f775SEmil Constantinescu   err_loc[1] = maxa;
60097453f775SEmil Constantinescu   err_loc[2] = maxr;
6010a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60117453f775SEmil Constantinescu   gmax   = err_glb[0];
60127453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60137453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60149c6b16b5SShri Abhyankar 
60159c6b16b5SShri Abhyankar   *norm = gmax;
60167453f775SEmil Constantinescu   *norma = gmaxa;
60177453f775SEmil Constantinescu   *normr = gmaxr;
60189c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60197453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60207453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60219c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60229c6b16b5SShri Abhyankar }
60239c6b16b5SShri Abhyankar 
60241c3436cfSJed Brown /*@
60258a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60261c3436cfSJed Brown 
60271c3436cfSJed Brown    Collective on TS
60281c3436cfSJed Brown 
60291c3436cfSJed Brown    Input Arguments:
60301c3436cfSJed Brown +  ts - time stepping context
6031a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6032a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6033a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60347619abb3SShri 
60351c3436cfSJed Brown    Output Arguments:
6036a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60378a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6038a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6039a4868fbcSLisandro Dalcin 
6040a4868fbcSLisandro Dalcin    Options Database Keys:
6041a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6042a4868fbcSLisandro Dalcin 
60431c3436cfSJed Brown    Level: developer
60441c3436cfSJed Brown 
60458a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
60461c3436cfSJed Brown @*/
60477453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60481c3436cfSJed Brown {
60498beabaa1SBarry Smith   PetscErrorCode ierr;
60501c3436cfSJed Brown 
60511c3436cfSJed Brown   PetscFunctionBegin;
6052a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60537453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6054a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
60557453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6056a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60571c3436cfSJed Brown   PetscFunctionReturn(0);
60581c3436cfSJed Brown }
60591c3436cfSJed Brown 
60608a175baeSEmil Constantinescu 
60618a175baeSEmil Constantinescu /*@
60628a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
60638a175baeSEmil Constantinescu 
60648a175baeSEmil Constantinescu    Collective on TS
60658a175baeSEmil Constantinescu 
60668a175baeSEmil Constantinescu    Input Arguments:
60678a175baeSEmil Constantinescu +  ts - time stepping context
60688a175baeSEmil Constantinescu .  E - error vector
60698a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60708a175baeSEmil Constantinescu -  Y - state vector, previous time step
60718a175baeSEmil Constantinescu 
60728a175baeSEmil Constantinescu    Output Arguments:
6073a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60748a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6075a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60768a175baeSEmil Constantinescu 
60778a175baeSEmil Constantinescu    Level: developer
60788a175baeSEmil Constantinescu 
60798a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
60808a175baeSEmil Constantinescu @*/
60818a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60828a175baeSEmil Constantinescu {
60838a175baeSEmil Constantinescu   PetscErrorCode    ierr;
60848a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
60858a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
60868a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
60878a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
60888a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
60898a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
60908a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
60918a175baeSEmil Constantinescu 
60928a175baeSEmil Constantinescu   PetscFunctionBegin;
60938a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60948a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
60958a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
60968a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
60978a175baeSEmil Constantinescu   PetscValidType(E,2);
60988a175baeSEmil Constantinescu   PetscValidType(U,3);
60998a175baeSEmil Constantinescu   PetscValidType(Y,4);
61008a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61018a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61028a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61038a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61048a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61058a175baeSEmil Constantinescu 
61068a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61078a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61088a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61098a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61108a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61118a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61128a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61138a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61148a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61158a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61168a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61178a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61188a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61198a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
612076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61218a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61228a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61238a175baeSEmil Constantinescu       if (tola>0.){
61248a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61258a175baeSEmil Constantinescu         na_loc++;
61268a175baeSEmil Constantinescu       }
61278a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61288a175baeSEmil Constantinescu       if (tolr>0.){
61298a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61308a175baeSEmil Constantinescu         nr_loc++;
61318a175baeSEmil Constantinescu       }
61328a175baeSEmil Constantinescu       tol=tola+tolr;
61338a175baeSEmil Constantinescu       if (tol>0.){
61348a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61358a175baeSEmil Constantinescu         n_loc++;
61368a175baeSEmil Constantinescu       }
61378a175baeSEmil Constantinescu     }
61388a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61398a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61408a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61418a175baeSEmil Constantinescu     const PetscScalar *atol;
61428a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61438a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
614476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61458a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61468a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61478a175baeSEmil Constantinescu       if (tola>0.){
61488a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61498a175baeSEmil Constantinescu         na_loc++;
61508a175baeSEmil Constantinescu       }
61518a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61528a175baeSEmil Constantinescu       if (tolr>0.){
61538a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61548a175baeSEmil Constantinescu         nr_loc++;
61558a175baeSEmil Constantinescu       }
61568a175baeSEmil Constantinescu       tol=tola+tolr;
61578a175baeSEmil Constantinescu       if (tol>0.){
61588a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61598a175baeSEmil Constantinescu         n_loc++;
61608a175baeSEmil Constantinescu       }
61618a175baeSEmil Constantinescu     }
61628a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61638a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61648a175baeSEmil Constantinescu     const PetscScalar *rtol;
61658a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61668a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
616776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61688a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61698a175baeSEmil Constantinescu       tola = ts->atol;
61708a175baeSEmil Constantinescu       if (tola>0.){
61718a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61728a175baeSEmil Constantinescu         na_loc++;
61738a175baeSEmil Constantinescu       }
61748a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61758a175baeSEmil Constantinescu       if (tolr>0.){
61768a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61778a175baeSEmil Constantinescu         nr_loc++;
61788a175baeSEmil Constantinescu       }
61798a175baeSEmil Constantinescu       tol=tola+tolr;
61808a175baeSEmil Constantinescu       if (tol>0.){
61818a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61828a175baeSEmil Constantinescu         n_loc++;
61838a175baeSEmil Constantinescu       }
61848a175baeSEmil Constantinescu     }
61858a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61868a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
61878a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
618876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61898a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61908a175baeSEmil Constantinescu       tola = ts->atol;
61918a175baeSEmil Constantinescu       if (tola>0.){
61928a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61938a175baeSEmil Constantinescu         na_loc++;
61948a175baeSEmil Constantinescu       }
61958a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61968a175baeSEmil Constantinescu       if (tolr>0.){
61978a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61988a175baeSEmil Constantinescu         nr_loc++;
61998a175baeSEmil Constantinescu       }
62008a175baeSEmil Constantinescu       tol=tola+tolr;
62018a175baeSEmil Constantinescu       if (tol>0.){
62028a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62038a175baeSEmil Constantinescu         n_loc++;
62048a175baeSEmil Constantinescu       }
62058a175baeSEmil Constantinescu     }
62068a175baeSEmil Constantinescu   }
62078a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62088a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62098a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62108a175baeSEmil Constantinescu 
62118a175baeSEmil Constantinescu   err_loc[0] = sum;
62128a175baeSEmil Constantinescu   err_loc[1] = suma;
62138a175baeSEmil Constantinescu   err_loc[2] = sumr;
62148a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62158a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62168a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62178a175baeSEmil Constantinescu 
6218a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62198a175baeSEmil Constantinescu 
62208a175baeSEmil Constantinescu   gsum   = err_glb[0];
62218a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62228a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62238a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62248a175baeSEmil Constantinescu   na_glb = err_glb[4];
62258a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62268a175baeSEmil Constantinescu 
62278a175baeSEmil Constantinescu   *norm  = 0.;
62288a175baeSEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
62298a175baeSEmil Constantinescu   *norma = 0.;
62308a175baeSEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62318a175baeSEmil Constantinescu   *normr = 0.;
62328a175baeSEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62338a175baeSEmil Constantinescu 
62348a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62358a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62368a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62378a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62388a175baeSEmil Constantinescu }
62398a175baeSEmil Constantinescu 
62408a175baeSEmil Constantinescu /*@
62418a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62428a175baeSEmil Constantinescu    Collective on TS
62438a175baeSEmil Constantinescu 
62448a175baeSEmil Constantinescu    Input Arguments:
62458a175baeSEmil Constantinescu +  ts - time stepping context
62468a175baeSEmil Constantinescu .  E - error vector
62478a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62488a175baeSEmil Constantinescu -  Y - state vector, previous time step
62498a175baeSEmil Constantinescu 
62508a175baeSEmil Constantinescu    Output Arguments:
6251a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62528a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6253a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62548a175baeSEmil Constantinescu 
62558a175baeSEmil Constantinescu    Level: developer
62568a175baeSEmil Constantinescu 
62578a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62588a175baeSEmil Constantinescu @*/
62598a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62608a175baeSEmil Constantinescu {
62618a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62628a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62638a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62648a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
62658a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62668a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
62678a175baeSEmil Constantinescu 
62688a175baeSEmil Constantinescu   PetscFunctionBegin;
62698a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62708a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
62718a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
62728a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
62738a175baeSEmil Constantinescu   PetscValidType(E,2);
62748a175baeSEmil Constantinescu   PetscValidType(U,3);
62758a175baeSEmil Constantinescu   PetscValidType(Y,4);
62768a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
62778a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
62788a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
62798a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
62808a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
62818a175baeSEmil Constantinescu 
62828a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
62838a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
62848a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
62858a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
62868a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62878a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62888a175baeSEmil Constantinescu 
62898a175baeSEmil Constantinescu   max=0.;
62908a175baeSEmil Constantinescu   maxa=0.;
62918a175baeSEmil Constantinescu   maxr=0.;
62928a175baeSEmil Constantinescu 
62938a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
62948a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
62958a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62968a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62978a175baeSEmil Constantinescu 
62988a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
629976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63008a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63018a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63028a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63038a175baeSEmil Constantinescu       tol  = tola+tolr;
63048a175baeSEmil Constantinescu       if (tola>0.){
63058a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63068a175baeSEmil Constantinescu       }
63078a175baeSEmil Constantinescu       if (tolr>0.){
63088a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63098a175baeSEmil Constantinescu       }
63108a175baeSEmil Constantinescu       if (tol>0.){
63118a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63128a175baeSEmil Constantinescu       }
63138a175baeSEmil Constantinescu     }
63148a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63158a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63168a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63178a175baeSEmil Constantinescu     const PetscScalar *atol;
63188a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63198a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
632076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63218a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63228a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63238a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63248a175baeSEmil Constantinescu       tol  = tola+tolr;
63258a175baeSEmil Constantinescu       if (tola>0.){
63268a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63278a175baeSEmil Constantinescu       }
63288a175baeSEmil Constantinescu       if (tolr>0.){
63298a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63308a175baeSEmil Constantinescu       }
63318a175baeSEmil Constantinescu       if (tol>0.){
63328a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63338a175baeSEmil Constantinescu       }
63348a175baeSEmil Constantinescu     }
63358a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63368a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63378a175baeSEmil Constantinescu     const PetscScalar *rtol;
63388a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63398a175baeSEmil Constantinescu 
63408a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
634176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63428a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63438a175baeSEmil Constantinescu       tola = ts->atol;
63448a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63458a175baeSEmil Constantinescu       tol  = tola+tolr;
63468a175baeSEmil Constantinescu       if (tola>0.){
63478a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63488a175baeSEmil Constantinescu       }
63498a175baeSEmil Constantinescu       if (tolr>0.){
63508a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63518a175baeSEmil Constantinescu       }
63528a175baeSEmil Constantinescu       if (tol>0.){
63538a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63548a175baeSEmil Constantinescu       }
63558a175baeSEmil Constantinescu     }
63568a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63578a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63588a175baeSEmil Constantinescu 
63598a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
636076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63618a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63628a175baeSEmil Constantinescu       tola = ts->atol;
63638a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63648a175baeSEmil Constantinescu       tol  = tola+tolr;
63658a175baeSEmil Constantinescu       if (tola>0.){
63668a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63678a175baeSEmil Constantinescu       }
63688a175baeSEmil Constantinescu       if (tolr>0.){
63698a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63708a175baeSEmil Constantinescu       }
63718a175baeSEmil Constantinescu       if (tol>0.){
63728a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63738a175baeSEmil Constantinescu       }
63748a175baeSEmil Constantinescu     }
63758a175baeSEmil Constantinescu   }
63768a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
63778a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63788a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63798a175baeSEmil Constantinescu   err_loc[0] = max;
63808a175baeSEmil Constantinescu   err_loc[1] = maxa;
63818a175baeSEmil Constantinescu   err_loc[2] = maxr;
6382a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63838a175baeSEmil Constantinescu   gmax   = err_glb[0];
63848a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
63858a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
63868a175baeSEmil Constantinescu 
63878a175baeSEmil Constantinescu   *norm = gmax;
63888a175baeSEmil Constantinescu   *norma = gmaxa;
63898a175baeSEmil Constantinescu   *normr = gmaxr;
63908a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63918a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63928a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63938a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63948a175baeSEmil Constantinescu }
63958a175baeSEmil Constantinescu 
63968a175baeSEmil Constantinescu /*@
63978a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
63988a175baeSEmil Constantinescu 
63998a175baeSEmil Constantinescu    Collective on TS
64008a175baeSEmil Constantinescu 
64018a175baeSEmil Constantinescu    Input Arguments:
64028a175baeSEmil Constantinescu +  ts - time stepping context
64038a175baeSEmil Constantinescu .  E - error vector
64048a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64058a175baeSEmil Constantinescu .  Y - state vector, previous time step
64068a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64078a175baeSEmil Constantinescu 
64088a175baeSEmil Constantinescu    Output Arguments:
6409a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64108a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6411a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64128a175baeSEmil Constantinescu 
64138a175baeSEmil Constantinescu    Options Database Keys:
64148a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64158a175baeSEmil Constantinescu 
64168a175baeSEmil Constantinescu    Level: developer
64178a175baeSEmil Constantinescu 
64188a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64198a175baeSEmil Constantinescu @*/
64208a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64218a175baeSEmil Constantinescu {
64228a175baeSEmil Constantinescu   PetscErrorCode ierr;
64238a175baeSEmil Constantinescu 
64248a175baeSEmil Constantinescu   PetscFunctionBegin;
64258a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64268a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64278a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
64288a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64298a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64308a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64318a175baeSEmil Constantinescu }
64328a175baeSEmil Constantinescu 
64338a175baeSEmil Constantinescu 
64348d59e960SJed Brown /*@
64358d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64368d59e960SJed Brown 
64378d59e960SJed Brown    Logically Collective on TS
64388d59e960SJed Brown 
64398d59e960SJed Brown    Input Arguments:
64408d59e960SJed Brown +  ts - time stepping context
64418d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64428d59e960SJed Brown 
64438d59e960SJed Brown    Note:
64448d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64458d59e960SJed Brown 
64468d59e960SJed Brown    Level: intermediate
64478d59e960SJed Brown 
64488d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64498d59e960SJed Brown @*/
64508d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64518d59e960SJed Brown {
64528d59e960SJed Brown   PetscFunctionBegin;
64538d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64548d59e960SJed Brown   ts->cfltime_local = cfltime;
64558d59e960SJed Brown   ts->cfltime       = -1.;
64568d59e960SJed Brown   PetscFunctionReturn(0);
64578d59e960SJed Brown }
64588d59e960SJed Brown 
64598d59e960SJed Brown /*@
64608d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
64618d59e960SJed Brown 
64628d59e960SJed Brown    Collective on TS
64638d59e960SJed Brown 
64648d59e960SJed Brown    Input Arguments:
64658d59e960SJed Brown .  ts - time stepping context
64668d59e960SJed Brown 
64678d59e960SJed Brown    Output Arguments:
64688d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
64698d59e960SJed Brown 
64708d59e960SJed Brown    Level: advanced
64718d59e960SJed Brown 
64728d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
64738d59e960SJed Brown @*/
64748d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
64758d59e960SJed Brown {
64768d59e960SJed Brown   PetscErrorCode ierr;
64778d59e960SJed Brown 
64788d59e960SJed Brown   PetscFunctionBegin;
64798d59e960SJed Brown   if (ts->cfltime < 0) {
6480b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64818d59e960SJed Brown   }
64828d59e960SJed Brown   *cfltime = ts->cfltime;
64838d59e960SJed Brown   PetscFunctionReturn(0);
64848d59e960SJed Brown }
64858d59e960SJed Brown 
6486d6ebe24aSShri Abhyankar /*@
6487d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6488d6ebe24aSShri Abhyankar 
6489d6ebe24aSShri Abhyankar    Input Parameters:
6490a2b725a8SWilliam Gropp +  ts   - the TS context.
6491d6ebe24aSShri Abhyankar .  xl   - lower bound.
6492a2b725a8SWilliam Gropp -  xu   - upper bound.
6493d6ebe24aSShri Abhyankar 
6494d6ebe24aSShri Abhyankar    Notes:
6495d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6496e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6497d6ebe24aSShri Abhyankar 
64982bd2b0e6SSatish Balay    Level: advanced
64992bd2b0e6SSatish Balay 
6500d6ebe24aSShri Abhyankar @*/
6501d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6502d6ebe24aSShri Abhyankar {
6503d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6504d6ebe24aSShri Abhyankar   SNES           snes;
6505d6ebe24aSShri Abhyankar 
6506d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6507d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6508d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6509d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6510d6ebe24aSShri Abhyankar }
6511d6ebe24aSShri Abhyankar 
6512b3603a34SBarry Smith /*@C
65134f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6514b3603a34SBarry Smith        in a time based line graph
6515b3603a34SBarry Smith 
6516b3603a34SBarry Smith    Collective on TS
6517b3603a34SBarry Smith 
6518b3603a34SBarry Smith    Input Parameters:
6519b3603a34SBarry Smith +  ts - the TS context
6520b3603a34SBarry Smith .  step - current time-step
6521b3603a34SBarry Smith .  ptime - current time
65227db568b7SBarry Smith .  u - current solution
65237db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6524b3603a34SBarry Smith 
6525b3d3934dSBarry Smith    Options Database:
65269ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6527b3d3934dSBarry Smith 
6528b3603a34SBarry Smith    Level: intermediate
6529b3603a34SBarry Smith 
653095452b02SPatrick Sanan    Notes:
653195452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6532b3603a34SBarry Smith 
65337db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65347db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65357db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65367db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6537b3603a34SBarry Smith @*/
65387db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6539b3603a34SBarry Smith {
6540b3603a34SBarry Smith   PetscErrorCode    ierr;
65417db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6542b3603a34SBarry Smith   const PetscScalar *yy;
654380666b62SBarry Smith   Vec               v;
6544b3603a34SBarry Smith 
6545b3603a34SBarry Smith   PetscFunctionBegin;
654663e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
654758ff32f7SBarry Smith   if (!step) {
6548a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65496934998bSLisandro Dalcin     PetscInt      dim;
6550a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65510ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6552bab0a581SBarry Smith     if (!ctx->names) {
6553bab0a581SBarry Smith       PetscBool flg;
6554bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6555bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6556bab0a581SBarry Smith       if (flg) {
6557bab0a581SBarry Smith         PetscInt i,n;
6558bab0a581SBarry Smith         char     **names;
6559bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6560bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6561bab0a581SBarry Smith         for (i=0; i<n; i++) {
6562bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6563bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6564bab0a581SBarry Smith         }
6565bab0a581SBarry Smith         names[n] = NULL;
6566bab0a581SBarry Smith         ctx->names = names;
6567bab0a581SBarry Smith       }
6568bab0a581SBarry Smith     }
6569387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6570387f4636SBarry Smith       char      **displaynames;
6571387f4636SBarry Smith       PetscBool flg;
6572387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6573580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6574c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6575387f4636SBarry Smith       if (flg) {
6576a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6577387f4636SBarry Smith       }
6578387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6579387f4636SBarry Smith     }
6580387f4636SBarry Smith     if (ctx->displaynames) {
6581387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6582387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6583387f4636SBarry Smith     } else if (ctx->names) {
65840910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65850b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6586387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6587b0bc92c6SBarry Smith     } else {
6588b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6589b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6590387f4636SBarry Smith     }
65910b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
659258ff32f7SBarry Smith   }
65936934998bSLisandro Dalcin 
65946934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65956934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
659680666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
65976934998bSLisandro Dalcin   if (ctx->displaynames) {
65986934998bSLisandro Dalcin     PetscInt i;
65996934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66006934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66016934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66026934998bSLisandro Dalcin   } else {
6603e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6604e3efe391SJed Brown     PetscInt  i,n;
66056934998bSLisandro Dalcin     PetscReal *yreal;
660680666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6607785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6608e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66090b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6610e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6611e3efe391SJed Brown #else
66120b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6613e3efe391SJed Brown #endif
661480666b62SBarry Smith   }
66156934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66166934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66176934998bSLisandro Dalcin 
6618b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66190b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66206934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66213923b477SBarry Smith   }
6622b3603a34SBarry Smith   PetscFunctionReturn(0);
6623b3603a34SBarry Smith }
6624b3603a34SBarry Smith 
6625b037adc7SBarry Smith /*@C
662631152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6627b037adc7SBarry Smith 
6628b037adc7SBarry Smith    Collective on TS
6629b037adc7SBarry Smith 
6630b037adc7SBarry Smith    Input Parameters:
6631b037adc7SBarry Smith +  ts - the TS context
6632b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6633b037adc7SBarry Smith 
6634b037adc7SBarry Smith    Level: intermediate
6635b037adc7SBarry Smith 
663695452b02SPatrick Sanan    Notes:
663795452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66387db568b7SBarry Smith 
6639a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6640b037adc7SBarry Smith @*/
664131152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6642b037adc7SBarry Smith {
6643b037adc7SBarry Smith   PetscErrorCode    ierr;
6644b037adc7SBarry Smith   PetscInt          i;
6645b037adc7SBarry Smith 
6646b037adc7SBarry Smith   PetscFunctionBegin;
6647b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6648b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66495537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6650b3d3934dSBarry Smith       break;
6651b3d3934dSBarry Smith     }
6652b3d3934dSBarry Smith   }
6653b3d3934dSBarry Smith   PetscFunctionReturn(0);
6654b3d3934dSBarry Smith }
6655b3d3934dSBarry Smith 
6656e673d494SBarry Smith /*@C
6657e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6658e673d494SBarry Smith 
6659e673d494SBarry Smith    Collective on TS
6660e673d494SBarry Smith 
6661e673d494SBarry Smith    Input Parameters:
6662e673d494SBarry Smith +  ts - the TS context
6663e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6664e673d494SBarry Smith 
6665e673d494SBarry Smith    Level: intermediate
6666e673d494SBarry Smith 
6667a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6668e673d494SBarry Smith @*/
6669e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6670e673d494SBarry Smith {
6671e673d494SBarry Smith   PetscErrorCode    ierr;
6672e673d494SBarry Smith 
6673e673d494SBarry Smith   PetscFunctionBegin;
6674e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6675e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6676e673d494SBarry Smith   PetscFunctionReturn(0);
6677e673d494SBarry Smith }
6678e673d494SBarry Smith 
6679b3d3934dSBarry Smith /*@C
6680b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6681b3d3934dSBarry Smith 
6682b3d3934dSBarry Smith    Collective on TS
6683b3d3934dSBarry Smith 
6684b3d3934dSBarry Smith    Input Parameter:
6685b3d3934dSBarry Smith .  ts - the TS context
6686b3d3934dSBarry Smith 
6687b3d3934dSBarry Smith    Output Parameter:
6688b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6689b3d3934dSBarry Smith 
6690b3d3934dSBarry Smith    Level: intermediate
6691b3d3934dSBarry Smith 
669295452b02SPatrick Sanan    Notes:
669395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66947db568b7SBarry Smith 
6695b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6696b3d3934dSBarry Smith @*/
6697b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6698b3d3934dSBarry Smith {
6699b3d3934dSBarry Smith   PetscInt       i;
6700b3d3934dSBarry Smith 
6701b3d3934dSBarry Smith   PetscFunctionBegin;
6702b3d3934dSBarry Smith   *names = NULL;
6703b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6704b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67055537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6706b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6707b3d3934dSBarry Smith       break;
6708387f4636SBarry Smith     }
6709387f4636SBarry Smith   }
6710387f4636SBarry Smith   PetscFunctionReturn(0);
6711387f4636SBarry Smith }
6712387f4636SBarry Smith 
6713a66092f1SBarry Smith /*@C
6714a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6715a66092f1SBarry Smith 
6716a66092f1SBarry Smith    Collective on TS
6717a66092f1SBarry Smith 
6718a66092f1SBarry Smith    Input Parameters:
6719a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6720a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6721a66092f1SBarry Smith 
6722a66092f1SBarry Smith    Level: intermediate
6723a66092f1SBarry Smith 
6724a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6725a66092f1SBarry Smith @*/
6726a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6727a66092f1SBarry Smith {
6728a66092f1SBarry Smith   PetscInt          j = 0,k;
6729a66092f1SBarry Smith   PetscErrorCode    ierr;
6730a66092f1SBarry Smith 
6731a66092f1SBarry Smith   PetscFunctionBegin;
6732a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6733a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6734a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6735a66092f1SBarry Smith   while (displaynames[j]) j++;
6736a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6737a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6738a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6739a66092f1SBarry Smith   j = 0;
6740a66092f1SBarry Smith   while (displaynames[j]) {
6741a66092f1SBarry Smith     k = 0;
6742a66092f1SBarry Smith     while (ctx->names[k]) {
6743a66092f1SBarry Smith       PetscBool flg;
6744a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6745a66092f1SBarry Smith       if (flg) {
6746a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6747a66092f1SBarry Smith         break;
6748a66092f1SBarry Smith       }
6749a66092f1SBarry Smith       k++;
6750a66092f1SBarry Smith     }
6751a66092f1SBarry Smith     j++;
6752a66092f1SBarry Smith   }
6753a66092f1SBarry Smith   PetscFunctionReturn(0);
6754a66092f1SBarry Smith }
6755a66092f1SBarry Smith 
6756387f4636SBarry Smith /*@C
6757387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6758387f4636SBarry Smith 
6759387f4636SBarry Smith    Collective on TS
6760387f4636SBarry Smith 
6761387f4636SBarry Smith    Input Parameters:
6762387f4636SBarry Smith +  ts - the TS context
6763a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6764387f4636SBarry Smith 
676595452b02SPatrick Sanan    Notes:
676695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67677db568b7SBarry Smith 
6768387f4636SBarry Smith    Level: intermediate
6769387f4636SBarry Smith 
6770387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6771387f4636SBarry Smith @*/
6772387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6773387f4636SBarry Smith {
6774a66092f1SBarry Smith   PetscInt          i;
6775387f4636SBarry Smith   PetscErrorCode    ierr;
6776387f4636SBarry Smith 
6777387f4636SBarry Smith   PetscFunctionBegin;
6778387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6779387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67805537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6781b3d3934dSBarry Smith       break;
6782b037adc7SBarry Smith     }
6783b037adc7SBarry Smith   }
6784b037adc7SBarry Smith   PetscFunctionReturn(0);
6785b037adc7SBarry Smith }
6786b037adc7SBarry Smith 
678780666b62SBarry Smith /*@C
678880666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
678980666b62SBarry Smith 
679080666b62SBarry Smith    Collective on TS
679180666b62SBarry Smith 
679280666b62SBarry Smith    Input Parameters:
679380666b62SBarry Smith +  ts - the TS context
679480666b62SBarry Smith .  transform - the transform function
67957684fa3eSBarry Smith .  destroy - function to destroy the optional context
679680666b62SBarry Smith -  ctx - optional context used by transform function
679780666b62SBarry Smith 
679895452b02SPatrick Sanan    Notes:
679995452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68007db568b7SBarry Smith 
680180666b62SBarry Smith    Level: intermediate
680280666b62SBarry Smith 
6803a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
680480666b62SBarry Smith @*/
68057684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
680680666b62SBarry Smith {
680780666b62SBarry Smith   PetscInt          i;
6808a66092f1SBarry Smith   PetscErrorCode    ierr;
680980666b62SBarry Smith 
681080666b62SBarry Smith   PetscFunctionBegin;
681180666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
681280666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68135537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
681480666b62SBarry Smith     }
681580666b62SBarry Smith   }
681680666b62SBarry Smith   PetscFunctionReturn(0);
681780666b62SBarry Smith }
681880666b62SBarry Smith 
6819e673d494SBarry Smith /*@C
6820e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6821e673d494SBarry Smith 
6822e673d494SBarry Smith    Collective on TSLGCtx
6823e673d494SBarry Smith 
6824e673d494SBarry Smith    Input Parameters:
6825e673d494SBarry Smith +  ts - the TS context
6826e673d494SBarry Smith .  transform - the transform function
68277684fa3eSBarry Smith .  destroy - function to destroy the optional context
6828e673d494SBarry Smith -  ctx - optional context used by transform function
6829e673d494SBarry Smith 
6830e673d494SBarry Smith    Level: intermediate
6831e673d494SBarry Smith 
6832a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6833e673d494SBarry Smith @*/
68347684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6835e673d494SBarry Smith {
6836e673d494SBarry Smith   PetscFunctionBegin;
6837e673d494SBarry Smith   ctx->transform    = transform;
68387684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6839e673d494SBarry Smith   ctx->transformctx = tctx;
6840e673d494SBarry Smith   PetscFunctionReturn(0);
6841e673d494SBarry Smith }
6842e673d494SBarry Smith 
6843ef20d060SBarry Smith /*@C
68448b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6845ef20d060SBarry Smith        in a time based line graph
6846ef20d060SBarry Smith 
6847ef20d060SBarry Smith    Collective on TS
6848ef20d060SBarry Smith 
6849ef20d060SBarry Smith    Input Parameters:
6850ef20d060SBarry Smith +  ts - the TS context
6851ef20d060SBarry Smith .  step - current time-step
6852ef20d060SBarry Smith .  ptime - current time
68537db568b7SBarry Smith .  u - current solution
68547db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6855ef20d060SBarry Smith 
6856ef20d060SBarry Smith    Level: intermediate
6857ef20d060SBarry Smith 
685895452b02SPatrick Sanan    Notes:
685995452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6860abd5a294SJed Brown 
6861abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6862abd5a294SJed Brown 
6863abd5a294SJed Brown    Options Database Keys:
68644f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6865ef20d060SBarry Smith 
6866abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6867ef20d060SBarry Smith @*/
68680910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6869ef20d060SBarry Smith {
6870ef20d060SBarry Smith   PetscErrorCode    ierr;
68710b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6872ef20d060SBarry Smith   const PetscScalar *yy;
6873ef20d060SBarry Smith   Vec               y;
6874ef20d060SBarry Smith 
6875ef20d060SBarry Smith   PetscFunctionBegin;
6876a9f9c1f6SBarry Smith   if (!step) {
6877a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68786934998bSLisandro Dalcin     PetscInt      dim;
6879a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68808b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68810910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6882a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6883a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6884a9f9c1f6SBarry Smith   }
68850910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6886ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68870910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6888ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6889e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6890e3efe391SJed Brown   {
6891e3efe391SJed Brown     PetscReal *yreal;
6892e3efe391SJed Brown     PetscInt  i,n;
6893e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6894785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6895e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
68960b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6897e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6898e3efe391SJed Brown   }
6899e3efe391SJed Brown #else
69000b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6901e3efe391SJed Brown #endif
6902ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6903ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6904b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69050b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69066934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69073923b477SBarry Smith   }
6908ef20d060SBarry Smith   PetscFunctionReturn(0);
6909ef20d060SBarry Smith }
6910ef20d060SBarry Smith 
69115e3b7effSJoseph Pusztay /*@C
69125e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
69135e3b7effSJoseph Pusztay 
69145e3b7effSJoseph Pusztay    Input Parameters:
69155e3b7effSJoseph Pusztay +  ts - the TS context
69165e3b7effSJoseph Pusztay .  step - current time-step
69175e3b7effSJoseph Pusztay .  ptime - current time
69185e3b7effSJoseph Pusztay .  u - current solution
69195e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
69205e3b7effSJoseph Pusztay 
69215e3b7effSJoseph Pusztay    Options Database:
6922918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
69235e3b7effSJoseph Pusztay 
69245e3b7effSJoseph Pusztay    Level: intermediate
69255e3b7effSJoseph Pusztay 
69265e3b7effSJoseph Pusztay @*/
69270ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
69281b575b74SJoseph Pusztay {
69291b575b74SJoseph Pusztay   PetscErrorCode    ierr;
69301b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
69311b575b74SJoseph Pusztay   const PetscScalar *yy;
6932b1670a61SJoseph Pusztay   PetscReal       *y,*x;
69331b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
69341b575b74SJoseph Pusztay   DM                dm;
69351b575b74SJoseph Pusztay 
69361b575b74SJoseph Pusztay   PetscFunctionBegin;
69371b575b74SJoseph Pusztay 
69381b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
69391b575b74SJoseph Pusztay   if (!step) {
69401b575b74SJoseph Pusztay     PetscDrawAxis axis;
69411b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
69421b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6943895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6944895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
69451b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
69461b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
69471b575b74SJoseph Pusztay     if (dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
69481b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69491b575b74SJoseph Pusztay     Np /= 2*dim;
69501b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69511b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69521b575b74SJoseph Pusztay   }
69531b575b74SJoseph Pusztay 
69541b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69551b575b74SJoseph Pusztay   Np /= 2*dim;
69561b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6957895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69581b575b74SJoseph Pusztay   /* get points from solution vector */
69591b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6960b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6961b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6962895f37d5SJoseph Pusztay   }
69631b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
69641b575b74SJoseph Pusztay 
69651b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69661b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
69671b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
69681b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
69691b575b74SJoseph Pusztay   }
69701b575b74SJoseph Pusztay 
6971918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6972918b1d10SJoseph Pusztay 
69731b575b74SJoseph Pusztay   PetscFunctionReturn(0);
69741b575b74SJoseph Pusztay }
69751b575b74SJoseph Pusztay 
69761b575b74SJoseph Pusztay 
69771b575b74SJoseph Pusztay 
69787cf37e64SBarry Smith /*@C
69797cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69807cf37e64SBarry Smith 
69817cf37e64SBarry Smith    Collective on TS
69827cf37e64SBarry Smith 
69837cf37e64SBarry Smith    Input Parameters:
69847cf37e64SBarry Smith +  ts - the TS context
69857cf37e64SBarry Smith .  step - current time-step
69867cf37e64SBarry Smith .  ptime - current time
69877cf37e64SBarry Smith .  u - current solution
69887cf37e64SBarry Smith -  dctx - unused context
69897cf37e64SBarry Smith 
69907cf37e64SBarry Smith    Level: intermediate
69917cf37e64SBarry Smith 
69927cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
69937cf37e64SBarry Smith 
69947cf37e64SBarry Smith    Options Database Keys:
69957cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
69967cf37e64SBarry Smith 
69977cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
69987cf37e64SBarry Smith @*/
6999edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70007cf37e64SBarry Smith {
70017cf37e64SBarry Smith   PetscErrorCode    ierr;
70027cf37e64SBarry Smith   Vec               y;
70037cf37e64SBarry Smith   PetscReal         nrm;
7004edbaebb3SBarry Smith   PetscBool         flg;
70057cf37e64SBarry Smith 
70067cf37e64SBarry Smith   PetscFunctionBegin;
70077cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70087cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70097cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7010edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7011edbaebb3SBarry Smith   if (flg) {
70127cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7013edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7014edbaebb3SBarry Smith   }
7015edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7016edbaebb3SBarry Smith   if (flg) {
7017edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7018edbaebb3SBarry Smith   }
7019edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70207cf37e64SBarry Smith   PetscFunctionReturn(0);
70217cf37e64SBarry Smith }
70227cf37e64SBarry Smith 
7023201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7024201da799SBarry Smith {
7025201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7026201da799SBarry Smith   PetscReal      x   = ptime,y;
7027201da799SBarry Smith   PetscErrorCode ierr;
7028201da799SBarry Smith   PetscInt       its;
7029201da799SBarry Smith 
7030201da799SBarry Smith   PetscFunctionBegin;
703163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7032201da799SBarry Smith   if (!n) {
7033201da799SBarry Smith     PetscDrawAxis axis;
7034201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7035201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7036201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7037201da799SBarry Smith     ctx->snes_its = 0;
7038201da799SBarry Smith   }
7039201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7040201da799SBarry Smith   y    = its - ctx->snes_its;
7041201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70423fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7043201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70446934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7045201da799SBarry Smith   }
7046201da799SBarry Smith   ctx->snes_its = its;
7047201da799SBarry Smith   PetscFunctionReturn(0);
7048201da799SBarry Smith }
7049201da799SBarry Smith 
7050201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7051201da799SBarry Smith {
7052201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7053201da799SBarry Smith   PetscReal      x   = ptime,y;
7054201da799SBarry Smith   PetscErrorCode ierr;
7055201da799SBarry Smith   PetscInt       its;
7056201da799SBarry Smith 
7057201da799SBarry Smith   PetscFunctionBegin;
705863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7059201da799SBarry Smith   if (!n) {
7060201da799SBarry Smith     PetscDrawAxis axis;
7061201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7062201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7063201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7064201da799SBarry Smith     ctx->ksp_its = 0;
7065201da799SBarry Smith   }
7066201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7067201da799SBarry Smith   y    = its - ctx->ksp_its;
7068201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
706999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7070201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70716934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7072201da799SBarry Smith   }
7073201da799SBarry Smith   ctx->ksp_its = its;
7074201da799SBarry Smith   PetscFunctionReturn(0);
7075201da799SBarry Smith }
7076f9c1d6abSBarry Smith 
7077f9c1d6abSBarry Smith /*@
7078f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7079f9c1d6abSBarry Smith 
7080d083f849SBarry Smith    Collective on TS
7081f9c1d6abSBarry Smith 
7082f9c1d6abSBarry Smith    Input Parameters:
7083f9c1d6abSBarry Smith +  ts - the TS context
7084f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7085f9c1d6abSBarry Smith 
7086f9c1d6abSBarry Smith    Output Parameters:
7087f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7088f9c1d6abSBarry Smith 
7089f9c1d6abSBarry Smith    Level: developer
7090f9c1d6abSBarry Smith 
7091f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7092f9c1d6abSBarry Smith @*/
7093f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7094f9c1d6abSBarry Smith {
7095f9c1d6abSBarry Smith   PetscErrorCode ierr;
7096f9c1d6abSBarry Smith 
7097f9c1d6abSBarry Smith   PetscFunctionBegin;
7098f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7099ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7100f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7101f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7102f9c1d6abSBarry Smith }
710324655328SShri 
7104b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7105b3d3934dSBarry Smith /*@C
7106b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7107b3d3934dSBarry Smith 
7108b3d3934dSBarry Smith    Collective on TS
7109b3d3934dSBarry Smith 
7110b3d3934dSBarry Smith    Input Parameters:
7111b3d3934dSBarry Smith .  ts  - the ODE solver object
7112b3d3934dSBarry Smith 
7113b3d3934dSBarry Smith    Output Parameter:
7114b3d3934dSBarry Smith .  ctx - the context
7115b3d3934dSBarry Smith 
7116b3d3934dSBarry Smith    Level: intermediate
7117b3d3934dSBarry Smith 
7118b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7119b3d3934dSBarry Smith 
7120b3d3934dSBarry Smith @*/
7121b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7122b3d3934dSBarry Smith {
7123b3d3934dSBarry Smith   PetscErrorCode ierr;
7124b3d3934dSBarry Smith 
7125b3d3934dSBarry Smith   PetscFunctionBegin;
7126a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7127b3d3934dSBarry Smith   PetscFunctionReturn(0);
7128b3d3934dSBarry Smith }
7129b3d3934dSBarry Smith 
7130b3d3934dSBarry Smith /*@C
7131b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7132b3d3934dSBarry Smith 
7133b3d3934dSBarry Smith    Collective on TS
7134b3d3934dSBarry Smith 
7135b3d3934dSBarry Smith    Input Parameters:
7136b3d3934dSBarry Smith +  ts - the TS context
7137b3d3934dSBarry Smith .  step - current time-step
7138b3d3934dSBarry Smith .  ptime - current time
71397db568b7SBarry Smith .  u  - current solution
71407db568b7SBarry Smith -  dctx - the envelope context
7141b3d3934dSBarry Smith 
7142b3d3934dSBarry Smith    Options Database:
7143b3d3934dSBarry Smith .  -ts_monitor_envelope
7144b3d3934dSBarry Smith 
7145b3d3934dSBarry Smith    Level: intermediate
7146b3d3934dSBarry Smith 
714795452b02SPatrick Sanan    Notes:
714895452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7149b3d3934dSBarry Smith 
71507db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7151b3d3934dSBarry Smith @*/
71527db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7153b3d3934dSBarry Smith {
7154b3d3934dSBarry Smith   PetscErrorCode       ierr;
71557db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7156b3d3934dSBarry Smith 
7157b3d3934dSBarry Smith   PetscFunctionBegin;
7158b3d3934dSBarry Smith   if (!ctx->max) {
7159b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7160b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7161b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7162b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7163b3d3934dSBarry Smith   } else {
7164b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7165b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7166b3d3934dSBarry Smith   }
7167b3d3934dSBarry Smith   PetscFunctionReturn(0);
7168b3d3934dSBarry Smith }
7169b3d3934dSBarry Smith 
7170b3d3934dSBarry Smith /*@C
7171b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7172b3d3934dSBarry Smith 
7173b3d3934dSBarry Smith    Collective on TS
7174b3d3934dSBarry Smith 
7175b3d3934dSBarry Smith    Input Parameter:
7176b3d3934dSBarry Smith .  ts - the TS context
7177b3d3934dSBarry Smith 
7178b3d3934dSBarry Smith    Output Parameter:
7179b3d3934dSBarry Smith +  max - the maximum values
7180b3d3934dSBarry Smith -  min - the minimum values
7181b3d3934dSBarry Smith 
718295452b02SPatrick Sanan    Notes:
718395452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71847db568b7SBarry Smith 
7185b3d3934dSBarry Smith    Level: intermediate
7186b3d3934dSBarry Smith 
7187b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7188b3d3934dSBarry Smith @*/
7189b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7190b3d3934dSBarry Smith {
7191b3d3934dSBarry Smith   PetscInt i;
7192b3d3934dSBarry Smith 
7193b3d3934dSBarry Smith   PetscFunctionBegin;
7194b3d3934dSBarry Smith   if (max) *max = NULL;
7195b3d3934dSBarry Smith   if (min) *min = NULL;
7196b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7197b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
71985537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7199b3d3934dSBarry Smith       if (max) *max = ctx->max;
7200b3d3934dSBarry Smith       if (min) *min = ctx->min;
7201b3d3934dSBarry Smith       break;
7202b3d3934dSBarry Smith     }
7203b3d3934dSBarry Smith   }
7204b3d3934dSBarry Smith   PetscFunctionReturn(0);
7205b3d3934dSBarry Smith }
7206b3d3934dSBarry Smith 
7207b3d3934dSBarry Smith /*@C
7208b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7209b3d3934dSBarry Smith 
7210b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7211b3d3934dSBarry Smith 
7212b3d3934dSBarry Smith    Input Parameter:
7213b3d3934dSBarry Smith .  ctx - the monitor context
7214b3d3934dSBarry Smith 
7215b3d3934dSBarry Smith    Level: intermediate
7216b3d3934dSBarry Smith 
72177db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7218b3d3934dSBarry Smith @*/
7219b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7220b3d3934dSBarry Smith {
7221b3d3934dSBarry Smith   PetscErrorCode ierr;
7222b3d3934dSBarry Smith 
7223b3d3934dSBarry Smith   PetscFunctionBegin;
7224b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7225b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7226b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7227b3d3934dSBarry Smith   PetscFunctionReturn(0);
7228b3d3934dSBarry Smith }
7229f2dee214SBarry Smith 
723024655328SShri /*@
7231dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7232dcb233daSLisandro Dalcin 
7233dcb233daSLisandro Dalcin    Collective on TS
7234dcb233daSLisandro Dalcin 
7235dcb233daSLisandro Dalcin    Input Parameter:
7236dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7237dcb233daSLisandro Dalcin 
7238dcb233daSLisandro Dalcin    Level: advanced
7239dcb233daSLisandro Dalcin 
7240dcb233daSLisandro Dalcin    Notes:
7241dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7242dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7243dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7244dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7245dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7246dcb233daSLisandro Dalcin    discontinuous source terms).
7247dcb233daSLisandro Dalcin 
7248dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7249dcb233daSLisandro Dalcin @*/
7250dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7251dcb233daSLisandro Dalcin {
7252dcb233daSLisandro Dalcin   PetscFunctionBegin;
7253dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7254dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7255dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7256dcb233daSLisandro Dalcin }
7257dcb233daSLisandro Dalcin 
7258dcb233daSLisandro Dalcin /*@
725924655328SShri    TSRollBack - Rolls back one time step
726024655328SShri 
726124655328SShri    Collective on TS
726224655328SShri 
726324655328SShri    Input Parameter:
726424655328SShri .  ts - the TS context obtained from TSCreate()
726524655328SShri 
726624655328SShri    Level: advanced
726724655328SShri 
726824655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
726924655328SShri @*/
727024655328SShri PetscErrorCode  TSRollBack(TS ts)
727124655328SShri {
727224655328SShri   PetscErrorCode ierr;
727324655328SShri 
727424655328SShri   PetscFunctionBegin;
727524655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7276b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
727724655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
727824655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
727924655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
728024655328SShri   ts->ptime = ts->ptime_prev;
7281be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
72822808aa04SLisandro Dalcin   ts->steps--;
7283b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
728424655328SShri   PetscFunctionReturn(0);
728524655328SShri }
7286aeb4809dSShri Abhyankar 
7287ff22ae23SHong Zhang /*@
7288ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7289ff22ae23SHong Zhang 
7290ff22ae23SHong Zhang    Input Parameter:
7291ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7292ff22ae23SHong Zhang 
72930429704eSStefano Zampini    Output Parameters:
72940429704eSStefano Zampini +  ns - the number of stages
72950429704eSStefano Zampini -  Y - the current stage vectors
72960429704eSStefano Zampini 
7297ff22ae23SHong Zhang    Level: advanced
7298ff22ae23SHong Zhang 
72990429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
73000429704eSStefano Zampini 
7301ff22ae23SHong Zhang .seealso: TSCreate()
7302ff22ae23SHong Zhang @*/
7303ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7304ff22ae23SHong Zhang {
7305ff22ae23SHong Zhang   PetscErrorCode ierr;
7306ff22ae23SHong Zhang 
7307ff22ae23SHong Zhang   PetscFunctionBegin;
7308ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
73090429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
73100429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
73110429704eSStefano Zampini   if (!ts->ops->getstages) {
73120429704eSStefano Zampini     if (ns) *ns = 0;
73130429704eSStefano Zampini     if (Y) *Y = NULL;
73140429704eSStefano Zampini   } else {
7315ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7316ff22ae23SHong Zhang   }
7317ff22ae23SHong Zhang   PetscFunctionReturn(0);
7318ff22ae23SHong Zhang }
7319ff22ae23SHong Zhang 
7320847ff0e1SMatthew G. Knepley /*@C
7321847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7322847ff0e1SMatthew G. Knepley 
7323847ff0e1SMatthew G. Knepley   Collective on SNES
7324847ff0e1SMatthew G. Knepley 
7325847ff0e1SMatthew G. Knepley   Input Parameters:
7326847ff0e1SMatthew G. Knepley + ts - the TS context
7327847ff0e1SMatthew G. Knepley . t - current timestep
7328847ff0e1SMatthew G. Knepley . U - state vector
7329847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7330847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7331847ff0e1SMatthew G. Knepley - ctx - an optional user context
7332847ff0e1SMatthew G. Knepley 
7333847ff0e1SMatthew G. Knepley   Output Parameters:
7334847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7335847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7336847ff0e1SMatthew G. Knepley 
7337847ff0e1SMatthew G. Knepley   Level: intermediate
7338847ff0e1SMatthew G. Knepley 
7339847ff0e1SMatthew G. Knepley   Notes:
7340847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7341847ff0e1SMatthew G. Knepley 
7342847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7343847ff0e1SMatthew G. Knepley 
7344847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7345847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7346847ff0e1SMatthew G. Knepley 
7347847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7348847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7349847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7350847ff0e1SMatthew G. Knepley 
7351847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7352847ff0e1SMatthew G. Knepley @*/
7353847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7354847ff0e1SMatthew G. Knepley {
7355847ff0e1SMatthew G. Knepley   SNES           snes;
7356847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7357847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7358847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7359847ff0e1SMatthew G. Knepley 
7360847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7361c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7362847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7363847ff0e1SMatthew G. Knepley   if (!color) {
7364847ff0e1SMatthew G. Knepley     DM         dm;
7365847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7366847ff0e1SMatthew G. Knepley 
7367847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7368847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7369847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7370847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7371847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7372847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7373847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7374847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7375847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7376847ff0e1SMatthew G. Knepley     } else {
7377847ff0e1SMatthew G. Knepley       MatColoring mc;
7378847ff0e1SMatthew G. Knepley 
7379847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7380847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7381847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7382847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7383847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7384847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7385847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7386847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7387847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7388847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7389847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7390847ff0e1SMatthew G. Knepley     }
7391847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7392847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7393847ff0e1SMatthew G. Knepley   }
7394847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7395847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7396847ff0e1SMatthew G. Knepley   if (J != B) {
7397847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7398847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7399847ff0e1SMatthew G. Knepley   }
7400847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7401847ff0e1SMatthew G. Knepley }
740293b34091SDebojyoti Ghosh 
7403cb9d8021SPierre Barbier de Reuille /*@
74046bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7405cb9d8021SPierre Barbier de Reuille 
7406cb9d8021SPierre Barbier de Reuille     Input Parameters:
74076bc98fa9SBarry Smith +    ts - the TS context
74086bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
74096bc98fa9SBarry Smith 
74106bc98fa9SBarry Smith     Calling sequence of func:
74116bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
74126bc98fa9SBarry Smith 
74136bc98fa9SBarry Smith +   ts - the TS context
74146bc98fa9SBarry Smith .   time - the current time (of the stage)
74156bc98fa9SBarry Smith .   state - the state to check if it is valid
74166bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7417cb9d8021SPierre Barbier de Reuille 
7418cb9d8021SPierre Barbier de Reuille     Level: intermediate
7419cb9d8021SPierre Barbier de Reuille 
74206bc98fa9SBarry Smith     Notes:
74216bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
74226bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
74236bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
74246bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
74256bc98fa9SBarry Smith 
74266bc98fa9SBarry Smith     Developer Notes:
74276bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
74286bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
74296bc98fa9SBarry Smith 
74306bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7431cb9d8021SPierre Barbier de Reuille @*/
7432cb9d8021SPierre Barbier de Reuille 
7433d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7434cb9d8021SPierre Barbier de Reuille {
7435cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7436cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7437cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7438cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7439cb9d8021SPierre Barbier de Reuille }
7440cb9d8021SPierre Barbier de Reuille 
7441cb9d8021SPierre Barbier de Reuille /*@
74426bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7443cb9d8021SPierre Barbier de Reuille 
7444cb9d8021SPierre Barbier de Reuille     Input Parameters:
74456bc98fa9SBarry Smith +    ts - the TS context
74466bc98fa9SBarry Smith .    stagetime - time of the simulation
74476bc98fa9SBarry Smith -    Y - state vector to check.
7448cb9d8021SPierre Barbier de Reuille 
7449cb9d8021SPierre Barbier de Reuille     Output Parameter:
74506bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7451cb9d8021SPierre Barbier de Reuille 
7452cb9d8021SPierre Barbier de Reuille     Note:
74536bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74546bc98fa9SBarry Smith     to check if the current state is valid.
745596a0c994SBarry Smith 
74566bc98fa9SBarry Smith     Level: developer
74576bc98fa9SBarry Smith 
74586bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7459cb9d8021SPierre Barbier de Reuille @*/
7460d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7461cb9d8021SPierre Barbier de Reuille {
7462cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7463cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7464cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7465cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7466d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7467cb9d8021SPierre Barbier de Reuille   }
7468cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7469cb9d8021SPierre Barbier de Reuille }
74701ceb14c0SBarry Smith 
747193b34091SDebojyoti Ghosh /*@C
7472e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
747393b34091SDebojyoti Ghosh 
7474d083f849SBarry Smith   Collective
747593b34091SDebojyoti Ghosh 
747693b34091SDebojyoti Ghosh   Input Parameter:
747793b34091SDebojyoti Ghosh . tsin    - The input TS
747893b34091SDebojyoti Ghosh 
747993b34091SDebojyoti Ghosh   Output Parameter:
7480e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
748193b34091SDebojyoti Ghosh 
74825eca1a21SEmil Constantinescu   Notes:
74835eca1a21SEmil 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.
74845eca1a21SEmil Constantinescu 
7485928bb9adSStefano 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);
74865eca1a21SEmil Constantinescu 
74875eca1a21SEmil Constantinescu   Level: developer
748893b34091SDebojyoti Ghosh 
7489e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
749093b34091SDebojyoti Ghosh @*/
7491baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
749293b34091SDebojyoti Ghosh {
749393b34091SDebojyoti Ghosh   TS             t;
749493b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7495dc846ba4SSatish Balay   SNES           snes_start;
7496dc846ba4SSatish Balay   DM             dm;
7497dc846ba4SSatish Balay   TSType         type;
749893b34091SDebojyoti Ghosh 
749993b34091SDebojyoti Ghosh   PetscFunctionBegin;
750093b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
750193b34091SDebojyoti Ghosh   *tsout = NULL;
750293b34091SDebojyoti Ghosh 
75037a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
750493b34091SDebojyoti Ghosh 
750593b34091SDebojyoti Ghosh   /* General TS description */
750693b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
750793b34091SDebojyoti Ghosh   t->setupcalled       = 0;
750893b34091SDebojyoti Ghosh   t->ksp_its           = 0;
750993b34091SDebojyoti Ghosh   t->snes_its          = 0;
751093b34091SDebojyoti Ghosh   t->nwork             = 0;
75117d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
751293b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
751393b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
751493b34091SDebojyoti Ghosh 
751534561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
751634561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7517d15a3a53SEmil Constantinescu 
751893b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
751993b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
752093b34091SDebojyoti Ghosh 
752193b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
752251699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
752393b34091SDebojyoti Ghosh 
7524e7069c78SShri   t->trajectory = tsin->trajectory;
7525e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7526e7069c78SShri 
7527e7069c78SShri   t->event = tsin->event;
75286b10a48eSSatish Balay   if (t->event) t->event->refct++;
7529e7069c78SShri 
753093b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
753193b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7532e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
753393b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
753493b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
753593b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
753693b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
753793b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
753893b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
753993b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
754093b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
754193b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
754293b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
754393b34091SDebojyoti Ghosh 
754493b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
754593b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
754693b34091SDebojyoti Ghosh 
754793b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
754893b34091SDebojyoti Ghosh 
754993b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
755093b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
755193b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
755293b34091SDebojyoti Ghosh 
755393b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
755493b34091SDebojyoti Ghosh 
75550d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75560d4fed19SBarry Smith     PetscInt i;
75570d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75580d4fed19SBarry Smith     for (i=0; i<10; i++) {
75590d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75600d4fed19SBarry Smith     }
75610d4fed19SBarry Smith   }
756293b34091SDebojyoti Ghosh   *tsout = t;
756393b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
756493b34091SDebojyoti Ghosh }
7565f3b1f45cSBarry Smith 
7566f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7567f3b1f45cSBarry Smith {
7568f3b1f45cSBarry Smith   PetscErrorCode ierr;
7569f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7570f3b1f45cSBarry Smith 
7571f3b1f45cSBarry Smith   PetscFunctionBegin;
7572f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7573f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7574f3b1f45cSBarry Smith }
7575f3b1f45cSBarry Smith 
7576f3b1f45cSBarry Smith /*@
7577f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7578f3b1f45cSBarry Smith 
7579d083f849SBarry Smith    Logically Collective on TS
7580f3b1f45cSBarry Smith 
7581f3b1f45cSBarry Smith     Input Parameters:
7582f3b1f45cSBarry Smith     TS - the time stepping routine
7583f3b1f45cSBarry Smith 
7584f3b1f45cSBarry Smith    Output Parameter:
7585f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7586f3b1f45cSBarry Smith 
7587f3b1f45cSBarry Smith    Options Database:
7588f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7589f3b1f45cSBarry Smith 
7590f3b1f45cSBarry Smith    Level: advanced
7591f3b1f45cSBarry Smith 
759295452b02SPatrick Sanan    Notes:
759395452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7594f3b1f45cSBarry Smith 
7595f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7596f3b1f45cSBarry Smith @*/
7597f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7598f3b1f45cSBarry Smith {
7599f3b1f45cSBarry Smith   Mat            J,B;
7600f3b1f45cSBarry Smith   PetscErrorCode ierr;
7601f3b1f45cSBarry Smith   TSRHSJacobian  func;
7602f3b1f45cSBarry Smith   void*          ctx;
7603f3b1f45cSBarry Smith 
7604f3b1f45cSBarry Smith   PetscFunctionBegin;
7605f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7606f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7607f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7608f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7609f3b1f45cSBarry Smith }
7610f3b1f45cSBarry Smith 
7611f3b1f45cSBarry Smith /*@C
7612f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7613f3b1f45cSBarry Smith 
7614d083f849SBarry Smith    Logically Collective on TS
7615f3b1f45cSBarry Smith 
7616f3b1f45cSBarry Smith     Input Parameters:
7617f3b1f45cSBarry Smith     TS - the time stepping routine
7618f3b1f45cSBarry Smith 
7619f3b1f45cSBarry Smith    Output Parameter:
7620f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7621f3b1f45cSBarry Smith 
7622f3b1f45cSBarry Smith    Options Database:
7623f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7624f3b1f45cSBarry Smith 
762595452b02SPatrick Sanan    Notes:
762695452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7627f3b1f45cSBarry Smith 
7628f3b1f45cSBarry Smith    Level: advanced
7629f3b1f45cSBarry Smith 
7630f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7631f3b1f45cSBarry Smith @*/
7632f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7633f3b1f45cSBarry Smith {
7634f3b1f45cSBarry Smith   Mat            J,B;
7635f3b1f45cSBarry Smith   PetscErrorCode ierr;
7636f3b1f45cSBarry Smith   void           *ctx;
7637f3b1f45cSBarry Smith   TSRHSJacobian  func;
7638f3b1f45cSBarry Smith 
7639f3b1f45cSBarry Smith   PetscFunctionBegin;
7640f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7641f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7642f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7643f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7644f3b1f45cSBarry Smith }
76450fe4d17eSHong Zhang 
76460fe4d17eSHong Zhang /*@
76470fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
76480fe4d17eSHong Zhang 
76490fe4d17eSHong Zhang   Logically collective
76500fe4d17eSHong Zhang 
76510fe4d17eSHong Zhang   Input Parameter:
76520fe4d17eSHong Zhang +  ts - timestepping context
76530fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76540fe4d17eSHong Zhang 
76550fe4d17eSHong Zhang   Options Database:
76560fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76570fe4d17eSHong Zhang 
76580fe4d17eSHong Zhang   Notes:
76590fe4d17eSHong Zhang     This is only useful for multirate methods
76600fe4d17eSHong Zhang 
76610fe4d17eSHong Zhang   Level: intermediate
76620fe4d17eSHong Zhang 
76630fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
76640fe4d17eSHong Zhang @*/
76650fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
76660fe4d17eSHong Zhang {
76670fe4d17eSHong Zhang   PetscFunctionBegin;
76680fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76690fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
76700fe4d17eSHong Zhang   PetscFunctionReturn(0);
76710fe4d17eSHong Zhang }
76720fe4d17eSHong Zhang 
76730fe4d17eSHong Zhang /*@
76740fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
76750fe4d17eSHong Zhang 
76760fe4d17eSHong Zhang   Not collective
76770fe4d17eSHong Zhang 
76780fe4d17eSHong Zhang   Input Parameter:
76790fe4d17eSHong Zhang .  ts - timestepping context
76800fe4d17eSHong Zhang 
76810fe4d17eSHong Zhang   Output Parameter:
76820fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76830fe4d17eSHong Zhang 
76840fe4d17eSHong Zhang   Level: intermediate
76850fe4d17eSHong Zhang 
76860fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
76870fe4d17eSHong Zhang @*/
76880fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
76890fe4d17eSHong Zhang {
76900fe4d17eSHong Zhang   PetscFunctionBegin;
76910fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76920fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
76930fe4d17eSHong Zhang   PetscFunctionReturn(0);
76940fe4d17eSHong Zhang }
7695