xref: /petsc/src/ts/interface/ts.c (revision d60b7d5cda9a3148fab971d2544cf20a2ffedfa6)
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) {
1973923b477SBarry Smith     PetscInt       howoften = 1;
198e669de00SBarry Smith     DM             dm;
199e669de00SBarry Smith     PetscBool      net;
200b3603a34SBarry Smith 
2010298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
202e669de00SBarry Smith     ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
203e669de00SBarry Smith     ierr = PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net);CHKERRQ(ierr);
204e669de00SBarry Smith     if (net) {
205e669de00SBarry Smith       TSMonitorLGCtxNetwork ctx;
206e669de00SBarry Smith       ierr = TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
207e669de00SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy);CHKERRQ(ierr);
208e669de00SBarry Smith       ierr = PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL);CHKERRQ(ierr);
209e669de00SBarry Smith     } else {
210e669de00SBarry Smith       TSMonitorLGCtx ctx;
211c793f718SLisandro Dalcin       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2124f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
213bdad233fSMatthew Knepley     }
214e669de00SBarry Smith   }
2156ba87a44SLisandro Dalcin 
2164f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
217ef20d060SBarry Smith   if (opt) {
2180b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2193923b477SBarry Smith     PetscInt       howoften = 1;
220ef20d060SBarry Smith 
2210298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
222c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2234f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
224ef20d060SBarry Smith   }
225edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2267cf37e64SBarry Smith 
2276934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2286934998bSLisandro Dalcin   if (opt) {
2296934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2306934998bSLisandro Dalcin     PetscInt       howoften = 1;
2316934998bSLisandro Dalcin 
2326934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2336ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2346934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2356934998bSLisandro Dalcin   }
2368b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2378b668821SLisandro Dalcin   if (opt) {
2388b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2398b668821SLisandro Dalcin     PetscInt       howoften = 1;
2408b668821SLisandro Dalcin 
2418b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2428b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2438b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2448b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2458b668821SLisandro Dalcin   }
2468b668821SLisandro Dalcin 
247201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
248201da799SBarry Smith   if (opt) {
249201da799SBarry Smith     TSMonitorLGCtx ctx;
250201da799SBarry Smith     PetscInt       howoften = 1;
251201da799SBarry Smith 
2520298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2536ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
254201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
255201da799SBarry Smith   }
256201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
257201da799SBarry Smith   if (opt) {
258201da799SBarry Smith     TSMonitorLGCtx ctx;
259201da799SBarry Smith     PetscInt       howoften = 1;
260201da799SBarry Smith 
2610298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2626ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
263201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
264201da799SBarry Smith   }
2658189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2668189c53fSBarry Smith   if (opt) {
2678189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2688189c53fSBarry Smith     PetscInt          howoften = 1;
2698189c53fSBarry Smith 
2700298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
271c793f718SLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2728189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2738189c53fSBarry Smith   }
2740ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2751b575b74SJoseph Pusztay   if (opt) {
2761b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2771b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2780ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2791b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2800ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2811b575b74SJoseph Pusztay   }
282ef20d060SBarry Smith   opt  = PETSC_FALSE;
2830dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
284a7cc72afSBarry Smith   if (opt) {
28583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28683a4ac43SBarry Smith     PetscInt         howoften = 1;
287a80ad3e0SBarry Smith 
2880298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
289c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
29083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
291bdad233fSMatthew Knepley   }
292fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2932d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2942d5ee99bSBarry Smith   if (opt) {
2952d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2962d5ee99bSBarry Smith     PetscReal        bounds[4];
2972d5ee99bSBarry Smith     PetscInt         n = 4;
2982d5ee99bSBarry Smith     PetscDraw        draw;
2996934998bSLisandro Dalcin     PetscDrawAxis    axis;
3002d5ee99bSBarry Smith 
3012d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3022d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
303c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3042d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3056934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3066934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3076934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3082d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3092d5ee99bSBarry Smith   }
3102d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3110dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3123a471f94SBarry Smith   if (opt) {
31383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
31483a4ac43SBarry Smith     PetscInt         howoften = 1;
3153a471f94SBarry Smith 
3160298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
317c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
31883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3193a471f94SBarry Smith   }
3200ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3210ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3220ed3bfb6SBarry Smith   if (opt) {
3230ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3240ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3250ed3bfb6SBarry Smith 
3260ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
327c793f718SLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3280ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3290ed3bfb6SBarry Smith   }
330fde5950dSBarry Smith 
331ed81e22dSJed Brown   opt  = PETSC_FALSE;
332589a23caSBarry 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);
333ed81e22dSJed Brown   if (flg) {
334ed81e22dSJed Brown     const char *ptr,*ptr2;
335ed81e22dSJed Brown     char       *filetemplate;
336ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
337ed81e22dSJed Brown     /* Do some cursory validation of the input. */
338ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
339ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
340ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
341ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
342ce94432eSBarry 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");
343ed81e22dSJed Brown       if (ptr2) break;
344ed81e22dSJed Brown     }
345ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
346ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
347ed81e22dSJed Brown   }
348bdad233fSMatthew Knepley 
349589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
350d1212d36SBarry Smith   if (flg) {
351d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
352d1212d36SBarry Smith     int                  ray = 0;
3533ee9839eSMatthew G. Knepley     DMDirection          ddir;
354d1212d36SBarry Smith     DM                   da;
355d1212d36SBarry Smith     PetscMPIInt          rank;
356d1212d36SBarry Smith 
357ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
3583ee9839eSMatthew G. Knepley     if (dir[0] == 'x') ddir = DM_X;
3593ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
360ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
361d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
362d1212d36SBarry Smith 
3635bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr);
364b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
365d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
366d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
367ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
368d1212d36SBarry Smith     if (!rank) {
369c793f718SLisandro Dalcin       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
370d1212d36SBarry Smith     }
37151b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
372d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
373d1212d36SBarry Smith   }
374589a23caSBarry Smith   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg);CHKERRQ(ierr);
37551b4a12fSMatthew G. Knepley   if (flg) {
37651b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37751b4a12fSMatthew G. Knepley     int                 ray = 0;
3783ee9839eSMatthew G. Knepley     DMDirection         ddir;
37951b4a12fSMatthew G. Knepley     DM                  da;
38051b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
381d1212d36SBarry Smith 
38251b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
3833ee9839eSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DM_X;
3843ee9839eSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DM_Y;
38551b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
38651b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3871c3436cfSJed Brown 
3885bd1e576SStefano Zampini     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr);
389b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
39051b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
39151b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
392c793f718SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
39351b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
39451b4a12fSMatthew G. Knepley   }
395a7a1495cSBarry Smith 
396b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
397b3d3934dSBarry Smith   if (opt) {
398b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
399b3d3934dSBarry Smith 
400b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
401b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
402b3d3934dSBarry Smith   }
403b3d3934dSBarry Smith 
404847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
405847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
406847ff0e1SMatthew G. Knepley   if (flg) {
407847ff0e1SMatthew G. Knepley     DM   dm;
408847ff0e1SMatthew G. Knepley     DMTS tdm;
409847ff0e1SMatthew G. Knepley 
410847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
411847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
412847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
413c793f718SLisandro Dalcin     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL);CHKERRQ(ierr);
414847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
415847ff0e1SMatthew G. Knepley   }
416847ff0e1SMatthew G. Knepley 
417d763cef2SBarry Smith   /* Handle specific TS options */
418d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4196991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
420d763cef2SBarry Smith   }
421fbc52257SHong Zhang 
422a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
423a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
424a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
425a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
426a7bdc993SLisandro Dalcin 
42768bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4284f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
42968bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
43068bece0bSHong Zhang   if (tflg) {
43168bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43268bece0bSHong Zhang   }
433a05bf03eSHong Zhang 
434a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
435d763cef2SBarry Smith 
436d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4370633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
438fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
439d763cef2SBarry Smith 
4404f122a70SLisandro Dalcin   if (ts->trajectory) {
4414f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
442d763cef2SBarry Smith   }
44368bece0bSHong Zhang 
4441ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4454f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4461ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4471ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4481ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4491ef27442SStefano Zampini   }
4504f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
451d763cef2SBarry Smith   PetscFunctionReturn(0);
452d763cef2SBarry Smith }
453d763cef2SBarry Smith 
454d2daff3dSHong Zhang /*@
45578fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
45678fbdcc8SBarry Smith 
45778fbdcc8SBarry Smith    Collective on TS
45878fbdcc8SBarry Smith 
45978fbdcc8SBarry Smith    Input Parameters:
46078fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
46178fbdcc8SBarry Smith 
4627a7aea1fSJed Brown    Output Parameters:
46378fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
46478fbdcc8SBarry Smith 
46578fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
46678fbdcc8SBarry Smith 
46778fbdcc8SBarry Smith    Level: advanced
46878fbdcc8SBarry Smith 
46978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
47078fbdcc8SBarry Smith 
47178fbdcc8SBarry Smith @*/
47278fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
47378fbdcc8SBarry Smith {
47478fbdcc8SBarry Smith   PetscFunctionBegin;
47578fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47678fbdcc8SBarry Smith   *tr = ts->trajectory;
47778fbdcc8SBarry Smith   PetscFunctionReturn(0);
47878fbdcc8SBarry Smith }
47978fbdcc8SBarry Smith 
48078fbdcc8SBarry Smith /*@
481bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
482d2daff3dSHong Zhang 
483d2daff3dSHong Zhang    Collective on TS
484d2daff3dSHong Zhang 
485d2daff3dSHong Zhang    Input Parameters:
486bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
487bc952696SBarry Smith 
48878fbdcc8SBarry Smith    Options Database:
48978fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
49078fbdcc8SBarry Smith -  -ts_trajectory_type type
49178fbdcc8SBarry Smith 
49268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
493d2daff3dSHong Zhang 
494c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
49578fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
49678fbdcc8SBarry Smith 
497d2daff3dSHong Zhang    Level: intermediate
498d2daff3dSHong Zhang 
4992d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
500d2daff3dSHong Zhang 
501d2daff3dSHong Zhang @*/
502bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
503d2daff3dSHong Zhang {
504bc952696SBarry Smith   PetscErrorCode ierr;
505bc952696SBarry Smith 
506d2daff3dSHong Zhang   PetscFunctionBegin;
507d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
508bc952696SBarry Smith   if (!ts->trajectory) {
509bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
510bc952696SBarry Smith   }
511d2daff3dSHong Zhang   PetscFunctionReturn(0);
512d2daff3dSHong Zhang }
513d2daff3dSHong Zhang 
514a7a1495cSBarry Smith /*@
5152d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
5162d29f1f2SStefano Zampini 
5172d29f1f2SStefano Zampini    Collective on TS
5182d29f1f2SStefano Zampini 
5192d29f1f2SStefano Zampini    Input Parameters:
5202d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5212d29f1f2SStefano Zampini 
5222d29f1f2SStefano Zampini    Level: intermediate
5232d29f1f2SStefano Zampini 
5242d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5252d29f1f2SStefano Zampini 
5262d29f1f2SStefano Zampini @*/
5272d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5282d29f1f2SStefano Zampini {
5292d29f1f2SStefano Zampini   PetscErrorCode ierr;
5302d29f1f2SStefano Zampini 
5312d29f1f2SStefano Zampini   PetscFunctionBegin;
5322d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5332d29f1f2SStefano Zampini   if (ts->trajectory) {
5342d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5352d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5362d29f1f2SStefano Zampini   }
5372d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5382d29f1f2SStefano Zampini }
5392d29f1f2SStefano Zampini 
5402d29f1f2SStefano Zampini /*@
541a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
542a7a1495cSBarry Smith       set with TSSetRHSJacobian().
543a7a1495cSBarry Smith 
544d083f849SBarry Smith    Collective on TS
545a7a1495cSBarry Smith 
546a7a1495cSBarry Smith    Input Parameters:
547316643e7SJed Brown +  ts - the TS context
548a7a1495cSBarry Smith .  t - current timestep
5490910c330SBarry Smith -  U - input vector
550a7a1495cSBarry Smith 
551a7a1495cSBarry Smith    Output Parameters:
552a7a1495cSBarry Smith +  A - Jacobian matrix
553a7a1495cSBarry Smith .  B - optional preconditioning matrix
554a7a1495cSBarry Smith -  flag - flag indicating matrix structure
555a7a1495cSBarry Smith 
556a7a1495cSBarry Smith    Notes:
557a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
558a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
559a7a1495cSBarry Smith 
56094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
561a7a1495cSBarry Smith    flag parameter.
562a7a1495cSBarry Smith 
563a7a1495cSBarry Smith    Level: developer
564a7a1495cSBarry Smith 
56594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
566a7a1495cSBarry Smith @*/
567d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
568a7a1495cSBarry Smith {
569dfbe8321SBarry Smith   PetscErrorCode   ierr;
570270bf2e7SJed Brown   PetscObjectState Ustate;
5716c1e1eecSBarry Smith   PetscObjectId    Uid;
57224989b8cSPeter Brune   DM               dm;
573942e3340SBarry Smith   DMTS             tsdm;
57424989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
57524989b8cSPeter Brune   void             *ctx;
576b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
577a7a1495cSBarry Smith 
578a7a1495cSBarry Smith   PetscFunctionBegin;
5790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5800910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5810910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
58224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
583942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
5846374d434SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
5852663174eSHong Zhang   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
58659e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5876c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
588971015bcSStefano Zampini 
5892663174eSHong Zhang   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0);
590d90be118SSean Farley 
591ae692cf2SHong Zhang   if (ts->rhsjacobian.shift && ts->rhsjacobian.reuse) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Should not call TSComputeRHSJacobian() on a shifted matrix (shift=%lf) when RHSJacobian is reusable.",ts->rhsjacobian.shift);
59224989b8cSPeter Brune   if (rhsjacobianfunc) {
59394ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
594a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
595d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
596a7a1495cSBarry Smith     PetscStackPop;
59794ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
598ef66eb69SBarry Smith   } else {
59994ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
600971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
601ef66eb69SBarry Smith   }
6020e4ef248SJed Brown   ts->rhsjacobian.time  = t;
603971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
604971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6057f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
60659e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
607a7a1495cSBarry Smith   PetscFunctionReturn(0);
608a7a1495cSBarry Smith }
609a7a1495cSBarry Smith 
610316643e7SJed Brown /*@
611d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
612d763cef2SBarry Smith 
613d083f849SBarry Smith    Collective on TS
614316643e7SJed Brown 
615316643e7SJed Brown    Input Parameters:
616316643e7SJed Brown +  ts - the TS context
617316643e7SJed Brown .  t - current time
6180910c330SBarry Smith -  U - state vector
619316643e7SJed Brown 
620316643e7SJed Brown    Output Parameter:
621316643e7SJed Brown .  y - right hand side
622316643e7SJed Brown 
623316643e7SJed Brown    Note:
624316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
625316643e7SJed Brown    is used internally within the nonlinear solvers.
626316643e7SJed Brown 
627316643e7SJed Brown    Level: developer
628316643e7SJed Brown 
629316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
630316643e7SJed Brown @*/
6310910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
632d763cef2SBarry Smith {
633dfbe8321SBarry Smith   PetscErrorCode ierr;
63424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
63524989b8cSPeter Brune   TSIFunction    ifunction;
63624989b8cSPeter Brune   void           *ctx;
63724989b8cSPeter Brune   DM             dm;
63824989b8cSPeter Brune 
639d763cef2SBarry Smith   PetscFunctionBegin;
6400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6410910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6420700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
64324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
64424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6450298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
646d763cef2SBarry Smith 
647ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
648d763cef2SBarry Smith 
6490910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
65024989b8cSPeter Brune   if (rhsfunction) {
6511be370b1SHong Zhang     ierr = VecLockReadPush(U);CHKERRQ(ierr);
652d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6530910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
654d763cef2SBarry Smith     PetscStackPop;
6551be370b1SHong Zhang     ierr = VecLockReadPop(U);CHKERRQ(ierr);
656214bc6a2SJed Brown   } else {
657214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
658b2cd27e8SJed Brown   }
65944a41b28SSean Farley 
6600910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
661d763cef2SBarry Smith   PetscFunctionReturn(0);
662d763cef2SBarry Smith }
663d763cef2SBarry Smith 
664ef20d060SBarry Smith /*@
665ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
666ef20d060SBarry Smith 
667d083f849SBarry Smith    Collective on TS
668ef20d060SBarry Smith 
669ef20d060SBarry Smith    Input Parameters:
670ef20d060SBarry Smith +  ts - the TS context
671ef20d060SBarry Smith -  t - current time
672ef20d060SBarry Smith 
673ef20d060SBarry Smith    Output Parameter:
6740910c330SBarry Smith .  U - the solution
675ef20d060SBarry Smith 
676ef20d060SBarry Smith    Note:
677ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
678ef20d060SBarry Smith    is used internally within the nonlinear solvers.
679ef20d060SBarry Smith 
680ef20d060SBarry Smith    Level: developer
681ef20d060SBarry Smith 
682abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
683ef20d060SBarry Smith @*/
6840910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
685ef20d060SBarry Smith {
686ef20d060SBarry Smith   PetscErrorCode     ierr;
687ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
688ef20d060SBarry Smith   void               *ctx;
689ef20d060SBarry Smith   DM                 dm;
690ef20d060SBarry Smith 
691ef20d060SBarry Smith   PetscFunctionBegin;
692ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6930910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
694ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
695ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
696ef20d060SBarry Smith 
697ef20d060SBarry Smith   if (solutionfunction) {
6989b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6990910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
700ef20d060SBarry Smith     PetscStackPop;
701ef20d060SBarry Smith   }
702ef20d060SBarry Smith   PetscFunctionReturn(0);
703ef20d060SBarry Smith }
7049b7cd975SBarry Smith /*@
7059b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7069b7cd975SBarry Smith 
707d083f849SBarry Smith    Collective on TS
7089b7cd975SBarry Smith 
7099b7cd975SBarry Smith    Input Parameters:
7109b7cd975SBarry Smith +  ts - the TS context
7119b7cd975SBarry Smith -  t - current time
7129b7cd975SBarry Smith 
7139b7cd975SBarry Smith    Output Parameter:
7149b7cd975SBarry Smith .  U - the function value
7159b7cd975SBarry Smith 
7169b7cd975SBarry Smith    Note:
7179b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7189b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7199b7cd975SBarry Smith 
7209b7cd975SBarry Smith    Level: developer
7219b7cd975SBarry Smith 
7229b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7239b7cd975SBarry Smith @*/
7249b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7259b7cd975SBarry Smith {
7269b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7279b7cd975SBarry Smith   void               *ctx;
7289b7cd975SBarry Smith   DM                 dm;
7299b7cd975SBarry Smith 
7309b7cd975SBarry Smith   PetscFunctionBegin;
7319b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7329b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7339b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7349b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7359b7cd975SBarry Smith 
7369b7cd975SBarry Smith   if (forcing) {
7379b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7389b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7399b7cd975SBarry Smith     PetscStackPop;
7409b7cd975SBarry Smith   }
7419b7cd975SBarry Smith   PetscFunctionReturn(0);
7429b7cd975SBarry Smith }
743ef20d060SBarry Smith 
744214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
745214bc6a2SJed Brown {
7462dd45cf8SJed Brown   Vec            F;
747214bc6a2SJed Brown   PetscErrorCode ierr;
748214bc6a2SJed Brown 
749214bc6a2SJed Brown   PetscFunctionBegin;
7500298fd71SBarry Smith   *Frhs = NULL;
7510298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
752214bc6a2SJed Brown   if (!ts->Frhs) {
7532dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
754214bc6a2SJed Brown   }
755214bc6a2SJed Brown   *Frhs = ts->Frhs;
756214bc6a2SJed Brown   PetscFunctionReturn(0);
757214bc6a2SJed Brown }
758214bc6a2SJed Brown 
759971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
760214bc6a2SJed Brown {
761214bc6a2SJed Brown   Mat            A,B;
7622dd45cf8SJed Brown   PetscErrorCode ierr;
76341a1d4d2SBarry Smith   TSIJacobian    ijacobian;
764214bc6a2SJed Brown 
765214bc6a2SJed Brown   PetscFunctionBegin;
766c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
767c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
76841a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
769214bc6a2SJed Brown   if (Arhs) {
770214bc6a2SJed Brown     if (!ts->Arhs) {
77141a1d4d2SBarry Smith       if (ijacobian) {
772214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
773*d60b7d5cSBarry Smith         ierr = TSSetMatStructure(ts,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
77441a1d4d2SBarry Smith       } else {
77541a1d4d2SBarry Smith         ts->Arhs = A;
77641a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
77741a1d4d2SBarry Smith       }
7783565c898SBarry Smith     } else {
7793565c898SBarry Smith       PetscBool flg;
7803565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7813565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7823565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7833565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7843565c898SBarry Smith         ts->Arhs = A;
7853565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7863565c898SBarry Smith       }
787214bc6a2SJed Brown     }
788214bc6a2SJed Brown     *Arhs = ts->Arhs;
789214bc6a2SJed Brown   }
790214bc6a2SJed Brown   if (Brhs) {
791214bc6a2SJed Brown     if (!ts->Brhs) {
792bdb70873SJed Brown       if (A != B) {
79341a1d4d2SBarry Smith         if (ijacobian) {
794214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
795bdb70873SJed Brown         } else {
79641a1d4d2SBarry Smith           ts->Brhs = B;
79741a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
79841a1d4d2SBarry Smith         }
79941a1d4d2SBarry Smith       } else {
800bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
80151699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
802bdb70873SJed Brown       }
803214bc6a2SJed Brown     }
804214bc6a2SJed Brown     *Brhs = ts->Brhs;
805214bc6a2SJed Brown   }
806214bc6a2SJed Brown   PetscFunctionReturn(0);
807214bc6a2SJed Brown }
808214bc6a2SJed Brown 
809316643e7SJed Brown /*@
8100910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
811316643e7SJed Brown 
812d083f849SBarry Smith    Collective on TS
813316643e7SJed Brown 
814316643e7SJed Brown    Input Parameters:
815316643e7SJed Brown +  ts - the TS context
816316643e7SJed Brown .  t - current time
8170910c330SBarry Smith .  U - state vector
8180910c330SBarry Smith .  Udot - time derivative of state vector
819214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
820316643e7SJed Brown 
821316643e7SJed Brown    Output Parameter:
822316643e7SJed Brown .  Y - right hand side
823316643e7SJed Brown 
824316643e7SJed Brown    Note:
825316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
826316643e7SJed Brown    is used internally within the nonlinear solvers.
827316643e7SJed Brown 
828316643e7SJed Brown    If the user did did not write their equations in implicit form, this
829316643e7SJed Brown    function recasts them in implicit form.
830316643e7SJed Brown 
831316643e7SJed Brown    Level: developer
832316643e7SJed Brown 
833316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
834316643e7SJed Brown @*/
8350910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
836316643e7SJed Brown {
837316643e7SJed Brown   PetscErrorCode ierr;
83824989b8cSPeter Brune   TSIFunction    ifunction;
83924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
84024989b8cSPeter Brune   void           *ctx;
84124989b8cSPeter Brune   DM             dm;
842316643e7SJed Brown 
843316643e7SJed Brown   PetscFunctionBegin;
8440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8450910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8460910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8470700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
848316643e7SJed Brown 
84924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
85024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8510298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
85224989b8cSPeter Brune 
853ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
854d90be118SSean Farley 
8550910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
85624989b8cSPeter Brune   if (ifunction) {
857316643e7SJed Brown     PetscStackPush("TS user implicit function");
8580910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
859316643e7SJed Brown     PetscStackPop;
860214bc6a2SJed Brown   }
861214bc6a2SJed Brown   if (imex) {
86224989b8cSPeter Brune     if (!ifunction) {
8630910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8642dd45cf8SJed Brown     }
86524989b8cSPeter Brune   } else if (rhsfunction) {
86624989b8cSPeter Brune     if (ifunction) {
867214bc6a2SJed Brown       Vec Frhs;
868214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8690910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
870214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8712dd45cf8SJed Brown     } else {
8720910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8730910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
874316643e7SJed Brown     }
8754a6899ffSJed Brown   }
8760910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
877316643e7SJed Brown   PetscFunctionReturn(0);
878316643e7SJed Brown }
879316643e7SJed Brown 
880cfa8a9a2SHong Zhang /*
881cfa8a9a2SHong Zhang    TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it.
882cfa8a9a2SHong Zhang 
883cfa8a9a2SHong Zhang    Note:
884cfa8a9a2SHong Zhang    This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian().
885cfa8a9a2SHong Zhang 
886cfa8a9a2SHong Zhang */
887cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B)
888cfa8a9a2SHong Zhang {
889cfa8a9a2SHong Zhang   PetscErrorCode   ierr;
890cfa8a9a2SHong Zhang 
891cfa8a9a2SHong Zhang   PetscFunctionBegin;
892cfa8a9a2SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
89303c97d52SHong Zhang   if (A != ts->Arhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat");
89403c97d52SHong Zhang   if (B != ts->Brhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat");
895cfa8a9a2SHong Zhang 
896cfa8a9a2SHong Zhang   if (ts->rhsjacobian.shift) {
897cfa8a9a2SHong Zhang     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
898cfa8a9a2SHong Zhang   }
899cfa8a9a2SHong Zhang   if (ts->rhsjacobian.scale == -1.) {
900cfa8a9a2SHong Zhang     ierr = MatScale(A,-1);CHKERRQ(ierr);
901cfa8a9a2SHong Zhang   }
902cfa8a9a2SHong Zhang   if (B && B == ts->Brhs && A != B) {
903cfa8a9a2SHong Zhang     if (ts->rhsjacobian.shift) {
904cfa8a9a2SHong Zhang       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
905cfa8a9a2SHong Zhang     }
906cfa8a9a2SHong Zhang     if (ts->rhsjacobian.scale == -1.) {
907cfa8a9a2SHong Zhang       ierr = MatScale(B,-1);CHKERRQ(ierr);
908cfa8a9a2SHong Zhang     }
909cfa8a9a2SHong Zhang   }
910cfa8a9a2SHong Zhang   ts->rhsjacobian.shift = 0;
911cfa8a9a2SHong Zhang   ts->rhsjacobian.scale = 1.;
912cfa8a9a2SHong Zhang   PetscFunctionReturn(0);
913cfa8a9a2SHong Zhang }
914cfa8a9a2SHong Zhang 
915316643e7SJed Brown /*@
916316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
917316643e7SJed Brown 
918d083f849SBarry Smith    Collective on TS
919316643e7SJed Brown 
920316643e7SJed Brown    Input
921316643e7SJed Brown       Input Parameters:
922316643e7SJed Brown +  ts - the TS context
923316643e7SJed Brown .  t - current timestep
9240910c330SBarry Smith .  U - state vector
9250910c330SBarry Smith .  Udot - time derivative of state vector
926214bc6a2SJed Brown .  shift - shift to apply, see note below
927214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
928316643e7SJed Brown 
929316643e7SJed Brown    Output Parameters:
930316643e7SJed Brown +  A - Jacobian matrix
9313565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
932316643e7SJed Brown 
933316643e7SJed Brown    Notes:
9340910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
935316643e7SJed Brown 
9360910c330SBarry Smith    dF/dU + shift*dF/dUdot
937316643e7SJed Brown 
938316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
939316643e7SJed Brown    is used internally within the nonlinear solvers.
940316643e7SJed Brown 
941316643e7SJed Brown    Level: developer
942316643e7SJed Brown 
943316643e7SJed Brown .seealso:  TSSetIJacobian()
94463495f91SJed Brown @*/
945d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
946316643e7SJed Brown {
947316643e7SJed Brown   PetscErrorCode ierr;
94824989b8cSPeter Brune   TSIJacobian    ijacobian;
94924989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
95024989b8cSPeter Brune   DM             dm;
95124989b8cSPeter Brune   void           *ctx;
952316643e7SJed Brown 
953316643e7SJed Brown   PetscFunctionBegin;
9540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9550910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9560910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
957316643e7SJed Brown   PetscValidPointer(A,6);
95894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
959316643e7SJed Brown   PetscValidPointer(B,7);
96094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
96124989b8cSPeter Brune 
96224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9640298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96524989b8cSPeter Brune 
966ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
967316643e7SJed Brown 
96894ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
96924989b8cSPeter Brune   if (ijacobian) {
970316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
971d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
972316643e7SJed Brown     PetscStackPop;
9734a6899ffSJed Brown   }
974214bc6a2SJed Brown   if (imex) {
975b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9764c26be97Sstefano_zampini       PetscBool assembled;
977971015bcSStefano Zampini       if (rhsjacobian) {
978971015bcSStefano Zampini         Mat Arhs = NULL;
979971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
980971015bcSStefano Zampini         if (A == Arhs) {
981e8b1e424SHong 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 */
982971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
983971015bcSStefano Zampini         }
984971015bcSStefano Zampini       }
98594ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9864c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9874c26be97Sstefano_zampini       if (!assembled) {
9884c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9894c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9904c26be97Sstefano_zampini       }
99194ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99294ab13aaSBarry Smith       if (A != B) {
99394ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9944c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9954c26be97Sstefano_zampini         if (!assembled) {
9964c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9974c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9984c26be97Sstefano_zampini         }
99994ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1000214bc6a2SJed Brown       }
1001214bc6a2SJed Brown     }
1002214bc6a2SJed Brown   } else {
1003e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1004e8b1e424SHong Zhang     if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */
1005214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1006e1244c69SJed Brown     }
1007e8b1e424SHong Zhang     if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */
1008e8b1e424SHong Zhang       PetscObjectState Ustate;
1009e8b1e424SHong Zhang       PetscObjectId    Uid;
1010e8b1e424SHong Zhang       TSRHSFunction    rhsfunction;
1011e8b1e424SHong Zhang 
1012e8b1e424SHong Zhang       ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1013e8b1e424SHong Zhang       ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
1014e8b1e424SHong Zhang       ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
1015097a96a2SHong Zhang       if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */
1016e8b1e424SHong 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 */
101734d322a1SHong Zhang         if (A != B) {
101834d322a1SHong Zhang           ierr = MatShift(B,shift-ts->rhsjacobian.shift);CHKERRQ(ierr);
101934d322a1SHong Zhang         }
1020e8b1e424SHong Zhang       } else {
10213565c898SBarry Smith         PetscBool flg;
1022e8b1e424SHong Zhang 
1023e8b1e424SHong Zhang         if (ts->rhsjacobian.reuse) { /* Undo the damage */
1024e8b1e424SHong Zhang           /* MatScale has a short path for this case.
1025e8b1e424SHong Zhang              However, this code path is taken the first time TSComputeRHSJacobian is called
1026e8b1e424SHong Zhang              and the matrices have not been assembled yet */
1027cfa8a9a2SHong Zhang           ierr = TSRecoverRHSJacobian(ts,A,B);CHKERRQ(ierr);
1028e8b1e424SHong Zhang         }
1029e8b1e424SHong Zhang         ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr);
10303565c898SBarry Smith         ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10313565c898SBarry Smith         /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10323565c898SBarry Smith         if (!flg) {
103394ab13aaSBarry Smith           ierr = MatScale(A,-1);CHKERRQ(ierr);
103494ab13aaSBarry Smith           ierr = MatShift(A,shift);CHKERRQ(ierr);
10353565c898SBarry Smith         }
103694ab13aaSBarry Smith         if (A != B) {
103794ab13aaSBarry Smith           ierr = MatScale(B,-1);CHKERRQ(ierr);
103894ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1039316643e7SJed Brown         }
1040e8b1e424SHong Zhang       }
1041e8b1e424SHong Zhang       ts->rhsjacobian.scale = -1;
1042e8b1e424SHong Zhang       ts->rhsjacobian.shift = shift;
1043*d60b7d5cSBarry Smith     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1044e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
104594ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
104694ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
104794ab13aaSBarry Smith         if (A != B) {
104894ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
104994ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1050214bc6a2SJed Brown         }
1051316643e7SJed Brown       }
1052e8b1e424SHong Zhang       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1053*d60b7d5cSBarry Smith       ierr = MatAXPY(A,-1,Arhs,ts->axpy_pattern);CHKERRQ(ierr);
105494ab13aaSBarry Smith       if (A != B) {
1055*d60b7d5cSBarry Smith         ierr = MatAXPY(B,-1,Brhs,ts->axpy_pattern);CHKERRQ(ierr);
1056316643e7SJed Brown       }
1057316643e7SJed Brown     }
1058316643e7SJed Brown   }
105994ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1060316643e7SJed Brown   PetscFunctionReturn(0);
1061316643e7SJed Brown }
1062316643e7SJed Brown 
1063d763cef2SBarry Smith /*@C
1064d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1065b5abc632SBarry Smith     where U_t = G(t,u).
1066d763cef2SBarry Smith 
10673f9fe445SBarry Smith     Logically Collective on TS
1068d763cef2SBarry Smith 
1069d763cef2SBarry Smith     Input Parameters:
1070d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10710298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1072d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1073d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10740298fd71SBarry Smith           function evaluation routine (may be NULL)
1075d763cef2SBarry Smith 
1076a96d6ef6SBarry Smith     Calling sequence of f:
1077a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1078d763cef2SBarry Smith 
1079a96d6ef6SBarry Smith +   ts - timestep context
1080a96d6ef6SBarry Smith .   t - current timestep
1081d763cef2SBarry Smith .   u - input vector
1082d763cef2SBarry Smith .   F - function vector
1083d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1084d763cef2SBarry Smith 
1085d763cef2SBarry Smith     Level: beginner
1086d763cef2SBarry Smith 
108795452b02SPatrick Sanan     Notes:
108895452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10892bbac0d3SBarry Smith 
1090ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1091d763cef2SBarry Smith @*/
1092089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1093d763cef2SBarry Smith {
1094089b2837SJed Brown   PetscErrorCode ierr;
1095089b2837SJed Brown   SNES           snes;
10960298fd71SBarry Smith   Vec            ralloc = NULL;
109724989b8cSPeter Brune   DM             dm;
1098d763cef2SBarry Smith 
1099089b2837SJed Brown   PetscFunctionBegin;
11000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1101ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
110224989b8cSPeter Brune 
110324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110424989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1105089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1106e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1107e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1108e856ceecSJed Brown     r = ralloc;
1109e856ceecSJed Brown   }
1110089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1111e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1112d763cef2SBarry Smith   PetscFunctionReturn(0);
1113d763cef2SBarry Smith }
1114d763cef2SBarry Smith 
1115ef20d060SBarry Smith /*@C
1116abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1117ef20d060SBarry Smith 
1118ef20d060SBarry Smith     Logically Collective on TS
1119ef20d060SBarry Smith 
1120ef20d060SBarry Smith     Input Parameters:
1121ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1122ef20d060SBarry Smith .   f - routine for evaluating the solution
1123ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11240298fd71SBarry Smith           function evaluation routine (may be NULL)
1125ef20d060SBarry Smith 
1126a96d6ef6SBarry Smith     Calling sequence of f:
1127a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
1128ef20d060SBarry Smith 
1129ef20d060SBarry Smith +   t - current timestep
1130ef20d060SBarry Smith .   u - output vector
1131ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1132ef20d060SBarry Smith 
11330ed3bfb6SBarry Smith     Options Database:
11340ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11350ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11360ed3bfb6SBarry Smith 
1137abd5a294SJed Brown     Notes:
1138abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1139abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1140abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1141abd5a294SJed Brown 
11424f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1143abd5a294SJed Brown 
1144ef20d060SBarry Smith     Level: beginner
1145ef20d060SBarry Smith 
11460ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1147ef20d060SBarry Smith @*/
1148ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1149ef20d060SBarry Smith {
1150ef20d060SBarry Smith   PetscErrorCode ierr;
1151ef20d060SBarry Smith   DM             dm;
1152ef20d060SBarry Smith 
1153ef20d060SBarry Smith   PetscFunctionBegin;
1154ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1155ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1156ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1157ef20d060SBarry Smith   PetscFunctionReturn(0);
1158ef20d060SBarry Smith }
1159ef20d060SBarry Smith 
11609b7cd975SBarry Smith /*@C
11619b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11629b7cd975SBarry Smith 
11639b7cd975SBarry Smith     Logically Collective on TS
11649b7cd975SBarry Smith 
11659b7cd975SBarry Smith     Input Parameters:
11669b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1167e162b725SBarry Smith .   func - routine for evaluating the forcing function
11689b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11690298fd71SBarry Smith           function evaluation routine (may be NULL)
11709b7cd975SBarry Smith 
11719b7cd975SBarry Smith     Calling sequence of func:
11726bc98fa9SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
11739b7cd975SBarry Smith 
11749b7cd975SBarry Smith +   t - current timestep
1175e162b725SBarry Smith .   f - output vector
11769b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11779b7cd975SBarry Smith 
11789b7cd975SBarry Smith     Notes:
11799b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1180e162b725SBarry 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
1181e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1182e162b725SBarry Smith 
1183e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1184e162b725SBarry Smith 
1185e162b725SBarry 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
1186e162b725SBarry Smith     parameters can be passed in the ctx variable.
11879b7cd975SBarry Smith 
11889b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11899b7cd975SBarry Smith 
11909b7cd975SBarry Smith     Level: beginner
11919b7cd975SBarry Smith 
11929b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11939b7cd975SBarry Smith @*/
1194e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11959b7cd975SBarry Smith {
11969b7cd975SBarry Smith   PetscErrorCode ierr;
11979b7cd975SBarry Smith   DM             dm;
11989b7cd975SBarry Smith 
11999b7cd975SBarry Smith   PetscFunctionBegin;
12009b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12019b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1202e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
12039b7cd975SBarry Smith   PetscFunctionReturn(0);
12049b7cd975SBarry Smith }
12059b7cd975SBarry Smith 
1206d763cef2SBarry Smith /*@C
1207f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1208b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1209d763cef2SBarry Smith 
12103f9fe445SBarry Smith    Logically Collective on TS
1211d763cef2SBarry Smith 
1212d763cef2SBarry Smith    Input Parameters:
1213d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1214e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1215e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1216d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1217d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12180298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1219d763cef2SBarry Smith 
1220f7ab8db6SBarry Smith    Calling sequence of f:
1221a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1222d763cef2SBarry Smith 
1223d763cef2SBarry Smith +  t - current timestep
1224d763cef2SBarry Smith .  u - input vector
1225e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1226e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1227d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1228d763cef2SBarry Smith 
12296cd88445SBarry Smith    Notes:
12306cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12316cd88445SBarry Smith 
12326cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1233ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1234d763cef2SBarry Smith 
1235d763cef2SBarry Smith    Level: beginner
1236d763cef2SBarry Smith 
1237ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1238d763cef2SBarry Smith 
1239d763cef2SBarry Smith @*/
1240e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1241d763cef2SBarry Smith {
1242277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1243089b2837SJed Brown   SNES           snes;
124424989b8cSPeter Brune   DM             dm;
124524989b8cSPeter Brune   TSIJacobian    ijacobian;
1246277b19d0SLisandro Dalcin 
1247d763cef2SBarry Smith   PetscFunctionBegin;
12480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1249e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1250e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1251e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1252e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1253d763cef2SBarry Smith 
125424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
125524989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
12560298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1257089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12585f659677SPeter Brune   if (!ijacobian) {
1259e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12600e4ef248SJed Brown   }
1261e5d3d808SBarry Smith   if (Amat) {
1262e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12630e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1264e5d3d808SBarry Smith     ts->Arhs = Amat;
12650e4ef248SJed Brown   }
1266e5d3d808SBarry Smith   if (Pmat) {
1267e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12680e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1269e5d3d808SBarry Smith     ts->Brhs = Pmat;
12700e4ef248SJed Brown   }
1271d763cef2SBarry Smith   PetscFunctionReturn(0);
1272d763cef2SBarry Smith }
1273d763cef2SBarry Smith 
1274316643e7SJed Brown /*@C
1275b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1276316643e7SJed Brown 
12773f9fe445SBarry Smith    Logically Collective on TS
1278316643e7SJed Brown 
1279316643e7SJed Brown    Input Parameters:
1280316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12810298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1282316643e7SJed Brown .  f   - the function evaluation routine
12830298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1284316643e7SJed Brown 
1285316643e7SJed Brown    Calling sequence of f:
12866bc98fa9SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1287316643e7SJed Brown 
1288316643e7SJed Brown +  t   - time at step/stage being solved
1289316643e7SJed Brown .  u   - state vector
1290316643e7SJed Brown .  u_t - time derivative of state vector
1291316643e7SJed Brown .  F   - function vector
1292316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1293316643e7SJed Brown 
1294316643e7SJed Brown    Important:
12952bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1296316643e7SJed Brown 
1297316643e7SJed Brown    Level: beginner
1298316643e7SJed Brown 
1299d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1300316643e7SJed Brown @*/
130151699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1302316643e7SJed Brown {
1303089b2837SJed Brown   PetscErrorCode ierr;
1304089b2837SJed Brown   SNES           snes;
130551699248SLisandro Dalcin   Vec            ralloc = NULL;
130624989b8cSPeter Brune   DM             dm;
1307316643e7SJed Brown 
1308316643e7SJed Brown   PetscFunctionBegin;
13090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
131051699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
131124989b8cSPeter Brune 
131224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
131324989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
131424989b8cSPeter Brune 
1315089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
131651699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
131751699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
131851699248SLisandro Dalcin     r  = ralloc;
1319e856ceecSJed Brown   }
132051699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
132151699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1322089b2837SJed Brown   PetscFunctionReturn(0);
1323089b2837SJed Brown }
1324089b2837SJed Brown 
1325089b2837SJed Brown /*@C
1326089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1327089b2837SJed Brown 
1328089b2837SJed Brown    Not Collective
1329089b2837SJed Brown 
1330089b2837SJed Brown    Input Parameter:
1331089b2837SJed Brown .  ts - the TS context
1332089b2837SJed Brown 
1333089b2837SJed Brown    Output Parameter:
13340298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13350298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13360298fd71SBarry Smith -  ctx - the function context (or NULL)
1337089b2837SJed Brown 
1338089b2837SJed Brown    Level: advanced
1339089b2837SJed Brown 
1340089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1341089b2837SJed Brown @*/
1342089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1343089b2837SJed Brown {
1344089b2837SJed Brown   PetscErrorCode ierr;
1345089b2837SJed Brown   SNES           snes;
134624989b8cSPeter Brune   DM             dm;
1347089b2837SJed Brown 
1348089b2837SJed Brown   PetscFunctionBegin;
1349089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1350089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13510298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
135224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
135324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1354089b2837SJed Brown   PetscFunctionReturn(0);
1355089b2837SJed Brown }
1356089b2837SJed Brown 
1357089b2837SJed Brown /*@C
1358089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1359089b2837SJed Brown 
1360089b2837SJed Brown    Not Collective
1361089b2837SJed Brown 
1362089b2837SJed Brown    Input Parameter:
1363089b2837SJed Brown .  ts - the TS context
1364089b2837SJed Brown 
1365089b2837SJed Brown    Output Parameter:
13660298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13670298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13680298fd71SBarry Smith -  ctx - the function context (or NULL)
1369089b2837SJed Brown 
1370089b2837SJed Brown    Level: advanced
1371089b2837SJed Brown 
13722bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1373089b2837SJed Brown @*/
1374089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1375089b2837SJed Brown {
1376089b2837SJed Brown   PetscErrorCode ierr;
1377089b2837SJed Brown   SNES           snes;
137824989b8cSPeter Brune   DM             dm;
1379089b2837SJed Brown 
1380089b2837SJed Brown   PetscFunctionBegin;
1381089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1382089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13830298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
138424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
138524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1386316643e7SJed Brown   PetscFunctionReturn(0);
1387316643e7SJed Brown }
1388316643e7SJed Brown 
1389316643e7SJed Brown /*@C
1390a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1391ae8867d6SBarry Smith         provided with TSSetIFunction().
1392316643e7SJed Brown 
13933f9fe445SBarry Smith    Logically Collective on TS
1394316643e7SJed Brown 
1395316643e7SJed Brown    Input Parameters:
1396316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1397e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1398e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1399316643e7SJed Brown .  f   - the Jacobian evaluation routine
14000298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1401316643e7SJed Brown 
1402316643e7SJed Brown    Calling sequence of f:
14036bc98fa9SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1404316643e7SJed Brown 
1405316643e7SJed Brown +  t    - time at step/stage being solved
14061b4a444bSJed Brown .  U    - state vector
14071b4a444bSJed Brown .  U_t  - time derivative of state vector
1408316643e7SJed Brown .  a    - shift
1409e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1410e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1411316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1412316643e7SJed Brown 
1413316643e7SJed Brown    Notes:
1414e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1415316643e7SJed Brown 
1416895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1417895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1418895c21f2SBarry Smith 
1419a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1420b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1421a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1422a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1423a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1424a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1425a4f0a591SBarry Smith 
14266cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14276cd88445SBarry Smith 
14286cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1429ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1430ca5f011dSBarry Smith 
1431316643e7SJed Brown    Level: beginner
1432316643e7SJed Brown 
1433ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1434316643e7SJed Brown 
1435316643e7SJed Brown @*/
1436e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1437316643e7SJed Brown {
1438316643e7SJed Brown   PetscErrorCode ierr;
1439089b2837SJed Brown   SNES           snes;
144024989b8cSPeter Brune   DM             dm;
1441316643e7SJed Brown 
1442316643e7SJed Brown   PetscFunctionBegin;
14430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1444e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1445e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1446e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1447e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
144824989b8cSPeter Brune 
144924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
145024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
145124989b8cSPeter Brune 
1452089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1453e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1454316643e7SJed Brown   PetscFunctionReturn(0);
1455316643e7SJed Brown }
1456316643e7SJed Brown 
1457e1244c69SJed Brown /*@
1458e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1459e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1460e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1461e1244c69SJed Brown    not been changed by the TS.
1462e1244c69SJed Brown 
1463e1244c69SJed Brown    Logically Collective
1464e1244c69SJed Brown 
1465e1244c69SJed Brown    Input Arguments:
1466e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1467e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1468e1244c69SJed Brown 
1469e1244c69SJed Brown    Level: intermediate
1470e1244c69SJed Brown 
1471e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1472e1244c69SJed Brown @*/
1473e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1474e1244c69SJed Brown {
1475e1244c69SJed Brown   PetscFunctionBegin;
1476e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1477e1244c69SJed Brown   PetscFunctionReturn(0);
1478e1244c69SJed Brown }
1479e1244c69SJed Brown 
1480efe9872eSLisandro Dalcin /*@C
1481efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1482efe9872eSLisandro Dalcin 
1483efe9872eSLisandro Dalcin    Logically Collective on TS
1484efe9872eSLisandro Dalcin 
1485efe9872eSLisandro Dalcin    Input Parameters:
1486efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1487efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1488efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1489efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1490efe9872eSLisandro Dalcin 
1491efe9872eSLisandro Dalcin    Calling sequence of fun:
14926bc98fa9SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1493efe9872eSLisandro Dalcin 
1494efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1495efe9872eSLisandro Dalcin .  U    - state vector
1496efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1497efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1498efe9872eSLisandro Dalcin .  F    - function vector
1499efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1500efe9872eSLisandro Dalcin 
1501efe9872eSLisandro Dalcin    Level: beginner
1502efe9872eSLisandro Dalcin 
1503a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction()
1504efe9872eSLisandro Dalcin @*/
1505efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1506efe9872eSLisandro Dalcin {
1507efe9872eSLisandro Dalcin   DM             dm;
1508efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin   PetscFunctionBegin;
1511efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1512efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1513efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1514efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1515efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1516efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1517efe9872eSLisandro Dalcin }
1518efe9872eSLisandro Dalcin 
1519efe9872eSLisandro Dalcin /*@C
1520efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1521efe9872eSLisandro Dalcin 
1522efe9872eSLisandro Dalcin   Not Collective
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin   Input Parameter:
1525efe9872eSLisandro Dalcin . ts - the TS context
1526efe9872eSLisandro Dalcin 
1527efe9872eSLisandro Dalcin   Output Parameter:
1528efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1529efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1530efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin   Level: advanced
1533efe9872eSLisandro Dalcin 
1534a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate()
1535efe9872eSLisandro Dalcin @*/
1536efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1537efe9872eSLisandro Dalcin {
1538efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1539efe9872eSLisandro Dalcin   SNES           snes;
1540efe9872eSLisandro Dalcin   DM             dm;
1541efe9872eSLisandro Dalcin 
1542efe9872eSLisandro Dalcin   PetscFunctionBegin;
1543efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1544efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1545efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1546efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1547efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1548efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1549efe9872eSLisandro Dalcin }
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin /*@C
1552bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1553efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1554efe9872eSLisandro Dalcin 
1555efe9872eSLisandro Dalcin    Logically Collective on TS
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin    Input Parameters:
1558efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1559efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1560efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1561efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1562efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1563efe9872eSLisandro Dalcin 
1564efe9872eSLisandro Dalcin    Calling sequence of jac:
15656bc98fa9SBarry 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);
1566efe9872eSLisandro Dalcin 
1567efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1568efe9872eSLisandro Dalcin .  U    - state vector
1569efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1570efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1571efe9872eSLisandro Dalcin .  v    - shift for U_t
1572efe9872eSLisandro Dalcin .  a    - shift for U_tt
1573efe9872eSLisandro 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
1574efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1575efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1576efe9872eSLisandro Dalcin 
1577efe9872eSLisandro Dalcin    Notes:
1578efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1581efe9872eSLisandro 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.
1582efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1583bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1584efe9872eSLisandro Dalcin 
1585efe9872eSLisandro Dalcin    Level: beginner
1586efe9872eSLisandro Dalcin 
1587a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian()
1588efe9872eSLisandro Dalcin @*/
1589efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1590efe9872eSLisandro Dalcin {
1591efe9872eSLisandro Dalcin   DM             dm;
1592efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1593efe9872eSLisandro Dalcin 
1594efe9872eSLisandro Dalcin   PetscFunctionBegin;
1595efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1596efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1597efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1598efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1599efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1600efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1601efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1602efe9872eSLisandro Dalcin }
1603efe9872eSLisandro Dalcin 
1604efe9872eSLisandro Dalcin /*@C
1605efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1608efe9872eSLisandro Dalcin 
1609efe9872eSLisandro Dalcin   Input Parameter:
1610efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1611efe9872eSLisandro Dalcin 
1612efe9872eSLisandro Dalcin   Output Parameters:
1613efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1614efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1615efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1616efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1617efe9872eSLisandro Dalcin 
161895452b02SPatrick Sanan   Notes:
161995452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1620efe9872eSLisandro Dalcin 
1621efe9872eSLisandro Dalcin   Level: advanced
1622efe9872eSLisandro Dalcin 
1623a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate()
1624efe9872eSLisandro Dalcin 
1625efe9872eSLisandro Dalcin @*/
1626efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1627efe9872eSLisandro Dalcin {
1628efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1629efe9872eSLisandro Dalcin   SNES           snes;
1630efe9872eSLisandro Dalcin   DM             dm;
1631efe9872eSLisandro Dalcin 
1632efe9872eSLisandro Dalcin   PetscFunctionBegin;
1633efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1634efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1635efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1636efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1637efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1638efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1639efe9872eSLisandro Dalcin }
1640efe9872eSLisandro Dalcin 
1641efe9872eSLisandro Dalcin /*@
1642efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1643efe9872eSLisandro Dalcin 
1644d083f849SBarry Smith   Collective on TS
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   Input Parameters:
1647efe9872eSLisandro Dalcin + ts - the TS context
1648efe9872eSLisandro Dalcin . t - current time
1649efe9872eSLisandro Dalcin . U - state vector
1650efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1651efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   Output Parameter:
1654efe9872eSLisandro Dalcin . F - the residual vector
1655efe9872eSLisandro Dalcin 
1656efe9872eSLisandro Dalcin   Note:
1657efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1658efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1659efe9872eSLisandro Dalcin 
1660efe9872eSLisandro Dalcin   Level: developer
1661efe9872eSLisandro Dalcin 
1662a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function()
1663efe9872eSLisandro Dalcin @*/
1664efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1665efe9872eSLisandro Dalcin {
1666efe9872eSLisandro Dalcin   DM             dm;
1667efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1668efe9872eSLisandro Dalcin   void           *ctx;
1669efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1670efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1671efe9872eSLisandro Dalcin 
1672efe9872eSLisandro Dalcin   PetscFunctionBegin;
1673efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1674efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1675efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1676efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1677efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1680efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1681efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1682efe9872eSLisandro Dalcin 
1683efe9872eSLisandro Dalcin   if (!I2Function) {
1684efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1685efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1686efe9872eSLisandro Dalcin   }
1687efe9872eSLisandro Dalcin 
1688efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1691efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1692efe9872eSLisandro Dalcin   PetscStackPop;
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin   if (rhsfunction) {
1695efe9872eSLisandro Dalcin     Vec Frhs;
1696efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1697efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1698efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1699efe9872eSLisandro Dalcin   }
1700efe9872eSLisandro Dalcin 
1701efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1702efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1703efe9872eSLisandro Dalcin }
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin /*@
1706efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1707efe9872eSLisandro Dalcin 
1708d083f849SBarry Smith   Collective on TS
1709efe9872eSLisandro Dalcin 
1710efe9872eSLisandro Dalcin   Input Parameters:
1711efe9872eSLisandro Dalcin + ts - the TS context
1712efe9872eSLisandro Dalcin . t - current timestep
1713efe9872eSLisandro Dalcin . U - state vector
1714efe9872eSLisandro Dalcin . V - time derivative of state vector
1715efe9872eSLisandro Dalcin . A - second time derivative of state vector
1716efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1717efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1718efe9872eSLisandro Dalcin 
1719efe9872eSLisandro Dalcin   Output Parameters:
1720efe9872eSLisandro Dalcin + J - Jacobian matrix
1721efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1722efe9872eSLisandro Dalcin 
1723efe9872eSLisandro Dalcin   Notes:
1724efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1727efe9872eSLisandro Dalcin 
1728efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1729efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1730efe9872eSLisandro Dalcin 
1731efe9872eSLisandro Dalcin   Level: developer
1732efe9872eSLisandro Dalcin 
1733efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1734efe9872eSLisandro Dalcin @*/
1735efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1736efe9872eSLisandro Dalcin {
1737efe9872eSLisandro Dalcin   DM             dm;
1738efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1739efe9872eSLisandro Dalcin   void           *ctx;
1740efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1741efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1742efe9872eSLisandro Dalcin 
1743efe9872eSLisandro Dalcin   PetscFunctionBegin;
1744efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1745efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1746efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1747efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1748efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1749efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1750efe9872eSLisandro Dalcin 
1751efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1752efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1753efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1754efe9872eSLisandro Dalcin 
1755efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1756efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1757efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1758efe9872eSLisandro Dalcin   }
1759efe9872eSLisandro Dalcin 
1760efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1761efe9872eSLisandro Dalcin 
1762efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1763efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1764efe9872eSLisandro Dalcin   PetscStackPop;
1765efe9872eSLisandro Dalcin 
1766efe9872eSLisandro Dalcin   if (rhsjacobian) {
1767*d60b7d5cSBarry Smith     Mat Jrhs,Prhs;
1768efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1769efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1770*d60b7d5cSBarry Smith     ierr = MatAXPY(J,-1,Jrhs,ts->axpy_pattern);CHKERRQ(ierr);
1771*d60b7d5cSBarry Smith     if (P != J) {ierr = MatAXPY(P,-1,Prhs,ts->axpy_pattern);CHKERRQ(ierr);}
1772efe9872eSLisandro Dalcin   }
1773efe9872eSLisandro Dalcin 
1774efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1775efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1776efe9872eSLisandro Dalcin }
1777efe9872eSLisandro Dalcin 
1778438f35afSJed Brown /*@C
1779438f35afSJed Brown    TSSetTransientVariable - sets function to transform from state to transient variables
1780438f35afSJed Brown 
1781438f35afSJed Brown    Logically Collective
1782438f35afSJed Brown 
1783438f35afSJed Brown    Input Arguments:
1784438f35afSJed Brown +  ts - time stepping context on which to change the transient variable
1785a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
1786438f35afSJed Brown -  ctx - a context for tvar
1787438f35afSJed Brown 
1788a96d6ef6SBarry Smith     Calling sequence of tvar:
1789a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
1790a96d6ef6SBarry Smith 
1791a96d6ef6SBarry Smith +   ts - timestep context
1792a96d6ef6SBarry Smith .   p - input vector (primative form)
1793a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
1794a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
1795a96d6ef6SBarry Smith 
1796438f35afSJed Brown    Level: advanced
1797438f35afSJed Brown 
1798438f35afSJed Brown    Notes:
1799438f35afSJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
1800438f35afSJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
1801438f35afSJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
1802438f35afSJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
1803438f35afSJed Brown    evaluated via the chain rule, as in
1804438f35afSJed Brown 
1805438f35afSJed Brown      dF/dP + shift * dF/dCdot dC/dP.
1806438f35afSJed Brown 
1807438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian()
1808438f35afSJed Brown @*/
1809438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx)
1810438f35afSJed Brown {
1811438f35afSJed Brown   PetscErrorCode ierr;
1812438f35afSJed Brown   DM             dm;
1813438f35afSJed Brown 
1814438f35afSJed Brown   PetscFunctionBegin;
1815438f35afSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1816438f35afSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1817438f35afSJed Brown   ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr);
1818438f35afSJed Brown   PetscFunctionReturn(0);
1819438f35afSJed Brown }
1820438f35afSJed Brown 
1821efe9872eSLisandro Dalcin /*@
1822e3c11fc1SJed Brown    TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables
1823e3c11fc1SJed Brown 
1824e3c11fc1SJed Brown    Logically Collective
1825e3c11fc1SJed Brown 
1826e3c11fc1SJed Brown    Input Parameters:
1827e3c11fc1SJed Brown +  ts - TS on which to compute
1828e3c11fc1SJed Brown -  U - state vector to be transformed to transient variables
1829e3c11fc1SJed Brown 
1830e3c11fc1SJed Brown    Output Parameters:
1831e3c11fc1SJed Brown .  C - transient (conservative) variable
1832e3c11fc1SJed Brown 
1833e3c11fc1SJed Brown    Developer Notes:
1834e3c11fc1SJed Brown    If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed.
1835e3c11fc1SJed Brown    This makes it safe to call without a guard.  One can use TSHasTransientVariable() to check if transient variables are
1836e3c11fc1SJed Brown    being used.
1837e3c11fc1SJed Brown 
1838e3c11fc1SJed Brown    Level: developer
1839e3c11fc1SJed Brown 
1840e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian()
1841e3c11fc1SJed Brown @*/
1842e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C)
1843e3c11fc1SJed Brown {
1844e3c11fc1SJed Brown   PetscErrorCode ierr;
1845e3c11fc1SJed Brown   DM             dm;
1846e3c11fc1SJed Brown   DMTS           dmts;
1847e3c11fc1SJed Brown 
1848e3c11fc1SJed Brown   PetscFunctionBegin;
1849e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1850e3c11fc1SJed Brown   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1851e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1852e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1853e3c11fc1SJed Brown   if (dmts->ops->transientvar) {
1854e3c11fc1SJed Brown     PetscValidHeaderSpecific(C,VEC_CLASSID,3);
1855e3c11fc1SJed Brown     ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr);
1856e3c11fc1SJed Brown   }
1857e3c11fc1SJed Brown   PetscFunctionReturn(0);
1858e3c11fc1SJed Brown }
1859e3c11fc1SJed Brown 
1860e3c11fc1SJed Brown /*@
1861e3c11fc1SJed Brown    TSHasTransientVariable - determine whether transient variables have been set
1862e3c11fc1SJed Brown 
1863e3c11fc1SJed Brown    Logically Collective
1864e3c11fc1SJed Brown 
1865e3c11fc1SJed Brown    Input Parameters:
1866e3c11fc1SJed Brown .  ts - TS on which to compute
1867e3c11fc1SJed Brown 
1868e3c11fc1SJed Brown    Output Parameters:
1869e3c11fc1SJed Brown .  has - PETSC_TRUE if transient variables have been set
1870e3c11fc1SJed Brown 
1871e3c11fc1SJed Brown    Level: developer
1872e3c11fc1SJed Brown 
1873e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable()
1874e3c11fc1SJed Brown @*/
1875e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has)
1876e3c11fc1SJed Brown {
1877e3c11fc1SJed Brown   PetscErrorCode ierr;
1878e3c11fc1SJed Brown   DM             dm;
1879e3c11fc1SJed Brown   DMTS           dmts;
1880e3c11fc1SJed Brown 
1881e3c11fc1SJed Brown   PetscFunctionBegin;
1882e3c11fc1SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1883e3c11fc1SJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1884e3c11fc1SJed Brown   ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr);
1885e3c11fc1SJed Brown   *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE;
1886e3c11fc1SJed Brown   PetscFunctionReturn(0);
1887e3c11fc1SJed Brown }
1888e3c11fc1SJed Brown 
1889e3c11fc1SJed Brown /*@
1890efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1891efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1892efe9872eSLisandro Dalcin 
1893d083f849SBarry Smith    Logically Collective on TS
1894efe9872eSLisandro Dalcin 
1895efe9872eSLisandro Dalcin    Input Parameters:
1896efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1897efe9872eSLisandro Dalcin .  u - the solution vector
1898efe9872eSLisandro Dalcin -  v - the time derivative vector
1899efe9872eSLisandro Dalcin 
1900efe9872eSLisandro Dalcin    Level: beginner
1901efe9872eSLisandro Dalcin 
1902efe9872eSLisandro Dalcin @*/
1903efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1904efe9872eSLisandro Dalcin {
1905efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1906efe9872eSLisandro Dalcin 
1907efe9872eSLisandro Dalcin   PetscFunctionBegin;
1908efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1909efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1910efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1911efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1912efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1913efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1914efe9872eSLisandro Dalcin   ts->vec_dot = v;
1915efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1916efe9872eSLisandro Dalcin }
1917efe9872eSLisandro Dalcin 
1918efe9872eSLisandro Dalcin /*@
1919efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1920efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1921efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1922efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1923efe9872eSLisandro Dalcin 
1924efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1925efe9872eSLisandro Dalcin 
1926efe9872eSLisandro Dalcin    Input Parameter:
1927efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1928efe9872eSLisandro Dalcin 
1929efe9872eSLisandro Dalcin    Output Parameter:
1930efe9872eSLisandro Dalcin +  u - the vector containing the solution
1931efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1932efe9872eSLisandro Dalcin 
1933efe9872eSLisandro Dalcin    Level: intermediate
1934efe9872eSLisandro Dalcin 
1935efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1936efe9872eSLisandro Dalcin 
1937efe9872eSLisandro Dalcin @*/
1938efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1939efe9872eSLisandro Dalcin {
1940efe9872eSLisandro Dalcin   PetscFunctionBegin;
1941efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1942efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1943efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1944efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1945efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1946efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1947efe9872eSLisandro Dalcin }
1948efe9872eSLisandro Dalcin 
194955849f57SBarry Smith /*@C
195055849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
195155849f57SBarry Smith 
195255849f57SBarry Smith   Collective on PetscViewer
195355849f57SBarry Smith 
195455849f57SBarry Smith   Input Parameters:
195555849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
195655849f57SBarry Smith            some related function before a call to TSLoad().
195755849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
195855849f57SBarry Smith 
195955849f57SBarry Smith    Level: intermediate
196055849f57SBarry Smith 
196155849f57SBarry Smith   Notes:
196255849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
196355849f57SBarry Smith 
196455849f57SBarry Smith   Notes for advanced users:
196555849f57SBarry Smith   Most users should not need to know the details of the binary storage
196655849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
196755849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
196855849f57SBarry Smith   format is
196955849f57SBarry Smith .vb
197055849f57SBarry Smith      has not yet been determined
197155849f57SBarry Smith .ve
197255849f57SBarry Smith 
197355849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
197455849f57SBarry Smith @*/
1975f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
197655849f57SBarry Smith {
197755849f57SBarry Smith   PetscErrorCode ierr;
197855849f57SBarry Smith   PetscBool      isbinary;
197955849f57SBarry Smith   PetscInt       classid;
198055849f57SBarry Smith   char           type[256];
19812d53ad75SBarry Smith   DMTS           sdm;
1982ad6bc421SBarry Smith   DM             dm;
198355849f57SBarry Smith 
198455849f57SBarry Smith   PetscFunctionBegin;
1985f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
198655849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
198755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
198855849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
198955849f57SBarry Smith 
1990060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1991ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1992060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1993f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1994f2c2a1b9SBarry Smith   if (ts->ops->load) {
1995f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1996f2c2a1b9SBarry Smith   }
1997ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1998ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1999ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
2000f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
2001f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
20022d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20032d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
200455849f57SBarry Smith   PetscFunctionReturn(0);
200555849f57SBarry Smith }
200655849f57SBarry Smith 
20079804daf3SBarry Smith #include <petscdraw.h>
2008e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2009e04113cfSBarry Smith #include <petscviewersaws.h>
2010f05ece33SBarry Smith #endif
2011fe2efc57SMark 
2012fe2efc57SMark /*@C
2013fe2efc57SMark    TSViewFromOptions - View from Options
2014fe2efc57SMark 
2015fe2efc57SMark    Collective on TS
2016fe2efc57SMark 
2017fe2efc57SMark    Input Parameters:
2018fe2efc57SMark +  A - the application ordering context
2019736c3998SJose E. Roman .  obj - Optional object
2020736c3998SJose E. Roman -  name - command line option
2021fe2efc57SMark 
2022fe2efc57SMark    Level: intermediate
2023fe2efc57SMark .seealso:  TS, TSView, PetscObjectViewFromOptions(), TSCreate()
2024fe2efc57SMark @*/
2025fe2efc57SMark PetscErrorCode  TSViewFromOptions(TS A,PetscObject obj,const char name[])
2026fe2efc57SMark {
2027fe2efc57SMark   PetscErrorCode ierr;
2028fe2efc57SMark 
2029fe2efc57SMark   PetscFunctionBegin;
2030fe2efc57SMark   PetscValidHeaderSpecific(A,TS_CLASSID,1);
2031fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
2032fe2efc57SMark   PetscFunctionReturn(0);
2033fe2efc57SMark }
2034fe2efc57SMark 
20357e2c5f70SBarry Smith /*@C
2036d763cef2SBarry Smith     TSView - Prints the TS data structure.
2037d763cef2SBarry Smith 
20384c49b128SBarry Smith     Collective on TS
2039d763cef2SBarry Smith 
2040d763cef2SBarry Smith     Input Parameters:
2041d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2042d763cef2SBarry Smith -   viewer - visualization context
2043d763cef2SBarry Smith 
2044d763cef2SBarry Smith     Options Database Key:
2045d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2046d763cef2SBarry Smith 
2047d763cef2SBarry Smith     Notes:
2048d763cef2SBarry Smith     The available visualization contexts include
2049b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2050b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2051d763cef2SBarry Smith          output where only the first processor opens
2052d763cef2SBarry Smith          the file.  All other processors send their
2053d763cef2SBarry Smith          data to the first processor to print.
2054d763cef2SBarry Smith 
2055d763cef2SBarry Smith     The user can open an alternative visualization context with
2056b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2057d763cef2SBarry Smith 
2058d763cef2SBarry Smith     Level: beginner
2059d763cef2SBarry Smith 
2060b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2061d763cef2SBarry Smith @*/
20627087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2063d763cef2SBarry Smith {
2064dfbe8321SBarry Smith   PetscErrorCode ierr;
206519fd82e9SBarry Smith   TSType         type;
20662b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20672d53ad75SBarry Smith   DMTS           sdm;
2068e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2069536b137fSBarry Smith   PetscBool      issaws;
2070f05ece33SBarry Smith #endif
2071d763cef2SBarry Smith 
2072d763cef2SBarry Smith   PetscFunctionBegin;
20730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20743050cee2SBarry Smith   if (!viewer) {
2075ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20763050cee2SBarry Smith   }
20770700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2078c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2079fd16b177SBarry Smith 
2080251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2081251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
208255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20832b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2084e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2085536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2086f05ece33SBarry Smith #endif
208732077d6dSBarry Smith   if (iascii) {
2088dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2089efd4aadfSBarry Smith     if (ts->ops->view) {
2090efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2091efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2092efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2093efd4aadfSBarry Smith     }
2094ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
209577431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2096ef85077eSLisandro Dalcin     }
2097ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20987c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2099ef85077eSLisandro Dalcin     }
2100efd4aadfSBarry Smith     if (ts->usessnes) {
2101efd4aadfSBarry Smith       PetscBool lin;
2102d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
21035ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2104d763cef2SBarry Smith       }
21055ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
21061ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2107efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2108efd4aadfSBarry Smith     }
2109193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2110a0af407cSBarry Smith     if (ts->vrtol) {
2111a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2112a0af407cSBarry Smith     } else {
2113a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2114a0af407cSBarry Smith     }
2115a0af407cSBarry Smith     if (ts->vatol) {
2116a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2117a0af407cSBarry Smith     } else {
2118a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2119a0af407cSBarry Smith     }
2120825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2121efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2122825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21230f5bd95cSBarry Smith   } else if (isstring) {
2124a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
212536a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
212636a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
212755849f57SBarry Smith   } else if (isbinary) {
212855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
212955849f57SBarry Smith     MPI_Comm    comm;
213055849f57SBarry Smith     PetscMPIInt rank;
213155849f57SBarry Smith     char        type[256];
213255849f57SBarry Smith 
213355849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
213455849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
213555849f57SBarry Smith     if (!rank) {
2136f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
213755849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
2138f253e43cSLisandro Dalcin       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
213955849f57SBarry Smith     }
214055849f57SBarry Smith     if (ts->ops->view) {
214155849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
214255849f57SBarry Smith     }
2143efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2144f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2145f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21462d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21472d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21482b0a91c0SBarry Smith   } else if (isdraw) {
21492b0a91c0SBarry Smith     PetscDraw draw;
21502b0a91c0SBarry Smith     char      str[36];
215189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21522b0a91c0SBarry Smith 
21532b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21542b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21552b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21562b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
215751fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
215889fd9fafSBarry Smith     bottom = y - h;
21592b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21602b0a91c0SBarry Smith     if (ts->ops->view) {
21612b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21622b0a91c0SBarry Smith     }
2163efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2164efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21652b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2166e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2167536b137fSBarry Smith   } else if (issaws) {
2168d45a07a7SBarry Smith     PetscMPIInt rank;
21692657e9d9SBarry Smith     const char  *name;
21702657e9d9SBarry Smith 
21712657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2172d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2173d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2174d45a07a7SBarry Smith       char       dir[1024];
2175d45a07a7SBarry Smith 
2176e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2177a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21782657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21792657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21802657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2181d763cef2SBarry Smith     }
21820acecf5bSBarry Smith     if (ts->ops->view) {
21830acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21840acecf5bSBarry Smith     }
2185f05ece33SBarry Smith #endif
2186f05ece33SBarry Smith   }
218736a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
218836a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
218936a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
219036a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
219136a9e3b9SBarry Smith   }
219236a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
219336a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2194f05ece33SBarry Smith 
2195b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2196251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2197b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2198d763cef2SBarry Smith   PetscFunctionReturn(0);
2199d763cef2SBarry Smith }
2200d763cef2SBarry Smith 
2201b07ff414SBarry Smith /*@
2202d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2203d763cef2SBarry Smith    the timesteppers.
2204d763cef2SBarry Smith 
22053f9fe445SBarry Smith    Logically Collective on TS
2206d763cef2SBarry Smith 
2207d763cef2SBarry Smith    Input Parameters:
2208d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2209d763cef2SBarry Smith -  usrP - optional user context
2210d763cef2SBarry Smith 
221195452b02SPatrick Sanan    Fortran Notes:
221295452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2213daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2214daf670e6SBarry Smith 
2215d763cef2SBarry Smith    Level: intermediate
2216d763cef2SBarry Smith 
2217d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2218d763cef2SBarry Smith @*/
22197087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2220d763cef2SBarry Smith {
2221d763cef2SBarry Smith   PetscFunctionBegin;
22220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2223d763cef2SBarry Smith   ts->user = usrP;
2224d763cef2SBarry Smith   PetscFunctionReturn(0);
2225d763cef2SBarry Smith }
2226d763cef2SBarry Smith 
2227b07ff414SBarry Smith /*@
2228d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2229d763cef2SBarry Smith     timestepper.
2230d763cef2SBarry Smith 
2231d763cef2SBarry Smith     Not Collective
2232d763cef2SBarry Smith 
2233d763cef2SBarry Smith     Input Parameter:
2234d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2235d763cef2SBarry Smith 
2236d763cef2SBarry Smith     Output Parameter:
2237d763cef2SBarry Smith .   usrP - user context
2238d763cef2SBarry Smith 
223995452b02SPatrick Sanan    Fortran Notes:
224095452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2241daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2242daf670e6SBarry Smith 
2243d763cef2SBarry Smith     Level: intermediate
2244d763cef2SBarry Smith 
2245d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2246d763cef2SBarry Smith @*/
2247e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2248d763cef2SBarry Smith {
2249d763cef2SBarry Smith   PetscFunctionBegin;
22500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2251e71120c6SJed Brown   *(void**)usrP = ts->user;
2252d763cef2SBarry Smith   PetscFunctionReturn(0);
2253d763cef2SBarry Smith }
2254d763cef2SBarry Smith 
2255d763cef2SBarry Smith /*@
225680275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2257d763cef2SBarry Smith 
2258d763cef2SBarry Smith    Not Collective
2259d763cef2SBarry Smith 
2260d763cef2SBarry Smith    Input Parameter:
2261d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2262d763cef2SBarry Smith 
2263d763cef2SBarry Smith    Output Parameter:
226480275a0aSLisandro Dalcin .  steps - number of steps completed so far
2265d763cef2SBarry Smith 
2266d763cef2SBarry Smith    Level: intermediate
2267d763cef2SBarry Smith 
22689be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2269d763cef2SBarry Smith @*/
227080275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2271d763cef2SBarry Smith {
2272d763cef2SBarry Smith   PetscFunctionBegin;
22730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227480275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
227580275a0aSLisandro Dalcin   *steps = ts->steps;
227680275a0aSLisandro Dalcin   PetscFunctionReturn(0);
227780275a0aSLisandro Dalcin }
227880275a0aSLisandro Dalcin 
227980275a0aSLisandro Dalcin /*@
228080275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
228180275a0aSLisandro Dalcin 
228280275a0aSLisandro Dalcin    Logically Collective on TS
228380275a0aSLisandro Dalcin 
228480275a0aSLisandro Dalcin    Input Parameters:
228580275a0aSLisandro Dalcin +  ts - the TS context
228680275a0aSLisandro Dalcin -  steps - number of steps completed so far
228780275a0aSLisandro Dalcin 
228880275a0aSLisandro Dalcin    Notes:
228980275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
229080275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
229180275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
229280275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
229380275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
229480275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
229580275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
229680275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
229780275a0aSLisandro Dalcin 
229880275a0aSLisandro Dalcin    Level: advanced
229980275a0aSLisandro Dalcin 
230080275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
230180275a0aSLisandro Dalcin @*/
230280275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
230380275a0aSLisandro Dalcin {
230480275a0aSLisandro Dalcin   PetscFunctionBegin;
230580275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
230680275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
230780275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
230880275a0aSLisandro Dalcin   ts->steps = steps;
2309d763cef2SBarry Smith   PetscFunctionReturn(0);
2310d763cef2SBarry Smith }
2311d763cef2SBarry Smith 
2312d763cef2SBarry Smith /*@
2313d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2314d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2315d763cef2SBarry Smith 
23163f9fe445SBarry Smith    Logically Collective on TS
2317d763cef2SBarry Smith 
2318d763cef2SBarry Smith    Input Parameters:
2319d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2320d763cef2SBarry Smith -  time_step - the size of the timestep
2321d763cef2SBarry Smith 
2322d763cef2SBarry Smith    Level: intermediate
2323d763cef2SBarry Smith 
2324aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2325d763cef2SBarry Smith 
2326d763cef2SBarry Smith @*/
23277087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2328d763cef2SBarry Smith {
2329d763cef2SBarry Smith   PetscFunctionBegin;
23300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2331c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2332d763cef2SBarry Smith   ts->time_step = time_step;
2333d763cef2SBarry Smith   PetscFunctionReturn(0);
2334d763cef2SBarry Smith }
2335d763cef2SBarry Smith 
2336a43b19c4SJed Brown /*@
233749354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
233849354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
233949354f04SShri Abhyankar      or just return at the final time TS computed.
2340a43b19c4SJed Brown 
2341a43b19c4SJed Brown   Logically Collective on TS
2342a43b19c4SJed Brown 
2343a43b19c4SJed Brown    Input Parameter:
2344a43b19c4SJed Brown +   ts - the time-step context
234549354f04SShri Abhyankar -   eftopt - exact final time option
2346a43b19c4SJed Brown 
2347feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2348feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2349feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2350feed9e9dSBarry Smith 
2351feed9e9dSBarry Smith    Options Database:
2352feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2353feed9e9dSBarry Smith 
2354ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2355ee346746SBarry Smith     then the final time you selected.
2356ee346746SBarry Smith 
2357a43b19c4SJed Brown    Level: beginner
2358a43b19c4SJed Brown 
2359f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2360a43b19c4SJed Brown @*/
236149354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2362a43b19c4SJed Brown {
2363a43b19c4SJed Brown   PetscFunctionBegin;
2364a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
236549354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
236649354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2367a43b19c4SJed Brown   PetscFunctionReturn(0);
2368a43b19c4SJed Brown }
2369a43b19c4SJed Brown 
2370d763cef2SBarry Smith /*@
2371f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2372f6953c82SLisandro Dalcin 
2373f6953c82SLisandro Dalcin    Not Collective
2374f6953c82SLisandro Dalcin 
2375f6953c82SLisandro Dalcin    Input Parameter:
2376f6953c82SLisandro Dalcin .  ts - the TS context
2377f6953c82SLisandro Dalcin 
2378f6953c82SLisandro Dalcin    Output Parameter:
2379f6953c82SLisandro Dalcin .  eftopt - exact final time option
2380f6953c82SLisandro Dalcin 
2381f6953c82SLisandro Dalcin    Level: beginner
2382f6953c82SLisandro Dalcin 
2383f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2384f6953c82SLisandro Dalcin @*/
2385f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2386f6953c82SLisandro Dalcin {
2387f6953c82SLisandro Dalcin   PetscFunctionBegin;
2388f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2389f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2390f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2391f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2392f6953c82SLisandro Dalcin }
2393f6953c82SLisandro Dalcin 
2394f6953c82SLisandro Dalcin /*@
2395d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2396d763cef2SBarry Smith 
2397d763cef2SBarry Smith    Not Collective
2398d763cef2SBarry Smith 
2399d763cef2SBarry Smith    Input Parameter:
2400d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2401d763cef2SBarry Smith 
2402d763cef2SBarry Smith    Output Parameter:
2403d763cef2SBarry Smith .  dt - the current timestep size
2404d763cef2SBarry Smith 
2405d763cef2SBarry Smith    Level: intermediate
2406d763cef2SBarry Smith 
2407aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2408d763cef2SBarry Smith 
2409d763cef2SBarry Smith @*/
24107087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2411d763cef2SBarry Smith {
2412d763cef2SBarry Smith   PetscFunctionBegin;
24130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2414f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2415d763cef2SBarry Smith   *dt = ts->time_step;
2416d763cef2SBarry Smith   PetscFunctionReturn(0);
2417d763cef2SBarry Smith }
2418d763cef2SBarry Smith 
2419d8e5e3e6SSatish Balay /*@
2420d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2421d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2422d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2423d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2424d763cef2SBarry Smith 
2425d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2426d763cef2SBarry Smith 
2427d763cef2SBarry Smith    Input Parameter:
2428d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2429d763cef2SBarry Smith 
2430d763cef2SBarry Smith    Output Parameter:
2431d763cef2SBarry Smith .  v - the vector containing the solution
2432d763cef2SBarry Smith 
243363e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
243463e21af5SBarry Smith    final time. It returns the solution at the next timestep.
243563e21af5SBarry Smith 
2436d763cef2SBarry Smith    Level: intermediate
2437d763cef2SBarry Smith 
24380ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2439d763cef2SBarry Smith 
2440d763cef2SBarry Smith @*/
24417087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2442d763cef2SBarry Smith {
2443d763cef2SBarry Smith   PetscFunctionBegin;
24440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24454482741eSBarry Smith   PetscValidPointer(v,2);
24468737fe31SLisandro Dalcin   *v = ts->vec_sol;
2447d763cef2SBarry Smith   PetscFunctionReturn(0);
2448d763cef2SBarry Smith }
2449d763cef2SBarry Smith 
245003fe5f5eSDebojyoti Ghosh /*@
2451b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
245203fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2453b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
245403fe5f5eSDebojyoti Ghosh    structure as the solution vector.
245503fe5f5eSDebojyoti Ghosh 
245603fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
245703fe5f5eSDebojyoti Ghosh 
245803fe5f5eSDebojyoti Ghosh    Parameters :
2459a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2460b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2461b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
246203fe5f5eSDebojyoti Ghosh        returned in v.
2463a2b725a8SWilliam Gropp -  v - the vector containing the n-th solution component
246403fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2465b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
246603fe5f5eSDebojyoti Ghosh 
24674cdd57e5SDebojyoti Ghosh    Level: advanced
246803fe5f5eSDebojyoti Ghosh 
246903fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
247003fe5f5eSDebojyoti Ghosh 
247103fe5f5eSDebojyoti Ghosh @*/
2472b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
247303fe5f5eSDebojyoti Ghosh {
247403fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
247503fe5f5eSDebojyoti Ghosh 
247603fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
247703fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2478b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
247903fe5f5eSDebojyoti Ghosh   else {
2480b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
248103fe5f5eSDebojyoti Ghosh   }
248203fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
248303fe5f5eSDebojyoti Ghosh }
248403fe5f5eSDebojyoti Ghosh 
24854cdd57e5SDebojyoti Ghosh /*@
24864cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24874cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24884cdd57e5SDebojyoti Ghosh 
24894cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24904cdd57e5SDebojyoti Ghosh 
24914cdd57e5SDebojyoti Ghosh    Parameters :
2492a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2493a2b725a8SWilliam Gropp -  v - the vector containing the auxiliary solution
24944cdd57e5SDebojyoti Ghosh 
24954cdd57e5SDebojyoti Ghosh    Level: intermediate
24964cdd57e5SDebojyoti Ghosh 
24974cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24984cdd57e5SDebojyoti Ghosh 
24994cdd57e5SDebojyoti Ghosh @*/
25004cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
25014cdd57e5SDebojyoti Ghosh {
25024cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25034cdd57e5SDebojyoti Ghosh 
25044cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25054cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2506f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
25074cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2508f6356ec7SDebojyoti Ghosh   } else {
2509f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2510f6356ec7SDebojyoti Ghosh   }
25114cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25124cdd57e5SDebojyoti Ghosh }
25134cdd57e5SDebojyoti Ghosh 
25144cdd57e5SDebojyoti Ghosh /*@
25154cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
25164cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
25174cdd57e5SDebojyoti Ghosh 
25184cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25194cdd57e5SDebojyoti Ghosh 
25209657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
25219657682dSDebojyoti Ghosh 
25224cdd57e5SDebojyoti Ghosh    Parameters :
2523a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2524657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
2525a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
25264cdd57e5SDebojyoti Ghosh 
25274cdd57e5SDebojyoti Ghosh    Level: intermediate
25284cdd57e5SDebojyoti Ghosh 
252957df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25304cdd57e5SDebojyoti Ghosh 
25314cdd57e5SDebojyoti Ghosh @*/
25320a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25334cdd57e5SDebojyoti Ghosh {
25344cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25354cdd57e5SDebojyoti Ghosh 
25364cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25374cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2538f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25390a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2540f6356ec7SDebojyoti Ghosh   } else {
2541f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2542f6356ec7SDebojyoti Ghosh   }
25434cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25444cdd57e5SDebojyoti Ghosh }
25454cdd57e5SDebojyoti Ghosh 
254657df6a1bSDebojyoti Ghosh /*@
254757df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
254857df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
254957df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
255057df6a1bSDebojyoti Ghosh 
255157df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
255257df6a1bSDebojyoti Ghosh 
255357df6a1bSDebojyoti Ghosh    Parameters :
2554a2b725a8SWilliam Gropp +  ts - the TS context obtained from TSCreate() (input parameter).
2555a2b725a8SWilliam Gropp -  v - the vector containing the error (same size as the solution).
255657df6a1bSDebojyoti Ghosh 
255757df6a1bSDebojyoti Ghosh    Level: intermediate
255857df6a1bSDebojyoti Ghosh 
255957df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
256057df6a1bSDebojyoti Ghosh 
256157df6a1bSDebojyoti Ghosh @*/
256257df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
256357df6a1bSDebojyoti Ghosh {
256457df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
256557df6a1bSDebojyoti Ghosh 
256657df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
256757df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25689657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
256957df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
257057df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
257157df6a1bSDebojyoti Ghosh   }
257257df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
257357df6a1bSDebojyoti Ghosh }
257457df6a1bSDebojyoti Ghosh 
2575bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2576d8e5e3e6SSatish Balay /*@
2577bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2578d763cef2SBarry Smith 
2579bdad233fSMatthew Knepley   Not collective
2580d763cef2SBarry Smith 
2581bdad233fSMatthew Knepley   Input Parameters:
2582bdad233fSMatthew Knepley + ts   - The TS
2583bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2584d763cef2SBarry Smith .vb
25850910c330SBarry Smith          U_t - A U = 0      (linear)
25860910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25870910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2588d763cef2SBarry Smith .ve
2589d763cef2SBarry Smith 
2590d763cef2SBarry Smith    Level: beginner
2591d763cef2SBarry Smith 
2592bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2593d763cef2SBarry Smith @*/
25947087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2595a7cc72afSBarry Smith {
25969e2a6581SJed Brown   PetscErrorCode ierr;
25979e2a6581SJed Brown 
2598d763cef2SBarry Smith   PetscFunctionBegin;
25990700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2600bdad233fSMatthew Knepley   ts->problem_type = type;
26019e2a6581SJed Brown   if (type == TS_LINEAR) {
26029e2a6581SJed Brown     SNES snes;
26039e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
26049e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
26059e2a6581SJed Brown   }
2606d763cef2SBarry Smith   PetscFunctionReturn(0);
2607d763cef2SBarry Smith }
2608d763cef2SBarry Smith 
2609bdad233fSMatthew Knepley /*@C
2610bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2611bdad233fSMatthew Knepley 
2612bdad233fSMatthew Knepley   Not collective
2613bdad233fSMatthew Knepley 
2614bdad233fSMatthew Knepley   Input Parameter:
2615bdad233fSMatthew Knepley . ts   - The TS
2616bdad233fSMatthew Knepley 
2617bdad233fSMatthew Knepley   Output Parameter:
2618bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2619bdad233fSMatthew Knepley .vb
2620089b2837SJed Brown          M U_t = A U
2621089b2837SJed Brown          M(t) U_t = A(t) U
2622b5abc632SBarry Smith          F(t,U,U_t)
2623bdad233fSMatthew Knepley .ve
2624bdad233fSMatthew Knepley 
2625bdad233fSMatthew Knepley    Level: beginner
2626bdad233fSMatthew Knepley 
2627bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2628bdad233fSMatthew Knepley @*/
26297087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2630a7cc72afSBarry Smith {
2631bdad233fSMatthew Knepley   PetscFunctionBegin;
26320700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26334482741eSBarry Smith   PetscValidIntPointer(type,2);
2634bdad233fSMatthew Knepley   *type = ts->problem_type;
2635bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2636bdad233fSMatthew Knepley }
2637d763cef2SBarry Smith 
2638303a5415SBarry Smith /*
2639303a5415SBarry 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()
2640303a5415SBarry Smith */
2641303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts)
2642303a5415SBarry Smith {
2643303a5415SBarry Smith   PetscErrorCode ierr;
2644303a5415SBarry Smith   PetscBool      isnone;
2645303a5415SBarry Smith 
2646303a5415SBarry Smith   PetscFunctionBegin;
2647303a5415SBarry Smith   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
2648303a5415SBarry Smith   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
2649303a5415SBarry Smith 
2650303a5415SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
2651303a5415SBarry Smith   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2652303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
2653303a5415SBarry Smith   } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) {
2654303a5415SBarry Smith     ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE;
2655303a5415SBarry Smith   }
2656303a5415SBarry Smith   PetscFunctionReturn(0);
2657303a5415SBarry Smith }
2658303a5415SBarry Smith 
2659303a5415SBarry Smith 
2660d763cef2SBarry Smith /*@
2661303a5415SBarry Smith    TSSetUp - Sets up the internal data structures for the later use of a timestepper.
2662d763cef2SBarry Smith 
2663d763cef2SBarry Smith    Collective on TS
2664d763cef2SBarry Smith 
2665d763cef2SBarry Smith    Input Parameter:
2666d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2667d763cef2SBarry Smith 
2668d763cef2SBarry Smith    Notes:
2669d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2670d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2671141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2672d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2673141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2674d763cef2SBarry Smith 
2675d763cef2SBarry Smith    Level: advanced
2676d763cef2SBarry Smith 
2677141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2678d763cef2SBarry Smith @*/
26797087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2680d763cef2SBarry Smith {
2681dfbe8321SBarry Smith   PetscErrorCode ierr;
26826c6b9e74SPeter Brune   DM             dm;
26836c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2684d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2685cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26866c6b9e74SPeter Brune   TSIJacobian    ijac;
2687efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26886c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2689d763cef2SBarry Smith 
2690d763cef2SBarry Smith   PetscFunctionBegin;
26910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2692277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2693277b19d0SLisandro Dalcin 
26947adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2695cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2696cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2697d763cef2SBarry Smith   }
2698277b19d0SLisandro Dalcin 
26991a638600SStefano Zampini   if (!ts->vec_sol) {
27001a638600SStefano Zampini     if (ts->dm) {
27011a638600SStefano Zampini       ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
27021a638600SStefano Zampini     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
27031a638600SStefano Zampini   }
2704277b19d0SLisandro Dalcin 
2705298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2706298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2707298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2708298bade4SHong Zhang   }
2709298bade4SHong Zhang 
2710cd4cee2dSHong Zhang   if (ts->quadraturets) {
2711cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2712ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2713cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2714cd4cee2dSHong Zhang   }
2715cd4cee2dSHong Zhang 
2716971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2717f23ba4b3SHong Zhang   if (rhsjac == TSComputeRHSJacobianConstant) {
2718e1244c69SJed Brown     Mat Amat,Pmat;
2719e1244c69SJed Brown     SNES snes;
2720e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2721e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2722e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2723e1244c69SJed Brown      * have displaced the RHS matrix */
2724971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2725abc0d4abSBarry 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 */
2726abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2727e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2728e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2729e1244c69SJed Brown     }
2730971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2731abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2732e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2733e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2734e1244c69SJed Brown     }
2735e1244c69SJed Brown   }
27362ffb9264SLisandro Dalcin 
27372ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27382ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27392ffb9264SLisandro Dalcin 
2740277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2741000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2742277b19d0SLisandro Dalcin   }
2743277b19d0SLisandro Dalcin 
2744303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
27452ffb9264SLisandro Dalcin 
2746a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27476c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27486c6b9e74SPeter Brune    */
27496c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27500298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27516c6b9e74SPeter Brune   if (!func) {
27526c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27536c6b9e74SPeter Brune   }
2754a6772fa2SLisandro 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.
27556c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27566c6b9e74SPeter Brune    */
27570298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27580298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2759efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27600298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2761efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27626c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27636c6b9e74SPeter Brune   }
2764c0517034SDebojyoti Ghosh 
2765c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2766c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2767c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2768c0517034SDebojyoti Ghosh   }
2769c0517034SDebojyoti Ghosh 
2770277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2771277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2772277b19d0SLisandro Dalcin }
2773277b19d0SLisandro Dalcin 
2774f6a906c0SBarry Smith /*@
2775277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2776277b19d0SLisandro Dalcin 
2777277b19d0SLisandro Dalcin    Collective on TS
2778277b19d0SLisandro Dalcin 
2779277b19d0SLisandro Dalcin    Input Parameter:
2780277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2781277b19d0SLisandro Dalcin 
2782277b19d0SLisandro Dalcin    Level: beginner
2783277b19d0SLisandro Dalcin 
2784277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2785277b19d0SLisandro Dalcin @*/
2786277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2787277b19d0SLisandro Dalcin {
27881d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2789277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2790277b19d0SLisandro Dalcin 
2791277b19d0SLisandro Dalcin   PetscFunctionBegin;
2792277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2793b18ea86cSHong Zhang 
2794277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2795277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2796277b19d0SLisandro Dalcin   }
2797277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2798e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2799bbd56ea5SKarl Rupp 
28004e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
28014e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2802214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
28036bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2804efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2805e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2806e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
280738637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2808bbd56ea5SKarl Rupp 
2809cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2810ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2811ecf68647SHong Zhang   if (ts->forward_solve) {
2812ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2813ecf68647SHong Zhang   }
2814cd4cee2dSHong Zhang   if (ts->quadraturets) {
2815cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
281636eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2817cd4cee2dSHong Zhang   }
28181d06f6b3SHong Zhang   while (ilink) {
28191d06f6b3SHong Zhang     next = ilink->next;
28201d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
28211d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
28221d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
28231d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
28241d06f6b3SHong Zhang     ilink = next;
282587f4e208SHong Zhang   }
2826545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2827277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2828d763cef2SBarry Smith   PetscFunctionReturn(0);
2829d763cef2SBarry Smith }
2830d763cef2SBarry Smith 
2831d8e5e3e6SSatish Balay /*@
2832d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2833d763cef2SBarry Smith    with TSCreate().
2834d763cef2SBarry Smith 
2835d763cef2SBarry Smith    Collective on TS
2836d763cef2SBarry Smith 
2837d763cef2SBarry Smith    Input Parameter:
2838d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2839d763cef2SBarry Smith 
2840d763cef2SBarry Smith    Level: beginner
2841d763cef2SBarry Smith 
2842d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2843d763cef2SBarry Smith @*/
28446bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2845d763cef2SBarry Smith {
28466849ba73SBarry Smith   PetscErrorCode ierr;
2847d763cef2SBarry Smith 
2848d763cef2SBarry Smith   PetscFunctionBegin;
28496bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2850ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
2851c793f718SLisandro Dalcin   if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);}
2852d763cef2SBarry Smith 
2853ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2854ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2855ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2856ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2857ecf68647SHong Zhang   }
2858e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2859e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28606bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28616d4c513bSLisandro Dalcin 
2862bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2863bc952696SBarry Smith 
286484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28656427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28666427ac75SLisandro Dalcin 
28676bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28686bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28696bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28700dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28716d4c513bSLisandro Dalcin 
2872cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2873a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2874d763cef2SBarry Smith   PetscFunctionReturn(0);
2875d763cef2SBarry Smith }
2876d763cef2SBarry Smith 
2877d8e5e3e6SSatish Balay /*@
2878d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2879d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2880d763cef2SBarry Smith 
2881d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2882d763cef2SBarry Smith 
2883d763cef2SBarry Smith    Input Parameter:
2884d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2885d763cef2SBarry Smith 
2886d763cef2SBarry Smith    Output Parameter:
2887d763cef2SBarry Smith .  snes - the nonlinear solver context
2888d763cef2SBarry Smith 
2889d763cef2SBarry Smith    Notes:
2890d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2891d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
289294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2893d763cef2SBarry Smith 
2894d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28950298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2896d763cef2SBarry Smith 
2897d763cef2SBarry Smith    Level: beginner
2898d763cef2SBarry Smith 
2899d763cef2SBarry Smith @*/
29007087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2901d763cef2SBarry Smith {
2902d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2903d372ba47SLisandro Dalcin 
2904d763cef2SBarry Smith   PetscFunctionBegin;
29050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29064482741eSBarry Smith   PetscValidPointer(snes,2);
2907d372ba47SLisandro Dalcin   if (!ts->snes) {
2908ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
290916413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
29100298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29113bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2912d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2913496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
29149e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
29159e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
29169e2a6581SJed Brown     }
2917d372ba47SLisandro Dalcin   }
2918d763cef2SBarry Smith   *snes = ts->snes;
2919d763cef2SBarry Smith   PetscFunctionReturn(0);
2920d763cef2SBarry Smith }
2921d763cef2SBarry Smith 
2922deb2cd25SJed Brown /*@
2923deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2924deb2cd25SJed Brown 
2925deb2cd25SJed Brown    Collective
2926deb2cd25SJed Brown 
2927deb2cd25SJed Brown    Input Parameter:
2928deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2929deb2cd25SJed Brown -  snes - the nonlinear solver context
2930deb2cd25SJed Brown 
2931deb2cd25SJed Brown    Notes:
2932deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2933deb2cd25SJed Brown 
2934deb2cd25SJed Brown    Level: developer
2935deb2cd25SJed Brown 
2936deb2cd25SJed Brown @*/
2937deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2938deb2cd25SJed Brown {
2939deb2cd25SJed Brown   PetscErrorCode ierr;
2940d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2941deb2cd25SJed Brown 
2942deb2cd25SJed Brown   PetscFunctionBegin;
2943deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2944deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2945deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2946deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2947bbd56ea5SKarl Rupp 
2948deb2cd25SJed Brown   ts->snes = snes;
2949bbd56ea5SKarl Rupp 
29500298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29510298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2952740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29530298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2954740132f1SEmil Constantinescu   }
2955deb2cd25SJed Brown   PetscFunctionReturn(0);
2956deb2cd25SJed Brown }
2957deb2cd25SJed Brown 
2958d8e5e3e6SSatish Balay /*@
295994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2960d763cef2SBarry Smith    a TS (timestepper) context.
2961d763cef2SBarry Smith 
296294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2963d763cef2SBarry Smith 
2964d763cef2SBarry Smith    Input Parameter:
2965d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2966d763cef2SBarry Smith 
2967d763cef2SBarry Smith    Output Parameter:
296894b7f48cSBarry Smith .  ksp - the nonlinear solver context
2969d763cef2SBarry Smith 
2970d763cef2SBarry Smith    Notes:
297194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2972d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2973d763cef2SBarry Smith    KSP and PC contexts as well.
2974d763cef2SBarry Smith 
297594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29760298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2977d763cef2SBarry Smith 
2978d763cef2SBarry Smith    Level: beginner
2979d763cef2SBarry Smith 
2980d763cef2SBarry Smith @*/
29817087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2982d763cef2SBarry Smith {
2983d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2984089b2837SJed Brown   SNES           snes;
2985d372ba47SLisandro Dalcin 
2986d763cef2SBarry Smith   PetscFunctionBegin;
29870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29884482741eSBarry Smith   PetscValidPointer(ksp,2);
298917186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2990e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2991089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2992089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2993d763cef2SBarry Smith   PetscFunctionReturn(0);
2994d763cef2SBarry Smith }
2995d763cef2SBarry Smith 
2996d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2997d763cef2SBarry Smith 
2998adb62b0dSMatthew Knepley /*@
2999618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
3000618ce8baSLisandro Dalcin 
3001618ce8baSLisandro Dalcin    Logically Collective on TS
3002618ce8baSLisandro Dalcin 
3003618ce8baSLisandro Dalcin    Input Parameters:
3004618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3005618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
3006618ce8baSLisandro Dalcin 
3007618ce8baSLisandro Dalcin    Options Database Keys:
3008618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
3009618ce8baSLisandro Dalcin 
3010618ce8baSLisandro Dalcin    Notes:
3011618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
3012618ce8baSLisandro Dalcin 
3013618ce8baSLisandro Dalcin    Level: intermediate
3014618ce8baSLisandro Dalcin 
3015618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
3016618ce8baSLisandro Dalcin @*/
3017618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
3018618ce8baSLisandro Dalcin {
3019618ce8baSLisandro Dalcin   PetscFunctionBegin;
3020618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3021618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3022618ce8baSLisandro Dalcin   if (maxsteps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
3023618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
3024618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3025618ce8baSLisandro Dalcin }
3026618ce8baSLisandro Dalcin 
3027618ce8baSLisandro Dalcin /*@
3028618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
3029618ce8baSLisandro Dalcin 
3030618ce8baSLisandro Dalcin    Not Collective
3031618ce8baSLisandro Dalcin 
3032618ce8baSLisandro Dalcin    Input Parameters:
3033618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3034618ce8baSLisandro Dalcin 
3035618ce8baSLisandro Dalcin    Output Parameter:
3036618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3037618ce8baSLisandro Dalcin 
3038618ce8baSLisandro Dalcin    Level: advanced
3039618ce8baSLisandro Dalcin 
3040618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3041618ce8baSLisandro Dalcin @*/
3042618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3043618ce8baSLisandro Dalcin {
3044618ce8baSLisandro Dalcin   PetscFunctionBegin;
3045618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3046618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3047618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3048618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3049618ce8baSLisandro Dalcin }
3050618ce8baSLisandro Dalcin 
3051618ce8baSLisandro Dalcin /*@
3052618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3053618ce8baSLisandro Dalcin 
3054618ce8baSLisandro Dalcin    Logically Collective on TS
3055618ce8baSLisandro Dalcin 
3056618ce8baSLisandro Dalcin    Input Parameters:
3057618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3058618ce8baSLisandro Dalcin -  maxtime - final time to step to
3059618ce8baSLisandro Dalcin 
3060618ce8baSLisandro Dalcin    Options Database Keys:
3061ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3062618ce8baSLisandro Dalcin 
3063618ce8baSLisandro Dalcin    Notes:
3064618ce8baSLisandro Dalcin    The default maximum time is 5.0
3065618ce8baSLisandro Dalcin 
3066618ce8baSLisandro Dalcin    Level: intermediate
3067618ce8baSLisandro Dalcin 
3068618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3069618ce8baSLisandro Dalcin @*/
3070618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3071618ce8baSLisandro Dalcin {
3072618ce8baSLisandro Dalcin   PetscFunctionBegin;
3073618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3074618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3075618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3076618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3077618ce8baSLisandro Dalcin }
3078618ce8baSLisandro Dalcin 
3079618ce8baSLisandro Dalcin /*@
3080618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3081618ce8baSLisandro Dalcin 
3082618ce8baSLisandro Dalcin    Not Collective
3083618ce8baSLisandro Dalcin 
3084618ce8baSLisandro Dalcin    Input Parameters:
3085618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3086618ce8baSLisandro Dalcin 
3087618ce8baSLisandro Dalcin    Output Parameter:
3088618ce8baSLisandro Dalcin .  maxtime - final time to step to
3089618ce8baSLisandro Dalcin 
3090618ce8baSLisandro Dalcin    Level: advanced
3091618ce8baSLisandro Dalcin 
3092618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3093618ce8baSLisandro Dalcin @*/
3094618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3095618ce8baSLisandro Dalcin {
3096618ce8baSLisandro Dalcin   PetscFunctionBegin;
3097618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3098618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3099618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3100618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3101618ce8baSLisandro Dalcin }
3102618ce8baSLisandro Dalcin 
3103618ce8baSLisandro Dalcin /*@
3104aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3105edc382c3SSatish Balay 
3106edc382c3SSatish Balay    Level: deprecated
3107edc382c3SSatish Balay 
3108aaa6c58dSLisandro Dalcin @*/
3109aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3110aaa6c58dSLisandro Dalcin {
3111aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3112aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3113aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3114aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3115aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3116aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3117aaa6c58dSLisandro Dalcin }
3118aaa6c58dSLisandro Dalcin 
3119aaa6c58dSLisandro Dalcin /*@
312019eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3121edc382c3SSatish Balay 
3122edc382c3SSatish Balay    Level: deprecated
3123edc382c3SSatish Balay 
3124adb62b0dSMatthew Knepley @*/
31257087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3126adb62b0dSMatthew Knepley {
3127adb62b0dSMatthew Knepley   PetscFunctionBegin;
31280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3129abc0a331SBarry Smith   if (maxsteps) {
31304482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3131adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3132adb62b0dSMatthew Knepley   }
3133abc0a331SBarry Smith   if (maxtime) {
31344482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3135adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3136adb62b0dSMatthew Knepley   }
3137adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3138adb62b0dSMatthew Knepley }
3139adb62b0dSMatthew Knepley 
3140d763cef2SBarry Smith /*@
314119eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3142edc382c3SSatish Balay 
3143edc382c3SSatish Balay    Level: deprecated
3144edc382c3SSatish Balay 
3145d763cef2SBarry Smith @*/
31467087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3147d763cef2SBarry Smith {
3148d763cef2SBarry Smith   PetscFunctionBegin;
31490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3150c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3151c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
315239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
315339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3154d763cef2SBarry Smith   PetscFunctionReturn(0);
3155d763cef2SBarry Smith }
3156d763cef2SBarry Smith 
3157d763cef2SBarry Smith /*@
31585c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3159edc382c3SSatish Balay 
3160edc382c3SSatish Balay    Level: deprecated
3161edc382c3SSatish Balay 
31625c5f5948SLisandro Dalcin @*/
3163e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31645c5f5948SLisandro Dalcin 
316519eac22cSLisandro Dalcin /*@
31664f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3167edc382c3SSatish Balay 
3168edc382c3SSatish Balay    Level: deprecated
3169edc382c3SSatish Balay 
31704f4e0956SLisandro Dalcin @*/
31714f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31724f4e0956SLisandro Dalcin 
31734f4e0956SLisandro Dalcin /*@
3174d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3175d763cef2SBarry Smith    for use by the TS routines.
3176d763cef2SBarry Smith 
3177d083f849SBarry Smith    Logically Collective on TS
3178d763cef2SBarry Smith 
3179d763cef2SBarry Smith    Input Parameters:
3180d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31810910c330SBarry Smith -  u - the solution vector
3182d763cef2SBarry Smith 
3183d763cef2SBarry Smith    Level: beginner
3184d763cef2SBarry Smith 
31850ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3186d763cef2SBarry Smith @*/
31870910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3188d763cef2SBarry Smith {
31898737fe31SLisandro Dalcin   PetscErrorCode ierr;
3190496e6a7aSJed Brown   DM             dm;
31918737fe31SLisandro Dalcin 
3192d763cef2SBarry Smith   PetscFunctionBegin;
31930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31940910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31950910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31966bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31970910c330SBarry Smith   ts->vec_sol = u;
3198bbd56ea5SKarl Rupp 
3199496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
32000910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3201d763cef2SBarry Smith   PetscFunctionReturn(0);
3202d763cef2SBarry Smith }
3203d763cef2SBarry Smith 
3204ac226902SBarry Smith /*@C
3205000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32063f2090d5SJed Brown   called once at the beginning of each time step.
3207000e7ae3SMatthew Knepley 
32083f9fe445SBarry Smith   Logically Collective on TS
3209000e7ae3SMatthew Knepley 
3210000e7ae3SMatthew Knepley   Input Parameters:
3211000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3212000e7ae3SMatthew Knepley - func - The function
3213000e7ae3SMatthew Knepley 
3214000e7ae3SMatthew Knepley   Calling sequence of func:
32156bc98fa9SBarry Smith .   PetscErrorCode func (TS ts);
3216000e7ae3SMatthew Knepley 
3217000e7ae3SMatthew Knepley   Level: intermediate
3218000e7ae3SMatthew Knepley 
3219dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3220000e7ae3SMatthew Knepley @*/
32217087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3222000e7ae3SMatthew Knepley {
3223000e7ae3SMatthew Knepley   PetscFunctionBegin;
32240700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3225ae60f76fSBarry Smith   ts->prestep = func;
3226000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3227000e7ae3SMatthew Knepley }
3228000e7ae3SMatthew Knepley 
322909ee8438SJed Brown /*@
32303f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32313f2090d5SJed Brown 
32323f2090d5SJed Brown   Collective on TS
32333f2090d5SJed Brown 
32343f2090d5SJed Brown   Input Parameters:
32353f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32363f2090d5SJed Brown 
32373f2090d5SJed Brown   Notes:
32383f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32393f2090d5SJed Brown   so most users would not generally call this routine themselves.
32403f2090d5SJed Brown 
32413f2090d5SJed Brown   Level: developer
32423f2090d5SJed Brown 
32439be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32443f2090d5SJed Brown @*/
32457087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32463f2090d5SJed Brown {
32473f2090d5SJed Brown   PetscErrorCode ierr;
32483f2090d5SJed Brown 
32493f2090d5SJed Brown   PetscFunctionBegin;
32500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3251ae60f76fSBarry Smith   if (ts->prestep) {
32525efd42a4SStefano Zampini     Vec              U;
32535efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32545efd42a4SStefano Zampini 
32555efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32565efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3257ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32585efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3259dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3260312ce896SJed Brown   }
32613f2090d5SJed Brown   PetscFunctionReturn(0);
32623f2090d5SJed Brown }
32633f2090d5SJed Brown 
3264b8123daeSJed Brown /*@C
3265b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3266b8123daeSJed Brown   called once at the beginning of each stage.
3267b8123daeSJed Brown 
3268b8123daeSJed Brown   Logically Collective on TS
3269b8123daeSJed Brown 
3270b8123daeSJed Brown   Input Parameters:
3271b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3272b8123daeSJed Brown - func - The function
3273b8123daeSJed Brown 
3274b8123daeSJed Brown   Calling sequence of func:
3275b8123daeSJed Brown .    PetscErrorCode func(TS ts, PetscReal stagetime);
3276b8123daeSJed Brown 
3277b8123daeSJed Brown   Level: intermediate
3278b8123daeSJed Brown 
3279b8123daeSJed Brown   Note:
3280b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
328180275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3282b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3283b8123daeSJed Brown 
32849be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3285b8123daeSJed Brown @*/
3286b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3287b8123daeSJed Brown {
3288b8123daeSJed Brown   PetscFunctionBegin;
3289b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3290ae60f76fSBarry Smith   ts->prestage = func;
3291b8123daeSJed Brown   PetscFunctionReturn(0);
3292b8123daeSJed Brown }
3293b8123daeSJed Brown 
32949be3e283SDebojyoti Ghosh /*@C
32959be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32969be3e283SDebojyoti Ghosh   called once at the end of each stage.
32979be3e283SDebojyoti Ghosh 
32989be3e283SDebojyoti Ghosh   Logically Collective on TS
32999be3e283SDebojyoti Ghosh 
33009be3e283SDebojyoti Ghosh   Input Parameters:
33019be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33029be3e283SDebojyoti Ghosh - func - The function
33039be3e283SDebojyoti Ghosh 
33049be3e283SDebojyoti Ghosh   Calling sequence of func:
33059be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33069be3e283SDebojyoti Ghosh 
33079be3e283SDebojyoti Ghosh   Level: intermediate
33089be3e283SDebojyoti Ghosh 
33099be3e283SDebojyoti Ghosh   Note:
33109be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
331180275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
33129be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33139be3e283SDebojyoti Ghosh 
33149be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33159be3e283SDebojyoti Ghosh @*/
33169be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33179be3e283SDebojyoti Ghosh {
33189be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33199be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33209be3e283SDebojyoti Ghosh   ts->poststage = func;
33219be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33229be3e283SDebojyoti Ghosh }
33239be3e283SDebojyoti Ghosh 
3324c688d042SShri Abhyankar /*@C
3325c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3326c688d042SShri Abhyankar   called once at the end of each step evaluation.
3327c688d042SShri Abhyankar 
3328c688d042SShri Abhyankar   Logically Collective on TS
3329c688d042SShri Abhyankar 
3330c688d042SShri Abhyankar   Input Parameters:
3331c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3332c688d042SShri Abhyankar - func - The function
3333c688d042SShri Abhyankar 
3334c688d042SShri Abhyankar   Calling sequence of func:
3335c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3336c688d042SShri Abhyankar 
3337c688d042SShri Abhyankar   Level: intermediate
3338c688d042SShri Abhyankar 
3339c688d042SShri Abhyankar   Note:
33401785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33411785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3342e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3343e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3344e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3345c688d042SShri Abhyankar 
3346c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3347c688d042SShri Abhyankar @*/
3348c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3349c688d042SShri Abhyankar {
3350c688d042SShri Abhyankar   PetscFunctionBegin;
3351c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3352c688d042SShri Abhyankar   ts->postevaluate = func;
3353c688d042SShri Abhyankar   PetscFunctionReturn(0);
3354c688d042SShri Abhyankar }
3355c688d042SShri Abhyankar 
3356b8123daeSJed Brown /*@
3357b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3358b8123daeSJed Brown 
3359b8123daeSJed Brown   Collective on TS
3360b8123daeSJed Brown 
3361b8123daeSJed Brown   Input Parameters:
3362b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33639be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3364b8123daeSJed Brown 
3365b8123daeSJed Brown   Notes:
3366b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3367b8123daeSJed Brown   most users would not generally call this routine themselves.
3368b8123daeSJed Brown 
3369b8123daeSJed Brown   Level: developer
3370b8123daeSJed Brown 
33719be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3372b8123daeSJed Brown @*/
3373b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3374b8123daeSJed Brown {
3375b8123daeSJed Brown   PetscFunctionBegin;
3376b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3377ae60f76fSBarry Smith   if (ts->prestage) {
3378ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3379b8123daeSJed Brown   }
3380b8123daeSJed Brown   PetscFunctionReturn(0);
3381b8123daeSJed Brown }
3382b8123daeSJed Brown 
33839be3e283SDebojyoti Ghosh /*@
33849be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33859be3e283SDebojyoti Ghosh 
33869be3e283SDebojyoti Ghosh   Collective on TS
33879be3e283SDebojyoti Ghosh 
33889be3e283SDebojyoti Ghosh   Input Parameters:
33899be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33909be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33919be3e283SDebojyoti Ghosh   stageindex  - Stage number
33929be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33939be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33949be3e283SDebojyoti Ghosh 
33959be3e283SDebojyoti Ghosh   Notes:
33969be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33979be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33989be3e283SDebojyoti Ghosh 
33999be3e283SDebojyoti Ghosh   Level: developer
34009be3e283SDebojyoti Ghosh 
34019be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34029be3e283SDebojyoti Ghosh @*/
34039be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34049be3e283SDebojyoti Ghosh {
34059be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34069be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34074beae5d8SLisandro Dalcin   if (ts->poststage) {
34089be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34099be3e283SDebojyoti Ghosh   }
34109be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34119be3e283SDebojyoti Ghosh }
34129be3e283SDebojyoti Ghosh 
3413c688d042SShri Abhyankar /*@
3414c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3415c688d042SShri Abhyankar 
3416c688d042SShri Abhyankar   Collective on TS
3417c688d042SShri Abhyankar 
3418c688d042SShri Abhyankar   Input Parameters:
3419c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3420c688d042SShri Abhyankar 
3421c688d042SShri Abhyankar   Notes:
3422c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3423c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3424c688d042SShri Abhyankar 
3425c688d042SShri Abhyankar   Level: developer
3426c688d042SShri Abhyankar 
3427c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3428c688d042SShri Abhyankar @*/
3429c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3430c688d042SShri Abhyankar {
3431c688d042SShri Abhyankar   PetscErrorCode ierr;
3432c688d042SShri Abhyankar 
3433c688d042SShri Abhyankar   PetscFunctionBegin;
3434c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3435c688d042SShri Abhyankar   if (ts->postevaluate) {
3436dcb233daSLisandro Dalcin     Vec              U;
3437dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3438dcb233daSLisandro Dalcin 
3439dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3440dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3441c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3442dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3443dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3444c688d042SShri Abhyankar   }
3445c688d042SShri Abhyankar   PetscFunctionReturn(0);
3446c688d042SShri Abhyankar }
3447c688d042SShri Abhyankar 
3448ac226902SBarry Smith /*@C
3449000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34503f2090d5SJed Brown   called once at the end of each time step.
3451000e7ae3SMatthew Knepley 
34523f9fe445SBarry Smith   Logically Collective on TS
3453000e7ae3SMatthew Knepley 
3454000e7ae3SMatthew Knepley   Input Parameters:
3455000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3456000e7ae3SMatthew Knepley - func - The function
3457000e7ae3SMatthew Knepley 
3458000e7ae3SMatthew Knepley   Calling sequence of func:
3459b8123daeSJed Brown $ func (TS ts);
3460000e7ae3SMatthew Knepley 
34611785ff2aSShri Abhyankar   Notes:
34621785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34631785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34641785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34651785ff2aSShri Abhyankar 
3466000e7ae3SMatthew Knepley   Level: intermediate
3467000e7ae3SMatthew Knepley 
3468dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3469000e7ae3SMatthew Knepley @*/
34707087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3471000e7ae3SMatthew Knepley {
3472000e7ae3SMatthew Knepley   PetscFunctionBegin;
34730700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3474ae60f76fSBarry Smith   ts->poststep = func;
3475000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3476000e7ae3SMatthew Knepley }
3477000e7ae3SMatthew Knepley 
347809ee8438SJed Brown /*@
34793f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34803f2090d5SJed Brown 
34813f2090d5SJed Brown   Collective on TS
34823f2090d5SJed Brown 
34833f2090d5SJed Brown   Input Parameters:
34843f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34853f2090d5SJed Brown 
34863f2090d5SJed Brown   Notes:
34873f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34883f2090d5SJed Brown   so most users would not generally call this routine themselves.
34893f2090d5SJed Brown 
34903f2090d5SJed Brown   Level: developer
34913f2090d5SJed Brown 
34923f2090d5SJed Brown @*/
34937087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34943f2090d5SJed Brown {
34953f2090d5SJed Brown   PetscErrorCode ierr;
34963f2090d5SJed Brown 
34973f2090d5SJed Brown   PetscFunctionBegin;
34980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3499ae60f76fSBarry Smith   if (ts->poststep) {
35005efd42a4SStefano Zampini     Vec              U;
35015efd42a4SStefano Zampini     PetscObjectState sprev,spost;
35025efd42a4SStefano Zampini 
35035efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
35045efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3505ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
35065efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3507dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
350872ac3e02SJed Brown   }
35093f2090d5SJed Brown   PetscFunctionReturn(0);
35103f2090d5SJed Brown }
35113f2090d5SJed Brown 
3512d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3513d763cef2SBarry Smith 
3514d763cef2SBarry Smith /*@C
3515a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3516d763cef2SBarry Smith    timestep to display the iteration's  progress.
3517d763cef2SBarry Smith 
35183f9fe445SBarry Smith    Logically Collective on TS
3519d763cef2SBarry Smith 
3520d763cef2SBarry Smith    Input Parameters:
3521d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3522e213d8f1SJed Brown .  monitor - monitoring routine
3523329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35240298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3525b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35260298fd71SBarry Smith           (may be NULL)
3527d763cef2SBarry Smith 
3528e213d8f1SJed Brown    Calling sequence of monitor:
3529dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3530d763cef2SBarry Smith 
3531d763cef2SBarry Smith +    ts - the TS context
353263e21af5SBarry 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)
35331f06c33eSBarry Smith .    time - current time
35340910c330SBarry Smith .    u - current iterate
3535d763cef2SBarry Smith -    mctx - [optional] monitoring context
3536d763cef2SBarry Smith 
3537d763cef2SBarry Smith    Notes:
3538d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3539d763cef2SBarry Smith    already has been loaded.
3540d763cef2SBarry Smith 
354195452b02SPatrick Sanan    Fortran Notes:
354295452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3543025f1a04SBarry Smith 
3544d763cef2SBarry Smith    Level: intermediate
3545d763cef2SBarry Smith 
3546a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3547d763cef2SBarry Smith @*/
3548c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3549d763cef2SBarry Smith {
355078064530SBarry Smith   PetscErrorCode ierr;
355178064530SBarry Smith   PetscInt       i;
355278064530SBarry Smith   PetscBool      identical;
355378064530SBarry Smith 
3554d763cef2SBarry Smith   PetscFunctionBegin;
35550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
355678064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
355778064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
355878064530SBarry Smith     if (identical) PetscFunctionReturn(0);
355978064530SBarry Smith   }
356017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3561d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35628704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3563d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3564d763cef2SBarry Smith   PetscFunctionReturn(0);
3565d763cef2SBarry Smith }
3566d763cef2SBarry Smith 
3567d763cef2SBarry Smith /*@C
3568a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3569d763cef2SBarry Smith 
35703f9fe445SBarry Smith    Logically Collective on TS
3571d763cef2SBarry Smith 
3572d763cef2SBarry Smith    Input Parameters:
3573d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3574d763cef2SBarry Smith 
3575d763cef2SBarry Smith    Notes:
3576d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3577d763cef2SBarry Smith 
3578d763cef2SBarry Smith    Level: intermediate
3579d763cef2SBarry Smith 
3580a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3581d763cef2SBarry Smith @*/
35827087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3583d763cef2SBarry Smith {
3584d952e501SBarry Smith   PetscErrorCode ierr;
3585d952e501SBarry Smith   PetscInt       i;
3586d952e501SBarry Smith 
3587d763cef2SBarry Smith   PetscFunctionBegin;
35880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3589d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35908704b422SBarry Smith     if (ts->monitordestroy[i]) {
35918704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3592d952e501SBarry Smith     }
3593d952e501SBarry Smith   }
3594d763cef2SBarry Smith   ts->numbermonitors = 0;
3595d763cef2SBarry Smith   PetscFunctionReturn(0);
3596d763cef2SBarry Smith }
3597d763cef2SBarry Smith 
3598721cd6eeSBarry Smith /*@C
3599721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36005516499fSSatish Balay 
36015516499fSSatish Balay    Level: intermediate
360241251cbbSSatish Balay 
360363e21af5SBarry Smith .seealso:  TSMonitorSet()
360441251cbbSSatish Balay @*/
3605721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3606d763cef2SBarry Smith {
3607dfbe8321SBarry Smith   PetscErrorCode ierr;
3608721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
360941aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3610d132466eSBarry Smith 
3611d763cef2SBarry Smith   PetscFunctionBegin;
36124d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
361341aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
361441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3615721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
361641aca3d6SBarry Smith   if (iascii) {
3617649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
361863e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
361963e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
362063e21af5SBarry Smith     } else {
36218392e04aSShri 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);
362263e21af5SBarry Smith     }
3623649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
362441aca3d6SBarry Smith   } else if (ibinary) {
362541aca3d6SBarry Smith     PetscMPIInt rank;
362641aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
362741aca3d6SBarry Smith     if (!rank) {
3628450a797fSBarry Smith       PetscBool skipHeader;
3629450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3630450a797fSBarry Smith 
3631450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3632450a797fSBarry Smith       if (!skipHeader) {
3633f253e43cSLisandro Dalcin          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
3634450a797fSBarry Smith        }
363541aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
363641aca3d6SBarry Smith     } else {
363741aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
363841aca3d6SBarry Smith     }
363941aca3d6SBarry Smith   }
3640721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3641d763cef2SBarry Smith   PetscFunctionReturn(0);
3642d763cef2SBarry Smith }
3643d763cef2SBarry Smith 
3644cc9c3a59SBarry Smith /*@C
3645cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3646cc9c3a59SBarry Smith 
3647cc9c3a59SBarry Smith    Level: intermediate
3648cc9c3a59SBarry Smith 
3649cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3650cc9c3a59SBarry Smith @*/
3651cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3652cc9c3a59SBarry Smith {
3653cc9c3a59SBarry Smith   PetscErrorCode ierr;
3654cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3655cc9c3a59SBarry Smith   PetscBool      iascii;
3656cc9c3a59SBarry Smith   PetscReal      max,min;
3657cc9c3a59SBarry Smith 
3658cc9c3a59SBarry Smith 
3659cc9c3a59SBarry Smith   PetscFunctionBegin;
3660cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3661cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3662cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3663cc9c3a59SBarry Smith   if (iascii) {
3664cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3665cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3666cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
36675132926bSBarry 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);
3668cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3669cc9c3a59SBarry Smith   }
3670cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3671cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3672cc9c3a59SBarry Smith }
3673cc9c3a59SBarry Smith 
3674cd652676SJed Brown /*@
3675cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3676cd652676SJed Brown 
3677cd652676SJed Brown    Collective on TS
3678cd652676SJed Brown 
3679cd652676SJed Brown    Input Argument:
3680cd652676SJed Brown +  ts - time stepping context
3681cd652676SJed Brown -  t - time to interpolate to
3682cd652676SJed Brown 
3683cd652676SJed Brown    Output Argument:
36840910c330SBarry Smith .  U - state at given time
3685cd652676SJed Brown 
3686cd652676SJed Brown    Level: intermediate
3687cd652676SJed Brown 
3688cd652676SJed Brown    Developer Notes:
3689cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3690cd652676SJed Brown 
3691874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3692cd652676SJed Brown @*/
36930910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3694cd652676SJed Brown {
3695cd652676SJed Brown   PetscErrorCode ierr;
3696cd652676SJed Brown 
3697cd652676SJed Brown   PetscFunctionBegin;
3698cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3699b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3700be5899b3SLisandro 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);
3701ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
37020910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3703cd652676SJed Brown   PetscFunctionReturn(0);
3704cd652676SJed Brown }
3705cd652676SJed Brown 
3706d763cef2SBarry Smith /*@
37076d9e5789SSean Farley    TSStep - Steps one time step
3708d763cef2SBarry Smith 
3709d763cef2SBarry Smith    Collective on TS
3710d763cef2SBarry Smith 
3711d763cef2SBarry Smith    Input Parameter:
3712d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3713d763cef2SBarry Smith 
371427829d71SBarry Smith    Level: developer
3715d763cef2SBarry Smith 
3716b8123daeSJed Brown    Notes:
371727829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
371827829d71SBarry Smith 
3719b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3720b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3721b8123daeSJed Brown 
372219eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
372319eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
372425cb2221SBarry Smith 
37259be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3726d763cef2SBarry Smith @*/
3727193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3728d763cef2SBarry Smith {
3729dfbe8321SBarry Smith   PetscErrorCode   ierr;
3730fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3731be5899b3SLisandro Dalcin   PetscReal        ptime;
3732d763cef2SBarry Smith 
3733d763cef2SBarry Smith   PetscFunctionBegin;
37340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3735f1d62c27SHong Zhang   ierr = PetscCitationsRegister("@article{tspaper,\n"
3736fffbeea8SBarry Smith                                 "  title         = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3737f1d62c27SHong Zhang                                 "  author        = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n"
3738f1d62c27SHong Zhang                                 "  journal       = {arXiv e-preprints},\n"
3739f1d62c27SHong Zhang                                 "  eprint        = {1806.01437},\n"
3740f1d62c27SHong Zhang                                 "  archivePrefix = {arXiv},\n"
3741f1d62c27SHong Zhang                                 "  year          = {2018}\n}\n",&cite);CHKERRQ(ierr);
3742fffbeea8SBarry Smith 
3743d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
374468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3745d405a339SMatthew Knepley 
3746fc8dbba5SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3747ef85077eSLisandro 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>");
3748a6772fa2SLisandro 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()");
3749be5899b3SLisandro 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");
3750a6772fa2SLisandro Dalcin 
3751be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3752be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37532808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3754fc8dbba5SLisandro Dalcin 
3755d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3756193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3757d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3758fc8dbba5SLisandro Dalcin 
3759fc8dbba5SLisandro Dalcin   if (ts->reason >= 0) {
3760be5899b3SLisandro Dalcin     ts->ptime_prev = ptime;
37612808aa04SLisandro Dalcin     ts->steps++;
3762be5899b3SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
376328d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_FALSE;
3764d2daff3dSHong Zhang   }
3765fc8dbba5SLisandro Dalcin 
3766fc8dbba5SLisandro Dalcin   if (!ts->reason) {
376708c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
376808c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
376908c7845fSBarry Smith   }
3770fc8dbba5SLisandro Dalcin 
3771fc8dbba5SLisandro 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]);
3772fc8dbba5SLisandro Dalcin   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
377308c7845fSBarry Smith   PetscFunctionReturn(0);
377408c7845fSBarry Smith }
377508c7845fSBarry Smith 
377608c7845fSBarry Smith /*@
37777cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37787cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37797cbde773SLisandro Dalcin 
37807cbde773SLisandro Dalcin    Collective on TS
37817cbde773SLisandro Dalcin 
37827cbde773SLisandro Dalcin    Input Arguments:
37837cbde773SLisandro Dalcin +  ts - time stepping context
37847cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37857cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37867cbde773SLisandro Dalcin 
37877cbde773SLisandro Dalcin    Output Arguments:
37887cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37897cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37907cbde773SLisandro Dalcin 
37917cbde773SLisandro Dalcin    Level: advanced
37927cbde773SLisandro Dalcin 
37937cbde773SLisandro Dalcin    Notes:
37947cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37957cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37967cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37977cbde773SLisandro Dalcin 
37987cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37997cbde773SLisandro Dalcin @*/
38007cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
38017cbde773SLisandro Dalcin {
38027cbde773SLisandro Dalcin   PetscErrorCode ierr;
38037cbde773SLisandro Dalcin 
38047cbde773SLisandro Dalcin   PetscFunctionBegin;
38057cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38067cbde773SLisandro Dalcin   PetscValidType(ts,1);
38077cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
38087cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
38097cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
38107cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
38117cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
38127cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
38137cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
38147cbde773SLisandro Dalcin   PetscFunctionReturn(0);
38157cbde773SLisandro Dalcin }
38167cbde773SLisandro Dalcin 
381705175c85SJed Brown /*@
381805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
381905175c85SJed Brown 
38201c3436cfSJed Brown    Collective on TS
382105175c85SJed Brown 
382205175c85SJed Brown    Input Arguments:
38231c3436cfSJed Brown +  ts - time stepping context
38241c3436cfSJed Brown .  order - desired order of accuracy
38250298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
382605175c85SJed Brown 
382705175c85SJed Brown    Output Arguments:
38280910c330SBarry Smith .  U - state at the end of the current step
382905175c85SJed Brown 
383005175c85SJed Brown    Level: advanced
383105175c85SJed Brown 
3832108c343cSJed Brown    Notes:
3833108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3834108c343cSJed 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.
3835108c343cSJed Brown 
38361c3436cfSJed Brown .seealso: TSStep(), TSAdapt
383705175c85SJed Brown @*/
38380910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
383905175c85SJed Brown {
384005175c85SJed Brown   PetscErrorCode ierr;
384105175c85SJed Brown 
384205175c85SJed Brown   PetscFunctionBegin;
384305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
384405175c85SJed Brown   PetscValidType(ts,1);
38450910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3846ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38470910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
384805175c85SJed Brown   PetscFunctionReturn(0);
384905175c85SJed Brown }
385005175c85SJed Brown 
3851aad739acSMatthew G. Knepley /*@C
38522e61be88SMatthew G. Knepley   TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping.
3853aad739acSMatthew G. Knepley 
3854aad739acSMatthew G. Knepley   Not collective
3855aad739acSMatthew G. Knepley 
3856aad739acSMatthew G. Knepley   Input Argument:
3857aad739acSMatthew G. Knepley . ts        - time stepping context
3858aad739acSMatthew G. Knepley 
3859aad739acSMatthew G. Knepley   Output Argument:
38602e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition
3861aad739acSMatthew G. Knepley 
3862aad739acSMatthew G. Knepley    Level: advanced
3863aad739acSMatthew G. Knepley 
3864aad739acSMatthew G. Knepley    Notes:
3865aad739acSMatthew G. Knepley    The calling sequence for the function is
38662e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u)
3867aad739acSMatthew G. Knepley $ ts - The timestepping context
38682e61be88SMatthew G. Knepley $ u  - The input vector in which the initial condition is stored
3869aad739acSMatthew G. Knepley 
38702e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition()
3871aad739acSMatthew G. Knepley @*/
38722e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec))
3873aad739acSMatthew G. Knepley {
3874aad739acSMatthew G. Knepley   PetscFunctionBegin;
3875aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
38762e61be88SMatthew G. Knepley   PetscValidPointer(initCondition, 2);
38772e61be88SMatthew G. Knepley   *initCondition = ts->ops->initcondition;
3878aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3879aad739acSMatthew G. Knepley }
3880aad739acSMatthew G. Knepley 
3881aad739acSMatthew G. Knepley /*@C
38822e61be88SMatthew G. Knepley   TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping.
3883aad739acSMatthew G. Knepley 
3884aad739acSMatthew G. Knepley   Logically collective on ts
3885aad739acSMatthew G. Knepley 
3886aad739acSMatthew G. Knepley   Input Arguments:
3887aad739acSMatthew G. Knepley + ts        - time stepping context
38882e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition
3889aad739acSMatthew G. Knepley 
3890aad739acSMatthew G. Knepley   Level: advanced
3891aad739acSMatthew G. Knepley 
3892a96d6ef6SBarry Smith   Calling sequence for initCondition:
3893a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u)
3894a96d6ef6SBarry Smith 
3895a96d6ef6SBarry Smith + ts - The timestepping context
3896a96d6ef6SBarry Smith - u  - The input vector in which the initial condition is to be stored
3897aad739acSMatthew G. Knepley 
38982e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition()
3899aad739acSMatthew G. Knepley @*/
39002e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec))
3901aad739acSMatthew G. Knepley {
3902aad739acSMatthew G. Knepley   PetscFunctionBegin;
3903aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
39042e61be88SMatthew G. Knepley   PetscValidFunction(initCondition, 2);
39052e61be88SMatthew G. Knepley   ts->ops->initcondition = initCondition;
3906aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3907aad739acSMatthew G. Knepley }
3908aad739acSMatthew G. Knepley 
3909aad739acSMatthew G. Knepley /*@
39102e61be88SMatthew G. Knepley   TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set.
3911aad739acSMatthew G. Knepley 
3912aad739acSMatthew G. Knepley   Collective on ts
3913aad739acSMatthew G. Knepley 
3914aad739acSMatthew G. Knepley   Input Arguments:
3915aad739acSMatthew G. Knepley + ts - time stepping context
39162e61be88SMatthew G. Knepley - u  - The Vec to store the condition in which will be used in TSSolve()
3917aad739acSMatthew G. Knepley 
3918aad739acSMatthew G. Knepley   Level: advanced
3919aad739acSMatthew G. Knepley 
39202e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
3921aad739acSMatthew G. Knepley @*/
39222e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u)
3923aad739acSMatthew G. Knepley {
3924aad739acSMatthew G. Knepley   PetscErrorCode ierr;
3925aad739acSMatthew G. Knepley 
3926aad739acSMatthew G. Knepley   PetscFunctionBegin;
3927aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3928aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
39292e61be88SMatthew G. Knepley   if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);}
3930aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3931aad739acSMatthew G. Knepley }
3932aad739acSMatthew G. Knepley 
3933aad739acSMatthew G. Knepley /*@C
3934aad739acSMatthew G. Knepley   TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping.
3935aad739acSMatthew G. Knepley 
3936aad739acSMatthew G. Knepley   Not collective
3937aad739acSMatthew G. Knepley 
3938aad739acSMatthew G. Knepley   Input Argument:
3939aad739acSMatthew G. Knepley . ts         - time stepping context
3940aad739acSMatthew G. Knepley 
3941aad739acSMatthew G. Knepley   Output Argument:
3942aad739acSMatthew G. Knepley . exactError - The function which computes the solution error
3943aad739acSMatthew G. Knepley 
3944aad739acSMatthew G. Knepley   Level: advanced
3945aad739acSMatthew G. Knepley 
3946a96d6ef6SBarry Smith   Calling sequence for exactError:
3947a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3948a96d6ef6SBarry Smith 
3949a96d6ef6SBarry Smith + ts - The timestepping context
3950a96d6ef6SBarry Smith . u  - The approximate solution vector
3951a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3952aad739acSMatthew G. Knepley 
3953aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3954aad739acSMatthew G. Knepley @*/
3955aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec))
3956aad739acSMatthew G. Knepley {
3957aad739acSMatthew G. Knepley   PetscFunctionBegin;
3958aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3959aad739acSMatthew G. Knepley   PetscValidPointer(exactError, 2);
3960aad739acSMatthew G. Knepley   *exactError = ts->ops->exacterror;
3961aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3962aad739acSMatthew G. Knepley }
3963aad739acSMatthew G. Knepley 
3964aad739acSMatthew G. Knepley /*@C
3965aad739acSMatthew G. Knepley   TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping.
3966aad739acSMatthew G. Knepley 
3967aad739acSMatthew G. Knepley   Logically collective on ts
3968aad739acSMatthew G. Knepley 
3969aad739acSMatthew G. Knepley   Input Arguments:
3970aad739acSMatthew G. Knepley + ts         - time stepping context
3971aad739acSMatthew G. Knepley - exactError - The function which computes the solution error
3972aad739acSMatthew G. Knepley 
3973aad739acSMatthew G. Knepley   Level: advanced
3974aad739acSMatthew G. Knepley 
3975a96d6ef6SBarry Smith   Calling sequence for exactError:
3976a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u)
3977a96d6ef6SBarry Smith 
3978a96d6ef6SBarry Smith + ts - The timestepping context
3979a96d6ef6SBarry Smith . u  - The approximate solution vector
3980a96d6ef6SBarry Smith - e  - The input vector in which the error is stored
3981aad739acSMatthew G. Knepley 
3982aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError()
3983aad739acSMatthew G. Knepley @*/
3984aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec))
3985aad739acSMatthew G. Knepley {
3986aad739acSMatthew G. Knepley   PetscFunctionBegin;
3987aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
3988f907fdbfSMatthew G. Knepley   PetscValidFunction(exactError, 2);
3989aad739acSMatthew G. Knepley   ts->ops->exacterror = exactError;
3990aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
3991aad739acSMatthew G. Knepley }
3992aad739acSMatthew G. Knepley 
3993aad739acSMatthew G. Knepley /*@
3994aad739acSMatthew G. Knepley   TSComputeExactError - Compute the solution error for the timestepping using the function previously set.
3995aad739acSMatthew G. Knepley 
3996aad739acSMatthew G. Knepley   Collective on ts
3997aad739acSMatthew G. Knepley 
3998aad739acSMatthew G. Knepley   Input Arguments:
3999aad739acSMatthew G. Knepley + ts - time stepping context
4000aad739acSMatthew G. Knepley . u  - The approximate solution
4001aad739acSMatthew G. Knepley - e  - The Vec used to store the error
4002aad739acSMatthew G. Knepley 
4003aad739acSMatthew G. Knepley   Level: advanced
4004aad739acSMatthew G. Knepley 
40052e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve()
4006aad739acSMatthew G. Knepley @*/
4007aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e)
4008aad739acSMatthew G. Knepley {
4009aad739acSMatthew G. Knepley   PetscErrorCode ierr;
4010aad739acSMatthew G. Knepley 
4011aad739acSMatthew G. Knepley   PetscFunctionBegin;
4012aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
4013aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(u, VEC_CLASSID, 2);
4014aad739acSMatthew G. Knepley   PetscValidHeaderSpecific(e, VEC_CLASSID, 3);
4015aad739acSMatthew G. Knepley   if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);}
4016aad739acSMatthew G. Knepley   PetscFunctionReturn(0);
4017aad739acSMatthew G. Knepley }
4018aad739acSMatthew G. Knepley 
4019b1cb36f3SHong Zhang /*@
40206a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
40216a4d4014SLisandro Dalcin 
40226a4d4014SLisandro Dalcin    Collective on TS
40236a4d4014SLisandro Dalcin 
40246a4d4014SLisandro Dalcin    Input Parameter:
40256a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
402663e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
402763e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
40285a3a76d0SJed Brown 
40296a4d4014SLisandro Dalcin    Level: beginner
40306a4d4014SLisandro Dalcin 
40315a3a76d0SJed Brown    Notes:
40325a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
40335a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40345a3a76d0SJed Brown    stepped over the final time.
40355a3a76d0SJed Brown 
403663e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
40376a4d4014SLisandro Dalcin @*/
4038cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
40396a4d4014SLisandro Dalcin {
4040b06615a5SLisandro Dalcin   Vec               solution;
40416a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
4042f22f69f0SBarry Smith 
40436a4d4014SLisandro Dalcin   PetscFunctionBegin;
40440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4045f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4046303a5415SBarry Smith 
4047303a5415SBarry Smith   ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr);
4048ee41a567SStefano 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 */
40490910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4050b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4051b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4052b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40535a3a76d0SJed Brown     }
40540910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4055715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4056bbd56ea5SKarl Rupp   } else if (u) {
40570910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40585a3a76d0SJed Brown   }
4059b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
406068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4061a6772fa2SLisandro Dalcin 
4062ef85077eSLisandro 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>");
4063a6772fa2SLisandro 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()");
4064a6772fa2SLisandro 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");
4065a6772fa2SLisandro Dalcin 
4066715f1b00SHong Zhang   if (ts->forward_solve) {
4067715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4068715f1b00SHong Zhang   }
4069715f1b00SHong Zhang 
4070e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4071715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4072e7069c78SShri      TSTrajectory to incorrectly save the output files
4073e7069c78SShri   */
4074715f1b00SHong Zhang   /* reset time step and iteration counters */
40752808aa04SLisandro Dalcin   if (!ts->steps) {
40765ef26d82SJed Brown     ts->ksp_its           = 0;
40775ef26d82SJed Brown     ts->snes_its          = 0;
4078c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4079c610991cSLisandro Dalcin     ts->reject            = 0;
40802808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
40812808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
40827d51462cSStefano Zampini     ts->rhsjacobian.time  = PETSC_MIN_REAL;
40832808aa04SLisandro Dalcin   }
4084e97c63d7SStefano Zampini 
4085e97c63d7SStefano Zampini   /* make sure initial time step does not overshoot final time */
4086e97c63d7SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) {
4087e97c63d7SStefano Zampini     PetscReal maxdt = ts->max_time-ts->ptime;
4088e97c63d7SStefano Zampini     PetscReal dt = ts->time_step;
4089e97c63d7SStefano Zampini 
4090e97c63d7SStefano Zampini     ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt);
4091e97c63d7SStefano Zampini   }
4092193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4093193ac0bcSJed Brown 
4094900f6b5bSMatthew G. Knepley   {
4095900f6b5bSMatthew G. Knepley     PetscViewer       viewer;
4096900f6b5bSMatthew G. Knepley     PetscViewerFormat format;
4097900f6b5bSMatthew G. Knepley     PetscBool         flg;
4098900f6b5bSMatthew G. Knepley     static PetscBool  incall = PETSC_FALSE;
4099900f6b5bSMatthew G. Knepley 
4100900f6b5bSMatthew G. Knepley     if (!incall) {
4101900f6b5bSMatthew G. Knepley       /* Estimate the convergence rate of the time discretization */
4102900f6b5bSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr);
4103900f6b5bSMatthew G. Knepley       if (flg) {
4104900f6b5bSMatthew G. Knepley         PetscConvEst conv;
4105900f6b5bSMatthew G. Knepley         DM           dm;
4106900f6b5bSMatthew G. Knepley         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4107900f6b5bSMatthew G. Knepley         PetscInt     Nf;
4108f2ed2dc7SMatthew G. Knepley         PetscBool    checkTemporal = PETSC_TRUE;
4109900f6b5bSMatthew G. Knepley 
4110900f6b5bSMatthew G. Knepley         incall = PETSC_TRUE;
4111f2ed2dc7SMatthew G. Knepley         ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr);
4112900f6b5bSMatthew G. Knepley         ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
4113900f6b5bSMatthew G. Knepley         ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4114900f6b5bSMatthew G. Knepley         ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr);
4115900f6b5bSMatthew G. Knepley         ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr);
4116f2ed2dc7SMatthew G. Knepley         ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr);
4117900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr);
4118900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr);
4119900f6b5bSMatthew G. Knepley         ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr);
4120900f6b5bSMatthew G. Knepley         ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr);
4121900f6b5bSMatthew G. Knepley         ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
4122900f6b5bSMatthew G. Knepley         ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr);
4123900f6b5bSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4124900f6b5bSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4125900f6b5bSMatthew G. Knepley         ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr);
4126900f6b5bSMatthew G. Knepley         ierr = PetscFree(alpha);CHKERRQ(ierr);
4127900f6b5bSMatthew G. Knepley         incall = PETSC_FALSE;
4128900f6b5bSMatthew G. Knepley       }
4129900f6b5bSMatthew G. Knepley     }
4130900f6b5bSMatthew G. Knepley   }
4131900f6b5bSMatthew G. Knepley 
4132ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4133f05ece33SBarry Smith 
4134193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4135193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4136a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4137cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4138a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4139be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4140db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4141db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4142e7069c78SShri 
41432808aa04SLisandro Dalcin     if (!ts->steps) {
41442808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41456427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41462808aa04SLisandro Dalcin     }
41476427ac75SLisandro Dalcin 
4148e1a7a14fSJed Brown     while (!ts->reason) {
41492808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41509687d888SLisandro Dalcin       if (!ts->steprollback) {
41519687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41529687d888SLisandro Dalcin       }
4153193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4154f3b1f45cSBarry Smith       if (ts->testjacobian) {
4155f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4156f3b1f45cSBarry Smith       }
4157f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4158f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4159f3b1f45cSBarry Smith       }
4160cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
41617b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4162b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
41637b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4164b1cb36f3SHong Zhang       }
416558818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
41667b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */
4167715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
41687b0e2f17SHong Zhang         if (ts->reason >= 0) ts->steps++;
4169715f1b00SHong Zhang       }
417010b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4171e783b05fSHong 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. */
417258818c2dSLisandro Dalcin       if (ts->steprollback) {
417358818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
417458818c2dSLisandro Dalcin       }
4175e783b05fSHong Zhang       if (!ts->steprollback) {
41762808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41771eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4178aeb4809dSShri Abhyankar       }
4179193ac0bcSJed Brown     }
41802808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41816427ac75SLisandro Dalcin 
418249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41830910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4184cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4185b06615a5SLisandro Dalcin       solution = u;
418663e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41870574a7fbSJed Brown     } else {
4188ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4189cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4190b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41910574a7fbSJed Brown     }
4192193ac0bcSJed Brown   }
4193d2daff3dSHong Zhang 
4194ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
419563e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
419656f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4197ef222394SBarry Smith   if (ts->adjoint_solve) {
4198ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41992b0a91c0SBarry Smith   }
42006a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
42016a4d4014SLisandro Dalcin }
42026a4d4014SLisandro Dalcin 
42037db568b7SBarry Smith /*@C
4204228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4205228d79bcSJed Brown 
4206228d79bcSJed Brown    Collective on TS
4207228d79bcSJed Brown 
4208228d79bcSJed Brown    Input Parameters:
4209228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4210228d79bcSJed Brown .  step - step number that has just completed
4211228d79bcSJed Brown .  ptime - model time of the state
42120910c330SBarry Smith -  u - state at the current model time
4213228d79bcSJed Brown 
4214228d79bcSJed Brown    Notes:
42157db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
42167db568b7SBarry Smith    Users would almost never call this routine directly.
4217228d79bcSJed Brown 
421863e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
421963e21af5SBarry Smith 
42207db568b7SBarry Smith    Level: developer
4221228d79bcSJed Brown 
4222228d79bcSJed Brown @*/
42230910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4224d763cef2SBarry Smith {
4225d6152f81SLisandro Dalcin   DM             dm;
4226a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4227d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4228d763cef2SBarry Smith 
4229d763cef2SBarry Smith   PetscFunctionBegin;
4230b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4231b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4232d6152f81SLisandro Dalcin 
4233d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4234d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4235d6152f81SLisandro Dalcin 
42368860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
4237d763cef2SBarry Smith   for (i=0; i<n; i++) {
42380910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4239d763cef2SBarry Smith   }
42408860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
4241d763cef2SBarry Smith   PetscFunctionReturn(0);
4242d763cef2SBarry Smith }
4243d763cef2SBarry Smith 
4244d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4245d763cef2SBarry Smith /*@C
42467db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4247a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4248d763cef2SBarry Smith 
4249d763cef2SBarry Smith    Collective on TS
4250d763cef2SBarry Smith 
4251d763cef2SBarry Smith    Input Parameters:
4252d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4253d763cef2SBarry Smith .  label - the title to put in the title bar
42547c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4255a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4256a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4257d763cef2SBarry Smith 
4258d763cef2SBarry Smith    Output Parameter:
42590b039ecaSBarry Smith .  ctx - the context
4260d763cef2SBarry Smith 
4261d763cef2SBarry Smith    Options Database Key:
42624f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
42638b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
42647db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
42657db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
42667db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
42677db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4268b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4269d763cef2SBarry Smith 
4270d763cef2SBarry Smith    Notes:
4271a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4272d763cef2SBarry Smith 
42737db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
42747db568b7SBarry Smith 
42757db568b7SBarry 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
42767db568b7SBarry 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
42777db568b7SBarry Smith    as the first argument.
42787db568b7SBarry Smith 
42797db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
42807db568b7SBarry Smith 
4281d763cef2SBarry Smith    Level: intermediate
4282d763cef2SBarry Smith 
42837db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
42847db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
42857db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
42867db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
42877db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
42887c922b88SBarry Smith 
4289d763cef2SBarry Smith @*/
4290a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4291d763cef2SBarry Smith {
42927f52daa2SLisandro Dalcin   PetscDraw      draw;
4293dfbe8321SBarry Smith   PetscErrorCode ierr;
4294d763cef2SBarry Smith 
4295d763cef2SBarry Smith   PetscFunctionBegin;
4296b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
42977f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
42987f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
42997f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4300287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43017f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4302a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4303d763cef2SBarry Smith   PetscFunctionReturn(0);
4304d763cef2SBarry Smith }
4305d763cef2SBarry Smith 
4306b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4307d763cef2SBarry Smith {
43080b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4309c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4310dfbe8321SBarry Smith   PetscErrorCode ierr;
4311d763cef2SBarry Smith 
4312d763cef2SBarry Smith   PetscFunctionBegin;
431363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4314b06615a5SLisandro Dalcin   if (!step) {
4315a9f9c1f6SBarry Smith     PetscDrawAxis axis;
43168b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4317a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
43188b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4319a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4320a9f9c1f6SBarry Smith   }
4321c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
43228b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
43230b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4324b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
43250b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
43266934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
43273923b477SBarry Smith   }
4328d763cef2SBarry Smith   PetscFunctionReturn(0);
4329d763cef2SBarry Smith }
4330d763cef2SBarry Smith 
4331d763cef2SBarry Smith /*@C
4332a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4333a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4334d763cef2SBarry Smith 
43350b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4336d763cef2SBarry Smith 
4337d763cef2SBarry Smith    Input Parameter:
43380b039ecaSBarry Smith .  ctx - the monitor context
4339d763cef2SBarry Smith 
4340d763cef2SBarry Smith    Level: intermediate
4341d763cef2SBarry Smith 
43424f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4343d763cef2SBarry Smith @*/
4344a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4345d763cef2SBarry Smith {
4346dfbe8321SBarry Smith   PetscErrorCode ierr;
4347d763cef2SBarry Smith 
4348d763cef2SBarry Smith   PetscFunctionBegin;
43497684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
43507684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
43517684fa3eSBarry Smith   }
43520b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
435331152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4354387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4355387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4356387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
43570b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4358d763cef2SBarry Smith   PetscFunctionReturn(0);
4359d763cef2SBarry Smith }
4360d763cef2SBarry Smith 
43611b575b74SJoseph Pusztay /*
43621b575b74SJoseph Pusztay 
43631b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
43641b575b74SJoseph Pusztay 
43651b575b74SJoseph Pusztay */
43661b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
43671b575b74SJoseph Pusztay {
43681b575b74SJoseph Pusztay   PetscDraw      draw;
43691b575b74SJoseph Pusztay   PetscErrorCode ierr;
43701b575b74SJoseph Pusztay 
43711b575b74SJoseph Pusztay   PetscFunctionBegin;
43721b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
43731b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43741b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43751b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
43761b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
43771b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
43781b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43791b575b74SJoseph Pusztay 
43801b575b74SJoseph Pusztay }
43811b575b74SJoseph Pusztay 
43821b575b74SJoseph Pusztay /*
43831b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
43841b575b74SJoseph Pusztay */
43851b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
43861b575b74SJoseph Pusztay {
43871b575b74SJoseph Pusztay   PetscErrorCode ierr;
43881b575b74SJoseph Pusztay 
43891b575b74SJoseph Pusztay   PetscFunctionBegin;
43901b575b74SJoseph Pusztay 
43911b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
43921b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
43931b575b74SJoseph Pusztay 
43941b575b74SJoseph Pusztay   PetscFunctionReturn(0);
43951b575b74SJoseph Pusztay 
43961b575b74SJoseph Pusztay }
43971b575b74SJoseph Pusztay 
4398d763cef2SBarry Smith /*@
4399b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4400d763cef2SBarry Smith 
4401d763cef2SBarry Smith    Not Collective
4402d763cef2SBarry Smith 
4403d763cef2SBarry Smith    Input Parameter:
4404d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4405d763cef2SBarry Smith 
4406d763cef2SBarry Smith    Output Parameter:
440719eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4408d763cef2SBarry Smith 
4409d763cef2SBarry Smith    Level: beginner
4410d763cef2SBarry Smith 
4411b8123daeSJed Brown    Note:
4412b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
44139be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4414b8123daeSJed Brown 
44158f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4416d763cef2SBarry Smith 
4417d763cef2SBarry Smith @*/
44187087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4419d763cef2SBarry Smith {
4420d763cef2SBarry Smith   PetscFunctionBegin;
44210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4422f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4423d763cef2SBarry Smith   *t = ts->ptime;
4424d763cef2SBarry Smith   PetscFunctionReturn(0);
4425d763cef2SBarry Smith }
4426d763cef2SBarry Smith 
4427e5e524a1SHong Zhang /*@
4428e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4429e5e524a1SHong Zhang 
4430e5e524a1SHong Zhang    Not Collective
4431e5e524a1SHong Zhang 
4432e5e524a1SHong Zhang    Input Parameter:
4433e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4434e5e524a1SHong Zhang 
4435e5e524a1SHong Zhang    Output Parameter:
4436e5e524a1SHong Zhang .  t  - the previous time
4437e5e524a1SHong Zhang 
4438e5e524a1SHong Zhang    Level: beginner
4439e5e524a1SHong Zhang 
4440aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4441e5e524a1SHong Zhang 
4442e5e524a1SHong Zhang @*/
4443e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4444e5e524a1SHong Zhang {
4445e5e524a1SHong Zhang   PetscFunctionBegin;
4446e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4447e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4448e5e524a1SHong Zhang   *t = ts->ptime_prev;
4449e5e524a1SHong Zhang   PetscFunctionReturn(0);
4450e5e524a1SHong Zhang }
4451e5e524a1SHong Zhang 
44526a4d4014SLisandro Dalcin /*@
44536a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
44546a4d4014SLisandro Dalcin 
44553f9fe445SBarry Smith    Logically Collective on TS
44566a4d4014SLisandro Dalcin 
44576a4d4014SLisandro Dalcin    Input Parameters:
44586a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
44596a4d4014SLisandro Dalcin -  time - the time
44606a4d4014SLisandro Dalcin 
44616a4d4014SLisandro Dalcin    Level: intermediate
44626a4d4014SLisandro Dalcin 
446319eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
44646a4d4014SLisandro Dalcin 
44656a4d4014SLisandro Dalcin @*/
44667087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
44676a4d4014SLisandro Dalcin {
44686a4d4014SLisandro Dalcin   PetscFunctionBegin;
44690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4470c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
44716a4d4014SLisandro Dalcin   ts->ptime = t;
44726a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
44736a4d4014SLisandro Dalcin }
44746a4d4014SLisandro Dalcin 
4475d763cef2SBarry Smith /*@C
4476d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4477d763cef2SBarry Smith    TS options in the database.
4478d763cef2SBarry Smith 
44793f9fe445SBarry Smith    Logically Collective on TS
4480d763cef2SBarry Smith 
4481d763cef2SBarry Smith    Input Parameter:
4482d763cef2SBarry Smith +  ts     - The TS context
4483d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4484d763cef2SBarry Smith 
4485d763cef2SBarry Smith    Notes:
4486d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4487d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4488d763cef2SBarry Smith    hyphen.
4489d763cef2SBarry Smith 
4490d763cef2SBarry Smith    Level: advanced
4491d763cef2SBarry Smith 
4492d763cef2SBarry Smith .seealso: TSSetFromOptions()
4493d763cef2SBarry Smith 
4494d763cef2SBarry Smith @*/
44957087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4496d763cef2SBarry Smith {
4497dfbe8321SBarry Smith   PetscErrorCode ierr;
4498089b2837SJed Brown   SNES           snes;
4499d763cef2SBarry Smith 
4500d763cef2SBarry Smith   PetscFunctionBegin;
45010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4502d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4503089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4504089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4505d763cef2SBarry Smith   PetscFunctionReturn(0);
4506d763cef2SBarry Smith }
4507d763cef2SBarry Smith 
4508d763cef2SBarry Smith /*@C
4509d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4510d763cef2SBarry Smith    TS options in the database.
4511d763cef2SBarry Smith 
45123f9fe445SBarry Smith    Logically Collective on TS
4513d763cef2SBarry Smith 
4514d763cef2SBarry Smith    Input Parameter:
4515d763cef2SBarry Smith +  ts     - The TS context
4516d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4517d763cef2SBarry Smith 
4518d763cef2SBarry Smith    Notes:
4519d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4520d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4521d763cef2SBarry Smith    hyphen.
4522d763cef2SBarry Smith 
4523d763cef2SBarry Smith    Level: advanced
4524d763cef2SBarry Smith 
4525d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4526d763cef2SBarry Smith 
4527d763cef2SBarry Smith @*/
45287087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4529d763cef2SBarry Smith {
4530dfbe8321SBarry Smith   PetscErrorCode ierr;
4531089b2837SJed Brown   SNES           snes;
4532d763cef2SBarry Smith 
4533d763cef2SBarry Smith   PetscFunctionBegin;
45340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4535d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4536089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4537089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4538d763cef2SBarry Smith   PetscFunctionReturn(0);
4539d763cef2SBarry Smith }
4540d763cef2SBarry Smith 
4541d763cef2SBarry Smith /*@C
4542d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4543d763cef2SBarry Smith    TS options in the database.
4544d763cef2SBarry Smith 
4545d763cef2SBarry Smith    Not Collective
4546d763cef2SBarry Smith 
4547d763cef2SBarry Smith    Input Parameter:
4548d763cef2SBarry Smith .  ts - The TS context
4549d763cef2SBarry Smith 
4550d763cef2SBarry Smith    Output Parameter:
4551d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4552d763cef2SBarry Smith 
455395452b02SPatrick Sanan    Notes:
455495452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4555d763cef2SBarry Smith    sufficient length to hold the prefix.
4556d763cef2SBarry Smith 
4557d763cef2SBarry Smith    Level: intermediate
4558d763cef2SBarry Smith 
4559d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4560d763cef2SBarry Smith @*/
45617087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4562d763cef2SBarry Smith {
4563dfbe8321SBarry Smith   PetscErrorCode ierr;
4564d763cef2SBarry Smith 
4565d763cef2SBarry Smith   PetscFunctionBegin;
45660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45674482741eSBarry Smith   PetscValidPointer(prefix,2);
4568d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4569d763cef2SBarry Smith   PetscFunctionReturn(0);
4570d763cef2SBarry Smith }
4571d763cef2SBarry Smith 
4572d763cef2SBarry Smith /*@C
4573d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4574d763cef2SBarry Smith 
4575d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4576d763cef2SBarry Smith 
4577d763cef2SBarry Smith    Input Parameter:
4578d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4579d763cef2SBarry Smith 
4580d763cef2SBarry Smith    Output Parameters:
4581e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4582e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4583e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4584e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4585d763cef2SBarry Smith 
458695452b02SPatrick Sanan    Notes:
458795452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4588d763cef2SBarry Smith 
4589d763cef2SBarry Smith    Level: intermediate
4590d763cef2SBarry Smith 
459180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4592d763cef2SBarry Smith 
4593d763cef2SBarry Smith @*/
4594e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4595d763cef2SBarry Smith {
4596089b2837SJed Brown   PetscErrorCode ierr;
459724989b8cSPeter Brune   DM             dm;
4598089b2837SJed Brown 
4599d763cef2SBarry Smith   PetscFunctionBegin;
460023a57915SBarry Smith   if (Amat || Pmat) {
460123a57915SBarry Smith     SNES snes;
4602089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
460323a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4604e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
460523a57915SBarry Smith   }
460624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
460724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4608d763cef2SBarry Smith   PetscFunctionReturn(0);
4609d763cef2SBarry Smith }
4610d763cef2SBarry Smith 
46112eca1d9cSJed Brown /*@C
46122eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
46132eca1d9cSJed Brown 
46142eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46152eca1d9cSJed Brown 
46162eca1d9cSJed Brown    Input Parameter:
46172eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46182eca1d9cSJed Brown 
46192eca1d9cSJed Brown    Output Parameters:
4620e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4621e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46222eca1d9cSJed Brown .  f   - The function to compute the matrices
46232eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46242eca1d9cSJed Brown 
462595452b02SPatrick Sanan    Notes:
462695452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
46272eca1d9cSJed Brown 
46282eca1d9cSJed Brown    Level: advanced
46292eca1d9cSJed Brown 
463080275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
46312eca1d9cSJed Brown 
46322eca1d9cSJed Brown @*/
4633e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46342eca1d9cSJed Brown {
4635089b2837SJed Brown   PetscErrorCode ierr;
463624989b8cSPeter Brune   DM             dm;
46370910c330SBarry Smith 
46382eca1d9cSJed Brown   PetscFunctionBegin;
4639c0aab802Sstefano_zampini   if (Amat || Pmat) {
4640c0aab802Sstefano_zampini     SNES snes;
4641089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4642f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4643e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4644c0aab802Sstefano_zampini   }
464524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
464624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
46472eca1d9cSJed Brown   PetscFunctionReturn(0);
46482eca1d9cSJed Brown }
46492eca1d9cSJed Brown 
46501713a123SBarry Smith /*@C
465183a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
46521713a123SBarry Smith    VecView() for the solution at each timestep
46531713a123SBarry Smith 
46541713a123SBarry Smith    Collective on TS
46551713a123SBarry Smith 
46561713a123SBarry Smith    Input Parameters:
46571713a123SBarry Smith +  ts - the TS context
46581713a123SBarry Smith .  step - current time-step
4659142b95e3SSatish Balay .  ptime - current time
46600298fd71SBarry Smith -  dummy - either a viewer or NULL
46611713a123SBarry Smith 
466299fdda47SBarry Smith    Options Database:
466399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
466499fdda47SBarry Smith 
466595452b02SPatrick Sanan    Notes:
466695452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
466799fdda47SBarry Smith        will look bad
466899fdda47SBarry Smith 
46691713a123SBarry Smith    Level: intermediate
46701713a123SBarry Smith 
4671a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
46721713a123SBarry Smith @*/
46730910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46741713a123SBarry Smith {
4675dfbe8321SBarry Smith   PetscErrorCode   ierr;
467683a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4677473a3ab2SBarry Smith   PetscDraw        draw;
46781713a123SBarry Smith 
46791713a123SBarry Smith   PetscFunctionBegin;
46806083293cSBarry Smith   if (!step && ictx->showinitial) {
46816083293cSBarry Smith     if (!ictx->initialsolution) {
46820910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
46831713a123SBarry Smith     }
46840910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
46856083293cSBarry Smith   }
4686b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46870dcf80beSBarry Smith 
46886083293cSBarry Smith   if (ictx->showinitial) {
46896083293cSBarry Smith     PetscReal pause;
46906083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
46916083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
46926083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
46936083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
46946083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
46956083293cSBarry Smith   }
46960910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4697473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
469851fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4699473a3ab2SBarry Smith     char      time[32];
4700473a3ab2SBarry Smith 
4701473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
470285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4703473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4704473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
470551fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4706473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4707473a3ab2SBarry Smith   }
4708473a3ab2SBarry Smith 
47096083293cSBarry Smith   if (ictx->showinitial) {
47106083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
47116083293cSBarry Smith   }
47121713a123SBarry Smith   PetscFunctionReturn(0);
47131713a123SBarry Smith }
47141713a123SBarry Smith 
47159110b2e7SHong Zhang /*@C
47162d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
47172d5ee99bSBarry Smith 
47182d5ee99bSBarry Smith    Collective on TS
47192d5ee99bSBarry Smith 
47202d5ee99bSBarry Smith    Input Parameters:
47212d5ee99bSBarry Smith +  ts - the TS context
47222d5ee99bSBarry Smith .  step - current time-step
47232d5ee99bSBarry Smith .  ptime - current time
47242d5ee99bSBarry Smith -  dummy - either a viewer or NULL
47252d5ee99bSBarry Smith 
47262d5ee99bSBarry Smith    Level: intermediate
47272d5ee99bSBarry Smith 
47282d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47292d5ee99bSBarry Smith @*/
47302d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47312d5ee99bSBarry Smith {
47322d5ee99bSBarry Smith   PetscErrorCode    ierr;
47332d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
47342d5ee99bSBarry Smith   PetscDraw         draw;
47356934998bSLisandro Dalcin   PetscDrawAxis     axis;
47362d5ee99bSBarry Smith   PetscInt          n;
47372d5ee99bSBarry Smith   PetscMPIInt       size;
47386934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
47392d5ee99bSBarry Smith   char              time[32];
47402d5ee99bSBarry Smith   const PetscScalar *U;
47412d5ee99bSBarry Smith 
47422d5ee99bSBarry Smith   PetscFunctionBegin;
47436934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
47446934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
47452d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
47466934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
47472d5ee99bSBarry Smith 
47482d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
47496934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
47506934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
47516934998bSLisandro Dalcin   if (!step) {
47526934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
47536934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
47546934998bSLisandro Dalcin   }
47552d5ee99bSBarry Smith 
47562d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
47576934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
47586934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4759a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
47606934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
47612d5ee99bSBarry Smith 
47626934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
47636934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
47642d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
47654b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
476685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
47672d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
476851fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
47692d5ee99bSBarry Smith   }
47706934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
47712d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
477256d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
47736934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
47742d5ee99bSBarry Smith   PetscFunctionReturn(0);
47752d5ee99bSBarry Smith }
47762d5ee99bSBarry Smith 
47776083293cSBarry Smith /*@C
477883a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
47796083293cSBarry Smith 
47806083293cSBarry Smith    Collective on TS
47816083293cSBarry Smith 
47826083293cSBarry Smith    Input Parameters:
47836083293cSBarry Smith .    ctx - the monitor context
47846083293cSBarry Smith 
47856083293cSBarry Smith    Level: intermediate
47866083293cSBarry Smith 
478783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
47886083293cSBarry Smith @*/
478983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
47906083293cSBarry Smith {
47916083293cSBarry Smith   PetscErrorCode ierr;
47926083293cSBarry Smith 
47936083293cSBarry Smith   PetscFunctionBegin;
479483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
479583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
479683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
47976083293cSBarry Smith   PetscFunctionReturn(0);
47986083293cSBarry Smith }
47996083293cSBarry Smith 
48006083293cSBarry Smith /*@C
480183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
48026083293cSBarry Smith 
48036083293cSBarry Smith    Collective on TS
48046083293cSBarry Smith 
48056083293cSBarry Smith    Input Parameter:
48066083293cSBarry Smith .    ts - time-step context
48076083293cSBarry Smith 
48086083293cSBarry Smith    Output Patameter:
48096083293cSBarry Smith .    ctx - the monitor context
48106083293cSBarry Smith 
481199fdda47SBarry Smith    Options Database:
481299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
481399fdda47SBarry Smith 
48146083293cSBarry Smith    Level: intermediate
48156083293cSBarry Smith 
481683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
48176083293cSBarry Smith @*/
481883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
48196083293cSBarry Smith {
48206083293cSBarry Smith   PetscErrorCode   ierr;
48216083293cSBarry Smith 
48226083293cSBarry Smith   PetscFunctionBegin;
4823b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
482483a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4825e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4826bbd56ea5SKarl Rupp 
482783a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4828473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4829c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4830473a3ab2SBarry Smith 
4831473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4832c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
48336083293cSBarry Smith   PetscFunctionReturn(0);
48346083293cSBarry Smith }
48356083293cSBarry Smith 
48363a471f94SBarry Smith /*@C
48370ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
48380ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
48390ed3bfb6SBarry Smith 
48400ed3bfb6SBarry Smith    Collective on TS
48410ed3bfb6SBarry Smith 
48420ed3bfb6SBarry Smith    Input Parameters:
48430ed3bfb6SBarry Smith +  ts - the TS context
48440ed3bfb6SBarry Smith .  step - current time-step
48450ed3bfb6SBarry Smith .  ptime - current time
48460ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
48470ed3bfb6SBarry Smith 
48480ed3bfb6SBarry Smith    Options Database:
48490ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48500ed3bfb6SBarry Smith 
48510ed3bfb6SBarry Smith    Level: intermediate
48520ed3bfb6SBarry Smith 
48530ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48540ed3bfb6SBarry Smith @*/
48550ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48560ed3bfb6SBarry Smith {
48570ed3bfb6SBarry Smith   PetscErrorCode   ierr;
48580ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
48590ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
48600ed3bfb6SBarry Smith   Vec              work;
48610ed3bfb6SBarry Smith 
48620ed3bfb6SBarry Smith   PetscFunctionBegin;
48630ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48640ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
48650ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
48660ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
48670ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
48680ed3bfb6SBarry Smith   PetscFunctionReturn(0);
48690ed3bfb6SBarry Smith }
48700ed3bfb6SBarry Smith 
48710ed3bfb6SBarry Smith /*@C
487283a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
48733a471f94SBarry Smith    VecView() for the error at each timestep
48743a471f94SBarry Smith 
48753a471f94SBarry Smith    Collective on TS
48763a471f94SBarry Smith 
48773a471f94SBarry Smith    Input Parameters:
48783a471f94SBarry Smith +  ts - the TS context
48793a471f94SBarry Smith .  step - current time-step
48803a471f94SBarry Smith .  ptime - current time
48810298fd71SBarry Smith -  dummy - either a viewer or NULL
48823a471f94SBarry Smith 
48830ed3bfb6SBarry Smith    Options Database:
48840ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
48850ed3bfb6SBarry Smith 
48863a471f94SBarry Smith    Level: intermediate
48873a471f94SBarry Smith 
48880ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
48893a471f94SBarry Smith @*/
48900910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48913a471f94SBarry Smith {
48923a471f94SBarry Smith   PetscErrorCode   ierr;
489383a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
489483a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
48953a471f94SBarry Smith   Vec              work;
48963a471f94SBarry Smith 
48973a471f94SBarry Smith   PetscFunctionBegin;
4898b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48990910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
49003a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
49010910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
49023a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
49033a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
49043a471f94SBarry Smith   PetscFunctionReturn(0);
49053a471f94SBarry Smith }
49063a471f94SBarry Smith 
4907af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49086c699258SBarry Smith /*@
49092a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49106c699258SBarry Smith 
4911d083f849SBarry Smith    Logically Collective on ts
49126c699258SBarry Smith 
49136c699258SBarry Smith    Input Parameters:
49142a808120SBarry Smith +  ts - the ODE integrator object
49152a808120SBarry Smith -  dm - the dm, cannot be NULL
49166c699258SBarry Smith 
4917e03a659cSJed Brown    Notes:
4918e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4919e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4920e03a659cSJed Brown    different problems using the same function space.
4921e03a659cSJed Brown 
49226c699258SBarry Smith    Level: intermediate
49236c699258SBarry Smith 
49246c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49256c699258SBarry Smith @*/
49267087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49276c699258SBarry Smith {
49286c699258SBarry Smith   PetscErrorCode ierr;
4929089b2837SJed Brown   SNES           snes;
4930942e3340SBarry Smith   DMTS           tsdm;
49316c699258SBarry Smith 
49326c699258SBarry Smith   PetscFunctionBegin;
49330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49342a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
493570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4936942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
49372a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4938942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4939942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
494024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
494124989b8cSPeter Brune         tsdm->originaldm = dm;
494224989b8cSPeter Brune       }
494324989b8cSPeter Brune     }
49446bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
494524989b8cSPeter Brune   }
49466c699258SBarry Smith   ts->dm = dm;
4947bbd56ea5SKarl Rupp 
4948089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4949089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
49506c699258SBarry Smith   PetscFunctionReturn(0);
49516c699258SBarry Smith }
49526c699258SBarry Smith 
49536c699258SBarry Smith /*@
49546c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
49556c699258SBarry Smith 
49563f9fe445SBarry Smith    Not Collective
49576c699258SBarry Smith 
49586c699258SBarry Smith    Input Parameter:
49596c699258SBarry Smith . ts - the preconditioner context
49606c699258SBarry Smith 
49616c699258SBarry Smith    Output Parameter:
49626c699258SBarry Smith .  dm - the dm
49636c699258SBarry Smith 
49646c699258SBarry Smith    Level: intermediate
49656c699258SBarry Smith 
49666c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
49676c699258SBarry Smith @*/
49687087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
49696c699258SBarry Smith {
4970496e6a7aSJed Brown   PetscErrorCode ierr;
4971496e6a7aSJed Brown 
49726c699258SBarry Smith   PetscFunctionBegin;
49730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4974496e6a7aSJed Brown   if (!ts->dm) {
4975ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4976496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4977496e6a7aSJed Brown   }
49786c699258SBarry Smith   *dm = ts->dm;
49796c699258SBarry Smith   PetscFunctionReturn(0);
49806c699258SBarry Smith }
49811713a123SBarry Smith 
49820f5c6efeSJed Brown /*@
49830f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
49840f5c6efeSJed Brown 
49853f9fe445SBarry Smith    Logically Collective on SNES
49860f5c6efeSJed Brown 
49870f5c6efeSJed Brown    Input Parameter:
4988d42a1c89SJed Brown + snes - nonlinear solver
49890910c330SBarry Smith . U - the current state at which to evaluate the residual
4990d42a1c89SJed Brown - ctx - user context, must be a TS
49910f5c6efeSJed Brown 
49920f5c6efeSJed Brown    Output Parameter:
49930f5c6efeSJed Brown . F - the nonlinear residual
49940f5c6efeSJed Brown 
49950f5c6efeSJed Brown    Notes:
49960f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
49970f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
49980f5c6efeSJed Brown 
49990f5c6efeSJed Brown    Level: advanced
50000f5c6efeSJed Brown 
50010f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
50020f5c6efeSJed Brown @*/
50030910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
50040f5c6efeSJed Brown {
50050f5c6efeSJed Brown   TS             ts = (TS)ctx;
50060f5c6efeSJed Brown   PetscErrorCode ierr;
50070f5c6efeSJed Brown 
50080f5c6efeSJed Brown   PetscFunctionBegin;
50090f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50100910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50110f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50120f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50130910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50140f5c6efeSJed Brown   PetscFunctionReturn(0);
50150f5c6efeSJed Brown }
50160f5c6efeSJed Brown 
50170f5c6efeSJed Brown /*@
50180f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50190f5c6efeSJed Brown 
50200f5c6efeSJed Brown    Collective on SNES
50210f5c6efeSJed Brown 
50220f5c6efeSJed Brown    Input Parameter:
50230f5c6efeSJed Brown + snes - nonlinear solver
50240910c330SBarry Smith . U - the current state at which to evaluate the residual
50250f5c6efeSJed Brown - ctx - user context, must be a TS
50260f5c6efeSJed Brown 
50270f5c6efeSJed Brown    Output Parameter:
50280f5c6efeSJed Brown + A - the Jacobian
50290f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
50300f5c6efeSJed Brown - flag - indicates any structure change in the matrix
50310f5c6efeSJed Brown 
50320f5c6efeSJed Brown    Notes:
50330f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50340f5c6efeSJed Brown 
50350f5c6efeSJed Brown    Level: developer
50360f5c6efeSJed Brown 
50370f5c6efeSJed Brown .seealso: SNESSetJacobian()
50380f5c6efeSJed Brown @*/
5039d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
50400f5c6efeSJed Brown {
50410f5c6efeSJed Brown   TS             ts = (TS)ctx;
50420f5c6efeSJed Brown   PetscErrorCode ierr;
50430f5c6efeSJed Brown 
50440f5c6efeSJed Brown   PetscFunctionBegin;
50450f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50470f5c6efeSJed Brown   PetscValidPointer(A,3);
504894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
50490f5c6efeSJed Brown   PetscValidPointer(B,4);
505094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
50510f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5052d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
50530f5c6efeSJed Brown   PetscFunctionReturn(0);
50540f5c6efeSJed Brown }
5055325fc9f4SBarry Smith 
50560e4ef248SJed Brown /*@C
50579ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
50580e4ef248SJed Brown 
50590e4ef248SJed Brown    Collective on TS
50600e4ef248SJed Brown 
50610e4ef248SJed Brown    Input Arguments:
50620e4ef248SJed Brown +  ts - time stepping context
50630e4ef248SJed Brown .  t - time at which to evaluate
50640910c330SBarry Smith .  U - state at which to evaluate
50650e4ef248SJed Brown -  ctx - context
50660e4ef248SJed Brown 
50670e4ef248SJed Brown    Output Arguments:
50680e4ef248SJed Brown .  F - right hand side
50690e4ef248SJed Brown 
50700e4ef248SJed Brown    Level: intermediate
50710e4ef248SJed Brown 
50720e4ef248SJed Brown    Notes:
50730e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
50740e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
50750e4ef248SJed Brown 
50760e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
50770e4ef248SJed Brown @*/
50780910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
50790e4ef248SJed Brown {
50800e4ef248SJed Brown   PetscErrorCode ierr;
50810e4ef248SJed Brown   Mat            Arhs,Brhs;
50820e4ef248SJed Brown 
50830e4ef248SJed Brown   PetscFunctionBegin;
50840e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
50852663174eSHong Zhang   /* undo the damage caused by shifting */
5086cfa8a9a2SHong Zhang   ierr = TSRecoverRHSJacobian(ts,Arhs,Brhs);CHKERRQ(ierr);
5087d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
50880910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
50890e4ef248SJed Brown   PetscFunctionReturn(0);
50900e4ef248SJed Brown }
50910e4ef248SJed Brown 
50920e4ef248SJed Brown /*@C
50930e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
50940e4ef248SJed Brown 
50950e4ef248SJed Brown    Collective on TS
50960e4ef248SJed Brown 
50970e4ef248SJed Brown    Input Arguments:
50980e4ef248SJed Brown +  ts - time stepping context
50990e4ef248SJed Brown .  t - time at which to evaluate
51000910c330SBarry Smith .  U - state at which to evaluate
51010e4ef248SJed Brown -  ctx - context
51020e4ef248SJed Brown 
51030e4ef248SJed Brown    Output Arguments:
51040e4ef248SJed Brown +  A - pointer to operator
51050e4ef248SJed Brown .  B - pointer to preconditioning matrix
51060e4ef248SJed Brown -  flg - matrix structure flag
51070e4ef248SJed Brown 
51080e4ef248SJed Brown    Level: intermediate
51090e4ef248SJed Brown 
51100e4ef248SJed Brown    Notes:
51110e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51120e4ef248SJed Brown 
51130e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51140e4ef248SJed Brown @*/
5115d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51160e4ef248SJed Brown {
51170e4ef248SJed Brown   PetscFunctionBegin;
51180e4ef248SJed Brown   PetscFunctionReturn(0);
51190e4ef248SJed Brown }
51200e4ef248SJed Brown 
51210026cea9SSean Farley /*@C
51220026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51230026cea9SSean Farley 
51240026cea9SSean Farley    Collective on TS
51250026cea9SSean Farley 
51260026cea9SSean Farley    Input Arguments:
51270026cea9SSean Farley +  ts - time stepping context
51280026cea9SSean Farley .  t - time at which to evaluate
51290910c330SBarry Smith .  U - state at which to evaluate
51300910c330SBarry Smith .  Udot - time derivative of state vector
51310026cea9SSean Farley -  ctx - context
51320026cea9SSean Farley 
51330026cea9SSean Farley    Output Arguments:
51340026cea9SSean Farley .  F - left hand side
51350026cea9SSean Farley 
51360026cea9SSean Farley    Level: intermediate
51370026cea9SSean Farley 
51380026cea9SSean Farley    Notes:
51390910c330SBarry 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
51400026cea9SSean Farley    user is required to write their own TSComputeIFunction.
51410026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
51420026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
51430026cea9SSean Farley 
51449ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
51459ae8fd06SBarry Smith 
51469ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
51470026cea9SSean Farley @*/
51480910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
51490026cea9SSean Farley {
51500026cea9SSean Farley   PetscErrorCode ierr;
51510026cea9SSean Farley   Mat            A,B;
51520026cea9SSean Farley 
51530026cea9SSean Farley   PetscFunctionBegin;
51540298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5155d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
51560910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
51570026cea9SSean Farley   PetscFunctionReturn(0);
51580026cea9SSean Farley }
51590026cea9SSean Farley 
51600026cea9SSean Farley /*@C
5161b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
51620026cea9SSean Farley 
51630026cea9SSean Farley    Collective on TS
51640026cea9SSean Farley 
51650026cea9SSean Farley    Input Arguments:
51660026cea9SSean Farley +  ts - time stepping context
51670026cea9SSean Farley .  t - time at which to evaluate
51680910c330SBarry Smith .  U - state at which to evaluate
51690910c330SBarry Smith .  Udot - time derivative of state vector
51700026cea9SSean Farley .  shift - shift to apply
51710026cea9SSean Farley -  ctx - context
51720026cea9SSean Farley 
51730026cea9SSean Farley    Output Arguments:
51740026cea9SSean Farley +  A - pointer to operator
51750026cea9SSean Farley .  B - pointer to preconditioning matrix
51760026cea9SSean Farley -  flg - matrix structure flag
51770026cea9SSean Farley 
5178b41af12eSJed Brown    Level: advanced
51790026cea9SSean Farley 
51800026cea9SSean Farley    Notes:
51810026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
51820026cea9SSean Farley 
5183b41af12eSJed Brown    It is only appropriate for problems of the form
5184b41af12eSJed Brown 
5185b41af12eSJed Brown $     M Udot = F(U,t)
5186b41af12eSJed Brown 
5187b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5188b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5189b41af12eSJed Brown   an implicit operator of the form
5190b41af12eSJed Brown 
5191b41af12eSJed Brown $    shift*M + J
5192b41af12eSJed Brown 
5193b41af12eSJed 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
5194b41af12eSJed Brown   a copy of M or reassemble it when requested.
5195b41af12eSJed Brown 
51960026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
51970026cea9SSean Farley @*/
5198d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
51990026cea9SSean Farley {
5200b41af12eSJed Brown   PetscErrorCode ierr;
5201b41af12eSJed Brown 
52020026cea9SSean Farley   PetscFunctionBegin;
520394ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5204b41af12eSJed Brown   ts->ijacobian.shift = shift;
52050026cea9SSean Farley   PetscFunctionReturn(0);
52060026cea9SSean Farley }
5207b41af12eSJed Brown 
5208e817cc15SEmil Constantinescu /*@
5209e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5210e817cc15SEmil Constantinescu 
5211e817cc15SEmil Constantinescu    Not Collective
5212e817cc15SEmil Constantinescu 
5213e817cc15SEmil Constantinescu    Input Parameter:
5214e817cc15SEmil Constantinescu .  ts - the TS context
5215e817cc15SEmil Constantinescu 
5216e817cc15SEmil Constantinescu    Output Parameter:
52174e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5218e817cc15SEmil Constantinescu 
5219e817cc15SEmil Constantinescu    Level: beginner
5220e817cc15SEmil Constantinescu 
5221e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5222e817cc15SEmil Constantinescu @*/
5223e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5224e817cc15SEmil Constantinescu {
5225e817cc15SEmil Constantinescu   PetscFunctionBegin;
5226e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5227e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5228e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5229e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5230e817cc15SEmil Constantinescu }
5231e817cc15SEmil Constantinescu 
5232e817cc15SEmil Constantinescu /*@
5233e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5234e817cc15SEmil Constantinescu 
5235e817cc15SEmil Constantinescu    Not Collective
5236e817cc15SEmil Constantinescu 
5237e817cc15SEmil Constantinescu    Input Parameter:
5238e817cc15SEmil Constantinescu +  ts - the TS context
52391297b224SEmil Constantinescu -  equation_type - see TSEquationType
5240e817cc15SEmil Constantinescu 
5241e817cc15SEmil Constantinescu    Level: advanced
5242e817cc15SEmil Constantinescu 
5243e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5244e817cc15SEmil Constantinescu @*/
5245e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5246e817cc15SEmil Constantinescu {
5247e817cc15SEmil Constantinescu   PetscFunctionBegin;
5248e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5249e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5250e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5251e817cc15SEmil Constantinescu }
52520026cea9SSean Farley 
52534af1b03aSJed Brown /*@
52544af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
52554af1b03aSJed Brown 
52564af1b03aSJed Brown    Not Collective
52574af1b03aSJed Brown 
52584af1b03aSJed Brown    Input Parameter:
52594af1b03aSJed Brown .  ts - the TS context
52604af1b03aSJed Brown 
52614af1b03aSJed Brown    Output Parameter:
52624af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
52634af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
52644af1b03aSJed Brown 
5265487e0bb9SJed Brown    Level: beginner
52664af1b03aSJed Brown 
5267cd652676SJed Brown    Notes:
5268cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
52694af1b03aSJed Brown 
52704af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
52714af1b03aSJed Brown @*/
52724af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
52734af1b03aSJed Brown {
52744af1b03aSJed Brown   PetscFunctionBegin;
52754af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52764af1b03aSJed Brown   PetscValidPointer(reason,2);
52774af1b03aSJed Brown   *reason = ts->reason;
52784af1b03aSJed Brown   PetscFunctionReturn(0);
52794af1b03aSJed Brown }
52804af1b03aSJed Brown 
5281d6ad946cSShri Abhyankar /*@
5282d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5283d6ad946cSShri Abhyankar 
52846b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
5285d6ad946cSShri Abhyankar 
52866b221cbeSPatrick Sanan    Input Parameters:
5287d6ad946cSShri Abhyankar +  ts - the TS context
52886b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5289d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5290d6ad946cSShri Abhyankar 
5291f5abba47SShri Abhyankar    Level: advanced
5292d6ad946cSShri Abhyankar 
5293d6ad946cSShri Abhyankar    Notes:
52946b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
5295d6ad946cSShri Abhyankar 
5296d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5297d6ad946cSShri Abhyankar @*/
5298d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5299d6ad946cSShri Abhyankar {
5300d6ad946cSShri Abhyankar   PetscFunctionBegin;
5301d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5302d6ad946cSShri Abhyankar   ts->reason = reason;
5303d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5304d6ad946cSShri Abhyankar }
5305d6ad946cSShri Abhyankar 
5306cc708dedSBarry Smith /*@
5307cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5308cc708dedSBarry Smith 
5309cc708dedSBarry Smith    Not Collective
5310cc708dedSBarry Smith 
5311cc708dedSBarry Smith    Input Parameter:
5312cc708dedSBarry Smith .  ts - the TS context
5313cc708dedSBarry Smith 
5314cc708dedSBarry Smith    Output Parameter:
531519eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5316cc708dedSBarry Smith 
5317487e0bb9SJed Brown    Level: beginner
5318cc708dedSBarry Smith 
5319cc708dedSBarry Smith    Notes:
5320cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5321cc708dedSBarry Smith 
5322cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5323cc708dedSBarry Smith @*/
5324cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5325cc708dedSBarry Smith {
5326cc708dedSBarry Smith   PetscFunctionBegin;
5327cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5328cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5329cc708dedSBarry Smith   *ftime = ts->solvetime;
5330cc708dedSBarry Smith   PetscFunctionReturn(0);
5331cc708dedSBarry Smith }
5332cc708dedSBarry Smith 
53332c18e0fdSBarry Smith /*@
53345ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
53359f67acb7SJed Brown    used by the time integrator.
53369f67acb7SJed Brown 
53379f67acb7SJed Brown    Not Collective
53389f67acb7SJed Brown 
53399f67acb7SJed Brown    Input Parameter:
53409f67acb7SJed Brown .  ts - TS context
53419f67acb7SJed Brown 
53429f67acb7SJed Brown    Output Parameter:
53439f67acb7SJed Brown .  nits - number of nonlinear iterations
53449f67acb7SJed Brown 
53459f67acb7SJed Brown    Notes:
53469f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53479f67acb7SJed Brown 
53489f67acb7SJed Brown    Level: intermediate
53499f67acb7SJed Brown 
53505ef26d82SJed Brown .seealso:  TSGetKSPIterations()
53519f67acb7SJed Brown @*/
53525ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
53539f67acb7SJed Brown {
53549f67acb7SJed Brown   PetscFunctionBegin;
53559f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53569f67acb7SJed Brown   PetscValidIntPointer(nits,2);
53575ef26d82SJed Brown   *nits = ts->snes_its;
53589f67acb7SJed Brown   PetscFunctionReturn(0);
53599f67acb7SJed Brown }
53609f67acb7SJed Brown 
53619f67acb7SJed Brown /*@
53625ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
53639f67acb7SJed Brown    used by the time integrator.
53649f67acb7SJed Brown 
53659f67acb7SJed Brown    Not Collective
53669f67acb7SJed Brown 
53679f67acb7SJed Brown    Input Parameter:
53689f67acb7SJed Brown .  ts - TS context
53699f67acb7SJed Brown 
53709f67acb7SJed Brown    Output Parameter:
53719f67acb7SJed Brown .  lits - number of linear iterations
53729f67acb7SJed Brown 
53739f67acb7SJed Brown    Notes:
53749f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
53759f67acb7SJed Brown 
53769f67acb7SJed Brown    Level: intermediate
53779f67acb7SJed Brown 
53785ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
53799f67acb7SJed Brown @*/
53805ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
53819f67acb7SJed Brown {
53829f67acb7SJed Brown   PetscFunctionBegin;
53839f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53849f67acb7SJed Brown   PetscValidIntPointer(lits,2);
53855ef26d82SJed Brown   *lits = ts->ksp_its;
53869f67acb7SJed Brown   PetscFunctionReturn(0);
53879f67acb7SJed Brown }
53889f67acb7SJed Brown 
5389cef5090cSJed Brown /*@
5390cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5391cef5090cSJed Brown 
5392cef5090cSJed Brown    Not Collective
5393cef5090cSJed Brown 
5394cef5090cSJed Brown    Input Parameter:
5395cef5090cSJed Brown .  ts - TS context
5396cef5090cSJed Brown 
5397cef5090cSJed Brown    Output Parameter:
5398cef5090cSJed Brown .  rejects - number of steps rejected
5399cef5090cSJed Brown 
5400cef5090cSJed Brown    Notes:
5401cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5402cef5090cSJed Brown 
5403cef5090cSJed Brown    Level: intermediate
5404cef5090cSJed Brown 
54055ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5406cef5090cSJed Brown @*/
5407cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5408cef5090cSJed Brown {
5409cef5090cSJed Brown   PetscFunctionBegin;
5410cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5411cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5412cef5090cSJed Brown   *rejects = ts->reject;
5413cef5090cSJed Brown   PetscFunctionReturn(0);
5414cef5090cSJed Brown }
5415cef5090cSJed Brown 
5416cef5090cSJed Brown /*@
5417cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5418cef5090cSJed Brown 
5419cef5090cSJed Brown    Not Collective
5420cef5090cSJed Brown 
5421cef5090cSJed Brown    Input Parameter:
5422cef5090cSJed Brown .  ts - TS context
5423cef5090cSJed Brown 
5424cef5090cSJed Brown    Output Parameter:
5425cef5090cSJed Brown .  fails - number of failed nonlinear solves
5426cef5090cSJed Brown 
5427cef5090cSJed Brown    Notes:
5428cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5429cef5090cSJed Brown 
5430cef5090cSJed Brown    Level: intermediate
5431cef5090cSJed Brown 
54325ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5433cef5090cSJed Brown @*/
5434cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5435cef5090cSJed Brown {
5436cef5090cSJed Brown   PetscFunctionBegin;
5437cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5438cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5439cef5090cSJed Brown   *fails = ts->num_snes_failures;
5440cef5090cSJed Brown   PetscFunctionReturn(0);
5441cef5090cSJed Brown }
5442cef5090cSJed Brown 
5443cef5090cSJed Brown /*@
5444cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5445cef5090cSJed Brown 
5446cef5090cSJed Brown    Not Collective
5447cef5090cSJed Brown 
5448cef5090cSJed Brown    Input Parameter:
5449cef5090cSJed Brown +  ts - TS context
5450cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5451cef5090cSJed Brown 
5452cef5090cSJed Brown    Notes:
5453cef5090cSJed Brown    The counter is reset to zero for each step
5454cef5090cSJed Brown 
5455cef5090cSJed Brown    Options Database Key:
5456cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5457cef5090cSJed Brown 
5458cef5090cSJed Brown    Level: intermediate
5459cef5090cSJed Brown 
54605ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5461cef5090cSJed Brown @*/
5462cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5463cef5090cSJed Brown {
5464cef5090cSJed Brown   PetscFunctionBegin;
5465cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5466cef5090cSJed Brown   ts->max_reject = rejects;
5467cef5090cSJed Brown   PetscFunctionReturn(0);
5468cef5090cSJed Brown }
5469cef5090cSJed Brown 
5470cef5090cSJed Brown /*@
5471cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5472cef5090cSJed Brown 
5473cef5090cSJed Brown    Not Collective
5474cef5090cSJed Brown 
5475cef5090cSJed Brown    Input Parameter:
5476cef5090cSJed Brown +  ts - TS context
5477cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5478cef5090cSJed Brown 
5479cef5090cSJed Brown    Notes:
5480cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5481cef5090cSJed Brown 
5482cef5090cSJed Brown    Options Database Key:
5483cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5484cef5090cSJed Brown 
5485cef5090cSJed Brown    Level: intermediate
5486cef5090cSJed Brown 
54875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5488cef5090cSJed Brown @*/
5489cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5490cef5090cSJed Brown {
5491cef5090cSJed Brown   PetscFunctionBegin;
5492cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5493cef5090cSJed Brown   ts->max_snes_failures = fails;
5494cef5090cSJed Brown   PetscFunctionReturn(0);
5495cef5090cSJed Brown }
5496cef5090cSJed Brown 
5497cef5090cSJed Brown /*@
5498cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5499cef5090cSJed Brown 
5500cef5090cSJed Brown    Not Collective
5501cef5090cSJed Brown 
5502cef5090cSJed Brown    Input Parameter:
5503cef5090cSJed Brown +  ts - TS context
5504cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5505cef5090cSJed Brown 
5506cef5090cSJed Brown    Options Database Key:
5507cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5508cef5090cSJed Brown 
5509cef5090cSJed Brown    Level: intermediate
5510cef5090cSJed Brown 
55115ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5512cef5090cSJed Brown @*/
5513cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5514cef5090cSJed Brown {
5515cef5090cSJed Brown   PetscFunctionBegin;
5516cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5517cef5090cSJed Brown   ts->errorifstepfailed = err;
5518cef5090cSJed Brown   PetscFunctionReturn(0);
5519cef5090cSJed Brown }
5520cef5090cSJed Brown 
5521fb1732b5SBarry Smith /*@C
5522fde5950dSBarry 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
5523fb1732b5SBarry Smith 
5524fb1732b5SBarry Smith    Collective on TS
5525fb1732b5SBarry Smith 
5526fb1732b5SBarry Smith    Input Parameters:
5527fb1732b5SBarry Smith +  ts - the TS context
5528fb1732b5SBarry Smith .  step - current time-step
5529fb1732b5SBarry Smith .  ptime - current time
55300910c330SBarry Smith .  u - current state
5531721cd6eeSBarry Smith -  vf - viewer and its format
5532fb1732b5SBarry Smith 
5533fb1732b5SBarry Smith    Level: intermediate
5534fb1732b5SBarry Smith 
5535fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5536fb1732b5SBarry Smith @*/
5537721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5538fb1732b5SBarry Smith {
5539fb1732b5SBarry Smith   PetscErrorCode ierr;
5540fb1732b5SBarry Smith 
5541fb1732b5SBarry Smith   PetscFunctionBegin;
5542721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5543721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5544721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5545ed81e22dSJed Brown   PetscFunctionReturn(0);
5546ed81e22dSJed Brown }
5547ed81e22dSJed Brown 
5548ed81e22dSJed Brown /*@C
5549ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5550ed81e22dSJed Brown 
5551ed81e22dSJed Brown    Collective on TS
5552ed81e22dSJed Brown 
5553ed81e22dSJed Brown    Input Parameters:
5554ed81e22dSJed Brown +  ts - the TS context
5555ed81e22dSJed Brown .  step - current time-step
5556ed81e22dSJed Brown .  ptime - current time
55570910c330SBarry Smith .  u - current state
5558ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5559ed81e22dSJed Brown 
5560ed81e22dSJed Brown    Level: intermediate
5561ed81e22dSJed Brown 
5562ed81e22dSJed Brown    Notes:
5563ed81e22dSJed 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.
5564ed81e22dSJed Brown    These are named according to the file name template.
5565ed81e22dSJed Brown 
5566ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5567ed81e22dSJed Brown 
5568ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5569ed81e22dSJed Brown @*/
55700910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5571ed81e22dSJed Brown {
5572ed81e22dSJed Brown   PetscErrorCode ierr;
5573ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5574ed81e22dSJed Brown   PetscViewer    viewer;
5575ed81e22dSJed Brown 
5576ed81e22dSJed Brown   PetscFunctionBegin;
557763e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
55788caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5579ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
55800910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5581ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5582ed81e22dSJed Brown   PetscFunctionReturn(0);
5583ed81e22dSJed Brown }
5584ed81e22dSJed Brown 
5585ed81e22dSJed Brown /*@C
5586ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5587ed81e22dSJed Brown 
5588ed81e22dSJed Brown    Collective on TS
5589ed81e22dSJed Brown 
5590ed81e22dSJed Brown    Input Parameters:
5591ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5592ed81e22dSJed Brown 
5593ed81e22dSJed Brown    Level: intermediate
5594ed81e22dSJed Brown 
5595ed81e22dSJed Brown    Note:
5596ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5597ed81e22dSJed Brown 
5598ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5599ed81e22dSJed Brown @*/
5600ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5601ed81e22dSJed Brown {
5602ed81e22dSJed Brown   PetscErrorCode ierr;
5603ed81e22dSJed Brown 
5604ed81e22dSJed Brown   PetscFunctionBegin;
5605ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5606fb1732b5SBarry Smith   PetscFunctionReturn(0);
5607fb1732b5SBarry Smith }
5608fb1732b5SBarry Smith 
560984df9cb4SJed Brown /*@
5610552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
561184df9cb4SJed Brown 
5612ed81e22dSJed Brown    Collective on TS if controller has not been created yet
561384df9cb4SJed Brown 
561484df9cb4SJed Brown    Input Arguments:
5615ed81e22dSJed Brown .  ts - time stepping context
561684df9cb4SJed Brown 
561784df9cb4SJed Brown    Output Arguments:
5618ed81e22dSJed Brown .  adapt - adaptive controller
561984df9cb4SJed Brown 
562084df9cb4SJed Brown    Level: intermediate
562184df9cb4SJed Brown 
5622ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
562384df9cb4SJed Brown @*/
5624552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
562584df9cb4SJed Brown {
562684df9cb4SJed Brown   PetscErrorCode ierr;
562784df9cb4SJed Brown 
562884df9cb4SJed Brown   PetscFunctionBegin;
562984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5630bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
563184df9cb4SJed Brown   if (!ts->adapt) {
5632ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
56333bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
56341c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
563584df9cb4SJed Brown   }
5636bec58848SLisandro Dalcin   *adapt = ts->adapt;
563784df9cb4SJed Brown   PetscFunctionReturn(0);
563884df9cb4SJed Brown }
5639d6ebe24aSShri Abhyankar 
56401c3436cfSJed Brown /*@
56411c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
56421c3436cfSJed Brown 
56431c3436cfSJed Brown    Logically Collective
56441c3436cfSJed Brown 
56451c3436cfSJed Brown    Input Arguments:
56461c3436cfSJed Brown +  ts - time integration context
56471c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
56480298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
56491c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
56500298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
56511c3436cfSJed Brown 
5652a3cdaa26SBarry Smith    Options Database keys:
5653a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5654a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5655a3cdaa26SBarry Smith 
56563ff766beSShri Abhyankar    Notes:
56573ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
56583ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
56593ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
56603ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
56613ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
56623ff766beSShri Abhyankar    differential variables.
56633ff766beSShri Abhyankar 
56641c3436cfSJed Brown    Level: beginner
56651c3436cfSJed Brown 
56665c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances()
56671c3436cfSJed Brown @*/
56681c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
56691c3436cfSJed Brown {
56701c3436cfSJed Brown   PetscErrorCode ierr;
56711c3436cfSJed Brown 
56721c3436cfSJed Brown   PetscFunctionBegin;
5673c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
56741c3436cfSJed Brown   if (vatol) {
56751c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
56761c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
56771c3436cfSJed Brown     ts->vatol = vatol;
56781c3436cfSJed Brown   }
5679c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
56801c3436cfSJed Brown   if (vrtol) {
56811c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
56821c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
56831c3436cfSJed Brown     ts->vrtol = vrtol;
56841c3436cfSJed Brown   }
56851c3436cfSJed Brown   PetscFunctionReturn(0);
56861c3436cfSJed Brown }
56871c3436cfSJed Brown 
5688c5033834SJed Brown /*@
5689c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5690c5033834SJed Brown 
5691c5033834SJed Brown    Logically Collective
5692c5033834SJed Brown 
5693c5033834SJed Brown    Input Arguments:
5694c5033834SJed Brown .  ts - time integration context
5695c5033834SJed Brown 
5696c5033834SJed Brown    Output Arguments:
56970298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
56980298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
56990298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
57000298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5701c5033834SJed Brown 
5702c5033834SJed Brown    Level: beginner
5703c5033834SJed Brown 
57045c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances()
5705c5033834SJed Brown @*/
5706c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5707c5033834SJed Brown {
5708c5033834SJed Brown   PetscFunctionBegin;
5709c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5710c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5711c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5712c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5713c5033834SJed Brown   PetscFunctionReturn(0);
5714c5033834SJed Brown }
5715c5033834SJed Brown 
57169c6b16b5SShri Abhyankar /*@
5717a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
57189c6b16b5SShri Abhyankar 
57199c6b16b5SShri Abhyankar    Collective on TS
57209c6b16b5SShri Abhyankar 
57219c6b16b5SShri Abhyankar    Input Arguments:
57229c6b16b5SShri Abhyankar +  ts - time stepping context
5723a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5724a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
57259c6b16b5SShri Abhyankar 
57269c6b16b5SShri Abhyankar    Output Arguments:
5727a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57287453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5729a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57309c6b16b5SShri Abhyankar 
57319c6b16b5SShri Abhyankar    Level: developer
57329c6b16b5SShri Abhyankar 
5733deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
57349c6b16b5SShri Abhyankar @*/
57357453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57369c6b16b5SShri Abhyankar {
57379c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
57389c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
57397453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57407453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57419c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
57427453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
57437453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
57447453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57459c6b16b5SShri Abhyankar 
57469c6b16b5SShri Abhyankar   PetscFunctionBegin;
57479c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5748a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5749a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5750a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5751a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5752a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5753a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57548a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57558a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5756a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57579c6b16b5SShri Abhyankar 
57589c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57599c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57609c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57619c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57629c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57637453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
57647453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
57657453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
57669c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
57679c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57689c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57699c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57709c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
577176cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57727453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57737453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57747453f775SEmil Constantinescu       if (tola>0.){
57757453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
57767453f775SEmil Constantinescu         na_loc++;
57777453f775SEmil Constantinescu       }
57787453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57797453f775SEmil Constantinescu       if (tolr>0.){
57807453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
57817453f775SEmil Constantinescu         nr_loc++;
57827453f775SEmil Constantinescu       }
57837453f775SEmil Constantinescu       tol=tola+tolr;
57847453f775SEmil Constantinescu       if (tol>0.){
57857453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
57867453f775SEmil Constantinescu         n_loc++;
57877453f775SEmil Constantinescu       }
57889c6b16b5SShri Abhyankar     }
57899c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57909c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57919c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57929c6b16b5SShri Abhyankar     const PetscScalar *atol;
57939c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57949c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
579576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57967453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57977453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57987453f775SEmil Constantinescu       if (tola>0.){
57997453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58007453f775SEmil Constantinescu         na_loc++;
58017453f775SEmil Constantinescu       }
58027453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58037453f775SEmil Constantinescu       if (tolr>0.){
58047453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58057453f775SEmil Constantinescu         nr_loc++;
58067453f775SEmil Constantinescu       }
58077453f775SEmil Constantinescu       tol=tola+tolr;
58087453f775SEmil Constantinescu       if (tol>0.){
58097453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58107453f775SEmil Constantinescu         n_loc++;
58117453f775SEmil Constantinescu       }
58129c6b16b5SShri Abhyankar     }
58139c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58149c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58159c6b16b5SShri Abhyankar     const PetscScalar *rtol;
58169c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58179c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
581876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58197453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58207453f775SEmil Constantinescu       tola = ts->atol;
58217453f775SEmil Constantinescu       if (tola>0.){
58227453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58237453f775SEmil Constantinescu         na_loc++;
58247453f775SEmil Constantinescu       }
58257453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58267453f775SEmil Constantinescu       if (tolr>0.){
58277453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58287453f775SEmil Constantinescu         nr_loc++;
58297453f775SEmil Constantinescu       }
58307453f775SEmil Constantinescu       tol=tola+tolr;
58317453f775SEmil Constantinescu       if (tol>0.){
58327453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58337453f775SEmil Constantinescu         n_loc++;
58347453f775SEmil Constantinescu       }
58359c6b16b5SShri Abhyankar     }
58369c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58379c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
58389c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
583976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58407453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
58417453f775SEmil Constantinescu       tola = ts->atol;
58427453f775SEmil Constantinescu       if (tola>0.){
58437453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
58447453f775SEmil Constantinescu         na_loc++;
58457453f775SEmil Constantinescu       }
58467453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58477453f775SEmil Constantinescu       if (tolr>0.){
58487453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
58497453f775SEmil Constantinescu         nr_loc++;
58507453f775SEmil Constantinescu       }
58517453f775SEmil Constantinescu       tol=tola+tolr;
58527453f775SEmil Constantinescu       if (tol>0.){
58537453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
58547453f775SEmil Constantinescu         n_loc++;
58557453f775SEmil Constantinescu       }
58569c6b16b5SShri Abhyankar     }
58579c6b16b5SShri Abhyankar   }
58589c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58599c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58609c6b16b5SShri Abhyankar 
58617453f775SEmil Constantinescu   err_loc[0] = sum;
58627453f775SEmil Constantinescu   err_loc[1] = suma;
58637453f775SEmil Constantinescu   err_loc[2] = sumr;
58647453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58657453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58667453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58677453f775SEmil Constantinescu 
5868a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58697453f775SEmil Constantinescu 
58707453f775SEmil Constantinescu   gsum   = err_glb[0];
58717453f775SEmil Constantinescu   gsuma  = err_glb[1];
58727453f775SEmil Constantinescu   gsumr  = err_glb[2];
58737453f775SEmil Constantinescu   n_glb  = err_glb[3];
58747453f775SEmil Constantinescu   na_glb = err_glb[4];
58757453f775SEmil Constantinescu   nr_glb = err_glb[5];
58767453f775SEmil Constantinescu 
5877b1316ef9SEmil Constantinescu   *norm  = 0.;
5878b1316ef9SEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
5879b1316ef9SEmil Constantinescu   *norma = 0.;
5880b1316ef9SEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5881b1316ef9SEmil Constantinescu   *normr = 0.;
5882b1316ef9SEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58839c6b16b5SShri Abhyankar 
58849c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58857453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58867453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58879c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
58889c6b16b5SShri Abhyankar }
58899c6b16b5SShri Abhyankar 
58909c6b16b5SShri Abhyankar /*@
5891a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
58929c6b16b5SShri Abhyankar 
58939c6b16b5SShri Abhyankar    Collective on TS
58949c6b16b5SShri Abhyankar 
58959c6b16b5SShri Abhyankar    Input Arguments:
58969c6b16b5SShri Abhyankar +  ts - time stepping context
5897a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5898a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
58999c6b16b5SShri Abhyankar 
59009c6b16b5SShri Abhyankar    Output Arguments:
5901a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59027453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
5903a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59049c6b16b5SShri Abhyankar 
59059c6b16b5SShri Abhyankar    Level: developer
59069c6b16b5SShri Abhyankar 
5907deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
59089c6b16b5SShri Abhyankar @*/
59097453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59109c6b16b5SShri Abhyankar {
59119c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
59127453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
59139c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
59147453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
59157453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
59167453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59179c6b16b5SShri Abhyankar 
59189c6b16b5SShri Abhyankar   PetscFunctionBegin;
59199c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5920a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5921a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5922a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5923a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5924a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5925a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
59268a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
59278a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5928a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
59299c6b16b5SShri Abhyankar 
59309c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
59319c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
59329c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
59339c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59349c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59357453f775SEmil Constantinescu 
59367453f775SEmil Constantinescu   max=0.;
59377453f775SEmil Constantinescu   maxa=0.;
59387453f775SEmil Constantinescu   maxr=0.;
59397453f775SEmil Constantinescu 
59407453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59419c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
59429c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59439c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59447453f775SEmil Constantinescu 
59457453f775SEmil Constantinescu     for (i=0; i<n; i++) {
594676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59477453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59487453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59497453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59507453f775SEmil Constantinescu       tol  = tola+tolr;
59517453f775SEmil Constantinescu       if (tola>0.){
59527453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59537453f775SEmil Constantinescu       }
59547453f775SEmil Constantinescu       if (tolr>0.){
59557453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59567453f775SEmil Constantinescu       }
59577453f775SEmil Constantinescu       if (tol>0.){
59587453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59597453f775SEmil Constantinescu       }
59609c6b16b5SShri Abhyankar     }
59619c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59629c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59639c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59649c6b16b5SShri Abhyankar     const PetscScalar *atol;
59659c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59667453f775SEmil Constantinescu     for (i=0; i<n; i++) {
596776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59687453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59697453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59707453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59717453f775SEmil Constantinescu       tol  = tola+tolr;
59727453f775SEmil Constantinescu       if (tola>0.){
59737453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59747453f775SEmil Constantinescu       }
59757453f775SEmil Constantinescu       if (tolr>0.){
59767453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59777453f775SEmil Constantinescu       }
59787453f775SEmil Constantinescu       if (tol>0.){
59797453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
59807453f775SEmil Constantinescu       }
59819c6b16b5SShri Abhyankar     }
59829c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59839c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59849c6b16b5SShri Abhyankar     const PetscScalar *rtol;
59859c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59867453f775SEmil Constantinescu 
59877453f775SEmil Constantinescu     for (i=0; i<n; i++) {
598876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59897453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59907453f775SEmil Constantinescu       tola = ts->atol;
59917453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59927453f775SEmil Constantinescu       tol  = tola+tolr;
59937453f775SEmil Constantinescu       if (tola>0.){
59947453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
59957453f775SEmil Constantinescu       }
59967453f775SEmil Constantinescu       if (tolr>0.){
59977453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
59987453f775SEmil Constantinescu       }
59997453f775SEmil Constantinescu       if (tol>0.){
60007453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60017453f775SEmil Constantinescu       }
60029c6b16b5SShri Abhyankar     }
60039c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60049c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
60057453f775SEmil Constantinescu 
60067453f775SEmil Constantinescu     for (i=0; i<n; i++) {
600776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60087453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60097453f775SEmil Constantinescu       tola = ts->atol;
60107453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60117453f775SEmil Constantinescu       tol  = tola+tolr;
60127453f775SEmil Constantinescu       if (tola>0.){
60137453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60147453f775SEmil Constantinescu       }
60157453f775SEmil Constantinescu       if (tolr>0.){
60167453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60177453f775SEmil Constantinescu       }
60187453f775SEmil Constantinescu       if (tol>0.){
60197453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60207453f775SEmil Constantinescu       }
60219c6b16b5SShri Abhyankar     }
60229c6b16b5SShri Abhyankar   }
60239c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60249c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60257453f775SEmil Constantinescu   err_loc[0] = max;
60267453f775SEmil Constantinescu   err_loc[1] = maxa;
60277453f775SEmil Constantinescu   err_loc[2] = maxr;
6028a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60297453f775SEmil Constantinescu   gmax   = err_glb[0];
60307453f775SEmil Constantinescu   gmaxa  = err_glb[1];
60317453f775SEmil Constantinescu   gmaxr  = err_glb[2];
60329c6b16b5SShri Abhyankar 
60339c6b16b5SShri Abhyankar   *norm = gmax;
60347453f775SEmil Constantinescu   *norma = gmaxa;
60357453f775SEmil Constantinescu   *normr = gmaxr;
60369c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60377453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60387453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60399c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60409c6b16b5SShri Abhyankar }
60419c6b16b5SShri Abhyankar 
60421c3436cfSJed Brown /*@
60438a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
60441c3436cfSJed Brown 
60451c3436cfSJed Brown    Collective on TS
60461c3436cfSJed Brown 
60471c3436cfSJed Brown    Input Arguments:
60481c3436cfSJed Brown +  ts - time stepping context
6049a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6050a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6051a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60527619abb3SShri 
60531c3436cfSJed Brown    Output Arguments:
6054a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60558a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6056a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6057a4868fbcSLisandro Dalcin 
6058a4868fbcSLisandro Dalcin    Options Database Keys:
6059a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6060a4868fbcSLisandro Dalcin 
60611c3436cfSJed Brown    Level: developer
60621c3436cfSJed Brown 
60638a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
60641c3436cfSJed Brown @*/
60657453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60661c3436cfSJed Brown {
60678beabaa1SBarry Smith   PetscErrorCode ierr;
60681c3436cfSJed Brown 
60691c3436cfSJed Brown   PetscFunctionBegin;
6070a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
60717453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6072a4868fbcSLisandro Dalcin   } else if (wnormtype == NORM_INFINITY) {
60737453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6074a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60751c3436cfSJed Brown   PetscFunctionReturn(0);
60761c3436cfSJed Brown }
60771c3436cfSJed Brown 
60788a175baeSEmil Constantinescu 
60798a175baeSEmil Constantinescu /*@
60808a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
60818a175baeSEmil Constantinescu 
60828a175baeSEmil Constantinescu    Collective on TS
60838a175baeSEmil Constantinescu 
60848a175baeSEmil Constantinescu    Input Arguments:
60858a175baeSEmil Constantinescu +  ts - time stepping context
60868a175baeSEmil Constantinescu .  E - error vector
60878a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60888a175baeSEmil Constantinescu -  Y - state vector, previous time step
60898a175baeSEmil Constantinescu 
60908a175baeSEmil Constantinescu    Output Arguments:
6091a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60928a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6093a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60948a175baeSEmil Constantinescu 
60958a175baeSEmil Constantinescu    Level: developer
60968a175baeSEmil Constantinescu 
60978a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
60988a175baeSEmil Constantinescu @*/
60998a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61008a175baeSEmil Constantinescu {
61018a175baeSEmil Constantinescu   PetscErrorCode    ierr;
61028a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
61038a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
61048a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
61058a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
61068a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
61078a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
61088a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
61098a175baeSEmil Constantinescu 
61108a175baeSEmil Constantinescu   PetscFunctionBegin;
61118a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61128a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
61138a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
61148a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
61158a175baeSEmil Constantinescu   PetscValidType(E,2);
61168a175baeSEmil Constantinescu   PetscValidType(U,3);
61178a175baeSEmil Constantinescu   PetscValidType(Y,4);
61188a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
61198a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
61208a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
61218a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
61228a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
61238a175baeSEmil Constantinescu 
61248a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
61258a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
61268a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
61278a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
61288a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61298a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61308a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
61318a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
61328a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
61338a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
61348a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
61358a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61368a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61378a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
613876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61398a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61408a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61418a175baeSEmil Constantinescu       if (tola>0.){
61428a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61438a175baeSEmil Constantinescu         na_loc++;
61448a175baeSEmil Constantinescu       }
61458a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61468a175baeSEmil Constantinescu       if (tolr>0.){
61478a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61488a175baeSEmil Constantinescu         nr_loc++;
61498a175baeSEmil Constantinescu       }
61508a175baeSEmil Constantinescu       tol=tola+tolr;
61518a175baeSEmil Constantinescu       if (tol>0.){
61528a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61538a175baeSEmil Constantinescu         n_loc++;
61548a175baeSEmil Constantinescu       }
61558a175baeSEmil Constantinescu     }
61568a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61578a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61588a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61598a175baeSEmil Constantinescu     const PetscScalar *atol;
61608a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61618a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
616276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61638a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61648a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61658a175baeSEmil Constantinescu       if (tola>0.){
61668a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61678a175baeSEmil Constantinescu         na_loc++;
61688a175baeSEmil Constantinescu       }
61698a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61708a175baeSEmil Constantinescu       if (tolr>0.){
61718a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61728a175baeSEmil Constantinescu         nr_loc++;
61738a175baeSEmil Constantinescu       }
61748a175baeSEmil Constantinescu       tol=tola+tolr;
61758a175baeSEmil Constantinescu       if (tol>0.){
61768a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
61778a175baeSEmil Constantinescu         n_loc++;
61788a175baeSEmil Constantinescu       }
61798a175baeSEmil Constantinescu     }
61808a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61818a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61828a175baeSEmil Constantinescu     const PetscScalar *rtol;
61838a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61848a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
618576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61868a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61878a175baeSEmil Constantinescu       tola = ts->atol;
61888a175baeSEmil Constantinescu       if (tola>0.){
61898a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
61908a175baeSEmil Constantinescu         na_loc++;
61918a175baeSEmil Constantinescu       }
61928a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61938a175baeSEmil Constantinescu       if (tolr>0.){
61948a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
61958a175baeSEmil Constantinescu         nr_loc++;
61968a175baeSEmil Constantinescu       }
61978a175baeSEmil Constantinescu       tol=tola+tolr;
61988a175baeSEmil Constantinescu       if (tol>0.){
61998a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62008a175baeSEmil Constantinescu         n_loc++;
62018a175baeSEmil Constantinescu       }
62028a175baeSEmil Constantinescu     }
62038a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62048a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
62058a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
620676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
62078a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62088a175baeSEmil Constantinescu       tola = ts->atol;
62098a175baeSEmil Constantinescu       if (tola>0.){
62108a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62118a175baeSEmil Constantinescu         na_loc++;
62128a175baeSEmil Constantinescu       }
62138a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62148a175baeSEmil Constantinescu       if (tolr>0.){
62158a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62168a175baeSEmil Constantinescu         nr_loc++;
62178a175baeSEmil Constantinescu       }
62188a175baeSEmil Constantinescu       tol=tola+tolr;
62198a175baeSEmil Constantinescu       if (tol>0.){
62208a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62218a175baeSEmil Constantinescu         n_loc++;
62228a175baeSEmil Constantinescu       }
62238a175baeSEmil Constantinescu     }
62248a175baeSEmil Constantinescu   }
62258a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
62268a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62278a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62288a175baeSEmil Constantinescu 
62298a175baeSEmil Constantinescu   err_loc[0] = sum;
62308a175baeSEmil Constantinescu   err_loc[1] = suma;
62318a175baeSEmil Constantinescu   err_loc[2] = sumr;
62328a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62338a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62348a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62358a175baeSEmil Constantinescu 
6236a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62378a175baeSEmil Constantinescu 
62388a175baeSEmil Constantinescu   gsum   = err_glb[0];
62398a175baeSEmil Constantinescu   gsuma  = err_glb[1];
62408a175baeSEmil Constantinescu   gsumr  = err_glb[2];
62418a175baeSEmil Constantinescu   n_glb  = err_glb[3];
62428a175baeSEmil Constantinescu   na_glb = err_glb[4];
62438a175baeSEmil Constantinescu   nr_glb = err_glb[5];
62448a175baeSEmil Constantinescu 
62458a175baeSEmil Constantinescu   *norm  = 0.;
62468a175baeSEmil Constantinescu   if (n_glb>0.){*norm  = PetscSqrtReal(gsum  / n_glb);}
62478a175baeSEmil Constantinescu   *norma = 0.;
62488a175baeSEmil Constantinescu   if (na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
62498a175baeSEmil Constantinescu   *normr = 0.;
62508a175baeSEmil Constantinescu   if (nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62518a175baeSEmil Constantinescu 
62528a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
62538a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
62548a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
62558a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62568a175baeSEmil Constantinescu }
62578a175baeSEmil Constantinescu 
62588a175baeSEmil Constantinescu /*@
62598a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
62608a175baeSEmil Constantinescu    Collective on TS
62618a175baeSEmil Constantinescu 
62628a175baeSEmil Constantinescu    Input Arguments:
62638a175baeSEmil Constantinescu +  ts - time stepping context
62648a175baeSEmil Constantinescu .  E - error vector
62658a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62668a175baeSEmil Constantinescu -  Y - state vector, previous time step
62678a175baeSEmil Constantinescu 
62688a175baeSEmil Constantinescu    Output Arguments:
6269a2b725a8SWilliam Gropp +  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62708a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
6271a2b725a8SWilliam Gropp -  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62728a175baeSEmil Constantinescu 
62738a175baeSEmil Constantinescu    Level: developer
62748a175baeSEmil Constantinescu 
62758a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
62768a175baeSEmil Constantinescu @*/
62778a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62788a175baeSEmil Constantinescu {
62798a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62808a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62818a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62828a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
62838a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62848a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
62858a175baeSEmil Constantinescu 
62868a175baeSEmil Constantinescu   PetscFunctionBegin;
62878a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62888a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
62898a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
62908a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
62918a175baeSEmil Constantinescu   PetscValidType(E,2);
62928a175baeSEmil Constantinescu   PetscValidType(U,3);
62938a175baeSEmil Constantinescu   PetscValidType(Y,4);
62948a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
62958a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
62968a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
62978a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
62988a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
62998a175baeSEmil Constantinescu 
63008a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
63018a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
63028a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
63038a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
63048a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
63058a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
63068a175baeSEmil Constantinescu 
63078a175baeSEmil Constantinescu   max=0.;
63088a175baeSEmil Constantinescu   maxa=0.;
63098a175baeSEmil Constantinescu   maxr=0.;
63108a175baeSEmil Constantinescu 
63118a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
63128a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
63138a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63148a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63158a175baeSEmil Constantinescu 
63168a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
631776cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63188a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63198a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63208a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63218a175baeSEmil Constantinescu       tol  = tola+tolr;
63228a175baeSEmil Constantinescu       if (tola>0.){
63238a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63248a175baeSEmil Constantinescu       }
63258a175baeSEmil Constantinescu       if (tolr>0.){
63268a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63278a175baeSEmil Constantinescu       }
63288a175baeSEmil Constantinescu       if (tol>0.){
63298a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63308a175baeSEmil Constantinescu       }
63318a175baeSEmil Constantinescu     }
63328a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63338a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63348a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63358a175baeSEmil Constantinescu     const PetscScalar *atol;
63368a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63378a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
633876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63398a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63408a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
63418a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63428a175baeSEmil Constantinescu       tol  = tola+tolr;
63438a175baeSEmil Constantinescu       if (tola>0.){
63448a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63458a175baeSEmil Constantinescu       }
63468a175baeSEmil Constantinescu       if (tolr>0.){
63478a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63488a175baeSEmil Constantinescu       }
63498a175baeSEmil Constantinescu       if (tol>0.){
63508a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63518a175baeSEmil Constantinescu       }
63528a175baeSEmil Constantinescu     }
63538a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63548a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63558a175baeSEmil Constantinescu     const PetscScalar *rtol;
63568a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63578a175baeSEmil Constantinescu 
63588a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
635976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63608a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63618a175baeSEmil Constantinescu       tola = ts->atol;
63628a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63638a175baeSEmil Constantinescu       tol  = tola+tolr;
63648a175baeSEmil Constantinescu       if (tola>0.){
63658a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63668a175baeSEmil Constantinescu       }
63678a175baeSEmil Constantinescu       if (tolr>0.){
63688a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63698a175baeSEmil Constantinescu       }
63708a175baeSEmil Constantinescu       if (tol>0.){
63718a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63728a175baeSEmil Constantinescu       }
63738a175baeSEmil Constantinescu     }
63748a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63758a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63768a175baeSEmil Constantinescu 
63778a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
637876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
63798a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63808a175baeSEmil Constantinescu       tola = ts->atol;
63818a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63828a175baeSEmil Constantinescu       tol  = tola+tolr;
63838a175baeSEmil Constantinescu       if (tola>0.){
63848a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
63858a175baeSEmil Constantinescu       }
63868a175baeSEmil Constantinescu       if (tolr>0.){
63878a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
63888a175baeSEmil Constantinescu       }
63898a175baeSEmil Constantinescu       if (tol>0.){
63908a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
63918a175baeSEmil Constantinescu       }
63928a175baeSEmil Constantinescu     }
63938a175baeSEmil Constantinescu   }
63948a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
63958a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63968a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63978a175baeSEmil Constantinescu   err_loc[0] = max;
63988a175baeSEmil Constantinescu   err_loc[1] = maxa;
63998a175baeSEmil Constantinescu   err_loc[2] = maxr;
6400a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64018a175baeSEmil Constantinescu   gmax   = err_glb[0];
64028a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
64038a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
64048a175baeSEmil Constantinescu 
64058a175baeSEmil Constantinescu   *norm = gmax;
64068a175baeSEmil Constantinescu   *norma = gmaxa;
64078a175baeSEmil Constantinescu   *normr = gmaxr;
64088a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
64098a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
64108a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
64118a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64128a175baeSEmil Constantinescu }
64138a175baeSEmil Constantinescu 
64148a175baeSEmil Constantinescu /*@
64158a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
64168a175baeSEmil Constantinescu 
64178a175baeSEmil Constantinescu    Collective on TS
64188a175baeSEmil Constantinescu 
64198a175baeSEmil Constantinescu    Input Arguments:
64208a175baeSEmil Constantinescu +  ts - time stepping context
64218a175baeSEmil Constantinescu .  E - error vector
64228a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64238a175baeSEmil Constantinescu .  Y - state vector, previous time step
64248a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64258a175baeSEmil Constantinescu 
64268a175baeSEmil Constantinescu    Output Arguments:
6427a2b725a8SWilliam Gropp +  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64288a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
6429a2b725a8SWilliam Gropp -  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
64308a175baeSEmil Constantinescu 
64318a175baeSEmil Constantinescu    Options Database Keys:
64328a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
64338a175baeSEmil Constantinescu 
64348a175baeSEmil Constantinescu    Level: developer
64358a175baeSEmil Constantinescu 
64368a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
64378a175baeSEmil Constantinescu @*/
64388a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64398a175baeSEmil Constantinescu {
64408a175baeSEmil Constantinescu   PetscErrorCode ierr;
64418a175baeSEmil Constantinescu 
64428a175baeSEmil Constantinescu   PetscFunctionBegin;
64438a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
64448a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64458a175baeSEmil Constantinescu   } else if (wnormtype == NORM_INFINITY) {
64468a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
64478a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64488a175baeSEmil Constantinescu   PetscFunctionReturn(0);
64498a175baeSEmil Constantinescu }
64508a175baeSEmil Constantinescu 
64518a175baeSEmil Constantinescu 
64528d59e960SJed Brown /*@
64538d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
64548d59e960SJed Brown 
64558d59e960SJed Brown    Logically Collective on TS
64568d59e960SJed Brown 
64578d59e960SJed Brown    Input Arguments:
64588d59e960SJed Brown +  ts - time stepping context
64598d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
64608d59e960SJed Brown 
64618d59e960SJed Brown    Note:
64628d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
64638d59e960SJed Brown 
64648d59e960SJed Brown    Level: intermediate
64658d59e960SJed Brown 
64668d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
64678d59e960SJed Brown @*/
64688d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
64698d59e960SJed Brown {
64708d59e960SJed Brown   PetscFunctionBegin;
64718d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64728d59e960SJed Brown   ts->cfltime_local = cfltime;
64738d59e960SJed Brown   ts->cfltime       = -1.;
64748d59e960SJed Brown   PetscFunctionReturn(0);
64758d59e960SJed Brown }
64768d59e960SJed Brown 
64778d59e960SJed Brown /*@
64788d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
64798d59e960SJed Brown 
64808d59e960SJed Brown    Collective on TS
64818d59e960SJed Brown 
64828d59e960SJed Brown    Input Arguments:
64838d59e960SJed Brown .  ts - time stepping context
64848d59e960SJed Brown 
64858d59e960SJed Brown    Output Arguments:
64868d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
64878d59e960SJed Brown 
64888d59e960SJed Brown    Level: advanced
64898d59e960SJed Brown 
64908d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
64918d59e960SJed Brown @*/
64928d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
64938d59e960SJed Brown {
64948d59e960SJed Brown   PetscErrorCode ierr;
64958d59e960SJed Brown 
64968d59e960SJed Brown   PetscFunctionBegin;
64978d59e960SJed Brown   if (ts->cfltime < 0) {
6498b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64998d59e960SJed Brown   }
65008d59e960SJed Brown   *cfltime = ts->cfltime;
65018d59e960SJed Brown   PetscFunctionReturn(0);
65028d59e960SJed Brown }
65038d59e960SJed Brown 
6504d6ebe24aSShri Abhyankar /*@
6505d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6506d6ebe24aSShri Abhyankar 
6507d6ebe24aSShri Abhyankar    Input Parameters:
6508a2b725a8SWilliam Gropp +  ts   - the TS context.
6509d6ebe24aSShri Abhyankar .  xl   - lower bound.
6510a2b725a8SWilliam Gropp -  xu   - upper bound.
6511d6ebe24aSShri Abhyankar 
6512d6ebe24aSShri Abhyankar    Notes:
6513d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6514e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6515d6ebe24aSShri Abhyankar 
65162bd2b0e6SSatish Balay    Level: advanced
65172bd2b0e6SSatish Balay 
6518d6ebe24aSShri Abhyankar @*/
6519d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6520d6ebe24aSShri Abhyankar {
6521d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6522d6ebe24aSShri Abhyankar   SNES           snes;
6523d6ebe24aSShri Abhyankar 
6524d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6525d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6526d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6527d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6528d6ebe24aSShri Abhyankar }
6529d6ebe24aSShri Abhyankar 
6530b3603a34SBarry Smith /*@C
65314f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6532b3603a34SBarry Smith        in a time based line graph
6533b3603a34SBarry Smith 
6534b3603a34SBarry Smith    Collective on TS
6535b3603a34SBarry Smith 
6536b3603a34SBarry Smith    Input Parameters:
6537b3603a34SBarry Smith +  ts - the TS context
6538b3603a34SBarry Smith .  step - current time-step
6539b3603a34SBarry Smith .  ptime - current time
65407db568b7SBarry Smith .  u - current solution
65417db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6542b3603a34SBarry Smith 
6543b3d3934dSBarry Smith    Options Database:
65449ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6545b3d3934dSBarry Smith 
6546b3603a34SBarry Smith    Level: intermediate
6547b3603a34SBarry Smith 
654895452b02SPatrick Sanan    Notes:
654995452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6550b3603a34SBarry Smith 
65517db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65527db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65537db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65547db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6555b3603a34SBarry Smith @*/
65567db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6557b3603a34SBarry Smith {
6558b3603a34SBarry Smith   PetscErrorCode    ierr;
65597db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6560b3603a34SBarry Smith   const PetscScalar *yy;
656180666b62SBarry Smith   Vec               v;
6562b3603a34SBarry Smith 
6563b3603a34SBarry Smith   PetscFunctionBegin;
656463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
656558ff32f7SBarry Smith   if (!step) {
6566a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65676934998bSLisandro Dalcin     PetscInt      dim;
6568a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
65690ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6570bab0a581SBarry Smith     if (!ctx->names) {
6571bab0a581SBarry Smith       PetscBool flg;
6572bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6573bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6574bab0a581SBarry Smith       if (flg) {
6575bab0a581SBarry Smith         PetscInt i,n;
6576bab0a581SBarry Smith         char     **names;
6577bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6578bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6579bab0a581SBarry Smith         for (i=0; i<n; i++) {
6580bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6581bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6582bab0a581SBarry Smith         }
6583bab0a581SBarry Smith         names[n] = NULL;
6584bab0a581SBarry Smith         ctx->names = names;
6585bab0a581SBarry Smith       }
6586bab0a581SBarry Smith     }
6587387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6588387f4636SBarry Smith       char      **displaynames;
6589387f4636SBarry Smith       PetscBool flg;
6590387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6591580bdb30SBarry Smith       ierr = PetscCalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6592c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6593387f4636SBarry Smith       if (flg) {
6594a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6595387f4636SBarry Smith       }
6596387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6597387f4636SBarry Smith     }
6598387f4636SBarry Smith     if (ctx->displaynames) {
6599387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6600387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6601387f4636SBarry Smith     } else if (ctx->names) {
66020910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
66030b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6604387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6605b0bc92c6SBarry Smith     } else {
6606b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6607b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6608387f4636SBarry Smith     }
66090b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
661058ff32f7SBarry Smith   }
66116934998bSLisandro Dalcin 
66126934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66136934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
661480666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66156934998bSLisandro Dalcin   if (ctx->displaynames) {
66166934998bSLisandro Dalcin     PetscInt i;
66176934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66186934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66196934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66206934998bSLisandro Dalcin   } else {
6621e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6622e3efe391SJed Brown     PetscInt  i,n;
66236934998bSLisandro Dalcin     PetscReal *yreal;
662480666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6625785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6626e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66270b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6628e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6629e3efe391SJed Brown #else
66300b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6631e3efe391SJed Brown #endif
663280666b62SBarry Smith   }
66336934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66346934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66356934998bSLisandro Dalcin 
6636b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66370b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66386934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66393923b477SBarry Smith   }
6640b3603a34SBarry Smith   PetscFunctionReturn(0);
6641b3603a34SBarry Smith }
6642b3603a34SBarry Smith 
6643b037adc7SBarry Smith /*@C
664431152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6645b037adc7SBarry Smith 
6646b037adc7SBarry Smith    Collective on TS
6647b037adc7SBarry Smith 
6648b037adc7SBarry Smith    Input Parameters:
6649b037adc7SBarry Smith +  ts - the TS context
6650b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6651b037adc7SBarry Smith 
6652b037adc7SBarry Smith    Level: intermediate
6653b037adc7SBarry Smith 
665495452b02SPatrick Sanan    Notes:
665595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66567db568b7SBarry Smith 
6657a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6658b037adc7SBarry Smith @*/
665931152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6660b037adc7SBarry Smith {
6661b037adc7SBarry Smith   PetscErrorCode    ierr;
6662b037adc7SBarry Smith   PetscInt          i;
6663b037adc7SBarry Smith 
6664b037adc7SBarry Smith   PetscFunctionBegin;
6665b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6666b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66675537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6668b3d3934dSBarry Smith       break;
6669b3d3934dSBarry Smith     }
6670b3d3934dSBarry Smith   }
6671b3d3934dSBarry Smith   PetscFunctionReturn(0);
6672b3d3934dSBarry Smith }
6673b3d3934dSBarry Smith 
6674e673d494SBarry Smith /*@C
6675e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6676e673d494SBarry Smith 
6677e673d494SBarry Smith    Collective on TS
6678e673d494SBarry Smith 
6679e673d494SBarry Smith    Input Parameters:
6680e673d494SBarry Smith +  ts - the TS context
6681e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6682e673d494SBarry Smith 
6683e673d494SBarry Smith    Level: intermediate
6684e673d494SBarry Smith 
6685a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6686e673d494SBarry Smith @*/
6687e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6688e673d494SBarry Smith {
6689e673d494SBarry Smith   PetscErrorCode    ierr;
6690e673d494SBarry Smith 
6691e673d494SBarry Smith   PetscFunctionBegin;
6692e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6693e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6694e673d494SBarry Smith   PetscFunctionReturn(0);
6695e673d494SBarry Smith }
6696e673d494SBarry Smith 
6697b3d3934dSBarry Smith /*@C
6698b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6699b3d3934dSBarry Smith 
6700b3d3934dSBarry Smith    Collective on TS
6701b3d3934dSBarry Smith 
6702b3d3934dSBarry Smith    Input Parameter:
6703b3d3934dSBarry Smith .  ts - the TS context
6704b3d3934dSBarry Smith 
6705b3d3934dSBarry Smith    Output Parameter:
6706b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6707b3d3934dSBarry Smith 
6708b3d3934dSBarry Smith    Level: intermediate
6709b3d3934dSBarry Smith 
671095452b02SPatrick Sanan    Notes:
671195452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67127db568b7SBarry Smith 
6713b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6714b3d3934dSBarry Smith @*/
6715b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6716b3d3934dSBarry Smith {
6717b3d3934dSBarry Smith   PetscInt       i;
6718b3d3934dSBarry Smith 
6719b3d3934dSBarry Smith   PetscFunctionBegin;
6720b3d3934dSBarry Smith   *names = NULL;
6721b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6722b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67235537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6724b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6725b3d3934dSBarry Smith       break;
6726387f4636SBarry Smith     }
6727387f4636SBarry Smith   }
6728387f4636SBarry Smith   PetscFunctionReturn(0);
6729387f4636SBarry Smith }
6730387f4636SBarry Smith 
6731a66092f1SBarry Smith /*@C
6732a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6733a66092f1SBarry Smith 
6734a66092f1SBarry Smith    Collective on TS
6735a66092f1SBarry Smith 
6736a66092f1SBarry Smith    Input Parameters:
6737a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6738a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6739a66092f1SBarry Smith 
6740a66092f1SBarry Smith    Level: intermediate
6741a66092f1SBarry Smith 
6742a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6743a66092f1SBarry Smith @*/
6744a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6745a66092f1SBarry Smith {
6746a66092f1SBarry Smith   PetscInt          j = 0,k;
6747a66092f1SBarry Smith   PetscErrorCode    ierr;
6748a66092f1SBarry Smith 
6749a66092f1SBarry Smith   PetscFunctionBegin;
6750a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6751a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6752a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6753a66092f1SBarry Smith   while (displaynames[j]) j++;
6754a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6755a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6756a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6757a66092f1SBarry Smith   j = 0;
6758a66092f1SBarry Smith   while (displaynames[j]) {
6759a66092f1SBarry Smith     k = 0;
6760a66092f1SBarry Smith     while (ctx->names[k]) {
6761a66092f1SBarry Smith       PetscBool flg;
6762a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6763a66092f1SBarry Smith       if (flg) {
6764a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6765a66092f1SBarry Smith         break;
6766a66092f1SBarry Smith       }
6767a66092f1SBarry Smith       k++;
6768a66092f1SBarry Smith     }
6769a66092f1SBarry Smith     j++;
6770a66092f1SBarry Smith   }
6771a66092f1SBarry Smith   PetscFunctionReturn(0);
6772a66092f1SBarry Smith }
6773a66092f1SBarry Smith 
6774387f4636SBarry Smith /*@C
6775387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6776387f4636SBarry Smith 
6777387f4636SBarry Smith    Collective on TS
6778387f4636SBarry Smith 
6779387f4636SBarry Smith    Input Parameters:
6780387f4636SBarry Smith +  ts - the TS context
6781a2b725a8SWilliam Gropp -  displaynames - the names of the components, final string must be NULL
6782387f4636SBarry Smith 
678395452b02SPatrick Sanan    Notes:
678495452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67857db568b7SBarry Smith 
6786387f4636SBarry Smith    Level: intermediate
6787387f4636SBarry Smith 
6788387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6789387f4636SBarry Smith @*/
6790387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6791387f4636SBarry Smith {
6792a66092f1SBarry Smith   PetscInt          i;
6793387f4636SBarry Smith   PetscErrorCode    ierr;
6794387f4636SBarry Smith 
6795387f4636SBarry Smith   PetscFunctionBegin;
6796387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6797387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67985537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6799b3d3934dSBarry Smith       break;
6800b037adc7SBarry Smith     }
6801b037adc7SBarry Smith   }
6802b037adc7SBarry Smith   PetscFunctionReturn(0);
6803b037adc7SBarry Smith }
6804b037adc7SBarry Smith 
680580666b62SBarry Smith /*@C
680680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
680780666b62SBarry Smith 
680880666b62SBarry Smith    Collective on TS
680980666b62SBarry Smith 
681080666b62SBarry Smith    Input Parameters:
681180666b62SBarry Smith +  ts - the TS context
681280666b62SBarry Smith .  transform - the transform function
68137684fa3eSBarry Smith .  destroy - function to destroy the optional context
681480666b62SBarry Smith -  ctx - optional context used by transform function
681580666b62SBarry Smith 
681695452b02SPatrick Sanan    Notes:
681795452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68187db568b7SBarry Smith 
681980666b62SBarry Smith    Level: intermediate
682080666b62SBarry Smith 
6821a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
682280666b62SBarry Smith @*/
68237684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
682480666b62SBarry Smith {
682580666b62SBarry Smith   PetscInt          i;
6826a66092f1SBarry Smith   PetscErrorCode    ierr;
682780666b62SBarry Smith 
682880666b62SBarry Smith   PetscFunctionBegin;
682980666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
683080666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68315537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
683280666b62SBarry Smith     }
683380666b62SBarry Smith   }
683480666b62SBarry Smith   PetscFunctionReturn(0);
683580666b62SBarry Smith }
683680666b62SBarry Smith 
6837e673d494SBarry Smith /*@C
6838e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6839e673d494SBarry Smith 
6840e673d494SBarry Smith    Collective on TSLGCtx
6841e673d494SBarry Smith 
6842e673d494SBarry Smith    Input Parameters:
6843e673d494SBarry Smith +  ts - the TS context
6844e673d494SBarry Smith .  transform - the transform function
68457684fa3eSBarry Smith .  destroy - function to destroy the optional context
6846e673d494SBarry Smith -  ctx - optional context used by transform function
6847e673d494SBarry Smith 
6848e673d494SBarry Smith    Level: intermediate
6849e673d494SBarry Smith 
6850a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6851e673d494SBarry Smith @*/
68527684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6853e673d494SBarry Smith {
6854e673d494SBarry Smith   PetscFunctionBegin;
6855e673d494SBarry Smith   ctx->transform    = transform;
68567684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6857e673d494SBarry Smith   ctx->transformctx = tctx;
6858e673d494SBarry Smith   PetscFunctionReturn(0);
6859e673d494SBarry Smith }
6860e673d494SBarry Smith 
6861ef20d060SBarry Smith /*@C
68628b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6863ef20d060SBarry Smith        in a time based line graph
6864ef20d060SBarry Smith 
6865ef20d060SBarry Smith    Collective on TS
6866ef20d060SBarry Smith 
6867ef20d060SBarry Smith    Input Parameters:
6868ef20d060SBarry Smith +  ts - the TS context
6869ef20d060SBarry Smith .  step - current time-step
6870ef20d060SBarry Smith .  ptime - current time
68717db568b7SBarry Smith .  u - current solution
68727db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6873ef20d060SBarry Smith 
6874ef20d060SBarry Smith    Level: intermediate
6875ef20d060SBarry Smith 
687695452b02SPatrick Sanan    Notes:
687795452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6878abd5a294SJed Brown 
6879abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6880abd5a294SJed Brown 
6881abd5a294SJed Brown    Options Database Keys:
68824f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6883ef20d060SBarry Smith 
6884abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6885ef20d060SBarry Smith @*/
68860910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6887ef20d060SBarry Smith {
6888ef20d060SBarry Smith   PetscErrorCode    ierr;
68890b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6890ef20d060SBarry Smith   const PetscScalar *yy;
6891ef20d060SBarry Smith   Vec               y;
6892ef20d060SBarry Smith 
6893ef20d060SBarry Smith   PetscFunctionBegin;
6894a9f9c1f6SBarry Smith   if (!step) {
6895a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68966934998bSLisandro Dalcin     PetscInt      dim;
6897a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68988b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68990910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6900a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6901a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6902a9f9c1f6SBarry Smith   }
69030910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6904ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69050910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6906ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6907e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6908e3efe391SJed Brown   {
6909e3efe391SJed Brown     PetscReal *yreal;
6910e3efe391SJed Brown     PetscInt  i,n;
6911e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6912785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6913e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69140b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6915e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6916e3efe391SJed Brown   }
6917e3efe391SJed Brown #else
69180b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6919e3efe391SJed Brown #endif
6920ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6921ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6922b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69230b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69246934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69253923b477SBarry Smith   }
6926ef20d060SBarry Smith   PetscFunctionReturn(0);
6927ef20d060SBarry Smith }
6928ef20d060SBarry Smith 
69295e3b7effSJoseph Pusztay /*@C
69305e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
69315e3b7effSJoseph Pusztay 
69325e3b7effSJoseph Pusztay    Input Parameters:
69335e3b7effSJoseph Pusztay +  ts - the TS context
69345e3b7effSJoseph Pusztay .  step - current time-step
69355e3b7effSJoseph Pusztay .  ptime - current time
69365e3b7effSJoseph Pusztay .  u - current solution
69375e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
69385e3b7effSJoseph Pusztay 
69395e3b7effSJoseph Pusztay    Options Database:
6940918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
69415e3b7effSJoseph Pusztay 
69425e3b7effSJoseph Pusztay    Level: intermediate
69435e3b7effSJoseph Pusztay 
69445e3b7effSJoseph Pusztay @*/
69450ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
69461b575b74SJoseph Pusztay {
69471b575b74SJoseph Pusztay   PetscErrorCode    ierr;
69481b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
69491b575b74SJoseph Pusztay   const PetscScalar *yy;
6950b1670a61SJoseph Pusztay   PetscReal       *y,*x;
69511b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
69521b575b74SJoseph Pusztay   DM                dm;
69531b575b74SJoseph Pusztay 
69541b575b74SJoseph Pusztay   PetscFunctionBegin;
69551b575b74SJoseph Pusztay 
69561b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
69571b575b74SJoseph Pusztay   if (!step) {
69581b575b74SJoseph Pusztay     PetscDrawAxis axis;
69591b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
69601b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6961895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6962895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
69631b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
69641b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
69651b575b74SJoseph Pusztay     if (dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
69661b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69671b575b74SJoseph Pusztay     Np /= 2*dim;
69681b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
69691b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
69701b575b74SJoseph Pusztay   }
69711b575b74SJoseph Pusztay 
69721b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
69731b575b74SJoseph Pusztay   Np /= 2*dim;
69741b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6975895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
69761b575b74SJoseph Pusztay   /* get points from solution vector */
69771b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6978b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6979b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6980895f37d5SJoseph Pusztay   }
69811b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
69821b575b74SJoseph Pusztay 
69831b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69841b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
69851b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
69861b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
69871b575b74SJoseph Pusztay   }
69881b575b74SJoseph Pusztay 
6989918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6990918b1d10SJoseph Pusztay 
69911b575b74SJoseph Pusztay   PetscFunctionReturn(0);
69921b575b74SJoseph Pusztay }
69931b575b74SJoseph Pusztay 
69941b575b74SJoseph Pusztay 
69951b575b74SJoseph Pusztay 
69967cf37e64SBarry Smith /*@C
69977cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69987cf37e64SBarry Smith 
69997cf37e64SBarry Smith    Collective on TS
70007cf37e64SBarry Smith 
70017cf37e64SBarry Smith    Input Parameters:
70027cf37e64SBarry Smith +  ts - the TS context
70037cf37e64SBarry Smith .  step - current time-step
70047cf37e64SBarry Smith .  ptime - current time
70057cf37e64SBarry Smith .  u - current solution
70067cf37e64SBarry Smith -  dctx - unused context
70077cf37e64SBarry Smith 
70087cf37e64SBarry Smith    Level: intermediate
70097cf37e64SBarry Smith 
70107cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
70117cf37e64SBarry Smith 
70127cf37e64SBarry Smith    Options Database Keys:
70137cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
70147cf37e64SBarry Smith 
70157cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
70167cf37e64SBarry Smith @*/
7017edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70187cf37e64SBarry Smith {
70197cf37e64SBarry Smith   PetscErrorCode    ierr;
70207cf37e64SBarry Smith   Vec               y;
70217cf37e64SBarry Smith   PetscReal         nrm;
7022edbaebb3SBarry Smith   PetscBool         flg;
70237cf37e64SBarry Smith 
70247cf37e64SBarry Smith   PetscFunctionBegin;
70257cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70267cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70277cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7028edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7029edbaebb3SBarry Smith   if (flg) {
70307cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7031edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7032edbaebb3SBarry Smith   }
7033edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7034edbaebb3SBarry Smith   if (flg) {
7035edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7036edbaebb3SBarry Smith   }
7037edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70387cf37e64SBarry Smith   PetscFunctionReturn(0);
70397cf37e64SBarry Smith }
70407cf37e64SBarry Smith 
7041201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7042201da799SBarry Smith {
7043201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7044201da799SBarry Smith   PetscReal      x   = ptime,y;
7045201da799SBarry Smith   PetscErrorCode ierr;
7046201da799SBarry Smith   PetscInt       its;
7047201da799SBarry Smith 
7048201da799SBarry Smith   PetscFunctionBegin;
704963e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7050201da799SBarry Smith   if (!n) {
7051201da799SBarry Smith     PetscDrawAxis axis;
7052201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7053201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7054201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7055201da799SBarry Smith     ctx->snes_its = 0;
7056201da799SBarry Smith   }
7057201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7058201da799SBarry Smith   y    = its - ctx->snes_its;
7059201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70603fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7061201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70626934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7063201da799SBarry Smith   }
7064201da799SBarry Smith   ctx->snes_its = its;
7065201da799SBarry Smith   PetscFunctionReturn(0);
7066201da799SBarry Smith }
7067201da799SBarry Smith 
7068201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7069201da799SBarry Smith {
7070201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7071201da799SBarry Smith   PetscReal      x   = ptime,y;
7072201da799SBarry Smith   PetscErrorCode ierr;
7073201da799SBarry Smith   PetscInt       its;
7074201da799SBarry Smith 
7075201da799SBarry Smith   PetscFunctionBegin;
707663e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7077201da799SBarry Smith   if (!n) {
7078201da799SBarry Smith     PetscDrawAxis axis;
7079201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7080201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7081201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7082201da799SBarry Smith     ctx->ksp_its = 0;
7083201da799SBarry Smith   }
7084201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7085201da799SBarry Smith   y    = its - ctx->ksp_its;
7086201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
708799fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7088201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70896934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7090201da799SBarry Smith   }
7091201da799SBarry Smith   ctx->ksp_its = its;
7092201da799SBarry Smith   PetscFunctionReturn(0);
7093201da799SBarry Smith }
7094f9c1d6abSBarry Smith 
7095f9c1d6abSBarry Smith /*@
7096f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7097f9c1d6abSBarry Smith 
7098d083f849SBarry Smith    Collective on TS
7099f9c1d6abSBarry Smith 
7100f9c1d6abSBarry Smith    Input Parameters:
7101f9c1d6abSBarry Smith +  ts - the TS context
7102f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7103f9c1d6abSBarry Smith 
7104f9c1d6abSBarry Smith    Output Parameters:
7105f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7106f9c1d6abSBarry Smith 
7107f9c1d6abSBarry Smith    Level: developer
7108f9c1d6abSBarry Smith 
7109f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7110f9c1d6abSBarry Smith @*/
7111f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7112f9c1d6abSBarry Smith {
7113f9c1d6abSBarry Smith   PetscErrorCode ierr;
7114f9c1d6abSBarry Smith 
7115f9c1d6abSBarry Smith   PetscFunctionBegin;
7116f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7117ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7118f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7119f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7120f9c1d6abSBarry Smith }
712124655328SShri 
7122b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7123b3d3934dSBarry Smith /*@C
7124b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7125b3d3934dSBarry Smith 
7126b3d3934dSBarry Smith    Collective on TS
7127b3d3934dSBarry Smith 
7128b3d3934dSBarry Smith    Input Parameters:
7129b3d3934dSBarry Smith .  ts  - the ODE solver object
7130b3d3934dSBarry Smith 
7131b3d3934dSBarry Smith    Output Parameter:
7132b3d3934dSBarry Smith .  ctx - the context
7133b3d3934dSBarry Smith 
7134b3d3934dSBarry Smith    Level: intermediate
7135b3d3934dSBarry Smith 
7136b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7137b3d3934dSBarry Smith 
7138b3d3934dSBarry Smith @*/
7139b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7140b3d3934dSBarry Smith {
7141b3d3934dSBarry Smith   PetscErrorCode ierr;
7142b3d3934dSBarry Smith 
7143b3d3934dSBarry Smith   PetscFunctionBegin;
7144a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7145b3d3934dSBarry Smith   PetscFunctionReturn(0);
7146b3d3934dSBarry Smith }
7147b3d3934dSBarry Smith 
7148b3d3934dSBarry Smith /*@C
7149b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7150b3d3934dSBarry Smith 
7151b3d3934dSBarry Smith    Collective on TS
7152b3d3934dSBarry Smith 
7153b3d3934dSBarry Smith    Input Parameters:
7154b3d3934dSBarry Smith +  ts - the TS context
7155b3d3934dSBarry Smith .  step - current time-step
7156b3d3934dSBarry Smith .  ptime - current time
71577db568b7SBarry Smith .  u  - current solution
71587db568b7SBarry Smith -  dctx - the envelope context
7159b3d3934dSBarry Smith 
7160b3d3934dSBarry Smith    Options Database:
7161b3d3934dSBarry Smith .  -ts_monitor_envelope
7162b3d3934dSBarry Smith 
7163b3d3934dSBarry Smith    Level: intermediate
7164b3d3934dSBarry Smith 
716595452b02SPatrick Sanan    Notes:
716695452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7167b3d3934dSBarry Smith 
71687db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7169b3d3934dSBarry Smith @*/
71707db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7171b3d3934dSBarry Smith {
7172b3d3934dSBarry Smith   PetscErrorCode       ierr;
71737db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7174b3d3934dSBarry Smith 
7175b3d3934dSBarry Smith   PetscFunctionBegin;
7176b3d3934dSBarry Smith   if (!ctx->max) {
7177b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7178b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7179b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7180b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7181b3d3934dSBarry Smith   } else {
7182b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7183b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7184b3d3934dSBarry Smith   }
7185b3d3934dSBarry Smith   PetscFunctionReturn(0);
7186b3d3934dSBarry Smith }
7187b3d3934dSBarry Smith 
7188b3d3934dSBarry Smith /*@C
7189b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7190b3d3934dSBarry Smith 
7191b3d3934dSBarry Smith    Collective on TS
7192b3d3934dSBarry Smith 
7193b3d3934dSBarry Smith    Input Parameter:
7194b3d3934dSBarry Smith .  ts - the TS context
7195b3d3934dSBarry Smith 
7196b3d3934dSBarry Smith    Output Parameter:
7197b3d3934dSBarry Smith +  max - the maximum values
7198b3d3934dSBarry Smith -  min - the minimum values
7199b3d3934dSBarry Smith 
720095452b02SPatrick Sanan    Notes:
720195452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
72027db568b7SBarry Smith 
7203b3d3934dSBarry Smith    Level: intermediate
7204b3d3934dSBarry Smith 
7205b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7206b3d3934dSBarry Smith @*/
7207b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7208b3d3934dSBarry Smith {
7209b3d3934dSBarry Smith   PetscInt i;
7210b3d3934dSBarry Smith 
7211b3d3934dSBarry Smith   PetscFunctionBegin;
7212b3d3934dSBarry Smith   if (max) *max = NULL;
7213b3d3934dSBarry Smith   if (min) *min = NULL;
7214b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7215b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
72165537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7217b3d3934dSBarry Smith       if (max) *max = ctx->max;
7218b3d3934dSBarry Smith       if (min) *min = ctx->min;
7219b3d3934dSBarry Smith       break;
7220b3d3934dSBarry Smith     }
7221b3d3934dSBarry Smith   }
7222b3d3934dSBarry Smith   PetscFunctionReturn(0);
7223b3d3934dSBarry Smith }
7224b3d3934dSBarry Smith 
7225b3d3934dSBarry Smith /*@C
7226b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7227b3d3934dSBarry Smith 
7228b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7229b3d3934dSBarry Smith 
7230b3d3934dSBarry Smith    Input Parameter:
7231b3d3934dSBarry Smith .  ctx - the monitor context
7232b3d3934dSBarry Smith 
7233b3d3934dSBarry Smith    Level: intermediate
7234b3d3934dSBarry Smith 
72357db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7236b3d3934dSBarry Smith @*/
7237b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7238b3d3934dSBarry Smith {
7239b3d3934dSBarry Smith   PetscErrorCode ierr;
7240b3d3934dSBarry Smith 
7241b3d3934dSBarry Smith   PetscFunctionBegin;
7242b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7243b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7244b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7245b3d3934dSBarry Smith   PetscFunctionReturn(0);
7246b3d3934dSBarry Smith }
7247f2dee214SBarry Smith 
724824655328SShri /*@
7249dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7250dcb233daSLisandro Dalcin 
7251dcb233daSLisandro Dalcin    Collective on TS
7252dcb233daSLisandro Dalcin 
7253dcb233daSLisandro Dalcin    Input Parameter:
7254dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7255dcb233daSLisandro Dalcin 
7256dcb233daSLisandro Dalcin    Level: advanced
7257dcb233daSLisandro Dalcin 
7258dcb233daSLisandro Dalcin    Notes:
7259dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7260dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7261dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7262dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7263dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7264dcb233daSLisandro Dalcin    discontinuous source terms).
7265dcb233daSLisandro Dalcin 
7266dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7267dcb233daSLisandro Dalcin @*/
7268dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7269dcb233daSLisandro Dalcin {
7270dcb233daSLisandro Dalcin   PetscFunctionBegin;
7271dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7272dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7273dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7274dcb233daSLisandro Dalcin }
7275dcb233daSLisandro Dalcin 
7276dcb233daSLisandro Dalcin /*@
727724655328SShri    TSRollBack - Rolls back one time step
727824655328SShri 
727924655328SShri    Collective on TS
728024655328SShri 
728124655328SShri    Input Parameter:
728224655328SShri .  ts - the TS context obtained from TSCreate()
728324655328SShri 
728424655328SShri    Level: advanced
728524655328SShri 
728624655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
728724655328SShri @*/
728824655328SShri PetscErrorCode  TSRollBack(TS ts)
728924655328SShri {
729024655328SShri   PetscErrorCode ierr;
729124655328SShri 
729224655328SShri   PetscFunctionBegin;
729324655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7294b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
729524655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
729624655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
729724655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
729824655328SShri   ts->ptime = ts->ptime_prev;
7299be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
73002808aa04SLisandro Dalcin   ts->steps--;
7301b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
730224655328SShri   PetscFunctionReturn(0);
730324655328SShri }
7304aeb4809dSShri Abhyankar 
7305ff22ae23SHong Zhang /*@
7306ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7307ff22ae23SHong Zhang 
7308ff22ae23SHong Zhang    Input Parameter:
7309ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7310ff22ae23SHong Zhang 
73110429704eSStefano Zampini    Output Parameters:
73120429704eSStefano Zampini +  ns - the number of stages
73130429704eSStefano Zampini -  Y - the current stage vectors
73140429704eSStefano Zampini 
7315ff22ae23SHong Zhang    Level: advanced
7316ff22ae23SHong Zhang 
73170429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
73180429704eSStefano Zampini 
7319ff22ae23SHong Zhang .seealso: TSCreate()
7320ff22ae23SHong Zhang @*/
7321ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7322ff22ae23SHong Zhang {
7323ff22ae23SHong Zhang   PetscErrorCode ierr;
7324ff22ae23SHong Zhang 
7325ff22ae23SHong Zhang   PetscFunctionBegin;
7326ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
73270429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
73280429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
73290429704eSStefano Zampini   if (!ts->ops->getstages) {
73300429704eSStefano Zampini     if (ns) *ns = 0;
73310429704eSStefano Zampini     if (Y) *Y = NULL;
73320429704eSStefano Zampini   } else {
7333ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7334ff22ae23SHong Zhang   }
7335ff22ae23SHong Zhang   PetscFunctionReturn(0);
7336ff22ae23SHong Zhang }
7337ff22ae23SHong Zhang 
7338847ff0e1SMatthew G. Knepley /*@C
7339847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7340847ff0e1SMatthew G. Knepley 
7341847ff0e1SMatthew G. Knepley   Collective on SNES
7342847ff0e1SMatthew G. Knepley 
7343847ff0e1SMatthew G. Knepley   Input Parameters:
7344847ff0e1SMatthew G. Knepley + ts - the TS context
7345847ff0e1SMatthew G. Knepley . t - current timestep
7346847ff0e1SMatthew G. Knepley . U - state vector
7347847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7348847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7349847ff0e1SMatthew G. Knepley - ctx - an optional user context
7350847ff0e1SMatthew G. Knepley 
7351847ff0e1SMatthew G. Knepley   Output Parameters:
7352847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7353847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7354847ff0e1SMatthew G. Knepley 
7355847ff0e1SMatthew G. Knepley   Level: intermediate
7356847ff0e1SMatthew G. Knepley 
7357847ff0e1SMatthew G. Knepley   Notes:
7358847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7359847ff0e1SMatthew G. Knepley 
7360847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7361847ff0e1SMatthew G. Knepley 
7362847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7363847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7364847ff0e1SMatthew G. Knepley 
7365847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7366847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7367847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7368847ff0e1SMatthew G. Knepley 
7369847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7370847ff0e1SMatthew G. Knepley @*/
7371847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7372847ff0e1SMatthew G. Knepley {
7373847ff0e1SMatthew G. Knepley   SNES           snes;
7374847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7375847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7376847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7377847ff0e1SMatthew G. Knepley 
7378847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7379c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7380847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7381847ff0e1SMatthew G. Knepley   if (!color) {
7382847ff0e1SMatthew G. Knepley     DM         dm;
7383847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7384847ff0e1SMatthew G. Knepley 
7385847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7386847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7387847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7388847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7389847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7390847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7391847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7392847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7393847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7394847ff0e1SMatthew G. Knepley     } else {
7395847ff0e1SMatthew G. Knepley       MatColoring mc;
7396847ff0e1SMatthew G. Knepley 
7397847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7398847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7399847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7400847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7401847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7402847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7403847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7404847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7405847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7406847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7407847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7408847ff0e1SMatthew G. Knepley     }
7409847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7410847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7411847ff0e1SMatthew G. Knepley   }
7412847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7413847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7414847ff0e1SMatthew G. Knepley   if (J != B) {
7415847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7416847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7417847ff0e1SMatthew G. Knepley   }
7418847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7419847ff0e1SMatthew G. Knepley }
742093b34091SDebojyoti Ghosh 
7421cb9d8021SPierre Barbier de Reuille /*@
74226bc98fa9SBarry Smith     TSSetFunctionDomainError - Set a function that tests if the current state vector is valid
7423cb9d8021SPierre Barbier de Reuille 
7424cb9d8021SPierre Barbier de Reuille     Input Parameters:
74256bc98fa9SBarry Smith +    ts - the TS context
74266bc98fa9SBarry Smith -    func - function called within TSFunctionDomainError
74276bc98fa9SBarry Smith 
74286bc98fa9SBarry Smith     Calling sequence of func:
74296bc98fa9SBarry Smith $     PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject)
74306bc98fa9SBarry Smith 
74316bc98fa9SBarry Smith +   ts - the TS context
74326bc98fa9SBarry Smith .   time - the current time (of the stage)
74336bc98fa9SBarry Smith .   state - the state to check if it is valid
74346bc98fa9SBarry Smith -   reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable
7435cb9d8021SPierre Barbier de Reuille 
7436cb9d8021SPierre Barbier de Reuille     Level: intermediate
7437cb9d8021SPierre Barbier de Reuille 
74386bc98fa9SBarry Smith     Notes:
74396bc98fa9SBarry Smith       If an implicit ODE solver is being used then, in addition to providing this routine, the
74406bc98fa9SBarry Smith       user's code should call SNESSetFunctionDomainError() when domain errors occur during
74416bc98fa9SBarry Smith       function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction().
74426bc98fa9SBarry Smith       Use TSGetSNES() to obtain the SNES object
74436bc98fa9SBarry Smith 
74446bc98fa9SBarry Smith     Developer Notes:
74456bc98fa9SBarry Smith       The naming of this function is inconsistent with the SNESSetFunctionDomainError()
74466bc98fa9SBarry Smith       since one takes a function pointer and the other does not.
74476bc98fa9SBarry Smith 
74486bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES()
7449cb9d8021SPierre Barbier de Reuille @*/
7450cb9d8021SPierre Barbier de Reuille 
7451d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7452cb9d8021SPierre Barbier de Reuille {
7453cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7454cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7455cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7456cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7457cb9d8021SPierre Barbier de Reuille }
7458cb9d8021SPierre Barbier de Reuille 
7459cb9d8021SPierre Barbier de Reuille /*@
74606bc98fa9SBarry Smith     TSFunctionDomainError - Checks if the current state is valid
7461cb9d8021SPierre Barbier de Reuille 
7462cb9d8021SPierre Barbier de Reuille     Input Parameters:
74636bc98fa9SBarry Smith +    ts - the TS context
74646bc98fa9SBarry Smith .    stagetime - time of the simulation
74656bc98fa9SBarry Smith -    Y - state vector to check.
7466cb9d8021SPierre Barbier de Reuille 
7467cb9d8021SPierre Barbier de Reuille     Output Parameter:
74686bc98fa9SBarry Smith .    accept - Set to PETSC_FALSE if the current state vector is valid.
7469cb9d8021SPierre Barbier de Reuille 
7470cb9d8021SPierre Barbier de Reuille     Note:
74716bc98fa9SBarry Smith     This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError())
74726bc98fa9SBarry Smith     to check if the current state is valid.
747396a0c994SBarry Smith 
74746bc98fa9SBarry Smith     Level: developer
74756bc98fa9SBarry Smith 
74766bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError()
7477cb9d8021SPierre Barbier de Reuille @*/
7478d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7479cb9d8021SPierre Barbier de Reuille {
7480cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7481cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7482cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7483cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7484d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7485cb9d8021SPierre Barbier de Reuille   }
7486cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7487cb9d8021SPierre Barbier de Reuille }
74881ceb14c0SBarry Smith 
748993b34091SDebojyoti Ghosh /*@C
7490e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
749193b34091SDebojyoti Ghosh 
7492d083f849SBarry Smith   Collective
749393b34091SDebojyoti Ghosh 
749493b34091SDebojyoti Ghosh   Input Parameter:
749593b34091SDebojyoti Ghosh . tsin    - The input TS
749693b34091SDebojyoti Ghosh 
749793b34091SDebojyoti Ghosh   Output Parameter:
7498e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
749993b34091SDebojyoti Ghosh 
75005eca1a21SEmil Constantinescu   Notes:
75015eca1a21SEmil 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.
75025eca1a21SEmil Constantinescu 
7503928bb9adSStefano 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);
75045eca1a21SEmil Constantinescu 
75055eca1a21SEmil Constantinescu   Level: developer
750693b34091SDebojyoti Ghosh 
7507e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
750893b34091SDebojyoti Ghosh @*/
7509baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
751093b34091SDebojyoti Ghosh {
751193b34091SDebojyoti Ghosh   TS             t;
751293b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7513dc846ba4SSatish Balay   SNES           snes_start;
7514dc846ba4SSatish Balay   DM             dm;
7515dc846ba4SSatish Balay   TSType         type;
751693b34091SDebojyoti Ghosh 
751793b34091SDebojyoti Ghosh   PetscFunctionBegin;
751893b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
751993b34091SDebojyoti Ghosh   *tsout = NULL;
752093b34091SDebojyoti Ghosh 
75217a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
752293b34091SDebojyoti Ghosh 
752393b34091SDebojyoti Ghosh   /* General TS description */
752493b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
752593b34091SDebojyoti Ghosh   t->setupcalled       = 0;
752693b34091SDebojyoti Ghosh   t->ksp_its           = 0;
752793b34091SDebojyoti Ghosh   t->snes_its          = 0;
752893b34091SDebojyoti Ghosh   t->nwork             = 0;
75297d51462cSStefano Zampini   t->rhsjacobian.time  = PETSC_MIN_REAL;
753093b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
753193b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
753293b34091SDebojyoti Ghosh 
753334561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
753434561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7535d15a3a53SEmil Constantinescu 
753693b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
753793b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
753893b34091SDebojyoti Ghosh 
753993b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
754051699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
754193b34091SDebojyoti Ghosh 
7542e7069c78SShri   t->trajectory = tsin->trajectory;
7543e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7544e7069c78SShri 
7545e7069c78SShri   t->event = tsin->event;
75466b10a48eSSatish Balay   if (t->event) t->event->refct++;
7547e7069c78SShri 
754893b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
754993b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7550e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
755193b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
755293b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
755393b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
755493b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
755593b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
755693b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
755793b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
755893b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
755993b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
756093b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
756193b34091SDebojyoti Ghosh 
756293b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
756393b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
756493b34091SDebojyoti Ghosh 
756593b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
756693b34091SDebojyoti Ghosh 
756793b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
756893b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
756993b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
757093b34091SDebojyoti Ghosh 
757193b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
757293b34091SDebojyoti Ghosh 
75730d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75740d4fed19SBarry Smith     PetscInt i;
75750d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75760d4fed19SBarry Smith     for (i=0; i<10; i++) {
75770d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75780d4fed19SBarry Smith     }
75790d4fed19SBarry Smith   }
758093b34091SDebojyoti Ghosh   *tsout = t;
758193b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
758293b34091SDebojyoti Ghosh }
7583f3b1f45cSBarry Smith 
7584f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7585f3b1f45cSBarry Smith {
7586f3b1f45cSBarry Smith   PetscErrorCode ierr;
7587f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7588f3b1f45cSBarry Smith 
7589f3b1f45cSBarry Smith   PetscFunctionBegin;
7590f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7591f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7592f3b1f45cSBarry Smith }
7593f3b1f45cSBarry Smith 
7594f3b1f45cSBarry Smith /*@
7595f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7596f3b1f45cSBarry Smith 
7597d083f849SBarry Smith    Logically Collective on TS
7598f3b1f45cSBarry Smith 
7599f3b1f45cSBarry Smith     Input Parameters:
7600f3b1f45cSBarry Smith     TS - the time stepping routine
7601f3b1f45cSBarry Smith 
7602f3b1f45cSBarry Smith    Output Parameter:
7603f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7604f3b1f45cSBarry Smith 
7605f3b1f45cSBarry Smith    Options Database:
7606f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7607f3b1f45cSBarry Smith 
7608f3b1f45cSBarry Smith    Level: advanced
7609f3b1f45cSBarry Smith 
761095452b02SPatrick Sanan    Notes:
761195452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7612f3b1f45cSBarry Smith 
7613f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7614f3b1f45cSBarry Smith @*/
7615f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7616f3b1f45cSBarry Smith {
7617f3b1f45cSBarry Smith   Mat            J,B;
7618f3b1f45cSBarry Smith   PetscErrorCode ierr;
7619f3b1f45cSBarry Smith   TSRHSJacobian  func;
7620f3b1f45cSBarry Smith   void*          ctx;
7621f3b1f45cSBarry Smith 
7622f3b1f45cSBarry Smith   PetscFunctionBegin;
7623f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7624f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7625f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7626f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7627f3b1f45cSBarry Smith }
7628f3b1f45cSBarry Smith 
7629f3b1f45cSBarry Smith /*@C
7630f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7631f3b1f45cSBarry Smith 
7632d083f849SBarry Smith    Logically Collective on TS
7633f3b1f45cSBarry Smith 
7634f3b1f45cSBarry Smith     Input Parameters:
7635f3b1f45cSBarry Smith     TS - the time stepping routine
7636f3b1f45cSBarry Smith 
7637f3b1f45cSBarry Smith    Output Parameter:
7638f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7639f3b1f45cSBarry Smith 
7640f3b1f45cSBarry Smith    Options Database:
7641f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7642f3b1f45cSBarry Smith 
764395452b02SPatrick Sanan    Notes:
764495452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7645f3b1f45cSBarry Smith 
7646f3b1f45cSBarry Smith    Level: advanced
7647f3b1f45cSBarry Smith 
7648f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7649f3b1f45cSBarry Smith @*/
7650f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7651f3b1f45cSBarry Smith {
7652f3b1f45cSBarry Smith   Mat            J,B;
7653f3b1f45cSBarry Smith   PetscErrorCode ierr;
7654f3b1f45cSBarry Smith   void           *ctx;
7655f3b1f45cSBarry Smith   TSRHSJacobian  func;
7656f3b1f45cSBarry Smith 
7657f3b1f45cSBarry Smith   PetscFunctionBegin;
7658f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7659f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7660f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7661f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7662f3b1f45cSBarry Smith }
76630fe4d17eSHong Zhang 
76640fe4d17eSHong Zhang /*@
76650fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
76660fe4d17eSHong Zhang 
76670fe4d17eSHong Zhang   Logically collective
76680fe4d17eSHong Zhang 
76690fe4d17eSHong Zhang   Input Parameter:
76700fe4d17eSHong Zhang +  ts - timestepping context
76710fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
76720fe4d17eSHong Zhang 
76730fe4d17eSHong Zhang   Options Database:
76740fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
76750fe4d17eSHong Zhang 
76760fe4d17eSHong Zhang   Notes:
76770fe4d17eSHong Zhang     This is only useful for multirate methods
76780fe4d17eSHong Zhang 
76790fe4d17eSHong Zhang   Level: intermediate
76800fe4d17eSHong Zhang 
76810fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
76820fe4d17eSHong Zhang @*/
76830fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
76840fe4d17eSHong Zhang {
76850fe4d17eSHong Zhang   PetscFunctionBegin;
76860fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
76870fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
76880fe4d17eSHong Zhang   PetscFunctionReturn(0);
76890fe4d17eSHong Zhang }
76900fe4d17eSHong Zhang 
76910fe4d17eSHong Zhang /*@
76920fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
76930fe4d17eSHong Zhang 
76940fe4d17eSHong Zhang   Not collective
76950fe4d17eSHong Zhang 
76960fe4d17eSHong Zhang   Input Parameter:
76970fe4d17eSHong Zhang .  ts - timestepping context
76980fe4d17eSHong Zhang 
76990fe4d17eSHong Zhang   Output Parameter:
77000fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
77010fe4d17eSHong Zhang 
77020fe4d17eSHong Zhang   Level: intermediate
77030fe4d17eSHong Zhang 
77040fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
77050fe4d17eSHong Zhang @*/
77060fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
77070fe4d17eSHong Zhang {
77080fe4d17eSHong Zhang   PetscFunctionBegin;
77090fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77100fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
77110fe4d17eSHong Zhang   PetscFunctionReturn(0);
77120fe4d17eSHong Zhang }
7713*d60b7d5cSBarry Smith 
7714*d60b7d5cSBarry Smith /*@
7715*d60b7d5cSBarry Smith     TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix.
7716*d60b7d5cSBarry Smith 
7717*d60b7d5cSBarry Smith    Logically  Collective on ts
7718*d60b7d5cSBarry Smith 
7719*d60b7d5cSBarry Smith    Input Parameters:
7720*d60b7d5cSBarry Smith +  ts - the time-stepper
7721*d60b7d5cSBarry Smith -  str - the structure (the default is UNKNOWN_NONZERO_PATTERN)
7722*d60b7d5cSBarry Smith 
7723*d60b7d5cSBarry Smith    Level: intermediate
7724*d60b7d5cSBarry Smith 
7725*d60b7d5cSBarry Smith    Notes:
7726*d60b7d5cSBarry Smith      When the relationship between the nonzero structures is known and supplied the solution process can be much faster
7727*d60b7d5cSBarry Smith 
7728*d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure
7729*d60b7d5cSBarry Smith  @*/
7730*d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str)
7731*d60b7d5cSBarry Smith {
7732*d60b7d5cSBarry Smith   PetscFunctionBegin;
7733*d60b7d5cSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7734*d60b7d5cSBarry Smith   ts->axpy_pattern = str;
7735*d60b7d5cSBarry Smith   PetscFunctionReturn(0);
7736*d60b7d5cSBarry Smith }
7737