xref: /petsc/src/ts/interface/ts.c (revision e8b1e4240ebc67e0f77e5d66dd47f5b5d80948a7)
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;
56524989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
566b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
567a7a1495cSBarry Smith 
568a7a1495cSBarry Smith   PetscFunctionBegin;
5690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5700910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5710910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
57224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
573942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
57424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5750298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
5766374d434SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
57759e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5786c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
579971015bcSStefano Zampini 
5806c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
581971015bcSStefano Zampini     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
582971015bcSStefano Zampini     if (A == ts->Arhs) {
583971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
584971015bcSStefano Zampini         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
585971015bcSStefano Zampini       }
586971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
587971015bcSStefano Zampini         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
588971015bcSStefano Zampini       }
589971015bcSStefano Zampini     }
590971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
591971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
592971015bcSStefano Zampini         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
593971015bcSStefano Zampini       }
594971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
595971015bcSStefano Zampini         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
596971015bcSStefano Zampini       }
597971015bcSStefano Zampini     }
598971015bcSStefano Zampini     ts->rhsjacobian.shift = 0;
599971015bcSStefano Zampini     ts->rhsjacobian.scale = 1.;
6000e4ef248SJed Brown     PetscFunctionReturn(0);
601f8ede8e7SJed Brown   }
602d90be118SSean Farley 
603ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
604d90be118SSean Farley 
605e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
606971015bcSStefano Zampini     if (A == ts->Arhs) {
607971015bcSStefano Zampini       /* MatScale has a short path for this case.
608971015bcSStefano Zampini          However, this code path is taken the first time TSComputeRHSJacobian is called
609971015bcSStefano Zampini          and the matrices have not assembled yet */
610971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
61194ab13aaSBarry Smith         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
612971015bcSStefano Zampini       }
613971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
61494ab13aaSBarry Smith         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
615971015bcSStefano Zampini       }
616971015bcSStefano Zampini     }
617971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
618971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
61994ab13aaSBarry Smith         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
620971015bcSStefano Zampini       }
621971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
62294ab13aaSBarry Smith         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
623e1244c69SJed Brown       }
624971015bcSStefano Zampini     }
625e1244c69SJed Brown   }
626e1244c69SJed Brown 
62724989b8cSPeter Brune   if (rhsjacobianfunc) {
62894ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
629a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
630d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
631a7a1495cSBarry Smith     PetscStackPop;
63294ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
633ef66eb69SBarry Smith   } else {
63494ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
635971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
636ef66eb69SBarry Smith   }
6370e4ef248SJed Brown   ts->rhsjacobian.time  = t;
638971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
639971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6407f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
64159e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
642a7a1495cSBarry Smith   PetscFunctionReturn(0);
643a7a1495cSBarry Smith }
644a7a1495cSBarry Smith 
645316643e7SJed Brown /*@
646d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
647d763cef2SBarry Smith 
648d083f849SBarry Smith    Collective on TS
649316643e7SJed Brown 
650316643e7SJed Brown    Input Parameters:
651316643e7SJed Brown +  ts - the TS context
652316643e7SJed Brown .  t - current time
6530910c330SBarry Smith -  U - state vector
654316643e7SJed Brown 
655316643e7SJed Brown    Output Parameter:
656316643e7SJed Brown .  y - right hand side
657316643e7SJed Brown 
658316643e7SJed Brown    Note:
659316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
660316643e7SJed Brown    is used internally within the nonlinear solvers.
661316643e7SJed Brown 
662316643e7SJed Brown    Level: developer
663316643e7SJed Brown 
664316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
665316643e7SJed Brown @*/
6660910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
667d763cef2SBarry Smith {
668dfbe8321SBarry Smith   PetscErrorCode ierr;
66924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
67024989b8cSPeter Brune   TSIFunction    ifunction;
67124989b8cSPeter Brune   void           *ctx;
67224989b8cSPeter Brune   DM             dm;
67324989b8cSPeter Brune 
674d763cef2SBarry Smith   PetscFunctionBegin;
6750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6760910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6770700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
67824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6800298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
681d763cef2SBarry Smith 
682ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
683d763cef2SBarry Smith 
6840910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
68524989b8cSPeter Brune   if (rhsfunction) {
6861be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
687d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6880910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
689d763cef2SBarry Smith     PetscStackPop;
6901be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
691214bc6a2SJed Brown   } else {
692214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
693b2cd27e8SJed Brown   }
69444a41b28SSean Farley 
6950910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
696d763cef2SBarry Smith   PetscFunctionReturn(0);
697d763cef2SBarry Smith }
698d763cef2SBarry Smith 
699ef20d060SBarry Smith /*@
700ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
701ef20d060SBarry Smith 
702d083f849SBarry Smith    Collective on TS
703ef20d060SBarry Smith 
704ef20d060SBarry Smith    Input Parameters:
705ef20d060SBarry Smith +  ts - the TS context
706ef20d060SBarry Smith -  t - current time
707ef20d060SBarry Smith 
708ef20d060SBarry Smith    Output Parameter:
7090910c330SBarry Smith .  U - the solution
710ef20d060SBarry Smith 
711ef20d060SBarry Smith    Note:
712ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
713ef20d060SBarry Smith    is used internally within the nonlinear solvers.
714ef20d060SBarry Smith 
715ef20d060SBarry Smith    Level: developer
716ef20d060SBarry Smith 
717abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
718ef20d060SBarry Smith @*/
7190910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
720ef20d060SBarry Smith {
721ef20d060SBarry Smith   PetscErrorCode     ierr;
722ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
723ef20d060SBarry Smith   void               *ctx;
724ef20d060SBarry Smith   DM                 dm;
725ef20d060SBarry Smith 
726ef20d060SBarry Smith   PetscFunctionBegin;
727ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7280910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
729ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
730ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
731ef20d060SBarry Smith 
732ef20d060SBarry Smith   if (solutionfunction) {
7339b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7340910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
735ef20d060SBarry Smith     PetscStackPop;
736ef20d060SBarry Smith   }
737ef20d060SBarry Smith   PetscFunctionReturn(0);
738ef20d060SBarry Smith }
7399b7cd975SBarry Smith /*@
7409b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7419b7cd975SBarry Smith 
742d083f849SBarry Smith    Collective on TS
7439b7cd975SBarry Smith 
7449b7cd975SBarry Smith    Input Parameters:
7459b7cd975SBarry Smith +  ts - the TS context
7469b7cd975SBarry Smith -  t - current time
7479b7cd975SBarry Smith 
7489b7cd975SBarry Smith    Output Parameter:
7499b7cd975SBarry Smith .  U - the function value
7509b7cd975SBarry Smith 
7519b7cd975SBarry Smith    Note:
7529b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7539b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7549b7cd975SBarry Smith 
7559b7cd975SBarry Smith    Level: developer
7569b7cd975SBarry Smith 
7579b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7589b7cd975SBarry Smith @*/
7599b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7609b7cd975SBarry Smith {
7619b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7629b7cd975SBarry Smith   void               *ctx;
7639b7cd975SBarry Smith   DM                 dm;
7649b7cd975SBarry Smith 
7659b7cd975SBarry Smith   PetscFunctionBegin;
7669b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7679b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7689b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7699b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7709b7cd975SBarry Smith 
7719b7cd975SBarry Smith   if (forcing) {
7729b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7739b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7749b7cd975SBarry Smith     PetscStackPop;
7759b7cd975SBarry Smith   }
7769b7cd975SBarry Smith   PetscFunctionReturn(0);
7779b7cd975SBarry Smith }
778ef20d060SBarry Smith 
779214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
780214bc6a2SJed Brown {
7812dd45cf8SJed Brown   Vec            F;
782214bc6a2SJed Brown   PetscErrorCode ierr;
783214bc6a2SJed Brown 
784214bc6a2SJed Brown   PetscFunctionBegin;
7850298fd71SBarry Smith   *Frhs = NULL;
7860298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
787214bc6a2SJed Brown   if (!ts->Frhs) {
7882dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
789214bc6a2SJed Brown   }
790214bc6a2SJed Brown   *Frhs = ts->Frhs;
791214bc6a2SJed Brown   PetscFunctionReturn(0);
792214bc6a2SJed Brown }
793214bc6a2SJed Brown 
794971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
795214bc6a2SJed Brown {
796214bc6a2SJed Brown   Mat            A,B;
7972dd45cf8SJed Brown   PetscErrorCode ierr;
79841a1d4d2SBarry Smith   TSIJacobian    ijacobian;
799214bc6a2SJed Brown 
800214bc6a2SJed Brown   PetscFunctionBegin;
801c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
802c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
80341a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
804214bc6a2SJed Brown   if (Arhs) {
805214bc6a2SJed Brown     if (!ts->Arhs) {
80641a1d4d2SBarry Smith       if (ijacobian) {
807214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
80841a1d4d2SBarry Smith       } else {
80941a1d4d2SBarry Smith         ts->Arhs = A;
81041a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
81141a1d4d2SBarry Smith       }
8123565c898SBarry Smith     } else {
8133565c898SBarry Smith       PetscBool flg;
8143565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8153565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8163565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8173565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8183565c898SBarry Smith         ts->Arhs = A;
8193565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8203565c898SBarry Smith       }
821214bc6a2SJed Brown     }
822214bc6a2SJed Brown     *Arhs = ts->Arhs;
823214bc6a2SJed Brown   }
824214bc6a2SJed Brown   if (Brhs) {
825214bc6a2SJed Brown     if (!ts->Brhs) {
826bdb70873SJed Brown       if (A != B) {
82741a1d4d2SBarry Smith         if (ijacobian) {
828214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
829bdb70873SJed Brown         } else {
83041a1d4d2SBarry Smith           ts->Brhs = B;
83141a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
83241a1d4d2SBarry Smith         }
83341a1d4d2SBarry Smith       } else {
834bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
83551699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
836bdb70873SJed Brown       }
837214bc6a2SJed Brown     }
838214bc6a2SJed Brown     *Brhs = ts->Brhs;
839214bc6a2SJed Brown   }
840214bc6a2SJed Brown   PetscFunctionReturn(0);
841214bc6a2SJed Brown }
842214bc6a2SJed Brown 
843316643e7SJed Brown /*@
8440910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
845316643e7SJed Brown 
846d083f849SBarry Smith    Collective on TS
847316643e7SJed Brown 
848316643e7SJed Brown    Input Parameters:
849316643e7SJed Brown +  ts - the TS context
850316643e7SJed Brown .  t - current time
8510910c330SBarry Smith .  U - state vector
8520910c330SBarry Smith .  Udot - time derivative of state vector
853214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
854316643e7SJed Brown 
855316643e7SJed Brown    Output Parameter:
856316643e7SJed Brown .  Y - right hand side
857316643e7SJed Brown 
858316643e7SJed Brown    Note:
859316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
860316643e7SJed Brown    is used internally within the nonlinear solvers.
861316643e7SJed Brown 
862316643e7SJed Brown    If the user did did not write their equations in implicit form, this
863316643e7SJed Brown    function recasts them in implicit form.
864316643e7SJed Brown 
865316643e7SJed Brown    Level: developer
866316643e7SJed Brown 
867316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
868316643e7SJed Brown @*/
8690910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
870316643e7SJed Brown {
871316643e7SJed Brown   PetscErrorCode ierr;
87224989b8cSPeter Brune   TSIFunction    ifunction;
87324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
87424989b8cSPeter Brune   void           *ctx;
87524989b8cSPeter Brune   DM             dm;
876316643e7SJed Brown 
877316643e7SJed Brown   PetscFunctionBegin;
8780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8790910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8800910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8810700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
882316643e7SJed Brown 
88324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88424989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8850298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
88624989b8cSPeter Brune 
887ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
888d90be118SSean Farley 
8890910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
89024989b8cSPeter Brune   if (ifunction) {
891316643e7SJed Brown     PetscStackPush("TS user implicit function");
8920910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
893316643e7SJed Brown     PetscStackPop;
894214bc6a2SJed Brown   }
895214bc6a2SJed Brown   if (imex) {
89624989b8cSPeter Brune     if (!ifunction) {
8970910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8982dd45cf8SJed Brown     }
89924989b8cSPeter Brune   } else if (rhsfunction) {
90024989b8cSPeter Brune     if (ifunction) {
901214bc6a2SJed Brown       Vec Frhs;
902214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9030910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
904214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9052dd45cf8SJed Brown     } else {
9060910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9070910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
908316643e7SJed Brown     }
9094a6899ffSJed Brown   }
9100910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
911316643e7SJed Brown   PetscFunctionReturn(0);
912316643e7SJed Brown }
913316643e7SJed Brown 
914316643e7SJed Brown /*@
915316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
916316643e7SJed Brown 
917d083f849SBarry Smith    Collective on TS
918316643e7SJed Brown 
919316643e7SJed Brown    Input
920316643e7SJed Brown       Input Parameters:
921316643e7SJed Brown +  ts - the TS context
922316643e7SJed Brown .  t - current timestep
9230910c330SBarry Smith .  U - state vector
9240910c330SBarry Smith .  Udot - time derivative of state vector
925214bc6a2SJed Brown .  shift - shift to apply, see note below
926214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
927316643e7SJed Brown 
928316643e7SJed Brown    Output Parameters:
929316643e7SJed Brown +  A - Jacobian matrix
9303565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
931316643e7SJed Brown 
932316643e7SJed Brown    Notes:
9330910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
934316643e7SJed Brown 
9350910c330SBarry Smith    dF/dU + shift*dF/dUdot
936316643e7SJed Brown 
937316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
938316643e7SJed Brown    is used internally within the nonlinear solvers.
939316643e7SJed Brown 
940316643e7SJed Brown    Level: developer
941316643e7SJed Brown 
942316643e7SJed Brown .seealso:  TSSetIJacobian()
94363495f91SJed Brown @*/
944d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
945316643e7SJed Brown {
946316643e7SJed Brown   PetscErrorCode ierr;
94724989b8cSPeter Brune   TSIJacobian    ijacobian;
94824989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
94924989b8cSPeter Brune   DM             dm;
95024989b8cSPeter Brune   void           *ctx;
951316643e7SJed Brown 
952316643e7SJed Brown   PetscFunctionBegin;
9530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9540910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9550910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
956316643e7SJed Brown   PetscValidPointer(A,6);
95794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
958316643e7SJed Brown   PetscValidPointer(B,7);
95994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
96024989b8cSPeter Brune 
96124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9630298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96424989b8cSPeter Brune 
965ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
966316643e7SJed Brown 
96794ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
96824989b8cSPeter Brune   if (ijacobian) {
969316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
970d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
971316643e7SJed Brown     PetscStackPop;
9724a6899ffSJed Brown   }
973214bc6a2SJed Brown   if (imex) {
974b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9754c26be97Sstefano_zampini       PetscBool assembled;
976971015bcSStefano Zampini       if (rhsjacobian) {
977971015bcSStefano Zampini         Mat Arhs = NULL;
978971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
979971015bcSStefano Zampini         if (A == Arhs) {
980*e8b1e424SHong 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 */
981971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
982971015bcSStefano Zampini         }
983971015bcSStefano Zampini       }
98494ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9854c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9864c26be97Sstefano_zampini       if (!assembled) {
9874c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9884c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9894c26be97Sstefano_zampini       }
99094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99194ab13aaSBarry Smith       if (A != B) {
99294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9934c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9944c26be97Sstefano_zampini         if (!assembled) {
9954c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9964c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9974c26be97Sstefano_zampini         }
99894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
999214bc6a2SJed Brown       }
1000214bc6a2SJed Brown     }
1001214bc6a2SJed Brown   } else {
1002e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1003*e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
1004214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1005e1244c69SJed Brown     }
1006*e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
1007*e8b1e424SHong Zhang       PetscObjectState Ustate;
1008*e8b1e424SHong Zhang       PetscObjectId    Uid;
1009*e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
1010*e8b1e424SHong Zhang 
1011*e8b1e424SHong Zhang       ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1012*e8b1e424SHong Zhang       ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
1013*e8b1e424SHong Zhang       ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
1014*e8b1e424SHong Zhang       if ((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 */
1015*e8b1e424SHong 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 */
1016*e8b1e424SHong Zhang       } else {
10173565c898SBarry Smith         PetscBool flg;
1018*e8b1e424SHong Zhang 
1019*e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1020*e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1021*e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1022*e8b1e424SHong Zhang              and the matrices have not been assembled yet */
1023*e8b1e424SHong Zhang           if (ts->rhsjacobian.shift) {
1024*e8b1e424SHong Zhang             ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
1025*e8b1e424SHong Zhang           }
1026*e8b1e424SHong Zhang           if (ts->rhsjacobian.scale == -1.) {
1027*e8b1e424SHong Zhang             ierr = MatScale(A,-1);CHKERRQ(ierr);
1028*e8b1e424SHong Zhang           }
1029*e8b1e424SHong Zhang           if (B && B == ts->Brhs && A != B) {
1030*e8b1e424SHong Zhang             if (ts->rhsjacobian.shift) {
1031*e8b1e424SHong Zhang               ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
1032*e8b1e424SHong Zhang             }
1033*e8b1e424SHong Zhang             if (ts->rhsjacobian.scale == -1.) {
1034*e8b1e424SHong Zhang               ierr = MatScale(B,-1);CHKERRQ(ierr);
1035*e8b1e424SHong Zhang             }
1036*e8b1e424SHong Zhang           }
1037*e8b1e424SHong Zhang           ts->rhsjacobian.shift = 0;
1038*e8b1e424SHong Zhang           ts->rhsjacobian.scale = 1.;
1039*e8b1e424SHong Zhang         }
1040*e8b1e424SHong Zhang         ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr);
10413565c898SBarry Smith         ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10423565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10433565c898SBarry Smith         if (!flg) {
104494ab13aaSBarry Smith           ierr = MatScale(A,-1);CHKERRQ(ierr);
104594ab13aaSBarry Smith           ierr = MatShift(A,shift);CHKERRQ(ierr);
10463565c898SBarry Smith         }
104794ab13aaSBarry Smith         if (A != B) {
104894ab13aaSBarry Smith           ierr = MatScale(B,-1);CHKERRQ(ierr);
104994ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1050316643e7SJed Brown         }
1051*e8b1e424SHong Zhang       }
1052*e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1053*e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1054*e8b1e424SHong Zhang     } else if (Arhs) {  /* Both IJacobian and RHSJacobian exist or the RHS matrix provided (A) is different from the internal RHS matrix (Arhs) */
1055e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1056*e8b1e424SHong Zhang 
1057e1244c69SJed Brown       if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */
105894ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
105994ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
106094ab13aaSBarry Smith         if (A != B) {
106194ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
106294ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1063214bc6a2SJed Brown         }
1064316643e7SJed Brown       }
1065*e8b1e424SHong Zhang       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
106694ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
106794ab13aaSBarry Smith       if (A != B) {
106894ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1069316643e7SJed Brown       }
1070316643e7SJed Brown     }
1071316643e7SJed Brown   }
107294ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1073316643e7SJed Brown   PetscFunctionReturn(0);
1074316643e7SJed Brown }
1075316643e7SJed Brown 
1076d763cef2SBarry Smith /*@C
1077d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1078b5abc632SBarry Smith     where U_t = G(t,u).
1079d763cef2SBarry Smith 
10803f9fe445SBarry Smith     Logically Collective on TS
1081d763cef2SBarry Smith 
1082d763cef2SBarry Smith     Input Parameters:
1083d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10840298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1085d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1086d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10870298fd71SBarry Smith           function evaluation routine (may be NULL)
1088d763cef2SBarry Smith 
1089a96d6ef6SBarry Smith     Calling sequence of f:
1090a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1091d763cef2SBarry Smith 
1092a96d6ef6SBarry Smith +   ts - timestep context
1093a96d6ef6SBarry Smith .   t - current timestep
1094d763cef2SBarry Smith .   u - input vector
1095d763cef2SBarry Smith .   F - function vector
1096d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1097d763cef2SBarry Smith 
1098d763cef2SBarry Smith     Level: beginner
1099d763cef2SBarry Smith 
110095452b02SPatrick Sanan     Notes:
110195452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
11022bbac0d3SBarry Smith 
1103ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1104d763cef2SBarry Smith @*/
1105089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1106d763cef2SBarry Smith {
1107089b2837SJed Brown   PetscErrorCode ierr;
1108089b2837SJed Brown   SNES           snes;
11090298fd71SBarry Smith   Vec            ralloc = NULL;
111024989b8cSPeter Brune   DM             dm;
1111d763cef2SBarry Smith 
1112089b2837SJed Brown   PetscFunctionBegin;
11130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1114ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
111524989b8cSPeter Brune 
111624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
111724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1118089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1119e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1120e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1121e856ceecSJed Brown     r = ralloc;
1122e856ceecSJed Brown   }
1123089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1124e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1125d763cef2SBarry Smith   PetscFunctionReturn(0);
1126d763cef2SBarry Smith }
1127d763cef2SBarry Smith 
1128ef20d060SBarry Smith /*@C
1129abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1130ef20d060SBarry Smith 
1131ef20d060SBarry Smith     Logically Collective on TS
1132ef20d060SBarry Smith 
1133ef20d060SBarry Smith     Input Parameters:
1134ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1135ef20d060SBarry Smith .   f - routine for evaluating the solution
1136ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11370298fd71SBarry Smith           function evaluation routine (may be NULL)
1138ef20d060SBarry Smith 
1139a96d6ef6SBarry Smith     Calling sequence of f:
1140a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1141ef20d060SBarry Smith 
1142ef20d060SBarry Smith +   t - current timestep
1143ef20d060SBarry Smith .   u - output vector
1144ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1145ef20d060SBarry Smith 
11460ed3bfb6SBarry Smith     Options Database:
11470ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11480ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11490ed3bfb6SBarry Smith 
1150abd5a294SJed Brown     Notes:
1151abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1152abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1153abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1154abd5a294SJed Brown 
11554f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1156abd5a294SJed Brown 
1157ef20d060SBarry Smith     Level: beginner
1158ef20d060SBarry Smith 
11590ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1160ef20d060SBarry Smith @*/
1161ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1162ef20d060SBarry Smith {
1163ef20d060SBarry Smith   PetscErrorCode ierr;
1164ef20d060SBarry Smith   DM             dm;
1165ef20d060SBarry Smith 
1166ef20d060SBarry Smith   PetscFunctionBegin;
1167ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1168ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1169ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1170ef20d060SBarry Smith   PetscFunctionReturn(0);
1171ef20d060SBarry Smith }
1172ef20d060SBarry Smith 
11739b7cd975SBarry Smith /*@C
11749b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11759b7cd975SBarry Smith 
11769b7cd975SBarry Smith     Logically Collective on TS
11779b7cd975SBarry Smith 
11789b7cd975SBarry Smith     Input Parameters:
11799b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1180e162b725SBarry Smith .   func - routine for evaluating the forcing function
11819b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11820298fd71SBarry Smith           function evaluation routine (may be NULL)
11839b7cd975SBarry Smith 
11849b7cd975SBarry Smith     Calling sequence of func:
11856bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11869b7cd975SBarry Smith 
11879b7cd975SBarry Smith +   t - current timestep
1188e162b725SBarry Smith .   f - output vector
11899b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11909b7cd975SBarry Smith 
11919b7cd975SBarry Smith     Notes:
11929b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1193e162b725SBarry 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
1194e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1195e162b725SBarry Smith 
1196e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1197e162b725SBarry Smith 
1198e162b725SBarry 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
1199e162b725SBarry Smith     parameters can be passed in the ctx variable.
12009b7cd975SBarry Smith 
12019b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
12029b7cd975SBarry Smith 
12039b7cd975SBarry Smith     Level: beginner
12049b7cd975SBarry Smith 
12059b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
12069b7cd975SBarry Smith @*/
1207e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
12089b7cd975SBarry Smith {
12099b7cd975SBarry Smith   PetscErrorCode ierr;
12109b7cd975SBarry Smith   DM             dm;
12119b7cd975SBarry Smith 
12129b7cd975SBarry Smith   PetscFunctionBegin;
12139b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12149b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1215e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
12169b7cd975SBarry Smith   PetscFunctionReturn(0);
12179b7cd975SBarry Smith }
12189b7cd975SBarry Smith 
1219d763cef2SBarry Smith /*@C
1220f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1221b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1222d763cef2SBarry Smith 
12233f9fe445SBarry Smith    Logically Collective on TS
1224d763cef2SBarry Smith 
1225d763cef2SBarry Smith    Input Parameters:
1226d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1227e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1228e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1229d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1230d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12310298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1232d763cef2SBarry Smith 
1233f7ab8db6SBarry Smith    Calling sequence of f:
1234a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1235d763cef2SBarry Smith 
1236d763cef2SBarry Smith +  t - current timestep
1237d763cef2SBarry Smith .  u - input vector
1238e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1239e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1240d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1241d763cef2SBarry Smith 
12426cd88445SBarry Smith    Notes:
12436cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12446cd88445SBarry Smith 
12456cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1246ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1247d763cef2SBarry Smith 
1248d763cef2SBarry Smith    Level: beginner
1249d763cef2SBarry Smith 
1250ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1251d763cef2SBarry Smith 
1252d763cef2SBarry Smith @*/
1253e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1254d763cef2SBarry Smith {
1255277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1256089b2837SJed Brown   SNES           snes;
125724989b8cSPeter Brune   DM             dm;
125824989b8cSPeter Brune   TSIJacobian    ijacobian;
1259277b19d0SLisandro Dalcin 
1260d763cef2SBarry Smith   PetscFunctionBegin;
12610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1262e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1263e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1264e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1265e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1266d763cef2SBarry Smith 
126724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126824989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1269e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1270e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1271e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1272e1244c69SJed Brown   }
12730298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1274089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12755f659677SPeter Brune   if (!ijacobian) {
1276e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12770e4ef248SJed Brown   }
1278e5d3d808SBarry Smith   if (Amat) {
1279e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12800e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1281e5d3d808SBarry Smith     ts->Arhs = Amat;
12820e4ef248SJed Brown   }
1283e5d3d808SBarry Smith   if (Pmat) {
1284e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12850e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1286e5d3d808SBarry Smith     ts->Brhs = Pmat;
12870e4ef248SJed Brown   }
1288d763cef2SBarry Smith   PetscFunctionReturn(0);
1289d763cef2SBarry Smith }
1290d763cef2SBarry Smith 
1291316643e7SJed Brown /*@C
1292b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1293316643e7SJed Brown 
12943f9fe445SBarry Smith    Logically Collective on TS
1295316643e7SJed Brown 
1296316643e7SJed Brown    Input Parameters:
1297316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12980298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1299316643e7SJed Brown .  f   - the function evaluation routine
13000298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1301316643e7SJed Brown 
1302316643e7SJed Brown    Calling sequence of f:
13036bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1304316643e7SJed Brown 
1305316643e7SJed Brown +  t   - time at step/stage being solved
1306316643e7SJed Brown .  u   - state vector
1307316643e7SJed Brown .  u_t - time derivative of state vector
1308316643e7SJed Brown .  F   - function vector
1309316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1310316643e7SJed Brown 
1311316643e7SJed Brown    Important:
13122bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1313316643e7SJed Brown 
1314316643e7SJed Brown    Level: beginner
1315316643e7SJed Brown 
1316d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1317316643e7SJed Brown @*/
131851699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1319316643e7SJed Brown {
1320089b2837SJed Brown   PetscErrorCode ierr;
1321089b2837SJed Brown   SNES           snes;
132251699248SLisandro Dalcin   Vec            ralloc = NULL;
132324989b8cSPeter Brune   DM             dm;
1324316643e7SJed Brown 
1325316643e7SJed Brown   PetscFunctionBegin;
13260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
132751699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
132824989b8cSPeter Brune 
132924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
133024989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
133124989b8cSPeter Brune 
1332089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
133351699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
133451699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
133551699248SLisandro Dalcin     r  = ralloc;
1336e856ceecSJed Brown   }
133751699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
133851699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1339089b2837SJed Brown   PetscFunctionReturn(0);
1340089b2837SJed Brown }
1341089b2837SJed Brown 
1342089b2837SJed Brown /*@C
1343089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1344089b2837SJed Brown 
1345089b2837SJed Brown    Not Collective
1346089b2837SJed Brown 
1347089b2837SJed Brown    Input Parameter:
1348089b2837SJed Brown .  ts - the TS context
1349089b2837SJed Brown 
1350089b2837SJed Brown    Output Parameter:
13510298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13520298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13530298fd71SBarry Smith -  ctx - the function context (or NULL)
1354089b2837SJed Brown 
1355089b2837SJed Brown    Level: advanced
1356089b2837SJed Brown 
1357089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1358089b2837SJed Brown @*/
1359089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1360089b2837SJed Brown {
1361089b2837SJed Brown   PetscErrorCode ierr;
1362089b2837SJed Brown   SNES           snes;
136324989b8cSPeter Brune   DM             dm;
1364089b2837SJed Brown 
1365089b2837SJed Brown   PetscFunctionBegin;
1366089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1367089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13680298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
136924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1371089b2837SJed Brown   PetscFunctionReturn(0);
1372089b2837SJed Brown }
1373089b2837SJed Brown 
1374089b2837SJed Brown /*@C
1375089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1376089b2837SJed Brown 
1377089b2837SJed Brown    Not Collective
1378089b2837SJed Brown 
1379089b2837SJed Brown    Input Parameter:
1380089b2837SJed Brown .  ts - the TS context
1381089b2837SJed Brown 
1382089b2837SJed Brown    Output Parameter:
13830298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13840298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13850298fd71SBarry Smith -  ctx - the function context (or NULL)
1386089b2837SJed Brown 
1387089b2837SJed Brown    Level: advanced
1388089b2837SJed Brown 
13892bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1390089b2837SJed Brown @*/
1391089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1392089b2837SJed Brown {
1393089b2837SJed Brown   PetscErrorCode ierr;
1394089b2837SJed Brown   SNES           snes;
139524989b8cSPeter Brune   DM             dm;
1396089b2837SJed Brown 
1397089b2837SJed Brown   PetscFunctionBegin;
1398089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1399089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14000298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
140124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
140224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1403316643e7SJed Brown   PetscFunctionReturn(0);
1404316643e7SJed Brown }
1405316643e7SJed Brown 
1406316643e7SJed Brown /*@C
1407a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1408ae8867d6SBarry Smith         provided with TSSetIFunction().
1409316643e7SJed Brown 
14103f9fe445SBarry Smith    Logically Collective on TS
1411316643e7SJed Brown 
1412316643e7SJed Brown    Input Parameters:
1413316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1414e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1415e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1416316643e7SJed Brown .  f   - the Jacobian evaluation routine
14170298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1418316643e7SJed Brown 
1419316643e7SJed Brown    Calling sequence of f:
14206bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1421316643e7SJed Brown 
1422316643e7SJed Brown +  t    - time at step/stage being solved
14231b4a444bSJed Brown .  U    - state vector
14241b4a444bSJed Brown .  U_t  - time derivative of state vector
1425316643e7SJed Brown .  a    - shift
1426e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1427e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1428316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1429316643e7SJed Brown 
1430316643e7SJed Brown    Notes:
1431e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1432316643e7SJed Brown 
1433895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1434895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1435895c21f2SBarry Smith 
1436a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1437b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1438a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1439a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1440a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1441a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1442a4f0a591SBarry Smith 
14436cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14446cd88445SBarry Smith 
14456cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1446ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1447ca5f011dSBarry Smith 
1448316643e7SJed Brown    Level: beginner
1449316643e7SJed Brown 
1450ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1451316643e7SJed Brown 
1452316643e7SJed Brown @*/
1453e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1454316643e7SJed Brown {
1455316643e7SJed Brown   PetscErrorCode ierr;
1456089b2837SJed Brown   SNES           snes;
145724989b8cSPeter Brune   DM             dm;
1458316643e7SJed Brown 
1459316643e7SJed Brown   PetscFunctionBegin;
14600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1461e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1462e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1463e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1464e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
146524989b8cSPeter Brune 
146624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
146724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
146824989b8cSPeter Brune 
1469089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1470e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1471316643e7SJed Brown   PetscFunctionReturn(0);
1472316643e7SJed Brown }
1473316643e7SJed Brown 
1474e1244c69SJed Brown /*@
1475e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1476e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1477e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1478e1244c69SJed Brown    not been changed by the TS.
1479e1244c69SJed Brown 
1480e1244c69SJed Brown    Logically Collective
1481e1244c69SJed Brown 
1482e1244c69SJed Brown    Input Arguments:
1483e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1484e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1485e1244c69SJed Brown 
1486e1244c69SJed Brown    Level: intermediate
1487e1244c69SJed Brown 
1488e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1489e1244c69SJed Brown @*/
1490e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1491e1244c69SJed Brown {
1492e1244c69SJed Brown   PetscFunctionBegin;
1493e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1494e1244c69SJed Brown   PetscFunctionReturn(0);
1495e1244c69SJed Brown }
1496e1244c69SJed Brown 
1497efe9872eSLisandro Dalcin /*@C
1498efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1499efe9872eSLisandro Dalcin 
1500efe9872eSLisandro Dalcin    Logically Collective on TS
1501efe9872eSLisandro Dalcin 
1502efe9872eSLisandro Dalcin    Input Parameters:
1503efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1504efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1505efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1506efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin    Calling sequence of fun:
15096bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1510efe9872eSLisandro Dalcin 
1511efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1512efe9872eSLisandro Dalcin .  U    - state vector
1513efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1514efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1515efe9872eSLisandro Dalcin .  F    - function vector
1516efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1517efe9872eSLisandro Dalcin 
1518efe9872eSLisandro Dalcin    Level: beginner
1519efe9872eSLisandro Dalcin 
1520a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1521efe9872eSLisandro Dalcin @*/
1522efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1523efe9872eSLisandro Dalcin {
1524efe9872eSLisandro Dalcin   DM             dm;
1525efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1526efe9872eSLisandro Dalcin 
1527efe9872eSLisandro Dalcin   PetscFunctionBegin;
1528efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1529efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1530efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1531efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1532efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1533efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1534efe9872eSLisandro Dalcin }
1535efe9872eSLisandro Dalcin 
1536efe9872eSLisandro Dalcin /*@C
1537efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin   Not Collective
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin   Input Parameter:
1542efe9872eSLisandro Dalcin . ts - the TS context
1543efe9872eSLisandro Dalcin 
1544efe9872eSLisandro Dalcin   Output Parameter:
1545efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1546efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1547efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1548efe9872eSLisandro Dalcin 
1549efe9872eSLisandro Dalcin   Level: advanced
1550efe9872eSLisandro Dalcin 
1551a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1552efe9872eSLisandro Dalcin @*/
1553efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1554efe9872eSLisandro Dalcin {
1555efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1556efe9872eSLisandro Dalcin   SNES           snes;
1557efe9872eSLisandro Dalcin   DM             dm;
1558efe9872eSLisandro Dalcin 
1559efe9872eSLisandro Dalcin   PetscFunctionBegin;
1560efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1561efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1562efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1563efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1564efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1565efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1566efe9872eSLisandro Dalcin }
1567efe9872eSLisandro Dalcin 
1568efe9872eSLisandro Dalcin /*@C
1569bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1570efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1571efe9872eSLisandro Dalcin 
1572efe9872eSLisandro Dalcin    Logically Collective on TS
1573efe9872eSLisandro Dalcin 
1574efe9872eSLisandro Dalcin    Input Parameters:
1575efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1576efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1577efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1578efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1579efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1580efe9872eSLisandro Dalcin 
1581efe9872eSLisandro Dalcin    Calling sequence of jac:
15826bc98fa9SBarry 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);
1583efe9872eSLisandro Dalcin 
1584efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1585efe9872eSLisandro Dalcin .  U    - state vector
1586efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1587efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1588efe9872eSLisandro Dalcin .  v    - shift for U_t
1589efe9872eSLisandro Dalcin .  a    - shift for U_tt
1590efe9872eSLisandro 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
1591efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1592efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1593efe9872eSLisandro Dalcin 
1594efe9872eSLisandro Dalcin    Notes:
1595efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1598efe9872eSLisandro 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.
1599efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1600bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1601efe9872eSLisandro Dalcin 
1602efe9872eSLisandro Dalcin    Level: beginner
1603efe9872eSLisandro Dalcin 
1604a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1605efe9872eSLisandro Dalcin @*/
1606efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1607efe9872eSLisandro Dalcin {
1608efe9872eSLisandro Dalcin   DM             dm;
1609efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1610efe9872eSLisandro Dalcin 
1611efe9872eSLisandro Dalcin   PetscFunctionBegin;
1612efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1613efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1614efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1615efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1616efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1617efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1618efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1619efe9872eSLisandro Dalcin }
1620efe9872eSLisandro Dalcin 
1621efe9872eSLisandro Dalcin /*@C
1622efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1623efe9872eSLisandro Dalcin 
1624efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1625efe9872eSLisandro Dalcin 
1626efe9872eSLisandro Dalcin   Input Parameter:
1627efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1628efe9872eSLisandro Dalcin 
1629efe9872eSLisandro Dalcin   Output Parameters:
1630efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1631efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1632efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1633efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1634efe9872eSLisandro Dalcin 
163595452b02SPatrick Sanan   Notes:
163695452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1637efe9872eSLisandro Dalcin 
1638efe9872eSLisandro Dalcin   Level: advanced
1639efe9872eSLisandro Dalcin 
1640a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1641efe9872eSLisandro Dalcin 
1642efe9872eSLisandro Dalcin @*/
1643efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1644efe9872eSLisandro Dalcin {
1645efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1646efe9872eSLisandro Dalcin   SNES           snes;
1647efe9872eSLisandro Dalcin   DM             dm;
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin   PetscFunctionBegin;
1650efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1651efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1652efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1653efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1654efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1655efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1656efe9872eSLisandro Dalcin }
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin /*@
1659efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1660efe9872eSLisandro Dalcin 
1661d083f849SBarry Smith   Collective on TS
1662efe9872eSLisandro Dalcin 
1663efe9872eSLisandro Dalcin   Input Parameters:
1664efe9872eSLisandro Dalcin + ts - the TS context
1665efe9872eSLisandro Dalcin . t - current time
1666efe9872eSLisandro Dalcin . U - state vector
1667efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1668efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1669efe9872eSLisandro Dalcin 
1670efe9872eSLisandro Dalcin   Output Parameter:
1671efe9872eSLisandro Dalcin . F - the residual vector
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   Note:
1674efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1675efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1676efe9872eSLisandro Dalcin 
1677efe9872eSLisandro Dalcin   Level: developer
1678efe9872eSLisandro Dalcin 
1679a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1680efe9872eSLisandro Dalcin @*/
1681efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1682efe9872eSLisandro Dalcin {
1683efe9872eSLisandro Dalcin   DM             dm;
1684efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1685efe9872eSLisandro Dalcin   void           *ctx;
1686efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1687efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1688efe9872eSLisandro Dalcin 
1689efe9872eSLisandro Dalcin   PetscFunctionBegin;
1690efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1691efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1692efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1693efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1694efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1695efe9872eSLisandro Dalcin 
1696efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1697efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1698efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1699efe9872eSLisandro Dalcin 
1700efe9872eSLisandro Dalcin   if (!I2Function) {
1701efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1702efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1703efe9872eSLisandro Dalcin   }
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1708efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1709efe9872eSLisandro Dalcin   PetscStackPop;
1710efe9872eSLisandro Dalcin 
1711efe9872eSLisandro Dalcin   if (rhsfunction) {
1712efe9872eSLisandro Dalcin     Vec Frhs;
1713efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1714efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1715efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1716efe9872eSLisandro Dalcin   }
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1719efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1720efe9872eSLisandro Dalcin }
1721efe9872eSLisandro Dalcin 
1722efe9872eSLisandro Dalcin /*@
1723efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1724efe9872eSLisandro Dalcin 
1725d083f849SBarry Smith   Collective on TS
1726efe9872eSLisandro Dalcin 
1727efe9872eSLisandro Dalcin   Input Parameters:
1728efe9872eSLisandro Dalcin + ts - the TS context
1729efe9872eSLisandro Dalcin . t - current timestep
1730efe9872eSLisandro Dalcin . U - state vector
1731efe9872eSLisandro Dalcin . V - time derivative of state vector
1732efe9872eSLisandro Dalcin . A - second time derivative of state vector
1733efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1734efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin   Output Parameters:
1737efe9872eSLisandro Dalcin + J - Jacobian matrix
1738efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin   Notes:
1741efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1742efe9872eSLisandro Dalcin 
1743efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1744efe9872eSLisandro Dalcin 
1745efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1746efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1747efe9872eSLisandro Dalcin 
1748efe9872eSLisandro Dalcin   Level: developer
1749efe9872eSLisandro Dalcin 
1750efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1751efe9872eSLisandro Dalcin @*/
1752efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1753efe9872eSLisandro Dalcin {
1754efe9872eSLisandro Dalcin   DM             dm;
1755efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1756efe9872eSLisandro Dalcin   void           *ctx;
1757efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1758efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1759efe9872eSLisandro Dalcin 
1760efe9872eSLisandro Dalcin   PetscFunctionBegin;
1761efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1762efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1763efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1764efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1765efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1766efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1767efe9872eSLisandro Dalcin 
1768efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1769efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1770efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1771efe9872eSLisandro Dalcin 
1772efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1773efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1774efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1775efe9872eSLisandro Dalcin   }
1776efe9872eSLisandro Dalcin 
1777efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1778efe9872eSLisandro Dalcin 
1779efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1780efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1781efe9872eSLisandro Dalcin   PetscStackPop;
1782efe9872eSLisandro Dalcin 
1783efe9872eSLisandro Dalcin   if (rhsjacobian) {
1784efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1785efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1786efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1787efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1788efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1789efe9872eSLisandro Dalcin   }
1790efe9872eSLisandro Dalcin 
1791efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1792efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1793efe9872eSLisandro Dalcin }
1794efe9872eSLisandro Dalcin 
1795438f35afSJed Brown /*@C
1796438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1797438f35afSJed Brown 
1798438f35afSJed Brown    Logically Collective
1799438f35afSJed Brown 
1800438f35afSJed Brown    Input Arguments:
1801438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1802a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1803438f35afSJed Brown -  ctx - a context for tvar
1804438f35afSJed Brown 
1805a96d6ef6SBarry Smith     Calling sequence of tvar:
1806a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1807a96d6ef6SBarry Smith 
1808a96d6ef6SBarry Smith +   ts - timestep context
1809a96d6ef6SBarry Smith .   p - input vector (primative form)
1810a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1811a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1812a96d6ef6SBarry Smith 
1813438f35afSJed Brown    Level: advanced
1814438f35afSJed Brown 
1815438f35afSJed Brown    Notes:
1816438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1817438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1818438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1819438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1820438f35afSJed Brown    evaluated via the chain rule, as in
1821438f35afSJed Brown 
1822438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1823438f35afSJed Brown 
1824438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1825438f35afSJed Brown @*/
1826438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1827438f35afSJed Brown {
1828438f35afSJed Brown   PetscErrorCode ierr;
1829438f35afSJed Brown   DM             dm;
1830438f35afSJed Brown 
1831438f35afSJed Brown   PetscFunctionBegin;
1832438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1833438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1834438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1835438f35afSJed Brown   PetscFunctionReturn(0);
1836438f35afSJed Brown }
1837438f35afSJed Brown 
1838efe9872eSLisandro Dalcin /*@
1839e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1840e3c11fc1SJed Brown 
1841e3c11fc1SJed Brown    Logically Collective
1842e3c11fc1SJed Brown 
1843e3c11fc1SJed Brown    Input Parameters:
1844e3c11fc1SJed Brown +  ts - TS on which to compute
1845e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1846e3c11fc1SJed Brown 
1847e3c11fc1SJed Brown    Output Parameters:
1848e3c11fc1SJed Brown .  C - transient (conservative) variable
1849e3c11fc1SJed Brown 
1850e3c11fc1SJed Brown    Developer Notes:
1851e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1852e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1853e3c11fc1SJed Brown    being used.
1854e3c11fc1SJed Brown 
1855e3c11fc1SJed Brown    Level: developer
1856e3c11fc1SJed Brown 
1857e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1858e3c11fc1SJed Brown @*/
1859e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1860e3c11fc1SJed Brown {
1861e3c11fc1SJed Brown   PetscErrorCode ierr;
1862e3c11fc1SJed Brown   DM             dm;
1863e3c11fc1SJed Brown   DMTS           dmts;
1864e3c11fc1SJed Brown 
1865e3c11fc1SJed Brown   PetscFunctionBegin;
1866e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1867e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1868e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1869e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1870e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1871e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1872e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1873e3c11fc1SJed Brown   }
1874e3c11fc1SJed Brown   PetscFunctionReturn(0);
1875e3c11fc1SJed Brown }
1876e3c11fc1SJed Brown 
1877e3c11fc1SJed Brown /*@
1878e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1879e3c11fc1SJed Brown 
1880e3c11fc1SJed Brown    Logically Collective
1881e3c11fc1SJed Brown 
1882e3c11fc1SJed Brown    Input Parameters:
1883e3c11fc1SJed Brown .  ts - TS on which to compute
1884e3c11fc1SJed Brown 
1885e3c11fc1SJed Brown    Output Parameters:
1886e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1887e3c11fc1SJed Brown 
1888e3c11fc1SJed Brown    Level: developer
1889e3c11fc1SJed Brown 
1890e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1891e3c11fc1SJed Brown @*/
1892e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1893e3c11fc1SJed Brown {
1894e3c11fc1SJed Brown   PetscErrorCode ierr;
1895e3c11fc1SJed Brown   DM             dm;
1896e3c11fc1SJed Brown   DMTS           dmts;
1897e3c11fc1SJed Brown 
1898e3c11fc1SJed Brown   PetscFunctionBegin;
1899e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1900e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1901e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1902e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1903e3c11fc1SJed Brown   PetscFunctionReturn(0);
1904e3c11fc1SJed Brown }
1905e3c11fc1SJed Brown 
1906e3c11fc1SJed Brown /*@
1907efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1908efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1909efe9872eSLisandro Dalcin 
1910d083f849SBarry Smith    Logically Collective on TS
1911efe9872eSLisandro Dalcin 
1912efe9872eSLisandro Dalcin    Input Parameters:
1913efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1914efe9872eSLisandro Dalcin .  u - the solution vector
1915efe9872eSLisandro Dalcin -  v - the time derivative vector
1916efe9872eSLisandro Dalcin 
1917efe9872eSLisandro Dalcin    Level: beginner
1918efe9872eSLisandro Dalcin 
1919efe9872eSLisandro Dalcin @*/
1920efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1921efe9872eSLisandro Dalcin {
1922efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1923efe9872eSLisandro Dalcin 
1924efe9872eSLisandro Dalcin   PetscFunctionBegin;
1925efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1926efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1927efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1928efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1929efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1930efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1931efe9872eSLisandro Dalcin   ts->vec_dot = v;
1932efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1933efe9872eSLisandro Dalcin }
1934efe9872eSLisandro Dalcin 
1935efe9872eSLisandro Dalcin /*@
1936efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1937efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1938efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1939efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1940efe9872eSLisandro Dalcin 
1941efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1942efe9872eSLisandro Dalcin 
1943efe9872eSLisandro Dalcin    Input Parameter:
1944efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1945efe9872eSLisandro Dalcin 
1946efe9872eSLisandro Dalcin    Output Parameter:
1947efe9872eSLisandro Dalcin +  u - the vector containing the solution
1948efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1949efe9872eSLisandro Dalcin 
1950efe9872eSLisandro Dalcin    Level: intermediate
1951efe9872eSLisandro Dalcin 
1952efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1953efe9872eSLisandro Dalcin 
1954efe9872eSLisandro Dalcin @*/
1955efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1956efe9872eSLisandro Dalcin {
1957efe9872eSLisandro Dalcin   PetscFunctionBegin;
1958efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1959efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1960efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1961efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1962efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1963efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1964efe9872eSLisandro Dalcin }
1965efe9872eSLisandro Dalcin 
196655849f57SBarry Smith /*@C
196755849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
196855849f57SBarry Smith 
196955849f57SBarry Smith   Collective on PetscViewer
197055849f57SBarry Smith 
197155849f57SBarry Smith   Input Parameters:
197255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
197355849f57SBarry Smith            some related function before a call to TSLoad().
197455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
197555849f57SBarry Smith 
197655849f57SBarry Smith    Level: intermediate
197755849f57SBarry Smith 
197855849f57SBarry Smith   Notes:
197955849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
198055849f57SBarry Smith 
198155849f57SBarry Smith   Notes for advanced users:
198255849f57SBarry Smith   Most users should not need to know the details of the binary storage
198355849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
198455849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
198555849f57SBarry Smith   format is
198655849f57SBarry Smith .vb
198755849f57SBarry Smith      has not yet been determined
198855849f57SBarry Smith .ve
198955849f57SBarry Smith 
199055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
199155849f57SBarry Smith @*/
1992f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
199355849f57SBarry Smith {
199455849f57SBarry Smith   PetscErrorCode ierr;
199555849f57SBarry Smith   PetscBool      isbinary;
199655849f57SBarry Smith   PetscInt       classid;
199755849f57SBarry Smith   char           type[256];
19982d53ad75SBarry Smith   DMTS           sdm;
1999ad6bc421SBarry Smith   DM             dm;
200055849f57SBarry Smith 
200155849f57SBarry Smith   PetscFunctionBegin;
2002f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
200355849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
200455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
200555849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
200655849f57SBarry Smith 
2007060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
2008ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
2009060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
2010f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
2011f2c2a1b9SBarry Smith   if (ts->ops->load) {
2012f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
2013f2c2a1b9SBarry Smith   }
2014ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
2015ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
2016ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
2017f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
2018f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
20192d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20202d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
202155849f57SBarry Smith   PetscFunctionReturn(0);
202255849f57SBarry Smith }
202355849f57SBarry Smith 
20249804daf3SBarry Smith #include <petscdraw.h>
2025e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2026e04113cfSBarry Smith #include <petscviewersaws.h>
2027f05ece33SBarry Smith #endif
2028fe2efc57SMark 
2029fe2efc57SMark /*@C
2030fe2efc57SMark    TSViewFromOptions - View from Options
2031fe2efc57SMark 
2032fe2efc57SMark    Collective on TS
2033fe2efc57SMark 
2034fe2efc57SMark    Input Parameters:
2035fe2efc57SMark +  A - the application ordering context
2036736c3998SJose E. Roman .  obj - Optional object
2037736c3998SJose E. Roman -  name - command line option
2038fe2efc57SMark 
2039fe2efc57SMark    Level: intermediate
2040fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
2041fe2efc57SMark @*/
2042fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
2043fe2efc57SMark {
2044fe2efc57SMark   PetscErrorCode ierr;
2045fe2efc57SMark 
2046fe2efc57SMark   PetscFunctionBegin;
2047fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
2048fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
2049fe2efc57SMark   PetscFunctionReturn(0);
2050fe2efc57SMark }
2051fe2efc57SMark 
20527e2c5f70SBarry Smith /*@C
2053d763cef2SBarry Smith     TSView - Prints the TS data structure.
2054d763cef2SBarry Smith 
20554c49b128SBarry Smith     Collective on TS
2056d763cef2SBarry Smith 
2057d763cef2SBarry Smith     Input Parameters:
2058d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2059d763cef2SBarry Smith -   viewer - visualization context
2060d763cef2SBarry Smith 
2061d763cef2SBarry Smith     Options Database Key:
2062d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2063d763cef2SBarry Smith 
2064d763cef2SBarry Smith     Notes:
2065d763cef2SBarry Smith     The available visualization contexts include
2066b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2067b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2068d763cef2SBarry Smith          output where only the first processor opens
2069d763cef2SBarry Smith          the file.  All other processors send their
2070d763cef2SBarry Smith          data to the first processor to print.
2071d763cef2SBarry Smith 
2072d763cef2SBarry Smith     The user can open an alternative visualization context with
2073b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith     Level: beginner
2076d763cef2SBarry Smith 
2077b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2078d763cef2SBarry Smith @*/
20797087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2080d763cef2SBarry Smith {
2081dfbe8321SBarry Smith   PetscErrorCode ierr;
208219fd82e9SBarry Smith   TSType         type;
20832b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20842d53ad75SBarry Smith   DMTS           sdm;
2085e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2086536b137fSBarry Smith   PetscBool      issaws;
2087f05ece33SBarry Smith #endif
2088d763cef2SBarry Smith 
2089d763cef2SBarry Smith   PetscFunctionBegin;
20900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20913050cee2SBarry Smith   if (!viewer) {
2092ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20933050cee2SBarry Smith   }
20940700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2095c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2096fd16b177SBarry Smith 
2097251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2098251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
209955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
21002b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2101e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2102536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2103f05ece33SBarry Smith #endif
210432077d6dSBarry Smith   if (iascii) {
2105dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2106efd4aadfSBarry Smith     if (ts->ops->view) {
2107efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2108efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2109efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2110efd4aadfSBarry Smith     }
2111ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
211277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2113ef85077eSLisandro Dalcin     }
2114ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
21157c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2116ef85077eSLisandro Dalcin     }
2117efd4aadfSBarry Smith     if (ts->usessnes) {
2118efd4aadfSBarry Smith       PetscBool lin;
2119d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
21205ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2121d763cef2SBarry Smith       }
21225ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
21231ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2124efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2125efd4aadfSBarry Smith     }
2126193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2127a0af407cSBarry Smith     if (ts->vrtol) {
2128a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2129a0af407cSBarry Smith     } else {
2130a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2131a0af407cSBarry Smith     }
2132a0af407cSBarry Smith     if (ts->vatol) {
2133a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2134a0af407cSBarry Smith     } else {
2135a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2136a0af407cSBarry Smith     }
2137825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2138efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2139825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21400f5bd95cSBarry Smith   } else if (isstring) {
2141a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
214236a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
214336a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
214455849f57SBarry Smith   } else if (isbinary) {
214555849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
214655849f57SBarry Smith     MPI_Comm    comm;
214755849f57SBarry Smith     PetscMPIInt rank;
214855849f57SBarry Smith     char        type[256];
214955849f57SBarry Smith 
215055849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
215155849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
215255849f57SBarry Smith     if (!rank) {
2153f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
215455849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2155f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
215655849f57SBarry Smith     }
215755849f57SBarry Smith     if (ts->ops->view) {
215855849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
215955849f57SBarry Smith     }
2160efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2161f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2162f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21632d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21642d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21652b0a91c0SBarry Smith   } else if (isdraw) {
21662b0a91c0SBarry Smith     PetscDraw draw;
21672b0a91c0SBarry Smith     char      str[36];
216889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21692b0a91c0SBarry Smith 
21702b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21712b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21722b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21732b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
217451fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
217589fd9fafSBarry Smith     bottom = y - h;
21762b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21772b0a91c0SBarry Smith     if (ts->ops->view) {
21782b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21792b0a91c0SBarry Smith     }
2180efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2181efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21822b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2183e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2184536b137fSBarry Smith   } else if (issaws) {
2185d45a07a7SBarry Smith     PetscMPIInt rank;
21862657e9d9SBarry Smith     const char  *name;
21872657e9d9SBarry Smith 
21882657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2189d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2190d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2191d45a07a7SBarry Smith       char       dir[1024];
2192d45a07a7SBarry Smith 
2193e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2194a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21952657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21962657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21972657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2198d763cef2SBarry Smith     }
21990acecf5bSBarry Smith     if (ts->ops->view) {
22000acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
22010acecf5bSBarry Smith     }
2202f05ece33SBarry Smith #endif
2203f05ece33SBarry Smith   }
220436a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
220536a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
220636a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
220736a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
220836a9e3b9SBarry Smith   }
220936a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
221036a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2211f05ece33SBarry Smith 
2212b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2213251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2214b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2215d763cef2SBarry Smith   PetscFunctionReturn(0);
2216d763cef2SBarry Smith }
2217d763cef2SBarry Smith 
2218b07ff414SBarry Smith /*@
2219d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2220d763cef2SBarry Smith    the timesteppers.
2221d763cef2SBarry Smith 
22223f9fe445SBarry Smith    Logically Collective on TS
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith    Input Parameters:
2225d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2226d763cef2SBarry Smith -  usrP - optional user context
2227d763cef2SBarry Smith 
222895452b02SPatrick Sanan    Fortran Notes:
222995452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2230daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2231daf670e6SBarry Smith 
2232d763cef2SBarry Smith    Level: intermediate
2233d763cef2SBarry Smith 
2234d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2235d763cef2SBarry Smith @*/
22367087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2237d763cef2SBarry Smith {
2238d763cef2SBarry Smith   PetscFunctionBegin;
22390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2240d763cef2SBarry Smith   ts->user = usrP;
2241d763cef2SBarry Smith   PetscFunctionReturn(0);
2242d763cef2SBarry Smith }
2243d763cef2SBarry Smith 
2244b07ff414SBarry Smith /*@
2245d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2246d763cef2SBarry Smith     timestepper.
2247d763cef2SBarry Smith 
2248d763cef2SBarry Smith     Not Collective
2249d763cef2SBarry Smith 
2250d763cef2SBarry Smith     Input Parameter:
2251d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2252d763cef2SBarry Smith 
2253d763cef2SBarry Smith     Output Parameter:
2254d763cef2SBarry Smith .   usrP - user context
2255d763cef2SBarry Smith 
225695452b02SPatrick Sanan    Fortran Notes:
225795452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2258daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2259daf670e6SBarry Smith 
2260d763cef2SBarry Smith     Level: intermediate
2261d763cef2SBarry Smith 
2262d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2263d763cef2SBarry Smith @*/
2264e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2265d763cef2SBarry Smith {
2266d763cef2SBarry Smith   PetscFunctionBegin;
22670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2268e71120c6SJed Brown   *(void**)usrP = ts->user;
2269d763cef2SBarry Smith   PetscFunctionReturn(0);
2270d763cef2SBarry Smith }
2271d763cef2SBarry Smith 
2272d763cef2SBarry Smith /*@
227380275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith    Not Collective
2276d763cef2SBarry Smith 
2277d763cef2SBarry Smith    Input Parameter:
2278d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2279d763cef2SBarry Smith 
2280d763cef2SBarry Smith    Output Parameter:
228180275a0aSLisandro Dalcin .  steps - number of steps completed so far
2282d763cef2SBarry Smith 
2283d763cef2SBarry Smith    Level: intermediate
2284d763cef2SBarry Smith 
22859be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2286d763cef2SBarry Smith @*/
228780275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2288d763cef2SBarry Smith {
2289d763cef2SBarry Smith   PetscFunctionBegin;
22900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
229180275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
229280275a0aSLisandro Dalcin   *steps = ts->steps;
229380275a0aSLisandro Dalcin   PetscFunctionReturn(0);
229480275a0aSLisandro Dalcin }
229580275a0aSLisandro Dalcin 
229680275a0aSLisandro Dalcin /*@
229780275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
229880275a0aSLisandro Dalcin 
229980275a0aSLisandro Dalcin    Logically Collective on TS
230080275a0aSLisandro Dalcin 
230180275a0aSLisandro Dalcin    Input Parameters:
230280275a0aSLisandro Dalcin +  ts - the TS context
230380275a0aSLisandro Dalcin -  steps - number of steps completed so far
230480275a0aSLisandro Dalcin 
230580275a0aSLisandro Dalcin    Notes:
230680275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
230780275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
230880275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
230980275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
231080275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
231180275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
231280275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
231380275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
231480275a0aSLisandro Dalcin 
231580275a0aSLisandro Dalcin    Level: advanced
231680275a0aSLisandro Dalcin 
231780275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
231880275a0aSLisandro Dalcin @*/
231980275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
232080275a0aSLisandro Dalcin {
232180275a0aSLisandro Dalcin   PetscFunctionBegin;
232280275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
232380275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
232480275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
232580275a0aSLisandro Dalcin   ts->steps = steps;
2326d763cef2SBarry Smith   PetscFunctionReturn(0);
2327d763cef2SBarry Smith }
2328d763cef2SBarry Smith 
2329d763cef2SBarry Smith /*@
2330d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2331d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2332d763cef2SBarry Smith 
23333f9fe445SBarry Smith    Logically Collective on TS
2334d763cef2SBarry Smith 
2335d763cef2SBarry Smith    Input Parameters:
2336d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2337d763cef2SBarry Smith -  time_step - the size of the timestep
2338d763cef2SBarry Smith 
2339d763cef2SBarry Smith    Level: intermediate
2340d763cef2SBarry Smith 
2341aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2342d763cef2SBarry Smith 
2343d763cef2SBarry Smith @*/
23447087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2345d763cef2SBarry Smith {
2346d763cef2SBarry Smith   PetscFunctionBegin;
23470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2348c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2349d763cef2SBarry Smith   ts->time_step = time_step;
2350d763cef2SBarry Smith   PetscFunctionReturn(0);
2351d763cef2SBarry Smith }
2352d763cef2SBarry Smith 
2353a43b19c4SJed Brown /*@
235449354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
235549354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
235649354f04SShri Abhyankar      or just return at the final time TS computed.
2357a43b19c4SJed Brown 
2358a43b19c4SJed Brown   Logically Collective on TS
2359a43b19c4SJed Brown 
2360a43b19c4SJed Brown    Input Parameter:
2361a43b19c4SJed Brown +   ts - the time-step context
236249354f04SShri Abhyankar -   eftopt - exact final time option
2363a43b19c4SJed Brown 
2364feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2365feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2366feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2367feed9e9dSBarry Smith 
2368feed9e9dSBarry Smith    Options Database:
2369feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2370feed9e9dSBarry Smith 
2371ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2372ee346746SBarry Smith     then the final time you selected.
2373ee346746SBarry Smith 
2374a43b19c4SJed Brown    Level: beginner
2375a43b19c4SJed Brown 
2376f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2377a43b19c4SJed Brown @*/
237849354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2379a43b19c4SJed Brown {
2380a43b19c4SJed Brown   PetscFunctionBegin;
2381a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
238249354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
238349354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2384a43b19c4SJed Brown   PetscFunctionReturn(0);
2385a43b19c4SJed Brown }
2386a43b19c4SJed Brown 
2387d763cef2SBarry Smith /*@
2388f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2389f6953c82SLisandro Dalcin 
2390f6953c82SLisandro Dalcin    Not Collective
2391f6953c82SLisandro Dalcin 
2392f6953c82SLisandro Dalcin    Input Parameter:
2393f6953c82SLisandro Dalcin .  ts - the TS context
2394f6953c82SLisandro Dalcin 
2395f6953c82SLisandro Dalcin    Output Parameter:
2396f6953c82SLisandro Dalcin .  eftopt - exact final time option
2397f6953c82SLisandro Dalcin 
2398f6953c82SLisandro Dalcin    Level: beginner
2399f6953c82SLisandro Dalcin 
2400f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2401f6953c82SLisandro Dalcin @*/
2402f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2403f6953c82SLisandro Dalcin {
2404f6953c82SLisandro Dalcin   PetscFunctionBegin;
2405f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2406f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2407f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2408f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2409f6953c82SLisandro Dalcin }
2410f6953c82SLisandro Dalcin 
2411f6953c82SLisandro Dalcin /*@
2412d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2413d763cef2SBarry Smith 
2414d763cef2SBarry Smith    Not Collective
2415d763cef2SBarry Smith 
2416d763cef2SBarry Smith    Input Parameter:
2417d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2418d763cef2SBarry Smith 
2419d763cef2SBarry Smith    Output Parameter:
2420d763cef2SBarry Smith .  dt - the current timestep size
2421d763cef2SBarry Smith 
2422d763cef2SBarry Smith    Level: intermediate
2423d763cef2SBarry Smith 
2424aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2425d763cef2SBarry Smith 
2426d763cef2SBarry Smith @*/
24277087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2428d763cef2SBarry Smith {
2429d763cef2SBarry Smith   PetscFunctionBegin;
24300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2431f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2432d763cef2SBarry Smith   *dt = ts->time_step;
2433d763cef2SBarry Smith   PetscFunctionReturn(0);
2434d763cef2SBarry Smith }
2435d763cef2SBarry Smith 
2436d8e5e3e6SSatish Balay /*@
2437d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2438d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2439d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2440d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2441d763cef2SBarry Smith 
2442d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2443d763cef2SBarry Smith 
2444d763cef2SBarry Smith    Input Parameter:
2445d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2446d763cef2SBarry Smith 
2447d763cef2SBarry Smith    Output Parameter:
2448d763cef2SBarry Smith .  v - the vector containing the solution
2449d763cef2SBarry Smith 
245063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
245163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
245263e21af5SBarry Smith 
2453d763cef2SBarry Smith    Level: intermediate
2454d763cef2SBarry Smith 
24550ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2456d763cef2SBarry Smith 
2457d763cef2SBarry Smith @*/
24587087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2459d763cef2SBarry Smith {
2460d763cef2SBarry Smith   PetscFunctionBegin;
24610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24624482741eSBarry Smith   PetscValidPointer(v,2);
24638737fe31SLisandro Dalcin   *v = ts->vec_sol;
2464d763cef2SBarry Smith   PetscFunctionReturn(0);
2465d763cef2SBarry Smith }
2466d763cef2SBarry Smith 
246703fe5f5eSDebojyoti Ghosh /*@
2468b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
246903fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2470b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
247103fe5f5eSDebojyoti Ghosh    structure as the solution vector.
247203fe5f5eSDebojyoti Ghosh 
247303fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
247403fe5f5eSDebojyoti Ghosh 
247503fe5f5eSDebojyoti Ghosh    Parameters :
2476a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2477b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2478b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
247903fe5f5eSDebojyoti Ghosh        returned in v.
2480a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
248103fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2482b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
248303fe5f5eSDebojyoti Ghosh 
24844cdd57e5SDebojyoti Ghosh    Level: advanced
248503fe5f5eSDebojyoti Ghosh 
248603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
248703fe5f5eSDebojyoti Ghosh 
248803fe5f5eSDebojyoti Ghosh @*/
2489b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
249003fe5f5eSDebojyoti Ghosh {
249103fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
249203fe5f5eSDebojyoti Ghosh 
249303fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
249403fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2495b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
249603fe5f5eSDebojyoti Ghosh   else {
2497b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
249803fe5f5eSDebojyoti Ghosh   }
249903fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
250003fe5f5eSDebojyoti Ghosh }
250103fe5f5eSDebojyoti Ghosh 
25024cdd57e5SDebojyoti Ghosh /*@
25034cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
25044cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
25054cdd57e5SDebojyoti Ghosh 
25064cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25074cdd57e5SDebojyoti Ghosh 
25084cdd57e5SDebojyoti Ghosh    Parameters :
2509a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2510a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
25114cdd57e5SDebojyoti Ghosh 
25124cdd57e5SDebojyoti Ghosh    Level: intermediate
25134cdd57e5SDebojyoti Ghosh 
25144cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
25154cdd57e5SDebojyoti Ghosh 
25164cdd57e5SDebojyoti Ghosh @*/
25174cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
25184cdd57e5SDebojyoti Ghosh {
25194cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25204cdd57e5SDebojyoti Ghosh 
25214cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25224cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2523f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
25244cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2525f6356ec7SDebojyoti Ghosh   } else {
2526f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2527f6356ec7SDebojyoti Ghosh   }
25284cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25294cdd57e5SDebojyoti Ghosh }
25304cdd57e5SDebojyoti Ghosh 
25314cdd57e5SDebojyoti Ghosh /*@
25324cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
25334cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
25344cdd57e5SDebojyoti Ghosh 
25354cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25364cdd57e5SDebojyoti Ghosh 
25379657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
25389657682dSDebojyoti Ghosh 
25394cdd57e5SDebojyoti Ghosh    Parameters :
2540a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2541657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2542a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
25434cdd57e5SDebojyoti Ghosh 
25444cdd57e5SDebojyoti Ghosh    Level: intermediate
25454cdd57e5SDebojyoti Ghosh 
254657df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25474cdd57e5SDebojyoti Ghosh 
25484cdd57e5SDebojyoti Ghosh @*/
25490a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25504cdd57e5SDebojyoti Ghosh {
25514cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25524cdd57e5SDebojyoti Ghosh 
25534cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25544cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2555f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25560a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2557f6356ec7SDebojyoti Ghosh   } else {
2558f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2559f6356ec7SDebojyoti Ghosh   }
25604cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25614cdd57e5SDebojyoti Ghosh }
25624cdd57e5SDebojyoti Ghosh 
256357df6a1bSDebojyoti Ghosh /*@
256457df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
256557df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
256657df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
256757df6a1bSDebojyoti Ghosh 
256857df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
256957df6a1bSDebojyoti Ghosh 
257057df6a1bSDebojyoti Ghosh    Parameters :
2571a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2572a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
257357df6a1bSDebojyoti Ghosh 
257457df6a1bSDebojyoti Ghosh    Level: intermediate
257557df6a1bSDebojyoti Ghosh 
257657df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
257757df6a1bSDebojyoti Ghosh 
257857df6a1bSDebojyoti Ghosh @*/
257957df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
258057df6a1bSDebojyoti Ghosh {
258157df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
258257df6a1bSDebojyoti Ghosh 
258357df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
258457df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25859657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
258657df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
258757df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
258857df6a1bSDebojyoti Ghosh   }
258957df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
259057df6a1bSDebojyoti Ghosh }
259157df6a1bSDebojyoti Ghosh 
2592bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2593d8e5e3e6SSatish Balay /*@
2594bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2595d763cef2SBarry Smith 
2596bdad233fSMatthew Knepley   Not collective
2597d763cef2SBarry Smith 
2598bdad233fSMatthew Knepley   Input Parameters:
2599bdad233fSMatthew Knepley + ts   - The TS
2600bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2601d763cef2SBarry Smith .vb
26020910c330SBarry Smith          U_t - A U = 0      (linear)
26030910c330SBarry Smith          U_t - A(t) U = 0   (linear)
26040910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2605d763cef2SBarry Smith .ve
2606d763cef2SBarry Smith 
2607d763cef2SBarry Smith    Level: beginner
2608d763cef2SBarry Smith 
2609bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2610d763cef2SBarry Smith @*/
26117087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2612a7cc72afSBarry Smith {
26139e2a6581SJed Brown   PetscErrorCode ierr;
26149e2a6581SJed Brown 
2615d763cef2SBarry Smith   PetscFunctionBegin;
26160700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2617bdad233fSMatthew Knepley   ts->problem_type = type;
26189e2a6581SJed Brown   if (type == TS_LINEAR) {
26199e2a6581SJed Brown     SNES snes;
26209e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
26219e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
26229e2a6581SJed Brown   }
2623d763cef2SBarry Smith   PetscFunctionReturn(0);
2624d763cef2SBarry Smith }
2625d763cef2SBarry Smith 
2626bdad233fSMatthew Knepley /*@C
2627bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2628bdad233fSMatthew Knepley 
2629bdad233fSMatthew Knepley   Not collective
2630bdad233fSMatthew Knepley 
2631bdad233fSMatthew Knepley   Input Parameter:
2632bdad233fSMatthew Knepley . ts   - The TS
2633bdad233fSMatthew Knepley 
2634bdad233fSMatthew Knepley   Output Parameter:
2635bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2636bdad233fSMatthew Knepley .vb
2637089b2837SJed Brown          M U_t = A U
2638089b2837SJed Brown          M(t) U_t = A(t) U
2639b5abc632SBarry Smith          F(t,U,U_t)
2640bdad233fSMatthew Knepley .ve
2641bdad233fSMatthew Knepley 
2642bdad233fSMatthew Knepley    Level: beginner
2643bdad233fSMatthew Knepley 
2644bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2645bdad233fSMatthew Knepley @*/
26467087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2647a7cc72afSBarry Smith {
2648bdad233fSMatthew Knepley   PetscFunctionBegin;
26490700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26504482741eSBarry Smith   PetscValidIntPointer(type,2);
2651bdad233fSMatthew Knepley   *type = ts->problem_type;
2652bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2653bdad233fSMatthew Knepley }
2654d763cef2SBarry Smith 
2655303a5415SBarry Smith /*
2656303a5415SBarry 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()
2657303a5415SBarry Smith */
2658303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2659303a5415SBarry Smith {
2660303a5415SBarry Smith   PetscErrorCode ierr;
2661303a5415SBarry Smith   PetscBool      isnone;
2662303a5415SBarry Smith 
2663303a5415SBarry Smith   PetscFunctionBegin;
2664303a5415SBarry Smith   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
2665303a5415SBarry Smith   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
2666303a5415SBarry Smith 
2667303a5415SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
2668303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2669303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2670303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2671303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2672303a5415SBarry Smith   }
2673303a5415SBarry Smith   PetscFunctionReturn(0);
2674303a5415SBarry Smith }
2675303a5415SBarry Smith 
2676303a5415SBarry Smith 
2677d763cef2SBarry Smith /*@
2678303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2679d763cef2SBarry Smith 
2680d763cef2SBarry Smith    Collective on TS
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith    Input Parameter:
2683d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2684d763cef2SBarry Smith 
2685d763cef2SBarry Smith    Notes:
2686d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2687d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2688141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2689d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2690141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2691d763cef2SBarry Smith 
2692d763cef2SBarry Smith    Level: advanced
2693d763cef2SBarry Smith 
2694141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2695d763cef2SBarry Smith @*/
26967087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2697d763cef2SBarry Smith {
2698dfbe8321SBarry Smith   PetscErrorCode ierr;
26996c6b9e74SPeter Brune   DM             dm;
27006c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2701d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2702cd11d68dSLisandro Dalcin   TSIFunction    ifun;
27036c6b9e74SPeter Brune   TSIJacobian    ijac;
2704efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
27056c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2706d763cef2SBarry Smith 
2707d763cef2SBarry Smith   PetscFunctionBegin;
27080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2709277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2710277b19d0SLisandro Dalcin 
27117adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2712cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2713cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2714d763cef2SBarry Smith   }
2715277b19d0SLisandro Dalcin 
27161a638600SStefano Zampini   if (!ts->vec_sol) {
27171a638600SStefano Zampini     if (ts->dm) {
27181a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
27191a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
27201a638600SStefano Zampini   }
2721277b19d0SLisandro Dalcin 
2722298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2723298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2724298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2725298bade4SHong Zhang   }
2726298bade4SHong Zhang 
2727cd4cee2dSHong Zhang   if (ts->quadraturets) {
2728cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2729ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2730cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2731cd4cee2dSHong Zhang   }
2732cd4cee2dSHong Zhang 
2733971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2734971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2735e1244c69SJed Brown     Mat Amat,Pmat;
2736e1244c69SJed Brown     SNES snes;
2737e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2738e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2739e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2740e1244c69SJed Brown      * have displaced the RHS matrix */
2741971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2742abc0d4abSBarry 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 */
2743abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2744e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2745e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2746e1244c69SJed Brown     }
2747971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2748abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2749e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2750e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2751e1244c69SJed Brown     }
2752e1244c69SJed Brown   }
27532ffb9264SLisandro Dalcin 
27542ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27552ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27562ffb9264SLisandro Dalcin 
2757277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2758000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2759277b19d0SLisandro Dalcin   }
2760277b19d0SLisandro Dalcin 
2761303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
27622ffb9264SLisandro Dalcin 
2763a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27646c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27656c6b9e74SPeter Brune    */
27666c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27670298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27686c6b9e74SPeter Brune   if (!func) {
27696c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27706c6b9e74SPeter Brune   }
2771a6772fa2SLisandro 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.
27726c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27736c6b9e74SPeter Brune    */
27740298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27750298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2776efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27770298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2778efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27796c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27806c6b9e74SPeter Brune   }
2781c0517034SDebojyoti Ghosh 
2782c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2783c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2784c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2785c0517034SDebojyoti Ghosh   }
2786c0517034SDebojyoti Ghosh 
2787277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2788277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2789277b19d0SLisandro Dalcin }
2790277b19d0SLisandro Dalcin 
2791f6a906c0SBarry Smith /*@
2792277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2793277b19d0SLisandro Dalcin 
2794277b19d0SLisandro Dalcin    Collective on TS
2795277b19d0SLisandro Dalcin 
2796277b19d0SLisandro Dalcin    Input Parameter:
2797277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2798277b19d0SLisandro Dalcin 
2799277b19d0SLisandro Dalcin    Level: beginner
2800277b19d0SLisandro Dalcin 
2801277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2802277b19d0SLisandro Dalcin @*/
2803277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2804277b19d0SLisandro Dalcin {
28051d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2806277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2807277b19d0SLisandro Dalcin 
2808277b19d0SLisandro Dalcin   PetscFunctionBegin;
2809277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2810b18ea86cSHong Zhang 
2811277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2812277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2813277b19d0SLisandro Dalcin   }
2814277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2815e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2816bbd56ea5SKarl Rupp 
28174e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
28184e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2819214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
28206bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2821efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2822e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2823e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
282438637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2825bbd56ea5SKarl Rupp 
2826cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2827ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2828ecf68647SHong Zhang   if (ts->forward_solve) {
2829ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2830ecf68647SHong Zhang   }
2831cd4cee2dSHong Zhang   if (ts->quadraturets) {
2832cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
283336eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2834cd4cee2dSHong Zhang   }
28351d06f6b3SHong Zhang   while (ilink) {
28361d06f6b3SHong Zhang     next = ilink->next;
28371d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
28381d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
28391d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
28401d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
28411d06f6b3SHong Zhang     ilink = next;
284287f4e208SHong Zhang   }
2843545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2844277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2845d763cef2SBarry Smith   PetscFunctionReturn(0);
2846d763cef2SBarry Smith }
2847d763cef2SBarry Smith 
2848d8e5e3e6SSatish Balay /*@
2849d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2850d763cef2SBarry Smith    with TSCreate().
2851d763cef2SBarry Smith 
2852d763cef2SBarry Smith    Collective on TS
2853d763cef2SBarry Smith 
2854d763cef2SBarry Smith    Input Parameter:
2855d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2856d763cef2SBarry Smith 
2857d763cef2SBarry Smith    Level: beginner
2858d763cef2SBarry Smith 
2859d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2860d763cef2SBarry Smith @*/
28616bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2862d763cef2SBarry Smith {
28636849ba73SBarry Smith   PetscErrorCode ierr;
2864d763cef2SBarry Smith 
2865d763cef2SBarry Smith   PetscFunctionBegin;
28666bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2867ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2868c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2869d763cef2SBarry Smith 
2870ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2871ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2872ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2873ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2874ecf68647SHong Zhang   }
2875e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2876e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28776bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28786d4c513bSLisandro Dalcin 
2879bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2880bc952696SBarry Smith 
288184df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28826427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28836427ac75SLisandro Dalcin 
28846bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28856bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28866bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28870dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28886d4c513bSLisandro Dalcin 
2889cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2890a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2891d763cef2SBarry Smith   PetscFunctionReturn(0);
2892d763cef2SBarry Smith }
2893d763cef2SBarry Smith 
2894d8e5e3e6SSatish Balay /*@
2895d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2896d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2897d763cef2SBarry Smith 
2898d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2899d763cef2SBarry Smith 
2900d763cef2SBarry Smith    Input Parameter:
2901d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2902d763cef2SBarry Smith 
2903d763cef2SBarry Smith    Output Parameter:
2904d763cef2SBarry Smith .  snes - the nonlinear solver context
2905d763cef2SBarry Smith 
2906d763cef2SBarry Smith    Notes:
2907d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2908d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
290994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2910d763cef2SBarry Smith 
2911d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
29120298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2913d763cef2SBarry Smith 
2914d763cef2SBarry Smith    Level: beginner
2915d763cef2SBarry Smith 
2916d763cef2SBarry Smith @*/
29177087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2918d763cef2SBarry Smith {
2919d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2920d372ba47SLisandro Dalcin 
2921d763cef2SBarry Smith   PetscFunctionBegin;
29220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29234482741eSBarry Smith   PetscValidPointer(snes,2);
2924d372ba47SLisandro Dalcin   if (!ts->snes) {
2925ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
292616413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
29270298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29283bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2929d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2930496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
29319e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
29329e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
29339e2a6581SJed Brown     }
2934d372ba47SLisandro Dalcin   }
2935d763cef2SBarry Smith   *snes = ts->snes;
2936d763cef2SBarry Smith   PetscFunctionReturn(0);
2937d763cef2SBarry Smith }
2938d763cef2SBarry Smith 
2939deb2cd25SJed Brown /*@
2940deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2941deb2cd25SJed Brown 
2942deb2cd25SJed Brown    Collective
2943deb2cd25SJed Brown 
2944deb2cd25SJed Brown    Input Parameter:
2945deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2946deb2cd25SJed Brown -  snes - the nonlinear solver context
2947deb2cd25SJed Brown 
2948deb2cd25SJed Brown    Notes:
2949deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2950deb2cd25SJed Brown 
2951deb2cd25SJed Brown    Level: developer
2952deb2cd25SJed Brown 
2953deb2cd25SJed Brown @*/
2954deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2955deb2cd25SJed Brown {
2956deb2cd25SJed Brown   PetscErrorCode ierr;
2957d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2958deb2cd25SJed Brown 
2959deb2cd25SJed Brown   PetscFunctionBegin;
2960deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2961deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2962deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2963deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2964bbd56ea5SKarl Rupp 
2965deb2cd25SJed Brown   ts->snes = snes;
2966bbd56ea5SKarl Rupp 
29670298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29680298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2969740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29700298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2971740132f1SEmil Constantinescu   }
2972deb2cd25SJed Brown   PetscFunctionReturn(0);
2973deb2cd25SJed Brown }
2974deb2cd25SJed Brown 
2975d8e5e3e6SSatish Balay /*@
297694b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2977d763cef2SBarry Smith    a TS (timestepper) context.
2978d763cef2SBarry Smith 
297994b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2980d763cef2SBarry Smith 
2981d763cef2SBarry Smith    Input Parameter:
2982d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2983d763cef2SBarry Smith 
2984d763cef2SBarry Smith    Output Parameter:
298594b7f48cSBarry Smith .  ksp - the nonlinear solver context
2986d763cef2SBarry Smith 
2987d763cef2SBarry Smith    Notes:
298894b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2989d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2990d763cef2SBarry Smith    KSP and PC contexts as well.
2991d763cef2SBarry Smith 
299294b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29930298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2994d763cef2SBarry Smith 
2995d763cef2SBarry Smith    Level: beginner
2996d763cef2SBarry Smith 
2997d763cef2SBarry Smith @*/
29987087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2999d763cef2SBarry Smith {
3000d372ba47SLisandro Dalcin   PetscErrorCode ierr;
3001089b2837SJed Brown   SNES           snes;
3002d372ba47SLisandro Dalcin 
3003d763cef2SBarry Smith   PetscFunctionBegin;
30040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30054482741eSBarry Smith   PetscValidPointer(ksp,2);
300617186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
3007e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
3008089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3009089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
3010d763cef2SBarry Smith   PetscFunctionReturn(0);
3011d763cef2SBarry Smith }
3012d763cef2SBarry Smith 
3013d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
3014d763cef2SBarry Smith 
3015adb62b0dSMatthew Knepley /*@
3016618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
3017618ce8baSLisandro Dalcin 
3018618ce8baSLisandro Dalcin    Logically Collective on TS
3019618ce8baSLisandro Dalcin 
3020618ce8baSLisandro Dalcin    Input Parameters:
3021618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3022618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
3023618ce8baSLisandro Dalcin 
3024618ce8baSLisandro Dalcin    Options Database Keys:
3025618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
3026618ce8baSLisandro Dalcin 
3027618ce8baSLisandro Dalcin    Notes:
3028618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
3029618ce8baSLisandro Dalcin 
3030618ce8baSLisandro Dalcin    Level: intermediate
3031618ce8baSLisandro Dalcin 
3032618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
3033618ce8baSLisandro Dalcin @*/
3034618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
3035618ce8baSLisandro Dalcin {
3036618ce8baSLisandro Dalcin   PetscFunctionBegin;
3037618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3038618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3039618ce8baSLisandro Dalcin   if (maxsteps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
3040618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
3041618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3042618ce8baSLisandro Dalcin }
3043618ce8baSLisandro Dalcin 
3044618ce8baSLisandro Dalcin /*@
3045618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
3046618ce8baSLisandro Dalcin 
3047618ce8baSLisandro Dalcin    Not Collective
3048618ce8baSLisandro Dalcin 
3049618ce8baSLisandro Dalcin    Input Parameters:
3050618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3051618ce8baSLisandro Dalcin 
3052618ce8baSLisandro Dalcin    Output Parameter:
3053618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3054618ce8baSLisandro Dalcin 
3055618ce8baSLisandro Dalcin    Level: advanced
3056618ce8baSLisandro Dalcin 
3057618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3058618ce8baSLisandro Dalcin @*/
3059618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3060618ce8baSLisandro Dalcin {
3061618ce8baSLisandro Dalcin   PetscFunctionBegin;
3062618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3063618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3064618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3065618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3066618ce8baSLisandro Dalcin }
3067618ce8baSLisandro Dalcin 
3068618ce8baSLisandro Dalcin /*@
3069618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3070618ce8baSLisandro Dalcin 
3071618ce8baSLisandro Dalcin    Logically Collective on TS
3072618ce8baSLisandro Dalcin 
3073618ce8baSLisandro Dalcin    Input Parameters:
3074618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3075618ce8baSLisandro Dalcin -  maxtime - final time to step to
3076618ce8baSLisandro Dalcin 
3077618ce8baSLisandro Dalcin    Options Database Keys:
3078ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3079618ce8baSLisandro Dalcin 
3080618ce8baSLisandro Dalcin    Notes:
3081618ce8baSLisandro Dalcin    The default maximum time is 5.0
3082618ce8baSLisandro Dalcin 
3083618ce8baSLisandro Dalcin    Level: intermediate
3084618ce8baSLisandro Dalcin 
3085618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3086618ce8baSLisandro Dalcin @*/
3087618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3088618ce8baSLisandro Dalcin {
3089618ce8baSLisandro Dalcin   PetscFunctionBegin;
3090618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3091618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3092618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3093618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3094618ce8baSLisandro Dalcin }
3095618ce8baSLisandro Dalcin 
3096618ce8baSLisandro Dalcin /*@
3097618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3098618ce8baSLisandro Dalcin 
3099618ce8baSLisandro Dalcin    Not Collective
3100618ce8baSLisandro Dalcin 
3101618ce8baSLisandro Dalcin    Input Parameters:
3102618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3103618ce8baSLisandro Dalcin 
3104618ce8baSLisandro Dalcin    Output Parameter:
3105618ce8baSLisandro Dalcin .  maxtime - final time to step to
3106618ce8baSLisandro Dalcin 
3107618ce8baSLisandro Dalcin    Level: advanced
3108618ce8baSLisandro Dalcin 
3109618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3110618ce8baSLisandro Dalcin @*/
3111618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3112618ce8baSLisandro Dalcin {
3113618ce8baSLisandro Dalcin   PetscFunctionBegin;
3114618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3115618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3116618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3117618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3118618ce8baSLisandro Dalcin }
3119618ce8baSLisandro Dalcin 
3120618ce8baSLisandro Dalcin /*@
3121aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3122edc382c3SSatish Balay 
3123edc382c3SSatish Balay    Level: deprecated
3124edc382c3SSatish Balay 
3125aaa6c58dSLisandro Dalcin @*/
3126aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3127aaa6c58dSLisandro Dalcin {
3128aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3129aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3130aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3131aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3132aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3133aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3134aaa6c58dSLisandro Dalcin }
3135aaa6c58dSLisandro Dalcin 
3136aaa6c58dSLisandro Dalcin /*@
313719eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3138edc382c3SSatish Balay 
3139edc382c3SSatish Balay    Level: deprecated
3140edc382c3SSatish Balay 
3141adb62b0dSMatthew Knepley @*/
31427087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3143adb62b0dSMatthew Knepley {
3144adb62b0dSMatthew Knepley   PetscFunctionBegin;
31450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3146abc0a331SBarry Smith   if (maxsteps) {
31474482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3148adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3149adb62b0dSMatthew Knepley   }
3150abc0a331SBarry Smith   if (maxtime) {
31514482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3152adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3153adb62b0dSMatthew Knepley   }
3154adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3155adb62b0dSMatthew Knepley }
3156adb62b0dSMatthew Knepley 
3157d763cef2SBarry Smith /*@
315819eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3159edc382c3SSatish Balay 
3160edc382c3SSatish Balay    Level: deprecated
3161edc382c3SSatish Balay 
3162d763cef2SBarry Smith @*/
31637087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3164d763cef2SBarry Smith {
3165d763cef2SBarry Smith   PetscFunctionBegin;
31660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3167c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3168c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
316939b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
317039b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3171d763cef2SBarry Smith   PetscFunctionReturn(0);
3172d763cef2SBarry Smith }
3173d763cef2SBarry Smith 
3174d763cef2SBarry Smith /*@
31755c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3176edc382c3SSatish Balay 
3177edc382c3SSatish Balay    Level: deprecated
3178edc382c3SSatish Balay 
31795c5f5948SLisandro Dalcin @*/
3180e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31815c5f5948SLisandro Dalcin 
318219eac22cSLisandro Dalcin /*@
31834f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3184edc382c3SSatish Balay 
3185edc382c3SSatish Balay    Level: deprecated
3186edc382c3SSatish Balay 
31874f4e0956SLisandro Dalcin @*/
31884f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31894f4e0956SLisandro Dalcin 
31904f4e0956SLisandro Dalcin /*@
3191d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3192d763cef2SBarry Smith    for use by the TS routines.
3193d763cef2SBarry Smith 
3194d083f849SBarry Smith    Logically Collective on TS
3195d763cef2SBarry Smith 
3196d763cef2SBarry Smith    Input Parameters:
3197d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31980910c330SBarry Smith -  u - the solution vector
3199d763cef2SBarry Smith 
3200d763cef2SBarry Smith    Level: beginner
3201d763cef2SBarry Smith 
32020ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3203d763cef2SBarry Smith @*/
32040910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3205d763cef2SBarry Smith {
32068737fe31SLisandro Dalcin   PetscErrorCode ierr;
3207496e6a7aSJed Brown   DM             dm;
32088737fe31SLisandro Dalcin 
3209d763cef2SBarry Smith   PetscFunctionBegin;
32100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32110910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32120910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
32136bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
32140910c330SBarry Smith   ts->vec_sol = u;
3215bbd56ea5SKarl Rupp 
3216496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
32170910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3218d763cef2SBarry Smith   PetscFunctionReturn(0);
3219d763cef2SBarry Smith }
3220d763cef2SBarry Smith 
3221ac226902SBarry Smith /*@C
3222000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32233f2090d5SJed Brown   called once at the beginning of each time step.
3224000e7ae3SMatthew Knepley 
32253f9fe445SBarry Smith   Logically Collective on TS
3226000e7ae3SMatthew Knepley 
3227000e7ae3SMatthew Knepley   Input Parameters:
3228000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3229000e7ae3SMatthew Knepley - func - The function
3230000e7ae3SMatthew Knepley 
3231000e7ae3SMatthew Knepley   Calling sequence of func:
32326bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3233000e7ae3SMatthew Knepley 
3234000e7ae3SMatthew Knepley   Level: intermediate
3235000e7ae3SMatthew Knepley 
3236dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3237000e7ae3SMatthew Knepley @*/
32387087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3239000e7ae3SMatthew Knepley {
3240000e7ae3SMatthew Knepley   PetscFunctionBegin;
32410700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3242ae60f76fSBarry Smith   ts->prestep = func;
3243000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3244000e7ae3SMatthew Knepley }
3245000e7ae3SMatthew Knepley 
324609ee8438SJed Brown /*@
32473f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32483f2090d5SJed Brown 
32493f2090d5SJed Brown   Collective on TS
32503f2090d5SJed Brown 
32513f2090d5SJed Brown   Input Parameters:
32523f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32533f2090d5SJed Brown 
32543f2090d5SJed Brown   Notes:
32553f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32563f2090d5SJed Brown   so most users would not generally call this routine themselves.
32573f2090d5SJed Brown 
32583f2090d5SJed Brown   Level: developer
32593f2090d5SJed Brown 
32609be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32613f2090d5SJed Brown @*/
32627087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32633f2090d5SJed Brown {
32643f2090d5SJed Brown   PetscErrorCode ierr;
32653f2090d5SJed Brown 
32663f2090d5SJed Brown   PetscFunctionBegin;
32670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3268ae60f76fSBarry Smith   if (ts->prestep) {
32695efd42a4SStefano Zampini     Vec              U;
32705efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32715efd42a4SStefano Zampini 
32725efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32735efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3274ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32755efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3276dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3277312ce896SJed Brown   }
32783f2090d5SJed Brown   PetscFunctionReturn(0);
32793f2090d5SJed Brown }
32803f2090d5SJed Brown 
3281b8123daeSJed Brown /*@C
3282b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3283b8123daeSJed Brown   called once at the beginning of each stage.
3284b8123daeSJed Brown 
3285b8123daeSJed Brown   Logically Collective on TS
3286b8123daeSJed Brown 
3287b8123daeSJed Brown   Input Parameters:
3288b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3289b8123daeSJed Brown - func - The function
3290b8123daeSJed Brown 
3291b8123daeSJed Brown   Calling sequence of func:
3292b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3293b8123daeSJed Brown 
3294b8123daeSJed Brown   Level: intermediate
3295b8123daeSJed Brown 
3296b8123daeSJed Brown   Note:
3297b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
329880275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3299b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3300b8123daeSJed Brown 
33019be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3302b8123daeSJed Brown @*/
3303b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3304b8123daeSJed Brown {
3305b8123daeSJed Brown   PetscFunctionBegin;
3306b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3307ae60f76fSBarry Smith   ts->prestage = func;
3308b8123daeSJed Brown   PetscFunctionReturn(0);
3309b8123daeSJed Brown }
3310b8123daeSJed Brown 
33119be3e283SDebojyoti Ghosh /*@C
33129be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33139be3e283SDebojyoti Ghosh   called once at the end of each stage.
33149be3e283SDebojyoti Ghosh 
33159be3e283SDebojyoti Ghosh   Logically Collective on TS
33169be3e283SDebojyoti Ghosh 
33179be3e283SDebojyoti Ghosh   Input Parameters:
33189be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33199be3e283SDebojyoti Ghosh - func - The function
33209be3e283SDebojyoti Ghosh 
33219be3e283SDebojyoti Ghosh   Calling sequence of func:
33229be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33239be3e283SDebojyoti Ghosh 
33249be3e283SDebojyoti Ghosh   Level: intermediate
33259be3e283SDebojyoti Ghosh 
33269be3e283SDebojyoti Ghosh   Note:
33279be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
332880275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
33299be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33309be3e283SDebojyoti Ghosh 
33319be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33329be3e283SDebojyoti Ghosh @*/
33339be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33349be3e283SDebojyoti Ghosh {
33359be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33369be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33379be3e283SDebojyoti Ghosh   ts->poststage = func;
33389be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33399be3e283SDebojyoti Ghosh }
33409be3e283SDebojyoti Ghosh 
3341c688d042SShri Abhyankar /*@C
3342c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3343c688d042SShri Abhyankar   called once at the end of each step evaluation.
3344c688d042SShri Abhyankar 
3345c688d042SShri Abhyankar   Logically Collective on TS
3346c688d042SShri Abhyankar 
3347c688d042SShri Abhyankar   Input Parameters:
3348c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3349c688d042SShri Abhyankar - func - The function
3350c688d042SShri Abhyankar 
3351c688d042SShri Abhyankar   Calling sequence of func:
3352c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3353c688d042SShri Abhyankar 
3354c688d042SShri Abhyankar   Level: intermediate
3355c688d042SShri Abhyankar 
3356c688d042SShri Abhyankar   Note:
33571785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33581785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3359e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3360e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3361e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3362c688d042SShri Abhyankar 
3363c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3364c688d042SShri Abhyankar @*/
3365c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3366c688d042SShri Abhyankar {
3367c688d042SShri Abhyankar   PetscFunctionBegin;
3368c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3369c688d042SShri Abhyankar   ts->postevaluate = func;
3370c688d042SShri Abhyankar   PetscFunctionReturn(0);
3371c688d042SShri Abhyankar }
3372c688d042SShri Abhyankar 
3373b8123daeSJed Brown /*@
3374b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3375b8123daeSJed Brown 
3376b8123daeSJed Brown   Collective on TS
3377b8123daeSJed Brown 
3378b8123daeSJed Brown   Input Parameters:
3379b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33809be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3381b8123daeSJed Brown 
3382b8123daeSJed Brown   Notes:
3383b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3384b8123daeSJed Brown   most users would not generally call this routine themselves.
3385b8123daeSJed Brown 
3386b8123daeSJed Brown   Level: developer
3387b8123daeSJed Brown 
33889be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3389b8123daeSJed Brown @*/
3390b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3391b8123daeSJed Brown {
3392b8123daeSJed Brown   PetscFunctionBegin;
3393b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3394ae60f76fSBarry Smith   if (ts->prestage) {
3395ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3396b8123daeSJed Brown   }
3397b8123daeSJed Brown   PetscFunctionReturn(0);
3398b8123daeSJed Brown }
3399b8123daeSJed Brown 
34009be3e283SDebojyoti Ghosh /*@
34019be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34029be3e283SDebojyoti Ghosh 
34039be3e283SDebojyoti Ghosh   Collective on TS
34049be3e283SDebojyoti Ghosh 
34059be3e283SDebojyoti Ghosh   Input Parameters:
34069be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34079be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34089be3e283SDebojyoti Ghosh   stageindex  - Stage number
34099be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34109be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34119be3e283SDebojyoti Ghosh 
34129be3e283SDebojyoti Ghosh   Notes:
34139be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34149be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34159be3e283SDebojyoti Ghosh 
34169be3e283SDebojyoti Ghosh   Level: developer
34179be3e283SDebojyoti Ghosh 
34189be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34199be3e283SDebojyoti Ghosh @*/
34209be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34219be3e283SDebojyoti Ghosh {
34229be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34239be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34244beae5d8SLisandro Dalcin   if (ts->poststage) {
34259be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34269be3e283SDebojyoti Ghosh   }
34279be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34289be3e283SDebojyoti Ghosh }
34299be3e283SDebojyoti Ghosh 
3430c688d042SShri Abhyankar /*@
3431c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3432c688d042SShri Abhyankar 
3433c688d042SShri Abhyankar   Collective on TS
3434c688d042SShri Abhyankar 
3435c688d042SShri Abhyankar   Input Parameters:
3436c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3437c688d042SShri Abhyankar 
3438c688d042SShri Abhyankar   Notes:
3439c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3440c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3441c688d042SShri Abhyankar 
3442c688d042SShri Abhyankar   Level: developer
3443c688d042SShri Abhyankar 
3444c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3445c688d042SShri Abhyankar @*/
3446c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3447c688d042SShri Abhyankar {
3448c688d042SShri Abhyankar   PetscErrorCode ierr;
3449c688d042SShri Abhyankar 
3450c688d042SShri Abhyankar   PetscFunctionBegin;
3451c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3452c688d042SShri Abhyankar   if (ts->postevaluate) {
3453dcb233daSLisandro Dalcin     Vec              U;
3454dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3455dcb233daSLisandro Dalcin 
3456dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3457dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3458c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3459dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3460dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3461c688d042SShri Abhyankar   }
3462c688d042SShri Abhyankar   PetscFunctionReturn(0);
3463c688d042SShri Abhyankar }
3464c688d042SShri Abhyankar 
3465ac226902SBarry Smith /*@C
3466000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34673f2090d5SJed Brown   called once at the end of each time step.
3468000e7ae3SMatthew Knepley 
34693f9fe445SBarry Smith   Logically Collective on TS
3470000e7ae3SMatthew Knepley 
3471000e7ae3SMatthew Knepley   Input Parameters:
3472000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3473000e7ae3SMatthew Knepley - func - The function
3474000e7ae3SMatthew Knepley 
3475000e7ae3SMatthew Knepley   Calling sequence of func:
3476b8123daeSJed Brown $ func (TS ts);
3477000e7ae3SMatthew Knepley 
34781785ff2aSShri Abhyankar   Notes:
34791785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34801785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34811785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34821785ff2aSShri Abhyankar 
3483000e7ae3SMatthew Knepley   Level: intermediate
3484000e7ae3SMatthew Knepley 
3485dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3486000e7ae3SMatthew Knepley @*/
34877087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3488000e7ae3SMatthew Knepley {
3489000e7ae3SMatthew Knepley   PetscFunctionBegin;
34900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3491ae60f76fSBarry Smith   ts->poststep = func;
3492000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3493000e7ae3SMatthew Knepley }
3494000e7ae3SMatthew Knepley 
349509ee8438SJed Brown /*@
34963f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34973f2090d5SJed Brown 
34983f2090d5SJed Brown   Collective on TS
34993f2090d5SJed Brown 
35003f2090d5SJed Brown   Input Parameters:
35013f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35023f2090d5SJed Brown 
35033f2090d5SJed Brown   Notes:
35043f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35053f2090d5SJed Brown   so most users would not generally call this routine themselves.
35063f2090d5SJed Brown 
35073f2090d5SJed Brown   Level: developer
35083f2090d5SJed Brown 
35093f2090d5SJed Brown @*/
35107087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35113f2090d5SJed Brown {
35123f2090d5SJed Brown   PetscErrorCode ierr;
35133f2090d5SJed Brown 
35143f2090d5SJed Brown   PetscFunctionBegin;
35150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3516ae60f76fSBarry Smith   if (ts->poststep) {
35175efd42a4SStefano Zampini     Vec              U;
35185efd42a4SStefano Zampini     PetscObjectState sprev,spost;
35195efd42a4SStefano Zampini 
35205efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
35215efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3522ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
35235efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3524dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
352572ac3e02SJed Brown   }
35263f2090d5SJed Brown   PetscFunctionReturn(0);
35273f2090d5SJed Brown }
35283f2090d5SJed Brown 
3529d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3530d763cef2SBarry Smith 
3531d763cef2SBarry Smith /*@C
3532a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3533d763cef2SBarry Smith    timestep to display the iteration's  progress.
3534d763cef2SBarry Smith 
35353f9fe445SBarry Smith    Logically Collective on TS
3536d763cef2SBarry Smith 
3537d763cef2SBarry Smith    Input Parameters:
3538d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3539e213d8f1SJed Brown .  monitor - monitoring routine
3540329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35410298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3542b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35430298fd71SBarry Smith           (may be NULL)
3544d763cef2SBarry Smith 
3545e213d8f1SJed Brown    Calling sequence of monitor:
3546dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3547d763cef2SBarry Smith 
3548d763cef2SBarry Smith +    ts - the TS context
354963e21af5SBarry 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)
35501f06c33eSBarry Smith .    time - current time
35510910c330SBarry Smith .    u - current iterate
3552d763cef2SBarry Smith -    mctx - [optional] monitoring context
3553d763cef2SBarry Smith 
3554d763cef2SBarry Smith    Notes:
3555d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3556d763cef2SBarry Smith    already has been loaded.
3557d763cef2SBarry Smith 
355895452b02SPatrick Sanan    Fortran Notes:
355995452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3560025f1a04SBarry Smith 
3561d763cef2SBarry Smith    Level: intermediate
3562d763cef2SBarry Smith 
3563a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3564d763cef2SBarry Smith @*/
3565c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3566d763cef2SBarry Smith {
356778064530SBarry Smith   PetscErrorCode ierr;
356878064530SBarry Smith   PetscInt       i;
356978064530SBarry Smith   PetscBool      identical;
357078064530SBarry Smith 
3571d763cef2SBarry Smith   PetscFunctionBegin;
35720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
357378064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
357478064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
357578064530SBarry Smith     if (identical) PetscFunctionReturn(0);
357678064530SBarry Smith   }
357717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3578d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35798704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3580d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3581d763cef2SBarry Smith   PetscFunctionReturn(0);
3582d763cef2SBarry Smith }
3583d763cef2SBarry Smith 
3584d763cef2SBarry Smith /*@C
3585a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3586d763cef2SBarry Smith 
35873f9fe445SBarry Smith    Logically Collective on TS
3588d763cef2SBarry Smith 
3589d763cef2SBarry Smith    Input Parameters:
3590d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3591d763cef2SBarry Smith 
3592d763cef2SBarry Smith    Notes:
3593d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3594d763cef2SBarry Smith 
3595d763cef2SBarry Smith    Level: intermediate
3596d763cef2SBarry Smith 
3597a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3598d763cef2SBarry Smith @*/
35997087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3600d763cef2SBarry Smith {
3601d952e501SBarry Smith   PetscErrorCode ierr;
3602d952e501SBarry Smith   PetscInt       i;
3603d952e501SBarry Smith 
3604d763cef2SBarry Smith   PetscFunctionBegin;
36050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3606d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36078704b422SBarry Smith     if (ts->monitordestroy[i]) {
36088704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3609d952e501SBarry Smith     }
3610d952e501SBarry Smith   }
3611d763cef2SBarry Smith   ts->numbermonitors = 0;
3612d763cef2SBarry Smith   PetscFunctionReturn(0);
3613d763cef2SBarry Smith }
3614d763cef2SBarry Smith 
3615721cd6eeSBarry Smith /*@C
3616721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36175516499fSSatish Balay 
36185516499fSSatish Balay    Level: intermediate
361941251cbbSSatish Balay 
362063e21af5SBarry Smith .seealso:  TSMonitorSet()
362141251cbbSSatish Balay @*/
3622721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3623d763cef2SBarry Smith {
3624dfbe8321SBarry Smith   PetscErrorCode ierr;
3625721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
362641aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3627d132466eSBarry Smith 
3628d763cef2SBarry Smith   PetscFunctionBegin;
36294d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
363041aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
363141aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3632721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
363341aca3d6SBarry Smith   if (iascii) {
3634649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
363563e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
363663e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
363763e21af5SBarry Smith     } else {
36388392e04aSShri 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);
363963e21af5SBarry Smith     }
3640649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
364141aca3d6SBarry Smith   } else if (ibinary) {
364241aca3d6SBarry Smith     PetscMPIInt rank;
364341aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
364441aca3d6SBarry Smith     if (!rank) {
3645450a797fSBarry Smith       PetscBool skipHeader;
3646450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3647450a797fSBarry Smith 
3648450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3649450a797fSBarry Smith       if (!skipHeader) {
3650f253e43cSLisandro Dalcin          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
3651450a797fSBarry Smith        }
365241aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
365341aca3d6SBarry Smith     } else {
365441aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
365541aca3d6SBarry Smith     }
365641aca3d6SBarry Smith   }
3657721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3658d763cef2SBarry Smith   PetscFunctionReturn(0);
3659d763cef2SBarry Smith }
3660d763cef2SBarry Smith 
3661cc9c3a59SBarry Smith /*@C
3662cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3663cc9c3a59SBarry Smith 
3664cc9c3a59SBarry Smith    Level: intermediate
3665cc9c3a59SBarry Smith 
3666cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3667cc9c3a59SBarry Smith @*/
3668cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3669cc9c3a59SBarry Smith {
3670cc9c3a59SBarry Smith   PetscErrorCode ierr;
3671cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3672cc9c3a59SBarry Smith   PetscBool      iascii;
3673cc9c3a59SBarry Smith   PetscReal      max,min;
3674cc9c3a59SBarry Smith 
3675cc9c3a59SBarry Smith 
3676cc9c3a59SBarry Smith   PetscFunctionBegin;
3677cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3678cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3679cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3680cc9c3a59SBarry Smith   if (iascii) {
3681cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3682cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3683cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
36845132926bSBarry 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);
3685cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3686cc9c3a59SBarry Smith   }
3687cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3688cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3689cc9c3a59SBarry Smith }
3690cc9c3a59SBarry Smith 
3691cd652676SJed Brown /*@
3692cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3693cd652676SJed Brown 
3694cd652676SJed Brown    Collective on TS
3695cd652676SJed Brown 
3696cd652676SJed Brown    Input Argument:
3697cd652676SJed Brown +  ts - time stepping context
3698cd652676SJed Brown -  t - time to interpolate to
3699cd652676SJed Brown 
3700cd652676SJed Brown    Output Argument:
37010910c330SBarry Smith .  U - state at given time
3702cd652676SJed Brown 
3703cd652676SJed Brown    Level: intermediate
3704cd652676SJed Brown 
3705cd652676SJed Brown    Developer Notes:
3706cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3707cd652676SJed Brown 
3708874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3709cd652676SJed Brown @*/
37100910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3711cd652676SJed Brown {
3712cd652676SJed Brown   PetscErrorCode ierr;
3713cd652676SJed Brown 
3714cd652676SJed Brown   PetscFunctionBegin;
3715cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3716b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3717be5899b3SLisandro 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);
3718ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
37190910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3720cd652676SJed Brown   PetscFunctionReturn(0);
3721cd652676SJed Brown }
3722cd652676SJed Brown 
3723d763cef2SBarry Smith /*@
37246d9e5789SSean Farley    TSStep - Steps one time step
3725d763cef2SBarry Smith 
3726d763cef2SBarry Smith    Collective on TS
3727d763cef2SBarry Smith 
3728d763cef2SBarry Smith    Input Parameter:
3729d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3730d763cef2SBarry Smith 
373127829d71SBarry Smith    Level: developer
3732d763cef2SBarry Smith 
3733b8123daeSJed Brown    Notes:
373427829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
373527829d71SBarry Smith 
3736b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3737b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3738b8123daeSJed Brown 
373919eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
374019eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
374125cb2221SBarry Smith 
37429be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3743d763cef2SBarry Smith @*/
3744193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3745d763cef2SBarry Smith {
3746dfbe8321SBarry Smith   PetscErrorCode   ierr;
3747fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3748be5899b3SLisandro Dalcin   PetscReal        ptime;
3749d763cef2SBarry Smith 
3750d763cef2SBarry Smith   PetscFunctionBegin;
37510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3752f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3753fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3754f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3755f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3756f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3757f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3758f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3759fffbeea8SBarry Smith 
3760d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
376168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3762d405a339SMatthew Knepley 
3763fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3764ef85077eSLisandro 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>");
3765a6772fa2SLisandro 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()");
3766be5899b3SLisandro 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");
3767a6772fa2SLisandro Dalcin 
3768be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3769be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37702808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3771fc8dbba5SLisandro Dalcin 
3772d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3773193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3774d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3775fc8dbba5SLisandro Dalcin 
3776fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3777be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
37782808aa04SLisandro Dalcin     ts->steps++;
3779be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
378028d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3781d2daff3dSHong Zhang   }
3782fc8dbba5SLisandro Dalcin 
3783fc8dbba5SLisandro Dalcin   if (!ts->reason) {
378408c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
378508c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
378608c7845fSBarry Smith   }
3787fc8dbba5SLisandro Dalcin 
3788fc8dbba5SLisandro 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]);
3789fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
379008c7845fSBarry Smith   PetscFunctionReturn(0);
379108c7845fSBarry Smith }
379208c7845fSBarry Smith 
379308c7845fSBarry Smith /*@
37947cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37957cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37967cbde773SLisandro Dalcin 
37977cbde773SLisandro Dalcin    Collective on TS
37987cbde773SLisandro Dalcin 
37997cbde773SLisandro Dalcin    Input Arguments:
38007cbde773SLisandro Dalcin +  ts - time stepping context
38017cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
38027cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
38037cbde773SLisandro Dalcin 
38047cbde773SLisandro Dalcin    Output Arguments:
38057cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
38067cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
38077cbde773SLisandro Dalcin 
38087cbde773SLisandro Dalcin    Level: advanced
38097cbde773SLisandro Dalcin 
38107cbde773SLisandro Dalcin    Notes:
38117cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
38127cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
38137cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
38147cbde773SLisandro Dalcin 
38157cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
38167cbde773SLisandro Dalcin @*/
38177cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
38187cbde773SLisandro Dalcin {
38197cbde773SLisandro Dalcin   PetscErrorCode ierr;
38207cbde773SLisandro Dalcin 
38217cbde773SLisandro Dalcin   PetscFunctionBegin;
38227cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38237cbde773SLisandro Dalcin   PetscValidType(ts,1);
38247cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
38257cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
38267cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
38277cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
38287cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
38297cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
38307cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
38317cbde773SLisandro Dalcin   PetscFunctionReturn(0);
38327cbde773SLisandro Dalcin }
38337cbde773SLisandro Dalcin 
383405175c85SJed Brown /*@
383505175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
383605175c85SJed Brown 
38371c3436cfSJed Brown    Collective on TS
383805175c85SJed Brown 
383905175c85SJed Brown    Input Arguments:
38401c3436cfSJed Brown +  ts - time stepping context
38411c3436cfSJed Brown .  order - desired order of accuracy
38420298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
384305175c85SJed Brown 
384405175c85SJed Brown    Output Arguments:
38450910c330SBarry Smith .  U - state at the end of the current step
384605175c85SJed Brown 
384705175c85SJed Brown    Level: advanced
384805175c85SJed Brown 
3849108c343cSJed Brown    Notes:
3850108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3851108c343cSJed 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.
3852108c343cSJed Brown 
38531c3436cfSJed Brown .seealso: TSStep(), TSAdapt
385405175c85SJed Brown @*/
38550910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
385605175c85SJed Brown {
385705175c85SJed Brown   PetscErrorCode ierr;
385805175c85SJed Brown 
385905175c85SJed Brown   PetscFunctionBegin;
386005175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
386105175c85SJed Brown   PetscValidType(ts,1);
38620910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3863ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38640910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
386505175c85SJed Brown   PetscFunctionReturn(0);
386605175c85SJed Brown }
386705175c85SJed Brown 
3868aad739acSMatthew G. Knepley /*@C
38692e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3870aad739acSMatthew G. Knepley 
3871aad739acSMatthew G. Knepley   Not collective
3872aad739acSMatthew G. Knepley 
3873aad739acSMatthew G. Knepley   Input Argument:
3874aad739acSMatthew G. Knepley . ts        - time stepping context
3875aad739acSMatthew G. Knepley 
3876aad739acSMatthew G. Knepley   Output Argument:
38772e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3878aad739acSMatthew G. Knepley 
3879aad739acSMatthew G. Knepley    Level: advanced
3880aad739acSMatthew G. Knepley 
3881aad739acSMatthew G. Knepley    Notes:
3882aad739acSMatthew G. Knepley    The calling sequence for the function is
38832e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3884aad739acSMatthew G. Knepley $ ts - The timestepping context
38852e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3886aad739acSMatthew G. Knepley 
38872e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3888aad739acSMatthew G. Knepley @*/
38892e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3890aad739acSMatthew G. Knepley {
3891aad739acSMatthew G. Knepley   PetscFunctionBegin;
3892aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38932e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38942e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3895aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3896aad739acSMatthew G. Knepley }
3897aad739acSMatthew G. Knepley 
3898aad739acSMatthew G. Knepley /*@C
38992e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3900aad739acSMatthew G. Knepley 
3901aad739acSMatthew G. Knepley   Logically collective on ts
3902aad739acSMatthew G. Knepley 
3903aad739acSMatthew G. Knepley   Input Arguments:
3904aad739acSMatthew G. Knepley + ts        - time stepping context
39052e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3906aad739acSMatthew G. Knepley 
3907aad739acSMatthew G. Knepley   Level: advanced
3908aad739acSMatthew G. Knepley 
3909a96d6ef6SBarry Smith   Calling sequence for initCondition:
3910a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3911a96d6ef6SBarry Smith 
3912a96d6ef6SBarry Smith + ts - The timestepping context
3913a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3914aad739acSMatthew G. Knepley 
39152e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3916aad739acSMatthew G. Knepley @*/
39172e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3918aad739acSMatthew G. Knepley {
3919aad739acSMatthew G. Knepley   PetscFunctionBegin;
3920aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
39212e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
39222e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3923aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3924aad739acSMatthew G. Knepley }
3925aad739acSMatthew G. Knepley 
3926aad739acSMatthew G. Knepley /*@
39272e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3928aad739acSMatthew G. Knepley 
3929aad739acSMatthew G. Knepley   Collective on ts
3930aad739acSMatthew G. Knepley 
3931aad739acSMatthew G. Knepley   Input Arguments:
3932aad739acSMatthew G. Knepley + ts - time stepping context
39332e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3934aad739acSMatthew G. Knepley 
3935aad739acSMatthew G. Knepley   Level: advanced
3936aad739acSMatthew G. Knepley 
39372e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3938aad739acSMatthew G. Knepley @*/
39392e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3940aad739acSMatthew G. Knepley {
3941aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3942aad739acSMatthew G. Knepley 
3943aad739acSMatthew G. Knepley   PetscFunctionBegin;
3944aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3945aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
39462e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3947aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3948aad739acSMatthew G. Knepley }
3949aad739acSMatthew G. Knepley 
3950aad739acSMatthew G. Knepley /*@C
3951aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3952aad739acSMatthew G. Knepley 
3953aad739acSMatthew G. Knepley   Not collective
3954aad739acSMatthew G. Knepley 
3955aad739acSMatthew G. Knepley   Input Argument:
3956aad739acSMatthew G. Knepley . ts         - time stepping context
3957aad739acSMatthew G. Knepley 
3958aad739acSMatthew G. Knepley   Output Argument:
3959aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3960aad739acSMatthew G. Knepley 
3961aad739acSMatthew G. Knepley   Level: advanced
3962aad739acSMatthew G. Knepley 
3963a96d6ef6SBarry Smith   Calling sequence for exactError:
3964a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3965a96d6ef6SBarry Smith 
3966a96d6ef6SBarry Smith + ts - The timestepping context
3967a96d6ef6SBarry Smith . u  - The approximate solution vector
3968a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3969aad739acSMatthew G. Knepley 
3970aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3971aad739acSMatthew G. Knepley @*/
3972aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3973aad739acSMatthew G. Knepley {
3974aad739acSMatthew G. Knepley   PetscFunctionBegin;
3975aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3976aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3977aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3978aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3979aad739acSMatthew G. Knepley }
3980aad739acSMatthew G. Knepley 
3981aad739acSMatthew G. Knepley /*@C
3982aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3983aad739acSMatthew G. Knepley 
3984aad739acSMatthew G. Knepley   Logically collective on ts
3985aad739acSMatthew G. Knepley 
3986aad739acSMatthew G. Knepley   Input Arguments:
3987aad739acSMatthew G. Knepley + ts         - time stepping context
3988aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3989aad739acSMatthew G. Knepley 
3990aad739acSMatthew G. Knepley   Level: advanced
3991aad739acSMatthew G. Knepley 
3992a96d6ef6SBarry Smith   Calling sequence for exactError:
3993a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3994a96d6ef6SBarry Smith 
3995a96d6ef6SBarry Smith + ts - The timestepping context
3996a96d6ef6SBarry Smith . u  - The approximate solution vector
3997a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3998aad739acSMatthew G. Knepley 
3999aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
4000aad739acSMatthew G. Knepley @*/
4001aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
4002aad739acSMatthew G. Knepley {
4003aad739acSMatthew G. Knepley   PetscFunctionBegin;
4004aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4005f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
4006aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
4007aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4008aad739acSMatthew G. Knepley }
4009aad739acSMatthew G. Knepley 
4010aad739acSMatthew G. Knepley /*@
4011aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
4012aad739acSMatthew G. Knepley 
4013aad739acSMatthew G. Knepley   Collective on ts
4014aad739acSMatthew G. Knepley 
4015aad739acSMatthew G. Knepley   Input Arguments:
4016aad739acSMatthew G. Knepley + ts - time stepping context
4017aad739acSMatthew G. Knepley . u  - The approximate solution
4018aad739acSMatthew G. Knepley - e  - The Vec used to store the error
4019aad739acSMatthew G. Knepley 
4020aad739acSMatthew G. Knepley   Level: advanced
4021aad739acSMatthew G. Knepley 
40222e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
4023aad739acSMatthew G. Knepley @*/
4024aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
4025aad739acSMatthew G. Knepley {
4026aad739acSMatthew G. Knepley   PetscErrorCode ierr;
4027aad739acSMatthew G. Knepley 
4028aad739acSMatthew G. Knepley   PetscFunctionBegin;
4029aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4030aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
4031aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
4032aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
4033aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4034aad739acSMatthew G. Knepley }
4035aad739acSMatthew G. Knepley 
4036b1cb36f3SHong Zhang /*@
40376a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
40386a4d4014SLisandro Dalcin 
40396a4d4014SLisandro Dalcin    Collective on TS
40406a4d4014SLisandro Dalcin 
40416a4d4014SLisandro Dalcin    Input Parameter:
40426a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
404363e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
404463e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
40455a3a76d0SJed Brown 
40466a4d4014SLisandro Dalcin    Level: beginner
40476a4d4014SLisandro Dalcin 
40485a3a76d0SJed Brown    Notes:
40495a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
40505a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40515a3a76d0SJed Brown    stepped over the final time.
40525a3a76d0SJed Brown 
405363e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
40546a4d4014SLisandro Dalcin @*/
4055cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
40566a4d4014SLisandro Dalcin {
4057b06615a5SLisandro Dalcin   Vec               solution;
40586a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
4059f22f69f0SBarry Smith 
40606a4d4014SLisandro Dalcin   PetscFunctionBegin;
40610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4062f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4063303a5415SBarry Smith 
4064303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
4065ee41a567SStefano 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 */
40660910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4067b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4068b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4069b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40705a3a76d0SJed Brown     }
40710910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4072715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4073bbd56ea5SKarl Rupp   } else if (u) {
40740910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40755a3a76d0SJed Brown   }
4076b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
407768bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4078a6772fa2SLisandro Dalcin 
4079ef85077eSLisandro 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>");
4080a6772fa2SLisandro 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()");
4081a6772fa2SLisandro 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");
4082a6772fa2SLisandro Dalcin 
4083715f1b00SHong Zhang   if (ts->forward_solve) {
4084715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4085715f1b00SHong Zhang   }
4086715f1b00SHong Zhang 
4087e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4088715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4089e7069c78SShri      TSTrajectory to incorrectly save the output files
4090e7069c78SShri   */
4091715f1b00SHong Zhang   /* reset time step and iteration counters */
40922808aa04SLisandro Dalcin   if (!ts->steps) {
40935ef26d82SJed Brown     ts->ksp_its           = 0;
40945ef26d82SJed Brown     ts->snes_its          = 0;
4095c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4096c610991cSLisandro Dalcin     ts->reject            = 0;
40972808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40982808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40997d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
41002808aa04SLisandro Dalcin   }
4101e97c63d7SStefano Zampini 
4102e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4103e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4104e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4105e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4106e97c63d7SStefano Zampini 
4107e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4108e97c63d7SStefano Zampini   }
4109193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4110193ac0bcSJed Brown 
4111900f6b5bSMatthew G. Knepley   {
4112900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4113900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4114900f6b5bSMatthew G. Knepley     PetscBool         flg;
4115900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4116900f6b5bSMatthew G. Knepley 
4117900f6b5bSMatthew G. Knepley     if (!incall) {
4118900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4119900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4120900f6b5bSMatthew G. Knepley       if (flg) {
4121900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4122900f6b5bSMatthew G. Knepley         DM           dm;
4123900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4124900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4125f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4126900f6b5bSMatthew G. Knepley 
4127900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4128f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
4129900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4130900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4131900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4132900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4133f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
4134900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4135900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4136900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4137900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4138900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4139900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4140900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4141900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4142900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4143900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4144900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4145900f6b5bSMatthew G. Knepley       }
4146900f6b5bSMatthew G. Knepley     }
4147900f6b5bSMatthew G. Knepley   }
4148900f6b5bSMatthew G. Knepley 
4149ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4150f05ece33SBarry Smith 
4151193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4152193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4153a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4154cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4155a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4156be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4157db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4158db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4159e7069c78SShri 
41602808aa04SLisandro Dalcin     if (!ts->steps) {
41612808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41626427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41632808aa04SLisandro Dalcin     }
41646427ac75SLisandro Dalcin 
4165e1a7a14fSJed Brown     while (!ts->reason) {
41662808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41679687d888SLisandro Dalcin       if (!ts->steprollback) {
41689687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41699687d888SLisandro Dalcin       }
4170193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4171f3b1f45cSBarry Smith       if (ts->testjacobian) {
4172f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4173f3b1f45cSBarry Smith       }
4174f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4175f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4176f3b1f45cSBarry Smith       }
4177cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
41787b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4179b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
41807b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4181b1cb36f3SHong Zhang       }
418258818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41837b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4184715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
41857b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4186715f1b00SHong Zhang       }
418710b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4188e783b05fSHong 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. */
418958818c2dSLisandro Dalcin       if (ts->steprollback) {
419058818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
419158818c2dSLisandro Dalcin       }
4192e783b05fSHong Zhang       if (!ts->steprollback) {
41932808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41941eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4195aeb4809dSShri Abhyankar       }
4196193ac0bcSJed Brown     }
41972808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41986427ac75SLisandro Dalcin 
419949354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
42000910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4201cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4202b06615a5SLisandro Dalcin       solution = u;
420363e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
42040574a7fbSJed Brown     } else {
4205ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4206cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4207b06615a5SLisandro Dalcin       solution = ts->vec_sol;
42080574a7fbSJed Brown     }
4209193ac0bcSJed Brown   }
4210d2daff3dSHong Zhang 
4211ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
421263e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
421356f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4214ef222394SBarry Smith   if (ts->adjoint_solve) {
4215ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
42162b0a91c0SBarry Smith   }
42176a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
42186a4d4014SLisandro Dalcin }
42196a4d4014SLisandro Dalcin 
42207db568b7SBarry Smith /*@C
4221228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4222228d79bcSJed Brown 
4223228d79bcSJed Brown    Collective on TS
4224228d79bcSJed Brown 
4225228d79bcSJed Brown    Input Parameters:
4226228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4227228d79bcSJed Brown .  step - step number that has just completed
4228228d79bcSJed Brown .  ptime - model time of the state
42290910c330SBarry Smith -  u - state at the current model time
4230228d79bcSJed Brown 
4231228d79bcSJed Brown    Notes:
42327db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
42337db568b7SBarry Smith    Users would almost never call this routine directly.
4234228d79bcSJed Brown 
423563e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
423663e21af5SBarry Smith 
42377db568b7SBarry Smith    Level: developer
4238228d79bcSJed Brown 
4239228d79bcSJed Brown @*/
42400910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4241d763cef2SBarry Smith {
4242d6152f81SLisandro Dalcin   DM             dm;
4243a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4244d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4245d763cef2SBarry Smith 
4246d763cef2SBarry Smith   PetscFunctionBegin;
4247b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4248b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4249d6152f81SLisandro Dalcin 
4250d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4251d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4252d6152f81SLisandro Dalcin 
42538860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4254d763cef2SBarry Smith   for (i=0; i<n; i++) {
42550910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4256d763cef2SBarry Smith   }
42578860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4258d763cef2SBarry Smith   PetscFunctionReturn(0);
4259d763cef2SBarry Smith }
4260d763cef2SBarry Smith 
4261d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4262d763cef2SBarry Smith /*@C
42637db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4264a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4265d763cef2SBarry Smith 
4266d763cef2SBarry Smith    Collective on TS
4267d763cef2SBarry Smith 
4268d763cef2SBarry Smith    Input Parameters:
4269d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4270d763cef2SBarry Smith .  label - the title to put in the title bar
42717c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4272a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4273a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4274d763cef2SBarry Smith 
4275d763cef2SBarry Smith    Output Parameter:
42760b039ecaSBarry Smith .  ctx - the context
4277d763cef2SBarry Smith 
4278d763cef2SBarry Smith    Options Database Key:
42794f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42808b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
42817db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42827db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42837db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42847db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4285b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4286d763cef2SBarry Smith 
4287d763cef2SBarry Smith    Notes:
4288a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4289d763cef2SBarry Smith 
42907db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42917db568b7SBarry Smith 
42927db568b7SBarry 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
42937db568b7SBarry 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
42947db568b7SBarry Smith    as the first argument.
42957db568b7SBarry Smith 
42967db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42977db568b7SBarry Smith 
4298d763cef2SBarry Smith    Level: intermediate
4299d763cef2SBarry Smith 
43007db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
43017db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
43027db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
43037db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
43047db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
43057c922b88SBarry Smith 
4306d763cef2SBarry Smith @*/
4307a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4308d763cef2SBarry Smith {
43097f52daa2SLisandro Dalcin   PetscDraw      draw;
4310dfbe8321SBarry Smith   PetscErrorCode ierr;
4311d763cef2SBarry Smith 
4312d763cef2SBarry Smith   PetscFunctionBegin;
4313b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
43147f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43157f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43167f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4317287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43187f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4319a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4320d763cef2SBarry Smith   PetscFunctionReturn(0);
4321d763cef2SBarry Smith }
4322d763cef2SBarry Smith 
4323b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4324d763cef2SBarry Smith {
43250b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4326c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4327dfbe8321SBarry Smith   PetscErrorCode ierr;
4328d763cef2SBarry Smith 
4329d763cef2SBarry Smith   PetscFunctionBegin;
433063e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4331b06615a5SLisandro Dalcin   if (!step) {
4332a9f9c1f6SBarry Smith     PetscDrawAxis axis;
43338b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4334a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43358b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4336a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4337a9f9c1f6SBarry Smith   }
4338c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43398b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
43400b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4341b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43420b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43436934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
43443923b477SBarry Smith   }
4345d763cef2SBarry Smith   PetscFunctionReturn(0);
4346d763cef2SBarry Smith }
4347d763cef2SBarry Smith 
4348d763cef2SBarry Smith /*@C
4349a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4350a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4351d763cef2SBarry Smith 
43520b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4353d763cef2SBarry Smith 
4354d763cef2SBarry Smith    Input Parameter:
43550b039ecaSBarry Smith .  ctx - the monitor context
4356d763cef2SBarry Smith 
4357d763cef2SBarry Smith    Level: intermediate
4358d763cef2SBarry Smith 
43594f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4360d763cef2SBarry Smith @*/
4361a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4362d763cef2SBarry Smith {
4363dfbe8321SBarry Smith   PetscErrorCode ierr;
4364d763cef2SBarry Smith 
4365d763cef2SBarry Smith   PetscFunctionBegin;
43667684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43677684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43687684fa3eSBarry Smith   }
43690b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
437031152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4371387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4372387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4373387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43740b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4375d763cef2SBarry Smith   PetscFunctionReturn(0);
4376d763cef2SBarry Smith }
4377d763cef2SBarry Smith 
43781b575b74SJoseph Pusztay /*
43791b575b74SJoseph Pusztay 
43801b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
43811b575b74SJoseph Pusztay 
43821b575b74SJoseph Pusztay */
43831b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43841b575b74SJoseph Pusztay {
43851b575b74SJoseph Pusztay   PetscDraw      draw;
43861b575b74SJoseph Pusztay   PetscErrorCode ierr;
43871b575b74SJoseph Pusztay 
43881b575b74SJoseph Pusztay   PetscFunctionBegin;
43891b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43901b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43911b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43921b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43931b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43941b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43951b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43961b575b74SJoseph Pusztay 
43971b575b74SJoseph Pusztay }
43981b575b74SJoseph Pusztay 
43991b575b74SJoseph Pusztay /*
44001b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
44011b575b74SJoseph Pusztay */
44021b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
44031b575b74SJoseph Pusztay {
44041b575b74SJoseph Pusztay   PetscErrorCode ierr;
44051b575b74SJoseph Pusztay 
44061b575b74SJoseph Pusztay   PetscFunctionBegin;
44071b575b74SJoseph Pusztay 
44081b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
44091b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
44101b575b74SJoseph Pusztay 
44111b575b74SJoseph Pusztay   PetscFunctionReturn(0);
44121b575b74SJoseph Pusztay 
44131b575b74SJoseph Pusztay }
44141b575b74SJoseph Pusztay 
4415d763cef2SBarry Smith /*@
4416b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4417d763cef2SBarry Smith 
4418d763cef2SBarry Smith    Not Collective
4419d763cef2SBarry Smith 
4420d763cef2SBarry Smith    Input Parameter:
4421d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4422d763cef2SBarry Smith 
4423d763cef2SBarry Smith    Output Parameter:
442419eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4425d763cef2SBarry Smith 
4426d763cef2SBarry Smith    Level: beginner
4427d763cef2SBarry Smith 
4428b8123daeSJed Brown    Note:
4429b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
44309be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4431b8123daeSJed Brown 
44328f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4433d763cef2SBarry Smith 
4434d763cef2SBarry Smith @*/
44357087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4436d763cef2SBarry Smith {
4437d763cef2SBarry Smith   PetscFunctionBegin;
44380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4439f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4440d763cef2SBarry Smith   *t = ts->ptime;
4441d763cef2SBarry Smith   PetscFunctionReturn(0);
4442d763cef2SBarry Smith }
4443d763cef2SBarry Smith 
4444e5e524a1SHong Zhang /*@
4445e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4446e5e524a1SHong Zhang 
4447e5e524a1SHong Zhang    Not Collective
4448e5e524a1SHong Zhang 
4449e5e524a1SHong Zhang    Input Parameter:
4450e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4451e5e524a1SHong Zhang 
4452e5e524a1SHong Zhang    Output Parameter:
4453e5e524a1SHong Zhang .  t  - the previous time
4454e5e524a1SHong Zhang 
4455e5e524a1SHong Zhang    Level: beginner
4456e5e524a1SHong Zhang 
4457aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4458e5e524a1SHong Zhang 
4459e5e524a1SHong Zhang @*/
4460e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4461e5e524a1SHong Zhang {
4462e5e524a1SHong Zhang   PetscFunctionBegin;
4463e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4464e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4465e5e524a1SHong Zhang   *t = ts->ptime_prev;
4466e5e524a1SHong Zhang   PetscFunctionReturn(0);
4467e5e524a1SHong Zhang }
4468e5e524a1SHong Zhang 
44696a4d4014SLisandro Dalcin /*@
44706a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44716a4d4014SLisandro Dalcin 
44723f9fe445SBarry Smith    Logically Collective on TS
44736a4d4014SLisandro Dalcin 
44746a4d4014SLisandro Dalcin    Input Parameters:
44756a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44766a4d4014SLisandro Dalcin -  time - the time
44776a4d4014SLisandro Dalcin 
44786a4d4014SLisandro Dalcin    Level: intermediate
44796a4d4014SLisandro Dalcin 
448019eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
44816a4d4014SLisandro Dalcin 
44826a4d4014SLisandro Dalcin @*/
44837087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44846a4d4014SLisandro Dalcin {
44856a4d4014SLisandro Dalcin   PetscFunctionBegin;
44860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4487c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44886a4d4014SLisandro Dalcin   ts->ptime = t;
44896a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44906a4d4014SLisandro Dalcin }
44916a4d4014SLisandro Dalcin 
4492d763cef2SBarry Smith /*@C
4493d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4494d763cef2SBarry Smith    TS options in the database.
4495d763cef2SBarry Smith 
44963f9fe445SBarry Smith    Logically Collective on TS
4497d763cef2SBarry Smith 
4498d763cef2SBarry Smith    Input Parameter:
4499d763cef2SBarry Smith +  ts     - The TS context
4500d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4501d763cef2SBarry Smith 
4502d763cef2SBarry Smith    Notes:
4503d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4504d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4505d763cef2SBarry Smith    hyphen.
4506d763cef2SBarry Smith 
4507d763cef2SBarry Smith    Level: advanced
4508d763cef2SBarry Smith 
4509d763cef2SBarry Smith .seealso: TSSetFromOptions()
4510d763cef2SBarry Smith 
4511d763cef2SBarry Smith @*/
45127087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4513d763cef2SBarry Smith {
4514dfbe8321SBarry Smith   PetscErrorCode ierr;
4515089b2837SJed Brown   SNES           snes;
4516d763cef2SBarry Smith 
4517d763cef2SBarry Smith   PetscFunctionBegin;
45180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4519d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4520089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4521089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4522d763cef2SBarry Smith   PetscFunctionReturn(0);
4523d763cef2SBarry Smith }
4524d763cef2SBarry Smith 
4525d763cef2SBarry Smith /*@C
4526d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4527d763cef2SBarry Smith    TS options in the database.
4528d763cef2SBarry Smith 
45293f9fe445SBarry Smith    Logically Collective on TS
4530d763cef2SBarry Smith 
4531d763cef2SBarry Smith    Input Parameter:
4532d763cef2SBarry Smith +  ts     - The TS context
4533d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4534d763cef2SBarry Smith 
4535d763cef2SBarry Smith    Notes:
4536d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4537d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4538d763cef2SBarry Smith    hyphen.
4539d763cef2SBarry Smith 
4540d763cef2SBarry Smith    Level: advanced
4541d763cef2SBarry Smith 
4542d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4543d763cef2SBarry Smith 
4544d763cef2SBarry Smith @*/
45457087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4546d763cef2SBarry Smith {
4547dfbe8321SBarry Smith   PetscErrorCode ierr;
4548089b2837SJed Brown   SNES           snes;
4549d763cef2SBarry Smith 
4550d763cef2SBarry Smith   PetscFunctionBegin;
45510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4552d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4553089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4554089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4555d763cef2SBarry Smith   PetscFunctionReturn(0);
4556d763cef2SBarry Smith }
4557d763cef2SBarry Smith 
4558d763cef2SBarry Smith /*@C
4559d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4560d763cef2SBarry Smith    TS options in the database.
4561d763cef2SBarry Smith 
4562d763cef2SBarry Smith    Not Collective
4563d763cef2SBarry Smith 
4564d763cef2SBarry Smith    Input Parameter:
4565d763cef2SBarry Smith .  ts - The TS context
4566d763cef2SBarry Smith 
4567d763cef2SBarry Smith    Output Parameter:
4568d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4569d763cef2SBarry Smith 
457095452b02SPatrick Sanan    Notes:
457195452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4572d763cef2SBarry Smith    sufficient length to hold the prefix.
4573d763cef2SBarry Smith 
4574d763cef2SBarry Smith    Level: intermediate
4575d763cef2SBarry Smith 
4576d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4577d763cef2SBarry Smith @*/
45787087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4579d763cef2SBarry Smith {
4580dfbe8321SBarry Smith   PetscErrorCode ierr;
4581d763cef2SBarry Smith 
4582d763cef2SBarry Smith   PetscFunctionBegin;
45830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45844482741eSBarry Smith   PetscValidPointer(prefix,2);
4585d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4586d763cef2SBarry Smith   PetscFunctionReturn(0);
4587d763cef2SBarry Smith }
4588d763cef2SBarry Smith 
4589d763cef2SBarry Smith /*@C
4590d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4591d763cef2SBarry Smith 
4592d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4593d763cef2SBarry Smith 
4594d763cef2SBarry Smith    Input Parameter:
4595d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4596d763cef2SBarry Smith 
4597d763cef2SBarry Smith    Output Parameters:
4598e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4599e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4600e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4601e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4602d763cef2SBarry Smith 
460395452b02SPatrick Sanan    Notes:
460495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4605d763cef2SBarry Smith 
4606d763cef2SBarry Smith    Level: intermediate
4607d763cef2SBarry Smith 
460880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4609d763cef2SBarry Smith 
4610d763cef2SBarry Smith @*/
4611e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4612d763cef2SBarry Smith {
4613089b2837SJed Brown   PetscErrorCode ierr;
461424989b8cSPeter Brune   DM             dm;
4615089b2837SJed Brown 
4616d763cef2SBarry Smith   PetscFunctionBegin;
461723a57915SBarry Smith   if (Amat || Pmat) {
461823a57915SBarry Smith     SNES snes;
4619089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
462023a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4621e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
462223a57915SBarry Smith   }
462324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
462424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4625d763cef2SBarry Smith   PetscFunctionReturn(0);
4626d763cef2SBarry Smith }
4627d763cef2SBarry Smith 
46282eca1d9cSJed Brown /*@C
46292eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
46302eca1d9cSJed Brown 
46312eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46322eca1d9cSJed Brown 
46332eca1d9cSJed Brown    Input Parameter:
46342eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46352eca1d9cSJed Brown 
46362eca1d9cSJed Brown    Output Parameters:
4637e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4638e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46392eca1d9cSJed Brown .  f   - The function to compute the matrices
46402eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46412eca1d9cSJed Brown 
464295452b02SPatrick Sanan    Notes:
464395452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
46442eca1d9cSJed Brown 
46452eca1d9cSJed Brown    Level: advanced
46462eca1d9cSJed Brown 
464780275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
46482eca1d9cSJed Brown 
46492eca1d9cSJed Brown @*/
4650e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46512eca1d9cSJed Brown {
4652089b2837SJed Brown   PetscErrorCode ierr;
465324989b8cSPeter Brune   DM             dm;
46540910c330SBarry Smith 
46552eca1d9cSJed Brown   PetscFunctionBegin;
4656c0aab802Sstefano_zampini   if (Amat || Pmat) {
4657c0aab802Sstefano_zampini     SNES snes;
4658089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4659f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4660e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4661c0aab802Sstefano_zampini   }
466224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
466324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46642eca1d9cSJed Brown   PetscFunctionReturn(0);
46652eca1d9cSJed Brown }
46662eca1d9cSJed Brown 
46671713a123SBarry Smith /*@C
466883a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46691713a123SBarry Smith    VecView() for the solution at each timestep
46701713a123SBarry Smith 
46711713a123SBarry Smith    Collective on TS
46721713a123SBarry Smith 
46731713a123SBarry Smith    Input Parameters:
46741713a123SBarry Smith +  ts - the TS context
46751713a123SBarry Smith .  step - current time-step
4676142b95e3SSatish Balay .  ptime - current time
46770298fd71SBarry Smith -  dummy - either a viewer or NULL
46781713a123SBarry Smith 
467999fdda47SBarry Smith    Options Database:
468099fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
468199fdda47SBarry Smith 
468295452b02SPatrick Sanan    Notes:
468395452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
468499fdda47SBarry Smith        will look bad
468599fdda47SBarry Smith 
46861713a123SBarry Smith    Level: intermediate
46871713a123SBarry Smith 
4688a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46891713a123SBarry Smith @*/
46900910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46911713a123SBarry Smith {
4692dfbe8321SBarry Smith   PetscErrorCode   ierr;
469383a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4694473a3ab2SBarry Smith   PetscDraw        draw;
46951713a123SBarry Smith 
46961713a123SBarry Smith   PetscFunctionBegin;
46976083293cSBarry Smith   if (!step && ictx->showinitial) {
46986083293cSBarry Smith     if (!ictx->initialsolution) {
46990910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
47001713a123SBarry Smith     }
47010910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
47026083293cSBarry Smith   }
4703b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47040dcf80beSBarry Smith 
47056083293cSBarry Smith   if (ictx->showinitial) {
47066083293cSBarry Smith     PetscReal pause;
47076083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
47086083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
47096083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
47106083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
47116083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
47126083293cSBarry Smith   }
47130910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4714473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
471551fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4716473a3ab2SBarry Smith     char      time[32];
4717473a3ab2SBarry Smith 
4718473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
471985b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4720473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4721473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
472251fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4723473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4724473a3ab2SBarry Smith   }
4725473a3ab2SBarry Smith 
47266083293cSBarry Smith   if (ictx->showinitial) {
47276083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
47286083293cSBarry Smith   }
47291713a123SBarry Smith   PetscFunctionReturn(0);
47301713a123SBarry Smith }
47311713a123SBarry Smith 
47329110b2e7SHong Zhang /*@C
47332d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
47342d5ee99bSBarry Smith 
47352d5ee99bSBarry Smith    Collective on TS
47362d5ee99bSBarry Smith 
47372d5ee99bSBarry Smith    Input Parameters:
47382d5ee99bSBarry Smith +  ts - the TS context
47392d5ee99bSBarry Smith .  step - current time-step
47402d5ee99bSBarry Smith .  ptime - current time
47412d5ee99bSBarry Smith -  dummy - either a viewer or NULL
47422d5ee99bSBarry Smith 
47432d5ee99bSBarry Smith    Level: intermediate
47442d5ee99bSBarry Smith 
47452d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47462d5ee99bSBarry Smith @*/
47472d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47482d5ee99bSBarry Smith {
47492d5ee99bSBarry Smith   PetscErrorCode    ierr;
47502d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47512d5ee99bSBarry Smith   PetscDraw         draw;
47526934998bSLisandro Dalcin   PetscDrawAxis     axis;
47532d5ee99bSBarry Smith   PetscInt          n;
47542d5ee99bSBarry Smith   PetscMPIInt       size;
47556934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47562d5ee99bSBarry Smith   char              time[32];
47572d5ee99bSBarry Smith   const PetscScalar *U;
47582d5ee99bSBarry Smith 
47592d5ee99bSBarry Smith   PetscFunctionBegin;
47606934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
47616934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47622d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47636934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47642d5ee99bSBarry Smith 
47652d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47666934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47676934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47686934998bSLisandro Dalcin   if (!step) {
47696934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47706934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47716934998bSLisandro Dalcin   }
47722d5ee99bSBarry Smith 
47732d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47746934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47756934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4776a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47776934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47782d5ee99bSBarry Smith 
47796934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47806934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47812d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47824b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
478385b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47842d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
478551fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47862d5ee99bSBarry Smith   }
47876934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47882d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
478956d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47906934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47912d5ee99bSBarry Smith   PetscFunctionReturn(0);
47922d5ee99bSBarry Smith }
47932d5ee99bSBarry Smith 
47946083293cSBarry Smith /*@C
479583a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
47966083293cSBarry Smith 
47976083293cSBarry Smith    Collective on TS
47986083293cSBarry Smith 
47996083293cSBarry Smith    Input Parameters:
48006083293cSBarry Smith .    ctx - the monitor context
48016083293cSBarry Smith 
48026083293cSBarry Smith    Level: intermediate
48036083293cSBarry Smith 
480483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
48056083293cSBarry Smith @*/
480683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
48076083293cSBarry Smith {
48086083293cSBarry Smith   PetscErrorCode ierr;
48096083293cSBarry Smith 
48106083293cSBarry Smith   PetscFunctionBegin;
481183a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
481283a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
481383a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
48146083293cSBarry Smith   PetscFunctionReturn(0);
48156083293cSBarry Smith }
48166083293cSBarry Smith 
48176083293cSBarry Smith /*@C
481883a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
48196083293cSBarry Smith 
48206083293cSBarry Smith    Collective on TS
48216083293cSBarry Smith 
48226083293cSBarry Smith    Input Parameter:
48236083293cSBarry Smith .    ts - time-step context
48246083293cSBarry Smith 
48256083293cSBarry Smith    Output Patameter:
48266083293cSBarry Smith .    ctx - the monitor context
48276083293cSBarry Smith 
482899fdda47SBarry Smith    Options Database:
482999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
483099fdda47SBarry Smith 
48316083293cSBarry Smith    Level: intermediate
48326083293cSBarry Smith 
483383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
48346083293cSBarry Smith @*/
483583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
48366083293cSBarry Smith {
48376083293cSBarry Smith   PetscErrorCode   ierr;
48386083293cSBarry Smith 
48396083293cSBarry Smith   PetscFunctionBegin;
4840b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
484183a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4842e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4843bbd56ea5SKarl Rupp 
484483a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4845473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4846c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4847473a3ab2SBarry Smith 
4848473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4849c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
48506083293cSBarry Smith   PetscFunctionReturn(0);
48516083293cSBarry Smith }
48526083293cSBarry Smith 
48533a471f94SBarry Smith /*@C
48540ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
48550ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
48560ed3bfb6SBarry Smith 
48570ed3bfb6SBarry Smith    Collective on TS
48580ed3bfb6SBarry Smith 
48590ed3bfb6SBarry Smith    Input Parameters:
48600ed3bfb6SBarry Smith +  ts - the TS context
48610ed3bfb6SBarry Smith .  step - current time-step
48620ed3bfb6SBarry Smith .  ptime - current time
48630ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
48640ed3bfb6SBarry Smith 
48650ed3bfb6SBarry Smith    Options Database:
48660ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48670ed3bfb6SBarry Smith 
48680ed3bfb6SBarry Smith    Level: intermediate
48690ed3bfb6SBarry Smith 
48700ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48710ed3bfb6SBarry Smith @*/
48720ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48730ed3bfb6SBarry Smith {
48740ed3bfb6SBarry Smith   PetscErrorCode   ierr;
48750ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
48760ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
48770ed3bfb6SBarry Smith   Vec              work;
48780ed3bfb6SBarry Smith 
48790ed3bfb6SBarry Smith   PetscFunctionBegin;
48800ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48810ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48820ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48830ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48840ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48850ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48860ed3bfb6SBarry Smith }
48870ed3bfb6SBarry Smith 
48880ed3bfb6SBarry Smith /*@C
488983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48903a471f94SBarry Smith    VecView() for the error at each timestep
48913a471f94SBarry Smith 
48923a471f94SBarry Smith    Collective on TS
48933a471f94SBarry Smith 
48943a471f94SBarry Smith    Input Parameters:
48953a471f94SBarry Smith +  ts - the TS context
48963a471f94SBarry Smith .  step - current time-step
48973a471f94SBarry Smith .  ptime - current time
48980298fd71SBarry Smith -  dummy - either a viewer or NULL
48993a471f94SBarry Smith 
49000ed3bfb6SBarry Smith    Options Database:
49010ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
49020ed3bfb6SBarry Smith 
49033a471f94SBarry Smith    Level: intermediate
49043a471f94SBarry Smith 
49050ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
49063a471f94SBarry Smith @*/
49070910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49083a471f94SBarry Smith {
49093a471f94SBarry Smith   PetscErrorCode   ierr;
491083a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
491183a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
49123a471f94SBarry Smith   Vec              work;
49133a471f94SBarry Smith 
49143a471f94SBarry Smith   PetscFunctionBegin;
4915b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49160910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
49173a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
49180910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
49193a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
49203a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
49213a471f94SBarry Smith   PetscFunctionReturn(0);
49223a471f94SBarry Smith }
49233a471f94SBarry Smith 
4924af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49256c699258SBarry Smith /*@
49262a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49276c699258SBarry Smith 
4928d083f849SBarry Smith    Logically Collective on ts
49296c699258SBarry Smith 
49306c699258SBarry Smith    Input Parameters:
49312a808120SBarry Smith +  ts - the ODE integrator object
49322a808120SBarry Smith -  dm - the dm, cannot be NULL
49336c699258SBarry Smith 
4934e03a659cSJed Brown    Notes:
4935e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4936e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4937e03a659cSJed Brown    different problems using the same function space.
4938e03a659cSJed Brown 
49396c699258SBarry Smith    Level: intermediate
49406c699258SBarry Smith 
49416c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49426c699258SBarry Smith @*/
49437087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49446c699258SBarry Smith {
49456c699258SBarry Smith   PetscErrorCode ierr;
4946089b2837SJed Brown   SNES           snes;
4947942e3340SBarry Smith   DMTS           tsdm;
49486c699258SBarry Smith 
49496c699258SBarry Smith   PetscFunctionBegin;
49500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49512a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
495270663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4953942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49542a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4955942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4956942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
495724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
495824989b8cSPeter Brune         tsdm->originaldm = dm;
495924989b8cSPeter Brune       }
496024989b8cSPeter Brune     }
49616bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
496224989b8cSPeter Brune   }
49636c699258SBarry Smith   ts->dm = dm;
4964bbd56ea5SKarl Rupp 
4965089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4966089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49676c699258SBarry Smith   PetscFunctionReturn(0);
49686c699258SBarry Smith }
49696c699258SBarry Smith 
49706c699258SBarry Smith /*@
49716c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49726c699258SBarry Smith 
49733f9fe445SBarry Smith    Not Collective
49746c699258SBarry Smith 
49756c699258SBarry Smith    Input Parameter:
49766c699258SBarry Smith . ts - the preconditioner context
49776c699258SBarry Smith 
49786c699258SBarry Smith    Output Parameter:
49796c699258SBarry Smith .  dm - the dm
49806c699258SBarry Smith 
49816c699258SBarry Smith    Level: intermediate
49826c699258SBarry Smith 
49836c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49846c699258SBarry Smith @*/
49857087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49866c699258SBarry Smith {
4987496e6a7aSJed Brown   PetscErrorCode ierr;
4988496e6a7aSJed Brown 
49896c699258SBarry Smith   PetscFunctionBegin;
49900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4991496e6a7aSJed Brown   if (!ts->dm) {
4992ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4993496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4994496e6a7aSJed Brown   }
49956c699258SBarry Smith   *dm = ts->dm;
49966c699258SBarry Smith   PetscFunctionReturn(0);
49976c699258SBarry Smith }
49981713a123SBarry Smith 
49990f5c6efeSJed Brown /*@
50000f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
50010f5c6efeSJed Brown 
50023f9fe445SBarry Smith    Logically Collective on SNES
50030f5c6efeSJed Brown 
50040f5c6efeSJed Brown    Input Parameter:
5005d42a1c89SJed Brown + snes - nonlinear solver
50060910c330SBarry Smith . U - the current state at which to evaluate the residual
5007d42a1c89SJed Brown - ctx - user context, must be a TS
50080f5c6efeSJed Brown 
50090f5c6efeSJed Brown    Output Parameter:
50100f5c6efeSJed Brown . F - the nonlinear residual
50110f5c6efeSJed Brown 
50120f5c6efeSJed Brown    Notes:
50130f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50140f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
50150f5c6efeSJed Brown 
50160f5c6efeSJed Brown    Level: advanced
50170f5c6efeSJed Brown 
50180f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
50190f5c6efeSJed Brown @*/
50200910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
50210f5c6efeSJed Brown {
50220f5c6efeSJed Brown   TS             ts = (TS)ctx;
50230f5c6efeSJed Brown   PetscErrorCode ierr;
50240f5c6efeSJed Brown 
50250f5c6efeSJed Brown   PetscFunctionBegin;
50260f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50280f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50290f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50300910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50310f5c6efeSJed Brown   PetscFunctionReturn(0);
50320f5c6efeSJed Brown }
50330f5c6efeSJed Brown 
50340f5c6efeSJed Brown /*@
50350f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50360f5c6efeSJed Brown 
50370f5c6efeSJed Brown    Collective on SNES
50380f5c6efeSJed Brown 
50390f5c6efeSJed Brown    Input Parameter:
50400f5c6efeSJed Brown + snes - nonlinear solver
50410910c330SBarry Smith . U - the current state at which to evaluate the residual
50420f5c6efeSJed Brown - ctx - user context, must be a TS
50430f5c6efeSJed Brown 
50440f5c6efeSJed Brown    Output Parameter:
50450f5c6efeSJed Brown + A - the Jacobian
50460f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50470f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50480f5c6efeSJed Brown 
50490f5c6efeSJed Brown    Notes:
50500f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50510f5c6efeSJed Brown 
50520f5c6efeSJed Brown    Level: developer
50530f5c6efeSJed Brown 
50540f5c6efeSJed Brown .seealso: SNESSetJacobian()
50550f5c6efeSJed Brown @*/
5056d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50570f5c6efeSJed Brown {
50580f5c6efeSJed Brown   TS             ts = (TS)ctx;
50590f5c6efeSJed Brown   PetscErrorCode ierr;
50600f5c6efeSJed Brown 
50610f5c6efeSJed Brown   PetscFunctionBegin;
50620f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50630910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50640f5c6efeSJed Brown   PetscValidPointer(A,3);
506594ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50660f5c6efeSJed Brown   PetscValidPointer(B,4);
506794ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50680f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5069d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50700f5c6efeSJed Brown   PetscFunctionReturn(0);
50710f5c6efeSJed Brown }
5072325fc9f4SBarry Smith 
50730e4ef248SJed Brown /*@C
50749ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50750e4ef248SJed Brown 
50760e4ef248SJed Brown    Collective on TS
50770e4ef248SJed Brown 
50780e4ef248SJed Brown    Input Arguments:
50790e4ef248SJed Brown +  ts - time stepping context
50800e4ef248SJed Brown .  t - time at which to evaluate
50810910c330SBarry Smith .  U - state at which to evaluate
50820e4ef248SJed Brown -  ctx - context
50830e4ef248SJed Brown 
50840e4ef248SJed Brown    Output Arguments:
50850e4ef248SJed Brown .  F - right hand side
50860e4ef248SJed Brown 
50870e4ef248SJed Brown    Level: intermediate
50880e4ef248SJed Brown 
50890e4ef248SJed Brown    Notes:
50900e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50910e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50920e4ef248SJed Brown 
50930e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50940e4ef248SJed Brown @*/
50950910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50960e4ef248SJed Brown {
50970e4ef248SJed Brown   PetscErrorCode ierr;
50980e4ef248SJed Brown   Mat            Arhs,Brhs;
50990e4ef248SJed Brown 
51000e4ef248SJed Brown   PetscFunctionBegin;
51010e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5102d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
51030910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
51040e4ef248SJed Brown   PetscFunctionReturn(0);
51050e4ef248SJed Brown }
51060e4ef248SJed Brown 
51070e4ef248SJed Brown /*@C
51080e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
51090e4ef248SJed Brown 
51100e4ef248SJed Brown    Collective on TS
51110e4ef248SJed Brown 
51120e4ef248SJed Brown    Input Arguments:
51130e4ef248SJed Brown +  ts - time stepping context
51140e4ef248SJed Brown .  t - time at which to evaluate
51150910c330SBarry Smith .  U - state at which to evaluate
51160e4ef248SJed Brown -  ctx - context
51170e4ef248SJed Brown 
51180e4ef248SJed Brown    Output Arguments:
51190e4ef248SJed Brown +  A - pointer to operator
51200e4ef248SJed Brown .  B - pointer to preconditioning matrix
51210e4ef248SJed Brown -  flg - matrix structure flag
51220e4ef248SJed Brown 
51230e4ef248SJed Brown    Level: intermediate
51240e4ef248SJed Brown 
51250e4ef248SJed Brown    Notes:
51260e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51270e4ef248SJed Brown 
51280e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51290e4ef248SJed Brown @*/
5130d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51310e4ef248SJed Brown {
51320e4ef248SJed Brown   PetscFunctionBegin;
51330e4ef248SJed Brown   PetscFunctionReturn(0);
51340e4ef248SJed Brown }
51350e4ef248SJed Brown 
51360026cea9SSean Farley /*@C
51370026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51380026cea9SSean Farley 
51390026cea9SSean Farley    Collective on TS
51400026cea9SSean Farley 
51410026cea9SSean Farley    Input Arguments:
51420026cea9SSean Farley +  ts - time stepping context
51430026cea9SSean Farley .  t - time at which to evaluate
51440910c330SBarry Smith .  U - state at which to evaluate
51450910c330SBarry Smith .  Udot - time derivative of state vector
51460026cea9SSean Farley -  ctx - context
51470026cea9SSean Farley 
51480026cea9SSean Farley    Output Arguments:
51490026cea9SSean Farley .  F - left hand side
51500026cea9SSean Farley 
51510026cea9SSean Farley    Level: intermediate
51520026cea9SSean Farley 
51530026cea9SSean Farley    Notes:
51540910c330SBarry 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
51550026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51560026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51570026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51580026cea9SSean Farley 
51599ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51609ae8fd06SBarry Smith 
51619ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51620026cea9SSean Farley @*/
51630910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51640026cea9SSean Farley {
51650026cea9SSean Farley   PetscErrorCode ierr;
51660026cea9SSean Farley   Mat            A,B;
51670026cea9SSean Farley 
51680026cea9SSean Farley   PetscFunctionBegin;
51690298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5170d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51710910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51720026cea9SSean Farley   PetscFunctionReturn(0);
51730026cea9SSean Farley }
51740026cea9SSean Farley 
51750026cea9SSean Farley /*@C
5176b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51770026cea9SSean Farley 
51780026cea9SSean Farley    Collective on TS
51790026cea9SSean Farley 
51800026cea9SSean Farley    Input Arguments:
51810026cea9SSean Farley +  ts - time stepping context
51820026cea9SSean Farley .  t - time at which to evaluate
51830910c330SBarry Smith .  U - state at which to evaluate
51840910c330SBarry Smith .  Udot - time derivative of state vector
51850026cea9SSean Farley .  shift - shift to apply
51860026cea9SSean Farley -  ctx - context
51870026cea9SSean Farley 
51880026cea9SSean Farley    Output Arguments:
51890026cea9SSean Farley +  A - pointer to operator
51900026cea9SSean Farley .  B - pointer to preconditioning matrix
51910026cea9SSean Farley -  flg - matrix structure flag
51920026cea9SSean Farley 
5193b41af12eSJed Brown    Level: advanced
51940026cea9SSean Farley 
51950026cea9SSean Farley    Notes:
51960026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51970026cea9SSean Farley 
5198b41af12eSJed Brown    It is only appropriate for problems of the form
5199b41af12eSJed Brown 
5200b41af12eSJed Brown $     M Udot = F(U,t)
5201b41af12eSJed Brown 
5202b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5203b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5204b41af12eSJed Brown   an implicit operator of the form
5205b41af12eSJed Brown 
5206b41af12eSJed Brown $    shift*M + J
5207b41af12eSJed Brown 
5208b41af12eSJed 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
5209b41af12eSJed Brown   a copy of M or reassemble it when requested.
5210b41af12eSJed Brown 
52110026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
52120026cea9SSean Farley @*/
5213d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
52140026cea9SSean Farley {
5215b41af12eSJed Brown   PetscErrorCode ierr;
5216b41af12eSJed Brown 
52170026cea9SSean Farley   PetscFunctionBegin;
521894ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5219b41af12eSJed Brown   ts->ijacobian.shift = shift;
52200026cea9SSean Farley   PetscFunctionReturn(0);
52210026cea9SSean Farley }
5222b41af12eSJed Brown 
5223e817cc15SEmil Constantinescu /*@
5224e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5225e817cc15SEmil Constantinescu 
5226e817cc15SEmil Constantinescu    Not Collective
5227e817cc15SEmil Constantinescu 
5228e817cc15SEmil Constantinescu    Input Parameter:
5229e817cc15SEmil Constantinescu .  ts - the TS context
5230e817cc15SEmil Constantinescu 
5231e817cc15SEmil Constantinescu    Output Parameter:
52324e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5233e817cc15SEmil Constantinescu 
5234e817cc15SEmil Constantinescu    Level: beginner
5235e817cc15SEmil Constantinescu 
5236e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5237e817cc15SEmil Constantinescu @*/
5238e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5239e817cc15SEmil Constantinescu {
5240e817cc15SEmil Constantinescu   PetscFunctionBegin;
5241e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5242e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5243e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5244e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5245e817cc15SEmil Constantinescu }
5246e817cc15SEmil Constantinescu 
5247e817cc15SEmil Constantinescu /*@
5248e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5249e817cc15SEmil Constantinescu 
5250e817cc15SEmil Constantinescu    Not Collective
5251e817cc15SEmil Constantinescu 
5252e817cc15SEmil Constantinescu    Input Parameter:
5253e817cc15SEmil Constantinescu +  ts - the TS context
52541297b224SEmil Constantinescu -  equation_type - see TSEquationType
5255e817cc15SEmil Constantinescu 
5256e817cc15SEmil Constantinescu    Level: advanced
5257e817cc15SEmil Constantinescu 
5258e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5259e817cc15SEmil Constantinescu @*/
5260e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5261e817cc15SEmil Constantinescu {
5262e817cc15SEmil Constantinescu   PetscFunctionBegin;
5263e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5264e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5265e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5266e817cc15SEmil Constantinescu }
52670026cea9SSean Farley 
52684af1b03aSJed Brown /*@
52694af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52704af1b03aSJed Brown 
52714af1b03aSJed Brown    Not Collective
52724af1b03aSJed Brown 
52734af1b03aSJed Brown    Input Parameter:
52744af1b03aSJed Brown .  ts - the TS context
52754af1b03aSJed Brown 
52764af1b03aSJed Brown    Output Parameter:
52774af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52784af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52794af1b03aSJed Brown 
5280487e0bb9SJed Brown    Level: beginner
52814af1b03aSJed Brown 
5282cd652676SJed Brown    Notes:
5283cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52844af1b03aSJed Brown 
52854af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52864af1b03aSJed Brown @*/
52874af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52884af1b03aSJed Brown {
52894af1b03aSJed Brown   PetscFunctionBegin;
52904af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52914af1b03aSJed Brown   PetscValidPointer(reason,2);
52924af1b03aSJed Brown   *reason = ts->reason;
52934af1b03aSJed Brown   PetscFunctionReturn(0);
52944af1b03aSJed Brown }
52954af1b03aSJed Brown 
5296d6ad946cSShri Abhyankar /*@
5297d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5298d6ad946cSShri Abhyankar 
52996b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5300d6ad946cSShri Abhyankar 
53016b221cbeSPatrick Sanan    Input Parameters:
5302d6ad946cSShri Abhyankar +  ts - the TS context
53036b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5304d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5305d6ad946cSShri Abhyankar 
5306f5abba47SShri Abhyankar    Level: advanced
5307d6ad946cSShri Abhyankar 
5308d6ad946cSShri Abhyankar    Notes:
53096b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5310d6ad946cSShri Abhyankar 
5311d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5312d6ad946cSShri Abhyankar @*/
5313d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5314d6ad946cSShri Abhyankar {
5315d6ad946cSShri Abhyankar   PetscFunctionBegin;
5316d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5317d6ad946cSShri Abhyankar   ts->reason = reason;
5318d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5319d6ad946cSShri Abhyankar }
5320d6ad946cSShri Abhyankar 
5321cc708dedSBarry Smith /*@
5322cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5323cc708dedSBarry Smith 
5324cc708dedSBarry Smith    Not Collective
5325cc708dedSBarry Smith 
5326cc708dedSBarry Smith    Input Parameter:
5327cc708dedSBarry Smith .  ts - the TS context
5328cc708dedSBarry Smith 
5329cc708dedSBarry Smith    Output Parameter:
533019eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5331cc708dedSBarry Smith 
5332487e0bb9SJed Brown    Level: beginner
5333cc708dedSBarry Smith 
5334cc708dedSBarry Smith    Notes:
5335cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5336cc708dedSBarry Smith 
5337cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5338cc708dedSBarry Smith @*/
5339cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5340cc708dedSBarry Smith {
5341cc708dedSBarry Smith   PetscFunctionBegin;
5342cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5343cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5344cc708dedSBarry Smith   *ftime = ts->solvetime;
5345cc708dedSBarry Smith   PetscFunctionReturn(0);
5346cc708dedSBarry Smith }
5347cc708dedSBarry Smith 
53482c18e0fdSBarry Smith /*@
53495ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53509f67acb7SJed Brown    used by the time integrator.
53519f67acb7SJed Brown 
53529f67acb7SJed Brown    Not Collective
53539f67acb7SJed Brown 
53549f67acb7SJed Brown    Input Parameter:
53559f67acb7SJed Brown .  ts - TS context
53569f67acb7SJed Brown 
53579f67acb7SJed Brown    Output Parameter:
53589f67acb7SJed Brown .  nits - number of nonlinear iterations
53599f67acb7SJed Brown 
53609f67acb7SJed Brown    Notes:
53619f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53629f67acb7SJed Brown 
53639f67acb7SJed Brown    Level: intermediate
53649f67acb7SJed Brown 
53655ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53669f67acb7SJed Brown @*/
53675ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53689f67acb7SJed Brown {
53699f67acb7SJed Brown   PetscFunctionBegin;
53709f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53719f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53725ef26d82SJed Brown   *nits = ts->snes_its;
53739f67acb7SJed Brown   PetscFunctionReturn(0);
53749f67acb7SJed Brown }
53759f67acb7SJed Brown 
53769f67acb7SJed Brown /*@
53775ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53789f67acb7SJed Brown    used by the time integrator.
53799f67acb7SJed Brown 
53809f67acb7SJed Brown    Not Collective
53819f67acb7SJed Brown 
53829f67acb7SJed Brown    Input Parameter:
53839f67acb7SJed Brown .  ts - TS context
53849f67acb7SJed Brown 
53859f67acb7SJed Brown    Output Parameter:
53869f67acb7SJed Brown .  lits - number of linear iterations
53879f67acb7SJed Brown 
53889f67acb7SJed Brown    Notes:
53899f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53909f67acb7SJed Brown 
53919f67acb7SJed Brown    Level: intermediate
53929f67acb7SJed Brown 
53935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
53949f67acb7SJed Brown @*/
53955ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
53969f67acb7SJed Brown {
53979f67acb7SJed Brown   PetscFunctionBegin;
53989f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53999f67acb7SJed Brown   PetscValidIntPointer(lits,2);
54005ef26d82SJed Brown   *lits = ts->ksp_its;
54019f67acb7SJed Brown   PetscFunctionReturn(0);
54029f67acb7SJed Brown }
54039f67acb7SJed Brown 
5404cef5090cSJed Brown /*@
5405cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5406cef5090cSJed Brown 
5407cef5090cSJed Brown    Not Collective
5408cef5090cSJed Brown 
5409cef5090cSJed Brown    Input Parameter:
5410cef5090cSJed Brown .  ts - TS context
5411cef5090cSJed Brown 
5412cef5090cSJed Brown    Output Parameter:
5413cef5090cSJed Brown .  rejects - number of steps rejected
5414cef5090cSJed Brown 
5415cef5090cSJed Brown    Notes:
5416cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5417cef5090cSJed Brown 
5418cef5090cSJed Brown    Level: intermediate
5419cef5090cSJed Brown 
54205ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5421cef5090cSJed Brown @*/
5422cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5423cef5090cSJed Brown {
5424cef5090cSJed Brown   PetscFunctionBegin;
5425cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5426cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5427cef5090cSJed Brown   *rejects = ts->reject;
5428cef5090cSJed Brown   PetscFunctionReturn(0);
5429cef5090cSJed Brown }
5430cef5090cSJed Brown 
5431cef5090cSJed Brown /*@
5432cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5433cef5090cSJed Brown 
5434cef5090cSJed Brown    Not Collective
5435cef5090cSJed Brown 
5436cef5090cSJed Brown    Input Parameter:
5437cef5090cSJed Brown .  ts - TS context
5438cef5090cSJed Brown 
5439cef5090cSJed Brown    Output Parameter:
5440cef5090cSJed Brown .  fails - number of failed nonlinear solves
5441cef5090cSJed Brown 
5442cef5090cSJed Brown    Notes:
5443cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5444cef5090cSJed Brown 
5445cef5090cSJed Brown    Level: intermediate
5446cef5090cSJed Brown 
54475ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5448cef5090cSJed Brown @*/
5449cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5450cef5090cSJed Brown {
5451cef5090cSJed Brown   PetscFunctionBegin;
5452cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5453cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5454cef5090cSJed Brown   *fails = ts->num_snes_failures;
5455cef5090cSJed Brown   PetscFunctionReturn(0);
5456cef5090cSJed Brown }
5457cef5090cSJed Brown 
5458cef5090cSJed Brown /*@
5459cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5460cef5090cSJed Brown 
5461cef5090cSJed Brown    Not Collective
5462cef5090cSJed Brown 
5463cef5090cSJed Brown    Input Parameter:
5464cef5090cSJed Brown +  ts - TS context
5465cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5466cef5090cSJed Brown 
5467cef5090cSJed Brown    Notes:
5468cef5090cSJed Brown    The counter is reset to zero for each step
5469cef5090cSJed Brown 
5470cef5090cSJed Brown    Options Database Key:
5471cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5472cef5090cSJed Brown 
5473cef5090cSJed Brown    Level: intermediate
5474cef5090cSJed Brown 
54755ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5476cef5090cSJed Brown @*/
5477cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5478cef5090cSJed Brown {
5479cef5090cSJed Brown   PetscFunctionBegin;
5480cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5481cef5090cSJed Brown   ts->max_reject = rejects;
5482cef5090cSJed Brown   PetscFunctionReturn(0);
5483cef5090cSJed Brown }
5484cef5090cSJed Brown 
5485cef5090cSJed Brown /*@
5486cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5487cef5090cSJed Brown 
5488cef5090cSJed Brown    Not Collective
5489cef5090cSJed Brown 
5490cef5090cSJed Brown    Input Parameter:
5491cef5090cSJed Brown +  ts - TS context
5492cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5493cef5090cSJed Brown 
5494cef5090cSJed Brown    Notes:
5495cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5496cef5090cSJed Brown 
5497cef5090cSJed Brown    Options Database Key:
5498cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5499cef5090cSJed Brown 
5500cef5090cSJed Brown    Level: intermediate
5501cef5090cSJed Brown 
55025ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5503cef5090cSJed Brown @*/
5504cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5505cef5090cSJed Brown {
5506cef5090cSJed Brown   PetscFunctionBegin;
5507cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5508cef5090cSJed Brown   ts->max_snes_failures = fails;
5509cef5090cSJed Brown   PetscFunctionReturn(0);
5510cef5090cSJed Brown }
5511cef5090cSJed Brown 
5512cef5090cSJed Brown /*@
5513cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5514cef5090cSJed Brown 
5515cef5090cSJed Brown    Not Collective
5516cef5090cSJed Brown 
5517cef5090cSJed Brown    Input Parameter:
5518cef5090cSJed Brown +  ts - TS context
5519cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5520cef5090cSJed Brown 
5521cef5090cSJed Brown    Options Database Key:
5522cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5523cef5090cSJed Brown 
5524cef5090cSJed Brown    Level: intermediate
5525cef5090cSJed Brown 
55265ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5527cef5090cSJed Brown @*/
5528cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5529cef5090cSJed Brown {
5530cef5090cSJed Brown   PetscFunctionBegin;
5531cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5532cef5090cSJed Brown   ts->errorifstepfailed = err;
5533cef5090cSJed Brown   PetscFunctionReturn(0);
5534cef5090cSJed Brown }
5535cef5090cSJed Brown 
5536fb1732b5SBarry Smith /*@C
5537fde5950dSBarry 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
5538fb1732b5SBarry Smith 
5539fb1732b5SBarry Smith    Collective on TS
5540fb1732b5SBarry Smith 
5541fb1732b5SBarry Smith    Input Parameters:
5542fb1732b5SBarry Smith +  ts - the TS context
5543fb1732b5SBarry Smith .  step - current time-step
5544fb1732b5SBarry Smith .  ptime - current time
55450910c330SBarry Smith .  u - current state
5546721cd6eeSBarry Smith -  vf - viewer and its format
5547fb1732b5SBarry Smith 
5548fb1732b5SBarry Smith    Level: intermediate
5549fb1732b5SBarry Smith 
5550fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5551fb1732b5SBarry Smith @*/
5552721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5553fb1732b5SBarry Smith {
5554fb1732b5SBarry Smith   PetscErrorCode ierr;
5555fb1732b5SBarry Smith 
5556fb1732b5SBarry Smith   PetscFunctionBegin;
5557721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5558721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5559721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5560ed81e22dSJed Brown   PetscFunctionReturn(0);
5561ed81e22dSJed Brown }
5562ed81e22dSJed Brown 
5563ed81e22dSJed Brown /*@C
5564ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5565ed81e22dSJed Brown 
5566ed81e22dSJed Brown    Collective on TS
5567ed81e22dSJed Brown 
5568ed81e22dSJed Brown    Input Parameters:
5569ed81e22dSJed Brown +  ts - the TS context
5570ed81e22dSJed Brown .  step - current time-step
5571ed81e22dSJed Brown .  ptime - current time
55720910c330SBarry Smith .  u - current state
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    Notes:
5578ed81e22dSJed 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.
5579ed81e22dSJed Brown    These are named according to the file name template.
5580ed81e22dSJed Brown 
5581ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5582ed81e22dSJed Brown 
5583ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5584ed81e22dSJed Brown @*/
55850910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5586ed81e22dSJed Brown {
5587ed81e22dSJed Brown   PetscErrorCode ierr;
5588ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5589ed81e22dSJed Brown   PetscViewer    viewer;
5590ed81e22dSJed Brown 
5591ed81e22dSJed Brown   PetscFunctionBegin;
559263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55938caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5594ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
55950910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5596ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5597ed81e22dSJed Brown   PetscFunctionReturn(0);
5598ed81e22dSJed Brown }
5599ed81e22dSJed Brown 
5600ed81e22dSJed Brown /*@C
5601ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5602ed81e22dSJed Brown 
5603ed81e22dSJed Brown    Collective on TS
5604ed81e22dSJed Brown 
5605ed81e22dSJed Brown    Input Parameters:
5606ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5607ed81e22dSJed Brown 
5608ed81e22dSJed Brown    Level: intermediate
5609ed81e22dSJed Brown 
5610ed81e22dSJed Brown    Note:
5611ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5612ed81e22dSJed Brown 
5613ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5614ed81e22dSJed Brown @*/
5615ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5616ed81e22dSJed Brown {
5617ed81e22dSJed Brown   PetscErrorCode ierr;
5618ed81e22dSJed Brown 
5619ed81e22dSJed Brown   PetscFunctionBegin;
5620ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5621fb1732b5SBarry Smith   PetscFunctionReturn(0);
5622fb1732b5SBarry Smith }
5623fb1732b5SBarry Smith 
562484df9cb4SJed Brown /*@
5625552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
562684df9cb4SJed Brown 
5627ed81e22dSJed Brown    Collective on TS if controller has not been created yet
562884df9cb4SJed Brown 
562984df9cb4SJed Brown    Input Arguments:
5630ed81e22dSJed Brown .  ts - time stepping context
563184df9cb4SJed Brown 
563284df9cb4SJed Brown    Output Arguments:
5633ed81e22dSJed Brown .  adapt - adaptive controller
563484df9cb4SJed Brown 
563584df9cb4SJed Brown    Level: intermediate
563684df9cb4SJed Brown 
5637ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
563884df9cb4SJed Brown @*/
5639552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
564084df9cb4SJed Brown {
564184df9cb4SJed Brown   PetscErrorCode ierr;
564284df9cb4SJed Brown 
564384df9cb4SJed Brown   PetscFunctionBegin;
564484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5645bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
564684df9cb4SJed Brown   if (!ts->adapt) {
5647ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56483bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56491c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
565084df9cb4SJed Brown   }
5651bec58848SLisandro Dalcin   *adapt = ts->adapt;
565284df9cb4SJed Brown   PetscFunctionReturn(0);
565384df9cb4SJed Brown }
5654d6ebe24aSShri Abhyankar 
56551c3436cfSJed Brown /*@
56561c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
56571c3436cfSJed Brown 
56581c3436cfSJed Brown    Logically Collective
56591c3436cfSJed Brown 
56601c3436cfSJed Brown    Input Arguments:
56611c3436cfSJed Brown +  ts - time integration context
56621c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56630298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56641c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56650298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
56661c3436cfSJed Brown 
5667a3cdaa26SBarry Smith    Options Database keys:
5668a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5669a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5670a3cdaa26SBarry Smith 
56713ff766beSShri Abhyankar    Notes:
56723ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
56733ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
56743ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
56753ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
56763ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
56773ff766beSShri Abhyankar    differential variables.
56783ff766beSShri Abhyankar 
56791c3436cfSJed Brown    Level: beginner
56801c3436cfSJed Brown 
56815c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
56821c3436cfSJed Brown @*/
56831c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56841c3436cfSJed Brown {
56851c3436cfSJed Brown   PetscErrorCode ierr;
56861c3436cfSJed Brown 
56871c3436cfSJed Brown   PetscFunctionBegin;
5688c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56891c3436cfSJed Brown   if (vatol) {
56901c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56911c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56921c3436cfSJed Brown     ts->vatol = vatol;
56931c3436cfSJed Brown   }
5694c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
56951c3436cfSJed Brown   if (vrtol) {
56961c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
56971c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
56981c3436cfSJed Brown     ts->vrtol = vrtol;
56991c3436cfSJed Brown   }
57001c3436cfSJed Brown   PetscFunctionReturn(0);
57011c3436cfSJed Brown }
57021c3436cfSJed Brown 
5703c5033834SJed Brown /*@
5704c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5705c5033834SJed Brown 
5706c5033834SJed Brown    Logically Collective
5707c5033834SJed Brown 
5708c5033834SJed Brown    Input Arguments:
5709c5033834SJed Brown .  ts - time integration context
5710c5033834SJed Brown 
5711c5033834SJed Brown    Output Arguments:
57120298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
57130298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
57140298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
57150298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5716c5033834SJed Brown 
5717c5033834SJed Brown    Level: beginner
5718c5033834SJed Brown 
57195c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5720c5033834SJed Brown @*/
5721c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5722c5033834SJed Brown {
5723c5033834SJed Brown   PetscFunctionBegin;
5724c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5725c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5726c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5727c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5728c5033834SJed Brown   PetscFunctionReturn(0);
5729c5033834SJed Brown }
5730c5033834SJed Brown 
57319c6b16b5SShri Abhyankar /*@
5732a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57339c6b16b5SShri Abhyankar 
57349c6b16b5SShri Abhyankar    Collective on TS
57359c6b16b5SShri Abhyankar 
57369c6b16b5SShri Abhyankar    Input Arguments:
57379c6b16b5SShri Abhyankar +  ts - time stepping context
5738a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5739a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57409c6b16b5SShri Abhyankar 
57419c6b16b5SShri Abhyankar    Output Arguments:
5742a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57437453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5744a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57459c6b16b5SShri Abhyankar 
57469c6b16b5SShri Abhyankar    Level: developer
57479c6b16b5SShri Abhyankar 
5748deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57499c6b16b5SShri Abhyankar @*/
57507453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57519c6b16b5SShri Abhyankar {
57529c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57539c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57547453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57557453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57569c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57577453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
57587453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
57597453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57609c6b16b5SShri Abhyankar 
57619c6b16b5SShri Abhyankar   PetscFunctionBegin;
57629c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5763a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5764a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5765a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5766a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5767a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5768a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57698a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57708a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5771a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57729c6b16b5SShri Abhyankar 
57739c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57749c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57759c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57769c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57779c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57787453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
57797453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
57807453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
57819c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57829c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57839c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57849c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57859c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
578676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57877453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57887453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57897453f775SEmil Constantinescu       if (tola>0.){
57907453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57917453f775SEmil Constantinescu         na_loc++;
57927453f775SEmil Constantinescu       }
57937453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57947453f775SEmil Constantinescu       if (tolr>0.){
57957453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57967453f775SEmil Constantinescu         nr_loc++;
57977453f775SEmil Constantinescu       }
57987453f775SEmil Constantinescu       tol=tola+tolr;
57997453f775SEmil Constantinescu       if (tol>0.){
58007453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58017453f775SEmil Constantinescu         n_loc++;
58027453f775SEmil Constantinescu       }
58039c6b16b5SShri Abhyankar     }
58049c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58059c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58069c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58079c6b16b5SShri Abhyankar     const PetscScalar *atol;
58089c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58099c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
581076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58117453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58127453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
58137453f775SEmil Constantinescu       if (tola>0.){
58147453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58157453f775SEmil Constantinescu         na_loc++;
58167453f775SEmil Constantinescu       }
58177453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58187453f775SEmil Constantinescu       if (tolr>0.){
58197453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58207453f775SEmil Constantinescu         nr_loc++;
58217453f775SEmil Constantinescu       }
58227453f775SEmil Constantinescu       tol=tola+tolr;
58237453f775SEmil Constantinescu       if (tol>0.){
58247453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58257453f775SEmil Constantinescu         n_loc++;
58267453f775SEmil Constantinescu       }
58279c6b16b5SShri Abhyankar     }
58289c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58299c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58309c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58319c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58329c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
583376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58347453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58357453f775SEmil Constantinescu       tola = ts->atol;
58367453f775SEmil Constantinescu       if (tola>0.){
58377453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58387453f775SEmil Constantinescu         na_loc++;
58397453f775SEmil Constantinescu       }
58407453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58417453f775SEmil Constantinescu       if (tolr>0.){
58427453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58437453f775SEmil Constantinescu         nr_loc++;
58447453f775SEmil Constantinescu       }
58457453f775SEmil Constantinescu       tol=tola+tolr;
58467453f775SEmil Constantinescu       if (tol>0.){
58477453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58487453f775SEmil Constantinescu         n_loc++;
58497453f775SEmil Constantinescu       }
58509c6b16b5SShri Abhyankar     }
58519c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58529c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58539c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
585476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58557453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58567453f775SEmil Constantinescu       tola = ts->atol;
58577453f775SEmil Constantinescu       if (tola>0.){
58587453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58597453f775SEmil Constantinescu         na_loc++;
58607453f775SEmil Constantinescu       }
58617453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58627453f775SEmil Constantinescu       if (tolr>0.){
58637453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58647453f775SEmil Constantinescu         nr_loc++;
58657453f775SEmil Constantinescu       }
58667453f775SEmil Constantinescu       tol=tola+tolr;
58677453f775SEmil Constantinescu       if (tol>0.){
58687453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58697453f775SEmil Constantinescu         n_loc++;
58707453f775SEmil Constantinescu       }
58719c6b16b5SShri Abhyankar     }
58729c6b16b5SShri Abhyankar   }
58739c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58749c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58759c6b16b5SShri Abhyankar 
58767453f775SEmil Constantinescu   err_loc[0] = sum;
58777453f775SEmil Constantinescu   err_loc[1] = suma;
58787453f775SEmil Constantinescu   err_loc[2] = sumr;
58797453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58807453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58817453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58827453f775SEmil Constantinescu 
5883a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58847453f775SEmil Constantinescu 
58857453f775SEmil Constantinescu   gsum   = err_glb[0];
58867453f775SEmil Constantinescu   gsuma  = err_glb[1];
58877453f775SEmil Constantinescu   gsumr  = err_glb[2];
58887453f775SEmil Constantinescu   n_glb  = err_glb[3];
58897453f775SEmil Constantinescu   na_glb = err_glb[4];
58907453f775SEmil Constantinescu   nr_glb = err_glb[5];
58917453f775SEmil Constantinescu 
5892b1316ef9SEmil Constantinescu   *norm  = 0.;
5893b1316ef9SEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
5894b1316ef9SEmil Constantinescu   *norma = 0.;
5895b1316ef9SEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5896b1316ef9SEmil Constantinescu   *normr = 0.;
5897b1316ef9SEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58989c6b16b5SShri Abhyankar 
58999c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59007453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59017453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59029c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
59039c6b16b5SShri Abhyankar }
59049c6b16b5SShri Abhyankar 
59059c6b16b5SShri Abhyankar /*@
5906a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
59079c6b16b5SShri Abhyankar 
59089c6b16b5SShri Abhyankar    Collective on TS
59099c6b16b5SShri Abhyankar 
59109c6b16b5SShri Abhyankar    Input Arguments:
59119c6b16b5SShri Abhyankar +  ts - time stepping context
5912a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5913a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
59149c6b16b5SShri Abhyankar 
59159c6b16b5SShri Abhyankar    Output Arguments:
5916a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59177453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5918a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59199c6b16b5SShri Abhyankar 
59209c6b16b5SShri Abhyankar    Level: developer
59219c6b16b5SShri Abhyankar 
5922deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
59239c6b16b5SShri Abhyankar @*/
59247453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59259c6b16b5SShri Abhyankar {
59269c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
59277453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
59289c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
59297453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
59307453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
59317453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59329c6b16b5SShri Abhyankar 
59339c6b16b5SShri Abhyankar   PetscFunctionBegin;
59349c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5935a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5936a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5937a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5938a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5939a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5940a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59418a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59428a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5943a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59449c6b16b5SShri Abhyankar 
59459c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59469c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59479c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59489c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59499c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59507453f775SEmil Constantinescu 
59517453f775SEmil Constantinescu   max=0.;
59527453f775SEmil Constantinescu   maxa=0.;
59537453f775SEmil Constantinescu   maxr=0.;
59547453f775SEmil Constantinescu 
59557453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59569c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59579c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59589c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59597453f775SEmil Constantinescu 
59607453f775SEmil Constantinescu     for (i=0; i<n; i++) {
596176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59627453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59637453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59647453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59657453f775SEmil Constantinescu       tol  = tola+tolr;
59667453f775SEmil Constantinescu       if (tola>0.){
59677453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59687453f775SEmil Constantinescu       }
59697453f775SEmil Constantinescu       if (tolr>0.){
59707453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59717453f775SEmil Constantinescu       }
59727453f775SEmil Constantinescu       if (tol>0.){
59737453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59747453f775SEmil Constantinescu       }
59759c6b16b5SShri Abhyankar     }
59769c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59779c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59789c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59799c6b16b5SShri Abhyankar     const PetscScalar *atol;
59809c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59817453f775SEmil Constantinescu     for (i=0; i<n; i++) {
598276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59837453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59847453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59857453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59867453f775SEmil Constantinescu       tol  = tola+tolr;
59877453f775SEmil Constantinescu       if (tola>0.){
59887453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59897453f775SEmil Constantinescu       }
59907453f775SEmil Constantinescu       if (tolr>0.){
59917453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59927453f775SEmil Constantinescu       }
59937453f775SEmil Constantinescu       if (tol>0.){
59947453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59957453f775SEmil Constantinescu       }
59969c6b16b5SShri Abhyankar     }
59979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59989c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59999c6b16b5SShri Abhyankar     const PetscScalar *rtol;
60009c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60017453f775SEmil Constantinescu 
60027453f775SEmil Constantinescu     for (i=0; i<n; i++) {
600376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60047453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60057453f775SEmil Constantinescu       tola = ts->atol;
60067453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60077453f775SEmil Constantinescu       tol  = tola+tolr;
60087453f775SEmil Constantinescu       if (tola>0.){
60097453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60107453f775SEmil Constantinescu       }
60117453f775SEmil Constantinescu       if (tolr>0.){
60127453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60137453f775SEmil Constantinescu       }
60147453f775SEmil Constantinescu       if (tol>0.){
60157453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60167453f775SEmil Constantinescu       }
60179c6b16b5SShri Abhyankar     }
60189c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60199c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
60207453f775SEmil Constantinescu 
60217453f775SEmil Constantinescu     for (i=0; i<n; i++) {
602276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60237453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60247453f775SEmil Constantinescu       tola = ts->atol;
60257453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60267453f775SEmil Constantinescu       tol  = tola+tolr;
60277453f775SEmil Constantinescu       if (tola>0.){
60287453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60297453f775SEmil Constantinescu       }
60307453f775SEmil Constantinescu       if (tolr>0.){
60317453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60327453f775SEmil Constantinescu       }
60337453f775SEmil Constantinescu       if (tol>0.){
60347453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60357453f775SEmil Constantinescu       }
60369c6b16b5SShri Abhyankar     }
60379c6b16b5SShri Abhyankar   }
60389c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60399c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60407453f775SEmil Constantinescu   err_loc[0] = max;
60417453f775SEmil Constantinescu   err_loc[1] = maxa;
60427453f775SEmil Constantinescu   err_loc[2] = maxr;
6043a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60447453f775SEmil Constantinescu   gmax   = err_glb[0];
60457453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60467453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60479c6b16b5SShri Abhyankar 
60489c6b16b5SShri Abhyankar   *norm = gmax;
60497453f775SEmil Constantinescu   *norma = gmaxa;
60507453f775SEmil Constantinescu   *normr = gmaxr;
60519c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60527453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60537453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60549c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60559c6b16b5SShri Abhyankar }
60569c6b16b5SShri Abhyankar 
60571c3436cfSJed Brown /*@
60588a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60591c3436cfSJed Brown 
60601c3436cfSJed Brown    Collective on TS
60611c3436cfSJed Brown 
60621c3436cfSJed Brown    Input Arguments:
60631c3436cfSJed Brown +  ts - time stepping context
6064a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6065a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6066a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60677619abb3SShri 
60681c3436cfSJed Brown    Output Arguments:
6069a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60708a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6071a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6072a4868fbcSLisandro Dalcin 
6073a4868fbcSLisandro Dalcin    Options Database Keys:
6074a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6075a4868fbcSLisandro Dalcin 
60761c3436cfSJed Brown    Level: developer
60771c3436cfSJed Brown 
60788a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
60791c3436cfSJed Brown @*/
60807453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60811c3436cfSJed Brown {
60828beabaa1SBarry Smith   PetscErrorCode ierr;
60831c3436cfSJed Brown 
60841c3436cfSJed Brown   PetscFunctionBegin;
6085a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60867453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6087a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
60887453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6089a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60901c3436cfSJed Brown   PetscFunctionReturn(0);
60911c3436cfSJed Brown }
60921c3436cfSJed Brown 
60938a175baeSEmil Constantinescu 
60948a175baeSEmil Constantinescu /*@
60958a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
60968a175baeSEmil Constantinescu 
60978a175baeSEmil Constantinescu    Collective on TS
60988a175baeSEmil Constantinescu 
60998a175baeSEmil Constantinescu    Input Arguments:
61008a175baeSEmil Constantinescu +  ts - time stepping context
61018a175baeSEmil Constantinescu .  E - error vector
61028a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61038a175baeSEmil Constantinescu -  Y - state vector, previous time step
61048a175baeSEmil Constantinescu 
61058a175baeSEmil Constantinescu    Output Arguments:
6106a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61078a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6108a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61098a175baeSEmil Constantinescu 
61108a175baeSEmil Constantinescu    Level: developer
61118a175baeSEmil Constantinescu 
61128a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
61138a175baeSEmil Constantinescu @*/
61148a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61158a175baeSEmil Constantinescu {
61168a175baeSEmil Constantinescu   PetscErrorCode    ierr;
61178a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
61188a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
61198a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
61208a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
61218a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
61228a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
61238a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
61248a175baeSEmil Constantinescu 
61258a175baeSEmil Constantinescu   PetscFunctionBegin;
61268a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61278a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
61288a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
61298a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61308a175baeSEmil Constantinescu   PetscValidType(E,2);
61318a175baeSEmil Constantinescu   PetscValidType(U,3);
61328a175baeSEmil Constantinescu   PetscValidType(Y,4);
61338a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61348a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61358a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61368a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61378a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61388a175baeSEmil Constantinescu 
61398a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61408a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61418a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61428a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61438a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61448a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61458a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61468a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61478a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61488a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61498a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61508a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61518a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61528a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
615376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61548a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61558a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61568a175baeSEmil Constantinescu       if (tola>0.){
61578a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61588a175baeSEmil Constantinescu         na_loc++;
61598a175baeSEmil Constantinescu       }
61608a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61618a175baeSEmil Constantinescu       if (tolr>0.){
61628a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61638a175baeSEmil Constantinescu         nr_loc++;
61648a175baeSEmil Constantinescu       }
61658a175baeSEmil Constantinescu       tol=tola+tolr;
61668a175baeSEmil Constantinescu       if (tol>0.){
61678a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61688a175baeSEmil Constantinescu         n_loc++;
61698a175baeSEmil Constantinescu       }
61708a175baeSEmil Constantinescu     }
61718a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61728a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61738a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61748a175baeSEmil Constantinescu     const PetscScalar *atol;
61758a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61768a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
617776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61788a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61798a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61808a175baeSEmil Constantinescu       if (tola>0.){
61818a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61828a175baeSEmil Constantinescu         na_loc++;
61838a175baeSEmil Constantinescu       }
61848a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61858a175baeSEmil Constantinescu       if (tolr>0.){
61868a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61878a175baeSEmil Constantinescu         nr_loc++;
61888a175baeSEmil Constantinescu       }
61898a175baeSEmil Constantinescu       tol=tola+tolr;
61908a175baeSEmil Constantinescu       if (tol>0.){
61918a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61928a175baeSEmil Constantinescu         n_loc++;
61938a175baeSEmil Constantinescu       }
61948a175baeSEmil Constantinescu     }
61958a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61968a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61978a175baeSEmil Constantinescu     const PetscScalar *rtol;
61988a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61998a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
620076cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62018a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62028a175baeSEmil Constantinescu       tola = ts->atol;
62038a175baeSEmil Constantinescu       if (tola>0.){
62048a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62058a175baeSEmil Constantinescu         na_loc++;
62068a175baeSEmil Constantinescu       }
62078a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62088a175baeSEmil Constantinescu       if (tolr>0.){
62098a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62108a175baeSEmil Constantinescu         nr_loc++;
62118a175baeSEmil Constantinescu       }
62128a175baeSEmil Constantinescu       tol=tola+tolr;
62138a175baeSEmil Constantinescu       if (tol>0.){
62148a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62158a175baeSEmil Constantinescu         n_loc++;
62168a175baeSEmil Constantinescu       }
62178a175baeSEmil Constantinescu     }
62188a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62198a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
62208a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
622176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62228a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62238a175baeSEmil Constantinescu       tola = ts->atol;
62248a175baeSEmil Constantinescu       if (tola>0.){
62258a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62268a175baeSEmil Constantinescu         na_loc++;
62278a175baeSEmil Constantinescu       }
62288a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62298a175baeSEmil Constantinescu       if (tolr>0.){
62308a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62318a175baeSEmil Constantinescu         nr_loc++;
62328a175baeSEmil Constantinescu       }
62338a175baeSEmil Constantinescu       tol=tola+tolr;
62348a175baeSEmil Constantinescu       if (tol>0.){
62358a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62368a175baeSEmil Constantinescu         n_loc++;
62378a175baeSEmil Constantinescu       }
62388a175baeSEmil Constantinescu     }
62398a175baeSEmil Constantinescu   }
62408a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62418a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62428a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62438a175baeSEmil Constantinescu 
62448a175baeSEmil Constantinescu   err_loc[0] = sum;
62458a175baeSEmil Constantinescu   err_loc[1] = suma;
62468a175baeSEmil Constantinescu   err_loc[2] = sumr;
62478a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62488a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62498a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62508a175baeSEmil Constantinescu 
6251a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62528a175baeSEmil Constantinescu 
62538a175baeSEmil Constantinescu   gsum   = err_glb[0];
62548a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62558a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62568a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62578a175baeSEmil Constantinescu   na_glb = err_glb[4];
62588a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62598a175baeSEmil Constantinescu 
62608a175baeSEmil Constantinescu   *norm  = 0.;
62618a175baeSEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
62628a175baeSEmil Constantinescu   *norma = 0.;
62638a175baeSEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62648a175baeSEmil Constantinescu   *normr = 0.;
62658a175baeSEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62668a175baeSEmil Constantinescu 
62678a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62688a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62698a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62708a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62718a175baeSEmil Constantinescu }
62728a175baeSEmil Constantinescu 
62738a175baeSEmil Constantinescu /*@
62748a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62758a175baeSEmil Constantinescu    Collective on TS
62768a175baeSEmil Constantinescu 
62778a175baeSEmil Constantinescu    Input Arguments:
62788a175baeSEmil Constantinescu +  ts - time stepping context
62798a175baeSEmil Constantinescu .  E - error vector
62808a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62818a175baeSEmil Constantinescu -  Y - state vector, previous time step
62828a175baeSEmil Constantinescu 
62838a175baeSEmil Constantinescu    Output Arguments:
6284a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62858a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6286a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62878a175baeSEmil Constantinescu 
62888a175baeSEmil Constantinescu    Level: developer
62898a175baeSEmil Constantinescu 
62908a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62918a175baeSEmil Constantinescu @*/
62928a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62938a175baeSEmil Constantinescu {
62948a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62958a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62968a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62978a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
62988a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62998a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
63008a175baeSEmil Constantinescu 
63018a175baeSEmil Constantinescu   PetscFunctionBegin;
63028a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63038a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
63048a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
63058a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
63068a175baeSEmil Constantinescu   PetscValidType(E,2);
63078a175baeSEmil Constantinescu   PetscValidType(U,3);
63088a175baeSEmil Constantinescu   PetscValidType(Y,4);
63098a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
63108a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
63118a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
63128a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
63138a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
63148a175baeSEmil Constantinescu 
63158a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
63168a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
63178a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
63188a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
63198a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
63208a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
63218a175baeSEmil Constantinescu 
63228a175baeSEmil Constantinescu   max=0.;
63238a175baeSEmil Constantinescu   maxa=0.;
63248a175baeSEmil Constantinescu   maxr=0.;
63258a175baeSEmil Constantinescu 
63268a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
63278a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
63288a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63298a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63308a175baeSEmil Constantinescu 
63318a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
633276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63338a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63348a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63358a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63368a175baeSEmil Constantinescu       tol  = tola+tolr;
63378a175baeSEmil Constantinescu       if (tola>0.){
63388a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63398a175baeSEmil Constantinescu       }
63408a175baeSEmil Constantinescu       if (tolr>0.){
63418a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63428a175baeSEmil Constantinescu       }
63438a175baeSEmil Constantinescu       if (tol>0.){
63448a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63458a175baeSEmil Constantinescu       }
63468a175baeSEmil Constantinescu     }
63478a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63488a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63498a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63508a175baeSEmil Constantinescu     const PetscScalar *atol;
63518a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63528a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
635376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63548a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63558a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63568a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63578a175baeSEmil Constantinescu       tol  = tola+tolr;
63588a175baeSEmil Constantinescu       if (tola>0.){
63598a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63608a175baeSEmil Constantinescu       }
63618a175baeSEmil Constantinescu       if (tolr>0.){
63628a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63638a175baeSEmil Constantinescu       }
63648a175baeSEmil Constantinescu       if (tol>0.){
63658a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63668a175baeSEmil Constantinescu       }
63678a175baeSEmil Constantinescu     }
63688a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63698a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63708a175baeSEmil Constantinescu     const PetscScalar *rtol;
63718a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63728a175baeSEmil Constantinescu 
63738a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
637476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63758a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63768a175baeSEmil Constantinescu       tola = ts->atol;
63778a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63788a175baeSEmil Constantinescu       tol  = tola+tolr;
63798a175baeSEmil Constantinescu       if (tola>0.){
63808a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63818a175baeSEmil Constantinescu       }
63828a175baeSEmil Constantinescu       if (tolr>0.){
63838a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63848a175baeSEmil Constantinescu       }
63858a175baeSEmil Constantinescu       if (tol>0.){
63868a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63878a175baeSEmil Constantinescu       }
63888a175baeSEmil Constantinescu     }
63898a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63908a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63918a175baeSEmil Constantinescu 
63928a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
639376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63948a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63958a175baeSEmil Constantinescu       tola = ts->atol;
63968a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63978a175baeSEmil Constantinescu       tol  = tola+tolr;
63988a175baeSEmil Constantinescu       if (tola>0.){
63998a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64008a175baeSEmil Constantinescu       }
64018a175baeSEmil Constantinescu       if (tolr>0.){
64028a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64038a175baeSEmil Constantinescu       }
64048a175baeSEmil Constantinescu       if (tol>0.){
64058a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64068a175baeSEmil Constantinescu       }
64078a175baeSEmil Constantinescu     }
64088a175baeSEmil Constantinescu   }
64098a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
64108a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
64118a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
64128a175baeSEmil Constantinescu   err_loc[0] = max;
64138a175baeSEmil Constantinescu   err_loc[1] = maxa;
64148a175baeSEmil Constantinescu   err_loc[2] = maxr;
6415a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64168a175baeSEmil Constantinescu   gmax   = err_glb[0];
64178a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
64188a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
64198a175baeSEmil Constantinescu 
64208a175baeSEmil Constantinescu   *norm = gmax;
64218a175baeSEmil Constantinescu   *norma = gmaxa;
64228a175baeSEmil Constantinescu   *normr = gmaxr;
64238a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
64248a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
64258a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
64268a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64278a175baeSEmil Constantinescu }
64288a175baeSEmil Constantinescu 
64298a175baeSEmil Constantinescu /*@
64308a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
64318a175baeSEmil Constantinescu 
64328a175baeSEmil Constantinescu    Collective on TS
64338a175baeSEmil Constantinescu 
64348a175baeSEmil Constantinescu    Input Arguments:
64358a175baeSEmil Constantinescu +  ts - time stepping context
64368a175baeSEmil Constantinescu .  E - error vector
64378a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64388a175baeSEmil Constantinescu .  Y - state vector, previous time step
64398a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64408a175baeSEmil Constantinescu 
64418a175baeSEmil Constantinescu    Output Arguments:
6442a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64438a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6444a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64458a175baeSEmil Constantinescu 
64468a175baeSEmil Constantinescu    Options Database Keys:
64478a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64488a175baeSEmil Constantinescu 
64498a175baeSEmil Constantinescu    Level: developer
64508a175baeSEmil Constantinescu 
64518a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64528a175baeSEmil Constantinescu @*/
64538a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64548a175baeSEmil Constantinescu {
64558a175baeSEmil Constantinescu   PetscErrorCode ierr;
64568a175baeSEmil Constantinescu 
64578a175baeSEmil Constantinescu   PetscFunctionBegin;
64588a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64598a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64608a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
64618a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64628a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64638a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64648a175baeSEmil Constantinescu }
64658a175baeSEmil Constantinescu 
64668a175baeSEmil Constantinescu 
64678d59e960SJed Brown /*@
64688d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64698d59e960SJed Brown 
64708d59e960SJed Brown    Logically Collective on TS
64718d59e960SJed Brown 
64728d59e960SJed Brown    Input Arguments:
64738d59e960SJed Brown +  ts - time stepping context
64748d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64758d59e960SJed Brown 
64768d59e960SJed Brown    Note:
64778d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64788d59e960SJed Brown 
64798d59e960SJed Brown    Level: intermediate
64808d59e960SJed Brown 
64818d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64828d59e960SJed Brown @*/
64838d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64848d59e960SJed Brown {
64858d59e960SJed Brown   PetscFunctionBegin;
64868d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64878d59e960SJed Brown   ts->cfltime_local = cfltime;
64888d59e960SJed Brown   ts->cfltime       = -1.;
64898d59e960SJed Brown   PetscFunctionReturn(0);
64908d59e960SJed Brown }
64918d59e960SJed Brown 
64928d59e960SJed Brown /*@
64938d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
64948d59e960SJed Brown 
64958d59e960SJed Brown    Collective on TS
64968d59e960SJed Brown 
64978d59e960SJed Brown    Input Arguments:
64988d59e960SJed Brown .  ts - time stepping context
64998d59e960SJed Brown 
65008d59e960SJed Brown    Output Arguments:
65018d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
65028d59e960SJed Brown 
65038d59e960SJed Brown    Level: advanced
65048d59e960SJed Brown 
65058d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
65068d59e960SJed Brown @*/
65078d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
65088d59e960SJed Brown {
65098d59e960SJed Brown   PetscErrorCode ierr;
65108d59e960SJed Brown 
65118d59e960SJed Brown   PetscFunctionBegin;
65128d59e960SJed Brown   if (ts->cfltime < 0) {
6513b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65148d59e960SJed Brown   }
65158d59e960SJed Brown   *cfltime = ts->cfltime;
65168d59e960SJed Brown   PetscFunctionReturn(0);
65178d59e960SJed Brown }
65188d59e960SJed Brown 
6519d6ebe24aSShri Abhyankar /*@
6520d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6521d6ebe24aSShri Abhyankar 
6522d6ebe24aSShri Abhyankar    Input Parameters:
6523a2b725a8SWilliam Gropp +  ts   - the TS context.
6524d6ebe24aSShri Abhyankar .  xl   - lower bound.
6525a2b725a8SWilliam Gropp -  xu   - upper bound.
6526d6ebe24aSShri Abhyankar 
6527d6ebe24aSShri Abhyankar    Notes:
6528d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6529e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6530d6ebe24aSShri Abhyankar 
65312bd2b0e6SSatish Balay    Level: advanced
65322bd2b0e6SSatish Balay 
6533d6ebe24aSShri Abhyankar @*/
6534d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6535d6ebe24aSShri Abhyankar {
6536d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6537d6ebe24aSShri Abhyankar   SNES           snes;
6538d6ebe24aSShri Abhyankar 
6539d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6540d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6541d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6542d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6543d6ebe24aSShri Abhyankar }
6544d6ebe24aSShri Abhyankar 
6545b3603a34SBarry Smith /*@C
65464f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6547b3603a34SBarry Smith        in a time based line graph
6548b3603a34SBarry Smith 
6549b3603a34SBarry Smith    Collective on TS
6550b3603a34SBarry Smith 
6551b3603a34SBarry Smith    Input Parameters:
6552b3603a34SBarry Smith +  ts - the TS context
6553b3603a34SBarry Smith .  step - current time-step
6554b3603a34SBarry Smith .  ptime - current time
65557db568b7SBarry Smith .  u - current solution
65567db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6557b3603a34SBarry Smith 
6558b3d3934dSBarry Smith    Options Database:
65599ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6560b3d3934dSBarry Smith 
6561b3603a34SBarry Smith    Level: intermediate
6562b3603a34SBarry Smith 
656395452b02SPatrick Sanan    Notes:
656495452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6565b3603a34SBarry Smith 
65667db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65677db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65687db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65697db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6570b3603a34SBarry Smith @*/
65717db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6572b3603a34SBarry Smith {
6573b3603a34SBarry Smith   PetscErrorCode    ierr;
65747db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6575b3603a34SBarry Smith   const PetscScalar *yy;
657680666b62SBarry Smith   Vec               v;
6577b3603a34SBarry Smith 
6578b3603a34SBarry Smith   PetscFunctionBegin;
657963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
658058ff32f7SBarry Smith   if (!step) {
6581a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65826934998bSLisandro Dalcin     PetscInt      dim;
6583a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65840ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6585bab0a581SBarry Smith     if (!ctx->names) {
6586bab0a581SBarry Smith       PetscBool flg;
6587bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6588bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6589bab0a581SBarry Smith       if (flg) {
6590bab0a581SBarry Smith         PetscInt i,n;
6591bab0a581SBarry Smith         char     **names;
6592bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6593bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6594bab0a581SBarry Smith         for (i=0; i<n; i++) {
6595bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6596bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6597bab0a581SBarry Smith         }
6598bab0a581SBarry Smith         names[n] = NULL;
6599bab0a581SBarry Smith         ctx->names = names;
6600bab0a581SBarry Smith       }
6601bab0a581SBarry Smith     }
6602387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6603387f4636SBarry Smith       char      **displaynames;
6604387f4636SBarry Smith       PetscBool flg;
6605387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6606580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6607c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6608387f4636SBarry Smith       if (flg) {
6609a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6610387f4636SBarry Smith       }
6611387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6612387f4636SBarry Smith     }
6613387f4636SBarry Smith     if (ctx->displaynames) {
6614387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6615387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6616387f4636SBarry Smith     } else if (ctx->names) {
66170910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
66180b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6619387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6620b0bc92c6SBarry Smith     } else {
6621b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6622b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6623387f4636SBarry Smith     }
66240b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
662558ff32f7SBarry Smith   }
66266934998bSLisandro Dalcin 
66276934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66286934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
662980666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66306934998bSLisandro Dalcin   if (ctx->displaynames) {
66316934998bSLisandro Dalcin     PetscInt i;
66326934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66336934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66346934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66356934998bSLisandro Dalcin   } else {
6636e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6637e3efe391SJed Brown     PetscInt  i,n;
66386934998bSLisandro Dalcin     PetscReal *yreal;
663980666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6640785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6641e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66420b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6643e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6644e3efe391SJed Brown #else
66450b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6646e3efe391SJed Brown #endif
664780666b62SBarry Smith   }
66486934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66496934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66506934998bSLisandro Dalcin 
6651b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66520b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66536934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66543923b477SBarry Smith   }
6655b3603a34SBarry Smith   PetscFunctionReturn(0);
6656b3603a34SBarry Smith }
6657b3603a34SBarry Smith 
6658b037adc7SBarry Smith /*@C
665931152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6660b037adc7SBarry Smith 
6661b037adc7SBarry Smith    Collective on TS
6662b037adc7SBarry Smith 
6663b037adc7SBarry Smith    Input Parameters:
6664b037adc7SBarry Smith +  ts - the TS context
6665b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6666b037adc7SBarry Smith 
6667b037adc7SBarry Smith    Level: intermediate
6668b037adc7SBarry Smith 
666995452b02SPatrick Sanan    Notes:
667095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66717db568b7SBarry Smith 
6672a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6673b037adc7SBarry Smith @*/
667431152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6675b037adc7SBarry Smith {
6676b037adc7SBarry Smith   PetscErrorCode    ierr;
6677b037adc7SBarry Smith   PetscInt          i;
6678b037adc7SBarry Smith 
6679b037adc7SBarry Smith   PetscFunctionBegin;
6680b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6681b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66825537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6683b3d3934dSBarry Smith       break;
6684b3d3934dSBarry Smith     }
6685b3d3934dSBarry Smith   }
6686b3d3934dSBarry Smith   PetscFunctionReturn(0);
6687b3d3934dSBarry Smith }
6688b3d3934dSBarry Smith 
6689e673d494SBarry Smith /*@C
6690e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6691e673d494SBarry Smith 
6692e673d494SBarry Smith    Collective on TS
6693e673d494SBarry Smith 
6694e673d494SBarry Smith    Input Parameters:
6695e673d494SBarry Smith +  ts - the TS context
6696e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6697e673d494SBarry Smith 
6698e673d494SBarry Smith    Level: intermediate
6699e673d494SBarry Smith 
6700a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6701e673d494SBarry Smith @*/
6702e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6703e673d494SBarry Smith {
6704e673d494SBarry Smith   PetscErrorCode    ierr;
6705e673d494SBarry Smith 
6706e673d494SBarry Smith   PetscFunctionBegin;
6707e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6708e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6709e673d494SBarry Smith   PetscFunctionReturn(0);
6710e673d494SBarry Smith }
6711e673d494SBarry Smith 
6712b3d3934dSBarry Smith /*@C
6713b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6714b3d3934dSBarry Smith 
6715b3d3934dSBarry Smith    Collective on TS
6716b3d3934dSBarry Smith 
6717b3d3934dSBarry Smith    Input Parameter:
6718b3d3934dSBarry Smith .  ts - the TS context
6719b3d3934dSBarry Smith 
6720b3d3934dSBarry Smith    Output Parameter:
6721b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6722b3d3934dSBarry Smith 
6723b3d3934dSBarry Smith    Level: intermediate
6724b3d3934dSBarry Smith 
672595452b02SPatrick Sanan    Notes:
672695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67277db568b7SBarry Smith 
6728b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6729b3d3934dSBarry Smith @*/
6730b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6731b3d3934dSBarry Smith {
6732b3d3934dSBarry Smith   PetscInt       i;
6733b3d3934dSBarry Smith 
6734b3d3934dSBarry Smith   PetscFunctionBegin;
6735b3d3934dSBarry Smith   *names = NULL;
6736b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6737b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67385537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6739b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6740b3d3934dSBarry Smith       break;
6741387f4636SBarry Smith     }
6742387f4636SBarry Smith   }
6743387f4636SBarry Smith   PetscFunctionReturn(0);
6744387f4636SBarry Smith }
6745387f4636SBarry Smith 
6746a66092f1SBarry Smith /*@C
6747a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6748a66092f1SBarry Smith 
6749a66092f1SBarry Smith    Collective on TS
6750a66092f1SBarry Smith 
6751a66092f1SBarry Smith    Input Parameters:
6752a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6753a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6754a66092f1SBarry Smith 
6755a66092f1SBarry Smith    Level: intermediate
6756a66092f1SBarry Smith 
6757a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6758a66092f1SBarry Smith @*/
6759a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6760a66092f1SBarry Smith {
6761a66092f1SBarry Smith   PetscInt          j = 0,k;
6762a66092f1SBarry Smith   PetscErrorCode    ierr;
6763a66092f1SBarry Smith 
6764a66092f1SBarry Smith   PetscFunctionBegin;
6765a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6766a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6767a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6768a66092f1SBarry Smith   while (displaynames[j]) j++;
6769a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6770a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6771a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6772a66092f1SBarry Smith   j = 0;
6773a66092f1SBarry Smith   while (displaynames[j]) {
6774a66092f1SBarry Smith     k = 0;
6775a66092f1SBarry Smith     while (ctx->names[k]) {
6776a66092f1SBarry Smith       PetscBool flg;
6777a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6778a66092f1SBarry Smith       if (flg) {
6779a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6780a66092f1SBarry Smith         break;
6781a66092f1SBarry Smith       }
6782a66092f1SBarry Smith       k++;
6783a66092f1SBarry Smith     }
6784a66092f1SBarry Smith     j++;
6785a66092f1SBarry Smith   }
6786a66092f1SBarry Smith   PetscFunctionReturn(0);
6787a66092f1SBarry Smith }
6788a66092f1SBarry Smith 
6789387f4636SBarry Smith /*@C
6790387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6791387f4636SBarry Smith 
6792387f4636SBarry Smith    Collective on TS
6793387f4636SBarry Smith 
6794387f4636SBarry Smith    Input Parameters:
6795387f4636SBarry Smith +  ts - the TS context
6796a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6797387f4636SBarry 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 
6801387f4636SBarry Smith    Level: intermediate
6802387f4636SBarry Smith 
6803387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6804387f4636SBarry Smith @*/
6805387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6806387f4636SBarry Smith {
6807a66092f1SBarry Smith   PetscInt          i;
6808387f4636SBarry Smith   PetscErrorCode    ierr;
6809387f4636SBarry Smith 
6810387f4636SBarry Smith   PetscFunctionBegin;
6811387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6812387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68135537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6814b3d3934dSBarry Smith       break;
6815b037adc7SBarry Smith     }
6816b037adc7SBarry Smith   }
6817b037adc7SBarry Smith   PetscFunctionReturn(0);
6818b037adc7SBarry Smith }
6819b037adc7SBarry Smith 
682080666b62SBarry Smith /*@C
682180666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
682280666b62SBarry Smith 
682380666b62SBarry Smith    Collective on TS
682480666b62SBarry Smith 
682580666b62SBarry Smith    Input Parameters:
682680666b62SBarry Smith +  ts - the TS context
682780666b62SBarry Smith .  transform - the transform function
68287684fa3eSBarry Smith .  destroy - function to destroy the optional context
682980666b62SBarry Smith -  ctx - optional context used by transform function
683080666b62SBarry Smith 
683195452b02SPatrick Sanan    Notes:
683295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68337db568b7SBarry Smith 
683480666b62SBarry Smith    Level: intermediate
683580666b62SBarry Smith 
6836a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
683780666b62SBarry Smith @*/
68387684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
683980666b62SBarry Smith {
684080666b62SBarry Smith   PetscInt          i;
6841a66092f1SBarry Smith   PetscErrorCode    ierr;
684280666b62SBarry Smith 
684380666b62SBarry Smith   PetscFunctionBegin;
684480666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
684580666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68465537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
684780666b62SBarry Smith     }
684880666b62SBarry Smith   }
684980666b62SBarry Smith   PetscFunctionReturn(0);
685080666b62SBarry Smith }
685180666b62SBarry Smith 
6852e673d494SBarry Smith /*@C
6853e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6854e673d494SBarry Smith 
6855e673d494SBarry Smith    Collective on TSLGCtx
6856e673d494SBarry Smith 
6857e673d494SBarry Smith    Input Parameters:
6858e673d494SBarry Smith +  ts - the TS context
6859e673d494SBarry Smith .  transform - the transform function
68607684fa3eSBarry Smith .  destroy - function to destroy the optional context
6861e673d494SBarry Smith -  ctx - optional context used by transform function
6862e673d494SBarry Smith 
6863e673d494SBarry Smith    Level: intermediate
6864e673d494SBarry Smith 
6865a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6866e673d494SBarry Smith @*/
68677684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6868e673d494SBarry Smith {
6869e673d494SBarry Smith   PetscFunctionBegin;
6870e673d494SBarry Smith   ctx->transform    = transform;
68717684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6872e673d494SBarry Smith   ctx->transformctx = tctx;
6873e673d494SBarry Smith   PetscFunctionReturn(0);
6874e673d494SBarry Smith }
6875e673d494SBarry Smith 
6876ef20d060SBarry Smith /*@C
68778b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6878ef20d060SBarry Smith        in a time based line graph
6879ef20d060SBarry Smith 
6880ef20d060SBarry Smith    Collective on TS
6881ef20d060SBarry Smith 
6882ef20d060SBarry Smith    Input Parameters:
6883ef20d060SBarry Smith +  ts - the TS context
6884ef20d060SBarry Smith .  step - current time-step
6885ef20d060SBarry Smith .  ptime - current time
68867db568b7SBarry Smith .  u - current solution
68877db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6888ef20d060SBarry Smith 
6889ef20d060SBarry Smith    Level: intermediate
6890ef20d060SBarry Smith 
689195452b02SPatrick Sanan    Notes:
689295452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6893abd5a294SJed Brown 
6894abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6895abd5a294SJed Brown 
6896abd5a294SJed Brown    Options Database Keys:
68974f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6898ef20d060SBarry Smith 
6899abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6900ef20d060SBarry Smith @*/
69010910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6902ef20d060SBarry Smith {
6903ef20d060SBarry Smith   PetscErrorCode    ierr;
69040b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6905ef20d060SBarry Smith   const PetscScalar *yy;
6906ef20d060SBarry Smith   Vec               y;
6907ef20d060SBarry Smith 
6908ef20d060SBarry Smith   PetscFunctionBegin;
6909a9f9c1f6SBarry Smith   if (!step) {
6910a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69116934998bSLisandro Dalcin     PetscInt      dim;
6912a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
69138b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
69140910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6915a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6916a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6917a9f9c1f6SBarry Smith   }
69180910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6919ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69200910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6921ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6922e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6923e3efe391SJed Brown   {
6924e3efe391SJed Brown     PetscReal *yreal;
6925e3efe391SJed Brown     PetscInt  i,n;
6926e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6927785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6928e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69290b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6930e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6931e3efe391SJed Brown   }
6932e3efe391SJed Brown #else
69330b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6934e3efe391SJed Brown #endif
6935ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6936ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6937b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69380b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69396934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69403923b477SBarry Smith   }
6941ef20d060SBarry Smith   PetscFunctionReturn(0);
6942ef20d060SBarry Smith }
6943ef20d060SBarry Smith 
69445e3b7effSJoseph Pusztay /*@C
69455e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
69465e3b7effSJoseph Pusztay 
69475e3b7effSJoseph Pusztay    Input Parameters:
69485e3b7effSJoseph Pusztay +  ts - the TS context
69495e3b7effSJoseph Pusztay .  step - current time-step
69505e3b7effSJoseph Pusztay .  ptime - current time
69515e3b7effSJoseph Pusztay .  u - current solution
69525e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
69535e3b7effSJoseph Pusztay 
69545e3b7effSJoseph Pusztay    Options Database:
6955918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
69565e3b7effSJoseph Pusztay 
69575e3b7effSJoseph Pusztay    Level: intermediate
69585e3b7effSJoseph Pusztay 
69595e3b7effSJoseph Pusztay @*/
69600ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
69611b575b74SJoseph Pusztay {
69621b575b74SJoseph Pusztay   PetscErrorCode    ierr;
69631b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
69641b575b74SJoseph Pusztay   const PetscScalar *yy;
6965b1670a61SJoseph Pusztay   PetscReal       *y,*x;
69661b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
69671b575b74SJoseph Pusztay   DM                dm;
69681b575b74SJoseph Pusztay 
69691b575b74SJoseph Pusztay   PetscFunctionBegin;
69701b575b74SJoseph Pusztay 
69711b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
69721b575b74SJoseph Pusztay   if (!step) {
69731b575b74SJoseph Pusztay     PetscDrawAxis axis;
69741b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
69751b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6976895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6977895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
69781b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
69791b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
69801b575b74SJoseph Pusztay     if (dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
69811b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69821b575b74SJoseph Pusztay     Np /= 2*dim;
69831b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69841b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69851b575b74SJoseph Pusztay   }
69861b575b74SJoseph Pusztay 
69871b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69881b575b74SJoseph Pusztay   Np /= 2*dim;
69891b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6990895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69911b575b74SJoseph Pusztay   /* get points from solution vector */
69921b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6993b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6994b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6995895f37d5SJoseph Pusztay   }
69961b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
69971b575b74SJoseph Pusztay 
69981b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69991b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
70001b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
70011b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
70021b575b74SJoseph Pusztay   }
70031b575b74SJoseph Pusztay 
7004918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
7005918b1d10SJoseph Pusztay 
70061b575b74SJoseph Pusztay   PetscFunctionReturn(0);
70071b575b74SJoseph Pusztay }
70081b575b74SJoseph Pusztay 
70091b575b74SJoseph Pusztay 
70101b575b74SJoseph Pusztay 
70117cf37e64SBarry Smith /*@C
70127cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
70137cf37e64SBarry Smith 
70147cf37e64SBarry Smith    Collective on TS
70157cf37e64SBarry Smith 
70167cf37e64SBarry Smith    Input Parameters:
70177cf37e64SBarry Smith +  ts - the TS context
70187cf37e64SBarry Smith .  step - current time-step
70197cf37e64SBarry Smith .  ptime - current time
70207cf37e64SBarry Smith .  u - current solution
70217cf37e64SBarry Smith -  dctx - unused context
70227cf37e64SBarry Smith 
70237cf37e64SBarry Smith    Level: intermediate
70247cf37e64SBarry Smith 
70257cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
70267cf37e64SBarry Smith 
70277cf37e64SBarry Smith    Options Database Keys:
70287cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
70297cf37e64SBarry Smith 
70307cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
70317cf37e64SBarry Smith @*/
7032edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70337cf37e64SBarry Smith {
70347cf37e64SBarry Smith   PetscErrorCode    ierr;
70357cf37e64SBarry Smith   Vec               y;
70367cf37e64SBarry Smith   PetscReal         nrm;
7037edbaebb3SBarry Smith   PetscBool         flg;
70387cf37e64SBarry Smith 
70397cf37e64SBarry Smith   PetscFunctionBegin;
70407cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70417cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70427cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7043edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7044edbaebb3SBarry Smith   if (flg) {
70457cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7046edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7047edbaebb3SBarry Smith   }
7048edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7049edbaebb3SBarry Smith   if (flg) {
7050edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7051edbaebb3SBarry Smith   }
7052edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70537cf37e64SBarry Smith   PetscFunctionReturn(0);
70547cf37e64SBarry Smith }
70557cf37e64SBarry Smith 
7056201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7057201da799SBarry Smith {
7058201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7059201da799SBarry Smith   PetscReal      x   = ptime,y;
7060201da799SBarry Smith   PetscErrorCode ierr;
7061201da799SBarry Smith   PetscInt       its;
7062201da799SBarry Smith 
7063201da799SBarry Smith   PetscFunctionBegin;
706463e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7065201da799SBarry Smith   if (!n) {
7066201da799SBarry Smith     PetscDrawAxis axis;
7067201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7068201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7069201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7070201da799SBarry Smith     ctx->snes_its = 0;
7071201da799SBarry Smith   }
7072201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7073201da799SBarry Smith   y    = its - ctx->snes_its;
7074201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70753fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7076201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70776934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7078201da799SBarry Smith   }
7079201da799SBarry Smith   ctx->snes_its = its;
7080201da799SBarry Smith   PetscFunctionReturn(0);
7081201da799SBarry Smith }
7082201da799SBarry Smith 
7083201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7084201da799SBarry Smith {
7085201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7086201da799SBarry Smith   PetscReal      x   = ptime,y;
7087201da799SBarry Smith   PetscErrorCode ierr;
7088201da799SBarry Smith   PetscInt       its;
7089201da799SBarry Smith 
7090201da799SBarry Smith   PetscFunctionBegin;
709163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7092201da799SBarry Smith   if (!n) {
7093201da799SBarry Smith     PetscDrawAxis axis;
7094201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7095201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7096201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7097201da799SBarry Smith     ctx->ksp_its = 0;
7098201da799SBarry Smith   }
7099201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7100201da799SBarry Smith   y    = its - ctx->ksp_its;
7101201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
710299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7103201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
71046934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7105201da799SBarry Smith   }
7106201da799SBarry Smith   ctx->ksp_its = its;
7107201da799SBarry Smith   PetscFunctionReturn(0);
7108201da799SBarry Smith }
7109f9c1d6abSBarry Smith 
7110f9c1d6abSBarry Smith /*@
7111f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7112f9c1d6abSBarry Smith 
7113d083f849SBarry Smith    Collective on TS
7114f9c1d6abSBarry Smith 
7115f9c1d6abSBarry Smith    Input Parameters:
7116f9c1d6abSBarry Smith +  ts - the TS context
7117f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7118f9c1d6abSBarry Smith 
7119f9c1d6abSBarry Smith    Output Parameters:
7120f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7121f9c1d6abSBarry Smith 
7122f9c1d6abSBarry Smith    Level: developer
7123f9c1d6abSBarry Smith 
7124f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7125f9c1d6abSBarry Smith @*/
7126f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7127f9c1d6abSBarry Smith {
7128f9c1d6abSBarry Smith   PetscErrorCode ierr;
7129f9c1d6abSBarry Smith 
7130f9c1d6abSBarry Smith   PetscFunctionBegin;
7131f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7132ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7133f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7134f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7135f9c1d6abSBarry Smith }
713624655328SShri 
7137b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7138b3d3934dSBarry Smith /*@C
7139b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7140b3d3934dSBarry Smith 
7141b3d3934dSBarry Smith    Collective on TS
7142b3d3934dSBarry Smith 
7143b3d3934dSBarry Smith    Input Parameters:
7144b3d3934dSBarry Smith .  ts  - the ODE solver object
7145b3d3934dSBarry Smith 
7146b3d3934dSBarry Smith    Output Parameter:
7147b3d3934dSBarry Smith .  ctx - the context
7148b3d3934dSBarry Smith 
7149b3d3934dSBarry Smith    Level: intermediate
7150b3d3934dSBarry Smith 
7151b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7152b3d3934dSBarry Smith 
7153b3d3934dSBarry Smith @*/
7154b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7155b3d3934dSBarry Smith {
7156b3d3934dSBarry Smith   PetscErrorCode ierr;
7157b3d3934dSBarry Smith 
7158b3d3934dSBarry Smith   PetscFunctionBegin;
7159a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7160b3d3934dSBarry Smith   PetscFunctionReturn(0);
7161b3d3934dSBarry Smith }
7162b3d3934dSBarry Smith 
7163b3d3934dSBarry Smith /*@C
7164b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7165b3d3934dSBarry Smith 
7166b3d3934dSBarry Smith    Collective on TS
7167b3d3934dSBarry Smith 
7168b3d3934dSBarry Smith    Input Parameters:
7169b3d3934dSBarry Smith +  ts - the TS context
7170b3d3934dSBarry Smith .  step - current time-step
7171b3d3934dSBarry Smith .  ptime - current time
71727db568b7SBarry Smith .  u  - current solution
71737db568b7SBarry Smith -  dctx - the envelope context
7174b3d3934dSBarry Smith 
7175b3d3934dSBarry Smith    Options Database:
7176b3d3934dSBarry Smith .  -ts_monitor_envelope
7177b3d3934dSBarry Smith 
7178b3d3934dSBarry Smith    Level: intermediate
7179b3d3934dSBarry Smith 
718095452b02SPatrick Sanan    Notes:
718195452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7182b3d3934dSBarry Smith 
71837db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7184b3d3934dSBarry Smith @*/
71857db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7186b3d3934dSBarry Smith {
7187b3d3934dSBarry Smith   PetscErrorCode       ierr;
71887db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7189b3d3934dSBarry Smith 
7190b3d3934dSBarry Smith   PetscFunctionBegin;
7191b3d3934dSBarry Smith   if (!ctx->max) {
7192b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7193b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7194b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7195b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7196b3d3934dSBarry Smith   } else {
7197b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7198b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7199b3d3934dSBarry Smith   }
7200b3d3934dSBarry Smith   PetscFunctionReturn(0);
7201b3d3934dSBarry Smith }
7202b3d3934dSBarry Smith 
7203b3d3934dSBarry Smith /*@C
7204b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7205b3d3934dSBarry Smith 
7206b3d3934dSBarry Smith    Collective on TS
7207b3d3934dSBarry Smith 
7208b3d3934dSBarry Smith    Input Parameter:
7209b3d3934dSBarry Smith .  ts - the TS context
7210b3d3934dSBarry Smith 
7211b3d3934dSBarry Smith    Output Parameter:
7212b3d3934dSBarry Smith +  max - the maximum values
7213b3d3934dSBarry Smith -  min - the minimum values
7214b3d3934dSBarry Smith 
721595452b02SPatrick Sanan    Notes:
721695452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
72177db568b7SBarry Smith 
7218b3d3934dSBarry Smith    Level: intermediate
7219b3d3934dSBarry Smith 
7220b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7221b3d3934dSBarry Smith @*/
7222b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7223b3d3934dSBarry Smith {
7224b3d3934dSBarry Smith   PetscInt i;
7225b3d3934dSBarry Smith 
7226b3d3934dSBarry Smith   PetscFunctionBegin;
7227b3d3934dSBarry Smith   if (max) *max = NULL;
7228b3d3934dSBarry Smith   if (min) *min = NULL;
7229b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7230b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
72315537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7232b3d3934dSBarry Smith       if (max) *max = ctx->max;
7233b3d3934dSBarry Smith       if (min) *min = ctx->min;
7234b3d3934dSBarry Smith       break;
7235b3d3934dSBarry Smith     }
7236b3d3934dSBarry Smith   }
7237b3d3934dSBarry Smith   PetscFunctionReturn(0);
7238b3d3934dSBarry Smith }
7239b3d3934dSBarry Smith 
7240b3d3934dSBarry Smith /*@C
7241b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7242b3d3934dSBarry Smith 
7243b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7244b3d3934dSBarry Smith 
7245b3d3934dSBarry Smith    Input Parameter:
7246b3d3934dSBarry Smith .  ctx - the monitor context
7247b3d3934dSBarry Smith 
7248b3d3934dSBarry Smith    Level: intermediate
7249b3d3934dSBarry Smith 
72507db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7251b3d3934dSBarry Smith @*/
7252b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7253b3d3934dSBarry Smith {
7254b3d3934dSBarry Smith   PetscErrorCode ierr;
7255b3d3934dSBarry Smith 
7256b3d3934dSBarry Smith   PetscFunctionBegin;
7257b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7258b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7259b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7260b3d3934dSBarry Smith   PetscFunctionReturn(0);
7261b3d3934dSBarry Smith }
7262f2dee214SBarry Smith 
726324655328SShri /*@
7264dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7265dcb233daSLisandro Dalcin 
7266dcb233daSLisandro Dalcin    Collective on TS
7267dcb233daSLisandro Dalcin 
7268dcb233daSLisandro Dalcin    Input Parameter:
7269dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7270dcb233daSLisandro Dalcin 
7271dcb233daSLisandro Dalcin    Level: advanced
7272dcb233daSLisandro Dalcin 
7273dcb233daSLisandro Dalcin    Notes:
7274dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7275dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7276dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7277dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7278dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7279dcb233daSLisandro Dalcin    discontinuous source terms).
7280dcb233daSLisandro Dalcin 
7281dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7282dcb233daSLisandro Dalcin @*/
7283dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7284dcb233daSLisandro Dalcin {
7285dcb233daSLisandro Dalcin   PetscFunctionBegin;
7286dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7287dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7288dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7289dcb233daSLisandro Dalcin }
7290dcb233daSLisandro Dalcin 
7291dcb233daSLisandro Dalcin /*@
729224655328SShri    TSRollBack - Rolls back one time step
729324655328SShri 
729424655328SShri    Collective on TS
729524655328SShri 
729624655328SShri    Input Parameter:
729724655328SShri .  ts - the TS context obtained from TSCreate()
729824655328SShri 
729924655328SShri    Level: advanced
730024655328SShri 
730124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
730224655328SShri @*/
730324655328SShri PetscErrorCode  TSRollBack(TS ts)
730424655328SShri {
730524655328SShri   PetscErrorCode ierr;
730624655328SShri 
730724655328SShri   PetscFunctionBegin;
730824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7309b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
731024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
731124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
731224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
731324655328SShri   ts->ptime = ts->ptime_prev;
7314be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
73152808aa04SLisandro Dalcin   ts->steps--;
7316b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
731724655328SShri   PetscFunctionReturn(0);
731824655328SShri }
7319aeb4809dSShri Abhyankar 
7320ff22ae23SHong Zhang /*@
7321ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7322ff22ae23SHong Zhang 
7323ff22ae23SHong Zhang    Input Parameter:
7324ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7325ff22ae23SHong Zhang 
73260429704eSStefano Zampini    Output Parameters:
73270429704eSStefano Zampini +  ns - the number of stages
73280429704eSStefano Zampini -  Y - the current stage vectors
73290429704eSStefano Zampini 
7330ff22ae23SHong Zhang    Level: advanced
7331ff22ae23SHong Zhang 
73320429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
73330429704eSStefano Zampini 
7334ff22ae23SHong Zhang .seealso: TSCreate()
7335ff22ae23SHong Zhang @*/
7336ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7337ff22ae23SHong Zhang {
7338ff22ae23SHong Zhang   PetscErrorCode ierr;
7339ff22ae23SHong Zhang 
7340ff22ae23SHong Zhang   PetscFunctionBegin;
7341ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
73420429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
73430429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
73440429704eSStefano Zampini   if (!ts->ops->getstages) {
73450429704eSStefano Zampini     if (ns) *ns = 0;
73460429704eSStefano Zampini     if (Y) *Y = NULL;
73470429704eSStefano Zampini   } else {
7348ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7349ff22ae23SHong Zhang   }
7350ff22ae23SHong Zhang   PetscFunctionReturn(0);
7351ff22ae23SHong Zhang }
7352ff22ae23SHong Zhang 
7353847ff0e1SMatthew G. Knepley /*@C
7354847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7355847ff0e1SMatthew G. Knepley 
7356847ff0e1SMatthew G. Knepley   Collective on SNES
7357847ff0e1SMatthew G. Knepley 
7358847ff0e1SMatthew G. Knepley   Input Parameters:
7359847ff0e1SMatthew G. Knepley + ts - the TS context
7360847ff0e1SMatthew G. Knepley . t - current timestep
7361847ff0e1SMatthew G. Knepley . U - state vector
7362847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7363847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7364847ff0e1SMatthew G. Knepley - ctx - an optional user context
7365847ff0e1SMatthew G. Knepley 
7366847ff0e1SMatthew G. Knepley   Output Parameters:
7367847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7368847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7369847ff0e1SMatthew G. Knepley 
7370847ff0e1SMatthew G. Knepley   Level: intermediate
7371847ff0e1SMatthew G. Knepley 
7372847ff0e1SMatthew G. Knepley   Notes:
7373847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7374847ff0e1SMatthew G. Knepley 
7375847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7376847ff0e1SMatthew G. Knepley 
7377847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7378847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7379847ff0e1SMatthew G. Knepley 
7380847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7381847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7382847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7383847ff0e1SMatthew G. Knepley 
7384847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7385847ff0e1SMatthew G. Knepley @*/
7386847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7387847ff0e1SMatthew G. Knepley {
7388847ff0e1SMatthew G. Knepley   SNES           snes;
7389847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7390847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7391847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7392847ff0e1SMatthew G. Knepley 
7393847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7394c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7395847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7396847ff0e1SMatthew G. Knepley   if (!color) {
7397847ff0e1SMatthew G. Knepley     DM         dm;
7398847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7399847ff0e1SMatthew G. Knepley 
7400847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7401847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7402847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7403847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7404847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7405847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7406847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7407847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7408847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7409847ff0e1SMatthew G. Knepley     } else {
7410847ff0e1SMatthew G. Knepley       MatColoring mc;
7411847ff0e1SMatthew G. Knepley 
7412847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7413847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7414847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7415847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7416847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7417847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7418847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7419847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7420847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7421847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7422847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7423847ff0e1SMatthew G. Knepley     }
7424847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7425847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7426847ff0e1SMatthew G. Knepley   }
7427847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7428847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7429847ff0e1SMatthew G. Knepley   if (J != B) {
7430847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7431847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7432847ff0e1SMatthew G. Knepley   }
7433847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7434847ff0e1SMatthew G. Knepley }
743593b34091SDebojyoti Ghosh 
7436cb9d8021SPierre Barbier de Reuille /*@
74376bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7438cb9d8021SPierre Barbier de Reuille 
7439cb9d8021SPierre Barbier de Reuille     Input Parameters:
74406bc98fa9SBarry Smith +    ts - the TS context
74416bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
74426bc98fa9SBarry Smith 
74436bc98fa9SBarry Smith     Calling sequence of func:
74446bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
74456bc98fa9SBarry Smith 
74466bc98fa9SBarry Smith +   ts - the TS context
74476bc98fa9SBarry Smith .   time - the current time (of the stage)
74486bc98fa9SBarry Smith .   state - the state to check if it is valid
74496bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7450cb9d8021SPierre Barbier de Reuille 
7451cb9d8021SPierre Barbier de Reuille     Level: intermediate
7452cb9d8021SPierre Barbier de Reuille 
74536bc98fa9SBarry Smith     Notes:
74546bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
74556bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
74566bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
74576bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
74586bc98fa9SBarry Smith 
74596bc98fa9SBarry Smith     Developer Notes:
74606bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
74616bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
74626bc98fa9SBarry Smith 
74636bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7464cb9d8021SPierre Barbier de Reuille @*/
7465cb9d8021SPierre Barbier de Reuille 
7466d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7467cb9d8021SPierre Barbier de Reuille {
7468cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7469cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7470cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7471cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7472cb9d8021SPierre Barbier de Reuille }
7473cb9d8021SPierre Barbier de Reuille 
7474cb9d8021SPierre Barbier de Reuille /*@
74756bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7476cb9d8021SPierre Barbier de Reuille 
7477cb9d8021SPierre Barbier de Reuille     Input Parameters:
74786bc98fa9SBarry Smith +    ts - the TS context
74796bc98fa9SBarry Smith .    stagetime - time of the simulation
74806bc98fa9SBarry Smith -    Y - state vector to check.
7481cb9d8021SPierre Barbier de Reuille 
7482cb9d8021SPierre Barbier de Reuille     Output Parameter:
74836bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7484cb9d8021SPierre Barbier de Reuille 
7485cb9d8021SPierre Barbier de Reuille     Note:
74866bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74876bc98fa9SBarry Smith     to check if the current state is valid.
748896a0c994SBarry Smith 
74896bc98fa9SBarry Smith     Level: developer
74906bc98fa9SBarry Smith 
74916bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7492cb9d8021SPierre Barbier de Reuille @*/
7493d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7494cb9d8021SPierre Barbier de Reuille {
7495cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7496cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7497cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7498cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7499d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7500cb9d8021SPierre Barbier de Reuille   }
7501cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7502cb9d8021SPierre Barbier de Reuille }
75031ceb14c0SBarry Smith 
750493b34091SDebojyoti Ghosh /*@C
7505e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
750693b34091SDebojyoti Ghosh 
7507d083f849SBarry Smith   Collective
750893b34091SDebojyoti Ghosh 
750993b34091SDebojyoti Ghosh   Input Parameter:
751093b34091SDebojyoti Ghosh . tsin    - The input TS
751193b34091SDebojyoti Ghosh 
751293b34091SDebojyoti Ghosh   Output Parameter:
7513e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
751493b34091SDebojyoti Ghosh 
75155eca1a21SEmil Constantinescu   Notes:
75165eca1a21SEmil 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.
75175eca1a21SEmil Constantinescu 
7518928bb9adSStefano 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);
75195eca1a21SEmil Constantinescu 
75205eca1a21SEmil Constantinescu   Level: developer
752193b34091SDebojyoti Ghosh 
7522e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
752393b34091SDebojyoti Ghosh @*/
7524baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
752593b34091SDebojyoti Ghosh {
752693b34091SDebojyoti Ghosh   TS             t;
752793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7528dc846ba4SSatish Balay   SNES           snes_start;
7529dc846ba4SSatish Balay   DM             dm;
7530dc846ba4SSatish Balay   TSType         type;
753193b34091SDebojyoti Ghosh 
753293b34091SDebojyoti Ghosh   PetscFunctionBegin;
753393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
753493b34091SDebojyoti Ghosh   *tsout = NULL;
753593b34091SDebojyoti Ghosh 
75367a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
753793b34091SDebojyoti Ghosh 
753893b34091SDebojyoti Ghosh   /* General TS description */
753993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
754093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
754193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
754293b34091SDebojyoti Ghosh   t->snes_its          = 0;
754393b34091SDebojyoti Ghosh   t->nwork             = 0;
75447d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
754593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
754693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
754793b34091SDebojyoti Ghosh 
754834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
754934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7550d15a3a53SEmil Constantinescu 
755193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
755293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
755393b34091SDebojyoti Ghosh 
755493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
755551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
755693b34091SDebojyoti Ghosh 
7557e7069c78SShri   t->trajectory = tsin->trajectory;
7558e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7559e7069c78SShri 
7560e7069c78SShri   t->event = tsin->event;
75616b10a48eSSatish Balay   if (t->event) t->event->refct++;
7562e7069c78SShri 
756393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
756493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7565e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
756693b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
756793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
756893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
756993b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
757093b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
757193b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
757293b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
757393b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
757493b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
757593b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
757693b34091SDebojyoti Ghosh 
757793b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
757893b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
757993b34091SDebojyoti Ghosh 
758093b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
758193b34091SDebojyoti Ghosh 
758293b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
758393b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
758493b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
758593b34091SDebojyoti Ghosh 
758693b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
758793b34091SDebojyoti Ghosh 
75880d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75890d4fed19SBarry Smith     PetscInt i;
75900d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75910d4fed19SBarry Smith     for (i=0; i<10; i++) {
75920d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75930d4fed19SBarry Smith     }
75940d4fed19SBarry Smith   }
759593b34091SDebojyoti Ghosh   *tsout = t;
759693b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
759793b34091SDebojyoti Ghosh }
7598f3b1f45cSBarry Smith 
7599f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7600f3b1f45cSBarry Smith {
7601f3b1f45cSBarry Smith   PetscErrorCode ierr;
7602f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7603f3b1f45cSBarry Smith 
7604f3b1f45cSBarry Smith   PetscFunctionBegin;
7605f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7606f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7607f3b1f45cSBarry Smith }
7608f3b1f45cSBarry Smith 
7609f3b1f45cSBarry Smith /*@
7610f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7611f3b1f45cSBarry Smith 
7612d083f849SBarry Smith    Logically Collective on TS
7613f3b1f45cSBarry Smith 
7614f3b1f45cSBarry Smith     Input Parameters:
7615f3b1f45cSBarry Smith     TS - the time stepping routine
7616f3b1f45cSBarry Smith 
7617f3b1f45cSBarry Smith    Output Parameter:
7618f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7619f3b1f45cSBarry Smith 
7620f3b1f45cSBarry Smith    Options Database:
7621f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7622f3b1f45cSBarry Smith 
7623f3b1f45cSBarry Smith    Level: advanced
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 .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7629f3b1f45cSBarry Smith @*/
7630f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7631f3b1f45cSBarry Smith {
7632f3b1f45cSBarry Smith   Mat            J,B;
7633f3b1f45cSBarry Smith   PetscErrorCode ierr;
7634f3b1f45cSBarry Smith   TSRHSJacobian  func;
7635f3b1f45cSBarry Smith   void*          ctx;
7636f3b1f45cSBarry Smith 
7637f3b1f45cSBarry Smith   PetscFunctionBegin;
7638f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7639f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7640f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7641f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7642f3b1f45cSBarry Smith }
7643f3b1f45cSBarry Smith 
7644f3b1f45cSBarry Smith /*@C
7645f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7646f3b1f45cSBarry Smith 
7647d083f849SBarry Smith    Logically Collective on TS
7648f3b1f45cSBarry Smith 
7649f3b1f45cSBarry Smith     Input Parameters:
7650f3b1f45cSBarry Smith     TS - the time stepping routine
7651f3b1f45cSBarry Smith 
7652f3b1f45cSBarry Smith    Output Parameter:
7653f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7654f3b1f45cSBarry Smith 
7655f3b1f45cSBarry Smith    Options Database:
7656f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7657f3b1f45cSBarry Smith 
765895452b02SPatrick Sanan    Notes:
765995452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7660f3b1f45cSBarry Smith 
7661f3b1f45cSBarry Smith    Level: advanced
7662f3b1f45cSBarry Smith 
7663f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7664f3b1f45cSBarry Smith @*/
7665f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7666f3b1f45cSBarry Smith {
7667f3b1f45cSBarry Smith   Mat            J,B;
7668f3b1f45cSBarry Smith   PetscErrorCode ierr;
7669f3b1f45cSBarry Smith   void           *ctx;
7670f3b1f45cSBarry Smith   TSRHSJacobian  func;
7671f3b1f45cSBarry Smith 
7672f3b1f45cSBarry Smith   PetscFunctionBegin;
7673f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7674f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7675f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7676f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7677f3b1f45cSBarry Smith }
76780fe4d17eSHong Zhang 
76790fe4d17eSHong Zhang /*@
76800fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
76810fe4d17eSHong Zhang 
76820fe4d17eSHong Zhang   Logically collective
76830fe4d17eSHong Zhang 
76840fe4d17eSHong Zhang   Input Parameter:
76850fe4d17eSHong Zhang +  ts - timestepping context
76860fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76870fe4d17eSHong Zhang 
76880fe4d17eSHong Zhang   Options Database:
76890fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76900fe4d17eSHong Zhang 
76910fe4d17eSHong Zhang   Notes:
76920fe4d17eSHong Zhang     This is only useful for multirate methods
76930fe4d17eSHong Zhang 
76940fe4d17eSHong Zhang   Level: intermediate
76950fe4d17eSHong Zhang 
76960fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
76970fe4d17eSHong Zhang @*/
76980fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
76990fe4d17eSHong Zhang {
77000fe4d17eSHong Zhang   PetscFunctionBegin;
77010fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77020fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
77030fe4d17eSHong Zhang   PetscFunctionReturn(0);
77040fe4d17eSHong Zhang }
77050fe4d17eSHong Zhang 
77060fe4d17eSHong Zhang /*@
77070fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
77080fe4d17eSHong Zhang 
77090fe4d17eSHong Zhang   Not collective
77100fe4d17eSHong Zhang 
77110fe4d17eSHong Zhang   Input Parameter:
77120fe4d17eSHong Zhang .  ts - timestepping context
77130fe4d17eSHong Zhang 
77140fe4d17eSHong Zhang   Output Parameter:
77150fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
77160fe4d17eSHong Zhang 
77170fe4d17eSHong Zhang   Level: intermediate
77180fe4d17eSHong Zhang 
77190fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
77200fe4d17eSHong Zhang @*/
77210fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
77220fe4d17eSHong Zhang {
77230fe4d17eSHong Zhang   PetscFunctionBegin;
77240fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77250fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
77260fe4d17eSHong Zhang   PetscFunctionReturn(0);
77270fe4d17eSHong Zhang }
7728