xref: /petsc/src/ts/interface/ts.c (revision 825ab93598c312e6a7e3a5e7ff7362a01a887e4f)
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>
6d763cef2SBarry Smith 
7d5ba7fb7SMatthew Knepley /* Logging support */
8d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
98d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
10d405a339SMatthew Knepley 
11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1249354f04SShri Abhyankar 
132d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
142d5ee99bSBarry Smith   PetscViewer   viewer;
152d5ee99bSBarry Smith   Vec           initialsolution;
162d5ee99bSBarry Smith   PetscBool     showinitial;
172d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
182d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
192d5ee99bSBarry Smith };
202d5ee99bSBarry Smith 
21fde5950dSBarry Smith /*@C
22fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
23fde5950dSBarry Smith 
24fde5950dSBarry Smith    Collective on TS
25fde5950dSBarry Smith 
26fde5950dSBarry Smith    Input Parameters:
27fde5950dSBarry Smith +  ts - TS object you wish to monitor
28fde5950dSBarry Smith .  name - the monitor type one is seeking
29fde5950dSBarry Smith .  help - message indicating what monitoring is done
30fde5950dSBarry Smith .  manual - manual page for the monitor
31fde5950dSBarry Smith .  monitor - the monitor function
32fde5950dSBarry 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
33fde5950dSBarry Smith 
34fde5950dSBarry Smith    Level: developer
35fde5950dSBarry Smith 
36fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
37fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
38fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
39fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
40fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
41fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
42fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
43fde5950dSBarry Smith @*/
44721cd6eeSBarry 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*))
45fde5950dSBarry Smith {
46fde5950dSBarry Smith   PetscErrorCode    ierr;
47fde5950dSBarry Smith   PetscViewer       viewer;
48fde5950dSBarry Smith   PetscViewerFormat format;
49fde5950dSBarry Smith   PetscBool         flg;
50fde5950dSBarry Smith 
51fde5950dSBarry Smith   PetscFunctionBegin;
52fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
53fde5950dSBarry Smith   if (flg) {
54721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
55721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
56721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
57fde5950dSBarry Smith     if (monitorsetup) {
58721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
59fde5950dSBarry Smith     }
60721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
61fde5950dSBarry Smith   }
62fde5950dSBarry Smith   PetscFunctionReturn(0);
63fde5950dSBarry Smith }
64fde5950dSBarry Smith 
65fde5950dSBarry Smith /*@C
66e875c4f7SBarry Smith    TSAdjointMonitorSensi - monitors the first lambda sensitivity
67287c2655SBarry Smith 
68287c2655SBarry Smith    Level: intermediate
69287c2655SBarry Smith 
70287c2655SBarry Smith .keywords: TS, set, monitor
71287c2655SBarry Smith 
72287c2655SBarry Smith .seealso: TSAdjointMonitorSet()
73287c2655SBarry Smith @*/
74f4c7b390SBarry Smith PetscErrorCode TSAdjointMonitorSensi(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
75287c2655SBarry Smith {
76287c2655SBarry Smith   PetscErrorCode ierr;
77287c2655SBarry Smith   PetscViewer    viewer = vf->viewer;
78287c2655SBarry Smith 
79287c2655SBarry Smith   PetscFunctionBegin;
80287c2655SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
81287c2655SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
82287c2655SBarry Smith   ierr = VecView(lambda[0],viewer);CHKERRQ(ierr);
83287c2655SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
84287c2655SBarry Smith   PetscFunctionReturn(0);
85287c2655SBarry Smith }
86287c2655SBarry Smith 
87287c2655SBarry Smith /*@C
88fde5950dSBarry Smith    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
89fde5950dSBarry Smith 
90fde5950dSBarry Smith    Collective on TS
91fde5950dSBarry Smith 
92fde5950dSBarry Smith    Input Parameters:
93fde5950dSBarry Smith +  ts - TS object you wish to monitor
94fde5950dSBarry Smith .  name - the monitor type one is seeking
95fde5950dSBarry Smith .  help - message indicating what monitoring is done
96fde5950dSBarry Smith .  manual - manual page for the monitor
97fde5950dSBarry Smith .  monitor - the monitor function
98fde5950dSBarry 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
99fde5950dSBarry Smith 
100fde5950dSBarry Smith    Level: developer
101fde5950dSBarry Smith 
102fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
103fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
104fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
105fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
106fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
107fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
108fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
109fde5950dSBarry Smith @*/
110721cd6eeSBarry Smith PetscErrorCode  TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
111fde5950dSBarry Smith {
112fde5950dSBarry Smith   PetscErrorCode    ierr;
113fde5950dSBarry Smith   PetscViewer       viewer;
114fde5950dSBarry Smith   PetscViewerFormat format;
115fde5950dSBarry Smith   PetscBool         flg;
116fde5950dSBarry Smith 
117fde5950dSBarry Smith   PetscFunctionBegin;
118fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
119fde5950dSBarry Smith   if (flg) {
120721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
121721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
122721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
123fde5950dSBarry Smith     if (monitorsetup) {
124721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
125fde5950dSBarry Smith     }
126721cd6eeSBarry Smith     ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
127fde5950dSBarry Smith   }
128fde5950dSBarry Smith   PetscFunctionReturn(0);
129fde5950dSBarry Smith }
130fde5950dSBarry Smith 
1312ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
1322ffb9264SLisandro Dalcin {
1332ffb9264SLisandro Dalcin   PetscErrorCode ierr;
1342ffb9264SLisandro Dalcin 
1352ffb9264SLisandro Dalcin   PetscFunctionBegin;
136b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
137b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
1382ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
1392ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
1402ffb9264SLisandro Dalcin   }
1412ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
1422ffb9264SLisandro Dalcin }
1432ffb9264SLisandro Dalcin 
144bdad233fSMatthew Knepley /*@
145bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
146bdad233fSMatthew Knepley 
147bdad233fSMatthew Knepley    Collective on TS
148bdad233fSMatthew Knepley 
149bdad233fSMatthew Knepley    Input Parameter:
150bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
151bdad233fSMatthew Knepley 
152bdad233fSMatthew Knepley    Options Database Keys:
153b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
154ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
155ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
156ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
157ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
158ef85077eSLisandro Dalcin .  -ts_final_time <time> - final time to compute to
1593e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
160a3cdaa26SBarry 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
161a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
162a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
163a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
164a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
165a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
166f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
167f3b1f45cSBarry 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
168ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
169847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
170bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
171de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
172de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1737cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1746934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1758b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
176de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
177de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
178de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
179de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1803e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1813e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
182fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
183e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
184110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
185110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
18653ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
18753ea634cSHong Zhang 
18891b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
189bdad233fSMatthew Knepley 
190bdad233fSMatthew Knepley    Level: beginner
191bdad233fSMatthew Knepley 
192bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
193bdad233fSMatthew Knepley 
194a313700dSBarry Smith .seealso: TSGetType()
195bdad233fSMatthew Knepley @*/
1967087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
197bdad233fSMatthew Knepley {
198bc952696SBarry Smith   PetscBool              opt,flg,tflg;
199dfbe8321SBarry Smith   PetscErrorCode         ierr;
200eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
20131748224SBarry Smith   PetscReal              time_step;
20249354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
203d1212d36SBarry Smith   char                   dir[16];
204cd11d68dSLisandro Dalcin   TSIFunction            ifun;
2056991f827SBarry Smith   const char             *defaultType;
2066991f827SBarry Smith   char                   typeName[256];
207bdad233fSMatthew Knepley 
208bdad233fSMatthew Knepley   PetscFunctionBegin;
2090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2106991f827SBarry Smith 
2116991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
212cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
213cd11d68dSLisandro Dalcin 
214cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
215cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
216cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
217cd11d68dSLisandro Dalcin   else
218cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
2196991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
2206991f827SBarry Smith   if (opt) {
2216991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
2226991f827SBarry Smith   } else {
2236991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
2246991f827SBarry Smith   }
225bdad233fSMatthew Knepley 
226bdad233fSMatthew Knepley   /* Handle generic TS options */
227ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
22819eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
2290298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
230ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
23131748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
232cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
23349354f04SShri 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);
23449354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
2350298fd71SBarry 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);
2360298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
2370298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2380298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2390298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
240bdad233fSMatthew Knepley 
241f3b1f45cSBarry 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);
242f3b1f45cSBarry 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);
24356f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
24456f85f32SBarry Smith   {
24556f85f32SBarry Smith   PetscBool set;
24656f85f32SBarry Smith   flg  = PETSC_FALSE;
24756f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
24856f85f32SBarry Smith   if (set) {
24956f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
25056f85f32SBarry Smith   }
25156f85f32SBarry Smith   }
25256f85f32SBarry Smith #endif
25356f85f32SBarry Smith 
254bdad233fSMatthew Knepley   /* Monitor options */
255cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
256fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
257fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
258e875c4f7SBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor_sensi","Monitor sensitivity in the adjoint computation","TSAdjointMonitorSensi",TSAdjointMonitorSensi,NULL);CHKERRQ(ierr);
259fde5950dSBarry Smith 
2605180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2615180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2625180491cSLisandro Dalcin 
2634f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
264b3603a34SBarry Smith   if (opt) {
2650b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2663923b477SBarry Smith     PetscInt       howoften = 1;
267b3603a34SBarry Smith 
2680298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2696ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2704f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
271bdad233fSMatthew Knepley   }
2726ba87a44SLisandro Dalcin 
2734f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
274ef20d060SBarry Smith   if (opt) {
2750b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2763923b477SBarry Smith     PetscInt       howoften = 1;
277ef20d060SBarry Smith 
2780298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2796ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2804f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
281ef20d060SBarry Smith   }
282edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2837cf37e64SBarry Smith 
2846934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2856934998bSLisandro Dalcin   if (opt) {
2866934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2876934998bSLisandro Dalcin     PetscInt       howoften = 1;
2886934998bSLisandro Dalcin 
2896934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2906ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2916934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2926934998bSLisandro Dalcin   }
2938b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2948b668821SLisandro Dalcin   if (opt) {
2958b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2968b668821SLisandro Dalcin     PetscInt       howoften = 1;
2978b668821SLisandro Dalcin 
2988b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2998b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
3008b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
3018b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
3028b668821SLisandro Dalcin   }
3038b668821SLisandro Dalcin 
304201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
305201da799SBarry Smith   if (opt) {
306201da799SBarry Smith     TSMonitorLGCtx ctx;
307201da799SBarry Smith     PetscInt       howoften = 1;
308201da799SBarry Smith 
3090298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
3106ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
311201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
312201da799SBarry Smith   }
313201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
314201da799SBarry Smith   if (opt) {
315201da799SBarry Smith     TSMonitorLGCtx ctx;
316201da799SBarry Smith     PetscInt       howoften = 1;
317201da799SBarry Smith 
3180298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
3196ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
320201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
321201da799SBarry Smith   }
3228189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
3238189c53fSBarry Smith   if (opt) {
3248189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
3258189c53fSBarry Smith     PetscInt          howoften = 1;
3268189c53fSBarry Smith 
3270298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
3286934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3298189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
3308189c53fSBarry Smith   }
331ef20d060SBarry Smith   opt  = PETSC_FALSE;
3320dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
333a7cc72afSBarry Smith   if (opt) {
33483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
33583a4ac43SBarry Smith     PetscInt         howoften = 1;
336a80ad3e0SBarry Smith 
3370298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
3380ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
33983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
340bdad233fSMatthew Knepley   }
341fb1732b5SBarry Smith   opt  = PETSC_FALSE;
3429110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
3439110b2e7SHong Zhang   if (opt) {
3449110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
3459110b2e7SHong Zhang     PetscInt         howoften = 1;
3469110b2e7SHong Zhang 
3479110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
3486934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3499110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3509110b2e7SHong Zhang   }
3519110b2e7SHong Zhang   opt  = PETSC_FALSE;
3522d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
3532d5ee99bSBarry Smith   if (opt) {
3542d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3552d5ee99bSBarry Smith     PetscReal        bounds[4];
3562d5ee99bSBarry Smith     PetscInt         n = 4;
3572d5ee99bSBarry Smith     PetscDraw        draw;
3586934998bSLisandro Dalcin     PetscDrawAxis    axis;
3592d5ee99bSBarry Smith 
3602d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3612d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3626934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3632d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3646934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3656934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3666934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3672d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3682d5ee99bSBarry Smith   }
3692d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3700dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3713a471f94SBarry Smith   if (opt) {
37283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
37383a4ac43SBarry Smith     PetscInt         howoften = 1;
3743a471f94SBarry Smith 
3750298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3760ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
37783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3783a471f94SBarry Smith   }
3790ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3800ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3810ed3bfb6SBarry Smith   if (opt) {
3820ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3830ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3840ed3bfb6SBarry Smith 
3850ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
3860ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3870ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3880ed3bfb6SBarry Smith   }
389fde5950dSBarry Smith 
390ed81e22dSJed Brown   opt  = PETSC_FALSE;
39191b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
392ed81e22dSJed Brown   if (flg) {
393ed81e22dSJed Brown     const char *ptr,*ptr2;
394ed81e22dSJed Brown     char       *filetemplate;
395ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
396ed81e22dSJed Brown     /* Do some cursory validation of the input. */
397ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
398ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
399ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
400ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
401ce94432eSBarry 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");
402ed81e22dSJed Brown       if (ptr2) break;
403ed81e22dSJed Brown     }
404ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
405ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
406ed81e22dSJed Brown   }
407bdad233fSMatthew Knepley 
408d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
409d1212d36SBarry Smith   if (flg) {
410d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
411d1212d36SBarry Smith     int                  ray = 0;
412d1212d36SBarry Smith     DMDADirection        ddir;
413d1212d36SBarry Smith     DM                   da;
414d1212d36SBarry Smith     PetscMPIInt          rank;
415d1212d36SBarry Smith 
416ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
417d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
418d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
419ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
420d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
421d1212d36SBarry Smith 
422d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
423b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
424d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
425d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
426ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
427d1212d36SBarry Smith     if (!rank) {
428d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
429d1212d36SBarry Smith     }
43051b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
431d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
432d1212d36SBarry Smith   }
43351b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
43451b4a12fSMatthew G. Knepley   if (flg) {
43551b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
43651b4a12fSMatthew G. Knepley     int                 ray = 0;
43751b4a12fSMatthew G. Knepley     DMDADirection       ddir;
43851b4a12fSMatthew G. Knepley     DM                  da;
43951b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
440d1212d36SBarry Smith 
44151b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
44251b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
44351b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
44451b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
44551b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
4461c3436cfSJed Brown 
44751b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
448b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
44951b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
45051b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
45151b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
45251b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
45351b4a12fSMatthew G. Knepley   }
454a7a1495cSBarry Smith 
455b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
456b3d3934dSBarry Smith   if (opt) {
457b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
458b3d3934dSBarry Smith 
459b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
460b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
461b3d3934dSBarry Smith   }
462b3d3934dSBarry Smith 
463847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
464847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
465847ff0e1SMatthew G. Knepley   if (flg) {
466847ff0e1SMatthew G. Knepley     DM   dm;
467847ff0e1SMatthew G. Knepley     DMTS tdm;
468847ff0e1SMatthew G. Knepley 
469847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
470847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
471847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
472847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
473847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
474847ff0e1SMatthew G. Knepley   }
475847ff0e1SMatthew G. Knepley 
476d763cef2SBarry Smith   /* Handle specific TS options */
477d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4786991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
479d763cef2SBarry Smith   }
480fbc52257SHong Zhang 
481a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
482a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
483a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
484a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
485a7bdc993SLisandro Dalcin 
48668bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4874f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
48868bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
48968bece0bSHong Zhang   if (tflg) {
49068bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
49168bece0bSHong Zhang   }
4924f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
49368bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
49468bece0bSHong Zhang   if (flg) {
49568bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
49668bece0bSHong Zhang     ts->adjoint_solve = tflg;
49768bece0bSHong Zhang   }
498d763cef2SBarry Smith 
499d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
5000633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
501fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
502d763cef2SBarry Smith 
5034f122a70SLisandro Dalcin   if (ts->trajectory) {
5044f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
505d763cef2SBarry Smith   }
50668bece0bSHong Zhang 
5074f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
5084f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
5094f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
510d763cef2SBarry Smith   PetscFunctionReturn(0);
511d763cef2SBarry Smith }
512d763cef2SBarry Smith 
513d2daff3dSHong Zhang /*@
51478fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
51578fbdcc8SBarry Smith 
51678fbdcc8SBarry Smith    Collective on TS
51778fbdcc8SBarry Smith 
51878fbdcc8SBarry Smith    Input Parameters:
51978fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
52078fbdcc8SBarry Smith 
52178fbdcc8SBarry Smith    Output Parameters;
52278fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
52378fbdcc8SBarry Smith 
52478fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
52578fbdcc8SBarry Smith 
52678fbdcc8SBarry Smith    Level: advanced
52778fbdcc8SBarry Smith 
52878fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
52978fbdcc8SBarry Smith 
53078fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
53178fbdcc8SBarry Smith @*/
53278fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
53378fbdcc8SBarry Smith {
53478fbdcc8SBarry Smith   PetscFunctionBegin;
53578fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53678fbdcc8SBarry Smith   *tr = ts->trajectory;
53778fbdcc8SBarry Smith   PetscFunctionReturn(0);
53878fbdcc8SBarry Smith }
53978fbdcc8SBarry Smith 
54078fbdcc8SBarry Smith /*@
541bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
542d2daff3dSHong Zhang 
543d2daff3dSHong Zhang    Collective on TS
544d2daff3dSHong Zhang 
545d2daff3dSHong Zhang    Input Parameters:
546bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
547bc952696SBarry Smith 
54878fbdcc8SBarry Smith    Options Database:
54978fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
55078fbdcc8SBarry Smith -  -ts_trajectory_type type
55178fbdcc8SBarry Smith 
55268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
553d2daff3dSHong Zhang 
55478fbdcc8SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and
55578fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
55678fbdcc8SBarry Smith 
557d2daff3dSHong Zhang    Level: intermediate
558d2daff3dSHong Zhang 
55978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()
560d2daff3dSHong Zhang 
561bc952696SBarry Smith .keywords: TS, set, checkpoint,
562d2daff3dSHong Zhang @*/
563bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
564d2daff3dSHong Zhang {
565bc952696SBarry Smith   PetscErrorCode ierr;
566bc952696SBarry Smith 
567d2daff3dSHong Zhang   PetscFunctionBegin;
568d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
569bc952696SBarry Smith   if (!ts->trajectory) {
570bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
571bc952696SBarry Smith   }
572d2daff3dSHong Zhang   PetscFunctionReturn(0);
573d2daff3dSHong Zhang }
574d2daff3dSHong Zhang 
575a7a1495cSBarry Smith /*@
576a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
577a7a1495cSBarry Smith       set with TSSetRHSJacobian().
578a7a1495cSBarry Smith 
579a7a1495cSBarry Smith    Collective on TS and Vec
580a7a1495cSBarry Smith 
581a7a1495cSBarry Smith    Input Parameters:
582316643e7SJed Brown +  ts - the TS context
583a7a1495cSBarry Smith .  t - current timestep
5840910c330SBarry Smith -  U - input vector
585a7a1495cSBarry Smith 
586a7a1495cSBarry Smith    Output Parameters:
587a7a1495cSBarry Smith +  A - Jacobian matrix
588a7a1495cSBarry Smith .  B - optional preconditioning matrix
589a7a1495cSBarry Smith -  flag - flag indicating matrix structure
590a7a1495cSBarry Smith 
591a7a1495cSBarry Smith    Notes:
592a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
593a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
594a7a1495cSBarry Smith 
59594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
596a7a1495cSBarry Smith    flag parameter.
597a7a1495cSBarry Smith 
598a7a1495cSBarry Smith    Level: developer
599a7a1495cSBarry Smith 
600a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
601a7a1495cSBarry Smith 
60294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
603a7a1495cSBarry Smith @*/
604d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
605a7a1495cSBarry Smith {
606dfbe8321SBarry Smith   PetscErrorCode   ierr;
607270bf2e7SJed Brown   PetscObjectState Ustate;
6086c1e1eecSBarry Smith   PetscObjectId    Uid;
60924989b8cSPeter Brune   DM               dm;
610942e3340SBarry Smith   DMTS             tsdm;
61124989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
61224989b8cSPeter Brune   void             *ctx;
61324989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
614b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
615a7a1495cSBarry Smith 
616a7a1495cSBarry Smith   PetscFunctionBegin;
6170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6180910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6190910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
62024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
621942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
62224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
6230298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
624b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
62559e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
6266c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
6276c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
6280e4ef248SJed Brown     PetscFunctionReturn(0);
629f8ede8e7SJed Brown   }
630d90be118SSean Farley 
631ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
632d90be118SSean Farley 
633e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
63494ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
63594ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
636303f9b21SStefano Zampini     if (B && A != B) {
63794ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
63894ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
639e1244c69SJed Brown     }
640e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
641e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
642e1244c69SJed Brown   }
643e1244c69SJed Brown 
64424989b8cSPeter Brune   if (rhsjacobianfunc) {
6456cd88445SBarry Smith     PetscBool missing;
64694ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
647a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
648d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
649a7a1495cSBarry Smith     PetscStackPop;
65094ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6516cd88445SBarry Smith     if (A) {
6526cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6536cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
6546cd88445SBarry Smith     }
6556cd88445SBarry Smith     if (B && B != A) {
6566cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6576cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
6586cd88445SBarry Smith     }
659ef66eb69SBarry Smith   } else {
66094ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
66194ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
662ef66eb69SBarry Smith   }
6630e4ef248SJed Brown   ts->rhsjacobian.time       = t;
6647f79407eSBarry Smith   ierr                       = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
66559e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
666a7a1495cSBarry Smith   PetscFunctionReturn(0);
667a7a1495cSBarry Smith }
668a7a1495cSBarry Smith 
669316643e7SJed Brown /*@
670d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
671d763cef2SBarry Smith 
672316643e7SJed Brown    Collective on TS and Vec
673316643e7SJed Brown 
674316643e7SJed Brown    Input Parameters:
675316643e7SJed Brown +  ts - the TS context
676316643e7SJed Brown .  t - current time
6770910c330SBarry Smith -  U - state vector
678316643e7SJed Brown 
679316643e7SJed Brown    Output Parameter:
680316643e7SJed Brown .  y - right hand side
681316643e7SJed Brown 
682316643e7SJed Brown    Note:
683316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
684316643e7SJed Brown    is used internally within the nonlinear solvers.
685316643e7SJed Brown 
686316643e7SJed Brown    Level: developer
687316643e7SJed Brown 
688316643e7SJed Brown .keywords: TS, compute
689316643e7SJed Brown 
690316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
691316643e7SJed Brown @*/
6920910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
693d763cef2SBarry Smith {
694dfbe8321SBarry Smith   PetscErrorCode ierr;
69524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
69624989b8cSPeter Brune   TSIFunction    ifunction;
69724989b8cSPeter Brune   void           *ctx;
69824989b8cSPeter Brune   DM             dm;
69924989b8cSPeter Brune 
700d763cef2SBarry Smith   PetscFunctionBegin;
7010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7020910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7030700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
70424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
70524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
7060298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
707d763cef2SBarry Smith 
708ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
709d763cef2SBarry Smith 
7100910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
71124989b8cSPeter Brune   if (rhsfunction) {
712d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
7130910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
714d763cef2SBarry Smith     PetscStackPop;
715214bc6a2SJed Brown   } else {
716214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
717b2cd27e8SJed Brown   }
71844a41b28SSean Farley 
7190910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
720d763cef2SBarry Smith   PetscFunctionReturn(0);
721d763cef2SBarry Smith }
722d763cef2SBarry Smith 
723ef20d060SBarry Smith /*@
724ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
725ef20d060SBarry Smith 
726ef20d060SBarry Smith    Collective on TS and Vec
727ef20d060SBarry Smith 
728ef20d060SBarry Smith    Input Parameters:
729ef20d060SBarry Smith +  ts - the TS context
730ef20d060SBarry Smith -  t - current time
731ef20d060SBarry Smith 
732ef20d060SBarry Smith    Output Parameter:
7330910c330SBarry Smith .  U - the solution
734ef20d060SBarry Smith 
735ef20d060SBarry Smith    Note:
736ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
737ef20d060SBarry Smith    is used internally within the nonlinear solvers.
738ef20d060SBarry Smith 
739ef20d060SBarry Smith    Level: developer
740ef20d060SBarry Smith 
741ef20d060SBarry Smith .keywords: TS, compute
742ef20d060SBarry Smith 
743abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
744ef20d060SBarry Smith @*/
7450910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
746ef20d060SBarry Smith {
747ef20d060SBarry Smith   PetscErrorCode     ierr;
748ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
749ef20d060SBarry Smith   void               *ctx;
750ef20d060SBarry Smith   DM                 dm;
751ef20d060SBarry Smith 
752ef20d060SBarry Smith   PetscFunctionBegin;
753ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7540910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
755ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
756ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
757ef20d060SBarry Smith 
758ef20d060SBarry Smith   if (solutionfunction) {
7599b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7600910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
761ef20d060SBarry Smith     PetscStackPop;
762ef20d060SBarry Smith   }
763ef20d060SBarry Smith   PetscFunctionReturn(0);
764ef20d060SBarry Smith }
7659b7cd975SBarry Smith /*@
7669b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7679b7cd975SBarry Smith 
7689b7cd975SBarry Smith    Collective on TS and Vec
7699b7cd975SBarry Smith 
7709b7cd975SBarry Smith    Input Parameters:
7719b7cd975SBarry Smith +  ts - the TS context
7729b7cd975SBarry Smith -  t - current time
7739b7cd975SBarry Smith 
7749b7cd975SBarry Smith    Output Parameter:
7759b7cd975SBarry Smith .  U - the function value
7769b7cd975SBarry Smith 
7779b7cd975SBarry Smith    Note:
7789b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7799b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7809b7cd975SBarry Smith 
7819b7cd975SBarry Smith    Level: developer
7829b7cd975SBarry Smith 
7839b7cd975SBarry Smith .keywords: TS, compute
7849b7cd975SBarry Smith 
7859b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7869b7cd975SBarry Smith @*/
7879b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7889b7cd975SBarry Smith {
7899b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7909b7cd975SBarry Smith   void               *ctx;
7919b7cd975SBarry Smith   DM                 dm;
7929b7cd975SBarry Smith 
7939b7cd975SBarry Smith   PetscFunctionBegin;
7949b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7959b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7969b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7979b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7989b7cd975SBarry Smith 
7999b7cd975SBarry Smith   if (forcing) {
8009b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
8019b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
8029b7cd975SBarry Smith     PetscStackPop;
8039b7cd975SBarry Smith   }
8049b7cd975SBarry Smith   PetscFunctionReturn(0);
8059b7cd975SBarry Smith }
806ef20d060SBarry Smith 
807214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
808214bc6a2SJed Brown {
8092dd45cf8SJed Brown   Vec            F;
810214bc6a2SJed Brown   PetscErrorCode ierr;
811214bc6a2SJed Brown 
812214bc6a2SJed Brown   PetscFunctionBegin;
8130298fd71SBarry Smith   *Frhs = NULL;
8140298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
815214bc6a2SJed Brown   if (!ts->Frhs) {
8162dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
817214bc6a2SJed Brown   }
818214bc6a2SJed Brown   *Frhs = ts->Frhs;
819214bc6a2SJed Brown   PetscFunctionReturn(0);
820214bc6a2SJed Brown }
821214bc6a2SJed Brown 
822214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
823214bc6a2SJed Brown {
824214bc6a2SJed Brown   Mat            A,B;
8252dd45cf8SJed Brown   PetscErrorCode ierr;
82641a1d4d2SBarry Smith   TSIJacobian    ijacobian;
827214bc6a2SJed Brown 
828214bc6a2SJed Brown   PetscFunctionBegin;
829c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
830c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
83141a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
832214bc6a2SJed Brown   if (Arhs) {
833214bc6a2SJed Brown     if (!ts->Arhs) {
83441a1d4d2SBarry Smith       if (ijacobian) {
835214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
83641a1d4d2SBarry Smith       } else {
83741a1d4d2SBarry Smith         ts->Arhs = A;
83841a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
83941a1d4d2SBarry Smith       }
8403565c898SBarry Smith     } else {
8413565c898SBarry Smith       PetscBool flg;
8423565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8433565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8443565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8453565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8463565c898SBarry Smith         ts->Arhs = A;
8473565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8483565c898SBarry Smith       }
849214bc6a2SJed Brown     }
850214bc6a2SJed Brown     *Arhs = ts->Arhs;
851214bc6a2SJed Brown   }
852214bc6a2SJed Brown   if (Brhs) {
853214bc6a2SJed Brown     if (!ts->Brhs) {
854bdb70873SJed Brown       if (A != B) {
85541a1d4d2SBarry Smith         if (ijacobian) {
856214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
857bdb70873SJed Brown         } else {
85841a1d4d2SBarry Smith           ts->Brhs = B;
85941a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
86041a1d4d2SBarry Smith         }
86141a1d4d2SBarry Smith       } else {
862bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
86351699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
864bdb70873SJed Brown       }
865214bc6a2SJed Brown     }
866214bc6a2SJed Brown     *Brhs = ts->Brhs;
867214bc6a2SJed Brown   }
868214bc6a2SJed Brown   PetscFunctionReturn(0);
869214bc6a2SJed Brown }
870214bc6a2SJed Brown 
871316643e7SJed Brown /*@
8720910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
873316643e7SJed Brown 
874316643e7SJed Brown    Collective on TS and Vec
875316643e7SJed Brown 
876316643e7SJed Brown    Input Parameters:
877316643e7SJed Brown +  ts - the TS context
878316643e7SJed Brown .  t - current time
8790910c330SBarry Smith .  U - state vector
8800910c330SBarry Smith .  Udot - time derivative of state vector
881214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
882316643e7SJed Brown 
883316643e7SJed Brown    Output Parameter:
884316643e7SJed Brown .  Y - right hand side
885316643e7SJed Brown 
886316643e7SJed Brown    Note:
887316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
888316643e7SJed Brown    is used internally within the nonlinear solvers.
889316643e7SJed Brown 
890316643e7SJed Brown    If the user did did not write their equations in implicit form, this
891316643e7SJed Brown    function recasts them in implicit form.
892316643e7SJed Brown 
893316643e7SJed Brown    Level: developer
894316643e7SJed Brown 
895316643e7SJed Brown .keywords: TS, compute
896316643e7SJed Brown 
897316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
898316643e7SJed Brown @*/
8990910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
900316643e7SJed Brown {
901316643e7SJed Brown   PetscErrorCode ierr;
90224989b8cSPeter Brune   TSIFunction    ifunction;
90324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
90424989b8cSPeter Brune   void           *ctx;
90524989b8cSPeter Brune   DM             dm;
906316643e7SJed Brown 
907316643e7SJed Brown   PetscFunctionBegin;
9080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9090910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9100910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
9110700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
912316643e7SJed Brown 
91324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91424989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
9150298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
91624989b8cSPeter Brune 
917ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
918d90be118SSean Farley 
9190910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
92024989b8cSPeter Brune   if (ifunction) {
921316643e7SJed Brown     PetscStackPush("TS user implicit function");
9220910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
923316643e7SJed Brown     PetscStackPop;
924214bc6a2SJed Brown   }
925214bc6a2SJed Brown   if (imex) {
92624989b8cSPeter Brune     if (!ifunction) {
9270910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
9282dd45cf8SJed Brown     }
92924989b8cSPeter Brune   } else if (rhsfunction) {
93024989b8cSPeter Brune     if (ifunction) {
931214bc6a2SJed Brown       Vec Frhs;
932214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9330910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
934214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9352dd45cf8SJed Brown     } else {
9360910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9370910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
938316643e7SJed Brown     }
9394a6899ffSJed Brown   }
9400910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
941316643e7SJed Brown   PetscFunctionReturn(0);
942316643e7SJed Brown }
943316643e7SJed Brown 
944316643e7SJed Brown /*@
945316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
946316643e7SJed Brown 
947316643e7SJed Brown    Collective on TS and Vec
948316643e7SJed Brown 
949316643e7SJed Brown    Input
950316643e7SJed Brown       Input Parameters:
951316643e7SJed Brown +  ts - the TS context
952316643e7SJed Brown .  t - current timestep
9530910c330SBarry Smith .  U - state vector
9540910c330SBarry Smith .  Udot - time derivative of state vector
955214bc6a2SJed Brown .  shift - shift to apply, see note below
956214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
957316643e7SJed Brown 
958316643e7SJed Brown    Output Parameters:
959316643e7SJed Brown +  A - Jacobian matrix
9603565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
961316643e7SJed Brown 
962316643e7SJed Brown    Notes:
9630910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
964316643e7SJed Brown 
9650910c330SBarry Smith    dF/dU + shift*dF/dUdot
966316643e7SJed Brown 
967316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
968316643e7SJed Brown    is used internally within the nonlinear solvers.
969316643e7SJed Brown 
970316643e7SJed Brown    Level: developer
971316643e7SJed Brown 
972316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
973316643e7SJed Brown 
974316643e7SJed Brown .seealso:  TSSetIJacobian()
97563495f91SJed Brown @*/
976d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
977316643e7SJed Brown {
978316643e7SJed Brown   PetscErrorCode ierr;
97924989b8cSPeter Brune   TSIJacobian    ijacobian;
98024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
98124989b8cSPeter Brune   DM             dm;
98224989b8cSPeter Brune   void           *ctx;
983316643e7SJed Brown 
984316643e7SJed Brown   PetscFunctionBegin;
9850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9860910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9870910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
988316643e7SJed Brown   PetscValidPointer(A,6);
98994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
990316643e7SJed Brown   PetscValidPointer(B,7);
99194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
99224989b8cSPeter Brune 
99324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
99424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9950298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
99624989b8cSPeter Brune 
997ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
998316643e7SJed Brown 
99994ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
100024989b8cSPeter Brune   if (ijacobian) {
10016cd88445SBarry Smith     PetscBool missing;
1002316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
1003d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
1004316643e7SJed Brown     PetscStackPop;
10056cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
10066cd88445SBarry Smith     if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
10073565c898SBarry Smith     if (B != A) {
10086cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
10096cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
10106cd88445SBarry Smith     }
10114a6899ffSJed Brown   }
1012214bc6a2SJed Brown   if (imex) {
1013b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
10144c26be97Sstefano_zampini       PetscBool assembled;
101594ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
10164c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
10174c26be97Sstefano_zampini       if (!assembled) {
10184c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10194c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10204c26be97Sstefano_zampini       }
102194ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
102294ab13aaSBarry Smith       if (A != B) {
102394ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
10244c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
10254c26be97Sstefano_zampini         if (!assembled) {
10264c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10274c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10284c26be97Sstefano_zampini         }
102994ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1030214bc6a2SJed Brown       }
1031214bc6a2SJed Brown     }
1032214bc6a2SJed Brown   } else {
1033e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1034e1244c69SJed Brown     if (rhsjacobian) {
1035214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1036d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1037e1244c69SJed Brown     }
103894ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10393565c898SBarry Smith       PetscBool flg;
1040e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1041e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10423565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10433565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10443565c898SBarry Smith       if (!flg) {
104594ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
104694ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10473565c898SBarry Smith       }
104894ab13aaSBarry Smith       if (A != B) {
104994ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
105094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1051316643e7SJed Brown       }
1052e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1053e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1054e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
105594ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
105694ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
105794ab13aaSBarry Smith         if (A != B) {
105894ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
105994ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1060214bc6a2SJed Brown         }
1061316643e7SJed Brown       }
106294ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
106394ab13aaSBarry Smith       if (A != B) {
106494ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1065316643e7SJed Brown       }
1066316643e7SJed Brown     }
1067316643e7SJed Brown   }
106894ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1069316643e7SJed Brown   PetscFunctionReturn(0);
1070316643e7SJed Brown }
1071316643e7SJed Brown 
1072d763cef2SBarry Smith /*@C
1073d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1074b5abc632SBarry Smith     where U_t = G(t,u).
1075d763cef2SBarry Smith 
10763f9fe445SBarry Smith     Logically Collective on TS
1077d763cef2SBarry Smith 
1078d763cef2SBarry Smith     Input Parameters:
1079d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10800298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1081d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1082d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10830298fd71SBarry Smith           function evaluation routine (may be NULL)
1084d763cef2SBarry Smith 
1085d763cef2SBarry Smith     Calling sequence of func:
108687828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1087d763cef2SBarry Smith 
1088d763cef2SBarry Smith +   t - current timestep
1089d763cef2SBarry Smith .   u - input vector
1090d763cef2SBarry Smith .   F - function vector
1091d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1092d763cef2SBarry Smith 
1093d763cef2SBarry Smith     Level: beginner
1094d763cef2SBarry Smith 
10952bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10962bbac0d3SBarry Smith 
1097d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1098d763cef2SBarry Smith 
1099ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1100d763cef2SBarry Smith @*/
1101089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1102d763cef2SBarry Smith {
1103089b2837SJed Brown   PetscErrorCode ierr;
1104089b2837SJed Brown   SNES           snes;
11050298fd71SBarry Smith   Vec            ralloc = NULL;
110624989b8cSPeter Brune   DM             dm;
1107d763cef2SBarry Smith 
1108089b2837SJed Brown   PetscFunctionBegin;
11090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1110ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
111124989b8cSPeter Brune 
111224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
111324989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1114089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1115e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1116e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1117e856ceecSJed Brown     r = ralloc;
1118e856ceecSJed Brown   }
1119089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1120e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1121d763cef2SBarry Smith   PetscFunctionReturn(0);
1122d763cef2SBarry Smith }
1123d763cef2SBarry Smith 
1124ef20d060SBarry Smith /*@C
1125abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1126ef20d060SBarry Smith 
1127ef20d060SBarry Smith     Logically Collective on TS
1128ef20d060SBarry Smith 
1129ef20d060SBarry Smith     Input Parameters:
1130ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1131ef20d060SBarry Smith .   f - routine for evaluating the solution
1132ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11330298fd71SBarry Smith           function evaluation routine (may be NULL)
1134ef20d060SBarry Smith 
1135ef20d060SBarry Smith     Calling sequence of func:
1136ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1137ef20d060SBarry Smith 
1138ef20d060SBarry Smith +   t - current timestep
1139ef20d060SBarry Smith .   u - output vector
1140ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1141ef20d060SBarry Smith 
11420ed3bfb6SBarry Smith     Options Database:
11430ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11440ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11450ed3bfb6SBarry Smith 
1146abd5a294SJed Brown     Notes:
1147abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1148abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1149abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1150abd5a294SJed Brown 
11514f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1152abd5a294SJed Brown 
1153ef20d060SBarry Smith     Level: beginner
1154ef20d060SBarry Smith 
1155ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1156ef20d060SBarry Smith 
11570ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1158ef20d060SBarry Smith @*/
1159ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1160ef20d060SBarry Smith {
1161ef20d060SBarry Smith   PetscErrorCode ierr;
1162ef20d060SBarry Smith   DM             dm;
1163ef20d060SBarry Smith 
1164ef20d060SBarry Smith   PetscFunctionBegin;
1165ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1166ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1167ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1168ef20d060SBarry Smith   PetscFunctionReturn(0);
1169ef20d060SBarry Smith }
1170ef20d060SBarry Smith 
11719b7cd975SBarry Smith /*@C
11729b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11739b7cd975SBarry Smith 
11749b7cd975SBarry Smith     Logically Collective on TS
11759b7cd975SBarry Smith 
11769b7cd975SBarry Smith     Input Parameters:
11779b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1178e162b725SBarry Smith .   func - routine for evaluating the forcing function
11799b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11800298fd71SBarry Smith           function evaluation routine (may be NULL)
11819b7cd975SBarry Smith 
11829b7cd975SBarry Smith     Calling sequence of func:
1183e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11849b7cd975SBarry Smith 
11859b7cd975SBarry Smith +   t - current timestep
1186e162b725SBarry Smith .   f - output vector
11879b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11889b7cd975SBarry Smith 
11899b7cd975SBarry Smith     Notes:
11909b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1191e162b725SBarry 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
1192e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1193e162b725SBarry Smith 
1194e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1195e162b725SBarry Smith 
1196e162b725SBarry 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
1197e162b725SBarry Smith     parameters can be passed in the ctx variable.
11989b7cd975SBarry Smith 
11999b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
12009b7cd975SBarry Smith 
12019b7cd975SBarry Smith     Level: beginner
12029b7cd975SBarry Smith 
12039b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
12049b7cd975SBarry Smith 
12059b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
12069b7cd975SBarry Smith @*/
1207e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
12089b7cd975SBarry Smith {
12099b7cd975SBarry Smith   PetscErrorCode ierr;
12109b7cd975SBarry Smith   DM             dm;
12119b7cd975SBarry Smith 
12129b7cd975SBarry Smith   PetscFunctionBegin;
12139b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12149b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1215e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
12169b7cd975SBarry Smith   PetscFunctionReturn(0);
12179b7cd975SBarry Smith }
12189b7cd975SBarry Smith 
1219d763cef2SBarry Smith /*@C
1220f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1221b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1222d763cef2SBarry Smith 
12233f9fe445SBarry Smith    Logically Collective on TS
1224d763cef2SBarry Smith 
1225d763cef2SBarry Smith    Input Parameters:
1226d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1227e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1228e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1229d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1230d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12310298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1232d763cef2SBarry Smith 
1233f7ab8db6SBarry Smith    Calling sequence of f:
1234ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1235d763cef2SBarry Smith 
1236d763cef2SBarry Smith +  t - current timestep
1237d763cef2SBarry Smith .  u - input vector
1238e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1239e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1240d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1241d763cef2SBarry Smith 
12426cd88445SBarry Smith    Notes:
12436cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12446cd88445SBarry Smith 
12456cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1246ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1247d763cef2SBarry Smith 
1248d763cef2SBarry Smith    Level: beginner
1249d763cef2SBarry Smith 
1250d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1251d763cef2SBarry Smith 
1252ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1253d763cef2SBarry Smith 
1254d763cef2SBarry Smith @*/
1255e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1256d763cef2SBarry Smith {
1257277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1258089b2837SJed Brown   SNES           snes;
125924989b8cSPeter Brune   DM             dm;
126024989b8cSPeter Brune   TSIJacobian    ijacobian;
1261277b19d0SLisandro Dalcin 
1262d763cef2SBarry Smith   PetscFunctionBegin;
12630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1264e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1265e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1266e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1267e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1268d763cef2SBarry Smith 
126924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
127024989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1271e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1272e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1273e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1274e1244c69SJed Brown   }
12750298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1276089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12775f659677SPeter Brune   if (!ijacobian) {
1278e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12790e4ef248SJed Brown   }
1280e5d3d808SBarry Smith   if (Amat) {
1281e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12820e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1283e5d3d808SBarry Smith     ts->Arhs = Amat;
12840e4ef248SJed Brown   }
1285e5d3d808SBarry Smith   if (Pmat) {
1286e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12870e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1288e5d3d808SBarry Smith     ts->Brhs = Pmat;
12890e4ef248SJed Brown   }
1290d763cef2SBarry Smith   PetscFunctionReturn(0);
1291d763cef2SBarry Smith }
1292d763cef2SBarry Smith 
1293316643e7SJed Brown /*@C
1294b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1295316643e7SJed Brown 
12963f9fe445SBarry Smith    Logically Collective on TS
1297316643e7SJed Brown 
1298316643e7SJed Brown    Input Parameters:
1299316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
13000298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1301316643e7SJed Brown .  f   - the function evaluation routine
13020298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1303316643e7SJed Brown 
1304316643e7SJed Brown    Calling sequence of f:
1305316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1306316643e7SJed Brown 
1307316643e7SJed Brown +  t   - time at step/stage being solved
1308316643e7SJed Brown .  u   - state vector
1309316643e7SJed Brown .  u_t - time derivative of state vector
1310316643e7SJed Brown .  F   - function vector
1311316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1312316643e7SJed Brown 
1313316643e7SJed Brown    Important:
13142bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1315316643e7SJed Brown 
1316316643e7SJed Brown    Level: beginner
1317316643e7SJed Brown 
1318316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1319316643e7SJed Brown 
1320d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1321316643e7SJed Brown @*/
132251699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1323316643e7SJed Brown {
1324089b2837SJed Brown   PetscErrorCode ierr;
1325089b2837SJed Brown   SNES           snes;
132651699248SLisandro Dalcin   Vec            ralloc = NULL;
132724989b8cSPeter Brune   DM             dm;
1328316643e7SJed Brown 
1329316643e7SJed Brown   PetscFunctionBegin;
13300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
133151699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
133224989b8cSPeter Brune 
133324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
133424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
133524989b8cSPeter Brune 
1336089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
133751699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
133851699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
133951699248SLisandro Dalcin     r  = ralloc;
1340e856ceecSJed Brown   }
134151699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
134251699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1343089b2837SJed Brown   PetscFunctionReturn(0);
1344089b2837SJed Brown }
1345089b2837SJed Brown 
1346089b2837SJed Brown /*@C
1347089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1348089b2837SJed Brown 
1349089b2837SJed Brown    Not Collective
1350089b2837SJed Brown 
1351089b2837SJed Brown    Input Parameter:
1352089b2837SJed Brown .  ts - the TS context
1353089b2837SJed Brown 
1354089b2837SJed Brown    Output Parameter:
13550298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13560298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13570298fd71SBarry Smith -  ctx - the function context (or NULL)
1358089b2837SJed Brown 
1359089b2837SJed Brown    Level: advanced
1360089b2837SJed Brown 
1361089b2837SJed Brown .keywords: TS, nonlinear, get, function
1362089b2837SJed Brown 
1363089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1364089b2837SJed Brown @*/
1365089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1366089b2837SJed Brown {
1367089b2837SJed Brown   PetscErrorCode ierr;
1368089b2837SJed Brown   SNES           snes;
136924989b8cSPeter Brune   DM             dm;
1370089b2837SJed Brown 
1371089b2837SJed Brown   PetscFunctionBegin;
1372089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1373089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13740298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
137524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1377089b2837SJed Brown   PetscFunctionReturn(0);
1378089b2837SJed Brown }
1379089b2837SJed Brown 
1380089b2837SJed Brown /*@C
1381089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1382089b2837SJed Brown 
1383089b2837SJed Brown    Not Collective
1384089b2837SJed Brown 
1385089b2837SJed Brown    Input Parameter:
1386089b2837SJed Brown .  ts - the TS context
1387089b2837SJed Brown 
1388089b2837SJed Brown    Output Parameter:
13890298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13900298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13910298fd71SBarry Smith -  ctx - the function context (or NULL)
1392089b2837SJed Brown 
1393089b2837SJed Brown    Level: advanced
1394089b2837SJed Brown 
1395089b2837SJed Brown .keywords: TS, nonlinear, get, function
1396089b2837SJed Brown 
13972bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1398089b2837SJed Brown @*/
1399089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1400089b2837SJed Brown {
1401089b2837SJed Brown   PetscErrorCode ierr;
1402089b2837SJed Brown   SNES           snes;
140324989b8cSPeter Brune   DM             dm;
1404089b2837SJed Brown 
1405089b2837SJed Brown   PetscFunctionBegin;
1406089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1407089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14080298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
140924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
141024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1411316643e7SJed Brown   PetscFunctionReturn(0);
1412316643e7SJed Brown }
1413316643e7SJed Brown 
1414316643e7SJed Brown /*@C
1415a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1416ae8867d6SBarry Smith         provided with TSSetIFunction().
1417316643e7SJed Brown 
14183f9fe445SBarry Smith    Logically Collective on TS
1419316643e7SJed Brown 
1420316643e7SJed Brown    Input Parameters:
1421316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1422e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1423e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1424316643e7SJed Brown .  f   - the Jacobian evaluation routine
14250298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1426316643e7SJed Brown 
1427316643e7SJed Brown    Calling sequence of f:
1428ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1429316643e7SJed Brown 
1430316643e7SJed Brown +  t    - time at step/stage being solved
14311b4a444bSJed Brown .  U    - state vector
14321b4a444bSJed Brown .  U_t  - time derivative of state vector
1433316643e7SJed Brown .  a    - shift
1434e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1435e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1436316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1437316643e7SJed Brown 
1438316643e7SJed Brown    Notes:
1439e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1440316643e7SJed Brown 
1441895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1442895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1443895c21f2SBarry Smith 
1444a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1445b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1446a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1447a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1448a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1449a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1450a4f0a591SBarry Smith 
14516cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14526cd88445SBarry Smith 
14536cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1454ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1455ca5f011dSBarry Smith 
1456316643e7SJed Brown    Level: beginner
1457316643e7SJed Brown 
1458316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1459316643e7SJed Brown 
1460ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1461316643e7SJed Brown 
1462316643e7SJed Brown @*/
1463e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1464316643e7SJed Brown {
1465316643e7SJed Brown   PetscErrorCode ierr;
1466089b2837SJed Brown   SNES           snes;
146724989b8cSPeter Brune   DM             dm;
1468316643e7SJed Brown 
1469316643e7SJed Brown   PetscFunctionBegin;
14700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1471e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1472e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1473e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1474e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
147524989b8cSPeter Brune 
147624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
147724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
147824989b8cSPeter Brune 
1479089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1480e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1481316643e7SJed Brown   PetscFunctionReturn(0);
1482316643e7SJed Brown }
1483316643e7SJed Brown 
1484e1244c69SJed Brown /*@
1485e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1486e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1487e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1488e1244c69SJed Brown    not been changed by the TS.
1489e1244c69SJed Brown 
1490e1244c69SJed Brown    Logically Collective
1491e1244c69SJed Brown 
1492e1244c69SJed Brown    Input Arguments:
1493e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1494e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1495e1244c69SJed Brown 
1496e1244c69SJed Brown    Level: intermediate
1497e1244c69SJed Brown 
1498e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1499e1244c69SJed Brown @*/
1500e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1501e1244c69SJed Brown {
1502e1244c69SJed Brown   PetscFunctionBegin;
1503e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1504e1244c69SJed Brown   PetscFunctionReturn(0);
1505e1244c69SJed Brown }
1506e1244c69SJed Brown 
1507efe9872eSLisandro Dalcin /*@C
1508efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin    Logically Collective on TS
1511efe9872eSLisandro Dalcin 
1512efe9872eSLisandro Dalcin    Input Parameters:
1513efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1514efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1515efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1516efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1517efe9872eSLisandro Dalcin 
1518efe9872eSLisandro Dalcin    Calling sequence of fun:
1519efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1522efe9872eSLisandro Dalcin .  U    - state vector
1523efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1524efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1525efe9872eSLisandro Dalcin .  F    - function vector
1526efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1527efe9872eSLisandro Dalcin 
1528efe9872eSLisandro Dalcin    Level: beginner
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1533efe9872eSLisandro Dalcin @*/
1534efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1535efe9872eSLisandro Dalcin {
1536efe9872eSLisandro Dalcin   DM             dm;
1537efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin   PetscFunctionBegin;
1540efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1541efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1542efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1543efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1544efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1545efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1546efe9872eSLisandro Dalcin }
1547efe9872eSLisandro Dalcin 
1548efe9872eSLisandro Dalcin /*@C
1549efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin   Not Collective
1552efe9872eSLisandro Dalcin 
1553efe9872eSLisandro Dalcin   Input Parameter:
1554efe9872eSLisandro Dalcin . ts - the TS context
1555efe9872eSLisandro Dalcin 
1556efe9872eSLisandro Dalcin   Output Parameter:
1557efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1558efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1559efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1560efe9872eSLisandro Dalcin 
1561efe9872eSLisandro Dalcin   Level: advanced
1562efe9872eSLisandro Dalcin 
1563efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1564efe9872eSLisandro Dalcin 
1565efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1566efe9872eSLisandro Dalcin @*/
1567efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1568efe9872eSLisandro Dalcin {
1569efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1570efe9872eSLisandro Dalcin   SNES           snes;
1571efe9872eSLisandro Dalcin   DM             dm;
1572efe9872eSLisandro Dalcin 
1573efe9872eSLisandro Dalcin   PetscFunctionBegin;
1574efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1575efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1576efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1577efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1578efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1579efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1580efe9872eSLisandro Dalcin }
1581efe9872eSLisandro Dalcin 
1582efe9872eSLisandro Dalcin /*@C
1583bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1584efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1585efe9872eSLisandro Dalcin 
1586efe9872eSLisandro Dalcin    Logically Collective on TS
1587efe9872eSLisandro Dalcin 
1588efe9872eSLisandro Dalcin    Input Parameters:
1589efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1590efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1591efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1592efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1593efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1594efe9872eSLisandro Dalcin 
1595efe9872eSLisandro Dalcin    Calling sequence of jac:
1596bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1599efe9872eSLisandro Dalcin .  U    - state vector
1600efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1601efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1602efe9872eSLisandro Dalcin .  v    - shift for U_t
1603efe9872eSLisandro Dalcin .  a    - shift for U_tt
1604efe9872eSLisandro 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
1605efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1606efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1607efe9872eSLisandro Dalcin 
1608efe9872eSLisandro Dalcin    Notes:
1609efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1610efe9872eSLisandro Dalcin 
1611efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1612efe9872eSLisandro 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.
1613efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1614bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1615efe9872eSLisandro Dalcin 
1616efe9872eSLisandro Dalcin    Level: beginner
1617efe9872eSLisandro Dalcin 
1618efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1619efe9872eSLisandro Dalcin 
1620efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1621efe9872eSLisandro Dalcin @*/
1622efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1623efe9872eSLisandro Dalcin {
1624efe9872eSLisandro Dalcin   DM             dm;
1625efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1626efe9872eSLisandro Dalcin 
1627efe9872eSLisandro Dalcin   PetscFunctionBegin;
1628efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1629efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1630efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1631efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1632efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1633efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1634efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1635efe9872eSLisandro Dalcin }
1636efe9872eSLisandro Dalcin 
1637efe9872eSLisandro Dalcin /*@C
1638efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1639efe9872eSLisandro Dalcin 
1640efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1641efe9872eSLisandro Dalcin 
1642efe9872eSLisandro Dalcin   Input Parameter:
1643efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1644efe9872eSLisandro Dalcin 
1645efe9872eSLisandro Dalcin   Output Parameters:
1646efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1647efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1648efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1649efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1650efe9872eSLisandro Dalcin 
1651efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   Level: advanced
1654efe9872eSLisandro Dalcin 
165580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1656efe9872eSLisandro Dalcin 
1657efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1658efe9872eSLisandro Dalcin @*/
1659efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1660efe9872eSLisandro Dalcin {
1661efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1662efe9872eSLisandro Dalcin   SNES           snes;
1663efe9872eSLisandro Dalcin   DM             dm;
1664efe9872eSLisandro Dalcin 
1665efe9872eSLisandro Dalcin   PetscFunctionBegin;
1666efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1667efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1668efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1669efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1670efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1671efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1672efe9872eSLisandro Dalcin }
1673efe9872eSLisandro Dalcin 
1674efe9872eSLisandro Dalcin /*@
1675efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1676efe9872eSLisandro Dalcin 
1677efe9872eSLisandro Dalcin   Collective on TS and Vec
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   Input Parameters:
1680efe9872eSLisandro Dalcin + ts - the TS context
1681efe9872eSLisandro Dalcin . t - current time
1682efe9872eSLisandro Dalcin . U - state vector
1683efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1684efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1685efe9872eSLisandro Dalcin 
1686efe9872eSLisandro Dalcin   Output Parameter:
1687efe9872eSLisandro Dalcin . F - the residual vector
1688efe9872eSLisandro Dalcin 
1689efe9872eSLisandro Dalcin   Note:
1690efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1691efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1692efe9872eSLisandro Dalcin 
1693efe9872eSLisandro Dalcin   Level: developer
1694efe9872eSLisandro Dalcin 
1695efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1696efe9872eSLisandro Dalcin 
1697efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1698efe9872eSLisandro Dalcin @*/
1699efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1700efe9872eSLisandro Dalcin {
1701efe9872eSLisandro Dalcin   DM             dm;
1702efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1703efe9872eSLisandro Dalcin   void           *ctx;
1704efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1705efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   PetscFunctionBegin;
1708efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1709efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1710efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1711efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1712efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1713efe9872eSLisandro Dalcin 
1714efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1715efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1716efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   if (!I2Function) {
1719efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1720efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1721efe9872eSLisandro Dalcin   }
1722efe9872eSLisandro Dalcin 
1723efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1724efe9872eSLisandro Dalcin 
1725efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1726efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1727efe9872eSLisandro Dalcin   PetscStackPop;
1728efe9872eSLisandro Dalcin 
1729efe9872eSLisandro Dalcin   if (rhsfunction) {
1730efe9872eSLisandro Dalcin     Vec Frhs;
1731efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1732efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1733efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1734efe9872eSLisandro Dalcin   }
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1737efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1738efe9872eSLisandro Dalcin }
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin /*@
1741efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1742efe9872eSLisandro Dalcin 
1743efe9872eSLisandro Dalcin   Collective on TS and Vec
1744efe9872eSLisandro Dalcin 
1745efe9872eSLisandro Dalcin   Input Parameters:
1746efe9872eSLisandro Dalcin + ts - the TS context
1747efe9872eSLisandro Dalcin . t - current timestep
1748efe9872eSLisandro Dalcin . U - state vector
1749efe9872eSLisandro Dalcin . V - time derivative of state vector
1750efe9872eSLisandro Dalcin . A - second time derivative of state vector
1751efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1752efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1753efe9872eSLisandro Dalcin 
1754efe9872eSLisandro Dalcin   Output Parameters:
1755efe9872eSLisandro Dalcin + J - Jacobian matrix
1756efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1757efe9872eSLisandro Dalcin 
1758efe9872eSLisandro Dalcin   Notes:
1759efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1760efe9872eSLisandro Dalcin 
1761efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1762efe9872eSLisandro Dalcin 
1763efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1764efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1765efe9872eSLisandro Dalcin 
1766efe9872eSLisandro Dalcin   Level: developer
1767efe9872eSLisandro Dalcin 
1768efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1769efe9872eSLisandro Dalcin 
1770efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1771efe9872eSLisandro Dalcin @*/
1772efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1773efe9872eSLisandro Dalcin {
1774efe9872eSLisandro Dalcin   DM             dm;
1775efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1776efe9872eSLisandro Dalcin   void           *ctx;
1777efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1778efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1779efe9872eSLisandro Dalcin 
1780efe9872eSLisandro Dalcin   PetscFunctionBegin;
1781efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1782efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1783efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1784efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1785efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1786efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1787efe9872eSLisandro Dalcin 
1788efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1789efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1790efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1791efe9872eSLisandro Dalcin 
1792efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1793efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1794efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1795efe9872eSLisandro Dalcin   }
1796efe9872eSLisandro Dalcin 
1797efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1798efe9872eSLisandro Dalcin 
1799efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1800efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1801efe9872eSLisandro Dalcin   PetscStackPop;
1802efe9872eSLisandro Dalcin 
1803efe9872eSLisandro Dalcin   if (rhsjacobian) {
1804efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1805efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1806efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1807efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1808efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1809efe9872eSLisandro Dalcin   }
1810efe9872eSLisandro Dalcin 
1811efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1812efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1813efe9872eSLisandro Dalcin }
1814efe9872eSLisandro Dalcin 
1815efe9872eSLisandro Dalcin /*@
1816efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1817efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1818efe9872eSLisandro Dalcin 
1819efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1820efe9872eSLisandro Dalcin 
1821efe9872eSLisandro Dalcin    Input Parameters:
1822efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1823efe9872eSLisandro Dalcin .  u - the solution vector
1824efe9872eSLisandro Dalcin -  v - the time derivative vector
1825efe9872eSLisandro Dalcin 
1826efe9872eSLisandro Dalcin    Level: beginner
1827efe9872eSLisandro Dalcin 
1828efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1829efe9872eSLisandro Dalcin @*/
1830efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1831efe9872eSLisandro Dalcin {
1832efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1833efe9872eSLisandro Dalcin 
1834efe9872eSLisandro Dalcin   PetscFunctionBegin;
1835efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1836efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1837efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1838efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1839efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1840efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1841efe9872eSLisandro Dalcin   ts->vec_dot = v;
1842efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1843efe9872eSLisandro Dalcin }
1844efe9872eSLisandro Dalcin 
1845efe9872eSLisandro Dalcin /*@
1846efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1847efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1848efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1849efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1850efe9872eSLisandro Dalcin 
1851efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1852efe9872eSLisandro Dalcin 
1853efe9872eSLisandro Dalcin    Input Parameter:
1854efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1855efe9872eSLisandro Dalcin 
1856efe9872eSLisandro Dalcin    Output Parameter:
1857efe9872eSLisandro Dalcin +  u - the vector containing the solution
1858efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1859efe9872eSLisandro Dalcin 
1860efe9872eSLisandro Dalcin    Level: intermediate
1861efe9872eSLisandro Dalcin 
1862efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1863efe9872eSLisandro Dalcin 
1864efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1865efe9872eSLisandro Dalcin @*/
1866efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1867efe9872eSLisandro Dalcin {
1868efe9872eSLisandro Dalcin   PetscFunctionBegin;
1869efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1870efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1871efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1872efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1873efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1874efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1875efe9872eSLisandro Dalcin }
1876efe9872eSLisandro Dalcin 
187755849f57SBarry Smith /*@C
187855849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
187955849f57SBarry Smith 
188055849f57SBarry Smith   Collective on PetscViewer
188155849f57SBarry Smith 
188255849f57SBarry Smith   Input Parameters:
188355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
188455849f57SBarry Smith            some related function before a call to TSLoad().
188555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
188655849f57SBarry Smith 
188755849f57SBarry Smith    Level: intermediate
188855849f57SBarry Smith 
188955849f57SBarry Smith   Notes:
189055849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
189155849f57SBarry Smith 
189255849f57SBarry Smith   Notes for advanced users:
189355849f57SBarry Smith   Most users should not need to know the details of the binary storage
189455849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
189555849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
189655849f57SBarry Smith   format is
189755849f57SBarry Smith .vb
189855849f57SBarry Smith      has not yet been determined
189955849f57SBarry Smith .ve
190055849f57SBarry Smith 
190155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
190255849f57SBarry Smith @*/
1903f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
190455849f57SBarry Smith {
190555849f57SBarry Smith   PetscErrorCode ierr;
190655849f57SBarry Smith   PetscBool      isbinary;
190755849f57SBarry Smith   PetscInt       classid;
190855849f57SBarry Smith   char           type[256];
19092d53ad75SBarry Smith   DMTS           sdm;
1910ad6bc421SBarry Smith   DM             dm;
191155849f57SBarry Smith 
191255849f57SBarry Smith   PetscFunctionBegin;
1913f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
191455849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
191555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
191655849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
191755849f57SBarry Smith 
1918060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1919ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1920060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1921f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1922f2c2a1b9SBarry Smith   if (ts->ops->load) {
1923f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1924f2c2a1b9SBarry Smith   }
1925ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1926ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1927ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1928f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1929f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19302d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19312d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
193255849f57SBarry Smith   PetscFunctionReturn(0);
193355849f57SBarry Smith }
193455849f57SBarry Smith 
19359804daf3SBarry Smith #include <petscdraw.h>
1936e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1937e04113cfSBarry Smith #include <petscviewersaws.h>
1938f05ece33SBarry Smith #endif
19397e2c5f70SBarry Smith /*@C
1940d763cef2SBarry Smith     TSView - Prints the TS data structure.
1941d763cef2SBarry Smith 
19424c49b128SBarry Smith     Collective on TS
1943d763cef2SBarry Smith 
1944d763cef2SBarry Smith     Input Parameters:
1945d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1946d763cef2SBarry Smith -   viewer - visualization context
1947d763cef2SBarry Smith 
1948d763cef2SBarry Smith     Options Database Key:
1949d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1950d763cef2SBarry Smith 
1951d763cef2SBarry Smith     Notes:
1952d763cef2SBarry Smith     The available visualization contexts include
1953b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1954b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1955d763cef2SBarry Smith          output where only the first processor opens
1956d763cef2SBarry Smith          the file.  All other processors send their
1957d763cef2SBarry Smith          data to the first processor to print.
1958d763cef2SBarry Smith 
1959d763cef2SBarry Smith     The user can open an alternative visualization context with
1960b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1961d763cef2SBarry Smith 
1962d763cef2SBarry Smith     Level: beginner
1963d763cef2SBarry Smith 
1964d763cef2SBarry Smith .keywords: TS, timestep, view
1965d763cef2SBarry Smith 
1966b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1967d763cef2SBarry Smith @*/
19687087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1969d763cef2SBarry Smith {
1970dfbe8321SBarry Smith   PetscErrorCode ierr;
197119fd82e9SBarry Smith   TSType         type;
19722b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19732d53ad75SBarry Smith   DMTS           sdm;
1974e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1975536b137fSBarry Smith   PetscBool      issaws;
1976f05ece33SBarry Smith #endif
1977d763cef2SBarry Smith 
1978d763cef2SBarry Smith   PetscFunctionBegin;
19790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19803050cee2SBarry Smith   if (!viewer) {
1981ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19823050cee2SBarry Smith   }
19830700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1984c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1985fd16b177SBarry Smith 
1986251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1987251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
198855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19892b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1990e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1991536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1992f05ece33SBarry Smith #endif
199332077d6dSBarry Smith   if (iascii) {
1994dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1995efd4aadfSBarry Smith     if (ts->ops->view) {
1996efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1997efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1998efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1999efd4aadfSBarry Smith     }
2000ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
200177431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2002ef85077eSLisandro Dalcin     }
2003ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20047c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2005ef85077eSLisandro Dalcin     }
2006efd4aadfSBarry Smith     if (ts->usessnes) {
2007efd4aadfSBarry Smith       PetscBool lin;
2008d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20095ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2010d763cef2SBarry Smith       }
20115ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
2012efd4aadfSBarry Smith       ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr);
2013efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2014efd4aadfSBarry Smith     }
2015193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2016a0af407cSBarry Smith     if (ts->vrtol) {
2017a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2018a0af407cSBarry Smith     } else {
2019a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2020a0af407cSBarry Smith     }
2021a0af407cSBarry Smith     if (ts->vatol) {
2022a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2023a0af407cSBarry Smith     } else {
2024a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2025a0af407cSBarry Smith     }
2026*825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2027efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2028*825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2029*825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
2030*825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2031*825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
2032*825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2033*825ab935SBarry Smith     }
20342d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20352d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20360f5bd95cSBarry Smith   } else if (isstring) {
2037a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
2038b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
203955849f57SBarry Smith   } else if (isbinary) {
204055849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
204155849f57SBarry Smith     MPI_Comm    comm;
204255849f57SBarry Smith     PetscMPIInt rank;
204355849f57SBarry Smith     char        type[256];
204455849f57SBarry Smith 
204555849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
204655849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
204755849f57SBarry Smith     if (!rank) {
204855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
204955849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
205055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
205155849f57SBarry Smith     }
205255849f57SBarry Smith     if (ts->ops->view) {
205355849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
205455849f57SBarry Smith     }
2055efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2056f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2057f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
20582d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20592d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20602b0a91c0SBarry Smith   } else if (isdraw) {
20612b0a91c0SBarry Smith     PetscDraw draw;
20622b0a91c0SBarry Smith     char      str[36];
206389fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20642b0a91c0SBarry Smith 
20652b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20662b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20672b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20682b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
206951fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
207089fd9fafSBarry Smith     bottom = y - h;
20712b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20722b0a91c0SBarry Smith     if (ts->ops->view) {
20732b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20742b0a91c0SBarry Smith     }
2075efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2076efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20772b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2078e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2079536b137fSBarry Smith   } else if (issaws) {
2080d45a07a7SBarry Smith     PetscMPIInt rank;
20812657e9d9SBarry Smith     const char  *name;
20822657e9d9SBarry Smith 
20832657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2084d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2085d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2086d45a07a7SBarry Smith       char       dir[1024];
2087d45a07a7SBarry Smith 
2088e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2089a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20902657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20912657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20922657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2093d763cef2SBarry Smith     }
20940acecf5bSBarry Smith     if (ts->ops->view) {
20950acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20960acecf5bSBarry Smith     }
2097f05ece33SBarry Smith #endif
2098f05ece33SBarry Smith   }
2099f05ece33SBarry Smith 
2100b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2101251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2102b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2103d763cef2SBarry Smith   PetscFunctionReturn(0);
2104d763cef2SBarry Smith }
2105d763cef2SBarry Smith 
2106b07ff414SBarry Smith /*@
2107d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2108d763cef2SBarry Smith    the timesteppers.
2109d763cef2SBarry Smith 
21103f9fe445SBarry Smith    Logically Collective on TS
2111d763cef2SBarry Smith 
2112d763cef2SBarry Smith    Input Parameters:
2113d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2114d763cef2SBarry Smith -  usrP - optional user context
2115d763cef2SBarry Smith 
2116daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2117daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2118daf670e6SBarry Smith 
2119d763cef2SBarry Smith    Level: intermediate
2120d763cef2SBarry Smith 
2121d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2122d763cef2SBarry Smith 
2123d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2124d763cef2SBarry Smith @*/
21257087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2126d763cef2SBarry Smith {
2127d763cef2SBarry Smith   PetscFunctionBegin;
21280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2129d763cef2SBarry Smith   ts->user = usrP;
2130d763cef2SBarry Smith   PetscFunctionReturn(0);
2131d763cef2SBarry Smith }
2132d763cef2SBarry Smith 
2133b07ff414SBarry Smith /*@
2134d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2135d763cef2SBarry Smith     timestepper.
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith     Not Collective
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith     Input Parameter:
2140d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith     Output Parameter:
2143d763cef2SBarry Smith .   usrP - user context
2144d763cef2SBarry Smith 
2145daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2146daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2147daf670e6SBarry Smith 
2148d763cef2SBarry Smith     Level: intermediate
2149d763cef2SBarry Smith 
2150d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2151d763cef2SBarry Smith 
2152d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2153d763cef2SBarry Smith @*/
2154e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2155d763cef2SBarry Smith {
2156d763cef2SBarry Smith   PetscFunctionBegin;
21570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2158e71120c6SJed Brown   *(void**)usrP = ts->user;
2159d763cef2SBarry Smith   PetscFunctionReturn(0);
2160d763cef2SBarry Smith }
2161d763cef2SBarry Smith 
2162d763cef2SBarry Smith /*@
216380275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2164d763cef2SBarry Smith 
2165d763cef2SBarry Smith    Not Collective
2166d763cef2SBarry Smith 
2167d763cef2SBarry Smith    Input Parameter:
2168d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2169d763cef2SBarry Smith 
2170d763cef2SBarry Smith    Output Parameter:
217180275a0aSLisandro Dalcin .  steps - number of steps completed so far
2172d763cef2SBarry Smith 
2173d763cef2SBarry Smith    Level: intermediate
2174d763cef2SBarry Smith 
2175d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
21769be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2177d763cef2SBarry Smith @*/
217880275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2179d763cef2SBarry Smith {
2180d763cef2SBarry Smith   PetscFunctionBegin;
21810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218280275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
218380275a0aSLisandro Dalcin   *steps = ts->steps;
218480275a0aSLisandro Dalcin   PetscFunctionReturn(0);
218580275a0aSLisandro Dalcin }
218680275a0aSLisandro Dalcin 
218780275a0aSLisandro Dalcin /*@
218880275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
218980275a0aSLisandro Dalcin 
219080275a0aSLisandro Dalcin    Logically Collective on TS
219180275a0aSLisandro Dalcin 
219280275a0aSLisandro Dalcin    Input Parameters:
219380275a0aSLisandro Dalcin +  ts - the TS context
219480275a0aSLisandro Dalcin -  steps - number of steps completed so far
219580275a0aSLisandro Dalcin 
219680275a0aSLisandro Dalcin    Notes:
219780275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
219880275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
219980275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
220080275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
220180275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
220280275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
220380275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
220480275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
220580275a0aSLisandro Dalcin 
220680275a0aSLisandro Dalcin    Level: advanced
220780275a0aSLisandro Dalcin 
220880275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
220980275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
221080275a0aSLisandro Dalcin @*/
221180275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
221280275a0aSLisandro Dalcin {
221380275a0aSLisandro Dalcin   PetscFunctionBegin;
221480275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
221580275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
221680275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
221780275a0aSLisandro Dalcin   ts->steps = steps;
2218d763cef2SBarry Smith   PetscFunctionReturn(0);
2219d763cef2SBarry Smith }
2220d763cef2SBarry Smith 
2221d763cef2SBarry Smith /*@
2222d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2223d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2224d763cef2SBarry Smith 
22253f9fe445SBarry Smith    Logically Collective on TS
2226d763cef2SBarry Smith 
2227d763cef2SBarry Smith    Input Parameters:
2228d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2229d763cef2SBarry Smith -  time_step - the size of the timestep
2230d763cef2SBarry Smith 
2231d763cef2SBarry Smith    Level: intermediate
2232d763cef2SBarry Smith 
2233aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2234d763cef2SBarry Smith 
2235d763cef2SBarry Smith .keywords: TS, set, timestep
2236d763cef2SBarry Smith @*/
22377087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2238d763cef2SBarry Smith {
2239d763cef2SBarry Smith   PetscFunctionBegin;
22400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2241c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2242d763cef2SBarry Smith   ts->time_step = time_step;
2243d763cef2SBarry Smith   PetscFunctionReturn(0);
2244d763cef2SBarry Smith }
2245d763cef2SBarry Smith 
2246a43b19c4SJed Brown /*@
224749354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
224849354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
224949354f04SShri Abhyankar      or just return at the final time TS computed.
2250a43b19c4SJed Brown 
2251a43b19c4SJed Brown   Logically Collective on TS
2252a43b19c4SJed Brown 
2253a43b19c4SJed Brown    Input Parameter:
2254a43b19c4SJed Brown +   ts - the time-step context
225549354f04SShri Abhyankar -   eftopt - exact final time option
2256a43b19c4SJed Brown 
2257feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2258feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2259feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2260feed9e9dSBarry Smith 
2261feed9e9dSBarry Smith    Options Database:
2262feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2263feed9e9dSBarry Smith 
2264ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2265ee346746SBarry Smith     then the final time you selected.
2266ee346746SBarry Smith 
2267a43b19c4SJed Brown    Level: beginner
2268a43b19c4SJed Brown 
2269f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2270a43b19c4SJed Brown @*/
227149354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2272a43b19c4SJed Brown {
2273a43b19c4SJed Brown   PetscFunctionBegin;
2274a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227549354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
227649354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2277a43b19c4SJed Brown   PetscFunctionReturn(0);
2278a43b19c4SJed Brown }
2279a43b19c4SJed Brown 
2280d763cef2SBarry Smith /*@
2281f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2282f6953c82SLisandro Dalcin 
2283f6953c82SLisandro Dalcin    Not Collective
2284f6953c82SLisandro Dalcin 
2285f6953c82SLisandro Dalcin    Input Parameter:
2286f6953c82SLisandro Dalcin .  ts - the TS context
2287f6953c82SLisandro Dalcin 
2288f6953c82SLisandro Dalcin    Output Parameter:
2289f6953c82SLisandro Dalcin .  eftopt - exact final time option
2290f6953c82SLisandro Dalcin 
2291f6953c82SLisandro Dalcin    Level: beginner
2292f6953c82SLisandro Dalcin 
2293f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2294f6953c82SLisandro Dalcin @*/
2295f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2296f6953c82SLisandro Dalcin {
2297f6953c82SLisandro Dalcin   PetscFunctionBegin;
2298f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2299f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2300f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2301f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2302f6953c82SLisandro Dalcin }
2303f6953c82SLisandro Dalcin 
2304f6953c82SLisandro Dalcin /*@
2305d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2306d763cef2SBarry Smith 
2307d763cef2SBarry Smith    Not Collective
2308d763cef2SBarry Smith 
2309d763cef2SBarry Smith    Input Parameter:
2310d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2311d763cef2SBarry Smith 
2312d763cef2SBarry Smith    Output Parameter:
2313d763cef2SBarry Smith .  dt - the current timestep size
2314d763cef2SBarry Smith 
2315d763cef2SBarry Smith    Level: intermediate
2316d763cef2SBarry Smith 
2317aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2318d763cef2SBarry Smith 
2319d763cef2SBarry Smith .keywords: TS, get, timestep
2320d763cef2SBarry Smith @*/
23217087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2322d763cef2SBarry Smith {
2323d763cef2SBarry Smith   PetscFunctionBegin;
23240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2325f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2326d763cef2SBarry Smith   *dt = ts->time_step;
2327d763cef2SBarry Smith   PetscFunctionReturn(0);
2328d763cef2SBarry Smith }
2329d763cef2SBarry Smith 
2330d8e5e3e6SSatish Balay /*@
2331d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2332d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2333d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2334d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2335d763cef2SBarry Smith 
2336d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2337d763cef2SBarry Smith 
2338d763cef2SBarry Smith    Input Parameter:
2339d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2340d763cef2SBarry Smith 
2341d763cef2SBarry Smith    Output Parameter:
2342d763cef2SBarry Smith .  v - the vector containing the solution
2343d763cef2SBarry Smith 
234463e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
234563e21af5SBarry Smith    final time. It returns the solution at the next timestep.
234663e21af5SBarry Smith 
2347d763cef2SBarry Smith    Level: intermediate
2348d763cef2SBarry Smith 
23490ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2350d763cef2SBarry Smith 
2351d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2352d763cef2SBarry Smith @*/
23537087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2354d763cef2SBarry Smith {
2355d763cef2SBarry Smith   PetscFunctionBegin;
23560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23574482741eSBarry Smith   PetscValidPointer(v,2);
23588737fe31SLisandro Dalcin   *v = ts->vec_sol;
2359d763cef2SBarry Smith   PetscFunctionReturn(0);
2360d763cef2SBarry Smith }
2361d763cef2SBarry Smith 
236203fe5f5eSDebojyoti Ghosh /*@
2363b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
236403fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2365b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
236603fe5f5eSDebojyoti Ghosh    structure as the solution vector.
236703fe5f5eSDebojyoti Ghosh 
236803fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
236903fe5f5eSDebojyoti Ghosh 
237003fe5f5eSDebojyoti Ghosh    Parameters :
237103fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2372b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2373b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
237403fe5f5eSDebojyoti Ghosh        returned in v.
2375b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
237603fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2377b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
237803fe5f5eSDebojyoti Ghosh 
23794cdd57e5SDebojyoti Ghosh    Level: advanced
238003fe5f5eSDebojyoti Ghosh 
238103fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
238203fe5f5eSDebojyoti Ghosh 
238303fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
238403fe5f5eSDebojyoti Ghosh @*/
2385b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
238603fe5f5eSDebojyoti Ghosh {
238703fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
238803fe5f5eSDebojyoti Ghosh 
238903fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
239003fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2391b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
239203fe5f5eSDebojyoti Ghosh   else {
2393b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
239403fe5f5eSDebojyoti Ghosh   }
239503fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
239603fe5f5eSDebojyoti Ghosh }
239703fe5f5eSDebojyoti Ghosh 
23984cdd57e5SDebojyoti Ghosh /*@
23994cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24004cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24014cdd57e5SDebojyoti Ghosh 
24024cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24034cdd57e5SDebojyoti Ghosh 
24044cdd57e5SDebojyoti Ghosh    Parameters :
24054cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
24064cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
24074cdd57e5SDebojyoti Ghosh 
24084cdd57e5SDebojyoti Ghosh    Level: intermediate
24094cdd57e5SDebojyoti Ghosh 
24104cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24114cdd57e5SDebojyoti Ghosh 
24124cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
24134cdd57e5SDebojyoti Ghosh @*/
24144cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24154cdd57e5SDebojyoti Ghosh {
24164cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24174cdd57e5SDebojyoti Ghosh 
24184cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24194cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2420f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24214cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2422f6356ec7SDebojyoti Ghosh   } else {
2423f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2424f6356ec7SDebojyoti Ghosh   }
24254cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24264cdd57e5SDebojyoti Ghosh }
24274cdd57e5SDebojyoti Ghosh 
24284cdd57e5SDebojyoti Ghosh /*@
24294cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24304cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24314cdd57e5SDebojyoti Ghosh 
24324cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24334cdd57e5SDebojyoti Ghosh 
24349657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24359657682dSDebojyoti Ghosh 
24364cdd57e5SDebojyoti Ghosh    Parameters :
24374cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2438657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
24394cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
24404cdd57e5SDebojyoti Ghosh 
24414cdd57e5SDebojyoti Ghosh    Level: intermediate
24424cdd57e5SDebojyoti Ghosh 
244357df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24444cdd57e5SDebojyoti Ghosh 
24454cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
24464cdd57e5SDebojyoti Ghosh @*/
24470a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24484cdd57e5SDebojyoti Ghosh {
24494cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24504cdd57e5SDebojyoti Ghosh 
24514cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24524cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2453f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
24540a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2455f6356ec7SDebojyoti Ghosh   } else {
2456f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2457f6356ec7SDebojyoti Ghosh   }
24584cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24594cdd57e5SDebojyoti Ghosh }
24604cdd57e5SDebojyoti Ghosh 
246157df6a1bSDebojyoti Ghosh /*@
246257df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
246357df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
246457df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
246557df6a1bSDebojyoti Ghosh 
246657df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
246757df6a1bSDebojyoti Ghosh 
246857df6a1bSDebojyoti Ghosh    Parameters :
246957df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
247057df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
247157df6a1bSDebojyoti Ghosh 
247257df6a1bSDebojyoti Ghosh    Level: intermediate
247357df6a1bSDebojyoti Ghosh 
247457df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
247557df6a1bSDebojyoti Ghosh 
247657df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
247757df6a1bSDebojyoti Ghosh @*/
247857df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
247957df6a1bSDebojyoti Ghosh {
248057df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
248157df6a1bSDebojyoti Ghosh 
248257df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
248357df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24849657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
248557df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
248657df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
248757df6a1bSDebojyoti Ghosh   }
248857df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
248957df6a1bSDebojyoti Ghosh }
249057df6a1bSDebojyoti Ghosh 
2491c235aa8dSHong Zhang /*@
2492dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2493c235aa8dSHong Zhang 
2494c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2495c235aa8dSHong Zhang 
2496c235aa8dSHong Zhang    Input Parameter:
2497c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2498c235aa8dSHong Zhang 
2499c235aa8dSHong Zhang    Output Parameter:
2500abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2501abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2502c235aa8dSHong Zhang 
2503c235aa8dSHong Zhang    Level: intermediate
2504c235aa8dSHong Zhang 
2505c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2506c235aa8dSHong Zhang 
2507c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2508c235aa8dSHong Zhang @*/
2509dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2510c235aa8dSHong Zhang {
2511c235aa8dSHong Zhang   PetscFunctionBegin;
2512c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2513abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2514abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2515abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2516c235aa8dSHong Zhang   PetscFunctionReturn(0);
2517c235aa8dSHong Zhang }
2518c235aa8dSHong Zhang 
2519bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2520d8e5e3e6SSatish Balay /*@
2521bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2522d763cef2SBarry Smith 
2523bdad233fSMatthew Knepley   Not collective
2524d763cef2SBarry Smith 
2525bdad233fSMatthew Knepley   Input Parameters:
2526bdad233fSMatthew Knepley + ts   - The TS
2527bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2528d763cef2SBarry Smith .vb
25290910c330SBarry Smith          U_t - A U = 0      (linear)
25300910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25310910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2532d763cef2SBarry Smith .ve
2533d763cef2SBarry Smith 
2534d763cef2SBarry Smith    Level: beginner
2535d763cef2SBarry Smith 
2536bdad233fSMatthew Knepley .keywords: TS, problem type
2537bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2538d763cef2SBarry Smith @*/
25397087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2540a7cc72afSBarry Smith {
25419e2a6581SJed Brown   PetscErrorCode ierr;
25429e2a6581SJed Brown 
2543d763cef2SBarry Smith   PetscFunctionBegin;
25440700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2545bdad233fSMatthew Knepley   ts->problem_type = type;
25469e2a6581SJed Brown   if (type == TS_LINEAR) {
25479e2a6581SJed Brown     SNES snes;
25489e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25499e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25509e2a6581SJed Brown   }
2551d763cef2SBarry Smith   PetscFunctionReturn(0);
2552d763cef2SBarry Smith }
2553d763cef2SBarry Smith 
2554bdad233fSMatthew Knepley /*@C
2555bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2556bdad233fSMatthew Knepley 
2557bdad233fSMatthew Knepley   Not collective
2558bdad233fSMatthew Knepley 
2559bdad233fSMatthew Knepley   Input Parameter:
2560bdad233fSMatthew Knepley . ts   - The TS
2561bdad233fSMatthew Knepley 
2562bdad233fSMatthew Knepley   Output Parameter:
2563bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2564bdad233fSMatthew Knepley .vb
2565089b2837SJed Brown          M U_t = A U
2566089b2837SJed Brown          M(t) U_t = A(t) U
2567b5abc632SBarry Smith          F(t,U,U_t)
2568bdad233fSMatthew Knepley .ve
2569bdad233fSMatthew Knepley 
2570bdad233fSMatthew Knepley    Level: beginner
2571bdad233fSMatthew Knepley 
2572bdad233fSMatthew Knepley .keywords: TS, problem type
2573bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2574bdad233fSMatthew Knepley @*/
25757087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2576a7cc72afSBarry Smith {
2577bdad233fSMatthew Knepley   PetscFunctionBegin;
25780700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25794482741eSBarry Smith   PetscValidIntPointer(type,2);
2580bdad233fSMatthew Knepley   *type = ts->problem_type;
2581bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2582bdad233fSMatthew Knepley }
2583d763cef2SBarry Smith 
2584d763cef2SBarry Smith /*@
2585d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2586d763cef2SBarry Smith    of a timestepper.
2587d763cef2SBarry Smith 
2588d763cef2SBarry Smith    Collective on TS
2589d763cef2SBarry Smith 
2590d763cef2SBarry Smith    Input Parameter:
2591d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2592d763cef2SBarry Smith 
2593d763cef2SBarry Smith    Notes:
2594d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2595d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2596141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2597d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2598141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2599d763cef2SBarry Smith 
2600d763cef2SBarry Smith    Level: advanced
2601d763cef2SBarry Smith 
2602d763cef2SBarry Smith .keywords: TS, timestep, setup
2603d763cef2SBarry Smith 
2604141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2605d763cef2SBarry Smith @*/
26067087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2607d763cef2SBarry Smith {
2608dfbe8321SBarry Smith   PetscErrorCode ierr;
26096c6b9e74SPeter Brune   DM             dm;
26106c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2611d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2612cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26136c6b9e74SPeter Brune   TSIJacobian    ijac;
2614efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26156c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
26162ffb9264SLisandro Dalcin   PetscBool      isnone;
2617d763cef2SBarry Smith 
2618d763cef2SBarry Smith   PetscFunctionBegin;
26190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2620277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2621277b19d0SLisandro Dalcin 
26227adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2623cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2624cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2625d763cef2SBarry Smith   }
2626277b19d0SLisandro Dalcin 
2627277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2628277b19d0SLisandro Dalcin 
2629e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2630e1244c69SJed Brown     Mat Amat,Pmat;
2631e1244c69SJed Brown     SNES snes;
2632e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2633e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2634e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2635e1244c69SJed Brown      * have displaced the RHS matrix */
2636e1244c69SJed Brown     if (Amat == ts->Arhs) {
2637abc0d4abSBarry 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 */
2638abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2639e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2640e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2641e1244c69SJed Brown     }
2642e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2643abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2644e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2645e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2646e1244c69SJed Brown     }
2647e1244c69SJed Brown   }
26482ffb9264SLisandro Dalcin 
26492ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
26502ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
26512ffb9264SLisandro Dalcin 
2652277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2653000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2654277b19d0SLisandro Dalcin   }
2655277b19d0SLisandro Dalcin 
26562ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
26572ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
26582ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
26592ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
26602ffb9264SLisandro Dalcin 
2661a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26626c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26636c6b9e74SPeter Brune    */
26646c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
26650298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
26666c6b9e74SPeter Brune   if (!func) {
26676c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
26686c6b9e74SPeter Brune   }
2669a6772fa2SLisandro 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.
26706c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26716c6b9e74SPeter Brune    */
26720298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26730298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2674efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26750298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2676efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
26776c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
26786c6b9e74SPeter Brune   }
2679c0517034SDebojyoti Ghosh 
2680c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2681c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2682c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2683c0517034SDebojyoti Ghosh   }
2684c0517034SDebojyoti Ghosh 
2685277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2686277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2687277b19d0SLisandro Dalcin }
2688277b19d0SLisandro Dalcin 
2689f6a906c0SBarry Smith /*@
2690f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2691f6a906c0SBarry Smith    of an adjoint solver
2692f6a906c0SBarry Smith 
2693f6a906c0SBarry Smith    Collective on TS
2694f6a906c0SBarry Smith 
2695f6a906c0SBarry Smith    Input Parameter:
2696f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2697f6a906c0SBarry Smith 
2698f6a906c0SBarry Smith    Level: advanced
2699f6a906c0SBarry Smith 
2700f6a906c0SBarry Smith .keywords: TS, timestep, setup
2701f6a906c0SBarry Smith 
2702947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2703f6a906c0SBarry Smith @*/
2704f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2705f6a906c0SBarry Smith {
2706f6a906c0SBarry Smith   PetscErrorCode ierr;
2707f6a906c0SBarry Smith 
2708f6a906c0SBarry Smith   PetscFunctionBegin;
2709f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2710f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
271123706b9aSHong Zhang   if (!ts->vecs_sensi) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2712c5767687SHong Zhang   if (ts->vecs_sensip && !ts->Jacp) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetRHSJacobian() first");
2713d8151eeaSBarry Smith 
2714947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function */
2715d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2716d8151eeaSBarry Smith     if (ts->vecs_sensip){
2717d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2718d8151eeaSBarry Smith     }
2719947abb85SHong Zhang   }
2720d8151eeaSBarry Smith 
272142f2b339SBarry Smith   if (ts->ops->adjointsetup) {
272242f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2723f6a906c0SBarry Smith   }
2724f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2725f6a906c0SBarry Smith   PetscFunctionReturn(0);
2726f6a906c0SBarry Smith }
2727f6a906c0SBarry Smith 
2728277b19d0SLisandro Dalcin /*@
2729277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2730277b19d0SLisandro Dalcin 
2731277b19d0SLisandro Dalcin    Collective on TS
2732277b19d0SLisandro Dalcin 
2733277b19d0SLisandro Dalcin    Input Parameter:
2734277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2735277b19d0SLisandro Dalcin 
2736277b19d0SLisandro Dalcin    Level: beginner
2737277b19d0SLisandro Dalcin 
2738277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2739277b19d0SLisandro Dalcin 
2740277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2741277b19d0SLisandro Dalcin @*/
2742277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2743277b19d0SLisandro Dalcin {
2744277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2745277b19d0SLisandro Dalcin 
2746277b19d0SLisandro Dalcin   PetscFunctionBegin;
2747277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2748b18ea86cSHong Zhang 
2749277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2750277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2751277b19d0SLisandro Dalcin   }
2752277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2753e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2754bbd56ea5SKarl Rupp 
27554e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27564e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2757214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27586bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2759efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2760e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2761e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
276238637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2763bbd56ea5SKarl Rupp 
2764d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2765d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2766715f1b00SHong Zhang 
2767ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
27682c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
276936eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2770715f1b00SHong Zhang 
2771022c081aSHong Zhang   ierr = PetscFree(ts->vecs_fwdsensipacked);CHKERRQ(ierr);
2772022c081aSHong Zhang 
2773277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2774d763cef2SBarry Smith   PetscFunctionReturn(0);
2775d763cef2SBarry Smith }
2776d763cef2SBarry Smith 
2777d8e5e3e6SSatish Balay /*@
2778d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2779d763cef2SBarry Smith    with TSCreate().
2780d763cef2SBarry Smith 
2781d763cef2SBarry Smith    Collective on TS
2782d763cef2SBarry Smith 
2783d763cef2SBarry Smith    Input Parameter:
2784d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith    Level: beginner
2787d763cef2SBarry Smith 
2788d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2789d763cef2SBarry Smith 
2790d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2791d763cef2SBarry Smith @*/
27926bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2793d763cef2SBarry Smith {
27946849ba73SBarry Smith   PetscErrorCode ierr;
2795d763cef2SBarry Smith 
2796d763cef2SBarry Smith   PetscFunctionBegin;
27976bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
27986bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
27996bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2800d763cef2SBarry Smith 
28016bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2802277b19d0SLisandro Dalcin 
2803e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2804e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28056bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28066d4c513bSLisandro Dalcin 
2807bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2808bc952696SBarry Smith 
280984df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28106427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28116427ac75SLisandro Dalcin 
28126bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28136bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28146bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28150dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28166d4c513bSLisandro Dalcin 
2817a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2818d763cef2SBarry Smith   PetscFunctionReturn(0);
2819d763cef2SBarry Smith }
2820d763cef2SBarry Smith 
2821d8e5e3e6SSatish Balay /*@
2822d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2823d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2824d763cef2SBarry Smith 
2825d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2826d763cef2SBarry Smith 
2827d763cef2SBarry Smith    Input Parameter:
2828d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2829d763cef2SBarry Smith 
2830d763cef2SBarry Smith    Output Parameter:
2831d763cef2SBarry Smith .  snes - the nonlinear solver context
2832d763cef2SBarry Smith 
2833d763cef2SBarry Smith    Notes:
2834d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2835d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
283694b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28390298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2840d763cef2SBarry Smith 
2841d763cef2SBarry Smith    Level: beginner
2842d763cef2SBarry Smith 
2843d763cef2SBarry Smith .keywords: timestep, get, SNES
2844d763cef2SBarry Smith @*/
28457087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2846d763cef2SBarry Smith {
2847d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2848d372ba47SLisandro Dalcin 
2849d763cef2SBarry Smith   PetscFunctionBegin;
28500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28514482741eSBarry Smith   PetscValidPointer(snes,2);
2852d372ba47SLisandro Dalcin   if (!ts->snes) {
2853ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
28540298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28553bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2856d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2857496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28589e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28599e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
28609e2a6581SJed Brown     }
2861d372ba47SLisandro Dalcin   }
2862d763cef2SBarry Smith   *snes = ts->snes;
2863d763cef2SBarry Smith   PetscFunctionReturn(0);
2864d763cef2SBarry Smith }
2865d763cef2SBarry Smith 
2866deb2cd25SJed Brown /*@
2867deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2868deb2cd25SJed Brown 
2869deb2cd25SJed Brown    Collective
2870deb2cd25SJed Brown 
2871deb2cd25SJed Brown    Input Parameter:
2872deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2873deb2cd25SJed Brown -  snes - the nonlinear solver context
2874deb2cd25SJed Brown 
2875deb2cd25SJed Brown    Notes:
2876deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2877deb2cd25SJed Brown 
2878deb2cd25SJed Brown    Level: developer
2879deb2cd25SJed Brown 
2880deb2cd25SJed Brown .keywords: timestep, set, SNES
2881deb2cd25SJed Brown @*/
2882deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2883deb2cd25SJed Brown {
2884deb2cd25SJed Brown   PetscErrorCode ierr;
2885d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2886deb2cd25SJed Brown 
2887deb2cd25SJed Brown   PetscFunctionBegin;
2888deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2889deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2890deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2891deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2892bbd56ea5SKarl Rupp 
2893deb2cd25SJed Brown   ts->snes = snes;
2894bbd56ea5SKarl Rupp 
28950298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28960298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2897740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28980298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2899740132f1SEmil Constantinescu   }
2900deb2cd25SJed Brown   PetscFunctionReturn(0);
2901deb2cd25SJed Brown }
2902deb2cd25SJed Brown 
2903d8e5e3e6SSatish Balay /*@
290494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2905d763cef2SBarry Smith    a TS (timestepper) context.
2906d763cef2SBarry Smith 
290794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2908d763cef2SBarry Smith 
2909d763cef2SBarry Smith    Input Parameter:
2910d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2911d763cef2SBarry Smith 
2912d763cef2SBarry Smith    Output Parameter:
291394b7f48cSBarry Smith .  ksp - the nonlinear solver context
2914d763cef2SBarry Smith 
2915d763cef2SBarry Smith    Notes:
291694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2917d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2918d763cef2SBarry Smith    KSP and PC contexts as well.
2919d763cef2SBarry Smith 
292094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29210298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2922d763cef2SBarry Smith 
2923d763cef2SBarry Smith    Level: beginner
2924d763cef2SBarry Smith 
292594b7f48cSBarry Smith .keywords: timestep, get, KSP
2926d763cef2SBarry Smith @*/
29277087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2928d763cef2SBarry Smith {
2929d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2930089b2837SJed Brown   SNES           snes;
2931d372ba47SLisandro Dalcin 
2932d763cef2SBarry Smith   PetscFunctionBegin;
29330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29344482741eSBarry Smith   PetscValidPointer(ksp,2);
293517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2936e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2937089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2938089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2939d763cef2SBarry Smith   PetscFunctionReturn(0);
2940d763cef2SBarry Smith }
2941d763cef2SBarry Smith 
2942d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2943d763cef2SBarry Smith 
2944adb62b0dSMatthew Knepley /*@
2945618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2946618ce8baSLisandro Dalcin 
2947618ce8baSLisandro Dalcin    Logically Collective on TS
2948618ce8baSLisandro Dalcin 
2949618ce8baSLisandro Dalcin    Input Parameters:
2950618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2951618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2952618ce8baSLisandro Dalcin 
2953618ce8baSLisandro Dalcin    Options Database Keys:
2954618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2955618ce8baSLisandro Dalcin 
2956618ce8baSLisandro Dalcin    Notes:
2957618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2958618ce8baSLisandro Dalcin 
2959618ce8baSLisandro Dalcin    Level: intermediate
2960618ce8baSLisandro Dalcin 
2961618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2962618ce8baSLisandro Dalcin 
2963618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2964618ce8baSLisandro Dalcin @*/
2965618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2966618ce8baSLisandro Dalcin {
2967618ce8baSLisandro Dalcin   PetscFunctionBegin;
2968618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2969618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2970618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2971618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2972618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2973618ce8baSLisandro Dalcin }
2974618ce8baSLisandro Dalcin 
2975618ce8baSLisandro Dalcin /*@
2976618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2977618ce8baSLisandro Dalcin 
2978618ce8baSLisandro Dalcin    Not Collective
2979618ce8baSLisandro Dalcin 
2980618ce8baSLisandro Dalcin    Input Parameters:
2981618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2982618ce8baSLisandro Dalcin 
2983618ce8baSLisandro Dalcin    Output Parameter:
2984618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2985618ce8baSLisandro Dalcin 
2986618ce8baSLisandro Dalcin    Level: advanced
2987618ce8baSLisandro Dalcin 
2988618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2989618ce8baSLisandro Dalcin 
2990618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2991618ce8baSLisandro Dalcin @*/
2992618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2993618ce8baSLisandro Dalcin {
2994618ce8baSLisandro Dalcin   PetscFunctionBegin;
2995618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2996618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2997618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2998618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2999618ce8baSLisandro Dalcin }
3000618ce8baSLisandro Dalcin 
3001618ce8baSLisandro Dalcin /*@
3002618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3003618ce8baSLisandro Dalcin 
3004618ce8baSLisandro Dalcin    Logically Collective on TS
3005618ce8baSLisandro Dalcin 
3006618ce8baSLisandro Dalcin    Input Parameters:
3007618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3008618ce8baSLisandro Dalcin -  maxtime - final time to step to
3009618ce8baSLisandro Dalcin 
3010618ce8baSLisandro Dalcin    Options Database Keys:
3011ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3012618ce8baSLisandro Dalcin 
3013618ce8baSLisandro Dalcin    Notes:
3014618ce8baSLisandro Dalcin    The default maximum time is 5.0
3015618ce8baSLisandro Dalcin 
3016618ce8baSLisandro Dalcin    Level: intermediate
3017618ce8baSLisandro Dalcin 
3018618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
3019618ce8baSLisandro Dalcin 
3020618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3021618ce8baSLisandro Dalcin @*/
3022618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3023618ce8baSLisandro Dalcin {
3024618ce8baSLisandro Dalcin   PetscFunctionBegin;
3025618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3026618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3027618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3028618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3029618ce8baSLisandro Dalcin }
3030618ce8baSLisandro Dalcin 
3031618ce8baSLisandro Dalcin /*@
3032618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3033618ce8baSLisandro Dalcin 
3034618ce8baSLisandro Dalcin    Not Collective
3035618ce8baSLisandro Dalcin 
3036618ce8baSLisandro Dalcin    Input Parameters:
3037618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3038618ce8baSLisandro Dalcin 
3039618ce8baSLisandro Dalcin    Output Parameter:
3040618ce8baSLisandro Dalcin .  maxtime - final time to step to
3041618ce8baSLisandro Dalcin 
3042618ce8baSLisandro Dalcin    Level: advanced
3043618ce8baSLisandro Dalcin 
3044618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
3045618ce8baSLisandro Dalcin 
3046618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3047618ce8baSLisandro Dalcin @*/
3048618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3049618ce8baSLisandro Dalcin {
3050618ce8baSLisandro Dalcin   PetscFunctionBegin;
3051618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3052618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3053618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3054618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3055618ce8baSLisandro Dalcin }
3056618ce8baSLisandro Dalcin 
3057618ce8baSLisandro Dalcin /*@
3058aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3059edc382c3SSatish Balay 
3060edc382c3SSatish Balay    Level: deprecated
3061edc382c3SSatish Balay 
3062aaa6c58dSLisandro Dalcin @*/
3063aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3064aaa6c58dSLisandro Dalcin {
3065aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3066aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3067aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3068aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3069aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3070aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3071aaa6c58dSLisandro Dalcin }
3072aaa6c58dSLisandro Dalcin 
3073aaa6c58dSLisandro Dalcin /*@
307419eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3075edc382c3SSatish Balay 
3076edc382c3SSatish Balay    Level: deprecated
3077edc382c3SSatish Balay 
3078adb62b0dSMatthew Knepley @*/
30797087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3080adb62b0dSMatthew Knepley {
3081adb62b0dSMatthew Knepley   PetscFunctionBegin;
30820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3083abc0a331SBarry Smith   if (maxsteps) {
30844482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3085adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3086adb62b0dSMatthew Knepley   }
3087abc0a331SBarry Smith   if (maxtime) {
30884482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3089adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3090adb62b0dSMatthew Knepley   }
3091adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3092adb62b0dSMatthew Knepley }
3093adb62b0dSMatthew Knepley 
3094d763cef2SBarry Smith /*@
309519eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3096edc382c3SSatish Balay 
3097edc382c3SSatish Balay    Level: deprecated
3098edc382c3SSatish Balay 
3099d763cef2SBarry Smith @*/
31007087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3101d763cef2SBarry Smith {
3102d763cef2SBarry Smith   PetscFunctionBegin;
31030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3104c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3105c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
310639b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
310739b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3108d763cef2SBarry Smith   PetscFunctionReturn(0);
3109d763cef2SBarry Smith }
3110d763cef2SBarry Smith 
3111d763cef2SBarry Smith /*@
31125c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3113edc382c3SSatish Balay 
3114edc382c3SSatish Balay    Level: deprecated
3115edc382c3SSatish Balay 
31165c5f5948SLisandro Dalcin @*/
3117e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31185c5f5948SLisandro Dalcin 
311919eac22cSLisandro Dalcin /*@
31204f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3121edc382c3SSatish Balay 
3122edc382c3SSatish Balay    Level: deprecated
3123edc382c3SSatish Balay 
31244f4e0956SLisandro Dalcin @*/
31254f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31264f4e0956SLisandro Dalcin 
31274f4e0956SLisandro Dalcin /*@
3128d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3129d763cef2SBarry Smith    for use by the TS routines.
3130d763cef2SBarry Smith 
31313f9fe445SBarry Smith    Logically Collective on TS and Vec
3132d763cef2SBarry Smith 
3133d763cef2SBarry Smith    Input Parameters:
3134d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31350910c330SBarry Smith -  u - the solution vector
3136d763cef2SBarry Smith 
3137d763cef2SBarry Smith    Level: beginner
3138d763cef2SBarry Smith 
3139715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
31400ed3bfb6SBarry Smith 
31410ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3142d763cef2SBarry Smith @*/
31430910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3144d763cef2SBarry Smith {
31458737fe31SLisandro Dalcin   PetscErrorCode ierr;
3146496e6a7aSJed Brown   DM             dm;
31478737fe31SLisandro Dalcin 
3148d763cef2SBarry Smith   PetscFunctionBegin;
31490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31500910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31510910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31526bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31530910c330SBarry Smith   ts->vec_sol = u;
3154bbd56ea5SKarl Rupp 
3155496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
31560910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3157d763cef2SBarry Smith   PetscFunctionReturn(0);
3158d763cef2SBarry Smith }
3159d763cef2SBarry Smith 
316073b18844SBarry Smith /*@
316173b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
316273b18844SBarry Smith 
316373b18844SBarry Smith    Logically Collective on TS
316473b18844SBarry Smith 
316573b18844SBarry Smith    Input Parameters:
316673b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
316773b18844SBarry Smith .  steps - number of steps to use
316873b18844SBarry Smith 
316973b18844SBarry Smith    Level: intermediate
317073b18844SBarry Smith 
317173b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
317273b18844SBarry Smith           so as to integrate back to less than the original timestep
317373b18844SBarry Smith 
317473b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
317573b18844SBarry Smith 
317673b18844SBarry Smith .seealso: TSSetExactFinalTime()
317773b18844SBarry Smith @*/
317873b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
317973b18844SBarry Smith {
318073b18844SBarry Smith   PetscFunctionBegin;
318173b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
318273b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
318373b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
31842808aa04SLisandro Dalcin   if (steps > ts->steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps");
318573b18844SBarry Smith   ts->adjoint_max_steps = steps;
318673b18844SBarry Smith   PetscFunctionReturn(0);
318773b18844SBarry Smith }
318873b18844SBarry Smith 
3189c235aa8dSHong Zhang /*@
3190715f1b00SHong Zhang    TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial values and w.r.t. the problem parameters
31910cf82b59SBarry Smith       for use by the TSAdjoint routines.
3192c235aa8dSHong Zhang 
3193c235aa8dSHong Zhang    Logically Collective on TS and Vec
3194c235aa8dSHong Zhang 
3195c235aa8dSHong Zhang    Input Parameters:
3196c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
3197abc2977eSBarry Smith .  lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector
3198abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
3199c235aa8dSHong Zhang 
3200c235aa8dSHong Zhang    Level: beginner
3201c235aa8dSHong Zhang 
3202abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
32030cf82b59SBarry Smith 
32041585b412SBarry Smith    After TSAdjointSolve() is called the lamba and the mu contain the computed sensitivities
32051585b412SBarry Smith 
3206715f1b00SHong Zhang .keywords: TS, timestep, set, sensitivity, initial values
3207c235aa8dSHong Zhang @*/
3208dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
3209c235aa8dSHong Zhang {
3210c235aa8dSHong Zhang   PetscFunctionBegin;
3211c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3212abc2977eSBarry Smith   PetscValidPointer(lambda,2);
3213abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
3214abc2977eSBarry Smith   ts->vecs_sensip = mu;
3215dfb21088SHong Zhang   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand");
3216abc2977eSBarry Smith   ts->numcost  = numcost;
3217ad8e2604SHong Zhang   PetscFunctionReturn(0);
3218ad8e2604SHong Zhang }
3219ad8e2604SHong Zhang 
3220ad8e2604SHong Zhang /*@C
32210cf82b59SBarry Smith   TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix.
3222ad8e2604SHong Zhang 
3223ad8e2604SHong Zhang   Logically Collective on TS
3224ad8e2604SHong Zhang 
3225ad8e2604SHong Zhang   Input Parameters:
3226ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
3227ad8e2604SHong Zhang - func - The function
3228ad8e2604SHong Zhang 
3229ad8e2604SHong Zhang   Calling sequence of func:
32300cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
323105755b9cSHong Zhang +   t - current timestep
32320cf82b59SBarry Smith .   y - input vector (current ODE solution)
323305755b9cSHong Zhang .   A - output matrix
323405755b9cSHong Zhang -   ctx - [optional] user-defined function context
3235ad8e2604SHong Zhang 
3236ad8e2604SHong Zhang   Level: intermediate
3237ad8e2604SHong Zhang 
32380cf82b59SBarry Smith   Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p
32390cf82b59SBarry Smith 
3240ad8e2604SHong Zhang .keywords: TS, sensitivity
3241ad8e2604SHong Zhang .seealso:
3242ad8e2604SHong Zhang @*/
32435bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
3244ad8e2604SHong Zhang {
3245ad8e2604SHong Zhang   PetscErrorCode ierr;
3246ad8e2604SHong Zhang 
3247ad8e2604SHong Zhang   PetscFunctionBegin;
3248ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
324923706b9aSHong Zhang   PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
3250ad8e2604SHong Zhang 
3251ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
3252ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
3253ad8e2604SHong Zhang   if(Amat) {
3254ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
3255ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
3256ad8e2604SHong Zhang     ts->Jacp = Amat;
3257ad8e2604SHong Zhang   }
3258ad8e2604SHong Zhang   PetscFunctionReturn(0);
3259ad8e2604SHong Zhang }
3260ad8e2604SHong Zhang 
32610cf82b59SBarry Smith /*@C
32625bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
3263ad8e2604SHong Zhang 
3264ad8e2604SHong Zhang   Collective on TS
3265ad8e2604SHong Zhang 
3266ad8e2604SHong Zhang   Input Parameters:
3267ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
3268ad8e2604SHong Zhang 
3269ad8e2604SHong Zhang   Level: developer
3270ad8e2604SHong Zhang 
3271ad8e2604SHong Zhang .keywords: TS, sensitivity
32725bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
3273ad8e2604SHong Zhang @*/
32745bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
3275ad8e2604SHong Zhang {
3276ad8e2604SHong Zhang   PetscErrorCode ierr;
3277ad8e2604SHong Zhang 
3278ad8e2604SHong Zhang   PetscFunctionBegin;
3279ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3280ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3281ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
3282ad8e2604SHong Zhang 
3283ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
3284ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
3285ad8e2604SHong Zhang   PetscStackPop;
3286c235aa8dSHong Zhang   PetscFunctionReturn(0);
3287c235aa8dSHong Zhang }
3288c235aa8dSHong Zhang 
32896fd0887fSHong Zhang /*@C
3290dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
32916fd0887fSHong Zhang 
32926fd0887fSHong Zhang     Logically Collective on TS
32936fd0887fSHong Zhang 
32946fd0887fSHong Zhang     Input Parameters:
32956fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
3296abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
32973d1d12c6SHong Zhang .   costintegral - vector that stores the integral values
32980cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
3299b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3300b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3301feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3302b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
33036fd0887fSHong Zhang 
33040cf82b59SBarry Smith     Calling sequence of rf:
33053d1d12c6SHong Zhang $   PetscErrorCode rf(TS ts,PetscReal t,Vec y,Vec f,void *ctx);
33066fd0887fSHong Zhang 
3307b612ec54SBarry Smith     Calling sequence of drdyf:
33080cf82b59SBarry Smith $   PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3309b612ec54SBarry Smith 
3310b612ec54SBarry Smith     Calling sequence of drdpf:
33110cf82b59SBarry Smith $   PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3312b612ec54SBarry Smith 
3313b612ec54SBarry Smith     Level: intermediate
33146fd0887fSHong Zhang 
3315715f1b00SHong Zhang     Notes: For optimization there is usually a single cost function (numcost = 1). For sensitivities there may be multiple cost functions
33160cf82b59SBarry Smith 
33176fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
33186fd0887fSHong Zhang 
3319dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
33206fd0887fSHong Zhang @*/
33213d1d12c6SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,Vec costintegral,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3322d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3323b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3324b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
33256fd0887fSHong Zhang {
33266fd0887fSHong Zhang   PetscErrorCode ierr;
33276fd0887fSHong Zhang 
33286fd0887fSHong Zhang   PetscFunctionBegin;
33296fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33303d1d12c6SHong Zhang   if (costintegral) PetscValidHeaderSpecific(costintegral,VEC_CLASSID,3);
3331715f1b00SHong Zhang   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients() or TSForwardSetIntegralGradients()");
3332c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
33336fd0887fSHong Zhang 
33343d1d12c6SHong Zhang   if (costintegral) {
33353d1d12c6SHong Zhang     ierr = PetscObjectReference((PetscObject)costintegral);CHKERRQ(ierr);
33363d1d12c6SHong Zhang     ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
33373d1d12c6SHong Zhang     ts->vec_costintegral = costintegral;
3338afe7b317SHong Zhang   } else {
3339afe7b317SHong Zhang     if (!ts->vec_costintegral) { /* Create a seq vec if user does not provide one */
3340afe7b317SHong Zhang       ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
3341afe7b317SHong Zhang     } else {
3342afe7b317SHong Zhang       ierr = VecSet(ts->vec_costintegral,0.0);CHKERRQ(ierr);
3343afe7b317SHong Zhang     }
3344afe7b317SHong Zhang   }
3345afe7b317SHong Zhang   if (!ts->vec_costintegrand) {
33462c39e106SBarry Smith     ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
3347afe7b317SHong Zhang   } else {
3348afe7b317SHong Zhang     ierr = VecSet(ts->vec_costintegrand,0.0);CHKERRQ(ierr);
3349afe7b317SHong Zhang   }
3350afe7b317SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
33510cf82b59SBarry Smith   ts->costintegrand    = rf;
335205755b9cSHong Zhang   ts->costintegrandctx = ctx;
3353b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3354b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
33556fd0887fSHong Zhang   PetscFunctionReturn(0);
33566fd0887fSHong Zhang }
33576fd0887fSHong Zhang 
335836eaed60SHong Zhang /*@
3359dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
336036eaed60SHong Zhang    It is valid to call the routine after a backward run.
336136eaed60SHong Zhang 
336236eaed60SHong Zhang    Not Collective
336336eaed60SHong Zhang 
336436eaed60SHong Zhang    Input Parameter:
336536eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
336636eaed60SHong Zhang 
336736eaed60SHong Zhang    Output Parameter:
33682c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
336936eaed60SHong Zhang 
337036eaed60SHong Zhang    Level: intermediate
337136eaed60SHong Zhang 
3372dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
337336eaed60SHong Zhang 
337436eaed60SHong Zhang .keywords: TS, sensitivity analysis
337536eaed60SHong Zhang @*/
3376dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
337736eaed60SHong Zhang {
337836eaed60SHong Zhang   PetscFunctionBegin;
337936eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
338036eaed60SHong Zhang   PetscValidPointer(v,2);
33812c39e106SBarry Smith   *v = ts->vec_costintegral;
338236eaed60SHong Zhang   PetscFunctionReturn(0);
338336eaed60SHong Zhang }
338436eaed60SHong Zhang 
33856fd0887fSHong Zhang /*@
3386022c081aSHong Zhang    TSComputeCostIntegrand - Evaluates the integral function in the cost functions.
33876fd0887fSHong Zhang 
33886fd0887fSHong Zhang    Input Parameters:
33896fd0887fSHong Zhang +  ts - the TS context
33906fd0887fSHong Zhang .  t - current time
33910cf82b59SBarry Smith -  y - state vector, i.e. current solution
33926fd0887fSHong Zhang 
33936fd0887fSHong Zhang    Output Parameter:
3394abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
33956fd0887fSHong Zhang 
33966fd0887fSHong Zhang    Note:
33976fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
33986fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
33996fd0887fSHong Zhang 
34006fd0887fSHong Zhang    Level: developer
34016fd0887fSHong Zhang 
34026fd0887fSHong Zhang .keywords: TS, compute
34036fd0887fSHong Zhang 
3404dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
34056fd0887fSHong Zhang @*/
3406022c081aSHong Zhang PetscErrorCode TSComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
34076fd0887fSHong Zhang {
34086fd0887fSHong Zhang   PetscErrorCode ierr;
34096fd0887fSHong Zhang 
34106fd0887fSHong Zhang   PetscFunctionBegin;
34116fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34120cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
34136fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
34146fd0887fSHong Zhang 
34150cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3416e4680132SHong Zhang   if (ts->costintegrand) {
3417e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
34180cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
34196fd0887fSHong Zhang     PetscStackPop;
34206fd0887fSHong Zhang   } else {
34216fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
34226fd0887fSHong Zhang   }
34236fd0887fSHong Zhang 
34240cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
34256fd0887fSHong Zhang   PetscFunctionReturn(0);
34266fd0887fSHong Zhang }
34276fd0887fSHong Zhang 
342805755b9cSHong Zhang /*@
3429d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
343005755b9cSHong Zhang 
343105755b9cSHong Zhang   Collective on TS
343205755b9cSHong Zhang 
343305755b9cSHong Zhang   Input Parameters:
343405755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
343505755b9cSHong Zhang 
343605755b9cSHong Zhang   Notes:
3437d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
343805755b9cSHong Zhang   so most users would not generally call this routine themselves.
343905755b9cSHong Zhang 
344005755b9cSHong Zhang   Level: developer
344105755b9cSHong Zhang 
344205755b9cSHong Zhang .keywords: TS, sensitivity
3443d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
344405755b9cSHong Zhang @*/
34450cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
344605755b9cSHong Zhang {
344705755b9cSHong Zhang   PetscErrorCode ierr;
344805755b9cSHong Zhang 
344905755b9cSHong Zhang   PetscFunctionBegin;
345005755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34510cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
345205755b9cSHong Zhang 
345305755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
34540cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
345505755b9cSHong Zhang   PetscStackPop;
345605755b9cSHong Zhang   PetscFunctionReturn(0);
345705755b9cSHong Zhang }
345805755b9cSHong Zhang 
345905755b9cSHong Zhang /*@
3460d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
346105755b9cSHong Zhang 
346205755b9cSHong Zhang   Collective on TS
346305755b9cSHong Zhang 
346405755b9cSHong Zhang   Input Parameters:
346505755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
346605755b9cSHong Zhang 
346705755b9cSHong Zhang   Notes:
346805755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
346905755b9cSHong Zhang   so most users would not generally call this routine themselves.
347005755b9cSHong Zhang 
347105755b9cSHong Zhang   Level: developer
347205755b9cSHong Zhang 
347305755b9cSHong Zhang .keywords: TS, sensitivity
3474d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
347505755b9cSHong Zhang @*/
34760cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
347705755b9cSHong Zhang {
347805755b9cSHong Zhang   PetscErrorCode ierr;
347905755b9cSHong Zhang 
348005755b9cSHong Zhang   PetscFunctionBegin;
348105755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34820cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
348305755b9cSHong Zhang 
348405755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
34850cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
348605755b9cSHong Zhang   PetscStackPop;
348705755b9cSHong Zhang   PetscFunctionReturn(0);
348805755b9cSHong Zhang }
348905755b9cSHong Zhang 
3490ac226902SBarry Smith /*@C
3491000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
34923f2090d5SJed Brown   called once at the beginning of each time step.
3493000e7ae3SMatthew Knepley 
34943f9fe445SBarry Smith   Logically Collective on TS
3495000e7ae3SMatthew Knepley 
3496000e7ae3SMatthew Knepley   Input Parameters:
3497000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3498000e7ae3SMatthew Knepley - func - The function
3499000e7ae3SMatthew Knepley 
3500000e7ae3SMatthew Knepley   Calling sequence of func:
3501000e7ae3SMatthew Knepley . func (TS ts);
3502000e7ae3SMatthew Knepley 
3503000e7ae3SMatthew Knepley   Level: intermediate
3504000e7ae3SMatthew Knepley 
3505000e7ae3SMatthew Knepley .keywords: TS, timestep
3506dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3507000e7ae3SMatthew Knepley @*/
35087087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3509000e7ae3SMatthew Knepley {
3510000e7ae3SMatthew Knepley   PetscFunctionBegin;
35110700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3512ae60f76fSBarry Smith   ts->prestep = func;
3513000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3514000e7ae3SMatthew Knepley }
3515000e7ae3SMatthew Knepley 
351609ee8438SJed Brown /*@
35173f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
35183f2090d5SJed Brown 
35193f2090d5SJed Brown   Collective on TS
35203f2090d5SJed Brown 
35213f2090d5SJed Brown   Input Parameters:
35223f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35233f2090d5SJed Brown 
35243f2090d5SJed Brown   Notes:
35253f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
35263f2090d5SJed Brown   so most users would not generally call this routine themselves.
35273f2090d5SJed Brown 
35283f2090d5SJed Brown   Level: developer
35293f2090d5SJed Brown 
35303f2090d5SJed Brown .keywords: TS, timestep
35319be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
35323f2090d5SJed Brown @*/
35337087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
35343f2090d5SJed Brown {
35353f2090d5SJed Brown   PetscErrorCode ierr;
35363f2090d5SJed Brown 
35373f2090d5SJed Brown   PetscFunctionBegin;
35380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3539ae60f76fSBarry Smith   if (ts->prestep) {
35405efd42a4SStefano Zampini     Vec              U;
35415efd42a4SStefano Zampini     PetscObjectState sprev,spost;
35425efd42a4SStefano Zampini 
35435efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
35445efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3545ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
35465efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3547dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3548312ce896SJed Brown   }
35493f2090d5SJed Brown   PetscFunctionReturn(0);
35503f2090d5SJed Brown }
35513f2090d5SJed Brown 
3552b8123daeSJed Brown /*@C
3553b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3554b8123daeSJed Brown   called once at the beginning of each stage.
3555b8123daeSJed Brown 
3556b8123daeSJed Brown   Logically Collective on TS
3557b8123daeSJed Brown 
3558b8123daeSJed Brown   Input Parameters:
3559b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3560b8123daeSJed Brown - func - The function
3561b8123daeSJed Brown 
3562b8123daeSJed Brown   Calling sequence of func:
3563b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3564b8123daeSJed Brown 
3565b8123daeSJed Brown   Level: intermediate
3566b8123daeSJed Brown 
3567b8123daeSJed Brown   Note:
3568b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
356980275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3570b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3571b8123daeSJed Brown 
3572b8123daeSJed Brown .keywords: TS, timestep
35739be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3574b8123daeSJed Brown @*/
3575b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3576b8123daeSJed Brown {
3577b8123daeSJed Brown   PetscFunctionBegin;
3578b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3579ae60f76fSBarry Smith   ts->prestage = func;
3580b8123daeSJed Brown   PetscFunctionReturn(0);
3581b8123daeSJed Brown }
3582b8123daeSJed Brown 
35839be3e283SDebojyoti Ghosh /*@C
35849be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
35859be3e283SDebojyoti Ghosh   called once at the end of each stage.
35869be3e283SDebojyoti Ghosh 
35879be3e283SDebojyoti Ghosh   Logically Collective on TS
35889be3e283SDebojyoti Ghosh 
35899be3e283SDebojyoti Ghosh   Input Parameters:
35909be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
35919be3e283SDebojyoti Ghosh - func - The function
35929be3e283SDebojyoti Ghosh 
35939be3e283SDebojyoti Ghosh   Calling sequence of func:
35949be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
35959be3e283SDebojyoti Ghosh 
35969be3e283SDebojyoti Ghosh   Level: intermediate
35979be3e283SDebojyoti Ghosh 
35989be3e283SDebojyoti Ghosh   Note:
35999be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
360080275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
36019be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
36029be3e283SDebojyoti Ghosh 
36039be3e283SDebojyoti Ghosh .keywords: TS, timestep
36049be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
36059be3e283SDebojyoti Ghosh @*/
36069be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
36079be3e283SDebojyoti Ghosh {
36089be3e283SDebojyoti Ghosh   PetscFunctionBegin;
36099be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
36109be3e283SDebojyoti Ghosh   ts->poststage = func;
36119be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
36129be3e283SDebojyoti Ghosh }
36139be3e283SDebojyoti Ghosh 
3614c688d042SShri Abhyankar /*@C
3615c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3616c688d042SShri Abhyankar   called once at the end of each step evaluation.
3617c688d042SShri Abhyankar 
3618c688d042SShri Abhyankar   Logically Collective on TS
3619c688d042SShri Abhyankar 
3620c688d042SShri Abhyankar   Input Parameters:
3621c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3622c688d042SShri Abhyankar - func - The function
3623c688d042SShri Abhyankar 
3624c688d042SShri Abhyankar   Calling sequence of func:
3625c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3626c688d042SShri Abhyankar 
3627c688d042SShri Abhyankar   Level: intermediate
3628c688d042SShri Abhyankar 
3629c688d042SShri Abhyankar   Note:
36301785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
36311785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3632e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3633e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3634e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3635c688d042SShri Abhyankar 
3636c688d042SShri Abhyankar .keywords: TS, timestep
3637c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3638c688d042SShri Abhyankar @*/
3639c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3640c688d042SShri Abhyankar {
3641c688d042SShri Abhyankar   PetscFunctionBegin;
3642c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3643c688d042SShri Abhyankar   ts->postevaluate = func;
3644c688d042SShri Abhyankar   PetscFunctionReturn(0);
3645c688d042SShri Abhyankar }
3646c688d042SShri Abhyankar 
3647b8123daeSJed Brown /*@
3648b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3649b8123daeSJed Brown 
3650b8123daeSJed Brown   Collective on TS
3651b8123daeSJed Brown 
3652b8123daeSJed Brown   Input Parameters:
3653b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
36549be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3655b8123daeSJed Brown 
3656b8123daeSJed Brown   Notes:
3657b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3658b8123daeSJed Brown   most users would not generally call this routine themselves.
3659b8123daeSJed Brown 
3660b8123daeSJed Brown   Level: developer
3661b8123daeSJed Brown 
3662b8123daeSJed Brown .keywords: TS, timestep
36639be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3664b8123daeSJed Brown @*/
3665b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3666b8123daeSJed Brown {
3667b8123daeSJed Brown   PetscErrorCode ierr;
3668b8123daeSJed Brown 
3669b8123daeSJed Brown   PetscFunctionBegin;
3670b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3671ae60f76fSBarry Smith   if (ts->prestage) {
3672ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3673b8123daeSJed Brown   }
3674b8123daeSJed Brown   PetscFunctionReturn(0);
3675b8123daeSJed Brown }
3676b8123daeSJed Brown 
36779be3e283SDebojyoti Ghosh /*@
36789be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
36799be3e283SDebojyoti Ghosh 
36809be3e283SDebojyoti Ghosh   Collective on TS
36819be3e283SDebojyoti Ghosh 
36829be3e283SDebojyoti Ghosh   Input Parameters:
36839be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
36849be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
36859be3e283SDebojyoti Ghosh   stageindex  - Stage number
36869be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
36879be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
36889be3e283SDebojyoti Ghosh 
36899be3e283SDebojyoti Ghosh   Notes:
36909be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
36919be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
36929be3e283SDebojyoti Ghosh 
36939be3e283SDebojyoti Ghosh   Level: developer
36949be3e283SDebojyoti Ghosh 
36959be3e283SDebojyoti Ghosh .keywords: TS, timestep
36969be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
36979be3e283SDebojyoti Ghosh @*/
36989be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
36999be3e283SDebojyoti Ghosh {
37009be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
37019be3e283SDebojyoti Ghosh 
37029be3e283SDebojyoti Ghosh   PetscFunctionBegin;
37039be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37044beae5d8SLisandro Dalcin   if (ts->poststage) {
37059be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
37069be3e283SDebojyoti Ghosh   }
37079be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
37089be3e283SDebojyoti Ghosh }
37099be3e283SDebojyoti Ghosh 
3710c688d042SShri Abhyankar /*@
3711c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3712c688d042SShri Abhyankar 
3713c688d042SShri Abhyankar   Collective on TS
3714c688d042SShri Abhyankar 
3715c688d042SShri Abhyankar   Input Parameters:
3716c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3717c688d042SShri Abhyankar 
3718c688d042SShri Abhyankar   Notes:
3719c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3720c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3721c688d042SShri Abhyankar 
3722c688d042SShri Abhyankar   Level: developer
3723c688d042SShri Abhyankar 
3724c688d042SShri Abhyankar .keywords: TS, timestep
3725c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3726c688d042SShri Abhyankar @*/
3727c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3728c688d042SShri Abhyankar {
3729c688d042SShri Abhyankar   PetscErrorCode ierr;
3730c688d042SShri Abhyankar 
3731c688d042SShri Abhyankar   PetscFunctionBegin;
3732c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3733c688d042SShri Abhyankar   if (ts->postevaluate) {
3734dcb233daSLisandro Dalcin     Vec              U;
3735dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3736dcb233daSLisandro Dalcin 
3737dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3738dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3739c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3740dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3741dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3742c688d042SShri Abhyankar   }
3743c688d042SShri Abhyankar   PetscFunctionReturn(0);
3744c688d042SShri Abhyankar }
3745c688d042SShri Abhyankar 
3746ac226902SBarry Smith /*@C
3747000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
37483f2090d5SJed Brown   called once at the end of each time step.
3749000e7ae3SMatthew Knepley 
37503f9fe445SBarry Smith   Logically Collective on TS
3751000e7ae3SMatthew Knepley 
3752000e7ae3SMatthew Knepley   Input Parameters:
3753000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3754000e7ae3SMatthew Knepley - func - The function
3755000e7ae3SMatthew Knepley 
3756000e7ae3SMatthew Knepley   Calling sequence of func:
3757b8123daeSJed Brown $ func (TS ts);
3758000e7ae3SMatthew Knepley 
37591785ff2aSShri Abhyankar   Notes:
37601785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
37611785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
37621785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
37631785ff2aSShri Abhyankar 
3764000e7ae3SMatthew Knepley   Level: intermediate
3765000e7ae3SMatthew Knepley 
3766000e7ae3SMatthew Knepley .keywords: TS, timestep
3767dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3768000e7ae3SMatthew Knepley @*/
37697087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3770000e7ae3SMatthew Knepley {
3771000e7ae3SMatthew Knepley   PetscFunctionBegin;
37720700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3773ae60f76fSBarry Smith   ts->poststep = func;
3774000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3775000e7ae3SMatthew Knepley }
3776000e7ae3SMatthew Knepley 
377709ee8438SJed Brown /*@
37783f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
37793f2090d5SJed Brown 
37803f2090d5SJed Brown   Collective on TS
37813f2090d5SJed Brown 
37823f2090d5SJed Brown   Input Parameters:
37833f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
37843f2090d5SJed Brown 
37853f2090d5SJed Brown   Notes:
37863f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
37873f2090d5SJed Brown   so most users would not generally call this routine themselves.
37883f2090d5SJed Brown 
37893f2090d5SJed Brown   Level: developer
37903f2090d5SJed Brown 
37913f2090d5SJed Brown .keywords: TS, timestep
37923f2090d5SJed Brown @*/
37937087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
37943f2090d5SJed Brown {
37953f2090d5SJed Brown   PetscErrorCode ierr;
37963f2090d5SJed Brown 
37973f2090d5SJed Brown   PetscFunctionBegin;
37980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3799ae60f76fSBarry Smith   if (ts->poststep) {
38005efd42a4SStefano Zampini     Vec              U;
38015efd42a4SStefano Zampini     PetscObjectState sprev,spost;
38025efd42a4SStefano Zampini 
38035efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
38045efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3805ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
38065efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3807dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
380872ac3e02SJed Brown   }
38093f2090d5SJed Brown   PetscFunctionReturn(0);
38103f2090d5SJed Brown }
38113f2090d5SJed Brown 
3812d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3813d763cef2SBarry Smith 
3814d763cef2SBarry Smith /*@C
3815a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3816d763cef2SBarry Smith    timestep to display the iteration's  progress.
3817d763cef2SBarry Smith 
38183f9fe445SBarry Smith    Logically Collective on TS
3819d763cef2SBarry Smith 
3820d763cef2SBarry Smith    Input Parameters:
3821d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3822e213d8f1SJed Brown .  monitor - monitoring routine
3823329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
38240298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3825b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
38260298fd71SBarry Smith           (may be NULL)
3827d763cef2SBarry Smith 
3828e213d8f1SJed Brown    Calling sequence of monitor:
3829dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3830d763cef2SBarry Smith 
3831d763cef2SBarry Smith +    ts - the TS context
383263e21af5SBarry 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)
38331f06c33eSBarry Smith .    time - current time
38340910c330SBarry Smith .    u - current iterate
3835d763cef2SBarry Smith -    mctx - [optional] monitoring context
3836d763cef2SBarry Smith 
3837d763cef2SBarry Smith    Notes:
3838d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3839d763cef2SBarry Smith    already has been loaded.
3840d763cef2SBarry Smith 
3841025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3842025f1a04SBarry Smith 
3843d763cef2SBarry Smith    Level: intermediate
3844d763cef2SBarry Smith 
3845d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3846d763cef2SBarry Smith 
3847a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3848d763cef2SBarry Smith @*/
3849c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3850d763cef2SBarry Smith {
385178064530SBarry Smith   PetscErrorCode ierr;
385278064530SBarry Smith   PetscInt       i;
385378064530SBarry Smith   PetscBool      identical;
385478064530SBarry Smith 
3855d763cef2SBarry Smith   PetscFunctionBegin;
38560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
385778064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
385878064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
385978064530SBarry Smith     if (identical) PetscFunctionReturn(0);
386078064530SBarry Smith   }
386117186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3862d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
38638704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3864d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3865d763cef2SBarry Smith   PetscFunctionReturn(0);
3866d763cef2SBarry Smith }
3867d763cef2SBarry Smith 
3868d763cef2SBarry Smith /*@C
3869a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3870d763cef2SBarry Smith 
38713f9fe445SBarry Smith    Logically Collective on TS
3872d763cef2SBarry Smith 
3873d763cef2SBarry Smith    Input Parameters:
3874d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3875d763cef2SBarry Smith 
3876d763cef2SBarry Smith    Notes:
3877d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3878d763cef2SBarry Smith 
3879d763cef2SBarry Smith    Level: intermediate
3880d763cef2SBarry Smith 
3881d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3882d763cef2SBarry Smith 
3883a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3884d763cef2SBarry Smith @*/
38857087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3886d763cef2SBarry Smith {
3887d952e501SBarry Smith   PetscErrorCode ierr;
3888d952e501SBarry Smith   PetscInt       i;
3889d952e501SBarry Smith 
3890d763cef2SBarry Smith   PetscFunctionBegin;
38910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3892d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
38938704b422SBarry Smith     if (ts->monitordestroy[i]) {
38948704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3895d952e501SBarry Smith     }
3896d952e501SBarry Smith   }
3897d763cef2SBarry Smith   ts->numbermonitors = 0;
3898d763cef2SBarry Smith   PetscFunctionReturn(0);
3899d763cef2SBarry Smith }
3900d763cef2SBarry Smith 
3901721cd6eeSBarry Smith /*@C
3902721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
39035516499fSSatish Balay 
39045516499fSSatish Balay    Level: intermediate
390541251cbbSSatish Balay 
39065516499fSSatish Balay .keywords: TS, set, monitor
39075516499fSSatish Balay 
390863e21af5SBarry Smith .seealso:  TSMonitorSet()
390941251cbbSSatish Balay @*/
3910721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3911d763cef2SBarry Smith {
3912dfbe8321SBarry Smith   PetscErrorCode ierr;
3913721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
391441aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3915d132466eSBarry Smith 
3916d763cef2SBarry Smith   PetscFunctionBegin;
39174d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
391841aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
391941aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3920721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
392141aca3d6SBarry Smith   if (iascii) {
3922649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
392363e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
392463e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
392563e21af5SBarry Smith     } else {
39268392e04aSShri 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);
392763e21af5SBarry Smith     }
3928649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
392941aca3d6SBarry Smith   } else if (ibinary) {
393041aca3d6SBarry Smith     PetscMPIInt rank;
393141aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
393241aca3d6SBarry Smith     if (!rank) {
3933450a797fSBarry Smith       PetscBool skipHeader;
3934450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3935450a797fSBarry Smith 
3936450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3937450a797fSBarry Smith       if (!skipHeader) {
3938450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3939450a797fSBarry Smith        }
394041aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
394141aca3d6SBarry Smith     } else {
394241aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
394341aca3d6SBarry Smith     }
394441aca3d6SBarry Smith   }
3945721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3946d763cef2SBarry Smith   PetscFunctionReturn(0);
3947d763cef2SBarry Smith }
3948d763cef2SBarry Smith 
39499110b2e7SHong Zhang /*@C
39509110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
39519110b2e7SHong Zhang    timestep to display the iteration's  progress.
39529110b2e7SHong Zhang 
39539110b2e7SHong Zhang    Logically Collective on TS
39549110b2e7SHong Zhang 
39559110b2e7SHong Zhang    Input Parameters:
39569110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
39579110b2e7SHong Zhang .  adjointmonitor - monitoring routine
39589110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
39599110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
39609110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
39619110b2e7SHong Zhang           (may be NULL)
39629110b2e7SHong Zhang 
39639110b2e7SHong Zhang    Calling sequence of monitor:
39649110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
39659110b2e7SHong Zhang 
39669110b2e7SHong Zhang +    ts - the TS context
39679110b2e7SHong Zhang .    steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have
39689110b2e7SHong Zhang                                been interpolated to)
39699110b2e7SHong Zhang .    time - current time
39709110b2e7SHong Zhang .    u - current iterate
39719110b2e7SHong Zhang .    numcost - number of cost functionos
39729110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
39739110b2e7SHong Zhang .    mu - sensitivities to parameters
39749110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
39759110b2e7SHong Zhang 
39769110b2e7SHong Zhang    Notes:
39779110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
39789110b2e7SHong Zhang    already has been loaded.
39799110b2e7SHong Zhang 
39809110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
39819110b2e7SHong Zhang 
39829110b2e7SHong Zhang    Level: intermediate
39839110b2e7SHong Zhang 
39849110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
39859110b2e7SHong Zhang 
39869110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
39879110b2e7SHong Zhang @*/
39889110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
39899110b2e7SHong Zhang {
399078064530SBarry Smith   PetscErrorCode ierr;
399178064530SBarry Smith   PetscInt       i;
399278064530SBarry Smith   PetscBool      identical;
399378064530SBarry Smith 
39949110b2e7SHong Zhang   PetscFunctionBegin;
39959110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
399678064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
399778064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
399878064530SBarry Smith     if (identical) PetscFunctionReturn(0);
399978064530SBarry Smith   }
40009110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
40019110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
40029110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
40039110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
40049110b2e7SHong Zhang   PetscFunctionReturn(0);
40059110b2e7SHong Zhang }
40069110b2e7SHong Zhang 
40079110b2e7SHong Zhang /*@C
40089110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
40099110b2e7SHong Zhang 
40109110b2e7SHong Zhang    Logically Collective on TS
40119110b2e7SHong Zhang 
40129110b2e7SHong Zhang    Input Parameters:
40139110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
40149110b2e7SHong Zhang 
40159110b2e7SHong Zhang    Notes:
40169110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
40179110b2e7SHong Zhang 
40189110b2e7SHong Zhang    Level: intermediate
40199110b2e7SHong Zhang 
40209110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
40219110b2e7SHong Zhang 
40229110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
40239110b2e7SHong Zhang @*/
40249110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
40259110b2e7SHong Zhang {
40269110b2e7SHong Zhang   PetscErrorCode ierr;
40279110b2e7SHong Zhang   PetscInt       i;
40289110b2e7SHong Zhang 
40299110b2e7SHong Zhang   PetscFunctionBegin;
40309110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40319110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
40329110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
40339110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
40349110b2e7SHong Zhang     }
40359110b2e7SHong Zhang   }
40369110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
40379110b2e7SHong Zhang   PetscFunctionReturn(0);
40389110b2e7SHong Zhang }
40399110b2e7SHong Zhang 
4040721cd6eeSBarry Smith /*@C
4041721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
4042110eb670SHong Zhang 
4043110eb670SHong Zhang    Level: intermediate
4044110eb670SHong Zhang 
4045110eb670SHong Zhang .keywords: TS, set, monitor
4046110eb670SHong Zhang 
4047110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
4048110eb670SHong Zhang @*/
4049721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
4050110eb670SHong Zhang {
4051110eb670SHong Zhang   PetscErrorCode ierr;
4052721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
4053110eb670SHong Zhang 
4054110eb670SHong Zhang   PetscFunctionBegin;
4055110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
4056721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
4057110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
4058110eb670SHong Zhang   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr);
4059110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
4060721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4061110eb670SHong Zhang   PetscFunctionReturn(0);
4062110eb670SHong Zhang }
4063110eb670SHong Zhang 
4064cd652676SJed Brown /*@
4065cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
4066cd652676SJed Brown 
4067cd652676SJed Brown    Collective on TS
4068cd652676SJed Brown 
4069cd652676SJed Brown    Input Argument:
4070cd652676SJed Brown +  ts - time stepping context
4071cd652676SJed Brown -  t - time to interpolate to
4072cd652676SJed Brown 
4073cd652676SJed Brown    Output Argument:
40740910c330SBarry Smith .  U - state at given time
4075cd652676SJed Brown 
4076cd652676SJed Brown    Level: intermediate
4077cd652676SJed Brown 
4078cd652676SJed Brown    Developer Notes:
4079cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
4080cd652676SJed Brown 
4081cd652676SJed Brown .keywords: TS, set
4082cd652676SJed Brown 
4083874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
4084cd652676SJed Brown @*/
40850910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
4086cd652676SJed Brown {
4087cd652676SJed Brown   PetscErrorCode ierr;
4088cd652676SJed Brown 
4089cd652676SJed Brown   PetscFunctionBegin;
4090cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4091b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4092be5899b3SLisandro 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);
4093ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
40940910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
4095cd652676SJed Brown   PetscFunctionReturn(0);
4096cd652676SJed Brown }
4097cd652676SJed Brown 
4098d763cef2SBarry Smith /*@
40996d9e5789SSean Farley    TSStep - Steps one time step
4100d763cef2SBarry Smith 
4101d763cef2SBarry Smith    Collective on TS
4102d763cef2SBarry Smith 
4103d763cef2SBarry Smith    Input Parameter:
4104d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4105d763cef2SBarry Smith 
410627829d71SBarry Smith    Level: developer
4107d763cef2SBarry Smith 
4108b8123daeSJed Brown    Notes:
410927829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
411027829d71SBarry Smith 
4111b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
4112b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
4113b8123daeSJed Brown 
411419eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
411519eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
411625cb2221SBarry Smith 
4117d763cef2SBarry Smith .keywords: TS, timestep, solve
4118d763cef2SBarry Smith 
41199be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
4120d763cef2SBarry Smith @*/
4121193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
4122d763cef2SBarry Smith {
4123dfbe8321SBarry Smith   PetscErrorCode   ierr;
4124fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
4125be5899b3SLisandro Dalcin   PetscReal        ptime;
4126d763cef2SBarry Smith 
4127d763cef2SBarry Smith   PetscFunctionBegin;
41280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4129fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
4130fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
4131fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
4132fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
4133fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
4134fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
4135302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
4136fffbeea8SBarry Smith 
4137d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
413868bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4139d405a339SMatthew Knepley 
4140ef85077eSLisandro 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>");
4141a6772fa2SLisandro 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()");
4142be5899b3SLisandro 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");
4143a6772fa2SLisandro Dalcin 
4144be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
4145be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
41462808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
4147e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
4148d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
4149193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
4150d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
4151be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
41522808aa04SLisandro Dalcin   ts->steps++;
4153be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
415428d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
4155362cd11cSLisandro Dalcin 
4156362cd11cSLisandro Dalcin   if (ts->reason < 0) {
4157cef5090cSJed Brown     if (ts->errorifstepfailed) {
415808c7845fSBarry Smith       if (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]);
415908c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
4160d2daff3dSHong Zhang     }
416108c7845fSBarry Smith   } else if (!ts->reason) {
416208c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
416308c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
416408c7845fSBarry Smith   }
416508c7845fSBarry Smith   PetscFunctionReturn(0);
416608c7845fSBarry Smith }
416708c7845fSBarry Smith 
416808c7845fSBarry Smith /*@
4169b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
417008c7845fSBarry Smith 
417108c7845fSBarry Smith    Collective on TS
417208c7845fSBarry Smith 
417308c7845fSBarry Smith    Input Parameter:
417408c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
417508c7845fSBarry Smith 
41766a4d4014SLisandro Dalcin    Level: intermediate
41776a4d4014SLisandro Dalcin 
4178b957a604SHong Zhang .keywords: TS, adjoint, step
41796a4d4014SLisandro Dalcin 
4180b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
41816a4d4014SLisandro Dalcin @*/
418208c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
41836a4d4014SLisandro Dalcin {
418408c7845fSBarry Smith   DM               dm;
41856a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
41866a4d4014SLisandro Dalcin 
41876a4d4014SLisandro Dalcin   PetscFunctionBegin;
41884a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
418908c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
419008c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
4191d763cef2SBarry Smith 
4192685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
4193d763cef2SBarry Smith 
4194be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
4195be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
419642f2b339SBarry Smith   if (!ts->ops->adjointstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of  %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name);
4197be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
419842f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
41998d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
42002808aa04SLisandro Dalcin   ts->adjoint_steps++; ts->steps--;
4201362cd11cSLisandro Dalcin 
4202362cd11cSLisandro Dalcin   if (ts->reason < 0) {
4203cef5090cSJed Brown     if (ts->errorifstepfailed) {
42046c4ed002SBarry Smith       if (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]);
42056c4ed002SBarry Smith       else if (ts->reason == TS_DIVERGED_STEP_REJECTED) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
42066c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
4207cef5090cSJed Brown     }
4208362cd11cSLisandro Dalcin   } else if (!ts->reason) {
42092808aa04SLisandro Dalcin     if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
4210362cd11cSLisandro Dalcin   }
4211d763cef2SBarry Smith   PetscFunctionReturn(0);
4212d763cef2SBarry Smith }
4213d763cef2SBarry Smith 
42147cbde773SLisandro Dalcin /*@
42157cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
42167cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
42177cbde773SLisandro Dalcin 
42187cbde773SLisandro Dalcin    Collective on TS
42197cbde773SLisandro Dalcin 
42207cbde773SLisandro Dalcin    Input Arguments:
42217cbde773SLisandro Dalcin +  ts - time stepping context
42227cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
42237cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
42247cbde773SLisandro Dalcin 
42257cbde773SLisandro Dalcin    Output Arguments:
42267cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
42277cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
42287cbde773SLisandro Dalcin 
42297cbde773SLisandro Dalcin    Level: advanced
42307cbde773SLisandro Dalcin 
42317cbde773SLisandro Dalcin    Notes:
42327cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
42337cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
42347cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
42357cbde773SLisandro Dalcin 
42367cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
42377cbde773SLisandro Dalcin @*/
42387cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
42397cbde773SLisandro Dalcin {
42407cbde773SLisandro Dalcin   PetscErrorCode ierr;
42417cbde773SLisandro Dalcin 
42427cbde773SLisandro Dalcin   PetscFunctionBegin;
42437cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42447cbde773SLisandro Dalcin   PetscValidType(ts,1);
42457cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
42467cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
42477cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
42487cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
42497cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
42507cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
42517cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
42527cbde773SLisandro Dalcin   PetscFunctionReturn(0);
42537cbde773SLisandro Dalcin }
42547cbde773SLisandro Dalcin 
425505175c85SJed Brown /*@
425605175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
425705175c85SJed Brown 
42581c3436cfSJed Brown    Collective on TS
425905175c85SJed Brown 
426005175c85SJed Brown    Input Arguments:
42611c3436cfSJed Brown +  ts - time stepping context
42621c3436cfSJed Brown .  order - desired order of accuracy
42630298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
426405175c85SJed Brown 
426505175c85SJed Brown    Output Arguments:
42660910c330SBarry Smith .  U - state at the end of the current step
426705175c85SJed Brown 
426805175c85SJed Brown    Level: advanced
426905175c85SJed Brown 
4270108c343cSJed Brown    Notes:
4271108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
4272108c343cSJed 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.
4273108c343cSJed Brown 
42741c3436cfSJed Brown .seealso: TSStep(), TSAdapt
427505175c85SJed Brown @*/
42760910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
427705175c85SJed Brown {
427805175c85SJed Brown   PetscErrorCode ierr;
427905175c85SJed Brown 
428005175c85SJed Brown   PetscFunctionBegin;
428105175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
428205175c85SJed Brown   PetscValidType(ts,1);
42830910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4284ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
42850910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
428605175c85SJed Brown   PetscFunctionReturn(0);
428705175c85SJed Brown }
428805175c85SJed Brown 
4289b1cb36f3SHong Zhang /*@
4290b1cb36f3SHong Zhang    TSForwardCostIntegral - Evaluate the cost integral in the forward run.
4291b1cb36f3SHong Zhang 
4292b1cb36f3SHong Zhang    Collective on TS
4293b1cb36f3SHong Zhang 
4294b1cb36f3SHong Zhang    Input Arguments:
4295b1cb36f3SHong Zhang .  ts - time stepping context
4296b1cb36f3SHong Zhang 
4297b1cb36f3SHong Zhang    Level: advanced
4298b1cb36f3SHong Zhang 
4299b1cb36f3SHong Zhang    Notes:
4300b1cb36f3SHong Zhang    This function cannot be called until TSStep() has been completed.
4301b1cb36f3SHong Zhang 
4302b1cb36f3SHong Zhang .seealso: TSSolve(), TSAdjointCostIntegral()
4303b1cb36f3SHong Zhang @*/
4304b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
4305b1cb36f3SHong Zhang {
4306b1cb36f3SHong Zhang   PetscErrorCode ierr;
4307b1cb36f3SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4308b1cb36f3SHong Zhang   if (!ts->ops->forwardintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the forward run",((PetscObject)ts)->type_name);
4309b1cb36f3SHong Zhang   ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
4310b1cb36f3SHong Zhang   PetscFunctionReturn(0);
4311b1cb36f3SHong Zhang }
4312d67e68b7SBarry Smith 
4313d763cef2SBarry Smith /*@
4314d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
4315d763cef2SBarry Smith 
4316d763cef2SBarry Smith    Collective on TS
4317d763cef2SBarry Smith 
4318d763cef2SBarry Smith    Input Parameter:
4319d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
432063e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
432163e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
43225a3a76d0SJed Brown 
4323d763cef2SBarry Smith    Level: beginner
4324d763cef2SBarry Smith 
43255a3a76d0SJed Brown    Notes:
43265a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
43275a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
43285a3a76d0SJed Brown    stepped over the final time.
43295a3a76d0SJed Brown 
4330d763cef2SBarry Smith .keywords: TS, timestep, solve
4331d763cef2SBarry Smith 
433263e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4333d763cef2SBarry Smith @*/
4334cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4335d763cef2SBarry Smith {
4336b06615a5SLisandro Dalcin   Vec               solution;
4337d763cef2SBarry Smith   PetscErrorCode    ierr;
4338d763cef2SBarry Smith 
4339d763cef2SBarry Smith   PetscFunctionBegin;
4340d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4341f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4342feed9e9dSBarry Smith 
4343ee41a567SStefano 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 */
43440910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4345b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4346b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4347b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
43485a3a76d0SJed Brown     }
43490910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4350715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
4351bbd56ea5SKarl Rupp   } else if (u) {
43520910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
43535a3a76d0SJed Brown   }
4354b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
435568bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4356a6772fa2SLisandro Dalcin 
4357ef85077eSLisandro 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>");
4358a6772fa2SLisandro 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()");
4359a6772fa2SLisandro 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");
4360a6772fa2SLisandro Dalcin 
4361715f1b00SHong Zhang   if (ts->forward_solve) {
4362715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
4363715f1b00SHong Zhang   }
4364715f1b00SHong Zhang 
4365e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4366715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
4367e7069c78SShri      TSTrajectory to incorrectly save the output files
4368e7069c78SShri   */
4369715f1b00SHong Zhang   /* reset time step and iteration counters */
43702808aa04SLisandro Dalcin   if (!ts->steps) {
43715ef26d82SJed Brown     ts->ksp_its           = 0;
43725ef26d82SJed Brown     ts->snes_its          = 0;
4373c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
4374c610991cSLisandro Dalcin     ts->reject            = 0;
43752808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
43762808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
43772808aa04SLisandro Dalcin   }
437810bc0e4dSStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime;
4379193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
4380193ac0bcSJed Brown 
4381ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4382f05ece33SBarry Smith 
4383193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4384193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4385a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4386cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4387a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4388be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4389db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
4390db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4391e7069c78SShri 
43922808aa04SLisandro Dalcin     if (!ts->steps) {
43932808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43946427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43952808aa04SLisandro Dalcin     }
43966427ac75SLisandro Dalcin 
4397e1a7a14fSJed Brown     while (!ts->reason) {
43982808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
43999687d888SLisandro Dalcin       if (!ts->steprollback) {
44009687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
44019687d888SLisandro Dalcin       }
4402193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4403f3b1f45cSBarry Smith       if (ts->testjacobian) {
4404f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
4405f3b1f45cSBarry Smith       }
4406f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
4407f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
4408f3b1f45cSBarry Smith       }
4409e783b05fSHong Zhang       if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
4410b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4411b1cb36f3SHong Zhang       }
441258818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
4413715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
4414715f1b00SHong Zhang       }
441510b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4416e783b05fSHong 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. */
441758818c2dSLisandro Dalcin       if (ts->steprollback) {
441858818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
441958818c2dSLisandro Dalcin       }
4420e783b05fSHong Zhang       if (!ts->steprollback) {
44212808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
44221eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4423aeb4809dSShri Abhyankar       }
4424193ac0bcSJed Brown     }
44252808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
44266427ac75SLisandro Dalcin 
442749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
44280910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4429cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4430b06615a5SLisandro Dalcin       solution = u;
443163e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
44320574a7fbSJed Brown     } else {
4433ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4434cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4435b06615a5SLisandro Dalcin       solution = ts->vec_sol;
44360574a7fbSJed Brown     }
4437193ac0bcSJed Brown   }
4438d2daff3dSHong Zhang 
4439ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
444063e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
444156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4442ef222394SBarry Smith   if (ts->adjoint_solve) {
4443ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
44442b0a91c0SBarry Smith   }
4445d763cef2SBarry Smith   PetscFunctionReturn(0);
4446d763cef2SBarry Smith }
4447d763cef2SBarry Smith 
4448b1cb36f3SHong Zhang /*@
4449b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4450b1cb36f3SHong Zhang 
4451b1cb36f3SHong Zhang  Collective on TS
4452b1cb36f3SHong Zhang 
4453b1cb36f3SHong Zhang  Input Arguments:
4454b1cb36f3SHong Zhang  .  ts - time stepping context
4455b1cb36f3SHong Zhang 
4456b1cb36f3SHong Zhang  Level: advanced
4457b1cb36f3SHong Zhang 
4458b1cb36f3SHong Zhang  Notes:
4459b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4460b1cb36f3SHong Zhang 
4461b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4462b1cb36f3SHong Zhang  @*/
4463b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4464b1cb36f3SHong Zhang {
4465b1cb36f3SHong Zhang     PetscErrorCode ierr;
4466b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4467b1cb36f3SHong Zhang     if (!ts->ops->adjointintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the adjoint run",((PetscObject)ts)->type_name);
4468b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4469b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4470b1cb36f3SHong Zhang }
4471b1cb36f3SHong Zhang 
4472c88f2d44SBarry Smith /*@
4473c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4474c88f2d44SBarry Smith 
4475c88f2d44SBarry Smith    Collective on TS
4476c88f2d44SBarry Smith 
4477c88f2d44SBarry Smith    Input Parameter:
44782c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4479c88f2d44SBarry Smith 
4480e87fd094SBarry Smith    Options Database:
4481715f1b00SHong Zhang . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial values
4482e87fd094SBarry Smith 
4483c88f2d44SBarry Smith    Level: intermediate
4484c88f2d44SBarry Smith 
4485c88f2d44SBarry Smith    Notes:
4486c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4487c88f2d44SBarry Smith 
448873b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
448973b18844SBarry Smith 
4490c88f2d44SBarry Smith .keywords: TS, timestep, solve
4491c88f2d44SBarry Smith 
4492b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4493c88f2d44SBarry Smith @*/
44942c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4495c88f2d44SBarry Smith {
4496c88f2d44SBarry Smith   PetscErrorCode    ierr;
4497c88f2d44SBarry Smith 
4498c88f2d44SBarry Smith   PetscFunctionBegin;
4499c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4500c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
450168bece0bSHong Zhang 
4502c88f2d44SBarry Smith   /* reset time step and iteration counters */
45032808aa04SLisandro Dalcin   ts->adjoint_steps     = 0;
4504c88f2d44SBarry Smith   ts->ksp_its           = 0;
4505c88f2d44SBarry Smith   ts->snes_its          = 0;
4506c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4507c88f2d44SBarry Smith   ts->reject            = 0;
4508c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4509c88f2d44SBarry Smith 
45102808aa04SLisandro Dalcin   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->steps;
45112808aa04SLisandro Dalcin   if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
4512c88f2d44SBarry Smith 
4513c88f2d44SBarry Smith   while (!ts->reason) {
45142808aa04SLisandro Dalcin     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);CHKERRQ(ierr);
45152808aa04SLisandro Dalcin     ierr = TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
45166427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4517616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4518b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4519b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4520b1cb36f3SHong Zhang     }
4521c88f2d44SBarry Smith   }
45222808aa04SLisandro Dalcin   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);CHKERRQ(ierr);
45232808aa04SLisandro Dalcin   ierr = TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4524c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4525e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4526685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
452711e1d5c3SBarry Smith   ts->adjoint_max_steps = 0;
4528c88f2d44SBarry Smith   PetscFunctionReturn(0);
4529c88f2d44SBarry Smith }
4530c88f2d44SBarry Smith 
45317db568b7SBarry Smith /*@C
4532228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4533228d79bcSJed Brown 
4534228d79bcSJed Brown    Collective on TS
4535228d79bcSJed Brown 
4536228d79bcSJed Brown    Input Parameters:
4537228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4538228d79bcSJed Brown .  step - step number that has just completed
4539228d79bcSJed Brown .  ptime - model time of the state
45400910c330SBarry Smith -  u - state at the current model time
4541228d79bcSJed Brown 
4542228d79bcSJed Brown    Notes:
45437db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
45447db568b7SBarry Smith    Users would almost never call this routine directly.
4545228d79bcSJed Brown 
454663e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
454763e21af5SBarry Smith 
45487db568b7SBarry Smith    Level: developer
4549228d79bcSJed Brown 
4550228d79bcSJed Brown .keywords: TS, timestep
4551228d79bcSJed Brown @*/
45520910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4553d763cef2SBarry Smith {
4554d6152f81SLisandro Dalcin   DM             dm;
4555a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4556d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4557d763cef2SBarry Smith 
4558d763cef2SBarry Smith   PetscFunctionBegin;
4559b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4560b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4561d6152f81SLisandro Dalcin 
4562d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4563d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4564d6152f81SLisandro Dalcin 
45655edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4566d763cef2SBarry Smith   for (i=0; i<n; i++) {
45670910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4568d763cef2SBarry Smith   }
45695edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4570d763cef2SBarry Smith   PetscFunctionReturn(0);
4571d763cef2SBarry Smith }
4572d763cef2SBarry Smith 
45737db568b7SBarry Smith /*@C
45749110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
45759110b2e7SHong Zhang 
45769110b2e7SHong Zhang    Collective on TS
45779110b2e7SHong Zhang 
45789110b2e7SHong Zhang    Input Parameters:
45799110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
45809110b2e7SHong Zhang .  step - step number that has just completed
45819110b2e7SHong Zhang .  ptime - model time of the state
45829110b2e7SHong Zhang .  u - state at the current model time
45839110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
45849110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
45859110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
45869110b2e7SHong Zhang 
45879110b2e7SHong Zhang    Notes:
45887db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
45897db568b7SBarry Smith    Users would almost never call this routine directly.
45909110b2e7SHong Zhang 
45917db568b7SBarry Smith    Level: developer
45929110b2e7SHong Zhang 
45939110b2e7SHong Zhang .keywords: TS, timestep
45949110b2e7SHong Zhang @*/
45959110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
45969110b2e7SHong Zhang {
45979110b2e7SHong Zhang   PetscErrorCode ierr;
45989110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
45999110b2e7SHong Zhang 
46009110b2e7SHong Zhang   PetscFunctionBegin;
46019110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46029110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
46039110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
46049110b2e7SHong Zhang   for (i=0; i<n; i++) {
46059110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
46069110b2e7SHong Zhang   }
46079110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
46089110b2e7SHong Zhang   PetscFunctionReturn(0);
46099110b2e7SHong Zhang }
46109110b2e7SHong Zhang 
4611d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4612d763cef2SBarry Smith /*@C
46137db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4614a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4615d763cef2SBarry Smith 
4616d763cef2SBarry Smith    Collective on TS
4617d763cef2SBarry Smith 
4618d763cef2SBarry Smith    Input Parameters:
4619d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4620d763cef2SBarry Smith .  label - the title to put in the title bar
46217c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4622a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4623a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4624d763cef2SBarry Smith 
4625d763cef2SBarry Smith    Output Parameter:
46260b039ecaSBarry Smith .  ctx - the context
4627d763cef2SBarry Smith 
4628d763cef2SBarry Smith    Options Database Key:
46294f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
46308b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
46317db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
46327db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
46337db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
46347db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4635b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4636d763cef2SBarry Smith 
4637d763cef2SBarry Smith    Notes:
4638a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4639d763cef2SBarry Smith 
46407db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
46417db568b7SBarry Smith 
46427db568b7SBarry 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
46437db568b7SBarry 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
46447db568b7SBarry Smith    as the first argument.
46457db568b7SBarry Smith 
46467db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
46477db568b7SBarry Smith 
4648d763cef2SBarry Smith    Level: intermediate
4649d763cef2SBarry Smith 
46507db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4651d763cef2SBarry Smith 
46527db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
46537db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
46547db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
46557db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
46567db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
46577c922b88SBarry Smith 
4658d763cef2SBarry Smith @*/
4659a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4660d763cef2SBarry Smith {
46617f52daa2SLisandro Dalcin   PetscDraw      draw;
4662dfbe8321SBarry Smith   PetscErrorCode ierr;
4663d763cef2SBarry Smith 
4664d763cef2SBarry Smith   PetscFunctionBegin;
4665b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
46667f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
46677f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
46687f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4669287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
46707f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4671a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4672d763cef2SBarry Smith   PetscFunctionReturn(0);
4673d763cef2SBarry Smith }
4674d763cef2SBarry Smith 
4675b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4676d763cef2SBarry Smith {
46770b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4678c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4679dfbe8321SBarry Smith   PetscErrorCode ierr;
4680d763cef2SBarry Smith 
4681d763cef2SBarry Smith   PetscFunctionBegin;
468263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4683b06615a5SLisandro Dalcin   if (!step) {
4684a9f9c1f6SBarry Smith     PetscDrawAxis axis;
46858b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4686a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
46878b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4688a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4689a9f9c1f6SBarry Smith   }
4690c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
46918b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
46920b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4693b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
46940b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
46956934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
46963923b477SBarry Smith   }
4697d763cef2SBarry Smith   PetscFunctionReturn(0);
4698d763cef2SBarry Smith }
4699d763cef2SBarry Smith 
4700d763cef2SBarry Smith /*@C
4701a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4702a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4703d763cef2SBarry Smith 
47040b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4705d763cef2SBarry Smith 
4706d763cef2SBarry Smith    Input Parameter:
47070b039ecaSBarry Smith .  ctx - the monitor context
4708d763cef2SBarry Smith 
4709d763cef2SBarry Smith    Level: intermediate
4710d763cef2SBarry Smith 
4711d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4712d763cef2SBarry Smith 
47134f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4714d763cef2SBarry Smith @*/
4715a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4716d763cef2SBarry Smith {
4717dfbe8321SBarry Smith   PetscErrorCode ierr;
4718d763cef2SBarry Smith 
4719d763cef2SBarry Smith   PetscFunctionBegin;
47207684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
47217684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
47227684fa3eSBarry Smith   }
47230b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
472431152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4725387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4726387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4727387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
47280b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4729d763cef2SBarry Smith   PetscFunctionReturn(0);
4730d763cef2SBarry Smith }
4731d763cef2SBarry Smith 
4732d763cef2SBarry Smith /*@
4733b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4734d763cef2SBarry Smith 
4735d763cef2SBarry Smith    Not Collective
4736d763cef2SBarry Smith 
4737d763cef2SBarry Smith    Input Parameter:
4738d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4739d763cef2SBarry Smith 
4740d763cef2SBarry Smith    Output Parameter:
474119eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4742d763cef2SBarry Smith 
4743d763cef2SBarry Smith    Level: beginner
4744d763cef2SBarry Smith 
4745b8123daeSJed Brown    Note:
4746b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
47479be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4748b8123daeSJed Brown 
4749aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4750d763cef2SBarry Smith 
4751d763cef2SBarry Smith .keywords: TS, get, time
4752d763cef2SBarry Smith @*/
47537087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4754d763cef2SBarry Smith {
4755d763cef2SBarry Smith   PetscFunctionBegin;
47560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4757f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4758d763cef2SBarry Smith   *t = ts->ptime;
4759d763cef2SBarry Smith   PetscFunctionReturn(0);
4760d763cef2SBarry Smith }
4761d763cef2SBarry Smith 
4762e5e524a1SHong Zhang /*@
4763e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4764e5e524a1SHong Zhang 
4765e5e524a1SHong Zhang    Not Collective
4766e5e524a1SHong Zhang 
4767e5e524a1SHong Zhang    Input Parameter:
4768e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4769e5e524a1SHong Zhang 
4770e5e524a1SHong Zhang    Output Parameter:
4771e5e524a1SHong Zhang .  t  - the previous time
4772e5e524a1SHong Zhang 
4773e5e524a1SHong Zhang    Level: beginner
4774e5e524a1SHong Zhang 
4775aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4776e5e524a1SHong Zhang 
4777e5e524a1SHong Zhang .keywords: TS, get, time
4778e5e524a1SHong Zhang @*/
4779e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4780e5e524a1SHong Zhang {
4781e5e524a1SHong Zhang   PetscFunctionBegin;
4782e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4783e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4784e5e524a1SHong Zhang   *t = ts->ptime_prev;
4785e5e524a1SHong Zhang   PetscFunctionReturn(0);
4786e5e524a1SHong Zhang }
4787e5e524a1SHong Zhang 
47886a4d4014SLisandro Dalcin /*@
47896a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
47906a4d4014SLisandro Dalcin 
47913f9fe445SBarry Smith    Logically Collective on TS
47926a4d4014SLisandro Dalcin 
47936a4d4014SLisandro Dalcin    Input Parameters:
47946a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
47956a4d4014SLisandro Dalcin -  time - the time
47966a4d4014SLisandro Dalcin 
47976a4d4014SLisandro Dalcin    Level: intermediate
47986a4d4014SLisandro Dalcin 
479919eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
48006a4d4014SLisandro Dalcin 
48016a4d4014SLisandro Dalcin .keywords: TS, set, time
48026a4d4014SLisandro Dalcin @*/
48037087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
48046a4d4014SLisandro Dalcin {
48056a4d4014SLisandro Dalcin   PetscFunctionBegin;
48060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4807c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
48086a4d4014SLisandro Dalcin   ts->ptime = t;
48096a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
48106a4d4014SLisandro Dalcin }
48116a4d4014SLisandro Dalcin 
4812d763cef2SBarry Smith /*@C
4813d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4814d763cef2SBarry Smith    TS options in the database.
4815d763cef2SBarry Smith 
48163f9fe445SBarry Smith    Logically Collective on TS
4817d763cef2SBarry Smith 
4818d763cef2SBarry Smith    Input Parameter:
4819d763cef2SBarry Smith +  ts     - The TS context
4820d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4821d763cef2SBarry Smith 
4822d763cef2SBarry Smith    Notes:
4823d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4824d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4825d763cef2SBarry Smith    hyphen.
4826d763cef2SBarry Smith 
4827d763cef2SBarry Smith    Level: advanced
4828d763cef2SBarry Smith 
4829d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4830d763cef2SBarry Smith 
4831d763cef2SBarry Smith .seealso: TSSetFromOptions()
4832d763cef2SBarry Smith 
4833d763cef2SBarry Smith @*/
48347087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4835d763cef2SBarry Smith {
4836dfbe8321SBarry Smith   PetscErrorCode ierr;
4837089b2837SJed Brown   SNES           snes;
4838d763cef2SBarry Smith 
4839d763cef2SBarry Smith   PetscFunctionBegin;
48400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4841d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4842089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4843089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4844d763cef2SBarry Smith   PetscFunctionReturn(0);
4845d763cef2SBarry Smith }
4846d763cef2SBarry Smith 
4847d763cef2SBarry Smith /*@C
4848d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4849d763cef2SBarry Smith    TS options in the database.
4850d763cef2SBarry Smith 
48513f9fe445SBarry Smith    Logically Collective on TS
4852d763cef2SBarry Smith 
4853d763cef2SBarry Smith    Input Parameter:
4854d763cef2SBarry Smith +  ts     - The TS context
4855d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4856d763cef2SBarry Smith 
4857d763cef2SBarry Smith    Notes:
4858d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4859d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4860d763cef2SBarry Smith    hyphen.
4861d763cef2SBarry Smith 
4862d763cef2SBarry Smith    Level: advanced
4863d763cef2SBarry Smith 
4864d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4865d763cef2SBarry Smith 
4866d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4867d763cef2SBarry Smith 
4868d763cef2SBarry Smith @*/
48697087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4870d763cef2SBarry Smith {
4871dfbe8321SBarry Smith   PetscErrorCode ierr;
4872089b2837SJed Brown   SNES           snes;
4873d763cef2SBarry Smith 
4874d763cef2SBarry Smith   PetscFunctionBegin;
48750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4876d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4877089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4878089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4879d763cef2SBarry Smith   PetscFunctionReturn(0);
4880d763cef2SBarry Smith }
4881d763cef2SBarry Smith 
4882d763cef2SBarry Smith /*@C
4883d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4884d763cef2SBarry Smith    TS options in the database.
4885d763cef2SBarry Smith 
4886d763cef2SBarry Smith    Not Collective
4887d763cef2SBarry Smith 
4888d763cef2SBarry Smith    Input Parameter:
4889d763cef2SBarry Smith .  ts - The TS context
4890d763cef2SBarry Smith 
4891d763cef2SBarry Smith    Output Parameter:
4892d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4893d763cef2SBarry Smith 
4894d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4895d763cef2SBarry Smith    sufficient length to hold the prefix.
4896d763cef2SBarry Smith 
4897d763cef2SBarry Smith    Level: intermediate
4898d763cef2SBarry Smith 
4899d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4900d763cef2SBarry Smith 
4901d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4902d763cef2SBarry Smith @*/
49037087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4904d763cef2SBarry Smith {
4905dfbe8321SBarry Smith   PetscErrorCode ierr;
4906d763cef2SBarry Smith 
4907d763cef2SBarry Smith   PetscFunctionBegin;
49080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49094482741eSBarry Smith   PetscValidPointer(prefix,2);
4910d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4911d763cef2SBarry Smith   PetscFunctionReturn(0);
4912d763cef2SBarry Smith }
4913d763cef2SBarry Smith 
4914d763cef2SBarry Smith /*@C
4915d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4916d763cef2SBarry Smith 
4917d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4918d763cef2SBarry Smith 
4919d763cef2SBarry Smith    Input Parameter:
4920d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4921d763cef2SBarry Smith 
4922d763cef2SBarry Smith    Output Parameters:
4923e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4924e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4925e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4926e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4927d763cef2SBarry Smith 
49280298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4929d763cef2SBarry Smith 
4930d763cef2SBarry Smith    Level: intermediate
4931d763cef2SBarry Smith 
493280275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4933d763cef2SBarry Smith 
4934d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4935d763cef2SBarry Smith @*/
4936e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4937d763cef2SBarry Smith {
4938089b2837SJed Brown   PetscErrorCode ierr;
493924989b8cSPeter Brune   DM             dm;
4940089b2837SJed Brown 
4941d763cef2SBarry Smith   PetscFunctionBegin;
494223a57915SBarry Smith   if (Amat || Pmat) {
494323a57915SBarry Smith     SNES snes;
4944089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
494523a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4946e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
494723a57915SBarry Smith   }
494824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
494924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4950d763cef2SBarry Smith   PetscFunctionReturn(0);
4951d763cef2SBarry Smith }
4952d763cef2SBarry Smith 
49532eca1d9cSJed Brown /*@C
49542eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
49552eca1d9cSJed Brown 
49562eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
49572eca1d9cSJed Brown 
49582eca1d9cSJed Brown    Input Parameter:
49592eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
49602eca1d9cSJed Brown 
49612eca1d9cSJed Brown    Output Parameters:
4962e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4963e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
49642eca1d9cSJed Brown .  f   - The function to compute the matrices
49652eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
49662eca1d9cSJed Brown 
49670298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
49682eca1d9cSJed Brown 
49692eca1d9cSJed Brown    Level: advanced
49702eca1d9cSJed Brown 
497180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
49722eca1d9cSJed Brown 
49732eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
49742eca1d9cSJed Brown @*/
4975e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
49762eca1d9cSJed Brown {
4977089b2837SJed Brown   PetscErrorCode ierr;
497824989b8cSPeter Brune   DM             dm;
49790910c330SBarry Smith 
49802eca1d9cSJed Brown   PetscFunctionBegin;
4981c0aab802Sstefano_zampini   if (Amat || Pmat) {
4982c0aab802Sstefano_zampini     SNES snes;
4983089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4984f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4985e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4986c0aab802Sstefano_zampini   }
498724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
498824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
49892eca1d9cSJed Brown   PetscFunctionReturn(0);
49902eca1d9cSJed Brown }
49912eca1d9cSJed Brown 
49921713a123SBarry Smith /*@C
499383a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
49941713a123SBarry Smith    VecView() for the solution at each timestep
49951713a123SBarry Smith 
49961713a123SBarry Smith    Collective on TS
49971713a123SBarry Smith 
49981713a123SBarry Smith    Input Parameters:
49991713a123SBarry Smith +  ts - the TS context
50001713a123SBarry Smith .  step - current time-step
5001142b95e3SSatish Balay .  ptime - current time
50020298fd71SBarry Smith -  dummy - either a viewer or NULL
50031713a123SBarry Smith 
500499fdda47SBarry Smith    Options Database:
500599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
500699fdda47SBarry Smith 
5007387f4636SBarry Smith    Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
500899fdda47SBarry Smith        will look bad
500999fdda47SBarry Smith 
50101713a123SBarry Smith    Level: intermediate
50111713a123SBarry Smith 
50121713a123SBarry Smith .keywords: TS,  vector, monitor, view
50131713a123SBarry Smith 
5014a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
50151713a123SBarry Smith @*/
50160910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
50171713a123SBarry Smith {
5018dfbe8321SBarry Smith   PetscErrorCode   ierr;
501983a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
5020473a3ab2SBarry Smith   PetscDraw        draw;
50211713a123SBarry Smith 
50221713a123SBarry Smith   PetscFunctionBegin;
50236083293cSBarry Smith   if (!step && ictx->showinitial) {
50246083293cSBarry Smith     if (!ictx->initialsolution) {
50250910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
50261713a123SBarry Smith     }
50270910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
50286083293cSBarry Smith   }
5029b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
50300dcf80beSBarry Smith 
50316083293cSBarry Smith   if (ictx->showinitial) {
50326083293cSBarry Smith     PetscReal pause;
50336083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
50346083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
50356083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
50366083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
50376083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
50386083293cSBarry Smith   }
50390910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
5040473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
504151fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
5042473a3ab2SBarry Smith     char      time[32];
5043473a3ab2SBarry Smith 
5044473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
504585b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
5046473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
5047473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
504851fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
5049473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
5050473a3ab2SBarry Smith   }
5051473a3ab2SBarry Smith 
50526083293cSBarry Smith   if (ictx->showinitial) {
50536083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
50546083293cSBarry Smith   }
50551713a123SBarry Smith   PetscFunctionReturn(0);
50561713a123SBarry Smith }
50571713a123SBarry Smith 
50589110b2e7SHong Zhang /*@C
50599110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
50609110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
50619110b2e7SHong Zhang 
50629110b2e7SHong Zhang    Collective on TS
50639110b2e7SHong Zhang 
50649110b2e7SHong Zhang    Input Parameters:
50659110b2e7SHong Zhang +  ts - the TS context
50669110b2e7SHong Zhang .  step - current time-step
50679110b2e7SHong Zhang .  ptime - current time
50689110b2e7SHong Zhang .  u - current state
50699110b2e7SHong Zhang .  numcost - number of cost functions
50709110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
50719110b2e7SHong Zhang .  mu - sensitivities to parameters
50729110b2e7SHong Zhang -  dummy - either a viewer or NULL
50739110b2e7SHong Zhang 
50749110b2e7SHong Zhang    Level: intermediate
50759110b2e7SHong Zhang 
50769110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
50779110b2e7SHong Zhang 
50789110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
50799110b2e7SHong Zhang @*/
50809110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
50819110b2e7SHong Zhang {
50829110b2e7SHong Zhang   PetscErrorCode   ierr;
50839110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
50849110b2e7SHong Zhang   PetscDraw        draw;
5085a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
5086a0046e2cSSatish Balay   char             time[32];
50879110b2e7SHong Zhang 
50889110b2e7SHong Zhang   PetscFunctionBegin;
50899110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
50909110b2e7SHong Zhang 
50919110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
50929110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
50939110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
50949110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
50959110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
50969110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
50979110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
50989110b2e7SHong Zhang   PetscFunctionReturn(0);
50999110b2e7SHong Zhang }
51009110b2e7SHong Zhang 
51012d5ee99bSBarry Smith /*@C
51022d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
51032d5ee99bSBarry Smith 
51042d5ee99bSBarry Smith    Collective on TS
51052d5ee99bSBarry Smith 
51062d5ee99bSBarry Smith    Input Parameters:
51072d5ee99bSBarry Smith +  ts - the TS context
51082d5ee99bSBarry Smith .  step - current time-step
51092d5ee99bSBarry Smith .  ptime - current time
51102d5ee99bSBarry Smith -  dummy - either a viewer or NULL
51112d5ee99bSBarry Smith 
51122d5ee99bSBarry Smith    Level: intermediate
51132d5ee99bSBarry Smith 
51142d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
51152d5ee99bSBarry Smith 
51162d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
51172d5ee99bSBarry Smith @*/
51182d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
51192d5ee99bSBarry Smith {
51202d5ee99bSBarry Smith   PetscErrorCode    ierr;
51212d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
51222d5ee99bSBarry Smith   PetscDraw         draw;
51236934998bSLisandro Dalcin   PetscDrawAxis     axis;
51242d5ee99bSBarry Smith   PetscInt          n;
51252d5ee99bSBarry Smith   PetscMPIInt       size;
51266934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
51272d5ee99bSBarry Smith   char              time[32];
51282d5ee99bSBarry Smith   const PetscScalar *U;
51292d5ee99bSBarry Smith 
51302d5ee99bSBarry Smith   PetscFunctionBegin;
51316934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
51326934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
51332d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
51346934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
51352d5ee99bSBarry Smith 
51362d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
51376934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
51386934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
51396934998bSLisandro Dalcin   if (!step) {
51406934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
51416934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
51426934998bSLisandro Dalcin   }
51432d5ee99bSBarry Smith 
51442d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
51456934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
51466934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
5147a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
51486934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
51492d5ee99bSBarry Smith 
51506934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
51516934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
51522d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
51534b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
515485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
51552d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
515651fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
51572d5ee99bSBarry Smith   }
51586934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
51592d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
516056d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
51616934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
51622d5ee99bSBarry Smith   PetscFunctionReturn(0);
51632d5ee99bSBarry Smith }
51642d5ee99bSBarry Smith 
51656083293cSBarry Smith /*@C
516683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
51676083293cSBarry Smith 
51686083293cSBarry Smith    Collective on TS
51696083293cSBarry Smith 
51706083293cSBarry Smith    Input Parameters:
51716083293cSBarry Smith .    ctx - the monitor context
51726083293cSBarry Smith 
51736083293cSBarry Smith    Level: intermediate
51746083293cSBarry Smith 
51756083293cSBarry Smith .keywords: TS,  vector, monitor, view
51766083293cSBarry Smith 
517783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
51786083293cSBarry Smith @*/
517983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
51806083293cSBarry Smith {
51816083293cSBarry Smith   PetscErrorCode ierr;
51826083293cSBarry Smith 
51836083293cSBarry Smith   PetscFunctionBegin;
518483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
518583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
518683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
51876083293cSBarry Smith   PetscFunctionReturn(0);
51886083293cSBarry Smith }
51896083293cSBarry Smith 
51906083293cSBarry Smith /*@C
519183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
51926083293cSBarry Smith 
51936083293cSBarry Smith    Collective on TS
51946083293cSBarry Smith 
51956083293cSBarry Smith    Input Parameter:
51966083293cSBarry Smith .    ts - time-step context
51976083293cSBarry Smith 
51986083293cSBarry Smith    Output Patameter:
51996083293cSBarry Smith .    ctx - the monitor context
52006083293cSBarry Smith 
520199fdda47SBarry Smith    Options Database:
520299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
520399fdda47SBarry Smith 
52046083293cSBarry Smith    Level: intermediate
52056083293cSBarry Smith 
52066083293cSBarry Smith .keywords: TS,  vector, monitor, view
52076083293cSBarry Smith 
520883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
52096083293cSBarry Smith @*/
521083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
52116083293cSBarry Smith {
52126083293cSBarry Smith   PetscErrorCode   ierr;
52136083293cSBarry Smith 
52146083293cSBarry Smith   PetscFunctionBegin;
5215b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
521683a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
5217e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
5218bbd56ea5SKarl Rupp 
521983a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
5220473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
5221c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
5222473a3ab2SBarry Smith 
5223473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
5224c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
52256083293cSBarry Smith   PetscFunctionReturn(0);
52266083293cSBarry Smith }
52276083293cSBarry Smith 
52283a471f94SBarry Smith /*@C
52290ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
52300ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
52310ed3bfb6SBarry Smith 
52320ed3bfb6SBarry Smith    Collective on TS
52330ed3bfb6SBarry Smith 
52340ed3bfb6SBarry Smith    Input Parameters:
52350ed3bfb6SBarry Smith +  ts - the TS context
52360ed3bfb6SBarry Smith .  step - current time-step
52370ed3bfb6SBarry Smith .  ptime - current time
52380ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
52390ed3bfb6SBarry Smith 
52400ed3bfb6SBarry Smith    Options Database:
52410ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
52420ed3bfb6SBarry Smith 
52430ed3bfb6SBarry Smith    Level: intermediate
52440ed3bfb6SBarry Smith 
52450ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
52460ed3bfb6SBarry Smith 
52470ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
52480ed3bfb6SBarry Smith @*/
52490ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
52500ed3bfb6SBarry Smith {
52510ed3bfb6SBarry Smith   PetscErrorCode   ierr;
52520ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
52530ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
52540ed3bfb6SBarry Smith   Vec              work;
52550ed3bfb6SBarry Smith 
52560ed3bfb6SBarry Smith   PetscFunctionBegin;
52570ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
52580ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
52590ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
52600ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
52610ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
52620ed3bfb6SBarry Smith   PetscFunctionReturn(0);
52630ed3bfb6SBarry Smith }
52640ed3bfb6SBarry Smith 
52650ed3bfb6SBarry Smith /*@C
526683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
52673a471f94SBarry Smith    VecView() for the error at each timestep
52683a471f94SBarry Smith 
52693a471f94SBarry Smith    Collective on TS
52703a471f94SBarry Smith 
52713a471f94SBarry Smith    Input Parameters:
52723a471f94SBarry Smith +  ts - the TS context
52733a471f94SBarry Smith .  step - current time-step
52743a471f94SBarry Smith .  ptime - current time
52750298fd71SBarry Smith -  dummy - either a viewer or NULL
52763a471f94SBarry Smith 
52770ed3bfb6SBarry Smith    Options Database:
52780ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
52790ed3bfb6SBarry Smith 
52803a471f94SBarry Smith    Level: intermediate
52813a471f94SBarry Smith 
52823a471f94SBarry Smith .keywords: TS,  vector, monitor, view
52833a471f94SBarry Smith 
52840ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
52853a471f94SBarry Smith @*/
52860910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
52873a471f94SBarry Smith {
52883a471f94SBarry Smith   PetscErrorCode   ierr;
528983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
529083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
52913a471f94SBarry Smith   Vec              work;
52923a471f94SBarry Smith 
52933a471f94SBarry Smith   PetscFunctionBegin;
5294b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
52950910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
52963a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
52970910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
52983a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
52993a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
53003a471f94SBarry Smith   PetscFunctionReturn(0);
53013a471f94SBarry Smith }
53023a471f94SBarry Smith 
5303af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
53046c699258SBarry Smith /*@
53052a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
53066c699258SBarry Smith 
53073f9fe445SBarry Smith    Logically Collective on TS and DM
53086c699258SBarry Smith 
53096c699258SBarry Smith    Input Parameters:
53102a808120SBarry Smith +  ts - the ODE integrator object
53112a808120SBarry Smith -  dm - the dm, cannot be NULL
53126c699258SBarry Smith 
53136c699258SBarry Smith    Level: intermediate
53146c699258SBarry Smith 
53156c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
53166c699258SBarry Smith @*/
53177087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
53186c699258SBarry Smith {
53196c699258SBarry Smith   PetscErrorCode ierr;
5320089b2837SJed Brown   SNES           snes;
5321942e3340SBarry Smith   DMTS           tsdm;
53226c699258SBarry Smith 
53236c699258SBarry Smith   PetscFunctionBegin;
53240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53252a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
532670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
5327942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
53282a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
5329942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
5330942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
533124989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
533224989b8cSPeter Brune         tsdm->originaldm = dm;
533324989b8cSPeter Brune       }
533424989b8cSPeter Brune     }
53356bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
533624989b8cSPeter Brune   }
53376c699258SBarry Smith   ts->dm = dm;
5338bbd56ea5SKarl Rupp 
5339089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5340089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
53416c699258SBarry Smith   PetscFunctionReturn(0);
53426c699258SBarry Smith }
53436c699258SBarry Smith 
53446c699258SBarry Smith /*@
53456c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
53466c699258SBarry Smith 
53473f9fe445SBarry Smith    Not Collective
53486c699258SBarry Smith 
53496c699258SBarry Smith    Input Parameter:
53506c699258SBarry Smith . ts - the preconditioner context
53516c699258SBarry Smith 
53526c699258SBarry Smith    Output Parameter:
53536c699258SBarry Smith .  dm - the dm
53546c699258SBarry Smith 
53556c699258SBarry Smith    Level: intermediate
53566c699258SBarry Smith 
53576c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
53586c699258SBarry Smith @*/
53597087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
53606c699258SBarry Smith {
5361496e6a7aSJed Brown   PetscErrorCode ierr;
5362496e6a7aSJed Brown 
53636c699258SBarry Smith   PetscFunctionBegin;
53640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5365496e6a7aSJed Brown   if (!ts->dm) {
5366ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
5367496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
5368496e6a7aSJed Brown   }
53696c699258SBarry Smith   *dm = ts->dm;
53706c699258SBarry Smith   PetscFunctionReturn(0);
53716c699258SBarry Smith }
53721713a123SBarry Smith 
53730f5c6efeSJed Brown /*@
53740f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
53750f5c6efeSJed Brown 
53763f9fe445SBarry Smith    Logically Collective on SNES
53770f5c6efeSJed Brown 
53780f5c6efeSJed Brown    Input Parameter:
5379d42a1c89SJed Brown + snes - nonlinear solver
53800910c330SBarry Smith . U - the current state at which to evaluate the residual
5381d42a1c89SJed Brown - ctx - user context, must be a TS
53820f5c6efeSJed Brown 
53830f5c6efeSJed Brown    Output Parameter:
53840f5c6efeSJed Brown . F - the nonlinear residual
53850f5c6efeSJed Brown 
53860f5c6efeSJed Brown    Notes:
53870f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
53880f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
53890f5c6efeSJed Brown 
53900f5c6efeSJed Brown    Level: advanced
53910f5c6efeSJed Brown 
53920f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
53930f5c6efeSJed Brown @*/
53940910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
53950f5c6efeSJed Brown {
53960f5c6efeSJed Brown   TS             ts = (TS)ctx;
53970f5c6efeSJed Brown   PetscErrorCode ierr;
53980f5c6efeSJed Brown 
53990f5c6efeSJed Brown   PetscFunctionBegin;
54000f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
54010910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
54020f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
54030f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
54040910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
54050f5c6efeSJed Brown   PetscFunctionReturn(0);
54060f5c6efeSJed Brown }
54070f5c6efeSJed Brown 
54080f5c6efeSJed Brown /*@
54090f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
54100f5c6efeSJed Brown 
54110f5c6efeSJed Brown    Collective on SNES
54120f5c6efeSJed Brown 
54130f5c6efeSJed Brown    Input Parameter:
54140f5c6efeSJed Brown + snes - nonlinear solver
54150910c330SBarry Smith . U - the current state at which to evaluate the residual
54160f5c6efeSJed Brown - ctx - user context, must be a TS
54170f5c6efeSJed Brown 
54180f5c6efeSJed Brown    Output Parameter:
54190f5c6efeSJed Brown + A - the Jacobian
54200f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
54210f5c6efeSJed Brown - flag - indicates any structure change in the matrix
54220f5c6efeSJed Brown 
54230f5c6efeSJed Brown    Notes:
54240f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
54250f5c6efeSJed Brown 
54260f5c6efeSJed Brown    Level: developer
54270f5c6efeSJed Brown 
54280f5c6efeSJed Brown .seealso: SNESSetJacobian()
54290f5c6efeSJed Brown @*/
5430d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
54310f5c6efeSJed Brown {
54320f5c6efeSJed Brown   TS             ts = (TS)ctx;
54330f5c6efeSJed Brown   PetscErrorCode ierr;
54340f5c6efeSJed Brown 
54350f5c6efeSJed Brown   PetscFunctionBegin;
54360f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
54370910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
54380f5c6efeSJed Brown   PetscValidPointer(A,3);
543994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
54400f5c6efeSJed Brown   PetscValidPointer(B,4);
544194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
54420f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5443d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
54440f5c6efeSJed Brown   PetscFunctionReturn(0);
54450f5c6efeSJed Brown }
5446325fc9f4SBarry Smith 
54470e4ef248SJed Brown /*@C
54489ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
54490e4ef248SJed Brown 
54500e4ef248SJed Brown    Collective on TS
54510e4ef248SJed Brown 
54520e4ef248SJed Brown    Input Arguments:
54530e4ef248SJed Brown +  ts - time stepping context
54540e4ef248SJed Brown .  t - time at which to evaluate
54550910c330SBarry Smith .  U - state at which to evaluate
54560e4ef248SJed Brown -  ctx - context
54570e4ef248SJed Brown 
54580e4ef248SJed Brown    Output Arguments:
54590e4ef248SJed Brown .  F - right hand side
54600e4ef248SJed Brown 
54610e4ef248SJed Brown    Level: intermediate
54620e4ef248SJed Brown 
54630e4ef248SJed Brown    Notes:
54640e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
54650e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
54660e4ef248SJed Brown 
54670e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
54680e4ef248SJed Brown @*/
54690910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
54700e4ef248SJed Brown {
54710e4ef248SJed Brown   PetscErrorCode ierr;
54720e4ef248SJed Brown   Mat            Arhs,Brhs;
54730e4ef248SJed Brown 
54740e4ef248SJed Brown   PetscFunctionBegin;
54750e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5476d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
54770910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
54780e4ef248SJed Brown   PetscFunctionReturn(0);
54790e4ef248SJed Brown }
54800e4ef248SJed Brown 
54810e4ef248SJed Brown /*@C
54820e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
54830e4ef248SJed Brown 
54840e4ef248SJed Brown    Collective on TS
54850e4ef248SJed Brown 
54860e4ef248SJed Brown    Input Arguments:
54870e4ef248SJed Brown +  ts - time stepping context
54880e4ef248SJed Brown .  t - time at which to evaluate
54890910c330SBarry Smith .  U - state at which to evaluate
54900e4ef248SJed Brown -  ctx - context
54910e4ef248SJed Brown 
54920e4ef248SJed Brown    Output Arguments:
54930e4ef248SJed Brown +  A - pointer to operator
54940e4ef248SJed Brown .  B - pointer to preconditioning matrix
54950e4ef248SJed Brown -  flg - matrix structure flag
54960e4ef248SJed Brown 
54970e4ef248SJed Brown    Level: intermediate
54980e4ef248SJed Brown 
54990e4ef248SJed Brown    Notes:
55000e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
55010e4ef248SJed Brown 
55020e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
55030e4ef248SJed Brown @*/
5504d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
55050e4ef248SJed Brown {
55060e4ef248SJed Brown   PetscFunctionBegin;
55070e4ef248SJed Brown   PetscFunctionReturn(0);
55080e4ef248SJed Brown }
55090e4ef248SJed Brown 
55100026cea9SSean Farley /*@C
55110026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
55120026cea9SSean Farley 
55130026cea9SSean Farley    Collective on TS
55140026cea9SSean Farley 
55150026cea9SSean Farley    Input Arguments:
55160026cea9SSean Farley +  ts - time stepping context
55170026cea9SSean Farley .  t - time at which to evaluate
55180910c330SBarry Smith .  U - state at which to evaluate
55190910c330SBarry Smith .  Udot - time derivative of state vector
55200026cea9SSean Farley -  ctx - context
55210026cea9SSean Farley 
55220026cea9SSean Farley    Output Arguments:
55230026cea9SSean Farley .  F - left hand side
55240026cea9SSean Farley 
55250026cea9SSean Farley    Level: intermediate
55260026cea9SSean Farley 
55270026cea9SSean Farley    Notes:
55280910c330SBarry 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
55290026cea9SSean Farley    user is required to write their own TSComputeIFunction.
55300026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
55310026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
55320026cea9SSean Farley 
55339ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
55349ae8fd06SBarry Smith 
55359ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
55360026cea9SSean Farley @*/
55370910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
55380026cea9SSean Farley {
55390026cea9SSean Farley   PetscErrorCode ierr;
55400026cea9SSean Farley   Mat            A,B;
55410026cea9SSean Farley 
55420026cea9SSean Farley   PetscFunctionBegin;
55430298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5544d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
55450910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
55460026cea9SSean Farley   PetscFunctionReturn(0);
55470026cea9SSean Farley }
55480026cea9SSean Farley 
55490026cea9SSean Farley /*@C
5550b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
55510026cea9SSean Farley 
55520026cea9SSean Farley    Collective on TS
55530026cea9SSean Farley 
55540026cea9SSean Farley    Input Arguments:
55550026cea9SSean Farley +  ts - time stepping context
55560026cea9SSean Farley .  t - time at which to evaluate
55570910c330SBarry Smith .  U - state at which to evaluate
55580910c330SBarry Smith .  Udot - time derivative of state vector
55590026cea9SSean Farley .  shift - shift to apply
55600026cea9SSean Farley -  ctx - context
55610026cea9SSean Farley 
55620026cea9SSean Farley    Output Arguments:
55630026cea9SSean Farley +  A - pointer to operator
55640026cea9SSean Farley .  B - pointer to preconditioning matrix
55650026cea9SSean Farley -  flg - matrix structure flag
55660026cea9SSean Farley 
5567b41af12eSJed Brown    Level: advanced
55680026cea9SSean Farley 
55690026cea9SSean Farley    Notes:
55700026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
55710026cea9SSean Farley 
5572b41af12eSJed Brown    It is only appropriate for problems of the form
5573b41af12eSJed Brown 
5574b41af12eSJed Brown $     M Udot = F(U,t)
5575b41af12eSJed Brown 
5576b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5577b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5578b41af12eSJed Brown   an implicit operator of the form
5579b41af12eSJed Brown 
5580b41af12eSJed Brown $    shift*M + J
5581b41af12eSJed Brown 
5582b41af12eSJed 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
5583b41af12eSJed Brown   a copy of M or reassemble it when requested.
5584b41af12eSJed Brown 
55850026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
55860026cea9SSean Farley @*/
5587d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
55880026cea9SSean Farley {
5589b41af12eSJed Brown   PetscErrorCode ierr;
5590b41af12eSJed Brown 
55910026cea9SSean Farley   PetscFunctionBegin;
559294ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5593b41af12eSJed Brown   ts->ijacobian.shift = shift;
55940026cea9SSean Farley   PetscFunctionReturn(0);
55950026cea9SSean Farley }
5596b41af12eSJed Brown 
5597e817cc15SEmil Constantinescu /*@
5598e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5599e817cc15SEmil Constantinescu 
5600e817cc15SEmil Constantinescu    Not Collective
5601e817cc15SEmil Constantinescu 
5602e817cc15SEmil Constantinescu    Input Parameter:
5603e817cc15SEmil Constantinescu .  ts - the TS context
5604e817cc15SEmil Constantinescu 
5605e817cc15SEmil Constantinescu    Output Parameter:
56064e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5607e817cc15SEmil Constantinescu 
5608e817cc15SEmil Constantinescu    Level: beginner
5609e817cc15SEmil Constantinescu 
5610e817cc15SEmil Constantinescu .keywords: TS, equation type
5611e817cc15SEmil Constantinescu 
5612e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5613e817cc15SEmil Constantinescu @*/
5614e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5615e817cc15SEmil Constantinescu {
5616e817cc15SEmil Constantinescu   PetscFunctionBegin;
5617e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5618e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5619e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5620e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5621e817cc15SEmil Constantinescu }
5622e817cc15SEmil Constantinescu 
5623e817cc15SEmil Constantinescu /*@
5624e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5625e817cc15SEmil Constantinescu 
5626e817cc15SEmil Constantinescu    Not Collective
5627e817cc15SEmil Constantinescu 
5628e817cc15SEmil Constantinescu    Input Parameter:
5629e817cc15SEmil Constantinescu +  ts - the TS context
56301297b224SEmil Constantinescu -  equation_type - see TSEquationType
5631e817cc15SEmil Constantinescu 
5632e817cc15SEmil Constantinescu    Level: advanced
5633e817cc15SEmil Constantinescu 
5634e817cc15SEmil Constantinescu .keywords: TS, equation type
5635e817cc15SEmil Constantinescu 
5636e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5637e817cc15SEmil Constantinescu @*/
5638e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5639e817cc15SEmil Constantinescu {
5640e817cc15SEmil Constantinescu   PetscFunctionBegin;
5641e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5642e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5643e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5644e817cc15SEmil Constantinescu }
56450026cea9SSean Farley 
56464af1b03aSJed Brown /*@
56474af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
56484af1b03aSJed Brown 
56494af1b03aSJed Brown    Not Collective
56504af1b03aSJed Brown 
56514af1b03aSJed Brown    Input Parameter:
56524af1b03aSJed Brown .  ts - the TS context
56534af1b03aSJed Brown 
56544af1b03aSJed Brown    Output Parameter:
56554af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
56564af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
56574af1b03aSJed Brown 
5658487e0bb9SJed Brown    Level: beginner
56594af1b03aSJed Brown 
5660cd652676SJed Brown    Notes:
5661cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
56624af1b03aSJed Brown 
56634af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
56644af1b03aSJed Brown 
56654af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
56664af1b03aSJed Brown @*/
56674af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
56684af1b03aSJed Brown {
56694af1b03aSJed Brown   PetscFunctionBegin;
56704af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56714af1b03aSJed Brown   PetscValidPointer(reason,2);
56724af1b03aSJed Brown   *reason = ts->reason;
56734af1b03aSJed Brown   PetscFunctionReturn(0);
56744af1b03aSJed Brown }
56754af1b03aSJed Brown 
5676d6ad946cSShri Abhyankar /*@
5677d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5678d6ad946cSShri Abhyankar 
5679d6ad946cSShri Abhyankar    Not Collective
5680d6ad946cSShri Abhyankar 
5681d6ad946cSShri Abhyankar    Input Parameter:
5682d6ad946cSShri Abhyankar +  ts - the TS context
5683d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5684d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5685d6ad946cSShri Abhyankar 
5686f5abba47SShri Abhyankar    Level: advanced
5687d6ad946cSShri Abhyankar 
5688d6ad946cSShri Abhyankar    Notes:
5689d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5690d6ad946cSShri Abhyankar 
5691d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5692d6ad946cSShri Abhyankar 
5693d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5694d6ad946cSShri Abhyankar @*/
5695d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5696d6ad946cSShri Abhyankar {
5697d6ad946cSShri Abhyankar   PetscFunctionBegin;
5698d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5699d6ad946cSShri Abhyankar   ts->reason = reason;
5700d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5701d6ad946cSShri Abhyankar }
5702d6ad946cSShri Abhyankar 
5703cc708dedSBarry Smith /*@
5704cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5705cc708dedSBarry Smith 
5706cc708dedSBarry Smith    Not Collective
5707cc708dedSBarry Smith 
5708cc708dedSBarry Smith    Input Parameter:
5709cc708dedSBarry Smith .  ts - the TS context
5710cc708dedSBarry Smith 
5711cc708dedSBarry Smith    Output Parameter:
571219eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5713cc708dedSBarry Smith 
5714487e0bb9SJed Brown    Level: beginner
5715cc708dedSBarry Smith 
5716cc708dedSBarry Smith    Notes:
5717cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5718cc708dedSBarry Smith 
5719cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5720cc708dedSBarry Smith 
5721cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5722cc708dedSBarry Smith @*/
5723cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5724cc708dedSBarry Smith {
5725cc708dedSBarry Smith   PetscFunctionBegin;
5726cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5727cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5728cc708dedSBarry Smith   *ftime = ts->solvetime;
5729cc708dedSBarry Smith   PetscFunctionReturn(0);
5730cc708dedSBarry Smith }
5731cc708dedSBarry Smith 
57322c18e0fdSBarry Smith /*@
57335ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
57349f67acb7SJed Brown    used by the time integrator.
57359f67acb7SJed Brown 
57369f67acb7SJed Brown    Not Collective
57379f67acb7SJed Brown 
57389f67acb7SJed Brown    Input Parameter:
57399f67acb7SJed Brown .  ts - TS context
57409f67acb7SJed Brown 
57419f67acb7SJed Brown    Output Parameter:
57429f67acb7SJed Brown .  nits - number of nonlinear iterations
57439f67acb7SJed Brown 
57449f67acb7SJed Brown    Notes:
57459f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
57469f67acb7SJed Brown 
57479f67acb7SJed Brown    Level: intermediate
57489f67acb7SJed Brown 
57499f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
57509f67acb7SJed Brown 
57515ef26d82SJed Brown .seealso:  TSGetKSPIterations()
57529f67acb7SJed Brown @*/
57535ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
57549f67acb7SJed Brown {
57559f67acb7SJed Brown   PetscFunctionBegin;
57569f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57579f67acb7SJed Brown   PetscValidIntPointer(nits,2);
57585ef26d82SJed Brown   *nits = ts->snes_its;
57599f67acb7SJed Brown   PetscFunctionReturn(0);
57609f67acb7SJed Brown }
57619f67acb7SJed Brown 
57629f67acb7SJed Brown /*@
57635ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
57649f67acb7SJed Brown    used by the time integrator.
57659f67acb7SJed Brown 
57669f67acb7SJed Brown    Not Collective
57679f67acb7SJed Brown 
57689f67acb7SJed Brown    Input Parameter:
57699f67acb7SJed Brown .  ts - TS context
57709f67acb7SJed Brown 
57719f67acb7SJed Brown    Output Parameter:
57729f67acb7SJed Brown .  lits - number of linear iterations
57739f67acb7SJed Brown 
57749f67acb7SJed Brown    Notes:
57759f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
57769f67acb7SJed Brown 
57779f67acb7SJed Brown    Level: intermediate
57789f67acb7SJed Brown 
57799f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
57809f67acb7SJed Brown 
57815ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
57829f67acb7SJed Brown @*/
57835ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
57849f67acb7SJed Brown {
57859f67acb7SJed Brown   PetscFunctionBegin;
57869f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57879f67acb7SJed Brown   PetscValidIntPointer(lits,2);
57885ef26d82SJed Brown   *lits = ts->ksp_its;
57899f67acb7SJed Brown   PetscFunctionReturn(0);
57909f67acb7SJed Brown }
57919f67acb7SJed Brown 
5792cef5090cSJed Brown /*@
5793cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5794cef5090cSJed Brown 
5795cef5090cSJed Brown    Not Collective
5796cef5090cSJed Brown 
5797cef5090cSJed Brown    Input Parameter:
5798cef5090cSJed Brown .  ts - TS context
5799cef5090cSJed Brown 
5800cef5090cSJed Brown    Output Parameter:
5801cef5090cSJed Brown .  rejects - number of steps rejected
5802cef5090cSJed Brown 
5803cef5090cSJed Brown    Notes:
5804cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5805cef5090cSJed Brown 
5806cef5090cSJed Brown    Level: intermediate
5807cef5090cSJed Brown 
5808cef5090cSJed Brown .keywords: TS, get, number
5809cef5090cSJed Brown 
58105ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5811cef5090cSJed Brown @*/
5812cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5813cef5090cSJed Brown {
5814cef5090cSJed Brown   PetscFunctionBegin;
5815cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5816cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5817cef5090cSJed Brown   *rejects = ts->reject;
5818cef5090cSJed Brown   PetscFunctionReturn(0);
5819cef5090cSJed Brown }
5820cef5090cSJed Brown 
5821cef5090cSJed Brown /*@
5822cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5823cef5090cSJed Brown 
5824cef5090cSJed Brown    Not Collective
5825cef5090cSJed Brown 
5826cef5090cSJed Brown    Input Parameter:
5827cef5090cSJed Brown .  ts - TS context
5828cef5090cSJed Brown 
5829cef5090cSJed Brown    Output Parameter:
5830cef5090cSJed Brown .  fails - number of failed nonlinear solves
5831cef5090cSJed Brown 
5832cef5090cSJed Brown    Notes:
5833cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5834cef5090cSJed Brown 
5835cef5090cSJed Brown    Level: intermediate
5836cef5090cSJed Brown 
5837cef5090cSJed Brown .keywords: TS, get, number
5838cef5090cSJed Brown 
58395ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5840cef5090cSJed Brown @*/
5841cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5842cef5090cSJed Brown {
5843cef5090cSJed Brown   PetscFunctionBegin;
5844cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5845cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5846cef5090cSJed Brown   *fails = ts->num_snes_failures;
5847cef5090cSJed Brown   PetscFunctionReturn(0);
5848cef5090cSJed Brown }
5849cef5090cSJed Brown 
5850cef5090cSJed Brown /*@
5851cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5852cef5090cSJed Brown 
5853cef5090cSJed Brown    Not Collective
5854cef5090cSJed Brown 
5855cef5090cSJed Brown    Input Parameter:
5856cef5090cSJed Brown +  ts - TS context
5857cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5858cef5090cSJed Brown 
5859cef5090cSJed Brown    Notes:
5860cef5090cSJed Brown    The counter is reset to zero for each step
5861cef5090cSJed Brown 
5862cef5090cSJed Brown    Options Database Key:
5863cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5864cef5090cSJed Brown 
5865cef5090cSJed Brown    Level: intermediate
5866cef5090cSJed Brown 
5867cef5090cSJed Brown .keywords: TS, set, maximum, number
5868cef5090cSJed Brown 
58695ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5870cef5090cSJed Brown @*/
5871cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5872cef5090cSJed Brown {
5873cef5090cSJed Brown   PetscFunctionBegin;
5874cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5875cef5090cSJed Brown   ts->max_reject = rejects;
5876cef5090cSJed Brown   PetscFunctionReturn(0);
5877cef5090cSJed Brown }
5878cef5090cSJed Brown 
5879cef5090cSJed Brown /*@
5880cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5881cef5090cSJed Brown 
5882cef5090cSJed Brown    Not Collective
5883cef5090cSJed Brown 
5884cef5090cSJed Brown    Input Parameter:
5885cef5090cSJed Brown +  ts - TS context
5886cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5887cef5090cSJed Brown 
5888cef5090cSJed Brown    Notes:
5889cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5890cef5090cSJed Brown 
5891cef5090cSJed Brown    Options Database Key:
5892cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5893cef5090cSJed Brown 
5894cef5090cSJed Brown    Level: intermediate
5895cef5090cSJed Brown 
5896cef5090cSJed Brown .keywords: TS, set, maximum, number
5897cef5090cSJed Brown 
58985ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5899cef5090cSJed Brown @*/
5900cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5901cef5090cSJed Brown {
5902cef5090cSJed Brown   PetscFunctionBegin;
5903cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5904cef5090cSJed Brown   ts->max_snes_failures = fails;
5905cef5090cSJed Brown   PetscFunctionReturn(0);
5906cef5090cSJed Brown }
5907cef5090cSJed Brown 
5908cef5090cSJed Brown /*@
5909cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5910cef5090cSJed Brown 
5911cef5090cSJed Brown    Not Collective
5912cef5090cSJed Brown 
5913cef5090cSJed Brown    Input Parameter:
5914cef5090cSJed Brown +  ts - TS context
5915cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5916cef5090cSJed Brown 
5917cef5090cSJed Brown    Options Database Key:
5918cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5919cef5090cSJed Brown 
5920cef5090cSJed Brown    Level: intermediate
5921cef5090cSJed Brown 
5922cef5090cSJed Brown .keywords: TS, set, error
5923cef5090cSJed Brown 
59245ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5925cef5090cSJed Brown @*/
5926cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5927cef5090cSJed Brown {
5928cef5090cSJed Brown   PetscFunctionBegin;
5929cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5930cef5090cSJed Brown   ts->errorifstepfailed = err;
5931cef5090cSJed Brown   PetscFunctionReturn(0);
5932cef5090cSJed Brown }
5933cef5090cSJed Brown 
5934fb1732b5SBarry Smith /*@C
5935fde5950dSBarry 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
5936fb1732b5SBarry Smith 
5937fb1732b5SBarry Smith    Collective on TS
5938fb1732b5SBarry Smith 
5939fb1732b5SBarry Smith    Input Parameters:
5940fb1732b5SBarry Smith +  ts - the TS context
5941fb1732b5SBarry Smith .  step - current time-step
5942fb1732b5SBarry Smith .  ptime - current time
59430910c330SBarry Smith .  u - current state
5944721cd6eeSBarry Smith -  vf - viewer and its format
5945fb1732b5SBarry Smith 
5946fb1732b5SBarry Smith    Level: intermediate
5947fb1732b5SBarry Smith 
5948fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5949fb1732b5SBarry Smith 
5950fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5951fb1732b5SBarry Smith @*/
5952721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5953fb1732b5SBarry Smith {
5954fb1732b5SBarry Smith   PetscErrorCode ierr;
5955fb1732b5SBarry Smith 
5956fb1732b5SBarry Smith   PetscFunctionBegin;
5957721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5958721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5959721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5960ed81e22dSJed Brown   PetscFunctionReturn(0);
5961ed81e22dSJed Brown }
5962ed81e22dSJed Brown 
5963ed81e22dSJed Brown /*@C
5964ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5965ed81e22dSJed Brown 
5966ed81e22dSJed Brown    Collective on TS
5967ed81e22dSJed Brown 
5968ed81e22dSJed Brown    Input Parameters:
5969ed81e22dSJed Brown +  ts - the TS context
5970ed81e22dSJed Brown .  step - current time-step
5971ed81e22dSJed Brown .  ptime - current time
59720910c330SBarry Smith .  u - current state
5973ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5974ed81e22dSJed Brown 
5975ed81e22dSJed Brown    Level: intermediate
5976ed81e22dSJed Brown 
5977ed81e22dSJed Brown    Notes:
5978ed81e22dSJed 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.
5979ed81e22dSJed Brown    These are named according to the file name template.
5980ed81e22dSJed Brown 
5981ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5982ed81e22dSJed Brown 
5983ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5984ed81e22dSJed Brown 
5985ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5986ed81e22dSJed Brown @*/
59870910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5988ed81e22dSJed Brown {
5989ed81e22dSJed Brown   PetscErrorCode ierr;
5990ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5991ed81e22dSJed Brown   PetscViewer    viewer;
5992ed81e22dSJed Brown 
5993ed81e22dSJed Brown   PetscFunctionBegin;
599463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
59958caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5996ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
59970910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5998ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5999ed81e22dSJed Brown   PetscFunctionReturn(0);
6000ed81e22dSJed Brown }
6001ed81e22dSJed Brown 
6002ed81e22dSJed Brown /*@C
6003ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
6004ed81e22dSJed Brown 
6005ed81e22dSJed Brown    Collective on TS
6006ed81e22dSJed Brown 
6007ed81e22dSJed Brown    Input Parameters:
6008ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
6009ed81e22dSJed Brown 
6010ed81e22dSJed Brown    Level: intermediate
6011ed81e22dSJed Brown 
6012ed81e22dSJed Brown    Note:
6013ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
6014ed81e22dSJed Brown 
6015ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
6016ed81e22dSJed Brown 
6017ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
6018ed81e22dSJed Brown @*/
6019ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
6020ed81e22dSJed Brown {
6021ed81e22dSJed Brown   PetscErrorCode ierr;
6022ed81e22dSJed Brown 
6023ed81e22dSJed Brown   PetscFunctionBegin;
6024ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
6025fb1732b5SBarry Smith   PetscFunctionReturn(0);
6026fb1732b5SBarry Smith }
6027fb1732b5SBarry Smith 
602884df9cb4SJed Brown /*@
6029552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
603084df9cb4SJed Brown 
6031ed81e22dSJed Brown    Collective on TS if controller has not been created yet
603284df9cb4SJed Brown 
603384df9cb4SJed Brown    Input Arguments:
6034ed81e22dSJed Brown .  ts - time stepping context
603584df9cb4SJed Brown 
603684df9cb4SJed Brown    Output Arguments:
6037ed81e22dSJed Brown .  adapt - adaptive controller
603884df9cb4SJed Brown 
603984df9cb4SJed Brown    Level: intermediate
604084df9cb4SJed Brown 
6041ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
604284df9cb4SJed Brown @*/
6043552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
604484df9cb4SJed Brown {
604584df9cb4SJed Brown   PetscErrorCode ierr;
604684df9cb4SJed Brown 
604784df9cb4SJed Brown   PetscFunctionBegin;
604884df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6049bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
605084df9cb4SJed Brown   if (!ts->adapt) {
6051ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
60523bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
60531c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
605484df9cb4SJed Brown   }
6055bec58848SLisandro Dalcin   *adapt = ts->adapt;
605684df9cb4SJed Brown   PetscFunctionReturn(0);
605784df9cb4SJed Brown }
6058d6ebe24aSShri Abhyankar 
60591c3436cfSJed Brown /*@
60601c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
60611c3436cfSJed Brown 
60621c3436cfSJed Brown    Logically Collective
60631c3436cfSJed Brown 
60641c3436cfSJed Brown    Input Arguments:
60651c3436cfSJed Brown +  ts - time integration context
60661c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
60670298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
60681c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
60690298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
60701c3436cfSJed Brown 
6071a3cdaa26SBarry Smith    Options Database keys:
6072a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
6073a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
6074a3cdaa26SBarry Smith 
60753ff766beSShri Abhyankar    Notes:
60763ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
60773ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
60783ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
60793ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
60803ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
60813ff766beSShri Abhyankar    differential variables.
60823ff766beSShri Abhyankar 
60831c3436cfSJed Brown    Level: beginner
60841c3436cfSJed Brown 
6085c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
60861c3436cfSJed Brown @*/
60871c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
60881c3436cfSJed Brown {
60891c3436cfSJed Brown   PetscErrorCode ierr;
60901c3436cfSJed Brown 
60911c3436cfSJed Brown   PetscFunctionBegin;
6092c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
60931c3436cfSJed Brown   if (vatol) {
60941c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
60951c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
60961c3436cfSJed Brown     ts->vatol = vatol;
60971c3436cfSJed Brown   }
6098c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
60991c3436cfSJed Brown   if (vrtol) {
61001c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
61011c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
61021c3436cfSJed Brown     ts->vrtol = vrtol;
61031c3436cfSJed Brown   }
61041c3436cfSJed Brown   PetscFunctionReturn(0);
61051c3436cfSJed Brown }
61061c3436cfSJed Brown 
6107c5033834SJed Brown /*@
6108c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
6109c5033834SJed Brown 
6110c5033834SJed Brown    Logically Collective
6111c5033834SJed Brown 
6112c5033834SJed Brown    Input Arguments:
6113c5033834SJed Brown .  ts - time integration context
6114c5033834SJed Brown 
6115c5033834SJed Brown    Output Arguments:
61160298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
61170298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
61180298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
61190298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
6120c5033834SJed Brown 
6121c5033834SJed Brown    Level: beginner
6122c5033834SJed Brown 
6123c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
6124c5033834SJed Brown @*/
6125c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
6126c5033834SJed Brown {
6127c5033834SJed Brown   PetscFunctionBegin;
6128c5033834SJed Brown   if (atol)  *atol  = ts->atol;
6129c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
6130c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
6131c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
6132c5033834SJed Brown   PetscFunctionReturn(0);
6133c5033834SJed Brown }
6134c5033834SJed Brown 
61359c6b16b5SShri Abhyankar /*@
6136a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
61379c6b16b5SShri Abhyankar 
61389c6b16b5SShri Abhyankar    Collective on TS
61399c6b16b5SShri Abhyankar 
61409c6b16b5SShri Abhyankar    Input Arguments:
61419c6b16b5SShri Abhyankar +  ts - time stepping context
6142a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6143a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
61449c6b16b5SShri Abhyankar 
61459c6b16b5SShri Abhyankar    Output Arguments:
61467453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
61477453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
61487453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
61499c6b16b5SShri Abhyankar 
61509c6b16b5SShri Abhyankar    Level: developer
61519c6b16b5SShri Abhyankar 
6152deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
61539c6b16b5SShri Abhyankar @*/
61547453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61559c6b16b5SShri Abhyankar {
61569c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
61579c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
61587453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
61597453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
61609c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
61617453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
61627453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
61637453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
61649c6b16b5SShri Abhyankar 
61659c6b16b5SShri Abhyankar   PetscFunctionBegin;
61669c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6167a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6168a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6169a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6170a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6171a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6172a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
61738a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
61748a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6175a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
61769c6b16b5SShri Abhyankar 
61779c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
61789c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
61799c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
61809c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
61819c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
61827453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
61837453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
61847453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
61859c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
61869c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
61879c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61889c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61899c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
61907453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61917453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
61927453f775SEmil Constantinescu       if(tola>0.){
61937453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
61947453f775SEmil Constantinescu         na_loc++;
61957453f775SEmil Constantinescu       }
61967453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61977453f775SEmil Constantinescu       if(tolr>0.){
61987453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
61997453f775SEmil Constantinescu         nr_loc++;
62007453f775SEmil Constantinescu       }
62017453f775SEmil Constantinescu       tol=tola+tolr;
62027453f775SEmil Constantinescu       if(tol>0.){
62037453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
62047453f775SEmil Constantinescu         n_loc++;
62057453f775SEmil Constantinescu       }
62069c6b16b5SShri Abhyankar     }
62079c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62089c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62099c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62109c6b16b5SShri Abhyankar     const PetscScalar *atol;
62119c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62129c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
62137453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62147453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
62157453f775SEmil Constantinescu       if(tola>0.){
62167453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
62177453f775SEmil Constantinescu         na_loc++;
62187453f775SEmil Constantinescu       }
62197453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62207453f775SEmil Constantinescu       if(tolr>0.){
62217453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
62227453f775SEmil Constantinescu         nr_loc++;
62237453f775SEmil Constantinescu       }
62247453f775SEmil Constantinescu       tol=tola+tolr;
62257453f775SEmil Constantinescu       if(tol>0.){
62267453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
62277453f775SEmil Constantinescu         n_loc++;
62287453f775SEmil Constantinescu       }
62299c6b16b5SShri Abhyankar     }
62309c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62319c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
62329c6b16b5SShri Abhyankar     const PetscScalar *rtol;
62339c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62349c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
62357453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62367453f775SEmil Constantinescu       tola = ts->atol;
62377453f775SEmil Constantinescu       if(tola>0.){
62387453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
62397453f775SEmil Constantinescu         na_loc++;
62407453f775SEmil Constantinescu       }
62417453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62427453f775SEmil Constantinescu       if(tolr>0.){
62437453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
62447453f775SEmil Constantinescu         nr_loc++;
62457453f775SEmil Constantinescu       }
62467453f775SEmil Constantinescu       tol=tola+tolr;
62477453f775SEmil Constantinescu       if(tol>0.){
62487453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
62497453f775SEmil Constantinescu         n_loc++;
62507453f775SEmil Constantinescu       }
62519c6b16b5SShri Abhyankar     }
62529c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62539c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
62549c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
62557453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
62567453f775SEmil Constantinescu      tola = ts->atol;
62577453f775SEmil Constantinescu       if(tola>0.){
62587453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
62597453f775SEmil Constantinescu         na_loc++;
62607453f775SEmil Constantinescu       }
62617453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62627453f775SEmil Constantinescu       if(tolr>0.){
62637453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
62647453f775SEmil Constantinescu         nr_loc++;
62657453f775SEmil Constantinescu       }
62667453f775SEmil Constantinescu       tol=tola+tolr;
62677453f775SEmil Constantinescu       if(tol>0.){
62687453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
62697453f775SEmil Constantinescu         n_loc++;
62707453f775SEmil Constantinescu       }
62719c6b16b5SShri Abhyankar     }
62729c6b16b5SShri Abhyankar   }
62739c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
62749c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
62759c6b16b5SShri Abhyankar 
62767453f775SEmil Constantinescu   err_loc[0] = sum;
62777453f775SEmil Constantinescu   err_loc[1] = suma;
62787453f775SEmil Constantinescu   err_loc[2] = sumr;
62797453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
62807453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
62817453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
62827453f775SEmil Constantinescu 
6283a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62847453f775SEmil Constantinescu 
62857453f775SEmil Constantinescu   gsum   = err_glb[0];
62867453f775SEmil Constantinescu   gsuma  = err_glb[1];
62877453f775SEmil Constantinescu   gsumr  = err_glb[2];
62887453f775SEmil Constantinescu   n_glb  = err_glb[3];
62897453f775SEmil Constantinescu   na_glb = err_glb[4];
62907453f775SEmil Constantinescu   nr_glb = err_glb[5];
62917453f775SEmil Constantinescu 
6292b1316ef9SEmil Constantinescu   *norm  = 0.;
6293b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
6294b1316ef9SEmil Constantinescu   *norma = 0.;
6295b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
6296b1316ef9SEmil Constantinescu   *normr = 0.;
6297b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
62989c6b16b5SShri Abhyankar 
62999c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63007453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63017453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63029c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
63039c6b16b5SShri Abhyankar }
63049c6b16b5SShri Abhyankar 
63059c6b16b5SShri Abhyankar /*@
6306a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
63079c6b16b5SShri Abhyankar 
63089c6b16b5SShri Abhyankar    Collective on TS
63099c6b16b5SShri Abhyankar 
63109c6b16b5SShri Abhyankar    Input Arguments:
63119c6b16b5SShri Abhyankar +  ts - time stepping context
6312a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6313a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
63149c6b16b5SShri Abhyankar 
63159c6b16b5SShri Abhyankar    Output Arguments:
63167453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
63177453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
63187453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
63199c6b16b5SShri Abhyankar 
63209c6b16b5SShri Abhyankar    Level: developer
63219c6b16b5SShri Abhyankar 
6322deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
63239c6b16b5SShri Abhyankar @*/
63247453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63259c6b16b5SShri Abhyankar {
63269c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
63277453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
63289c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
63297453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
63307453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
63317453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
63329c6b16b5SShri Abhyankar 
63339c6b16b5SShri Abhyankar   PetscFunctionBegin;
63349c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6335a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6336a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6337a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6338a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6339a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6340a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
63418a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
63428a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6343a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
63449c6b16b5SShri Abhyankar 
63459c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
63469c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
63479c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
63489c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
63499c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
63507453f775SEmil Constantinescu 
63517453f775SEmil Constantinescu   max=0.;
63527453f775SEmil Constantinescu   maxa=0.;
63537453f775SEmil Constantinescu   maxr=0.;
63547453f775SEmil Constantinescu 
63557453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
63569c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
63579c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63589c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63597453f775SEmil Constantinescu 
63607453f775SEmil Constantinescu     for (i=0; i<n; i++) {
63617453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
63627453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
63637453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63647453f775SEmil Constantinescu       tol  = tola+tolr;
63657453f775SEmil Constantinescu       if(tola>0.){
63667453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
63677453f775SEmil Constantinescu       }
63687453f775SEmil Constantinescu       if(tolr>0.){
63697453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
63707453f775SEmil Constantinescu       }
63717453f775SEmil Constantinescu       if(tol>0.){
63727453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
63737453f775SEmil Constantinescu       }
63749c6b16b5SShri Abhyankar     }
63759c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63769c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63779c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
63789c6b16b5SShri Abhyankar     const PetscScalar *atol;
63799c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63807453f775SEmil Constantinescu     for (i=0; i<n; i++) {
63817453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
63827453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
63837453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63847453f775SEmil Constantinescu       tol  = tola+tolr;
63857453f775SEmil Constantinescu       if(tola>0.){
63867453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
63877453f775SEmil Constantinescu       }
63887453f775SEmil Constantinescu       if(tolr>0.){
63897453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
63907453f775SEmil Constantinescu       }
63917453f775SEmil Constantinescu       if(tol>0.){
63927453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
63937453f775SEmil Constantinescu       }
63949c6b16b5SShri Abhyankar     }
63959c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63969c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63979c6b16b5SShri Abhyankar     const PetscScalar *rtol;
63989c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63997453f775SEmil Constantinescu 
64007453f775SEmil Constantinescu     for (i=0; i<n; i++) {
64017453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
64027453f775SEmil Constantinescu       tola = ts->atol;
64037453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64047453f775SEmil Constantinescu       tol  = tola+tolr;
64057453f775SEmil Constantinescu       if(tola>0.){
64067453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
64077453f775SEmil Constantinescu       }
64087453f775SEmil Constantinescu       if(tolr>0.){
64097453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
64107453f775SEmil Constantinescu       }
64117453f775SEmil Constantinescu       if(tol>0.){
64127453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
64137453f775SEmil Constantinescu       }
64149c6b16b5SShri Abhyankar     }
64159c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64169c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
64177453f775SEmil Constantinescu 
64187453f775SEmil Constantinescu     for (i=0; i<n; i++) {
64197453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
64207453f775SEmil Constantinescu       tola = ts->atol;
64217453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64227453f775SEmil Constantinescu       tol  = tola+tolr;
64237453f775SEmil Constantinescu       if(tola>0.){
64247453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
64257453f775SEmil Constantinescu       }
64267453f775SEmil Constantinescu       if(tolr>0.){
64277453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
64287453f775SEmil Constantinescu       }
64297453f775SEmil Constantinescu       if(tol>0.){
64307453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
64317453f775SEmil Constantinescu       }
64329c6b16b5SShri Abhyankar     }
64339c6b16b5SShri Abhyankar   }
64349c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
64359c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
64367453f775SEmil Constantinescu   err_loc[0] = max;
64377453f775SEmil Constantinescu   err_loc[1] = maxa;
64387453f775SEmil Constantinescu   err_loc[2] = maxr;
6439a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
64407453f775SEmil Constantinescu   gmax   = err_glb[0];
64417453f775SEmil Constantinescu   gmaxa  = err_glb[1];
64427453f775SEmil Constantinescu   gmaxr  = err_glb[2];
64439c6b16b5SShri Abhyankar 
64449c6b16b5SShri Abhyankar   *norm = gmax;
64457453f775SEmil Constantinescu   *norma = gmaxa;
64467453f775SEmil Constantinescu   *normr = gmaxr;
64479c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
64487453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
64497453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
64509c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
64519c6b16b5SShri Abhyankar }
64529c6b16b5SShri Abhyankar 
64531c3436cfSJed Brown /*@
64548a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
64551c3436cfSJed Brown 
64561c3436cfSJed Brown    Collective on TS
64571c3436cfSJed Brown 
64581c3436cfSJed Brown    Input Arguments:
64591c3436cfSJed Brown +  ts - time stepping context
6460a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6461a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6462a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
64637619abb3SShri 
64641c3436cfSJed Brown    Output Arguments:
64658a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
64668a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
64678a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6468a4868fbcSLisandro Dalcin 
6469a4868fbcSLisandro Dalcin    Options Database Keys:
6470a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6471a4868fbcSLisandro Dalcin 
64721c3436cfSJed Brown    Level: developer
64731c3436cfSJed Brown 
64748a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
64751c3436cfSJed Brown @*/
64767453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
64771c3436cfSJed Brown {
64788beabaa1SBarry Smith   PetscErrorCode ierr;
64791c3436cfSJed Brown 
64801c3436cfSJed Brown   PetscFunctionBegin;
6481a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
64827453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6483a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
64847453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6485a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
64861c3436cfSJed Brown   PetscFunctionReturn(0);
64871c3436cfSJed Brown }
64881c3436cfSJed Brown 
64898a175baeSEmil Constantinescu 
64908a175baeSEmil Constantinescu /*@
64918a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
64928a175baeSEmil Constantinescu 
64938a175baeSEmil Constantinescu    Collective on TS
64948a175baeSEmil Constantinescu 
64958a175baeSEmil Constantinescu    Input Arguments:
64968a175baeSEmil Constantinescu +  ts - time stepping context
64978a175baeSEmil Constantinescu .  E - error vector
64988a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
64998a175baeSEmil Constantinescu -  Y - state vector, previous time step
65008a175baeSEmil Constantinescu 
65018a175baeSEmil Constantinescu    Output Arguments:
65028a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
65038a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
65048a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
65058a175baeSEmil Constantinescu 
65068a175baeSEmil Constantinescu    Level: developer
65078a175baeSEmil Constantinescu 
65088a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
65098a175baeSEmil Constantinescu @*/
65108a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
65118a175baeSEmil Constantinescu {
65128a175baeSEmil Constantinescu   PetscErrorCode    ierr;
65138a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
65148a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
65158a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
65168a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
65178a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
65188a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
65198a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
65208a175baeSEmil Constantinescu 
65218a175baeSEmil Constantinescu   PetscFunctionBegin;
65228a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65238a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
65248a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
65258a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
65268a175baeSEmil Constantinescu   PetscValidType(E,2);
65278a175baeSEmil Constantinescu   PetscValidType(U,3);
65288a175baeSEmil Constantinescu   PetscValidType(Y,4);
65298a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
65308a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
65318a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
65328a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
65338a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
65348a175baeSEmil Constantinescu 
65358a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
65368a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
65378a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
65388a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
65398a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
65408a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
65418a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
65428a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
65438a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
65448a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
65458a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
65468a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
65478a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
65488a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
65498a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
65508a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
65518a175baeSEmil Constantinescu       if(tola>0.){
65528a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
65538a175baeSEmil Constantinescu         na_loc++;
65548a175baeSEmil Constantinescu       }
65558a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
65568a175baeSEmil Constantinescu       if(tolr>0.){
65578a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
65588a175baeSEmil Constantinescu         nr_loc++;
65598a175baeSEmil Constantinescu       }
65608a175baeSEmil Constantinescu       tol=tola+tolr;
65618a175baeSEmil Constantinescu       if(tol>0.){
65628a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
65638a175baeSEmil Constantinescu         n_loc++;
65648a175baeSEmil Constantinescu       }
65658a175baeSEmil Constantinescu     }
65668a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
65678a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
65688a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
65698a175baeSEmil Constantinescu     const PetscScalar *atol;
65708a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
65718a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
65728a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
65738a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
65748a175baeSEmil Constantinescu       if(tola>0.){
65758a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
65768a175baeSEmil Constantinescu         na_loc++;
65778a175baeSEmil Constantinescu       }
65788a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
65798a175baeSEmil Constantinescu       if(tolr>0.){
65808a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
65818a175baeSEmil Constantinescu         nr_loc++;
65828a175baeSEmil Constantinescu       }
65838a175baeSEmil Constantinescu       tol=tola+tolr;
65848a175baeSEmil Constantinescu       if(tol>0.){
65858a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
65868a175baeSEmil Constantinescu         n_loc++;
65878a175baeSEmil Constantinescu       }
65888a175baeSEmil Constantinescu     }
65898a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
65908a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
65918a175baeSEmil Constantinescu     const PetscScalar *rtol;
65928a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
65938a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
65948a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
65958a175baeSEmil Constantinescu       tola = ts->atol;
65968a175baeSEmil Constantinescu       if(tola>0.){
65978a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
65988a175baeSEmil Constantinescu         na_loc++;
65998a175baeSEmil Constantinescu       }
66008a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66018a175baeSEmil Constantinescu       if(tolr>0.){
66028a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
66038a175baeSEmil Constantinescu         nr_loc++;
66048a175baeSEmil Constantinescu       }
66058a175baeSEmil Constantinescu       tol=tola+tolr;
66068a175baeSEmil Constantinescu       if(tol>0.){
66078a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
66088a175baeSEmil Constantinescu         n_loc++;
66098a175baeSEmil Constantinescu       }
66108a175baeSEmil Constantinescu     }
66118a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
66128a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
66138a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
66148a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
66158a175baeSEmil Constantinescu      tola = ts->atol;
66168a175baeSEmil Constantinescu       if(tola>0.){
66178a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
66188a175baeSEmil Constantinescu         na_loc++;
66198a175baeSEmil Constantinescu       }
66208a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
66218a175baeSEmil Constantinescu       if(tolr>0.){
66228a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
66238a175baeSEmil Constantinescu         nr_loc++;
66248a175baeSEmil Constantinescu       }
66258a175baeSEmil Constantinescu       tol=tola+tolr;
66268a175baeSEmil Constantinescu       if(tol>0.){
66278a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
66288a175baeSEmil Constantinescu         n_loc++;
66298a175baeSEmil Constantinescu       }
66308a175baeSEmil Constantinescu     }
66318a175baeSEmil Constantinescu   }
66328a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
66338a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
66348a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
66358a175baeSEmil Constantinescu 
66368a175baeSEmil Constantinescu   err_loc[0] = sum;
66378a175baeSEmil Constantinescu   err_loc[1] = suma;
66388a175baeSEmil Constantinescu   err_loc[2] = sumr;
66398a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
66408a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
66418a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
66428a175baeSEmil Constantinescu 
6643a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
66448a175baeSEmil Constantinescu 
66458a175baeSEmil Constantinescu   gsum   = err_glb[0];
66468a175baeSEmil Constantinescu   gsuma  = err_glb[1];
66478a175baeSEmil Constantinescu   gsumr  = err_glb[2];
66488a175baeSEmil Constantinescu   n_glb  = err_glb[3];
66498a175baeSEmil Constantinescu   na_glb = err_glb[4];
66508a175baeSEmil Constantinescu   nr_glb = err_glb[5];
66518a175baeSEmil Constantinescu 
66528a175baeSEmil Constantinescu   *norm  = 0.;
66538a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
66548a175baeSEmil Constantinescu   *norma = 0.;
66558a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
66568a175baeSEmil Constantinescu   *normr = 0.;
66578a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
66588a175baeSEmil Constantinescu 
66598a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
66608a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
66618a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
66628a175baeSEmil Constantinescu   PetscFunctionReturn(0);
66638a175baeSEmil Constantinescu }
66648a175baeSEmil Constantinescu 
66658a175baeSEmil Constantinescu /*@
66668a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
66678a175baeSEmil Constantinescu    Collective on TS
66688a175baeSEmil Constantinescu 
66698a175baeSEmil Constantinescu    Input Arguments:
66708a175baeSEmil Constantinescu +  ts - time stepping context
66718a175baeSEmil Constantinescu .  E - error vector
66728a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
66738a175baeSEmil Constantinescu -  Y - state vector, previous time step
66748a175baeSEmil Constantinescu 
66758a175baeSEmil Constantinescu    Output Arguments:
66768a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
66778a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
66788a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
66798a175baeSEmil Constantinescu 
66808a175baeSEmil Constantinescu    Level: developer
66818a175baeSEmil Constantinescu 
66828a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
66838a175baeSEmil Constantinescu @*/
66848a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
66858a175baeSEmil Constantinescu {
66868a175baeSEmil Constantinescu   PetscErrorCode    ierr;
66878a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
66888a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
66898a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
66908a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
66918a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
66928a175baeSEmil Constantinescu 
66938a175baeSEmil Constantinescu   PetscFunctionBegin;
66948a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
66958a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
66968a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
66978a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
66988a175baeSEmil Constantinescu   PetscValidType(E,2);
66998a175baeSEmil Constantinescu   PetscValidType(U,3);
67008a175baeSEmil Constantinescu   PetscValidType(Y,4);
67018a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
67028a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
67038a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
67048a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
67058a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
67068a175baeSEmil Constantinescu 
67078a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
67088a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
67098a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
67108a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
67118a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
67128a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
67138a175baeSEmil Constantinescu 
67148a175baeSEmil Constantinescu   max=0.;
67158a175baeSEmil Constantinescu   maxa=0.;
67168a175baeSEmil Constantinescu   maxr=0.;
67178a175baeSEmil Constantinescu 
67188a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
67198a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
67208a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
67218a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
67228a175baeSEmil Constantinescu 
67238a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
67248a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
67258a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
67268a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
67278a175baeSEmil Constantinescu       tol  = tola+tolr;
67288a175baeSEmil Constantinescu       if(tola>0.){
67298a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
67308a175baeSEmil Constantinescu       }
67318a175baeSEmil Constantinescu       if(tolr>0.){
67328a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
67338a175baeSEmil Constantinescu       }
67348a175baeSEmil Constantinescu       if(tol>0.){
67358a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
67368a175baeSEmil Constantinescu       }
67378a175baeSEmil Constantinescu     }
67388a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
67398a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
67408a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
67418a175baeSEmil Constantinescu     const PetscScalar *atol;
67428a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
67438a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
67448a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
67458a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
67468a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
67478a175baeSEmil Constantinescu       tol  = tola+tolr;
67488a175baeSEmil Constantinescu       if(tola>0.){
67498a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
67508a175baeSEmil Constantinescu       }
67518a175baeSEmil Constantinescu       if(tolr>0.){
67528a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
67538a175baeSEmil Constantinescu       }
67548a175baeSEmil Constantinescu       if(tol>0.){
67558a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
67568a175baeSEmil Constantinescu       }
67578a175baeSEmil Constantinescu     }
67588a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
67598a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
67608a175baeSEmil Constantinescu     const PetscScalar *rtol;
67618a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
67628a175baeSEmil Constantinescu 
67638a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
67648a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
67658a175baeSEmil Constantinescu       tola = ts->atol;
67668a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
67678a175baeSEmil Constantinescu       tol  = tola+tolr;
67688a175baeSEmil Constantinescu       if(tola>0.){
67698a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
67708a175baeSEmil Constantinescu       }
67718a175baeSEmil Constantinescu       if(tolr>0.){
67728a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
67738a175baeSEmil Constantinescu       }
67748a175baeSEmil Constantinescu       if(tol>0.){
67758a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
67768a175baeSEmil Constantinescu       }
67778a175baeSEmil Constantinescu     }
67788a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
67798a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
67808a175baeSEmil Constantinescu 
67818a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
67828a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
67838a175baeSEmil Constantinescu       tola = ts->atol;
67848a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
67858a175baeSEmil Constantinescu       tol  = tola+tolr;
67868a175baeSEmil Constantinescu       if(tola>0.){
67878a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
67888a175baeSEmil Constantinescu       }
67898a175baeSEmil Constantinescu       if(tolr>0.){
67908a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
67918a175baeSEmil Constantinescu       }
67928a175baeSEmil Constantinescu       if(tol>0.){
67938a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
67948a175baeSEmil Constantinescu       }
67958a175baeSEmil Constantinescu     }
67968a175baeSEmil Constantinescu   }
67978a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
67988a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
67998a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
68008a175baeSEmil Constantinescu   err_loc[0] = max;
68018a175baeSEmil Constantinescu   err_loc[1] = maxa;
68028a175baeSEmil Constantinescu   err_loc[2] = maxr;
6803a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
68048a175baeSEmil Constantinescu   gmax   = err_glb[0];
68058a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
68068a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
68078a175baeSEmil Constantinescu 
68088a175baeSEmil Constantinescu   *norm = gmax;
68098a175baeSEmil Constantinescu   *norma = gmaxa;
68108a175baeSEmil Constantinescu   *normr = gmaxr;
68118a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
68128a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
68138a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
68148a175baeSEmil Constantinescu   PetscFunctionReturn(0);
68158a175baeSEmil Constantinescu }
68168a175baeSEmil Constantinescu 
68178a175baeSEmil Constantinescu /*@
68188a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
68198a175baeSEmil Constantinescu 
68208a175baeSEmil Constantinescu    Collective on TS
68218a175baeSEmil Constantinescu 
68228a175baeSEmil Constantinescu    Input Arguments:
68238a175baeSEmil Constantinescu +  ts - time stepping context
68248a175baeSEmil Constantinescu .  E - error vector
68258a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
68268a175baeSEmil Constantinescu .  Y - state vector, previous time step
68278a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
68288a175baeSEmil Constantinescu 
68298a175baeSEmil Constantinescu    Output Arguments:
68308a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
68318a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
68328a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
68338a175baeSEmil Constantinescu 
68348a175baeSEmil Constantinescu    Options Database Keys:
68358a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
68368a175baeSEmil Constantinescu 
68378a175baeSEmil Constantinescu    Level: developer
68388a175baeSEmil Constantinescu 
68398a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
68408a175baeSEmil Constantinescu @*/
68418a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
68428a175baeSEmil Constantinescu {
68438a175baeSEmil Constantinescu   PetscErrorCode ierr;
68448a175baeSEmil Constantinescu 
68458a175baeSEmil Constantinescu   PetscFunctionBegin;
68468a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
68478a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
68488a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
68498a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
68508a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
68518a175baeSEmil Constantinescu   PetscFunctionReturn(0);
68528a175baeSEmil Constantinescu }
68538a175baeSEmil Constantinescu 
68548a175baeSEmil Constantinescu 
68558d59e960SJed Brown /*@
68568d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
68578d59e960SJed Brown 
68588d59e960SJed Brown    Logically Collective on TS
68598d59e960SJed Brown 
68608d59e960SJed Brown    Input Arguments:
68618d59e960SJed Brown +  ts - time stepping context
68628d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
68638d59e960SJed Brown 
68648d59e960SJed Brown    Note:
68658d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
68668d59e960SJed Brown 
68678d59e960SJed Brown    Level: intermediate
68688d59e960SJed Brown 
68698d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
68708d59e960SJed Brown @*/
68718d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
68728d59e960SJed Brown {
68738d59e960SJed Brown   PetscFunctionBegin;
68748d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
68758d59e960SJed Brown   ts->cfltime_local = cfltime;
68768d59e960SJed Brown   ts->cfltime       = -1.;
68778d59e960SJed Brown   PetscFunctionReturn(0);
68788d59e960SJed Brown }
68798d59e960SJed Brown 
68808d59e960SJed Brown /*@
68818d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
68828d59e960SJed Brown 
68838d59e960SJed Brown    Collective on TS
68848d59e960SJed Brown 
68858d59e960SJed Brown    Input Arguments:
68868d59e960SJed Brown .  ts - time stepping context
68878d59e960SJed Brown 
68888d59e960SJed Brown    Output Arguments:
68898d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
68908d59e960SJed Brown 
68918d59e960SJed Brown    Level: advanced
68928d59e960SJed Brown 
68938d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
68948d59e960SJed Brown @*/
68958d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
68968d59e960SJed Brown {
68978d59e960SJed Brown   PetscErrorCode ierr;
68988d59e960SJed Brown 
68998d59e960SJed Brown   PetscFunctionBegin;
69008d59e960SJed Brown   if (ts->cfltime < 0) {
6901b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
69028d59e960SJed Brown   }
69038d59e960SJed Brown   *cfltime = ts->cfltime;
69048d59e960SJed Brown   PetscFunctionReturn(0);
69058d59e960SJed Brown }
69068d59e960SJed Brown 
6907d6ebe24aSShri Abhyankar /*@
6908d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6909d6ebe24aSShri Abhyankar 
6910d6ebe24aSShri Abhyankar    Input Parameters:
6911d6ebe24aSShri Abhyankar .  ts   - the TS context.
6912d6ebe24aSShri Abhyankar .  xl   - lower bound.
6913d6ebe24aSShri Abhyankar .  xu   - upper bound.
6914d6ebe24aSShri Abhyankar 
6915d6ebe24aSShri Abhyankar    Notes:
6916d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6917e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6918d6ebe24aSShri Abhyankar 
69192bd2b0e6SSatish Balay    Level: advanced
69202bd2b0e6SSatish Balay 
6921d6ebe24aSShri Abhyankar @*/
6922d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6923d6ebe24aSShri Abhyankar {
6924d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6925d6ebe24aSShri Abhyankar   SNES           snes;
6926d6ebe24aSShri Abhyankar 
6927d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6928d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6929d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6930d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6931d6ebe24aSShri Abhyankar }
6932d6ebe24aSShri Abhyankar 
6933325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6934c6db04a5SJed Brown #include <mex.h>
6935325fc9f4SBarry Smith 
6936325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6937325fc9f4SBarry Smith 
6938325fc9f4SBarry Smith /*
6939325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6940325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6941325fc9f4SBarry Smith 
6942325fc9f4SBarry Smith    Collective on TS
6943325fc9f4SBarry Smith 
6944325fc9f4SBarry Smith    Input Parameters:
6945325fc9f4SBarry Smith +  snes - the TS context
69460910c330SBarry Smith -  u - input vector
6947325fc9f4SBarry Smith 
6948325fc9f4SBarry Smith    Output Parameter:
6949325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6950325fc9f4SBarry Smith 
6951325fc9f4SBarry Smith    Notes:
6952325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6953325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6954325fc9f4SBarry Smith    themselves.
6955325fc9f4SBarry Smith 
6956325fc9f4SBarry Smith    Level: developer
6957325fc9f4SBarry Smith 
6958325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6959325fc9f4SBarry Smith 
6960325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6961325fc9f4SBarry Smith */
69620910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6963325fc9f4SBarry Smith {
6964325fc9f4SBarry Smith   PetscErrorCode  ierr;
6965325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6966325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6967325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6968325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6969325fc9f4SBarry Smith 
6970325fc9f4SBarry Smith   PetscFunctionBegin;
6971325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
69720910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
69730910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6974325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
69750910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6976325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6977325fc9f4SBarry Smith 
6978325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
69790910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
69800910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
69810910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6982bbd56ea5SKarl Rupp 
6983325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6984325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6985325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6986325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6987325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6988325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6989325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6990325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6991325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6992325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6993325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6994325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6995325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6996325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6997325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6998325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6999325fc9f4SBarry Smith   PetscFunctionReturn(0);
7000325fc9f4SBarry Smith }
7001325fc9f4SBarry Smith 
7002325fc9f4SBarry Smith /*
7003325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
7004325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
7005e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
7006325fc9f4SBarry Smith 
7007325fc9f4SBarry Smith    Logically Collective on TS
7008325fc9f4SBarry Smith 
7009325fc9f4SBarry Smith    Input Parameters:
7010325fc9f4SBarry Smith +  ts - the TS context
7011325fc9f4SBarry Smith -  func - function evaluation routine
7012325fc9f4SBarry Smith 
7013325fc9f4SBarry Smith    Calling sequence of func:
70140910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
7015325fc9f4SBarry Smith 
7016325fc9f4SBarry Smith    Level: beginner
7017325fc9f4SBarry Smith 
7018325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
7019325fc9f4SBarry Smith 
7020325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7021325fc9f4SBarry Smith */
7022cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
7023325fc9f4SBarry Smith {
7024325fc9f4SBarry Smith   PetscErrorCode  ierr;
7025325fc9f4SBarry Smith   TSMatlabContext *sctx;
7026325fc9f4SBarry Smith 
7027325fc9f4SBarry Smith   PetscFunctionBegin;
7028325fc9f4SBarry Smith   /* currently sctx is memory bleed */
702995dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
7030325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
7031325fc9f4SBarry Smith   /*
7032325fc9f4SBarry Smith      This should work, but it doesn't
7033325fc9f4SBarry Smith   sctx->ctx = ctx;
7034325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
7035325fc9f4SBarry Smith   */
7036325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
7037bbd56ea5SKarl Rupp 
70380298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
7039325fc9f4SBarry Smith   PetscFunctionReturn(0);
7040325fc9f4SBarry Smith }
7041325fc9f4SBarry Smith 
7042325fc9f4SBarry Smith /*
7043325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
7044325fc9f4SBarry Smith                          TSSetJacobianMatlab().
7045325fc9f4SBarry Smith 
7046325fc9f4SBarry Smith    Collective on TS
7047325fc9f4SBarry Smith 
7048325fc9f4SBarry Smith    Input Parameters:
7049cdcf91faSSean Farley +  ts - the TS context
70500910c330SBarry Smith .  u - input vector
7051325fc9f4SBarry Smith .  A, B - the matrices
7052325fc9f4SBarry Smith -  ctx - user context
7053325fc9f4SBarry Smith 
7054325fc9f4SBarry Smith    Level: developer
7055325fc9f4SBarry Smith 
7056325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
7057325fc9f4SBarry Smith 
7058325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
7059325fc9f4SBarry Smith @*/
7060f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
7061325fc9f4SBarry Smith {
7062325fc9f4SBarry Smith   PetscErrorCode  ierr;
7063325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
7064325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
7065325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
7066325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
7067325fc9f4SBarry Smith 
7068325fc9f4SBarry Smith   PetscFunctionBegin;
7069cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
70700910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
7071325fc9f4SBarry Smith 
70720910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
7073325fc9f4SBarry Smith 
7074cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
70750910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
70760910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
70770910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
70780910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
7079bbd56ea5SKarl Rupp 
7080325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
7081325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
7082325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
7083325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
7084325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
7085325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
7086325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
7087325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
7088325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
7089325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
7090325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
7091325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
7092325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
7093325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
7094325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
7095325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
7096325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
7097325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
7098325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
7099325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
7100325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
7101325fc9f4SBarry Smith   PetscFunctionReturn(0);
7102325fc9f4SBarry Smith }
7103325fc9f4SBarry Smith 
7104325fc9f4SBarry Smith /*
7105325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
7106e3c5b3baSBarry Smith    vector for use by the TS routines in solving ODEs from MATLAB. Here the function is a string containing the name of a MATLAB function
7107325fc9f4SBarry Smith 
7108325fc9f4SBarry Smith    Logically Collective on TS
7109325fc9f4SBarry Smith 
7110325fc9f4SBarry Smith    Input Parameters:
7111cdcf91faSSean Farley +  ts - the TS context
7112325fc9f4SBarry Smith .  A,B - Jacobian matrices
7113325fc9f4SBarry Smith .  func - function evaluation routine
7114325fc9f4SBarry Smith -  ctx - user context
7115325fc9f4SBarry Smith 
7116325fc9f4SBarry Smith    Calling sequence of func:
71170910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
7118325fc9f4SBarry Smith 
7119325fc9f4SBarry Smith    Level: developer
7120325fc9f4SBarry Smith 
7121325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
7122325fc9f4SBarry Smith 
7123325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7124325fc9f4SBarry Smith */
7125cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
7126325fc9f4SBarry Smith {
7127325fc9f4SBarry Smith   PetscErrorCode  ierr;
7128325fc9f4SBarry Smith   TSMatlabContext *sctx;
7129325fc9f4SBarry Smith 
7130325fc9f4SBarry Smith   PetscFunctionBegin;
7131325fc9f4SBarry Smith   /* currently sctx is memory bleed */
713295dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
7133325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
7134325fc9f4SBarry Smith   /*
7135325fc9f4SBarry Smith      This should work, but it doesn't
7136325fc9f4SBarry Smith   sctx->ctx = ctx;
7137325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
7138325fc9f4SBarry Smith   */
7139325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
7140bbd56ea5SKarl Rupp 
7141cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
7142325fc9f4SBarry Smith   PetscFunctionReturn(0);
7143325fc9f4SBarry Smith }
7144325fc9f4SBarry Smith 
7145b5b1a830SBarry Smith /*
7146b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
7147b5b1a830SBarry Smith 
7148b5b1a830SBarry Smith    Collective on TS
7149b5b1a830SBarry Smith 
7150b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
7151b5b1a830SBarry Smith @*/
71520910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
7153b5b1a830SBarry Smith {
7154b5b1a830SBarry Smith   PetscErrorCode  ierr;
7155b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
7156a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
7157b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
7158b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
7159b5b1a830SBarry Smith 
7160b5b1a830SBarry Smith   PetscFunctionBegin;
7161cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
71620910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
7163b5b1a830SBarry Smith 
7164cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
71650910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
7166bbd56ea5SKarl Rupp 
7167b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
7168b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
7169b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
7170b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
7171b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
7172b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
7173b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
7174b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
7175b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
7176b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
7177b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
7178b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
7179b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
7180b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
7181b5b1a830SBarry Smith   PetscFunctionReturn(0);
7182b5b1a830SBarry Smith }
7183b5b1a830SBarry Smith 
7184b5b1a830SBarry Smith /*
7185b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
7186b5b1a830SBarry Smith 
7187b5b1a830SBarry Smith    Level: developer
7188b5b1a830SBarry Smith 
7189b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
7190b5b1a830SBarry Smith 
7191b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
7192b5b1a830SBarry Smith */
7193cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
7194b5b1a830SBarry Smith {
7195b5b1a830SBarry Smith   PetscErrorCode  ierr;
7196b5b1a830SBarry Smith   TSMatlabContext *sctx;
7197b5b1a830SBarry Smith 
7198b5b1a830SBarry Smith   PetscFunctionBegin;
7199b5b1a830SBarry Smith   /* currently sctx is memory bleed */
720095dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
7201b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
7202b5b1a830SBarry Smith   /*
7203b5b1a830SBarry Smith      This should work, but it doesn't
7204b5b1a830SBarry Smith   sctx->ctx = ctx;
7205b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
7206b5b1a830SBarry Smith   */
7207b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
7208bbd56ea5SKarl Rupp 
72090298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
7210b5b1a830SBarry Smith   PetscFunctionReturn(0);
7211b5b1a830SBarry Smith }
7212325fc9f4SBarry Smith #endif
7213b3603a34SBarry Smith 
7214b3603a34SBarry Smith /*@C
72154f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
7216b3603a34SBarry Smith        in a time based line graph
7217b3603a34SBarry Smith 
7218b3603a34SBarry Smith    Collective on TS
7219b3603a34SBarry Smith 
7220b3603a34SBarry Smith    Input Parameters:
7221b3603a34SBarry Smith +  ts - the TS context
7222b3603a34SBarry Smith .  step - current time-step
7223b3603a34SBarry Smith .  ptime - current time
72247db568b7SBarry Smith .  u - current solution
72257db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
7226b3603a34SBarry Smith 
7227b3d3934dSBarry Smith    Options Database:
72289ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
7229b3d3934dSBarry Smith 
7230b3603a34SBarry Smith    Level: intermediate
7231b3603a34SBarry Smith 
72326934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
7233b3603a34SBarry Smith 
7234b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
7235b3603a34SBarry Smith 
72367db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
72377db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
72387db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
72397db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
7240b3603a34SBarry Smith @*/
72417db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7242b3603a34SBarry Smith {
7243b3603a34SBarry Smith   PetscErrorCode    ierr;
72447db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
7245b3603a34SBarry Smith   const PetscScalar *yy;
724680666b62SBarry Smith   Vec               v;
7247b3603a34SBarry Smith 
7248b3603a34SBarry Smith   PetscFunctionBegin;
724963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
725058ff32f7SBarry Smith   if (!step) {
7251a9f9c1f6SBarry Smith     PetscDrawAxis axis;
72526934998bSLisandro Dalcin     PetscInt      dim;
7253a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7254a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
7255bab0a581SBarry Smith     if (!ctx->names) {
7256bab0a581SBarry Smith       PetscBool flg;
7257bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
7258bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
7259bab0a581SBarry Smith       if (flg) {
7260bab0a581SBarry Smith         PetscInt i,n;
7261bab0a581SBarry Smith         char     **names;
7262bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
7263bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
7264bab0a581SBarry Smith         for (i=0; i<n; i++) {
7265bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
7266bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
7267bab0a581SBarry Smith         }
7268bab0a581SBarry Smith         names[n] = NULL;
7269bab0a581SBarry Smith         ctx->names = names;
7270bab0a581SBarry Smith       }
7271bab0a581SBarry Smith     }
7272387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
7273387f4636SBarry Smith       char      **displaynames;
7274387f4636SBarry Smith       PetscBool flg;
7275387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
727695dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
7277387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
7278c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
7279387f4636SBarry Smith       if (flg) {
7280a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
7281387f4636SBarry Smith       }
7282387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
7283387f4636SBarry Smith     }
7284387f4636SBarry Smith     if (ctx->displaynames) {
7285387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
7286387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
7287387f4636SBarry Smith     } else if (ctx->names) {
72880910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
72890b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7290387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
7291b0bc92c6SBarry Smith     } else {
7292b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7293b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7294387f4636SBarry Smith     }
72950b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
729658ff32f7SBarry Smith   }
72976934998bSLisandro Dalcin 
72986934998bSLisandro Dalcin   if (!ctx->transform) v = u;
72996934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
730080666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
73016934998bSLisandro Dalcin   if (ctx->displaynames) {
73026934998bSLisandro Dalcin     PetscInt i;
73036934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
73046934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
73056934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
73066934998bSLisandro Dalcin   } else {
7307e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7308e3efe391SJed Brown     PetscInt  i,n;
73096934998bSLisandro Dalcin     PetscReal *yreal;
731080666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
7311785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7312e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
73130b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7314e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7315e3efe391SJed Brown #else
73160b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7317e3efe391SJed Brown #endif
731880666b62SBarry Smith   }
73196934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
73206934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
73216934998bSLisandro Dalcin 
7322b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
73230b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73246934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
73253923b477SBarry Smith   }
7326b3603a34SBarry Smith   PetscFunctionReturn(0);
7327b3603a34SBarry Smith }
7328b3603a34SBarry Smith 
7329b037adc7SBarry Smith /*@C
733031152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7331b037adc7SBarry Smith 
7332b037adc7SBarry Smith    Collective on TS
7333b037adc7SBarry Smith 
7334b037adc7SBarry Smith    Input Parameters:
7335b037adc7SBarry Smith +  ts - the TS context
7336b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
7337b037adc7SBarry Smith 
7338b037adc7SBarry Smith    Level: intermediate
7339b037adc7SBarry Smith 
73407db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
73417db568b7SBarry Smith 
7342b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
7343b037adc7SBarry Smith 
7344a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
7345b037adc7SBarry Smith @*/
734631152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
7347b037adc7SBarry Smith {
7348b037adc7SBarry Smith   PetscErrorCode    ierr;
7349b037adc7SBarry Smith   PetscInt          i;
7350b037adc7SBarry Smith 
7351b037adc7SBarry Smith   PetscFunctionBegin;
7352b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7353b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
73545537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
7355b3d3934dSBarry Smith       break;
7356b3d3934dSBarry Smith     }
7357b3d3934dSBarry Smith   }
7358b3d3934dSBarry Smith   PetscFunctionReturn(0);
7359b3d3934dSBarry Smith }
7360b3d3934dSBarry Smith 
7361e673d494SBarry Smith /*@C
7362e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7363e673d494SBarry Smith 
7364e673d494SBarry Smith    Collective on TS
7365e673d494SBarry Smith 
7366e673d494SBarry Smith    Input Parameters:
7367e673d494SBarry Smith +  ts - the TS context
7368e673d494SBarry Smith -  names - the names of the components, final string must be NULL
7369e673d494SBarry Smith 
7370e673d494SBarry Smith    Level: intermediate
7371e673d494SBarry Smith 
7372e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7373e673d494SBarry Smith 
7374a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7375e673d494SBarry Smith @*/
7376e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7377e673d494SBarry Smith {
7378e673d494SBarry Smith   PetscErrorCode    ierr;
7379e673d494SBarry Smith 
7380e673d494SBarry Smith   PetscFunctionBegin;
7381e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
7382e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
7383e673d494SBarry Smith   PetscFunctionReturn(0);
7384e673d494SBarry Smith }
7385e673d494SBarry Smith 
7386b3d3934dSBarry Smith /*@C
7387b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
7388b3d3934dSBarry Smith 
7389b3d3934dSBarry Smith    Collective on TS
7390b3d3934dSBarry Smith 
7391b3d3934dSBarry Smith    Input Parameter:
7392b3d3934dSBarry Smith .  ts - the TS context
7393b3d3934dSBarry Smith 
7394b3d3934dSBarry Smith    Output Parameter:
7395b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
7396b3d3934dSBarry Smith 
7397b3d3934dSBarry Smith    Level: intermediate
7398b3d3934dSBarry Smith 
73997db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
74007db568b7SBarry Smith 
7401b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7402b3d3934dSBarry Smith 
7403b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7404b3d3934dSBarry Smith @*/
7405b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7406b3d3934dSBarry Smith {
7407b3d3934dSBarry Smith   PetscInt       i;
7408b3d3934dSBarry Smith 
7409b3d3934dSBarry Smith   PetscFunctionBegin;
7410b3d3934dSBarry Smith   *names = NULL;
7411b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7412b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
74135537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7414b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
7415b3d3934dSBarry Smith       break;
7416387f4636SBarry Smith     }
7417387f4636SBarry Smith   }
7418387f4636SBarry Smith   PetscFunctionReturn(0);
7419387f4636SBarry Smith }
7420387f4636SBarry Smith 
7421a66092f1SBarry Smith /*@C
7422a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
7423a66092f1SBarry Smith 
7424a66092f1SBarry Smith    Collective on TS
7425a66092f1SBarry Smith 
7426a66092f1SBarry Smith    Input Parameters:
7427a66092f1SBarry Smith +  ctx - the TSMonitorLG context
7428a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
7429a66092f1SBarry Smith 
7430a66092f1SBarry Smith    Level: intermediate
7431a66092f1SBarry Smith 
7432a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
7433a66092f1SBarry Smith 
7434a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7435a66092f1SBarry Smith @*/
7436a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7437a66092f1SBarry Smith {
7438a66092f1SBarry Smith   PetscInt          j = 0,k;
7439a66092f1SBarry Smith   PetscErrorCode    ierr;
7440a66092f1SBarry Smith 
7441a66092f1SBarry Smith   PetscFunctionBegin;
7442a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
7443a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
7444a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
7445a66092f1SBarry Smith   while (displaynames[j]) j++;
7446a66092f1SBarry Smith   ctx->ndisplayvariables = j;
7447a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
7448a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
7449a66092f1SBarry Smith   j = 0;
7450a66092f1SBarry Smith   while (displaynames[j]) {
7451a66092f1SBarry Smith     k = 0;
7452a66092f1SBarry Smith     while (ctx->names[k]) {
7453a66092f1SBarry Smith       PetscBool flg;
7454a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
7455a66092f1SBarry Smith       if (flg) {
7456a66092f1SBarry Smith         ctx->displayvariables[j] = k;
7457a66092f1SBarry Smith         break;
7458a66092f1SBarry Smith       }
7459a66092f1SBarry Smith       k++;
7460a66092f1SBarry Smith     }
7461a66092f1SBarry Smith     j++;
7462a66092f1SBarry Smith   }
7463a66092f1SBarry Smith   PetscFunctionReturn(0);
7464a66092f1SBarry Smith }
7465a66092f1SBarry Smith 
7466387f4636SBarry Smith /*@C
7467387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
7468387f4636SBarry Smith 
7469387f4636SBarry Smith    Collective on TS
7470387f4636SBarry Smith 
7471387f4636SBarry Smith    Input Parameters:
7472387f4636SBarry Smith +  ts - the TS context
7473387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
7474387f4636SBarry Smith 
74757db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
74767db568b7SBarry Smith 
7477387f4636SBarry Smith    Level: intermediate
7478387f4636SBarry Smith 
7479387f4636SBarry Smith .keywords: TS,  vector, monitor, view
7480387f4636SBarry Smith 
7481387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7482387f4636SBarry Smith @*/
7483387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7484387f4636SBarry Smith {
7485a66092f1SBarry Smith   PetscInt          i;
7486387f4636SBarry Smith   PetscErrorCode    ierr;
7487387f4636SBarry Smith 
7488387f4636SBarry Smith   PetscFunctionBegin;
7489387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7490387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
74915537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
7492b3d3934dSBarry Smith       break;
7493b037adc7SBarry Smith     }
7494b037adc7SBarry Smith   }
7495b037adc7SBarry Smith   PetscFunctionReturn(0);
7496b037adc7SBarry Smith }
7497b037adc7SBarry Smith 
749880666b62SBarry Smith /*@C
749980666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
750080666b62SBarry Smith 
750180666b62SBarry Smith    Collective on TS
750280666b62SBarry Smith 
750380666b62SBarry Smith    Input Parameters:
750480666b62SBarry Smith +  ts - the TS context
750580666b62SBarry Smith .  transform - the transform function
75067684fa3eSBarry Smith .  destroy - function to destroy the optional context
750780666b62SBarry Smith -  ctx - optional context used by transform function
750880666b62SBarry Smith 
75097db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
75107db568b7SBarry Smith 
751180666b62SBarry Smith    Level: intermediate
751280666b62SBarry Smith 
751380666b62SBarry Smith .keywords: TS,  vector, monitor, view
751480666b62SBarry Smith 
7515a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
751680666b62SBarry Smith @*/
75177684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
751880666b62SBarry Smith {
751980666b62SBarry Smith   PetscInt          i;
7520a66092f1SBarry Smith   PetscErrorCode    ierr;
752180666b62SBarry Smith 
752280666b62SBarry Smith   PetscFunctionBegin;
752380666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
752480666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
75255537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
752680666b62SBarry Smith     }
752780666b62SBarry Smith   }
752880666b62SBarry Smith   PetscFunctionReturn(0);
752980666b62SBarry Smith }
753080666b62SBarry Smith 
7531e673d494SBarry Smith /*@C
7532e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
7533e673d494SBarry Smith 
7534e673d494SBarry Smith    Collective on TSLGCtx
7535e673d494SBarry Smith 
7536e673d494SBarry Smith    Input Parameters:
7537e673d494SBarry Smith +  ts - the TS context
7538e673d494SBarry Smith .  transform - the transform function
75397684fa3eSBarry Smith .  destroy - function to destroy the optional context
7540e673d494SBarry Smith -  ctx - optional context used by transform function
7541e673d494SBarry Smith 
7542e673d494SBarry Smith    Level: intermediate
7543e673d494SBarry Smith 
7544e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7545e673d494SBarry Smith 
7546a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7547e673d494SBarry Smith @*/
75487684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7549e673d494SBarry Smith {
7550e673d494SBarry Smith   PetscFunctionBegin;
7551e673d494SBarry Smith   ctx->transform    = transform;
75527684fa3eSBarry Smith   ctx->transformdestroy = destroy;
7553e673d494SBarry Smith   ctx->transformctx = tctx;
7554e673d494SBarry Smith   PetscFunctionReturn(0);
7555e673d494SBarry Smith }
7556e673d494SBarry Smith 
7557ef20d060SBarry Smith /*@C
75588b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
7559ef20d060SBarry Smith        in a time based line graph
7560ef20d060SBarry Smith 
7561ef20d060SBarry Smith    Collective on TS
7562ef20d060SBarry Smith 
7563ef20d060SBarry Smith    Input Parameters:
7564ef20d060SBarry Smith +  ts - the TS context
7565ef20d060SBarry Smith .  step - current time-step
7566ef20d060SBarry Smith .  ptime - current time
75677db568b7SBarry Smith .  u - current solution
75687db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
7569ef20d060SBarry Smith 
7570ef20d060SBarry Smith    Level: intermediate
7571ef20d060SBarry Smith 
75726934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
7573abd5a294SJed Brown 
7574abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
7575abd5a294SJed Brown 
7576abd5a294SJed Brown    Options Database Keys:
75774f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
7578ef20d060SBarry Smith 
7579ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
7580ef20d060SBarry Smith 
7581abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7582ef20d060SBarry Smith @*/
75830910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7584ef20d060SBarry Smith {
7585ef20d060SBarry Smith   PetscErrorCode    ierr;
75860b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7587ef20d060SBarry Smith   const PetscScalar *yy;
7588ef20d060SBarry Smith   Vec               y;
7589ef20d060SBarry Smith 
7590ef20d060SBarry Smith   PetscFunctionBegin;
7591a9f9c1f6SBarry Smith   if (!step) {
7592a9f9c1f6SBarry Smith     PetscDrawAxis axis;
75936934998bSLisandro Dalcin     PetscInt      dim;
7594a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
75958b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
75960910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7597a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7598a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7599a9f9c1f6SBarry Smith   }
76000910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
7601ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
76020910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7603ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
7604e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7605e3efe391SJed Brown   {
7606e3efe391SJed Brown     PetscReal *yreal;
7607e3efe391SJed Brown     PetscInt  i,n;
7608e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
7609785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7610e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
76110b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7612e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7613e3efe391SJed Brown   }
7614e3efe391SJed Brown #else
76150b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7616e3efe391SJed Brown #endif
7617ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
7618ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
7619b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
76200b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
76216934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
76223923b477SBarry Smith   }
7623ef20d060SBarry Smith   PetscFunctionReturn(0);
7624ef20d060SBarry Smith }
7625ef20d060SBarry Smith 
76267cf37e64SBarry Smith /*@C
76277cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
76287cf37e64SBarry Smith 
76297cf37e64SBarry Smith    Collective on TS
76307cf37e64SBarry Smith 
76317cf37e64SBarry Smith    Input Parameters:
76327cf37e64SBarry Smith +  ts - the TS context
76337cf37e64SBarry Smith .  step - current time-step
76347cf37e64SBarry Smith .  ptime - current time
76357cf37e64SBarry Smith .  u - current solution
76367cf37e64SBarry Smith -  dctx - unused context
76377cf37e64SBarry Smith 
76387cf37e64SBarry Smith    Level: intermediate
76397cf37e64SBarry Smith 
76407cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
76417cf37e64SBarry Smith 
76427cf37e64SBarry Smith    Options Database Keys:
76437cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
76447cf37e64SBarry Smith 
76457cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
76467cf37e64SBarry Smith 
76477cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
76487cf37e64SBarry Smith @*/
7649edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
76507cf37e64SBarry Smith {
76517cf37e64SBarry Smith   PetscErrorCode    ierr;
76527cf37e64SBarry Smith   Vec               y;
76537cf37e64SBarry Smith   PetscReal         nrm;
7654edbaebb3SBarry Smith   PetscBool         flg;
76557cf37e64SBarry Smith 
76567cf37e64SBarry Smith   PetscFunctionBegin;
76577cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
76587cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
76597cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7660edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7661edbaebb3SBarry Smith   if (flg) {
76627cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7663edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7664edbaebb3SBarry Smith   }
7665edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7666edbaebb3SBarry Smith   if (flg) {
7667edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7668edbaebb3SBarry Smith   }
7669edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
76707cf37e64SBarry Smith   PetscFunctionReturn(0);
76717cf37e64SBarry Smith }
76727cf37e64SBarry Smith 
7673201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7674201da799SBarry Smith {
7675201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7676201da799SBarry Smith   PetscReal      x   = ptime,y;
7677201da799SBarry Smith   PetscErrorCode ierr;
7678201da799SBarry Smith   PetscInt       its;
7679201da799SBarry Smith 
7680201da799SBarry Smith   PetscFunctionBegin;
768163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7682201da799SBarry Smith   if (!n) {
7683201da799SBarry Smith     PetscDrawAxis axis;
7684201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7685201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7686201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7687201da799SBarry Smith     ctx->snes_its = 0;
7688201da799SBarry Smith   }
7689201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7690201da799SBarry Smith   y    = its - ctx->snes_its;
7691201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
76923fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7693201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
76946934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7695201da799SBarry Smith   }
7696201da799SBarry Smith   ctx->snes_its = its;
7697201da799SBarry Smith   PetscFunctionReturn(0);
7698201da799SBarry Smith }
7699201da799SBarry Smith 
7700201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7701201da799SBarry Smith {
7702201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7703201da799SBarry Smith   PetscReal      x   = ptime,y;
7704201da799SBarry Smith   PetscErrorCode ierr;
7705201da799SBarry Smith   PetscInt       its;
7706201da799SBarry Smith 
7707201da799SBarry Smith   PetscFunctionBegin;
770863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7709201da799SBarry Smith   if (!n) {
7710201da799SBarry Smith     PetscDrawAxis axis;
7711201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7712201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7713201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7714201da799SBarry Smith     ctx->ksp_its = 0;
7715201da799SBarry Smith   }
7716201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7717201da799SBarry Smith   y    = its - ctx->ksp_its;
7718201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
771999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7720201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
77216934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7722201da799SBarry Smith   }
7723201da799SBarry Smith   ctx->ksp_its = its;
7724201da799SBarry Smith   PetscFunctionReturn(0);
7725201da799SBarry Smith }
7726f9c1d6abSBarry Smith 
7727f9c1d6abSBarry Smith /*@
7728f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7729f9c1d6abSBarry Smith 
7730f9c1d6abSBarry Smith    Collective on TS and Vec
7731f9c1d6abSBarry Smith 
7732f9c1d6abSBarry Smith    Input Parameters:
7733f9c1d6abSBarry Smith +  ts - the TS context
7734f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7735f9c1d6abSBarry Smith 
7736f9c1d6abSBarry Smith    Output Parameters:
7737f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7738f9c1d6abSBarry Smith 
7739f9c1d6abSBarry Smith    Level: developer
7740f9c1d6abSBarry Smith 
7741f9c1d6abSBarry Smith .keywords: TS, compute
7742f9c1d6abSBarry Smith 
7743f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7744f9c1d6abSBarry Smith @*/
7745f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7746f9c1d6abSBarry Smith {
7747f9c1d6abSBarry Smith   PetscErrorCode ierr;
7748f9c1d6abSBarry Smith 
7749f9c1d6abSBarry Smith   PetscFunctionBegin;
7750f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7751ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7752f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7753f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7754f9c1d6abSBarry Smith }
775524655328SShri 
7756b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7757b3d3934dSBarry Smith /*@C
7758b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7759b3d3934dSBarry Smith 
7760b3d3934dSBarry Smith    Collective on TS
7761b3d3934dSBarry Smith 
7762b3d3934dSBarry Smith    Input Parameters:
7763b3d3934dSBarry Smith .  ts  - the ODE solver object
7764b3d3934dSBarry Smith 
7765b3d3934dSBarry Smith    Output Parameter:
7766b3d3934dSBarry Smith .  ctx - the context
7767b3d3934dSBarry Smith 
7768b3d3934dSBarry Smith    Level: intermediate
7769b3d3934dSBarry Smith 
7770b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7771b3d3934dSBarry Smith 
7772b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7773b3d3934dSBarry Smith 
7774b3d3934dSBarry Smith @*/
7775b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7776b3d3934dSBarry Smith {
7777b3d3934dSBarry Smith   PetscErrorCode ierr;
7778b3d3934dSBarry Smith 
7779b3d3934dSBarry Smith   PetscFunctionBegin;
7780a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7781b3d3934dSBarry Smith   PetscFunctionReturn(0);
7782b3d3934dSBarry Smith }
7783b3d3934dSBarry Smith 
7784b3d3934dSBarry Smith /*@C
7785b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7786b3d3934dSBarry Smith 
7787b3d3934dSBarry Smith    Collective on TS
7788b3d3934dSBarry Smith 
7789b3d3934dSBarry Smith    Input Parameters:
7790b3d3934dSBarry Smith +  ts - the TS context
7791b3d3934dSBarry Smith .  step - current time-step
7792b3d3934dSBarry Smith .  ptime - current time
77937db568b7SBarry Smith .  u  - current solution
77947db568b7SBarry Smith -  dctx - the envelope context
7795b3d3934dSBarry Smith 
7796b3d3934dSBarry Smith    Options Database:
7797b3d3934dSBarry Smith .  -ts_monitor_envelope
7798b3d3934dSBarry Smith 
7799b3d3934dSBarry Smith    Level: intermediate
7800b3d3934dSBarry Smith 
7801b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7802b3d3934dSBarry Smith 
7803b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7804b3d3934dSBarry Smith 
78057db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7806b3d3934dSBarry Smith @*/
78077db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7808b3d3934dSBarry Smith {
7809b3d3934dSBarry Smith   PetscErrorCode       ierr;
78107db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7811b3d3934dSBarry Smith 
7812b3d3934dSBarry Smith   PetscFunctionBegin;
7813b3d3934dSBarry Smith   if (!ctx->max) {
7814b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7815b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7816b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7817b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7818b3d3934dSBarry Smith   } else {
7819b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7820b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7821b3d3934dSBarry Smith   }
7822b3d3934dSBarry Smith   PetscFunctionReturn(0);
7823b3d3934dSBarry Smith }
7824b3d3934dSBarry Smith 
7825b3d3934dSBarry Smith /*@C
7826b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7827b3d3934dSBarry Smith 
7828b3d3934dSBarry Smith    Collective on TS
7829b3d3934dSBarry Smith 
7830b3d3934dSBarry Smith    Input Parameter:
7831b3d3934dSBarry Smith .  ts - the TS context
7832b3d3934dSBarry Smith 
7833b3d3934dSBarry Smith    Output Parameter:
7834b3d3934dSBarry Smith +  max - the maximum values
7835b3d3934dSBarry Smith -  min - the minimum values
7836b3d3934dSBarry Smith 
78377db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
78387db568b7SBarry Smith 
7839b3d3934dSBarry Smith    Level: intermediate
7840b3d3934dSBarry Smith 
7841b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7842b3d3934dSBarry Smith 
7843b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7844b3d3934dSBarry Smith @*/
7845b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7846b3d3934dSBarry Smith {
7847b3d3934dSBarry Smith   PetscInt i;
7848b3d3934dSBarry Smith 
7849b3d3934dSBarry Smith   PetscFunctionBegin;
7850b3d3934dSBarry Smith   if (max) *max = NULL;
7851b3d3934dSBarry Smith   if (min) *min = NULL;
7852b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7853b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
78545537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7855b3d3934dSBarry Smith       if (max) *max = ctx->max;
7856b3d3934dSBarry Smith       if (min) *min = ctx->min;
7857b3d3934dSBarry Smith       break;
7858b3d3934dSBarry Smith     }
7859b3d3934dSBarry Smith   }
7860b3d3934dSBarry Smith   PetscFunctionReturn(0);
7861b3d3934dSBarry Smith }
7862b3d3934dSBarry Smith 
7863b3d3934dSBarry Smith /*@C
7864b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7865b3d3934dSBarry Smith 
7866b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7867b3d3934dSBarry Smith 
7868b3d3934dSBarry Smith    Input Parameter:
7869b3d3934dSBarry Smith .  ctx - the monitor context
7870b3d3934dSBarry Smith 
7871b3d3934dSBarry Smith    Level: intermediate
7872b3d3934dSBarry Smith 
7873b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7874b3d3934dSBarry Smith 
78757db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7876b3d3934dSBarry Smith @*/
7877b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7878b3d3934dSBarry Smith {
7879b3d3934dSBarry Smith   PetscErrorCode ierr;
7880b3d3934dSBarry Smith 
7881b3d3934dSBarry Smith   PetscFunctionBegin;
7882b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7883b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7884b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7885b3d3934dSBarry Smith   PetscFunctionReturn(0);
7886b3d3934dSBarry Smith }
7887f2dee214SBarry Smith 
788824655328SShri /*@
7889dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7890dcb233daSLisandro Dalcin 
7891dcb233daSLisandro Dalcin    Collective on TS
7892dcb233daSLisandro Dalcin 
7893dcb233daSLisandro Dalcin    Input Parameter:
7894dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7895dcb233daSLisandro Dalcin 
7896dcb233daSLisandro Dalcin    Level: advanced
7897dcb233daSLisandro Dalcin 
7898dcb233daSLisandro Dalcin    Notes:
7899dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7900dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7901dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7902dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7903dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7904dcb233daSLisandro Dalcin    discontinuous source terms).
7905dcb233daSLisandro Dalcin 
7906dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7907dcb233daSLisandro Dalcin 
7908dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7909dcb233daSLisandro Dalcin @*/
7910dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7911dcb233daSLisandro Dalcin {
7912dcb233daSLisandro Dalcin   PetscFunctionBegin;
7913dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7914dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7915dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7916dcb233daSLisandro Dalcin }
7917dcb233daSLisandro Dalcin 
7918dcb233daSLisandro Dalcin /*@
791924655328SShri    TSRollBack - Rolls back one time step
792024655328SShri 
792124655328SShri    Collective on TS
792224655328SShri 
792324655328SShri    Input Parameter:
792424655328SShri .  ts - the TS context obtained from TSCreate()
792524655328SShri 
792624655328SShri    Level: advanced
792724655328SShri 
792824655328SShri .keywords: TS, timestep, rollback
792924655328SShri 
793024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
793124655328SShri @*/
793224655328SShri PetscErrorCode  TSRollBack(TS ts)
793324655328SShri {
793424655328SShri   PetscErrorCode ierr;
793524655328SShri 
793624655328SShri   PetscFunctionBegin;
793724655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7938b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
793924655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
794024655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
794124655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
794224655328SShri   ts->ptime = ts->ptime_prev;
7943be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
79442808aa04SLisandro Dalcin   ts->steps--;
7945b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
794624655328SShri   PetscFunctionReturn(0);
794724655328SShri }
7948aeb4809dSShri Abhyankar 
7949ff22ae23SHong Zhang /*@
7950ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7951ff22ae23SHong Zhang 
7952ff22ae23SHong Zhang    Input Parameter:
7953ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7954ff22ae23SHong Zhang 
7955ff22ae23SHong Zhang    Level: advanced
7956ff22ae23SHong Zhang 
7957ff22ae23SHong Zhang .keywords: TS, getstages
7958ff22ae23SHong Zhang 
7959ff22ae23SHong Zhang .seealso: TSCreate()
7960ff22ae23SHong Zhang @*/
7961ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7962ff22ae23SHong Zhang {
7963ff22ae23SHong Zhang   PetscErrorCode ierr;
7964ff22ae23SHong Zhang 
7965ff22ae23SHong Zhang   PetscFunctionBegin;
7966ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7967ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7968ff22ae23SHong Zhang 
7969ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7970ff22ae23SHong Zhang   else {
7971ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7972ff22ae23SHong Zhang   }
7973ff22ae23SHong Zhang   PetscFunctionReturn(0);
7974ff22ae23SHong Zhang }
7975ff22ae23SHong Zhang 
7976847ff0e1SMatthew G. Knepley /*@C
7977847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7978847ff0e1SMatthew G. Knepley 
7979847ff0e1SMatthew G. Knepley   Collective on SNES
7980847ff0e1SMatthew G. Knepley 
7981847ff0e1SMatthew G. Knepley   Input Parameters:
7982847ff0e1SMatthew G. Knepley + ts - the TS context
7983847ff0e1SMatthew G. Knepley . t - current timestep
7984847ff0e1SMatthew G. Knepley . U - state vector
7985847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7986847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7987847ff0e1SMatthew G. Knepley - ctx - an optional user context
7988847ff0e1SMatthew G. Knepley 
7989847ff0e1SMatthew G. Knepley   Output Parameters:
7990847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7991847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7992847ff0e1SMatthew G. Knepley 
7993847ff0e1SMatthew G. Knepley   Level: intermediate
7994847ff0e1SMatthew G. Knepley 
7995847ff0e1SMatthew G. Knepley   Notes:
7996847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7997847ff0e1SMatthew G. Knepley 
7998847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7999847ff0e1SMatthew G. Knepley 
8000847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
8001847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
8002847ff0e1SMatthew G. Knepley 
8003847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
8004847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
8005847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
8006847ff0e1SMatthew G. Knepley 
8007847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
8008847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
8009847ff0e1SMatthew G. Knepley @*/
8010847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
8011847ff0e1SMatthew G. Knepley {
8012847ff0e1SMatthew G. Knepley   SNES           snes;
8013847ff0e1SMatthew G. Knepley   MatFDColoring  color;
8014847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
8015847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
8016847ff0e1SMatthew G. Knepley 
8017847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
8018c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
8019847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
8020847ff0e1SMatthew G. Knepley   if (!color) {
8021847ff0e1SMatthew G. Knepley     DM         dm;
8022847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
8023847ff0e1SMatthew G. Knepley 
8024847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
8025847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
8026847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
8027847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
8028847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
8029847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
8030847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
8031847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
8032847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
8033847ff0e1SMatthew G. Knepley     } else {
8034847ff0e1SMatthew G. Knepley       MatColoring mc;
8035847ff0e1SMatthew G. Knepley 
8036847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
8037847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
8038847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
8039847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
8040847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
8041847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
8042847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
8043847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
8044847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
8045847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
8046847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
8047847ff0e1SMatthew G. Knepley     }
8048847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
8049847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
8050847ff0e1SMatthew G. Knepley   }
8051847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
8052847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
8053847ff0e1SMatthew G. Knepley   if (J != B) {
8054847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8055847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8056847ff0e1SMatthew G. Knepley   }
8057847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
8058847ff0e1SMatthew G. Knepley }
805993b34091SDebojyoti Ghosh 
8060cb9d8021SPierre Barbier de Reuille /*@
8061cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
8062cb9d8021SPierre Barbier de Reuille 
8063cb9d8021SPierre Barbier de Reuille     Input Parameters:
8064cb9d8021SPierre Barbier de Reuille     ts - the TS context
8065cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
8066cb9d8021SPierre Barbier de Reuille 
8067cb9d8021SPierre Barbier de Reuille     Level: intermediate
8068cb9d8021SPierre Barbier de Reuille 
8069cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
8070cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
8071cb9d8021SPierre Barbier de Reuille @*/
8072cb9d8021SPierre Barbier de Reuille 
8073d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
8074cb9d8021SPierre Barbier de Reuille {
8075cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
8076cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
8077cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
8078cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
8079cb9d8021SPierre Barbier de Reuille }
8080cb9d8021SPierre Barbier de Reuille 
8081cb9d8021SPierre Barbier de Reuille /*@
8082cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
8083cb9d8021SPierre Barbier de Reuille 
8084cb9d8021SPierre Barbier de Reuille     Input Parameters:
8085cb9d8021SPierre Barbier de Reuille     ts - the TS context
8086cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
8087d183316bSPierre Barbier de Reuille     Y - state vector to check.
8088cb9d8021SPierre Barbier de Reuille 
8089cb9d8021SPierre Barbier de Reuille     Output Parameter:
8090cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
8091cb9d8021SPierre Barbier de Reuille 
8092cb9d8021SPierre Barbier de Reuille     Note:
8093cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
8094cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
809596a0c994SBarry Smith 
809696a0c994SBarry Smith     Level: advanced
8097cb9d8021SPierre Barbier de Reuille @*/
8098d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
8099cb9d8021SPierre Barbier de Reuille {
8100cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
8101cb9d8021SPierre Barbier de Reuille 
8102cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
8103cb9d8021SPierre Barbier de Reuille 
8104cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8105cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
8106cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
8107d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
8108cb9d8021SPierre Barbier de Reuille   }
8109cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
8110cb9d8021SPierre Barbier de Reuille }
81111ceb14c0SBarry Smith 
811293b34091SDebojyoti Ghosh /*@C
8113e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
811493b34091SDebojyoti Ghosh 
811593b34091SDebojyoti Ghosh   Collective on MPI_Comm
811693b34091SDebojyoti Ghosh 
811793b34091SDebojyoti Ghosh   Input Parameter:
811893b34091SDebojyoti Ghosh . tsin    - The input TS
811993b34091SDebojyoti Ghosh 
812093b34091SDebojyoti Ghosh   Output Parameter:
8121e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
812293b34091SDebojyoti Ghosh 
81235eca1a21SEmil Constantinescu   Notes:
81245eca1a21SEmil 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.
81255eca1a21SEmil Constantinescu 
8126baa10174SEmil Constantinescu   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); ierr = TSSetSNES(ts,snes_dup);
81275eca1a21SEmil Constantinescu 
81285eca1a21SEmil Constantinescu   Level: developer
812993b34091SDebojyoti Ghosh 
8130e5168f73SEmil Constantinescu .keywords: TS, clone
8131e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
813293b34091SDebojyoti Ghosh @*/
8133baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
813493b34091SDebojyoti Ghosh {
813593b34091SDebojyoti Ghosh   TS             t;
813693b34091SDebojyoti Ghosh   PetscErrorCode ierr;
8137dc846ba4SSatish Balay   SNES           snes_start;
8138dc846ba4SSatish Balay   DM             dm;
8139dc846ba4SSatish Balay   TSType         type;
814093b34091SDebojyoti Ghosh 
814193b34091SDebojyoti Ghosh   PetscFunctionBegin;
814293b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
814393b34091SDebojyoti Ghosh   *tsout = NULL;
814493b34091SDebojyoti Ghosh 
81457a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
814693b34091SDebojyoti Ghosh 
814793b34091SDebojyoti Ghosh   /* General TS description */
814893b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
814993b34091SDebojyoti Ghosh   t->setupcalled       = 0;
815093b34091SDebojyoti Ghosh   t->ksp_its           = 0;
815193b34091SDebojyoti Ghosh   t->snes_its          = 0;
815293b34091SDebojyoti Ghosh   t->nwork             = 0;
815393b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
815493b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
815593b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
815693b34091SDebojyoti Ghosh 
815734561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
815834561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
8159d15a3a53SEmil Constantinescu 
816093b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
816193b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
816293b34091SDebojyoti Ghosh 
816393b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
816451699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
816593b34091SDebojyoti Ghosh 
8166e7069c78SShri   t->trajectory = tsin->trajectory;
8167e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
8168e7069c78SShri 
8169e7069c78SShri   t->event = tsin->event;
81706b10a48eSSatish Balay   if (t->event) t->event->refct++;
8171e7069c78SShri 
817293b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
817393b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
8174e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
817593b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
817693b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
817793b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
817893b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
817993b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
818093b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
818193b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
818293b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
818393b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
818493b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
818593b34091SDebojyoti Ghosh 
818693b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
818793b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
818893b34091SDebojyoti Ghosh 
818993b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
819093b34091SDebojyoti Ghosh 
819193b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
819293b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
819393b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
819493b34091SDebojyoti Ghosh 
819593b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
819693b34091SDebojyoti Ghosh 
81970d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
81980d4fed19SBarry Smith     PetscInt i;
81990d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
82000d4fed19SBarry Smith     for (i=0; i<10; i++) {
82010d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
82020d4fed19SBarry Smith     }
82030d4fed19SBarry Smith   }
820493b34091SDebojyoti Ghosh   *tsout = t;
820593b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
820693b34091SDebojyoti Ghosh }
8207f3b1f45cSBarry Smith 
8208f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
8209f3b1f45cSBarry Smith {
8210f3b1f45cSBarry Smith   PetscErrorCode ierr;
8211f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
8212f3b1f45cSBarry Smith 
8213f3b1f45cSBarry Smith   PetscFunctionBegin;
8214f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
8215f3b1f45cSBarry Smith   PetscFunctionReturn(0);
8216f3b1f45cSBarry Smith }
8217f3b1f45cSBarry Smith 
8218f3b1f45cSBarry Smith /*@
8219f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
8220f3b1f45cSBarry Smith 
8221f3b1f45cSBarry Smith    Logically Collective on TS and Mat
8222f3b1f45cSBarry Smith 
8223f3b1f45cSBarry Smith     Input Parameters:
8224f3b1f45cSBarry Smith     TS - the time stepping routine
8225f3b1f45cSBarry Smith 
8226f3b1f45cSBarry Smith    Output Parameter:
8227f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
8228f3b1f45cSBarry Smith 
8229f3b1f45cSBarry Smith    Options Database:
8230f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
8231f3b1f45cSBarry Smith 
8232f3b1f45cSBarry Smith    Level: advanced
8233f3b1f45cSBarry Smith 
8234f3b1f45cSBarry Smith    Notes: This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
8235f3b1f45cSBarry Smith 
8236f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
8237f3b1f45cSBarry Smith @*/
8238f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
8239f3b1f45cSBarry Smith {
8240f3b1f45cSBarry Smith   Mat            J,B;
8241f3b1f45cSBarry Smith   PetscErrorCode ierr;
8242f3b1f45cSBarry Smith   TSRHSJacobian  func;
8243f3b1f45cSBarry Smith   void*          ctx;
8244f3b1f45cSBarry Smith 
8245f3b1f45cSBarry Smith   PetscFunctionBegin;
8246f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
8247f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
8248f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
8249f3b1f45cSBarry Smith   PetscFunctionReturn(0);
8250f3b1f45cSBarry Smith }
8251f3b1f45cSBarry Smith 
8252f3b1f45cSBarry Smith /*@C
8253f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
8254f3b1f45cSBarry Smith 
8255f3b1f45cSBarry Smith    Logically Collective on TS and Mat
8256f3b1f45cSBarry Smith 
8257f3b1f45cSBarry Smith     Input Parameters:
8258f3b1f45cSBarry Smith     TS - the time stepping routine
8259f3b1f45cSBarry Smith 
8260f3b1f45cSBarry Smith    Output Parameter:
8261f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
8262f3b1f45cSBarry Smith 
8263f3b1f45cSBarry Smith    Options Database:
8264f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
8265f3b1f45cSBarry Smith 
8266f3b1f45cSBarry Smith    Notes: This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
8267f3b1f45cSBarry Smith 
8268f3b1f45cSBarry Smith    Level: advanced
8269f3b1f45cSBarry Smith 
8270f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
8271f3b1f45cSBarry Smith @*/
8272f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
8273f3b1f45cSBarry Smith {
8274f3b1f45cSBarry Smith   Mat            J,B;
8275f3b1f45cSBarry Smith   PetscErrorCode ierr;
8276f3b1f45cSBarry Smith   void           *ctx;
8277f3b1f45cSBarry Smith   TSRHSJacobian  func;
8278f3b1f45cSBarry Smith 
8279f3b1f45cSBarry Smith   PetscFunctionBegin;
8280f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
8281f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
8282f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
8283f3b1f45cSBarry Smith   PetscFunctionReturn(0);
8284f3b1f45cSBarry Smith }
8285