xref: /petsc/src/ts/interface/ts.c (revision 287c2655d648353bfb2fc9b0abbaccecc8a8143c)
163dd3a1aSKris Buschelman 
2af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
41e25c274SJed Brown #include <petscdmda.h>
52d5ee99bSBarry Smith #include <petscviewer.h>
62d5ee99bSBarry Smith #include <petscdraw.h>
7d763cef2SBarry Smith 
8d5ba7fb7SMatthew Knepley /* Logging support */
9d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
108d0ad7a8SHong Zhang PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
11d405a339SMatthew Knepley 
12feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
152d5ee99bSBarry Smith   PetscViewer   viewer;
162d5ee99bSBarry Smith   Vec           initialsolution;
172d5ee99bSBarry Smith   PetscBool     showinitial;
182d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
192d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
202d5ee99bSBarry Smith };
212d5ee99bSBarry Smith 
22fde5950dSBarry Smith /*@C
23fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
24fde5950dSBarry Smith 
25fde5950dSBarry Smith    Collective on TS
26fde5950dSBarry Smith 
27fde5950dSBarry Smith    Input Parameters:
28fde5950dSBarry Smith +  ts - TS object you wish to monitor
29fde5950dSBarry Smith .  name - the monitor type one is seeking
30fde5950dSBarry Smith .  help - message indicating what monitoring is done
31fde5950dSBarry Smith .  manual - manual page for the monitor
32fde5950dSBarry Smith .  monitor - the monitor function
33fde5950dSBarry 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
34fde5950dSBarry Smith 
35fde5950dSBarry Smith    Level: developer
36fde5950dSBarry Smith 
37fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
38fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
39fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
40fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
41fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
42fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
43fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
44fde5950dSBarry Smith @*/
45721cd6eeSBarry 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*))
46fde5950dSBarry Smith {
47fde5950dSBarry Smith   PetscErrorCode    ierr;
48fde5950dSBarry Smith   PetscViewer       viewer;
49fde5950dSBarry Smith   PetscViewerFormat format;
50fde5950dSBarry Smith   PetscBool         flg;
51fde5950dSBarry Smith 
52fde5950dSBarry Smith   PetscFunctionBegin;
53fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
54fde5950dSBarry Smith   if (flg) {
55721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
56721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
57721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
58fde5950dSBarry Smith     if (monitorsetup) {
59721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
60fde5950dSBarry Smith     }
61721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
62fde5950dSBarry Smith   }
63fde5950dSBarry Smith   PetscFunctionReturn(0);
64fde5950dSBarry Smith }
65fde5950dSBarry Smith 
66fde5950dSBarry Smith /*@C
67*287c2655SBarry Smith    TSAdjointMonitorGradient - monitors the first lambda gradient
68*287c2655SBarry Smith 
69*287c2655SBarry Smith    Level: intermediate
70*287c2655SBarry Smith 
71*287c2655SBarry Smith .keywords: TS, set, monitor
72*287c2655SBarry Smith 
73*287c2655SBarry Smith .seealso: TSAdjointMonitorSet()
74*287c2655SBarry Smith @*/
75*287c2655SBarry Smith PetscErrorCode TSAdjointMonitorGradient(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
76*287c2655SBarry Smith {
77*287c2655SBarry Smith   PetscErrorCode ierr;
78*287c2655SBarry Smith   PetscViewer    viewer = vf->viewer;
79*287c2655SBarry Smith 
80*287c2655SBarry Smith   PetscFunctionBegin;
81*287c2655SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
82*287c2655SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
83*287c2655SBarry Smith   ierr = VecView(lambda[0],viewer);CHKERRQ(ierr);
84*287c2655SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
85*287c2655SBarry Smith   PetscFunctionReturn(0);
86*287c2655SBarry Smith }
87*287c2655SBarry Smith 
88*287c2655SBarry Smith /*@C
89fde5950dSBarry Smith    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
90fde5950dSBarry Smith 
91fde5950dSBarry Smith    Collective on TS
92fde5950dSBarry Smith 
93fde5950dSBarry Smith    Input Parameters:
94fde5950dSBarry Smith +  ts - TS object you wish to monitor
95fde5950dSBarry Smith .  name - the monitor type one is seeking
96fde5950dSBarry Smith .  help - message indicating what monitoring is done
97fde5950dSBarry Smith .  manual - manual page for the monitor
98fde5950dSBarry Smith .  monitor - the monitor function
99fde5950dSBarry 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
100fde5950dSBarry Smith 
101fde5950dSBarry Smith    Level: developer
102fde5950dSBarry Smith 
103fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
104fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
105fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
106fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
107fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
108fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
109fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
110fde5950dSBarry Smith @*/
111721cd6eeSBarry 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*))
112fde5950dSBarry Smith {
113fde5950dSBarry Smith   PetscErrorCode    ierr;
114fde5950dSBarry Smith   PetscViewer       viewer;
115fde5950dSBarry Smith   PetscViewerFormat format;
116fde5950dSBarry Smith   PetscBool         flg;
117fde5950dSBarry Smith 
118fde5950dSBarry Smith   PetscFunctionBegin;
119fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
120fde5950dSBarry Smith   if (flg) {
121721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
122721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
123721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
124fde5950dSBarry Smith     if (monitorsetup) {
125721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
126fde5950dSBarry Smith     }
127721cd6eeSBarry Smith     ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
128fde5950dSBarry Smith   }
129fde5950dSBarry Smith   PetscFunctionReturn(0);
130fde5950dSBarry Smith }
131fde5950dSBarry Smith 
1322ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
1332ffb9264SLisandro Dalcin {
1342ffb9264SLisandro Dalcin   PetscErrorCode ierr;
1352ffb9264SLisandro Dalcin 
1362ffb9264SLisandro Dalcin   PetscFunctionBegin;
137b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
138b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
1392ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
1402ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
1412ffb9264SLisandro Dalcin   }
1422ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
1432ffb9264SLisandro Dalcin }
1442ffb9264SLisandro Dalcin 
145bdad233fSMatthew Knepley /*@
146bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
147bdad233fSMatthew Knepley 
148bdad233fSMatthew Knepley    Collective on TS
149bdad233fSMatthew Knepley 
150bdad233fSMatthew Knepley    Input Parameter:
151bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
152bdad233fSMatthew Knepley 
153bdad233fSMatthew Knepley    Options Database Keys:
154b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
155ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
1563e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
1573e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
1583e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
159a3cdaa26SBarry 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
160a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
161a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
162a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
163a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
164a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
165ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
166847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
167bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
168de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
169de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1706934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
171de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
172de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
173de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
174de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1753e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1763e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
177fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
178e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
179110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
180110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
18153ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
18253ea634cSHong Zhang 
18391b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
184bdad233fSMatthew Knepley 
185bdad233fSMatthew Knepley    Level: beginner
186bdad233fSMatthew Knepley 
187bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
188bdad233fSMatthew Knepley 
189a313700dSBarry Smith .seealso: TSGetType()
190bdad233fSMatthew Knepley @*/
1917087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
192bdad233fSMatthew Knepley {
193bc952696SBarry Smith   PetscBool              opt,flg,tflg;
194dfbe8321SBarry Smith   PetscErrorCode         ierr;
195eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
19631748224SBarry Smith   PetscReal              time_step;
19749354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
198d1212d36SBarry Smith   char                   dir[16];
199cd11d68dSLisandro Dalcin   TSIFunction            ifun;
2006991f827SBarry Smith   const char             *defaultType;
2016991f827SBarry Smith   char                   typeName[256];
202bdad233fSMatthew Knepley 
203bdad233fSMatthew Knepley   PetscFunctionBegin;
2040700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2056991f827SBarry Smith 
2066991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
207cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
208cd11d68dSLisandro Dalcin 
209cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
210cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
211cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
212cd11d68dSLisandro Dalcin   else
213cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
2146991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
2156991f827SBarry Smith   if (opt) {
2166991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
2176991f827SBarry Smith   } else {
2186991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
2196991f827SBarry Smith   }
220bdad233fSMatthew Knepley 
221bdad233fSMatthew Knepley   /* Handle generic TS options */
2220298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
2230298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
2240298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
22531748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
226cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
22749354f04SShri 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);
22849354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
2290298fd71SBarry 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);
2300298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
2310298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2320298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2330298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
234bdad233fSMatthew Knepley 
23556f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
23656f85f32SBarry Smith   {
23756f85f32SBarry Smith   PetscBool set;
23856f85f32SBarry Smith   flg  = PETSC_FALSE;
23956f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
24056f85f32SBarry Smith   if (set) {
24156f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
24256f85f32SBarry Smith   }
24356f85f32SBarry Smith   }
24456f85f32SBarry Smith #endif
24556f85f32SBarry Smith 
246bdad233fSMatthew Knepley   /* Monitor options */
247cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
248fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
249fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
250*287c2655SBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor_gradient","Monitor gradient in the adjoint computation","TSAdjointMonitorGradient",TSAdjointMonitorGradient,NULL);CHKERRQ(ierr);
251fde5950dSBarry Smith 
2525180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2535180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2545180491cSLisandro Dalcin 
2554f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
256b3603a34SBarry Smith   if (opt) {
2570b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2583923b477SBarry Smith     PetscInt       howoften = 1;
259b3603a34SBarry Smith 
2600298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2616ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2624f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
263bdad233fSMatthew Knepley   }
2646ba87a44SLisandro Dalcin 
2654f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
266ef20d060SBarry Smith   if (opt) {
2670b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2683923b477SBarry Smith     PetscInt       howoften = 1;
269ef20d060SBarry Smith 
2700298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2716ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2724f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
273ef20d060SBarry Smith   }
2746934998bSLisandro Dalcin 
2756934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2766934998bSLisandro Dalcin   if (opt) {
2776934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2786934998bSLisandro Dalcin     PetscInt       howoften = 1;
2796934998bSLisandro Dalcin 
2806934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2816ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2826934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2836934998bSLisandro Dalcin   }
284201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
285201da799SBarry Smith   if (opt) {
286201da799SBarry Smith     TSMonitorLGCtx ctx;
287201da799SBarry Smith     PetscInt       howoften = 1;
288201da799SBarry Smith 
2890298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2906ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
291201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
292201da799SBarry Smith   }
293201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
294201da799SBarry Smith   if (opt) {
295201da799SBarry Smith     TSMonitorLGCtx ctx;
296201da799SBarry Smith     PetscInt       howoften = 1;
297201da799SBarry Smith 
2980298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2996ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
300201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
301201da799SBarry Smith   }
3028189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
3038189c53fSBarry Smith   if (opt) {
3048189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
3058189c53fSBarry Smith     PetscInt          howoften = 1;
3068189c53fSBarry Smith 
3070298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
3086934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3098189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
3108189c53fSBarry Smith   }
311ef20d060SBarry Smith   opt  = PETSC_FALSE;
3120dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
313a7cc72afSBarry Smith   if (opt) {
31483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
31583a4ac43SBarry Smith     PetscInt         howoften = 1;
316a80ad3e0SBarry Smith 
3170298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
3186934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
31983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
320bdad233fSMatthew Knepley   }
321fb1732b5SBarry Smith   opt  = PETSC_FALSE;
3229110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
3239110b2e7SHong Zhang   if (opt) {
3249110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
3259110b2e7SHong Zhang     PetscInt         howoften = 1;
3269110b2e7SHong Zhang 
3279110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
3286934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3299110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3309110b2e7SHong Zhang   }
3319110b2e7SHong Zhang   opt  = PETSC_FALSE;
3322d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
3332d5ee99bSBarry Smith   if (opt) {
3342d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3352d5ee99bSBarry Smith     PetscReal        bounds[4];
3362d5ee99bSBarry Smith     PetscInt         n = 4;
3372d5ee99bSBarry Smith     PetscDraw        draw;
3386934998bSLisandro Dalcin     PetscDrawAxis    axis;
3392d5ee99bSBarry Smith 
3402d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3412d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3426934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3432d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3446934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3456934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3466934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3472d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3482d5ee99bSBarry Smith   }
3492d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3500dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3513a471f94SBarry Smith   if (opt) {
35283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
35383a4ac43SBarry Smith     PetscInt         howoften = 1;
3543a471f94SBarry Smith 
3550298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3566934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
35783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3583a471f94SBarry Smith   }
359fde5950dSBarry Smith 
360ed81e22dSJed Brown   opt  = PETSC_FALSE;
36191b97e58SBarry 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);
362ed81e22dSJed Brown   if (flg) {
363ed81e22dSJed Brown     const char *ptr,*ptr2;
364ed81e22dSJed Brown     char       *filetemplate;
365ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
366ed81e22dSJed Brown     /* Do some cursory validation of the input. */
367ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
368ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
369ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
370ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
371ce94432eSBarry 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");
372ed81e22dSJed Brown       if (ptr2) break;
373ed81e22dSJed Brown     }
374ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
375ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
376ed81e22dSJed Brown   }
377bdad233fSMatthew Knepley 
378d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
379d1212d36SBarry Smith   if (flg) {
380d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
381d1212d36SBarry Smith     int                  ray = 0;
382d1212d36SBarry Smith     DMDADirection        ddir;
383d1212d36SBarry Smith     DM                   da;
384d1212d36SBarry Smith     PetscMPIInt          rank;
385d1212d36SBarry Smith 
386ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
387d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
388d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
389ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
390d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
391d1212d36SBarry Smith 
392d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
393b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
394d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
395d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
396ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
397d1212d36SBarry Smith     if (!rank) {
398d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
399d1212d36SBarry Smith     }
40051b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
401d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
402d1212d36SBarry Smith   }
40351b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
40451b4a12fSMatthew G. Knepley   if (flg) {
40551b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
40651b4a12fSMatthew G. Knepley     int                 ray = 0;
40751b4a12fSMatthew G. Knepley     DMDADirection       ddir;
40851b4a12fSMatthew G. Knepley     DM                  da;
40951b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
410d1212d36SBarry Smith 
41151b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
41251b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
41351b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
41451b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
41551b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
4161c3436cfSJed Brown 
41751b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
418b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
41951b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
42051b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
42151b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
42251b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
42351b4a12fSMatthew G. Knepley   }
424a7a1495cSBarry Smith 
425b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
426b3d3934dSBarry Smith   if (opt) {
427b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
428b3d3934dSBarry Smith 
429b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
430b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
431b3d3934dSBarry Smith   }
432b3d3934dSBarry Smith 
433847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
434847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
435847ff0e1SMatthew G. Knepley   if (flg) {
436847ff0e1SMatthew G. Knepley     DM   dm;
437847ff0e1SMatthew G. Knepley     DMTS tdm;
438847ff0e1SMatthew G. Knepley 
439847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
440847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
441847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
442847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
443847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
444847ff0e1SMatthew G. Knepley   }
445847ff0e1SMatthew G. Knepley 
4462ffb9264SLisandro Dalcin   /* Handle TSAdapt options */
4472ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
4482ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
449ee346746SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
450d763cef2SBarry Smith 
451d763cef2SBarry Smith   /* Handle specific TS options */
452d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4536991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
454d763cef2SBarry Smith   }
455fbc52257SHong Zhang 
45668bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4574f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
45868bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
45968bece0bSHong Zhang   if (tflg) {
46068bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
46168bece0bSHong Zhang   }
4624f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
46368bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
46468bece0bSHong Zhang   if (flg) {
46568bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
46668bece0bSHong Zhang     ts->adjoint_solve = tflg;
46768bece0bSHong Zhang   }
468d763cef2SBarry Smith 
469d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4700633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
471fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
472d763cef2SBarry Smith 
4734f122a70SLisandro Dalcin   if (ts->trajectory) {
4744f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
475d763cef2SBarry Smith   }
47668bece0bSHong Zhang 
4774f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4784f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4794f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
480d763cef2SBarry Smith   PetscFunctionReturn(0);
481d763cef2SBarry Smith }
482d763cef2SBarry Smith 
483d2daff3dSHong Zhang /*@
48478fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
48578fbdcc8SBarry Smith 
48678fbdcc8SBarry Smith    Collective on TS
48778fbdcc8SBarry Smith 
48878fbdcc8SBarry Smith    Input Parameters:
48978fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
49078fbdcc8SBarry Smith 
49178fbdcc8SBarry Smith    Output Parameters;
49278fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
49378fbdcc8SBarry Smith 
49478fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
49578fbdcc8SBarry Smith 
49678fbdcc8SBarry Smith    Level: advanced
49778fbdcc8SBarry Smith 
49878fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
49978fbdcc8SBarry Smith 
50078fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
50178fbdcc8SBarry Smith @*/
50278fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
50378fbdcc8SBarry Smith {
50478fbdcc8SBarry Smith   PetscFunctionBegin;
50578fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50678fbdcc8SBarry Smith   *tr = ts->trajectory;
50778fbdcc8SBarry Smith   PetscFunctionReturn(0);
50878fbdcc8SBarry Smith }
50978fbdcc8SBarry Smith 
51078fbdcc8SBarry Smith /*@
511bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
512d2daff3dSHong Zhang 
513d2daff3dSHong Zhang    Collective on TS
514d2daff3dSHong Zhang 
515d2daff3dSHong Zhang    Input Parameters:
516bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
517bc952696SBarry Smith 
51878fbdcc8SBarry Smith    Options Database:
51978fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
52078fbdcc8SBarry Smith -  -ts_trajectory_type type
52178fbdcc8SBarry Smith 
52268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
523d2daff3dSHong Zhang 
52478fbdcc8SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/bin/PetscBinaryIOTrajectory.py and
52578fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
52678fbdcc8SBarry Smith 
527d2daff3dSHong Zhang    Level: intermediate
528d2daff3dSHong Zhang 
52978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()
530d2daff3dSHong Zhang 
531bc952696SBarry Smith .keywords: TS, set, checkpoint,
532d2daff3dSHong Zhang @*/
533bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
534d2daff3dSHong Zhang {
535bc952696SBarry Smith   PetscErrorCode ierr;
536bc952696SBarry Smith 
537d2daff3dSHong Zhang   PetscFunctionBegin;
538d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
539bc952696SBarry Smith   if (!ts->trajectory) {
540bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
541972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
542bc952696SBarry Smith   }
543d2daff3dSHong Zhang   PetscFunctionReturn(0);
544d2daff3dSHong Zhang }
545d2daff3dSHong Zhang 
546a7a1495cSBarry Smith /*@
547a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
548a7a1495cSBarry Smith       set with TSSetRHSJacobian().
549a7a1495cSBarry Smith 
550a7a1495cSBarry Smith    Collective on TS and Vec
551a7a1495cSBarry Smith 
552a7a1495cSBarry Smith    Input Parameters:
553316643e7SJed Brown +  ts - the TS context
554a7a1495cSBarry Smith .  t - current timestep
5550910c330SBarry Smith -  U - input vector
556a7a1495cSBarry Smith 
557a7a1495cSBarry Smith    Output Parameters:
558a7a1495cSBarry Smith +  A - Jacobian matrix
559a7a1495cSBarry Smith .  B - optional preconditioning matrix
560a7a1495cSBarry Smith -  flag - flag indicating matrix structure
561a7a1495cSBarry Smith 
562a7a1495cSBarry Smith    Notes:
563a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
564a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
565a7a1495cSBarry Smith 
56694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
567a7a1495cSBarry Smith    flag parameter.
568a7a1495cSBarry Smith 
569a7a1495cSBarry Smith    Level: developer
570a7a1495cSBarry Smith 
571a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
572a7a1495cSBarry Smith 
57394b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
574a7a1495cSBarry Smith @*/
575d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
576a7a1495cSBarry Smith {
577dfbe8321SBarry Smith   PetscErrorCode ierr;
578270bf2e7SJed Brown   PetscObjectState Ustate;
57924989b8cSPeter Brune   DM             dm;
580942e3340SBarry Smith   DMTS           tsdm;
58124989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
58224989b8cSPeter Brune   void           *ctx;
58324989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
584b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
585a7a1495cSBarry Smith 
586a7a1495cSBarry Smith   PetscFunctionBegin;
5870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5880910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5890910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
59024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
591942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
59224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5930298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
594b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
59559e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
596b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5970e4ef248SJed Brown     PetscFunctionReturn(0);
598f8ede8e7SJed Brown   }
599d90be118SSean Farley 
600ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
601d90be118SSean Farley 
602e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
60394ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
60494ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
60594ab13aaSBarry Smith     if (A != B) {
60694ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
60794ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
608e1244c69SJed Brown     }
609e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
610e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
611e1244c69SJed Brown   }
612e1244c69SJed Brown 
61324989b8cSPeter Brune   if (rhsjacobianfunc) {
6146cd88445SBarry Smith     PetscBool missing;
61594ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
616a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
617d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
618a7a1495cSBarry Smith     PetscStackPop;
61994ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6206cd88445SBarry Smith     if (A) {
6216cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6226cd88445SBarry 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");
6236cd88445SBarry Smith     }
6246cd88445SBarry Smith     if (B && B != A) {
6256cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6266cd88445SBarry 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");
6276cd88445SBarry Smith     }
628ef66eb69SBarry Smith   } else {
62994ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
63094ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
631ef66eb69SBarry Smith   }
6320e4ef248SJed Brown   ts->rhsjacobian.time       = t;
6330910c330SBarry Smith   ts->rhsjacobian.X          = U;
63459e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
635a7a1495cSBarry Smith   PetscFunctionReturn(0);
636a7a1495cSBarry Smith }
637a7a1495cSBarry Smith 
638316643e7SJed Brown /*@
639d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
640d763cef2SBarry Smith 
641316643e7SJed Brown    Collective on TS and Vec
642316643e7SJed Brown 
643316643e7SJed Brown    Input Parameters:
644316643e7SJed Brown +  ts - the TS context
645316643e7SJed Brown .  t - current time
6460910c330SBarry Smith -  U - state vector
647316643e7SJed Brown 
648316643e7SJed Brown    Output Parameter:
649316643e7SJed Brown .  y - right hand side
650316643e7SJed Brown 
651316643e7SJed Brown    Note:
652316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
653316643e7SJed Brown    is used internally within the nonlinear solvers.
654316643e7SJed Brown 
655316643e7SJed Brown    Level: developer
656316643e7SJed Brown 
657316643e7SJed Brown .keywords: TS, compute
658316643e7SJed Brown 
659316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
660316643e7SJed Brown @*/
6610910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
662d763cef2SBarry Smith {
663dfbe8321SBarry Smith   PetscErrorCode ierr;
66424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
66524989b8cSPeter Brune   TSIFunction    ifunction;
66624989b8cSPeter Brune   void           *ctx;
66724989b8cSPeter Brune   DM             dm;
66824989b8cSPeter Brune 
669d763cef2SBarry Smith   PetscFunctionBegin;
6700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6710910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6720700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
67324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6750298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
676d763cef2SBarry Smith 
677ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
678d763cef2SBarry Smith 
6790910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
68024989b8cSPeter Brune   if (rhsfunction) {
681d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6820910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
683d763cef2SBarry Smith     PetscStackPop;
684214bc6a2SJed Brown   } else {
685214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
686b2cd27e8SJed Brown   }
68744a41b28SSean Farley 
6880910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
689d763cef2SBarry Smith   PetscFunctionReturn(0);
690d763cef2SBarry Smith }
691d763cef2SBarry Smith 
692ef20d060SBarry Smith /*@
693ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
694ef20d060SBarry Smith 
695ef20d060SBarry Smith    Collective on TS and Vec
696ef20d060SBarry Smith 
697ef20d060SBarry Smith    Input Parameters:
698ef20d060SBarry Smith +  ts - the TS context
699ef20d060SBarry Smith -  t - current time
700ef20d060SBarry Smith 
701ef20d060SBarry Smith    Output Parameter:
7020910c330SBarry Smith .  U - the solution
703ef20d060SBarry Smith 
704ef20d060SBarry Smith    Note:
705ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
706ef20d060SBarry Smith    is used internally within the nonlinear solvers.
707ef20d060SBarry Smith 
708ef20d060SBarry Smith    Level: developer
709ef20d060SBarry Smith 
710ef20d060SBarry Smith .keywords: TS, compute
711ef20d060SBarry Smith 
712abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
713ef20d060SBarry Smith @*/
7140910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
715ef20d060SBarry Smith {
716ef20d060SBarry Smith   PetscErrorCode     ierr;
717ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
718ef20d060SBarry Smith   void               *ctx;
719ef20d060SBarry Smith   DM                 dm;
720ef20d060SBarry Smith 
721ef20d060SBarry Smith   PetscFunctionBegin;
722ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7230910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
724ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
725ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
726ef20d060SBarry Smith 
727ef20d060SBarry Smith   if (solutionfunction) {
7289b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7290910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
730ef20d060SBarry Smith     PetscStackPop;
731ef20d060SBarry Smith   }
732ef20d060SBarry Smith   PetscFunctionReturn(0);
733ef20d060SBarry Smith }
7349b7cd975SBarry Smith /*@
7359b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7369b7cd975SBarry Smith 
7379b7cd975SBarry Smith    Collective on TS and Vec
7389b7cd975SBarry Smith 
7399b7cd975SBarry Smith    Input Parameters:
7409b7cd975SBarry Smith +  ts - the TS context
7419b7cd975SBarry Smith -  t - current time
7429b7cd975SBarry Smith 
7439b7cd975SBarry Smith    Output Parameter:
7449b7cd975SBarry Smith .  U - the function value
7459b7cd975SBarry Smith 
7469b7cd975SBarry Smith    Note:
7479b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7489b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7499b7cd975SBarry Smith 
7509b7cd975SBarry Smith    Level: developer
7519b7cd975SBarry Smith 
7529b7cd975SBarry Smith .keywords: TS, compute
7539b7cd975SBarry Smith 
7549b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7559b7cd975SBarry Smith @*/
7569b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7579b7cd975SBarry Smith {
7589b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7599b7cd975SBarry Smith   void               *ctx;
7609b7cd975SBarry Smith   DM                 dm;
7619b7cd975SBarry Smith 
7629b7cd975SBarry Smith   PetscFunctionBegin;
7639b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7649b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7659b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7669b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7679b7cd975SBarry Smith 
7689b7cd975SBarry Smith   if (forcing) {
7699b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7709b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7719b7cd975SBarry Smith     PetscStackPop;
7729b7cd975SBarry Smith   }
7739b7cd975SBarry Smith   PetscFunctionReturn(0);
7749b7cd975SBarry Smith }
775ef20d060SBarry Smith 
776214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
777214bc6a2SJed Brown {
7782dd45cf8SJed Brown   Vec            F;
779214bc6a2SJed Brown   PetscErrorCode ierr;
780214bc6a2SJed Brown 
781214bc6a2SJed Brown   PetscFunctionBegin;
7820298fd71SBarry Smith   *Frhs = NULL;
7830298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
784214bc6a2SJed Brown   if (!ts->Frhs) {
7852dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
786214bc6a2SJed Brown   }
787214bc6a2SJed Brown   *Frhs = ts->Frhs;
788214bc6a2SJed Brown   PetscFunctionReturn(0);
789214bc6a2SJed Brown }
790214bc6a2SJed Brown 
791214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
792214bc6a2SJed Brown {
793214bc6a2SJed Brown   Mat            A,B;
7942dd45cf8SJed Brown   PetscErrorCode ierr;
795214bc6a2SJed Brown 
796214bc6a2SJed Brown   PetscFunctionBegin;
797c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
798c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7990298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
800214bc6a2SJed Brown   if (Arhs) {
801214bc6a2SJed Brown     if (!ts->Arhs) {
802214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
803214bc6a2SJed Brown     }
804214bc6a2SJed Brown     *Arhs = ts->Arhs;
805214bc6a2SJed Brown   }
806214bc6a2SJed Brown   if (Brhs) {
807214bc6a2SJed Brown     if (!ts->Brhs) {
808bdb70873SJed Brown       if (A != B) {
809214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
810bdb70873SJed Brown       } else {
811bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
81251699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
813bdb70873SJed Brown       }
814214bc6a2SJed Brown     }
815214bc6a2SJed Brown     *Brhs = ts->Brhs;
816214bc6a2SJed Brown   }
817214bc6a2SJed Brown   PetscFunctionReturn(0);
818214bc6a2SJed Brown }
819214bc6a2SJed Brown 
820316643e7SJed Brown /*@
8210910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
822316643e7SJed Brown 
823316643e7SJed Brown    Collective on TS and Vec
824316643e7SJed Brown 
825316643e7SJed Brown    Input Parameters:
826316643e7SJed Brown +  ts - the TS context
827316643e7SJed Brown .  t - current time
8280910c330SBarry Smith .  U - state vector
8290910c330SBarry Smith .  Udot - time derivative of state vector
830214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
831316643e7SJed Brown 
832316643e7SJed Brown    Output Parameter:
833316643e7SJed Brown .  Y - right hand side
834316643e7SJed Brown 
835316643e7SJed Brown    Note:
836316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
837316643e7SJed Brown    is used internally within the nonlinear solvers.
838316643e7SJed Brown 
839316643e7SJed Brown    If the user did did not write their equations in implicit form, this
840316643e7SJed Brown    function recasts them in implicit form.
841316643e7SJed Brown 
842316643e7SJed Brown    Level: developer
843316643e7SJed Brown 
844316643e7SJed Brown .keywords: TS, compute
845316643e7SJed Brown 
846316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
847316643e7SJed Brown @*/
8480910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
849316643e7SJed Brown {
850316643e7SJed Brown   PetscErrorCode ierr;
85124989b8cSPeter Brune   TSIFunction    ifunction;
85224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
85324989b8cSPeter Brune   void           *ctx;
85424989b8cSPeter Brune   DM             dm;
855316643e7SJed Brown 
856316643e7SJed Brown   PetscFunctionBegin;
8570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8580910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8590910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8600700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
861316643e7SJed Brown 
86224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
86324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8640298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
86524989b8cSPeter Brune 
866ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
867d90be118SSean Farley 
8680910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
86924989b8cSPeter Brune   if (ifunction) {
870316643e7SJed Brown     PetscStackPush("TS user implicit function");
8710910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
872316643e7SJed Brown     PetscStackPop;
873214bc6a2SJed Brown   }
874214bc6a2SJed Brown   if (imex) {
87524989b8cSPeter Brune     if (!ifunction) {
8760910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8772dd45cf8SJed Brown     }
87824989b8cSPeter Brune   } else if (rhsfunction) {
87924989b8cSPeter Brune     if (ifunction) {
880214bc6a2SJed Brown       Vec Frhs;
881214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8820910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
883214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8842dd45cf8SJed Brown     } else {
8850910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8860910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
887316643e7SJed Brown     }
8884a6899ffSJed Brown   }
8890910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
890316643e7SJed Brown   PetscFunctionReturn(0);
891316643e7SJed Brown }
892316643e7SJed Brown 
893316643e7SJed Brown /*@
894316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
895316643e7SJed Brown 
896316643e7SJed Brown    Collective on TS and Vec
897316643e7SJed Brown 
898316643e7SJed Brown    Input
899316643e7SJed Brown       Input Parameters:
900316643e7SJed Brown +  ts - the TS context
901316643e7SJed Brown .  t - current timestep
9020910c330SBarry Smith .  U - state vector
9030910c330SBarry Smith .  Udot - time derivative of state vector
904214bc6a2SJed Brown .  shift - shift to apply, see note below
905214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
906316643e7SJed Brown 
907316643e7SJed Brown    Output Parameters:
908316643e7SJed Brown +  A - Jacobian matrix
909316643e7SJed Brown .  B - optional preconditioning matrix
910316643e7SJed Brown -  flag - flag indicating matrix structure
911316643e7SJed Brown 
912316643e7SJed Brown    Notes:
9130910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
914316643e7SJed Brown 
9150910c330SBarry Smith    dF/dU + shift*dF/dUdot
916316643e7SJed Brown 
917316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
918316643e7SJed Brown    is used internally within the nonlinear solvers.
919316643e7SJed Brown 
920316643e7SJed Brown    Level: developer
921316643e7SJed Brown 
922316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
923316643e7SJed Brown 
924316643e7SJed Brown .seealso:  TSSetIJacobian()
92563495f91SJed Brown @*/
926d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
927316643e7SJed Brown {
928316643e7SJed Brown   PetscErrorCode ierr;
92924989b8cSPeter Brune   TSIJacobian    ijacobian;
93024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
93124989b8cSPeter Brune   DM             dm;
93224989b8cSPeter Brune   void           *ctx;
933316643e7SJed Brown 
934316643e7SJed Brown   PetscFunctionBegin;
9350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9360910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9370910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
938316643e7SJed Brown   PetscValidPointer(A,6);
93994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
940316643e7SJed Brown   PetscValidPointer(B,7);
94194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
94224989b8cSPeter Brune 
94324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
94424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9450298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
94624989b8cSPeter Brune 
947ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
948316643e7SJed Brown 
94994ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
95024989b8cSPeter Brune   if (ijacobian) {
9516cd88445SBarry Smith     PetscBool missing;
952316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
953d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
954316643e7SJed Brown     PetscStackPop;
9556cd88445SBarry Smith     if (A) {
9566cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9576cd88445SBarry 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");
9586cd88445SBarry Smith     }
9596cd88445SBarry Smith     if (B && B != A) {
9606cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9616cd88445SBarry 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");
9626cd88445SBarry Smith     }
9634a6899ffSJed Brown   }
964214bc6a2SJed Brown   if (imex) {
965b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9664c26be97Sstefano_zampini       PetscBool assembled;
96794ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9684c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9694c26be97Sstefano_zampini       if (!assembled) {
9704c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9714c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9724c26be97Sstefano_zampini       }
97394ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
97494ab13aaSBarry Smith       if (A != B) {
97594ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9764c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9774c26be97Sstefano_zampini         if (!assembled) {
9784c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9794c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9804c26be97Sstefano_zampini         }
98194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
982214bc6a2SJed Brown       }
983214bc6a2SJed Brown     }
984214bc6a2SJed Brown   } else {
985e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
986e1244c69SJed Brown     if (rhsjacobian) {
987214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
988d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
989e1244c69SJed Brown     }
99094ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
991e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
992e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
99394ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
99494ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99594ab13aaSBarry Smith       if (A != B) {
99694ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
99794ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
998316643e7SJed Brown       }
999e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1000e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1001e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
100294ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
100394ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
100494ab13aaSBarry Smith         if (A != B) {
100594ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
100694ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1007214bc6a2SJed Brown         }
1008316643e7SJed Brown       }
100994ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
101094ab13aaSBarry Smith       if (A != B) {
101194ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1012316643e7SJed Brown       }
1013316643e7SJed Brown     }
1014316643e7SJed Brown   }
101594ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1016316643e7SJed Brown   PetscFunctionReturn(0);
1017316643e7SJed Brown }
1018316643e7SJed Brown 
1019d763cef2SBarry Smith /*@C
1020d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1021b5abc632SBarry Smith     where U_t = G(t,u).
1022d763cef2SBarry Smith 
10233f9fe445SBarry Smith     Logically Collective on TS
1024d763cef2SBarry Smith 
1025d763cef2SBarry Smith     Input Parameters:
1026d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10270298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1028d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1029d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10300298fd71SBarry Smith           function evaluation routine (may be NULL)
1031d763cef2SBarry Smith 
1032d763cef2SBarry Smith     Calling sequence of func:
103387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1034d763cef2SBarry Smith 
1035d763cef2SBarry Smith +   t - current timestep
1036d763cef2SBarry Smith .   u - input vector
1037d763cef2SBarry Smith .   F - function vector
1038d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith     Level: beginner
1041d763cef2SBarry Smith 
10422bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10432bbac0d3SBarry Smith 
1044d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1045d763cef2SBarry Smith 
1046ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1047d763cef2SBarry Smith @*/
1048089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1049d763cef2SBarry Smith {
1050089b2837SJed Brown   PetscErrorCode ierr;
1051089b2837SJed Brown   SNES           snes;
10520298fd71SBarry Smith   Vec            ralloc = NULL;
105324989b8cSPeter Brune   DM             dm;
1054d763cef2SBarry Smith 
1055089b2837SJed Brown   PetscFunctionBegin;
10560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1057ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
105824989b8cSPeter Brune 
105924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
106024989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1061089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1062e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1063e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1064e856ceecSJed Brown     r = ralloc;
1065e856ceecSJed Brown   }
1066089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1067e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1068d763cef2SBarry Smith   PetscFunctionReturn(0);
1069d763cef2SBarry Smith }
1070d763cef2SBarry Smith 
1071ef20d060SBarry Smith /*@C
1072abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1073ef20d060SBarry Smith 
1074ef20d060SBarry Smith     Logically Collective on TS
1075ef20d060SBarry Smith 
1076ef20d060SBarry Smith     Input Parameters:
1077ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1078ef20d060SBarry Smith .   f - routine for evaluating the solution
1079ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10800298fd71SBarry Smith           function evaluation routine (may be NULL)
1081ef20d060SBarry Smith 
1082ef20d060SBarry Smith     Calling sequence of func:
1083ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1084ef20d060SBarry Smith 
1085ef20d060SBarry Smith +   t - current timestep
1086ef20d060SBarry Smith .   u - output vector
1087ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1088ef20d060SBarry Smith 
1089abd5a294SJed Brown     Notes:
1090abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1091abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1092abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1093abd5a294SJed Brown 
10944f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1095abd5a294SJed Brown 
1096ef20d060SBarry Smith     Level: beginner
1097ef20d060SBarry Smith 
1098ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1099ef20d060SBarry Smith 
11009b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1101ef20d060SBarry Smith @*/
1102ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1103ef20d060SBarry Smith {
1104ef20d060SBarry Smith   PetscErrorCode ierr;
1105ef20d060SBarry Smith   DM             dm;
1106ef20d060SBarry Smith 
1107ef20d060SBarry Smith   PetscFunctionBegin;
1108ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1109ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1110ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1111ef20d060SBarry Smith   PetscFunctionReturn(0);
1112ef20d060SBarry Smith }
1113ef20d060SBarry Smith 
11149b7cd975SBarry Smith /*@C
11159b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11169b7cd975SBarry Smith 
11179b7cd975SBarry Smith     Logically Collective on TS
11189b7cd975SBarry Smith 
11199b7cd975SBarry Smith     Input Parameters:
11209b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1121e162b725SBarry Smith .   func - routine for evaluating the forcing function
11229b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11230298fd71SBarry Smith           function evaluation routine (may be NULL)
11249b7cd975SBarry Smith 
11259b7cd975SBarry Smith     Calling sequence of func:
1126e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11279b7cd975SBarry Smith 
11289b7cd975SBarry Smith +   t - current timestep
1129e162b725SBarry Smith .   f - output vector
11309b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11319b7cd975SBarry Smith 
11329b7cd975SBarry Smith     Notes:
11339b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1134e162b725SBarry 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
1135e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1136e162b725SBarry Smith 
1137e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1138e162b725SBarry Smith 
1139e162b725SBarry 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
1140e162b725SBarry Smith     parameters can be passed in the ctx variable.
11419b7cd975SBarry Smith 
11429b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11439b7cd975SBarry Smith 
11449b7cd975SBarry Smith     Level: beginner
11459b7cd975SBarry Smith 
11469b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11479b7cd975SBarry Smith 
11489b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11499b7cd975SBarry Smith @*/
1150e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11519b7cd975SBarry Smith {
11529b7cd975SBarry Smith   PetscErrorCode ierr;
11539b7cd975SBarry Smith   DM             dm;
11549b7cd975SBarry Smith 
11559b7cd975SBarry Smith   PetscFunctionBegin;
11569b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11579b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1158e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11599b7cd975SBarry Smith   PetscFunctionReturn(0);
11609b7cd975SBarry Smith }
11619b7cd975SBarry Smith 
1162d763cef2SBarry Smith /*@C
1163f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1164b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1165d763cef2SBarry Smith 
11663f9fe445SBarry Smith    Logically Collective on TS
1167d763cef2SBarry Smith 
1168d763cef2SBarry Smith    Input Parameters:
1169d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1170e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1171e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1172d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1173d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11740298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1175d763cef2SBarry Smith 
1176f7ab8db6SBarry Smith    Calling sequence of f:
1177ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1178d763cef2SBarry Smith 
1179d763cef2SBarry Smith +  t - current timestep
1180d763cef2SBarry Smith .  u - input vector
1181e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1182e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1183d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1184d763cef2SBarry Smith 
11856cd88445SBarry Smith    Notes:
11866cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11876cd88445SBarry Smith 
11886cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1189ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1190d763cef2SBarry Smith 
1191d763cef2SBarry Smith    Level: beginner
1192d763cef2SBarry Smith 
1193d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1194d763cef2SBarry Smith 
1195ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1196d763cef2SBarry Smith 
1197d763cef2SBarry Smith @*/
1198e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1199d763cef2SBarry Smith {
1200277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1201089b2837SJed Brown   SNES           snes;
120224989b8cSPeter Brune   DM             dm;
120324989b8cSPeter Brune   TSIJacobian    ijacobian;
1204277b19d0SLisandro Dalcin 
1205d763cef2SBarry Smith   PetscFunctionBegin;
12060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1207e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1208e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1209e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1210e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1211d763cef2SBarry Smith 
121224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
121324989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1214e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1215e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1216e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1217e1244c69SJed Brown   }
12180298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1219089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12205f659677SPeter Brune   if (!ijacobian) {
1221e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12220e4ef248SJed Brown   }
1223e5d3d808SBarry Smith   if (Amat) {
1224e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12250e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1226e5d3d808SBarry Smith     ts->Arhs = Amat;
12270e4ef248SJed Brown   }
1228e5d3d808SBarry Smith   if (Pmat) {
1229e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12300e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1231e5d3d808SBarry Smith     ts->Brhs = Pmat;
12320e4ef248SJed Brown   }
1233d763cef2SBarry Smith   PetscFunctionReturn(0);
1234d763cef2SBarry Smith }
1235d763cef2SBarry Smith 
1236316643e7SJed Brown 
1237316643e7SJed Brown /*@C
1238b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1239316643e7SJed Brown 
12403f9fe445SBarry Smith    Logically Collective on TS
1241316643e7SJed Brown 
1242316643e7SJed Brown    Input Parameters:
1243316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12440298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1245316643e7SJed Brown .  f   - the function evaluation routine
12460298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1247316643e7SJed Brown 
1248316643e7SJed Brown    Calling sequence of f:
1249316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1250316643e7SJed Brown 
1251316643e7SJed Brown +  t   - time at step/stage being solved
1252316643e7SJed Brown .  u   - state vector
1253316643e7SJed Brown .  u_t - time derivative of state vector
1254316643e7SJed Brown .  F   - function vector
1255316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1256316643e7SJed Brown 
1257316643e7SJed Brown    Important:
12582bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1259316643e7SJed Brown 
1260316643e7SJed Brown    Level: beginner
1261316643e7SJed Brown 
1262316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1263316643e7SJed Brown 
1264d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1265316643e7SJed Brown @*/
126651699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1267316643e7SJed Brown {
1268089b2837SJed Brown   PetscErrorCode ierr;
1269089b2837SJed Brown   SNES           snes;
127051699248SLisandro Dalcin   Vec            ralloc = NULL;
127124989b8cSPeter Brune   DM             dm;
1272316643e7SJed Brown 
1273316643e7SJed Brown   PetscFunctionBegin;
12740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
127551699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
127624989b8cSPeter Brune 
127724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
127824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
127924989b8cSPeter Brune 
1280089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
128151699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
128251699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
128351699248SLisandro Dalcin     r  = ralloc;
1284e856ceecSJed Brown   }
128551699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
128651699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1287089b2837SJed Brown   PetscFunctionReturn(0);
1288089b2837SJed Brown }
1289089b2837SJed Brown 
1290089b2837SJed Brown /*@C
1291089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1292089b2837SJed Brown 
1293089b2837SJed Brown    Not Collective
1294089b2837SJed Brown 
1295089b2837SJed Brown    Input Parameter:
1296089b2837SJed Brown .  ts - the TS context
1297089b2837SJed Brown 
1298089b2837SJed Brown    Output Parameter:
12990298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13000298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13010298fd71SBarry Smith -  ctx - the function context (or NULL)
1302089b2837SJed Brown 
1303089b2837SJed Brown    Level: advanced
1304089b2837SJed Brown 
1305089b2837SJed Brown .keywords: TS, nonlinear, get, function
1306089b2837SJed Brown 
1307089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1308089b2837SJed Brown @*/
1309089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1310089b2837SJed Brown {
1311089b2837SJed Brown   PetscErrorCode ierr;
1312089b2837SJed Brown   SNES           snes;
131324989b8cSPeter Brune   DM             dm;
1314089b2837SJed Brown 
1315089b2837SJed Brown   PetscFunctionBegin;
1316089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1317089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13180298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
131924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
132024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1321089b2837SJed Brown   PetscFunctionReturn(0);
1322089b2837SJed Brown }
1323089b2837SJed Brown 
1324089b2837SJed Brown /*@C
1325089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1326089b2837SJed Brown 
1327089b2837SJed Brown    Not Collective
1328089b2837SJed Brown 
1329089b2837SJed Brown    Input Parameter:
1330089b2837SJed Brown .  ts - the TS context
1331089b2837SJed Brown 
1332089b2837SJed Brown    Output Parameter:
13330298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13340298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13350298fd71SBarry Smith -  ctx - the function context (or NULL)
1336089b2837SJed Brown 
1337089b2837SJed Brown    Level: advanced
1338089b2837SJed Brown 
1339089b2837SJed Brown .keywords: TS, nonlinear, get, function
1340089b2837SJed Brown 
13412bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1342089b2837SJed Brown @*/
1343089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1344089b2837SJed Brown {
1345089b2837SJed Brown   PetscErrorCode ierr;
1346089b2837SJed Brown   SNES           snes;
134724989b8cSPeter Brune   DM             dm;
1348089b2837SJed Brown 
1349089b2837SJed Brown   PetscFunctionBegin;
1350089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1351089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13520298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
135324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
135424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1355316643e7SJed Brown   PetscFunctionReturn(0);
1356316643e7SJed Brown }
1357316643e7SJed Brown 
1358316643e7SJed Brown /*@C
1359a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1360ae8867d6SBarry Smith         provided with TSSetIFunction().
1361316643e7SJed Brown 
13623f9fe445SBarry Smith    Logically Collective on TS
1363316643e7SJed Brown 
1364316643e7SJed Brown    Input Parameters:
1365316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1366e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1367e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1368316643e7SJed Brown .  f   - the Jacobian evaluation routine
13690298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1370316643e7SJed Brown 
1371316643e7SJed Brown    Calling sequence of f:
1372ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1373316643e7SJed Brown 
1374316643e7SJed Brown +  t    - time at step/stage being solved
13751b4a444bSJed Brown .  U    - state vector
13761b4a444bSJed Brown .  U_t  - time derivative of state vector
1377316643e7SJed Brown .  a    - shift
1378e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1379e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1380316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1381316643e7SJed Brown 
1382316643e7SJed Brown    Notes:
1383e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1384316643e7SJed Brown 
1385895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1386895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1387895c21f2SBarry Smith 
1388a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1389b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1390a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1391a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1392a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1393a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1394a4f0a591SBarry Smith 
13956cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13966cd88445SBarry Smith 
13976cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1398ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1399ca5f011dSBarry Smith 
1400316643e7SJed Brown    Level: beginner
1401316643e7SJed Brown 
1402316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1403316643e7SJed Brown 
1404ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1405316643e7SJed Brown 
1406316643e7SJed Brown @*/
1407e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1408316643e7SJed Brown {
1409316643e7SJed Brown   PetscErrorCode ierr;
1410089b2837SJed Brown   SNES           snes;
141124989b8cSPeter Brune   DM             dm;
1412316643e7SJed Brown 
1413316643e7SJed Brown   PetscFunctionBegin;
14140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1415e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1416e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1417e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1418e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
141924989b8cSPeter Brune 
142024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
142124989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
142224989b8cSPeter Brune 
1423089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1424e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1425316643e7SJed Brown   PetscFunctionReturn(0);
1426316643e7SJed Brown }
1427316643e7SJed Brown 
1428e1244c69SJed Brown /*@
1429e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1430e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1431e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1432e1244c69SJed Brown    not been changed by the TS.
1433e1244c69SJed Brown 
1434e1244c69SJed Brown    Logically Collective
1435e1244c69SJed Brown 
1436e1244c69SJed Brown    Input Arguments:
1437e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1438e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1439e1244c69SJed Brown 
1440e1244c69SJed Brown    Level: intermediate
1441e1244c69SJed Brown 
1442e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1443e1244c69SJed Brown @*/
1444e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1445e1244c69SJed Brown {
1446e1244c69SJed Brown   PetscFunctionBegin;
1447e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1448e1244c69SJed Brown   PetscFunctionReturn(0);
1449e1244c69SJed Brown }
1450e1244c69SJed Brown 
1451efe9872eSLisandro Dalcin /*@C
1452efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1453efe9872eSLisandro Dalcin 
1454efe9872eSLisandro Dalcin    Logically Collective on TS
1455efe9872eSLisandro Dalcin 
1456efe9872eSLisandro Dalcin    Input Parameters:
1457efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1458efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1459efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1460efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1461efe9872eSLisandro Dalcin 
1462efe9872eSLisandro Dalcin    Calling sequence of fun:
1463efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1464efe9872eSLisandro Dalcin 
1465efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1466efe9872eSLisandro Dalcin .  U    - state vector
1467efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1468efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1469efe9872eSLisandro Dalcin .  F    - function vector
1470efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1471efe9872eSLisandro Dalcin 
1472efe9872eSLisandro Dalcin    Level: beginner
1473efe9872eSLisandro Dalcin 
1474efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1475efe9872eSLisandro Dalcin 
1476efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1477efe9872eSLisandro Dalcin @*/
1478efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1479efe9872eSLisandro Dalcin {
1480efe9872eSLisandro Dalcin   DM             dm;
1481efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1482efe9872eSLisandro Dalcin 
1483efe9872eSLisandro Dalcin   PetscFunctionBegin;
1484efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1485efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1486efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1487efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1488efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1489efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1490efe9872eSLisandro Dalcin }
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin /*@C
1493efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1494efe9872eSLisandro Dalcin 
1495efe9872eSLisandro Dalcin   Not Collective
1496efe9872eSLisandro Dalcin 
1497efe9872eSLisandro Dalcin   Input Parameter:
1498efe9872eSLisandro Dalcin . ts - the TS context
1499efe9872eSLisandro Dalcin 
1500efe9872eSLisandro Dalcin   Output Parameter:
1501efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1502efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1503efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1504efe9872eSLisandro Dalcin 
1505efe9872eSLisandro Dalcin   Level: advanced
1506efe9872eSLisandro Dalcin 
1507efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1508efe9872eSLisandro Dalcin 
1509efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1510efe9872eSLisandro Dalcin @*/
1511efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1512efe9872eSLisandro Dalcin {
1513efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1514efe9872eSLisandro Dalcin   SNES           snes;
1515efe9872eSLisandro Dalcin   DM             dm;
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin   PetscFunctionBegin;
1518efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1519efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1520efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1521efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1522efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1523efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1524efe9872eSLisandro Dalcin }
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin /*@C
1527bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1528efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin    Logically Collective on TS
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin    Input Parameters:
1533efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1534efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1535efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1536efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1537efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin    Calling sequence of jac:
1540bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1541efe9872eSLisandro Dalcin 
1542efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1543efe9872eSLisandro Dalcin .  U    - state vector
1544efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1545efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1546efe9872eSLisandro Dalcin .  v    - shift for U_t
1547efe9872eSLisandro Dalcin .  a    - shift for U_tt
1548efe9872eSLisandro 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
1549efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1550efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1551efe9872eSLisandro Dalcin 
1552efe9872eSLisandro Dalcin    Notes:
1553efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1554efe9872eSLisandro Dalcin 
1555efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1556efe9872eSLisandro 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.
1557efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1558bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1559efe9872eSLisandro Dalcin 
1560efe9872eSLisandro Dalcin    Level: beginner
1561efe9872eSLisandro Dalcin 
1562efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1563efe9872eSLisandro Dalcin 
1564efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1565efe9872eSLisandro Dalcin @*/
1566efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1567efe9872eSLisandro Dalcin {
1568efe9872eSLisandro Dalcin   DM             dm;
1569efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1570efe9872eSLisandro Dalcin 
1571efe9872eSLisandro Dalcin   PetscFunctionBegin;
1572efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1573efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1574efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1575efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1576efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1577efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1578efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1579efe9872eSLisandro Dalcin }
1580efe9872eSLisandro Dalcin 
1581efe9872eSLisandro Dalcin /*@C
1582efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1583efe9872eSLisandro Dalcin 
1584efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1585efe9872eSLisandro Dalcin 
1586efe9872eSLisandro Dalcin   Input Parameter:
1587efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1588efe9872eSLisandro Dalcin 
1589efe9872eSLisandro Dalcin   Output Parameters:
1590efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1591efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1592efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1593efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1594efe9872eSLisandro Dalcin 
1595efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin   Level: advanced
1598efe9872eSLisandro Dalcin 
1599efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
1600efe9872eSLisandro Dalcin 
1601efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1602efe9872eSLisandro Dalcin @*/
1603efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1604efe9872eSLisandro Dalcin {
1605efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1606efe9872eSLisandro Dalcin   SNES           snes;
1607efe9872eSLisandro Dalcin   DM             dm;
1608efe9872eSLisandro Dalcin 
1609efe9872eSLisandro Dalcin   PetscFunctionBegin;
1610efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1611efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1612efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1613efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1614efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1615efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1616efe9872eSLisandro Dalcin }
1617efe9872eSLisandro Dalcin 
1618efe9872eSLisandro Dalcin /*@
1619efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1620efe9872eSLisandro Dalcin 
1621efe9872eSLisandro Dalcin   Collective on TS and Vec
1622efe9872eSLisandro Dalcin 
1623efe9872eSLisandro Dalcin   Input Parameters:
1624efe9872eSLisandro Dalcin + ts - the TS context
1625efe9872eSLisandro Dalcin . t - current time
1626efe9872eSLisandro Dalcin . U - state vector
1627efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1628efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1629efe9872eSLisandro Dalcin 
1630efe9872eSLisandro Dalcin   Output Parameter:
1631efe9872eSLisandro Dalcin . F - the residual vector
1632efe9872eSLisandro Dalcin 
1633efe9872eSLisandro Dalcin   Note:
1634efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1635efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1636efe9872eSLisandro Dalcin 
1637efe9872eSLisandro Dalcin   Level: developer
1638efe9872eSLisandro Dalcin 
1639efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1640efe9872eSLisandro Dalcin 
1641efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1642efe9872eSLisandro Dalcin @*/
1643efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1644efe9872eSLisandro Dalcin {
1645efe9872eSLisandro Dalcin   DM             dm;
1646efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1647efe9872eSLisandro Dalcin   void           *ctx;
1648efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1649efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1650efe9872eSLisandro Dalcin 
1651efe9872eSLisandro Dalcin   PetscFunctionBegin;
1652efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1653efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1654efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1655efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1656efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1659efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1660efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1661efe9872eSLisandro Dalcin 
1662efe9872eSLisandro Dalcin   if (!I2Function) {
1663efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1664efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1665efe9872eSLisandro Dalcin   }
1666efe9872eSLisandro Dalcin 
1667efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1668efe9872eSLisandro Dalcin 
1669efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1670efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1671efe9872eSLisandro Dalcin   PetscStackPop;
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   if (rhsfunction) {
1674efe9872eSLisandro Dalcin     Vec Frhs;
1675efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1676efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1677efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1678efe9872eSLisandro Dalcin   }
1679efe9872eSLisandro Dalcin 
1680efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1681efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1682efe9872eSLisandro Dalcin }
1683efe9872eSLisandro Dalcin 
1684efe9872eSLisandro Dalcin /*@
1685efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1686efe9872eSLisandro Dalcin 
1687efe9872eSLisandro Dalcin   Collective on TS and Vec
1688efe9872eSLisandro Dalcin 
1689efe9872eSLisandro Dalcin   Input Parameters:
1690efe9872eSLisandro Dalcin + ts - the TS context
1691efe9872eSLisandro Dalcin . t - current timestep
1692efe9872eSLisandro Dalcin . U - state vector
1693efe9872eSLisandro Dalcin . V - time derivative of state vector
1694efe9872eSLisandro Dalcin . A - second time derivative of state vector
1695efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1696efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1697efe9872eSLisandro Dalcin 
1698efe9872eSLisandro Dalcin   Output Parameters:
1699efe9872eSLisandro Dalcin + J - Jacobian matrix
1700efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1701efe9872eSLisandro Dalcin 
1702efe9872eSLisandro Dalcin   Notes:
1703efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1708efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1709efe9872eSLisandro Dalcin 
1710efe9872eSLisandro Dalcin   Level: developer
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1713efe9872eSLisandro Dalcin 
1714efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1715efe9872eSLisandro Dalcin @*/
1716efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1717efe9872eSLisandro Dalcin {
1718efe9872eSLisandro Dalcin   DM             dm;
1719efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1720efe9872eSLisandro Dalcin   void           *ctx;
1721efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1722efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1723efe9872eSLisandro Dalcin 
1724efe9872eSLisandro Dalcin   PetscFunctionBegin;
1725efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1726efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1727efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1728efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1729efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1730efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1731efe9872eSLisandro Dalcin 
1732efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1733efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1734efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1737efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1738efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1739efe9872eSLisandro Dalcin   }
1740efe9872eSLisandro Dalcin 
1741efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1742efe9872eSLisandro Dalcin 
1743efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1744efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1745efe9872eSLisandro Dalcin   PetscStackPop;
1746efe9872eSLisandro Dalcin 
1747efe9872eSLisandro Dalcin   if (rhsjacobian) {
1748efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1749efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1750efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1751efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1752efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1753efe9872eSLisandro Dalcin   }
1754efe9872eSLisandro Dalcin 
1755efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1756efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1757efe9872eSLisandro Dalcin }
1758efe9872eSLisandro Dalcin 
1759efe9872eSLisandro Dalcin /*@
1760efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1761efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1762efe9872eSLisandro Dalcin 
1763efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1764efe9872eSLisandro Dalcin 
1765efe9872eSLisandro Dalcin    Input Parameters:
1766efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1767efe9872eSLisandro Dalcin .  u - the solution vector
1768efe9872eSLisandro Dalcin -  v - the time derivative vector
1769efe9872eSLisandro Dalcin 
1770efe9872eSLisandro Dalcin    Level: beginner
1771efe9872eSLisandro Dalcin 
1772efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1773efe9872eSLisandro Dalcin @*/
1774efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1775efe9872eSLisandro Dalcin {
1776efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1777efe9872eSLisandro Dalcin 
1778efe9872eSLisandro Dalcin   PetscFunctionBegin;
1779efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1780efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1781efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1782efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1783efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1784efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1785efe9872eSLisandro Dalcin   ts->vec_dot = v;
1786efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1787efe9872eSLisandro Dalcin }
1788efe9872eSLisandro Dalcin 
1789efe9872eSLisandro Dalcin /*@
1790efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1791efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1792efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1793efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1794efe9872eSLisandro Dalcin 
1795efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1796efe9872eSLisandro Dalcin 
1797efe9872eSLisandro Dalcin    Input Parameter:
1798efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1799efe9872eSLisandro Dalcin 
1800efe9872eSLisandro Dalcin    Output Parameter:
1801efe9872eSLisandro Dalcin +  u - the vector containing the solution
1802efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1803efe9872eSLisandro Dalcin 
1804efe9872eSLisandro Dalcin    Level: intermediate
1805efe9872eSLisandro Dalcin 
1806efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1807efe9872eSLisandro Dalcin 
1808efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1809efe9872eSLisandro Dalcin @*/
1810efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1811efe9872eSLisandro Dalcin {
1812efe9872eSLisandro Dalcin   PetscFunctionBegin;
1813efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1814efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1815efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1816efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1817efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1818efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1819efe9872eSLisandro Dalcin }
1820efe9872eSLisandro Dalcin 
182155849f57SBarry Smith /*@C
182255849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
182355849f57SBarry Smith 
182455849f57SBarry Smith   Collective on PetscViewer
182555849f57SBarry Smith 
182655849f57SBarry Smith   Input Parameters:
182755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
182855849f57SBarry Smith            some related function before a call to TSLoad().
182955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
183055849f57SBarry Smith 
183155849f57SBarry Smith    Level: intermediate
183255849f57SBarry Smith 
183355849f57SBarry Smith   Notes:
183455849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
183555849f57SBarry Smith 
183655849f57SBarry Smith   Notes for advanced users:
183755849f57SBarry Smith   Most users should not need to know the details of the binary storage
183855849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
183955849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
184055849f57SBarry Smith   format is
184155849f57SBarry Smith .vb
184255849f57SBarry Smith      has not yet been determined
184355849f57SBarry Smith .ve
184455849f57SBarry Smith 
184555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
184655849f57SBarry Smith @*/
1847f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
184855849f57SBarry Smith {
184955849f57SBarry Smith   PetscErrorCode ierr;
185055849f57SBarry Smith   PetscBool      isbinary;
185155849f57SBarry Smith   PetscInt       classid;
185255849f57SBarry Smith   char           type[256];
18532d53ad75SBarry Smith   DMTS           sdm;
1854ad6bc421SBarry Smith   DM             dm;
185555849f57SBarry Smith 
185655849f57SBarry Smith   PetscFunctionBegin;
1857f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
185855849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
185955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
186055849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
186155849f57SBarry Smith 
1862060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1863ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1864060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1865f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1866f2c2a1b9SBarry Smith   if (ts->ops->load) {
1867f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1868f2c2a1b9SBarry Smith   }
1869ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1870ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1871ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1872f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1873f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18742d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18752d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
187655849f57SBarry Smith   PetscFunctionReturn(0);
187755849f57SBarry Smith }
187855849f57SBarry Smith 
18799804daf3SBarry Smith #include <petscdraw.h>
1880e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1881e04113cfSBarry Smith #include <petscviewersaws.h>
1882f05ece33SBarry Smith #endif
18837e2c5f70SBarry Smith /*@C
1884d763cef2SBarry Smith     TSView - Prints the TS data structure.
1885d763cef2SBarry Smith 
18864c49b128SBarry Smith     Collective on TS
1887d763cef2SBarry Smith 
1888d763cef2SBarry Smith     Input Parameters:
1889d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1890d763cef2SBarry Smith -   viewer - visualization context
1891d763cef2SBarry Smith 
1892d763cef2SBarry Smith     Options Database Key:
1893d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1894d763cef2SBarry Smith 
1895d763cef2SBarry Smith     Notes:
1896d763cef2SBarry Smith     The available visualization contexts include
1897b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1898b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1899d763cef2SBarry Smith          output where only the first processor opens
1900d763cef2SBarry Smith          the file.  All other processors send their
1901d763cef2SBarry Smith          data to the first processor to print.
1902d763cef2SBarry Smith 
1903d763cef2SBarry Smith     The user can open an alternative visualization context with
1904b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1905d763cef2SBarry Smith 
1906d763cef2SBarry Smith     Level: beginner
1907d763cef2SBarry Smith 
1908d763cef2SBarry Smith .keywords: TS, timestep, view
1909d763cef2SBarry Smith 
1910b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1911d763cef2SBarry Smith @*/
19127087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1913d763cef2SBarry Smith {
1914dfbe8321SBarry Smith   PetscErrorCode ierr;
191519fd82e9SBarry Smith   TSType         type;
19162b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19172d53ad75SBarry Smith   DMTS           sdm;
1918e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1919536b137fSBarry Smith   PetscBool      issaws;
1920f05ece33SBarry Smith #endif
1921d763cef2SBarry Smith 
1922d763cef2SBarry Smith   PetscFunctionBegin;
19230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19243050cee2SBarry Smith   if (!viewer) {
1925ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19263050cee2SBarry Smith   }
19270700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1928c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1929fd16b177SBarry Smith 
1930251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1931251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
193255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19332b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1934e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1935536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1936f05ece33SBarry Smith #endif
193732077d6dSBarry Smith   if (iascii) {
1938dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
193977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
19407c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1941d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
19425ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1943c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1944d763cef2SBarry Smith     }
19455ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1946193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1947a0af407cSBarry Smith     if (ts->vrtol) {
1948a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1949a0af407cSBarry Smith     } else {
1950a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1951a0af407cSBarry Smith     }
1952a0af407cSBarry Smith     if (ts->vatol) {
1953a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1954a0af407cSBarry Smith     } else {
1955a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1956a0af407cSBarry Smith     }
19572d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19582d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1959d52bd9f3SBarry Smith     if (ts->ops->view) {
1960d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1961d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1962d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1963d52bd9f3SBarry Smith     }
19640f5bd95cSBarry Smith   } else if (isstring) {
1965a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1966b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
196755849f57SBarry Smith   } else if (isbinary) {
196855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
196955849f57SBarry Smith     MPI_Comm    comm;
197055849f57SBarry Smith     PetscMPIInt rank;
197155849f57SBarry Smith     char        type[256];
197255849f57SBarry Smith 
197355849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
197455849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
197555849f57SBarry Smith     if (!rank) {
197655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
197755849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
197855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
197955849f57SBarry Smith     }
198055849f57SBarry Smith     if (ts->ops->view) {
198155849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
198255849f57SBarry Smith     }
1983f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1984f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19852d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19862d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19872b0a91c0SBarry Smith   } else if (isdraw) {
19882b0a91c0SBarry Smith     PetscDraw draw;
19892b0a91c0SBarry Smith     char      str[36];
199089fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19912b0a91c0SBarry Smith 
19922b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19932b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19942b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19952b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
199651fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
199789fd9fafSBarry Smith     bottom = y - h;
19982b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19992b0a91c0SBarry Smith     if (ts->ops->view) {
20002b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20012b0a91c0SBarry Smith     }
20022b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2003e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2004536b137fSBarry Smith   } else if (issaws) {
2005d45a07a7SBarry Smith     PetscMPIInt rank;
20062657e9d9SBarry Smith     const char  *name;
20072657e9d9SBarry Smith 
20082657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2009d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2010d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2011d45a07a7SBarry Smith       char       dir[1024];
2012d45a07a7SBarry Smith 
2013e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2014a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20152657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20162657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20172657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2018d763cef2SBarry Smith     }
20190acecf5bSBarry Smith     if (ts->ops->view) {
20200acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20210acecf5bSBarry Smith     }
2022f05ece33SBarry Smith #endif
2023f05ece33SBarry Smith   }
2024f05ece33SBarry Smith 
2025b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2026251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2027b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2028d763cef2SBarry Smith   PetscFunctionReturn(0);
2029d763cef2SBarry Smith }
2030d763cef2SBarry Smith 
2031d763cef2SBarry Smith 
2032b07ff414SBarry Smith /*@
2033d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2034d763cef2SBarry Smith    the timesteppers.
2035d763cef2SBarry Smith 
20363f9fe445SBarry Smith    Logically Collective on TS
2037d763cef2SBarry Smith 
2038d763cef2SBarry Smith    Input Parameters:
2039d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2040d763cef2SBarry Smith -  usrP - optional user context
2041d763cef2SBarry Smith 
2042daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2043daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2044daf670e6SBarry Smith 
2045d763cef2SBarry Smith    Level: intermediate
2046d763cef2SBarry Smith 
2047d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2048d763cef2SBarry Smith 
2049d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2050d763cef2SBarry Smith @*/
20517087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2052d763cef2SBarry Smith {
2053d763cef2SBarry Smith   PetscFunctionBegin;
20540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2055d763cef2SBarry Smith   ts->user = usrP;
2056d763cef2SBarry Smith   PetscFunctionReturn(0);
2057d763cef2SBarry Smith }
2058d763cef2SBarry Smith 
2059b07ff414SBarry Smith /*@
2060d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2061d763cef2SBarry Smith     timestepper.
2062d763cef2SBarry Smith 
2063d763cef2SBarry Smith     Not Collective
2064d763cef2SBarry Smith 
2065d763cef2SBarry Smith     Input Parameter:
2066d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2067d763cef2SBarry Smith 
2068d763cef2SBarry Smith     Output Parameter:
2069d763cef2SBarry Smith .   usrP - user context
2070d763cef2SBarry Smith 
2071daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2072daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2073daf670e6SBarry Smith 
2074d763cef2SBarry Smith     Level: intermediate
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2079d763cef2SBarry Smith @*/
2080e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2081d763cef2SBarry Smith {
2082d763cef2SBarry Smith   PetscFunctionBegin;
20830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2084e71120c6SJed Brown   *(void**)usrP = ts->user;
2085d763cef2SBarry Smith   PetscFunctionReturn(0);
2086d763cef2SBarry Smith }
2087d763cef2SBarry Smith 
2088d763cef2SBarry Smith /*@
2089b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
2090d763cef2SBarry Smith 
2091d763cef2SBarry Smith    Not Collective
2092d763cef2SBarry Smith 
2093d763cef2SBarry Smith    Input Parameter:
2094d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2095d763cef2SBarry Smith 
2096d763cef2SBarry Smith    Output Parameter:
2097b8123daeSJed Brown .  iter - number of steps completed so far
2098d763cef2SBarry Smith 
2099d763cef2SBarry Smith    Level: intermediate
2100d763cef2SBarry Smith 
2101d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
21029be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2103d763cef2SBarry Smith @*/
21047087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
2105d763cef2SBarry Smith {
2106d763cef2SBarry Smith   PetscFunctionBegin;
21070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21084482741eSBarry Smith   PetscValidIntPointer(iter,2);
2109d763cef2SBarry Smith   *iter = ts->steps;
2110d763cef2SBarry Smith   PetscFunctionReturn(0);
2111d763cef2SBarry Smith }
2112d763cef2SBarry Smith 
2113d763cef2SBarry Smith /*@
2114d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
2115d763cef2SBarry Smith    as well as the initial time.
2116d763cef2SBarry Smith 
21173f9fe445SBarry Smith    Logically Collective on TS
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith    Input Parameters:
2120d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2121d763cef2SBarry Smith .  initial_time - the initial time
2122d763cef2SBarry Smith -  time_step - the size of the timestep
2123d763cef2SBarry Smith 
2124d763cef2SBarry Smith    Level: intermediate
2125d763cef2SBarry Smith 
2126d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
2127d763cef2SBarry Smith 
2128d763cef2SBarry Smith .keywords: TS, set, initial, timestep
2129d763cef2SBarry Smith @*/
21307087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2131d763cef2SBarry Smith {
2132e144a568SJed Brown   PetscErrorCode ierr;
2133e144a568SJed Brown 
2134d763cef2SBarry Smith   PetscFunctionBegin;
21350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2136e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2137d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2138d763cef2SBarry Smith   PetscFunctionReturn(0);
2139d763cef2SBarry Smith }
2140d763cef2SBarry Smith 
2141d763cef2SBarry Smith /*@
2142d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2143d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2144d763cef2SBarry Smith 
21453f9fe445SBarry Smith    Logically Collective on TS
2146d763cef2SBarry Smith 
2147d763cef2SBarry Smith    Input Parameters:
2148d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2149d763cef2SBarry Smith -  time_step - the size of the timestep
2150d763cef2SBarry Smith 
2151d763cef2SBarry Smith    Level: intermediate
2152d763cef2SBarry Smith 
2153d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2154d763cef2SBarry Smith 
2155d763cef2SBarry Smith .keywords: TS, set, timestep
2156d763cef2SBarry Smith @*/
21577087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2158d763cef2SBarry Smith {
2159d763cef2SBarry Smith   PetscFunctionBegin;
21600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2161c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2162d763cef2SBarry Smith   ts->time_step = time_step;
2163d763cef2SBarry Smith   PetscFunctionReturn(0);
2164d763cef2SBarry Smith }
2165d763cef2SBarry Smith 
2166a43b19c4SJed Brown /*@
216749354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
216849354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
216949354f04SShri Abhyankar      or just return at the final time TS computed.
2170a43b19c4SJed Brown 
2171a43b19c4SJed Brown   Logically Collective on TS
2172a43b19c4SJed Brown 
2173a43b19c4SJed Brown    Input Parameter:
2174a43b19c4SJed Brown +   ts - the time-step context
217549354f04SShri Abhyankar -   eftopt - exact final time option
2176a43b19c4SJed Brown 
2177feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2178feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2179feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2180feed9e9dSBarry Smith 
2181feed9e9dSBarry Smith    Options Database:
2182feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2183feed9e9dSBarry Smith 
2184ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2185ee346746SBarry Smith     then the final time you selected.
2186ee346746SBarry Smith 
2187a43b19c4SJed Brown    Level: beginner
2188a43b19c4SJed Brown 
2189a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
2190a43b19c4SJed Brown @*/
219149354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2192a43b19c4SJed Brown {
2193a43b19c4SJed Brown   PetscFunctionBegin;
2194a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
219549354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
219649354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2197a43b19c4SJed Brown   PetscFunctionReturn(0);
2198a43b19c4SJed Brown }
2199a43b19c4SJed Brown 
2200d763cef2SBarry Smith /*@
2201d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2202d763cef2SBarry Smith 
2203d763cef2SBarry Smith    Not Collective
2204d763cef2SBarry Smith 
2205d763cef2SBarry Smith    Input Parameter:
2206d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2207d763cef2SBarry Smith 
2208d763cef2SBarry Smith    Output Parameter:
2209d763cef2SBarry Smith .  dt - the current timestep size
2210d763cef2SBarry Smith 
2211d763cef2SBarry Smith    Level: intermediate
2212d763cef2SBarry Smith 
2213d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2214d763cef2SBarry Smith 
2215d763cef2SBarry Smith .keywords: TS, get, timestep
2216d763cef2SBarry Smith @*/
22177087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2218d763cef2SBarry Smith {
2219d763cef2SBarry Smith   PetscFunctionBegin;
22200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2221f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2222d763cef2SBarry Smith   *dt = ts->time_step;
2223d763cef2SBarry Smith   PetscFunctionReturn(0);
2224d763cef2SBarry Smith }
2225d763cef2SBarry Smith 
2226d8e5e3e6SSatish Balay /*@
2227d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2228d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2229d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2230d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2231d763cef2SBarry Smith 
2232d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2233d763cef2SBarry Smith 
2234d763cef2SBarry Smith    Input Parameter:
2235d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2236d763cef2SBarry Smith 
2237d763cef2SBarry Smith    Output Parameter:
2238d763cef2SBarry Smith .  v - the vector containing the solution
2239d763cef2SBarry Smith 
224063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
224163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
224263e21af5SBarry Smith 
2243d763cef2SBarry Smith    Level: intermediate
2244d763cef2SBarry Smith 
2245b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()
2246d763cef2SBarry Smith 
2247d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2248d763cef2SBarry Smith @*/
22497087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2250d763cef2SBarry Smith {
2251d763cef2SBarry Smith   PetscFunctionBegin;
22520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22534482741eSBarry Smith   PetscValidPointer(v,2);
22548737fe31SLisandro Dalcin   *v = ts->vec_sol;
2255d763cef2SBarry Smith   PetscFunctionReturn(0);
2256d763cef2SBarry Smith }
2257d763cef2SBarry Smith 
225803fe5f5eSDebojyoti Ghosh /*@
2259b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
226003fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2261b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
226203fe5f5eSDebojyoti Ghosh    structure as the solution vector.
226303fe5f5eSDebojyoti Ghosh 
226403fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
226503fe5f5eSDebojyoti Ghosh 
226603fe5f5eSDebojyoti Ghosh    Parameters :
226703fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2268b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2269b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
227003fe5f5eSDebojyoti Ghosh        returned in v.
2271b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
227203fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2273b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
227403fe5f5eSDebojyoti Ghosh 
22754cdd57e5SDebojyoti Ghosh    Level: advanced
227603fe5f5eSDebojyoti Ghosh 
227703fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
227803fe5f5eSDebojyoti Ghosh 
227903fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
228003fe5f5eSDebojyoti Ghosh @*/
2281b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
228203fe5f5eSDebojyoti Ghosh {
228303fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
228403fe5f5eSDebojyoti Ghosh 
228503fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
228603fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2287b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
228803fe5f5eSDebojyoti Ghosh   else {
2289b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
229003fe5f5eSDebojyoti Ghosh   }
229103fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
229203fe5f5eSDebojyoti Ghosh }
229303fe5f5eSDebojyoti Ghosh 
22944cdd57e5SDebojyoti Ghosh /*@
22954cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
22964cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
22974cdd57e5SDebojyoti Ghosh 
22984cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
22994cdd57e5SDebojyoti Ghosh 
23004cdd57e5SDebojyoti Ghosh    Parameters :
23014cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
23024cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
23034cdd57e5SDebojyoti Ghosh 
23044cdd57e5SDebojyoti Ghosh    Level: intermediate
23054cdd57e5SDebojyoti Ghosh 
23064cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23074cdd57e5SDebojyoti Ghosh 
23084cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
23094cdd57e5SDebojyoti Ghosh @*/
23104cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23114cdd57e5SDebojyoti Ghosh {
23124cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23134cdd57e5SDebojyoti Ghosh 
23144cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23154cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2316f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23174cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2318f6356ec7SDebojyoti Ghosh   } else {
2319f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2320f6356ec7SDebojyoti Ghosh   }
23214cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23224cdd57e5SDebojyoti Ghosh }
23234cdd57e5SDebojyoti Ghosh 
23244cdd57e5SDebojyoti Ghosh /*@
23254cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23264cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23274cdd57e5SDebojyoti Ghosh 
23284cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23294cdd57e5SDebojyoti Ghosh 
23309657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23319657682dSDebojyoti Ghosh 
23324cdd57e5SDebojyoti Ghosh    Parameters :
23334cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2334657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
23354cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
23364cdd57e5SDebojyoti Ghosh 
23374cdd57e5SDebojyoti Ghosh    Level: intermediate
23384cdd57e5SDebojyoti Ghosh 
233957df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
23404cdd57e5SDebojyoti Ghosh 
23414cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
23424cdd57e5SDebojyoti Ghosh @*/
23430a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
23444cdd57e5SDebojyoti Ghosh {
23454cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23464cdd57e5SDebojyoti Ghosh 
23474cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23484cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2349f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
23500a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2351f6356ec7SDebojyoti Ghosh   } else {
2352f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2353f6356ec7SDebojyoti Ghosh   }
23544cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23554cdd57e5SDebojyoti Ghosh }
23564cdd57e5SDebojyoti Ghosh 
235757df6a1bSDebojyoti Ghosh /*@
235857df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
235957df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
236057df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
236157df6a1bSDebojyoti Ghosh 
236257df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
236357df6a1bSDebojyoti Ghosh 
236457df6a1bSDebojyoti Ghosh    Parameters :
236557df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
236657df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
236757df6a1bSDebojyoti Ghosh 
236857df6a1bSDebojyoti Ghosh    Level: intermediate
236957df6a1bSDebojyoti Ghosh 
237057df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
237157df6a1bSDebojyoti Ghosh 
237257df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
237357df6a1bSDebojyoti Ghosh @*/
237457df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
237557df6a1bSDebojyoti Ghosh {
237657df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
237757df6a1bSDebojyoti Ghosh 
237857df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
237957df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23809657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
238157df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
238257df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
238357df6a1bSDebojyoti Ghosh   }
238457df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
238557df6a1bSDebojyoti Ghosh }
238657df6a1bSDebojyoti Ghosh 
2387c235aa8dSHong Zhang /*@
2388dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2389c235aa8dSHong Zhang 
2390c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2391c235aa8dSHong Zhang 
2392c235aa8dSHong Zhang    Input Parameter:
2393c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2394c235aa8dSHong Zhang 
2395c235aa8dSHong Zhang    Output Parameter:
2396abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2397abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2398c235aa8dSHong Zhang 
2399c235aa8dSHong Zhang    Level: intermediate
2400c235aa8dSHong Zhang 
2401c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2402c235aa8dSHong Zhang 
2403c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2404c235aa8dSHong Zhang @*/
2405dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2406c235aa8dSHong Zhang {
2407c235aa8dSHong Zhang   PetscFunctionBegin;
2408c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2409abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2410abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2411abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2412c235aa8dSHong Zhang   PetscFunctionReturn(0);
2413c235aa8dSHong Zhang }
2414c235aa8dSHong Zhang 
2415bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2416d8e5e3e6SSatish Balay /*@
2417bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2418d763cef2SBarry Smith 
2419bdad233fSMatthew Knepley   Not collective
2420d763cef2SBarry Smith 
2421bdad233fSMatthew Knepley   Input Parameters:
2422bdad233fSMatthew Knepley + ts   - The TS
2423bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2424d763cef2SBarry Smith .vb
24250910c330SBarry Smith          U_t - A U = 0      (linear)
24260910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24270910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2428d763cef2SBarry Smith .ve
2429d763cef2SBarry Smith 
2430d763cef2SBarry Smith    Level: beginner
2431d763cef2SBarry Smith 
2432bdad233fSMatthew Knepley .keywords: TS, problem type
2433bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2434d763cef2SBarry Smith @*/
24357087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2436a7cc72afSBarry Smith {
24379e2a6581SJed Brown   PetscErrorCode ierr;
24389e2a6581SJed Brown 
2439d763cef2SBarry Smith   PetscFunctionBegin;
24400700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2441bdad233fSMatthew Knepley   ts->problem_type = type;
24429e2a6581SJed Brown   if (type == TS_LINEAR) {
24439e2a6581SJed Brown     SNES snes;
24449e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24459e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24469e2a6581SJed Brown   }
2447d763cef2SBarry Smith   PetscFunctionReturn(0);
2448d763cef2SBarry Smith }
2449d763cef2SBarry Smith 
2450bdad233fSMatthew Knepley /*@C
2451bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2452bdad233fSMatthew Knepley 
2453bdad233fSMatthew Knepley   Not collective
2454bdad233fSMatthew Knepley 
2455bdad233fSMatthew Knepley   Input Parameter:
2456bdad233fSMatthew Knepley . ts   - The TS
2457bdad233fSMatthew Knepley 
2458bdad233fSMatthew Knepley   Output Parameter:
2459bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2460bdad233fSMatthew Knepley .vb
2461089b2837SJed Brown          M U_t = A U
2462089b2837SJed Brown          M(t) U_t = A(t) U
2463b5abc632SBarry Smith          F(t,U,U_t)
2464bdad233fSMatthew Knepley .ve
2465bdad233fSMatthew Knepley 
2466bdad233fSMatthew Knepley    Level: beginner
2467bdad233fSMatthew Knepley 
2468bdad233fSMatthew Knepley .keywords: TS, problem type
2469bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2470bdad233fSMatthew Knepley @*/
24717087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2472a7cc72afSBarry Smith {
2473bdad233fSMatthew Knepley   PetscFunctionBegin;
24740700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24754482741eSBarry Smith   PetscValidIntPointer(type,2);
2476bdad233fSMatthew Knepley   *type = ts->problem_type;
2477bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2478bdad233fSMatthew Knepley }
2479d763cef2SBarry Smith 
2480d763cef2SBarry Smith /*@
2481d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2482d763cef2SBarry Smith    of a timestepper.
2483d763cef2SBarry Smith 
2484d763cef2SBarry Smith    Collective on TS
2485d763cef2SBarry Smith 
2486d763cef2SBarry Smith    Input Parameter:
2487d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2488d763cef2SBarry Smith 
2489d763cef2SBarry Smith    Notes:
2490d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2491d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2492d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
2493d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2494d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
2495d763cef2SBarry Smith 
2496d763cef2SBarry Smith    Level: advanced
2497d763cef2SBarry Smith 
2498d763cef2SBarry Smith .keywords: TS, timestep, setup
2499d763cef2SBarry Smith 
2500d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
2501d763cef2SBarry Smith @*/
25027087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2503d763cef2SBarry Smith {
2504dfbe8321SBarry Smith   PetscErrorCode ierr;
25056c6b9e74SPeter Brune   DM             dm;
25066c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2507d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2508cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25096c6b9e74SPeter Brune   TSIJacobian    ijac;
2510efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25116c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25122ffb9264SLisandro Dalcin   PetscBool      isnone;
2513d763cef2SBarry Smith 
2514d763cef2SBarry Smith   PetscFunctionBegin;
25150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2516277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2517277b19d0SLisandro Dalcin 
25187adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2519cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2520cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2521d763cef2SBarry Smith   }
2522277b19d0SLisandro Dalcin 
2523277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2524277b19d0SLisandro Dalcin 
2525e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2526e1244c69SJed Brown     Mat Amat,Pmat;
2527e1244c69SJed Brown     SNES snes;
2528e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2529e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2530e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2531e1244c69SJed Brown      * have displaced the RHS matrix */
2532e1244c69SJed Brown     if (Amat == ts->Arhs) {
2533e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2534e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2535e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2536e1244c69SJed Brown     }
2537e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2538e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2539e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2540e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2541e1244c69SJed Brown     }
2542e1244c69SJed Brown   }
25432ffb9264SLisandro Dalcin 
25442ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25452ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25462ffb9264SLisandro Dalcin 
2547277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2548000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2549277b19d0SLisandro Dalcin   }
2550277b19d0SLisandro Dalcin 
25512ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
25522ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
25532ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
25542ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25552ffb9264SLisandro Dalcin 
2556a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25576c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25586c6b9e74SPeter Brune    */
25596c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25600298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25616c6b9e74SPeter Brune   if (!func) {
25626c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25636c6b9e74SPeter Brune   }
2564a6772fa2SLisandro 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.
25656c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25666c6b9e74SPeter Brune    */
25670298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25680298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2569efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25700298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2571efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25726c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25736c6b9e74SPeter Brune   }
2574c0517034SDebojyoti Ghosh 
2575c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2576c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2577c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2578c0517034SDebojyoti Ghosh   }
2579c0517034SDebojyoti Ghosh 
2580277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2581277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2582277b19d0SLisandro Dalcin }
2583277b19d0SLisandro Dalcin 
2584f6a906c0SBarry Smith /*@
2585f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2586f6a906c0SBarry Smith    of an adjoint solver
2587f6a906c0SBarry Smith 
2588f6a906c0SBarry Smith    Collective on TS
2589f6a906c0SBarry Smith 
2590f6a906c0SBarry Smith    Input Parameter:
2591f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2592f6a906c0SBarry Smith 
2593f6a906c0SBarry Smith    Level: advanced
2594f6a906c0SBarry Smith 
2595f6a906c0SBarry Smith .keywords: TS, timestep, setup
2596f6a906c0SBarry Smith 
2597947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2598f6a906c0SBarry Smith @*/
2599f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2600f6a906c0SBarry Smith {
2601f6a906c0SBarry Smith   PetscErrorCode ierr;
2602f6a906c0SBarry Smith 
2603f6a906c0SBarry Smith   PetscFunctionBegin;
2604f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2605f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2606dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2607d8151eeaSBarry Smith 
2608947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2609d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2610d8151eeaSBarry Smith     if (ts->vecs_sensip){
2611d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2612d8151eeaSBarry Smith     }
2613947abb85SHong Zhang   }
2614d8151eeaSBarry Smith 
261542f2b339SBarry Smith   if (ts->ops->adjointsetup) {
261642f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2617f6a906c0SBarry Smith   }
2618f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2619f6a906c0SBarry Smith   PetscFunctionReturn(0);
2620f6a906c0SBarry Smith }
2621f6a906c0SBarry Smith 
2622277b19d0SLisandro Dalcin /*@
2623277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2624277b19d0SLisandro Dalcin 
2625277b19d0SLisandro Dalcin    Collective on TS
2626277b19d0SLisandro Dalcin 
2627277b19d0SLisandro Dalcin    Input Parameter:
2628277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2629277b19d0SLisandro Dalcin 
2630277b19d0SLisandro Dalcin    Level: beginner
2631277b19d0SLisandro Dalcin 
2632277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2633277b19d0SLisandro Dalcin 
2634277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2635277b19d0SLisandro Dalcin @*/
2636277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2637277b19d0SLisandro Dalcin {
2638277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2639277b19d0SLisandro Dalcin 
2640277b19d0SLisandro Dalcin   PetscFunctionBegin;
2641277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2642b18ea86cSHong Zhang 
2643277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2644277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2645277b19d0SLisandro Dalcin   }
2646277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2647e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2648bbd56ea5SKarl Rupp 
26494e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26504e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2651214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26526bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2653efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2654e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2655e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
265638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2657bbd56ea5SKarl Rupp 
2658947abb85SHong Zhang  if (ts->vec_costintegral) {
2659d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2660d8151eeaSBarry Smith     if (ts->vecs_drdp){
2661d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2662d8151eeaSBarry Smith     }
2663947abb85SHong Zhang   }
2664d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2665d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2666ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26672c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
266836eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2669277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2670d763cef2SBarry Smith   PetscFunctionReturn(0);
2671d763cef2SBarry Smith }
2672d763cef2SBarry Smith 
2673d8e5e3e6SSatish Balay /*@
2674d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2675d763cef2SBarry Smith    with TSCreate().
2676d763cef2SBarry Smith 
2677d763cef2SBarry Smith    Collective on TS
2678d763cef2SBarry Smith 
2679d763cef2SBarry Smith    Input Parameter:
2680d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith    Level: beginner
2683d763cef2SBarry Smith 
2684d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2685d763cef2SBarry Smith 
2686d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2687d763cef2SBarry Smith @*/
26886bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2689d763cef2SBarry Smith {
26906849ba73SBarry Smith   PetscErrorCode ierr;
2691d763cef2SBarry Smith 
2692d763cef2SBarry Smith   PetscFunctionBegin;
26936bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26946bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26956bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2696d763cef2SBarry Smith 
26976bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2698277b19d0SLisandro Dalcin 
2699e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2700e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27016bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27026d4c513bSLisandro Dalcin 
2703bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2704bc952696SBarry Smith 
270584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27066427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27076427ac75SLisandro Dalcin 
27086bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27096bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27106bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27110dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27126d4c513bSLisandro Dalcin 
2713a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2714d763cef2SBarry Smith   PetscFunctionReturn(0);
2715d763cef2SBarry Smith }
2716d763cef2SBarry Smith 
2717d8e5e3e6SSatish Balay /*@
2718d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2719d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2720d763cef2SBarry Smith 
2721d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2722d763cef2SBarry Smith 
2723d763cef2SBarry Smith    Input Parameter:
2724d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2725d763cef2SBarry Smith 
2726d763cef2SBarry Smith    Output Parameter:
2727d763cef2SBarry Smith .  snes - the nonlinear solver context
2728d763cef2SBarry Smith 
2729d763cef2SBarry Smith    Notes:
2730d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2731d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
273294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2733d763cef2SBarry Smith 
2734d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27350298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2736d763cef2SBarry Smith 
2737d763cef2SBarry Smith    Level: beginner
2738d763cef2SBarry Smith 
2739d763cef2SBarry Smith .keywords: timestep, get, SNES
2740d763cef2SBarry Smith @*/
27417087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2742d763cef2SBarry Smith {
2743d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2744d372ba47SLisandro Dalcin 
2745d763cef2SBarry Smith   PetscFunctionBegin;
27460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27474482741eSBarry Smith   PetscValidPointer(snes,2);
2748d372ba47SLisandro Dalcin   if (!ts->snes) {
2749ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
27500298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27513bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2752d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2753496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27549e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27559e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27569e2a6581SJed Brown     }
2757d372ba47SLisandro Dalcin   }
2758d763cef2SBarry Smith   *snes = ts->snes;
2759d763cef2SBarry Smith   PetscFunctionReturn(0);
2760d763cef2SBarry Smith }
2761d763cef2SBarry Smith 
2762deb2cd25SJed Brown /*@
2763deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2764deb2cd25SJed Brown 
2765deb2cd25SJed Brown    Collective
2766deb2cd25SJed Brown 
2767deb2cd25SJed Brown    Input Parameter:
2768deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2769deb2cd25SJed Brown -  snes - the nonlinear solver context
2770deb2cd25SJed Brown 
2771deb2cd25SJed Brown    Notes:
2772deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2773deb2cd25SJed Brown 
2774deb2cd25SJed Brown    Level: developer
2775deb2cd25SJed Brown 
2776deb2cd25SJed Brown .keywords: timestep, set, SNES
2777deb2cd25SJed Brown @*/
2778deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2779deb2cd25SJed Brown {
2780deb2cd25SJed Brown   PetscErrorCode ierr;
2781d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2782deb2cd25SJed Brown 
2783deb2cd25SJed Brown   PetscFunctionBegin;
2784deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2785deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2786deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2787deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2788bbd56ea5SKarl Rupp 
2789deb2cd25SJed Brown   ts->snes = snes;
2790bbd56ea5SKarl Rupp 
27910298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27920298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2793740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27940298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2795740132f1SEmil Constantinescu   }
2796deb2cd25SJed Brown   PetscFunctionReturn(0);
2797deb2cd25SJed Brown }
2798deb2cd25SJed Brown 
2799d8e5e3e6SSatish Balay /*@
280094b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2801d763cef2SBarry Smith    a TS (timestepper) context.
2802d763cef2SBarry Smith 
280394b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2804d763cef2SBarry Smith 
2805d763cef2SBarry Smith    Input Parameter:
2806d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2807d763cef2SBarry Smith 
2808d763cef2SBarry Smith    Output Parameter:
280994b7f48cSBarry Smith .  ksp - the nonlinear solver context
2810d763cef2SBarry Smith 
2811d763cef2SBarry Smith    Notes:
281294b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2813d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2814d763cef2SBarry Smith    KSP and PC contexts as well.
2815d763cef2SBarry Smith 
281694b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28170298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2818d763cef2SBarry Smith 
2819d763cef2SBarry Smith    Level: beginner
2820d763cef2SBarry Smith 
282194b7f48cSBarry Smith .keywords: timestep, get, KSP
2822d763cef2SBarry Smith @*/
28237087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2824d763cef2SBarry Smith {
2825d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2826089b2837SJed Brown   SNES           snes;
2827d372ba47SLisandro Dalcin 
2828d763cef2SBarry Smith   PetscFunctionBegin;
28290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28304482741eSBarry Smith   PetscValidPointer(ksp,2);
283117186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2832e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2833089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2834089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2835d763cef2SBarry Smith   PetscFunctionReturn(0);
2836d763cef2SBarry Smith }
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2839d763cef2SBarry Smith 
2840adb62b0dSMatthew Knepley /*@
2841adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2842adb62b0dSMatthew Knepley    maximum time for iteration.
2843adb62b0dSMatthew Knepley 
28443f9fe445SBarry Smith    Not Collective
2845adb62b0dSMatthew Knepley 
2846adb62b0dSMatthew Knepley    Input Parameters:
2847adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
28480298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
28490298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2850adb62b0dSMatthew Knepley 
2851adb62b0dSMatthew Knepley    Level: intermediate
2852adb62b0dSMatthew Knepley 
2853adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2854adb62b0dSMatthew Knepley @*/
28557087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2856adb62b0dSMatthew Knepley {
2857adb62b0dSMatthew Knepley   PetscFunctionBegin;
28580700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2859abc0a331SBarry Smith   if (maxsteps) {
28604482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2861adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2862adb62b0dSMatthew Knepley   }
2863abc0a331SBarry Smith   if (maxtime) {
28644482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2865adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2866adb62b0dSMatthew Knepley   }
2867adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2868adb62b0dSMatthew Knepley }
2869adb62b0dSMatthew Knepley 
2870d763cef2SBarry Smith /*@
2871d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2872d763cef2SBarry Smith    maximum time for iteration.
2873d763cef2SBarry Smith 
28743f9fe445SBarry Smith    Logically Collective on TS
2875d763cef2SBarry Smith 
2876d763cef2SBarry Smith    Input Parameters:
2877d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2878d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2879d763cef2SBarry Smith -  maxtime - final time to iterate to
2880d763cef2SBarry Smith 
2881d763cef2SBarry Smith    Options Database Keys:
2882d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
28833bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2884d763cef2SBarry Smith 
2885d763cef2SBarry Smith    Notes:
2886d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2887d763cef2SBarry Smith 
2888d763cef2SBarry Smith    Level: intermediate
2889d763cef2SBarry Smith 
2890d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2891a43b19c4SJed Brown 
2892a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2893d763cef2SBarry Smith @*/
28947087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2895d763cef2SBarry Smith {
2896d763cef2SBarry Smith   PetscFunctionBegin;
28970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2898c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2899c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
290039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
290139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2902d763cef2SBarry Smith   PetscFunctionReturn(0);
2903d763cef2SBarry Smith }
2904d763cef2SBarry Smith 
2905d763cef2SBarry Smith /*@
2906d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2907d763cef2SBarry Smith    for use by the TS routines.
2908d763cef2SBarry Smith 
29093f9fe445SBarry Smith    Logically Collective on TS and Vec
2910d763cef2SBarry Smith 
2911d763cef2SBarry Smith    Input Parameters:
2912d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
29130910c330SBarry Smith -  u - the solution vector
2914d763cef2SBarry Smith 
2915d763cef2SBarry Smith    Level: beginner
2916d763cef2SBarry Smith 
2917d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2918d763cef2SBarry Smith @*/
29190910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2920d763cef2SBarry Smith {
29218737fe31SLisandro Dalcin   PetscErrorCode ierr;
2922496e6a7aSJed Brown   DM             dm;
29238737fe31SLisandro Dalcin 
2924d763cef2SBarry Smith   PetscFunctionBegin;
29250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29260910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
29270910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
29286bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
29290910c330SBarry Smith   ts->vec_sol = u;
2930bbd56ea5SKarl Rupp 
2931496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
29320910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2933d763cef2SBarry Smith   PetscFunctionReturn(0);
2934d763cef2SBarry Smith }
2935d763cef2SBarry Smith 
293673b18844SBarry Smith /*@
293773b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
293873b18844SBarry Smith 
293973b18844SBarry Smith    Logically Collective on TS
294073b18844SBarry Smith 
294173b18844SBarry Smith    Input Parameters:
294273b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
294373b18844SBarry Smith .  steps - number of steps to use
294473b18844SBarry Smith 
294573b18844SBarry Smith    Level: intermediate
294673b18844SBarry Smith 
294773b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
294873b18844SBarry Smith           so as to integrate back to less than the original timestep
294973b18844SBarry Smith 
295073b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
295173b18844SBarry Smith 
295273b18844SBarry Smith .seealso: TSSetExactFinalTime()
295373b18844SBarry Smith @*/
295473b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
295573b18844SBarry Smith {
295673b18844SBarry Smith   PetscFunctionBegin;
295773b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
295873b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
295973b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
296073b18844SBarry Smith   if (steps > ts->total_steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps");
296173b18844SBarry Smith   ts->adjoint_max_steps = steps;
296273b18844SBarry Smith   PetscFunctionReturn(0);
296373b18844SBarry Smith }
296473b18844SBarry Smith 
2965c235aa8dSHong Zhang /*@
2966dfb21088SHong Zhang    TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters
29670cf82b59SBarry Smith       for use by the TSAdjoint routines.
2968c235aa8dSHong Zhang 
2969c235aa8dSHong Zhang    Logically Collective on TS and Vec
2970c235aa8dSHong Zhang 
2971c235aa8dSHong Zhang    Input Parameters:
2972c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2973abc2977eSBarry 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
2974abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2975c235aa8dSHong Zhang 
2976c235aa8dSHong Zhang    Level: beginner
2977c235aa8dSHong Zhang 
2978abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
29790cf82b59SBarry Smith 
2980c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2981c235aa8dSHong Zhang @*/
2982dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2983c235aa8dSHong Zhang {
2984c235aa8dSHong Zhang   PetscFunctionBegin;
2985c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2986abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2987abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2988abc2977eSBarry Smith   ts->vecs_sensip = mu;
2989dfb21088SHong 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");
2990abc2977eSBarry Smith   ts->numcost  = numcost;
2991ad8e2604SHong Zhang   PetscFunctionReturn(0);
2992ad8e2604SHong Zhang }
2993ad8e2604SHong Zhang 
2994ad8e2604SHong Zhang /*@C
29950cf82b59SBarry 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.
2996ad8e2604SHong Zhang 
2997ad8e2604SHong Zhang   Logically Collective on TS
2998ad8e2604SHong Zhang 
2999ad8e2604SHong Zhang   Input Parameters:
3000ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
3001ad8e2604SHong Zhang - func - The function
3002ad8e2604SHong Zhang 
3003ad8e2604SHong Zhang   Calling sequence of func:
30040cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
300505755b9cSHong Zhang +   t - current timestep
30060cf82b59SBarry Smith .   y - input vector (current ODE solution)
300705755b9cSHong Zhang .   A - output matrix
300805755b9cSHong Zhang -   ctx - [optional] user-defined function context
3009ad8e2604SHong Zhang 
3010ad8e2604SHong Zhang   Level: intermediate
3011ad8e2604SHong Zhang 
30120cf82b59SBarry 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
30130cf82b59SBarry Smith 
3014ad8e2604SHong Zhang .keywords: TS, sensitivity
3015ad8e2604SHong Zhang .seealso:
3016ad8e2604SHong Zhang @*/
30175bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
3018ad8e2604SHong Zhang {
3019ad8e2604SHong Zhang   PetscErrorCode ierr;
3020ad8e2604SHong Zhang 
3021ad8e2604SHong Zhang   PetscFunctionBegin;
3022ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3023ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
3024ad8e2604SHong Zhang 
3025ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
3026ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
3027ad8e2604SHong Zhang   if(Amat) {
3028ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
3029ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
3030ad8e2604SHong Zhang     ts->Jacp = Amat;
3031ad8e2604SHong Zhang   }
3032ad8e2604SHong Zhang   PetscFunctionReturn(0);
3033ad8e2604SHong Zhang }
3034ad8e2604SHong Zhang 
30350cf82b59SBarry Smith /*@C
30365bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
3037ad8e2604SHong Zhang 
3038ad8e2604SHong Zhang   Collective on TS
3039ad8e2604SHong Zhang 
3040ad8e2604SHong Zhang   Input Parameters:
3041ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
3042ad8e2604SHong Zhang 
3043ad8e2604SHong Zhang   Level: developer
3044ad8e2604SHong Zhang 
3045ad8e2604SHong Zhang .keywords: TS, sensitivity
30465bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
3047ad8e2604SHong Zhang @*/
30485bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
3049ad8e2604SHong Zhang {
3050ad8e2604SHong Zhang   PetscErrorCode ierr;
3051ad8e2604SHong Zhang 
3052ad8e2604SHong Zhang   PetscFunctionBegin;
3053ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3054ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3055ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
3056ad8e2604SHong Zhang 
3057ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
3058ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
3059ad8e2604SHong Zhang   PetscStackPop;
3060c235aa8dSHong Zhang   PetscFunctionReturn(0);
3061c235aa8dSHong Zhang }
3062c235aa8dSHong Zhang 
30636fd0887fSHong Zhang /*@C
3064dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
30656fd0887fSHong Zhang 
30666fd0887fSHong Zhang     Logically Collective on TS
30676fd0887fSHong Zhang 
30686fd0887fSHong Zhang     Input Parameters:
30696fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
3070abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
30710cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
3072b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3073b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3074feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3075b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
30766fd0887fSHong Zhang 
30770cf82b59SBarry Smith     Calling sequence of rf:
30780cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
30796fd0887fSHong Zhang 
30806fd0887fSHong Zhang +   t - current timestep
30810cf82b59SBarry Smith .   y - input vector
30820cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
30836fd0887fSHong Zhang -   ctx - [optional] user-defined function context
30846fd0887fSHong Zhang 
3085b612ec54SBarry Smith    Calling sequence of drdyf:
30860cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3087b612ec54SBarry Smith 
3088b612ec54SBarry Smith    Calling sequence of drdpf:
30890cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3090b612ec54SBarry Smith 
3091b612ec54SBarry Smith     Level: intermediate
30926fd0887fSHong Zhang 
3093abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
30940cf82b59SBarry Smith 
30956fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
30966fd0887fSHong Zhang 
3097dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
30986fd0887fSHong Zhang @*/
3099dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3100d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3101b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3102b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
31036fd0887fSHong Zhang {
31046fd0887fSHong Zhang   PetscErrorCode ierr;
31056fd0887fSHong Zhang 
31066fd0887fSHong Zhang   PetscFunctionBegin;
31076fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3108dfb21088SHong 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()");
3109c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
31106fd0887fSHong Zhang 
3111b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
3112abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
31132c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
31140cf82b59SBarry Smith   ts->costintegrand    = rf;
311505755b9cSHong Zhang   ts->costintegrandctx = ctx;
3116b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3117b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
31186fd0887fSHong Zhang   PetscFunctionReturn(0);
31196fd0887fSHong Zhang }
31206fd0887fSHong Zhang 
312136eaed60SHong Zhang /*@
3122dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
312336eaed60SHong Zhang    It is valid to call the routine after a backward run.
312436eaed60SHong Zhang 
312536eaed60SHong Zhang    Not Collective
312636eaed60SHong Zhang 
312736eaed60SHong Zhang    Input Parameter:
312836eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
312936eaed60SHong Zhang 
313036eaed60SHong Zhang    Output Parameter:
31312c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
313236eaed60SHong Zhang 
313336eaed60SHong Zhang    Level: intermediate
313436eaed60SHong Zhang 
3135dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
313636eaed60SHong Zhang 
313736eaed60SHong Zhang .keywords: TS, sensitivity analysis
313836eaed60SHong Zhang @*/
3139dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
314036eaed60SHong Zhang {
314136eaed60SHong Zhang   PetscFunctionBegin;
314236eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
314336eaed60SHong Zhang   PetscValidPointer(v,2);
31442c39e106SBarry Smith   *v = ts->vec_costintegral;
314536eaed60SHong Zhang   PetscFunctionReturn(0);
314636eaed60SHong Zhang }
314736eaed60SHong Zhang 
31486fd0887fSHong Zhang /*@
31492c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
31506fd0887fSHong Zhang 
31516fd0887fSHong Zhang    Input Parameters:
31526fd0887fSHong Zhang +  ts - the TS context
31536fd0887fSHong Zhang .  t - current time
31540cf82b59SBarry Smith -  y - state vector, i.e. current solution
31556fd0887fSHong Zhang 
31566fd0887fSHong Zhang    Output Parameter:
3157abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
31586fd0887fSHong Zhang 
31596fd0887fSHong Zhang    Note:
31606fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
31616fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
31626fd0887fSHong Zhang 
31636fd0887fSHong Zhang    Level: developer
31646fd0887fSHong Zhang 
31656fd0887fSHong Zhang .keywords: TS, compute
31666fd0887fSHong Zhang 
3167dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
31686fd0887fSHong Zhang @*/
31690cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
31706fd0887fSHong Zhang {
31716fd0887fSHong Zhang   PetscErrorCode ierr;
31726fd0887fSHong Zhang 
31736fd0887fSHong Zhang   PetscFunctionBegin;
31746fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31750cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
31766fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
31776fd0887fSHong Zhang 
31780cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3179e4680132SHong Zhang   if (ts->costintegrand) {
3180e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
31810cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
31826fd0887fSHong Zhang     PetscStackPop;
31836fd0887fSHong Zhang   } else {
31846fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
31856fd0887fSHong Zhang   }
31866fd0887fSHong Zhang 
31870cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
31886fd0887fSHong Zhang   PetscFunctionReturn(0);
31896fd0887fSHong Zhang }
31906fd0887fSHong Zhang 
319105755b9cSHong Zhang /*@
3192d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
319305755b9cSHong Zhang 
319405755b9cSHong Zhang   Collective on TS
319505755b9cSHong Zhang 
319605755b9cSHong Zhang   Input Parameters:
319705755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
319805755b9cSHong Zhang 
319905755b9cSHong Zhang   Notes:
3200d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
320105755b9cSHong Zhang   so most users would not generally call this routine themselves.
320205755b9cSHong Zhang 
320305755b9cSHong Zhang   Level: developer
320405755b9cSHong Zhang 
320505755b9cSHong Zhang .keywords: TS, sensitivity
3206d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
320705755b9cSHong Zhang @*/
32080cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
320905755b9cSHong Zhang {
321005755b9cSHong Zhang   PetscErrorCode ierr;
321105755b9cSHong Zhang 
321205755b9cSHong Zhang   PetscFunctionBegin;
321305755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32140cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
321505755b9cSHong Zhang 
321605755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
32170cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
321805755b9cSHong Zhang   PetscStackPop;
321905755b9cSHong Zhang   PetscFunctionReturn(0);
322005755b9cSHong Zhang }
322105755b9cSHong Zhang 
322205755b9cSHong Zhang /*@
3223d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
322405755b9cSHong Zhang 
322505755b9cSHong Zhang   Collective on TS
322605755b9cSHong Zhang 
322705755b9cSHong Zhang   Input Parameters:
322805755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
322905755b9cSHong Zhang 
323005755b9cSHong Zhang   Notes:
323105755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
323205755b9cSHong Zhang   so most users would not generally call this routine themselves.
323305755b9cSHong Zhang 
323405755b9cSHong Zhang   Level: developer
323505755b9cSHong Zhang 
323605755b9cSHong Zhang .keywords: TS, sensitivity
3237d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
323805755b9cSHong Zhang @*/
32390cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
324005755b9cSHong Zhang {
324105755b9cSHong Zhang   PetscErrorCode ierr;
324205755b9cSHong Zhang 
324305755b9cSHong Zhang   PetscFunctionBegin;
324405755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32450cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
324605755b9cSHong Zhang 
324705755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
32480cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
324905755b9cSHong Zhang   PetscStackPop;
325005755b9cSHong Zhang   PetscFunctionReturn(0);
325105755b9cSHong Zhang }
325205755b9cSHong Zhang 
3253ac226902SBarry Smith /*@C
3254000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32553f2090d5SJed Brown   called once at the beginning of each time step.
3256000e7ae3SMatthew Knepley 
32573f9fe445SBarry Smith   Logically Collective on TS
3258000e7ae3SMatthew Knepley 
3259000e7ae3SMatthew Knepley   Input Parameters:
3260000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3261000e7ae3SMatthew Knepley - func - The function
3262000e7ae3SMatthew Knepley 
3263000e7ae3SMatthew Knepley   Calling sequence of func:
3264000e7ae3SMatthew Knepley . func (TS ts);
3265000e7ae3SMatthew Knepley 
3266000e7ae3SMatthew Knepley   Level: intermediate
3267000e7ae3SMatthew Knepley 
3268000e7ae3SMatthew Knepley .keywords: TS, timestep
32699be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
3270000e7ae3SMatthew Knepley @*/
32717087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3272000e7ae3SMatthew Knepley {
3273000e7ae3SMatthew Knepley   PetscFunctionBegin;
32740700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3275ae60f76fSBarry Smith   ts->prestep = func;
3276000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3277000e7ae3SMatthew Knepley }
3278000e7ae3SMatthew Knepley 
327909ee8438SJed Brown /*@
32803f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32813f2090d5SJed Brown 
32823f2090d5SJed Brown   Collective on TS
32833f2090d5SJed Brown 
32843f2090d5SJed Brown   Input Parameters:
32853f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32863f2090d5SJed Brown 
32873f2090d5SJed Brown   Notes:
32883f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32893f2090d5SJed Brown   so most users would not generally call this routine themselves.
32903f2090d5SJed Brown 
32913f2090d5SJed Brown   Level: developer
32923f2090d5SJed Brown 
32933f2090d5SJed Brown .keywords: TS, timestep
32949be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32953f2090d5SJed Brown @*/
32967087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32973f2090d5SJed Brown {
32983f2090d5SJed Brown   PetscErrorCode ierr;
32993f2090d5SJed Brown 
33003f2090d5SJed Brown   PetscFunctionBegin;
33010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3302ae60f76fSBarry Smith   if (ts->prestep) {
3303ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
3304312ce896SJed Brown   }
33053f2090d5SJed Brown   PetscFunctionReturn(0);
33063f2090d5SJed Brown }
33073f2090d5SJed Brown 
3308b8123daeSJed Brown /*@C
3309b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3310b8123daeSJed Brown   called once at the beginning of each stage.
3311b8123daeSJed Brown 
3312b8123daeSJed Brown   Logically Collective on TS
3313b8123daeSJed Brown 
3314b8123daeSJed Brown   Input Parameters:
3315b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3316b8123daeSJed Brown - func - The function
3317b8123daeSJed Brown 
3318b8123daeSJed Brown   Calling sequence of func:
3319b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3320b8123daeSJed Brown 
3321b8123daeSJed Brown   Level: intermediate
3322b8123daeSJed Brown 
3323b8123daeSJed Brown   Note:
3324b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3325b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
3326b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3327b8123daeSJed Brown 
3328b8123daeSJed Brown .keywords: TS, timestep
33299be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3330b8123daeSJed Brown @*/
3331b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3332b8123daeSJed Brown {
3333b8123daeSJed Brown   PetscFunctionBegin;
3334b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3335ae60f76fSBarry Smith   ts->prestage = func;
3336b8123daeSJed Brown   PetscFunctionReturn(0);
3337b8123daeSJed Brown }
3338b8123daeSJed Brown 
33399be3e283SDebojyoti Ghosh /*@C
33409be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33419be3e283SDebojyoti Ghosh   called once at the end of each stage.
33429be3e283SDebojyoti Ghosh 
33439be3e283SDebojyoti Ghosh   Logically Collective on TS
33449be3e283SDebojyoti Ghosh 
33459be3e283SDebojyoti Ghosh   Input Parameters:
33469be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33479be3e283SDebojyoti Ghosh - func - The function
33489be3e283SDebojyoti Ghosh 
33499be3e283SDebojyoti Ghosh   Calling sequence of func:
33509be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33519be3e283SDebojyoti Ghosh 
33529be3e283SDebojyoti Ghosh   Level: intermediate
33539be3e283SDebojyoti Ghosh 
33549be3e283SDebojyoti Ghosh   Note:
33559be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
33569be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
33579be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33589be3e283SDebojyoti Ghosh 
33599be3e283SDebojyoti Ghosh .keywords: TS, timestep
33609be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33619be3e283SDebojyoti Ghosh @*/
33629be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33639be3e283SDebojyoti Ghosh {
33649be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33659be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33669be3e283SDebojyoti Ghosh   ts->poststage = func;
33679be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33689be3e283SDebojyoti Ghosh }
33699be3e283SDebojyoti Ghosh 
3370c688d042SShri Abhyankar /*@C
3371c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3372c688d042SShri Abhyankar   called once at the end of each step evaluation.
3373c688d042SShri Abhyankar 
3374c688d042SShri Abhyankar   Logically Collective on TS
3375c688d042SShri Abhyankar 
3376c688d042SShri Abhyankar   Input Parameters:
3377c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3378c688d042SShri Abhyankar - func - The function
3379c688d042SShri Abhyankar 
3380c688d042SShri Abhyankar   Calling sequence of func:
3381c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3382c688d042SShri Abhyankar 
3383c688d042SShri Abhyankar   Level: intermediate
3384c688d042SShri Abhyankar 
3385c688d042SShri Abhyankar   Note:
33861785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33871785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3388e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3389e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3390e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3391c688d042SShri Abhyankar 
3392c688d042SShri Abhyankar .keywords: TS, timestep
3393c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3394c688d042SShri Abhyankar @*/
3395c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3396c688d042SShri Abhyankar {
3397c688d042SShri Abhyankar   PetscFunctionBegin;
3398c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3399c688d042SShri Abhyankar   ts->postevaluate = func;
3400c688d042SShri Abhyankar   PetscFunctionReturn(0);
3401c688d042SShri Abhyankar }
3402c688d042SShri Abhyankar 
3403b8123daeSJed Brown /*@
3404b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3405b8123daeSJed Brown 
3406b8123daeSJed Brown   Collective on TS
3407b8123daeSJed Brown 
3408b8123daeSJed Brown   Input Parameters:
3409b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
34109be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3411b8123daeSJed Brown 
3412b8123daeSJed Brown   Notes:
3413b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3414b8123daeSJed Brown   most users would not generally call this routine themselves.
3415b8123daeSJed Brown 
3416b8123daeSJed Brown   Level: developer
3417b8123daeSJed Brown 
3418b8123daeSJed Brown .keywords: TS, timestep
34199be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3420b8123daeSJed Brown @*/
3421b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3422b8123daeSJed Brown {
3423b8123daeSJed Brown   PetscErrorCode ierr;
3424b8123daeSJed Brown 
3425b8123daeSJed Brown   PetscFunctionBegin;
3426b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3427ae60f76fSBarry Smith   if (ts->prestage) {
3428ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3429b8123daeSJed Brown   }
3430b8123daeSJed Brown   PetscFunctionReturn(0);
3431b8123daeSJed Brown }
3432b8123daeSJed Brown 
34339be3e283SDebojyoti Ghosh /*@
34349be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34359be3e283SDebojyoti Ghosh 
34369be3e283SDebojyoti Ghosh   Collective on TS
34379be3e283SDebojyoti Ghosh 
34389be3e283SDebojyoti Ghosh   Input Parameters:
34399be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34409be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34419be3e283SDebojyoti Ghosh   stageindex  - Stage number
34429be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34439be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34449be3e283SDebojyoti Ghosh 
34459be3e283SDebojyoti Ghosh   Notes:
34469be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34479be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34489be3e283SDebojyoti Ghosh 
34499be3e283SDebojyoti Ghosh   Level: developer
34509be3e283SDebojyoti Ghosh 
34519be3e283SDebojyoti Ghosh .keywords: TS, timestep
34529be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34539be3e283SDebojyoti Ghosh @*/
34549be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34559be3e283SDebojyoti Ghosh {
34569be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
34579be3e283SDebojyoti Ghosh 
34589be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34599be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34604beae5d8SLisandro Dalcin   if (ts->poststage) {
34619be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34629be3e283SDebojyoti Ghosh   }
34639be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34649be3e283SDebojyoti Ghosh }
34659be3e283SDebojyoti Ghosh 
3466c688d042SShri Abhyankar /*@
3467c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3468c688d042SShri Abhyankar 
3469c688d042SShri Abhyankar   Collective on TS
3470c688d042SShri Abhyankar 
3471c688d042SShri Abhyankar   Input Parameters:
3472c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3473c688d042SShri Abhyankar 
3474c688d042SShri Abhyankar   Notes:
3475c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3476c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3477c688d042SShri Abhyankar 
3478c688d042SShri Abhyankar   Level: developer
3479c688d042SShri Abhyankar 
3480c688d042SShri Abhyankar .keywords: TS, timestep
3481c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3482c688d042SShri Abhyankar @*/
3483c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3484c688d042SShri Abhyankar {
3485c688d042SShri Abhyankar   PetscErrorCode ierr;
3486c688d042SShri Abhyankar 
3487c688d042SShri Abhyankar   PetscFunctionBegin;
3488c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3489c688d042SShri Abhyankar   if (ts->postevaluate) {
3490c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3491c688d042SShri Abhyankar   }
3492c688d042SShri Abhyankar   PetscFunctionReturn(0);
3493c688d042SShri Abhyankar }
3494c688d042SShri Abhyankar 
3495ac226902SBarry Smith /*@C
3496000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34973f2090d5SJed Brown   called once at the end of each time step.
3498000e7ae3SMatthew Knepley 
34993f9fe445SBarry Smith   Logically Collective on TS
3500000e7ae3SMatthew Knepley 
3501000e7ae3SMatthew Knepley   Input Parameters:
3502000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3503000e7ae3SMatthew Knepley - func - The function
3504000e7ae3SMatthew Knepley 
3505000e7ae3SMatthew Knepley   Calling sequence of func:
3506b8123daeSJed Brown $ func (TS ts);
3507000e7ae3SMatthew Knepley 
35081785ff2aSShri Abhyankar   Notes:
35091785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
35101785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
35111785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
35121785ff2aSShri Abhyankar 
3513000e7ae3SMatthew Knepley   Level: intermediate
3514000e7ae3SMatthew Knepley 
3515000e7ae3SMatthew Knepley .keywords: TS, timestep
35161785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
3517000e7ae3SMatthew Knepley @*/
35187087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3519000e7ae3SMatthew Knepley {
3520000e7ae3SMatthew Knepley   PetscFunctionBegin;
35210700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3522ae60f76fSBarry Smith   ts->poststep = func;
3523000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3524000e7ae3SMatthew Knepley }
3525000e7ae3SMatthew Knepley 
352609ee8438SJed Brown /*@
35273f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
35283f2090d5SJed Brown 
35293f2090d5SJed Brown   Collective on TS
35303f2090d5SJed Brown 
35313f2090d5SJed Brown   Input Parameters:
35323f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35333f2090d5SJed Brown 
35343f2090d5SJed Brown   Notes:
35353f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35363f2090d5SJed Brown   so most users would not generally call this routine themselves.
35373f2090d5SJed Brown 
35383f2090d5SJed Brown   Level: developer
35393f2090d5SJed Brown 
35403f2090d5SJed Brown .keywords: TS, timestep
35413f2090d5SJed Brown @*/
35427087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35433f2090d5SJed Brown {
35443f2090d5SJed Brown   PetscErrorCode ierr;
35453f2090d5SJed Brown 
35463f2090d5SJed Brown   PetscFunctionBegin;
35470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3548ae60f76fSBarry Smith   if (ts->poststep) {
3549ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
355072ac3e02SJed Brown   }
35513f2090d5SJed Brown   PetscFunctionReturn(0);
35523f2090d5SJed Brown }
35533f2090d5SJed Brown 
3554d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3555d763cef2SBarry Smith 
3556d763cef2SBarry Smith /*@C
3557a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3558d763cef2SBarry Smith    timestep to display the iteration's  progress.
3559d763cef2SBarry Smith 
35603f9fe445SBarry Smith    Logically Collective on TS
3561d763cef2SBarry Smith 
3562d763cef2SBarry Smith    Input Parameters:
3563d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3564e213d8f1SJed Brown .  monitor - monitoring routine
3565329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35660298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3567b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35680298fd71SBarry Smith           (may be NULL)
3569d763cef2SBarry Smith 
3570e213d8f1SJed Brown    Calling sequence of monitor:
35710910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3572d763cef2SBarry Smith 
3573d763cef2SBarry Smith +    ts - the TS context
357463e21af5SBarry 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)
35751f06c33eSBarry Smith .    time - current time
35760910c330SBarry Smith .    u - current iterate
3577d763cef2SBarry Smith -    mctx - [optional] monitoring context
3578d763cef2SBarry Smith 
3579d763cef2SBarry Smith    Notes:
3580d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3581d763cef2SBarry Smith    already has been loaded.
3582d763cef2SBarry Smith 
3583025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3584025f1a04SBarry Smith 
3585d763cef2SBarry Smith    Level: intermediate
3586d763cef2SBarry Smith 
3587d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3588d763cef2SBarry Smith 
3589a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3590d763cef2SBarry Smith @*/
3591c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3592d763cef2SBarry Smith {
359378064530SBarry Smith   PetscErrorCode ierr;
359478064530SBarry Smith   PetscInt       i;
359578064530SBarry Smith   PetscBool      identical;
359678064530SBarry Smith 
3597d763cef2SBarry Smith   PetscFunctionBegin;
35980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
359978064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
360078064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
360178064530SBarry Smith     if (identical) PetscFunctionReturn(0);
360278064530SBarry Smith   }
360317186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3604d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
36058704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3606d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3607d763cef2SBarry Smith   PetscFunctionReturn(0);
3608d763cef2SBarry Smith }
3609d763cef2SBarry Smith 
3610d763cef2SBarry Smith /*@C
3611a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3612d763cef2SBarry Smith 
36133f9fe445SBarry Smith    Logically Collective on TS
3614d763cef2SBarry Smith 
3615d763cef2SBarry Smith    Input Parameters:
3616d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3617d763cef2SBarry Smith 
3618d763cef2SBarry Smith    Notes:
3619d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3620d763cef2SBarry Smith 
3621d763cef2SBarry Smith    Level: intermediate
3622d763cef2SBarry Smith 
3623d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3624d763cef2SBarry Smith 
3625a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3626d763cef2SBarry Smith @*/
36277087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3628d763cef2SBarry Smith {
3629d952e501SBarry Smith   PetscErrorCode ierr;
3630d952e501SBarry Smith   PetscInt       i;
3631d952e501SBarry Smith 
3632d763cef2SBarry Smith   PetscFunctionBegin;
36330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3634d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36358704b422SBarry Smith     if (ts->monitordestroy[i]) {
36368704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3637d952e501SBarry Smith     }
3638d952e501SBarry Smith   }
3639d763cef2SBarry Smith   ts->numbermonitors = 0;
3640d763cef2SBarry Smith   PetscFunctionReturn(0);
3641d763cef2SBarry Smith }
3642d763cef2SBarry Smith 
3643721cd6eeSBarry Smith /*@C
3644721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36455516499fSSatish Balay 
36465516499fSSatish Balay    Level: intermediate
364741251cbbSSatish Balay 
36485516499fSSatish Balay .keywords: TS, set, monitor
36495516499fSSatish Balay 
365063e21af5SBarry Smith .seealso:  TSMonitorSet()
365141251cbbSSatish Balay @*/
3652721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3653d763cef2SBarry Smith {
3654dfbe8321SBarry Smith   PetscErrorCode ierr;
3655721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
365641aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3657d132466eSBarry Smith 
3658d763cef2SBarry Smith   PetscFunctionBegin;
36594d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
366041aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
366141aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3662721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
366341aca3d6SBarry Smith   if (iascii) {
3664649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
366563e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
366663e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
366763e21af5SBarry Smith     } else {
36688392e04aSShri 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);
366963e21af5SBarry Smith     }
3670649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
367141aca3d6SBarry Smith   } else if (ibinary) {
367241aca3d6SBarry Smith     PetscMPIInt rank;
367341aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
367441aca3d6SBarry Smith     if (!rank) {
3675450a797fSBarry Smith       PetscBool skipHeader;
3676450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3677450a797fSBarry Smith 
3678450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3679450a797fSBarry Smith       if (!skipHeader) {
3680450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3681450a797fSBarry Smith        }
368241aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
368341aca3d6SBarry Smith     } else {
368441aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
368541aca3d6SBarry Smith     }
368641aca3d6SBarry Smith   }
3687721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3688d763cef2SBarry Smith   PetscFunctionReturn(0);
3689d763cef2SBarry Smith }
3690d763cef2SBarry Smith 
36919110b2e7SHong Zhang /*@C
36929110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
36939110b2e7SHong Zhang    timestep to display the iteration's  progress.
36949110b2e7SHong Zhang 
36959110b2e7SHong Zhang    Logically Collective on TS
36969110b2e7SHong Zhang 
36979110b2e7SHong Zhang    Input Parameters:
36989110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
36999110b2e7SHong Zhang .  adjointmonitor - monitoring routine
37009110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
37019110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
37029110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
37039110b2e7SHong Zhang           (may be NULL)
37049110b2e7SHong Zhang 
37059110b2e7SHong Zhang    Calling sequence of monitor:
37069110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
37079110b2e7SHong Zhang 
37089110b2e7SHong Zhang +    ts - the TS context
37099110b2e7SHong 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
37109110b2e7SHong Zhang                                been interpolated to)
37119110b2e7SHong Zhang .    time - current time
37129110b2e7SHong Zhang .    u - current iterate
37139110b2e7SHong Zhang .    numcost - number of cost functionos
37149110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
37159110b2e7SHong Zhang .    mu - sensitivities to parameters
37169110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
37179110b2e7SHong Zhang 
37189110b2e7SHong Zhang    Notes:
37199110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
37209110b2e7SHong Zhang    already has been loaded.
37219110b2e7SHong Zhang 
37229110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
37239110b2e7SHong Zhang 
37249110b2e7SHong Zhang    Level: intermediate
37259110b2e7SHong Zhang 
37269110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
37279110b2e7SHong Zhang 
37289110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
37299110b2e7SHong Zhang @*/
37309110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
37319110b2e7SHong Zhang {
373278064530SBarry Smith   PetscErrorCode ierr;
373378064530SBarry Smith   PetscInt       i;
373478064530SBarry Smith   PetscBool      identical;
373578064530SBarry Smith 
37369110b2e7SHong Zhang   PetscFunctionBegin;
37379110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
373878064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
373978064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
374078064530SBarry Smith     if (identical) PetscFunctionReturn(0);
374178064530SBarry Smith   }
37429110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
37439110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
37449110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
37459110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
37469110b2e7SHong Zhang   PetscFunctionReturn(0);
37479110b2e7SHong Zhang }
37489110b2e7SHong Zhang 
37499110b2e7SHong Zhang /*@C
37509110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
37519110b2e7SHong Zhang 
37529110b2e7SHong Zhang    Logically Collective on TS
37539110b2e7SHong Zhang 
37549110b2e7SHong Zhang    Input Parameters:
37559110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
37569110b2e7SHong Zhang 
37579110b2e7SHong Zhang    Notes:
37589110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
37599110b2e7SHong Zhang 
37609110b2e7SHong Zhang    Level: intermediate
37619110b2e7SHong Zhang 
37629110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
37639110b2e7SHong Zhang 
37649110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
37659110b2e7SHong Zhang @*/
37669110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
37679110b2e7SHong Zhang {
37689110b2e7SHong Zhang   PetscErrorCode ierr;
37699110b2e7SHong Zhang   PetscInt       i;
37709110b2e7SHong Zhang 
37719110b2e7SHong Zhang   PetscFunctionBegin;
37729110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37739110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
37749110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
37759110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
37769110b2e7SHong Zhang     }
37779110b2e7SHong Zhang   }
37789110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
37799110b2e7SHong Zhang   PetscFunctionReturn(0);
37809110b2e7SHong Zhang }
37819110b2e7SHong Zhang 
3782721cd6eeSBarry Smith /*@C
3783721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3784110eb670SHong Zhang 
3785110eb670SHong Zhang    Level: intermediate
3786110eb670SHong Zhang 
3787110eb670SHong Zhang .keywords: TS, set, monitor
3788110eb670SHong Zhang 
3789110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3790110eb670SHong Zhang @*/
3791721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3792110eb670SHong Zhang {
3793110eb670SHong Zhang   PetscErrorCode ierr;
3794721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3795110eb670SHong Zhang 
3796110eb670SHong Zhang   PetscFunctionBegin;
3797110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3798721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3799110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3800110eb670SHong 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);
3801110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3802721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3803110eb670SHong Zhang   PetscFunctionReturn(0);
3804110eb670SHong Zhang }
3805110eb670SHong Zhang 
3806cd652676SJed Brown /*@
3807cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3808cd652676SJed Brown 
3809cd652676SJed Brown    Collective on TS
3810cd652676SJed Brown 
3811cd652676SJed Brown    Input Argument:
3812cd652676SJed Brown +  ts - time stepping context
3813cd652676SJed Brown -  t - time to interpolate to
3814cd652676SJed Brown 
3815cd652676SJed Brown    Output Argument:
38160910c330SBarry Smith .  U - state at given time
3817cd652676SJed Brown 
3818cd652676SJed Brown    Level: intermediate
3819cd652676SJed Brown 
3820cd652676SJed Brown    Developer Notes:
3821cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3822cd652676SJed Brown 
3823cd652676SJed Brown .keywords: TS, set
3824cd652676SJed Brown 
3825874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3826cd652676SJed Brown @*/
38270910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3828cd652676SJed Brown {
3829cd652676SJed Brown   PetscErrorCode ierr;
3830cd652676SJed Brown 
3831cd652676SJed Brown   PetscFunctionBegin;
3832cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3833b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3834be5899b3SLisandro 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);
3835ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
38360910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3837cd652676SJed Brown   PetscFunctionReturn(0);
3838cd652676SJed Brown }
3839cd652676SJed Brown 
3840d763cef2SBarry Smith /*@
38416d9e5789SSean Farley    TSStep - Steps one time step
3842d763cef2SBarry Smith 
3843d763cef2SBarry Smith    Collective on TS
3844d763cef2SBarry Smith 
3845d763cef2SBarry Smith    Input Parameter:
3846d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3847d763cef2SBarry Smith 
384827829d71SBarry Smith    Level: developer
3849d763cef2SBarry Smith 
3850b8123daeSJed Brown    Notes:
385127829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
385227829d71SBarry Smith 
3853b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3854b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3855b8123daeSJed Brown 
385625cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
385725cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
385825cb2221SBarry Smith 
3859d763cef2SBarry Smith .keywords: TS, timestep, solve
3860d763cef2SBarry Smith 
38619be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3862d763cef2SBarry Smith @*/
3863193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3864d763cef2SBarry Smith {
3865dfbe8321SBarry Smith   PetscErrorCode   ierr;
3866fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3867be5899b3SLisandro Dalcin   PetscReal        ptime;
3868d763cef2SBarry Smith 
3869d763cef2SBarry Smith   PetscFunctionBegin;
38700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3871fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3872fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3873fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3874fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3875fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3876fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3877302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3878fffbeea8SBarry Smith 
3879d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
388068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3881d405a339SMatthew Knepley 
3882a6772fa2SLisandro 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()");
3883be5899b3SLisandro 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");
3884a6772fa2SLisandro Dalcin 
3885be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3886362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3887be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
3888e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3889d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3890193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3891d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3892be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
3893be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps++;
3894be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
389528d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3896362cd11cSLisandro Dalcin 
3897362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3898cef5090cSJed Brown     if (ts->errorifstepfailed) {
389908c7845fSBarry 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]);
390008c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3901d2daff3dSHong Zhang     }
390208c7845fSBarry Smith   } else if (!ts->reason) {
390308c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
390408c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
390508c7845fSBarry Smith   }
390608c7845fSBarry Smith   PetscFunctionReturn(0);
390708c7845fSBarry Smith }
390808c7845fSBarry Smith 
390908c7845fSBarry Smith /*@
3910b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
391108c7845fSBarry Smith 
391208c7845fSBarry Smith    Collective on TS
391308c7845fSBarry Smith 
391408c7845fSBarry Smith    Input Parameter:
391508c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
391608c7845fSBarry Smith 
39176a4d4014SLisandro Dalcin    Level: intermediate
39186a4d4014SLisandro Dalcin 
3919b957a604SHong Zhang .keywords: TS, adjoint, step
39206a4d4014SLisandro Dalcin 
3921b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
39226a4d4014SLisandro Dalcin @*/
392308c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
39246a4d4014SLisandro Dalcin {
392508c7845fSBarry Smith   DM               dm;
39266a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
39276a4d4014SLisandro Dalcin 
39286a4d4014SLisandro Dalcin   PetscFunctionBegin;
39294a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
393008c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
393108c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3932d763cef2SBarry Smith 
3933685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
3934d763cef2SBarry Smith 
3935be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3936be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
393742f2b339SBarry 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);
3938be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
393942f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
39408d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3941be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps--;
3942362cd11cSLisandro Dalcin 
3943362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3944cef5090cSJed Brown     if (ts->errorifstepfailed) {
39456c4ed002SBarry 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]);
39466c4ed002SBarry 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]);
39476c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3948cef5090cSJed Brown     }
3949362cd11cSLisandro Dalcin   } else if (!ts->reason) {
395073b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
3951362cd11cSLisandro Dalcin   }
3952d763cef2SBarry Smith   PetscFunctionReturn(0);
3953d763cef2SBarry Smith }
3954d763cef2SBarry Smith 
39557cbde773SLisandro Dalcin /*@
39567cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
39577cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
39587cbde773SLisandro Dalcin 
39597cbde773SLisandro Dalcin    Collective on TS
39607cbde773SLisandro Dalcin 
39617cbde773SLisandro Dalcin    Input Arguments:
39627cbde773SLisandro Dalcin +  ts - time stepping context
39637cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
39647cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
39657cbde773SLisandro Dalcin 
39667cbde773SLisandro Dalcin    Output Arguments:
39677cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
39687cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
39697cbde773SLisandro Dalcin 
39707cbde773SLisandro Dalcin    Level: advanced
39717cbde773SLisandro Dalcin 
39727cbde773SLisandro Dalcin    Notes:
39737cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
39747cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
39757cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
39767cbde773SLisandro Dalcin 
39777cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
39787cbde773SLisandro Dalcin @*/
39797cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
39807cbde773SLisandro Dalcin {
39817cbde773SLisandro Dalcin   PetscErrorCode ierr;
39827cbde773SLisandro Dalcin 
39837cbde773SLisandro Dalcin   PetscFunctionBegin;
39847cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39857cbde773SLisandro Dalcin   PetscValidType(ts,1);
39867cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
39877cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
39887cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
39897cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
39907cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
39917cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
39927cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
39937cbde773SLisandro Dalcin   PetscFunctionReturn(0);
39947cbde773SLisandro Dalcin }
39957cbde773SLisandro Dalcin 
399605175c85SJed Brown /*@
399705175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
399805175c85SJed Brown 
39991c3436cfSJed Brown    Collective on TS
400005175c85SJed Brown 
400105175c85SJed Brown    Input Arguments:
40021c3436cfSJed Brown +  ts - time stepping context
40031c3436cfSJed Brown .  order - desired order of accuracy
40040298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
400505175c85SJed Brown 
400605175c85SJed Brown    Output Arguments:
40070910c330SBarry Smith .  U - state at the end of the current step
400805175c85SJed Brown 
400905175c85SJed Brown    Level: advanced
401005175c85SJed Brown 
4011108c343cSJed Brown    Notes:
4012108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
4013108c343cSJed 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.
4014108c343cSJed Brown 
40151c3436cfSJed Brown .seealso: TSStep(), TSAdapt
401605175c85SJed Brown @*/
40170910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
401805175c85SJed Brown {
401905175c85SJed Brown   PetscErrorCode ierr;
402005175c85SJed Brown 
402105175c85SJed Brown   PetscFunctionBegin;
402205175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
402305175c85SJed Brown   PetscValidType(ts,1);
40240910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4025ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
40260910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
402705175c85SJed Brown   PetscFunctionReturn(0);
402805175c85SJed Brown }
402905175c85SJed Brown 
4030b1cb36f3SHong Zhang /*@
4031b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
4032b1cb36f3SHong Zhang 
4033b1cb36f3SHong Zhang  Collective on TS
4034b1cb36f3SHong Zhang 
4035b1cb36f3SHong Zhang  Input Arguments:
4036b1cb36f3SHong Zhang  .  ts - time stepping context
4037b1cb36f3SHong Zhang 
4038b1cb36f3SHong Zhang  Level: advanced
4039b1cb36f3SHong Zhang 
4040b1cb36f3SHong Zhang  Notes:
4041b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
4042b1cb36f3SHong Zhang 
4043b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
4044b1cb36f3SHong Zhang  @*/
4045b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
4046b1cb36f3SHong Zhang {
4047b1cb36f3SHong Zhang     PetscErrorCode ierr;
4048b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4049b1cb36f3SHong 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);
4050b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
4051b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4052b1cb36f3SHong Zhang }
4053d67e68b7SBarry Smith 
4054d763cef2SBarry Smith /*@
4055d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
4056d763cef2SBarry Smith 
4057d763cef2SBarry Smith    Collective on TS
4058d763cef2SBarry Smith 
4059d763cef2SBarry Smith    Input Parameter:
4060d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
406163e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
406263e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
40635a3a76d0SJed Brown 
4064d763cef2SBarry Smith    Level: beginner
4065d763cef2SBarry Smith 
40665a3a76d0SJed Brown    Notes:
40675a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
40685a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
40695a3a76d0SJed Brown    stepped over the final time.
40705a3a76d0SJed Brown 
4071d763cef2SBarry Smith .keywords: TS, timestep, solve
4072d763cef2SBarry Smith 
407363e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4074d763cef2SBarry Smith @*/
4075cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4076d763cef2SBarry Smith {
4077b06615a5SLisandro Dalcin   Vec               solution;
4078d763cef2SBarry Smith   PetscErrorCode    ierr;
4079d763cef2SBarry Smith 
4080d763cef2SBarry Smith   PetscFunctionBegin;
4081d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4082f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4083feed9e9dSBarry Smith 
408449354f04SShri Abhyankar   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
4085b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
40860910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4087b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4088b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4089b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
40905a3a76d0SJed Brown     }
40910910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4092bbd56ea5SKarl Rupp   } else if (u) {
40930910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
40945a3a76d0SJed Brown   }
4095b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
409668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4097a6772fa2SLisandro Dalcin 
4098a6772fa2SLisandro 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()");
4099a6772fa2SLisandro 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");
4100a6772fa2SLisandro Dalcin 
4101e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
4102e7069c78SShri      restarts the step after an event. Resetting these counters in such a case causes
4103e7069c78SShri      TSTrajectory to incorrectly save the output files
4104e7069c78SShri   */
4105938c94caSShri Abhyankar 
4106193ac0bcSJed Brown   ts->steps             = 0;
41075ef26d82SJed Brown   ts->ksp_its           = 0;
41085ef26d82SJed Brown   ts->snes_its          = 0;
4109c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
4110c610991cSLisandro Dalcin   ts->reject            = 0;
4111193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
4112193ac0bcSJed Brown 
4113ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4114f05ece33SBarry Smith 
4115193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4116193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4117a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4118cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4119a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4120be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4121db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
4122db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4123e7069c78SShri 
4124938c94caSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41256427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
4126e7069c78SShri 
412728d5b5d6SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
412828d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_TRUE;
41296427ac75SLisandro Dalcin 
4130e1a7a14fSJed Brown     while (!ts->reason) {
4131938c94caSShri Abhyankar       ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41329687d888SLisandro Dalcin       if (!ts->steprollback) {
41339687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41349687d888SLisandro Dalcin       }
4135193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4136e783b05fSHong 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. */
4137b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4138b1cb36f3SHong Zhang       }
413910b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
4140e783b05fSHong 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. */
4141e783b05fSHong Zhang       if (!ts->steprollback) {
4142938c94caSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41431eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4144aeb4809dSShri Abhyankar       }
4145193ac0bcSJed Brown     }
4146938c94caSShri Abhyankar     ierr = TSMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41476427ac75SLisandro Dalcin 
414849354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41490910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4150cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4151b06615a5SLisandro Dalcin       solution = u;
415263e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41530574a7fbSJed Brown     } else {
4154ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4155cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4156b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41570574a7fbSJed Brown     }
4158193ac0bcSJed Brown   }
4159d2daff3dSHong Zhang 
4160ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
416163e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
416256f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4163ef222394SBarry Smith   if (ts->adjoint_solve) {
4164ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41652b0a91c0SBarry Smith   }
4166d763cef2SBarry Smith   PetscFunctionReturn(0);
4167d763cef2SBarry Smith }
4168d763cef2SBarry Smith 
4169b1cb36f3SHong Zhang /*@
4170b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4171b1cb36f3SHong Zhang 
4172b1cb36f3SHong Zhang  Collective on TS
4173b1cb36f3SHong Zhang 
4174b1cb36f3SHong Zhang  Input Arguments:
4175b1cb36f3SHong Zhang  .  ts - time stepping context
4176b1cb36f3SHong Zhang 
4177b1cb36f3SHong Zhang  Level: advanced
4178b1cb36f3SHong Zhang 
4179b1cb36f3SHong Zhang  Notes:
4180b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4181b1cb36f3SHong Zhang 
4182b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4183b1cb36f3SHong Zhang  @*/
4184b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4185b1cb36f3SHong Zhang {
4186b1cb36f3SHong Zhang     PetscErrorCode ierr;
4187b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4188b1cb36f3SHong 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);
4189b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4190b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4191b1cb36f3SHong Zhang }
4192b1cb36f3SHong Zhang 
4193c88f2d44SBarry Smith /*@
4194c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4195c88f2d44SBarry Smith 
4196c88f2d44SBarry Smith    Collective on TS
4197c88f2d44SBarry Smith 
4198c88f2d44SBarry Smith    Input Parameter:
41992c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4200c88f2d44SBarry Smith 
4201e87fd094SBarry Smith    Options Database:
4202e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
4203e87fd094SBarry Smith 
4204c88f2d44SBarry Smith    Level: intermediate
4205c88f2d44SBarry Smith 
4206c88f2d44SBarry Smith    Notes:
4207c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4208c88f2d44SBarry Smith 
420973b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
421073b18844SBarry Smith 
4211c88f2d44SBarry Smith .keywords: TS, timestep, solve
4212c88f2d44SBarry Smith 
4213b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4214c88f2d44SBarry Smith @*/
42152c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4216c88f2d44SBarry Smith {
4217c88f2d44SBarry Smith   PetscErrorCode    ierr;
4218c88f2d44SBarry Smith 
4219c88f2d44SBarry Smith   PetscFunctionBegin;
4220c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4221c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
422268bece0bSHong Zhang 
4223c88f2d44SBarry Smith   /* reset time step and iteration counters */
4224c88f2d44SBarry Smith   ts->steps             = 0;
4225c88f2d44SBarry Smith   ts->ksp_its           = 0;
4226c88f2d44SBarry Smith   ts->snes_its          = 0;
4227c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4228c88f2d44SBarry Smith   ts->reject            = 0;
4229c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4230c88f2d44SBarry Smith 
423173b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
4232c88f2d44SBarry Smith 
423373b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
4234c88f2d44SBarry Smith   while (!ts->reason) {
423555c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
42369110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
42376427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4238616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4239b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4240b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4241b1cb36f3SHong Zhang     }
4242c88f2d44SBarry Smith   }
424326656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
424426656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4245c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4246e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4247685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
4248c88f2d44SBarry Smith   PetscFunctionReturn(0);
4249c88f2d44SBarry Smith }
4250c88f2d44SBarry Smith 
42517db568b7SBarry Smith /*@C
4252228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4253228d79bcSJed Brown 
4254228d79bcSJed Brown    Collective on TS
4255228d79bcSJed Brown 
4256228d79bcSJed Brown    Input Parameters:
4257228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4258228d79bcSJed Brown .  step - step number that has just completed
4259228d79bcSJed Brown .  ptime - model time of the state
42600910c330SBarry Smith -  u - state at the current model time
4261228d79bcSJed Brown 
4262228d79bcSJed Brown    Notes:
42637db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
42647db568b7SBarry Smith    Users would almost never call this routine directly.
4265228d79bcSJed Brown 
426663e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
426763e21af5SBarry Smith 
42687db568b7SBarry Smith    Level: developer
4269228d79bcSJed Brown 
4270228d79bcSJed Brown .keywords: TS, timestep
4271228d79bcSJed Brown @*/
42720910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4273d763cef2SBarry Smith {
4274d6152f81SLisandro Dalcin   DM             dm;
4275a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4276d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4277d763cef2SBarry Smith 
4278d763cef2SBarry Smith   PetscFunctionBegin;
4279b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4280b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4281d6152f81SLisandro Dalcin 
4282d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4283d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4284d6152f81SLisandro Dalcin 
42855edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4286d763cef2SBarry Smith   for (i=0; i<n; i++) {
42870910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4288d763cef2SBarry Smith   }
42895edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4290d763cef2SBarry Smith   PetscFunctionReturn(0);
4291d763cef2SBarry Smith }
4292d763cef2SBarry Smith 
42937db568b7SBarry Smith /*@C
42949110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
42959110b2e7SHong Zhang 
42969110b2e7SHong Zhang    Collective on TS
42979110b2e7SHong Zhang 
42989110b2e7SHong Zhang    Input Parameters:
42999110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
43009110b2e7SHong Zhang .  step - step number that has just completed
43019110b2e7SHong Zhang .  ptime - model time of the state
43029110b2e7SHong Zhang .  u - state at the current model time
43039110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
43049110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
43059110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
43069110b2e7SHong Zhang 
43079110b2e7SHong Zhang    Notes:
43087db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
43097db568b7SBarry Smith    Users would almost never call this routine directly.
43109110b2e7SHong Zhang 
43117db568b7SBarry Smith    Level: developer
43129110b2e7SHong Zhang 
43139110b2e7SHong Zhang .keywords: TS, timestep
43149110b2e7SHong Zhang @*/
43159110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
43169110b2e7SHong Zhang {
43179110b2e7SHong Zhang   PetscErrorCode ierr;
43189110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
43199110b2e7SHong Zhang 
43209110b2e7SHong Zhang   PetscFunctionBegin;
43219110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43229110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
43239110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
43249110b2e7SHong Zhang   for (i=0; i<n; i++) {
43259110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
43269110b2e7SHong Zhang   }
43279110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
43289110b2e7SHong Zhang   PetscFunctionReturn(0);
43299110b2e7SHong Zhang }
43309110b2e7SHong Zhang 
4331d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4332d763cef2SBarry Smith /*@C
43337db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4334a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4335d763cef2SBarry Smith 
4336d763cef2SBarry Smith    Collective on TS
4337d763cef2SBarry Smith 
4338d763cef2SBarry Smith    Input Parameters:
4339d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4340d763cef2SBarry Smith .  label - the title to put in the title bar
43417c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4342a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4343a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4344d763cef2SBarry Smith 
4345d763cef2SBarry Smith    Output Parameter:
43460b039ecaSBarry Smith .  ctx - the context
4347d763cef2SBarry Smith 
4348d763cef2SBarry Smith    Options Database Key:
43494f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
43507db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
43517db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
43527db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
43537db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4354b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4355d763cef2SBarry Smith 
4356d763cef2SBarry Smith    Notes:
4357a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4358d763cef2SBarry Smith 
43597db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
43607db568b7SBarry Smith 
43617db568b7SBarry 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
43627db568b7SBarry 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
43637db568b7SBarry Smith    as the first argument.
43647db568b7SBarry Smith 
43657db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
43667db568b7SBarry Smith 
43677db568b7SBarry Smith 
4368d763cef2SBarry Smith    Level: intermediate
4369d763cef2SBarry Smith 
43707db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4371d763cef2SBarry Smith 
43727db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
43737db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
43747db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
43757db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
43767db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
43777c922b88SBarry Smith 
4378d763cef2SBarry Smith @*/
4379a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4380d763cef2SBarry Smith {
43817f52daa2SLisandro Dalcin   PetscDraw      draw;
4382dfbe8321SBarry Smith   PetscErrorCode ierr;
4383d763cef2SBarry Smith 
4384d763cef2SBarry Smith   PetscFunctionBegin;
4385b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
43867f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
43877f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
43887f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4389287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
43907f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4391a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4392d763cef2SBarry Smith   PetscFunctionReturn(0);
4393d763cef2SBarry Smith }
4394d763cef2SBarry Smith 
4395b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4396d763cef2SBarry Smith {
43970b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4398c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4399dfbe8321SBarry Smith   PetscErrorCode ierr;
4400d763cef2SBarry Smith 
4401d763cef2SBarry Smith   PetscFunctionBegin;
440263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4403b06615a5SLisandro Dalcin   if (!step) {
4404a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4405a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
44066934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
4407a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4408a9f9c1f6SBarry Smith   }
4409c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
44100b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4411b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
44120b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44136934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
44143923b477SBarry Smith   }
4415d763cef2SBarry Smith   PetscFunctionReturn(0);
4416d763cef2SBarry Smith }
4417d763cef2SBarry Smith 
4418d763cef2SBarry Smith /*@C
4419a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4420a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4421d763cef2SBarry Smith 
44220b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4423d763cef2SBarry Smith 
4424d763cef2SBarry Smith    Input Parameter:
44250b039ecaSBarry Smith .  ctx - the monitor context
4426d763cef2SBarry Smith 
4427d763cef2SBarry Smith    Level: intermediate
4428d763cef2SBarry Smith 
4429d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4430d763cef2SBarry Smith 
44314f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4432d763cef2SBarry Smith @*/
4433a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4434d763cef2SBarry Smith {
4435dfbe8321SBarry Smith   PetscErrorCode ierr;
4436d763cef2SBarry Smith 
4437d763cef2SBarry Smith   PetscFunctionBegin;
44387684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
44397684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
44407684fa3eSBarry Smith   }
44410b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
444231152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4443387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4444387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4445387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
44460b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4447d763cef2SBarry Smith   PetscFunctionReturn(0);
4448d763cef2SBarry Smith }
4449d763cef2SBarry Smith 
4450d763cef2SBarry Smith /*@
4451b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4452d763cef2SBarry Smith 
4453d763cef2SBarry Smith    Not Collective
4454d763cef2SBarry Smith 
4455d763cef2SBarry Smith    Input Parameter:
4456d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4457d763cef2SBarry Smith 
4458d763cef2SBarry Smith    Output Parameter:
445963e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
4460d763cef2SBarry Smith 
4461d763cef2SBarry Smith    Level: beginner
4462d763cef2SBarry Smith 
4463b8123daeSJed Brown    Note:
4464b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
44659be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4466b8123daeSJed Brown 
446763e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
4468d763cef2SBarry Smith 
4469d763cef2SBarry Smith .keywords: TS, get, time
4470d763cef2SBarry Smith @*/
44717087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4472d763cef2SBarry Smith {
4473d763cef2SBarry Smith   PetscFunctionBegin;
44740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4475f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4476d763cef2SBarry Smith   *t = ts->ptime;
4477d763cef2SBarry Smith   PetscFunctionReturn(0);
4478d763cef2SBarry Smith }
4479d763cef2SBarry Smith 
4480e5e524a1SHong Zhang /*@
4481e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4482e5e524a1SHong Zhang 
4483e5e524a1SHong Zhang    Not Collective
4484e5e524a1SHong Zhang 
4485e5e524a1SHong Zhang    Input Parameter:
4486e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4487e5e524a1SHong Zhang 
4488e5e524a1SHong Zhang    Output Parameter:
4489e5e524a1SHong Zhang .  t  - the previous time
4490e5e524a1SHong Zhang 
4491e5e524a1SHong Zhang    Level: beginner
4492e5e524a1SHong Zhang 
4493e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
4494e5e524a1SHong Zhang 
4495e5e524a1SHong Zhang .keywords: TS, get, time
4496e5e524a1SHong Zhang @*/
4497e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4498e5e524a1SHong Zhang {
4499e5e524a1SHong Zhang   PetscFunctionBegin;
4500e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4501e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4502e5e524a1SHong Zhang   *t = ts->ptime_prev;
4503e5e524a1SHong Zhang   PetscFunctionReturn(0);
4504e5e524a1SHong Zhang }
4505e5e524a1SHong Zhang 
45066a4d4014SLisandro Dalcin /*@
45076a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
45086a4d4014SLisandro Dalcin 
45093f9fe445SBarry Smith    Logically Collective on TS
45106a4d4014SLisandro Dalcin 
45116a4d4014SLisandro Dalcin    Input Parameters:
45126a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
45136a4d4014SLisandro Dalcin -  time - the time
45146a4d4014SLisandro Dalcin 
45156a4d4014SLisandro Dalcin    Level: intermediate
45166a4d4014SLisandro Dalcin 
45176a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
45186a4d4014SLisandro Dalcin 
45196a4d4014SLisandro Dalcin .keywords: TS, set, time
45206a4d4014SLisandro Dalcin @*/
45217087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
45226a4d4014SLisandro Dalcin {
45236a4d4014SLisandro Dalcin   PetscFunctionBegin;
45240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4525c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
45266a4d4014SLisandro Dalcin   ts->ptime = t;
45276a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
45286a4d4014SLisandro Dalcin }
45296a4d4014SLisandro Dalcin 
4530d763cef2SBarry Smith /*@C
4531d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4532d763cef2SBarry Smith    TS options in the database.
4533d763cef2SBarry Smith 
45343f9fe445SBarry Smith    Logically Collective on TS
4535d763cef2SBarry Smith 
4536d763cef2SBarry Smith    Input Parameter:
4537d763cef2SBarry Smith +  ts     - The TS context
4538d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4539d763cef2SBarry Smith 
4540d763cef2SBarry Smith    Notes:
4541d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4542d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4543d763cef2SBarry Smith    hyphen.
4544d763cef2SBarry Smith 
4545d763cef2SBarry Smith    Level: advanced
4546d763cef2SBarry Smith 
4547d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4548d763cef2SBarry Smith 
4549d763cef2SBarry Smith .seealso: TSSetFromOptions()
4550d763cef2SBarry Smith 
4551d763cef2SBarry Smith @*/
45527087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4553d763cef2SBarry Smith {
4554dfbe8321SBarry Smith   PetscErrorCode ierr;
4555089b2837SJed Brown   SNES           snes;
4556d763cef2SBarry Smith 
4557d763cef2SBarry Smith   PetscFunctionBegin;
45580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4559d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4560089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4561089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4562d763cef2SBarry Smith   PetscFunctionReturn(0);
4563d763cef2SBarry Smith }
4564d763cef2SBarry Smith 
4565d763cef2SBarry Smith 
4566d763cef2SBarry Smith /*@C
4567d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4568d763cef2SBarry Smith    TS options in the database.
4569d763cef2SBarry Smith 
45703f9fe445SBarry Smith    Logically Collective on TS
4571d763cef2SBarry Smith 
4572d763cef2SBarry Smith    Input Parameter:
4573d763cef2SBarry Smith +  ts     - The TS context
4574d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4575d763cef2SBarry Smith 
4576d763cef2SBarry Smith    Notes:
4577d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4578d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4579d763cef2SBarry Smith    hyphen.
4580d763cef2SBarry Smith 
4581d763cef2SBarry Smith    Level: advanced
4582d763cef2SBarry Smith 
4583d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4584d763cef2SBarry Smith 
4585d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4586d763cef2SBarry Smith 
4587d763cef2SBarry Smith @*/
45887087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4589d763cef2SBarry Smith {
4590dfbe8321SBarry Smith   PetscErrorCode ierr;
4591089b2837SJed Brown   SNES           snes;
4592d763cef2SBarry Smith 
4593d763cef2SBarry Smith   PetscFunctionBegin;
45940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4595d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4596089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4597089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4598d763cef2SBarry Smith   PetscFunctionReturn(0);
4599d763cef2SBarry Smith }
4600d763cef2SBarry Smith 
4601d763cef2SBarry Smith /*@C
4602d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4603d763cef2SBarry Smith    TS options in the database.
4604d763cef2SBarry Smith 
4605d763cef2SBarry Smith    Not Collective
4606d763cef2SBarry Smith 
4607d763cef2SBarry Smith    Input Parameter:
4608d763cef2SBarry Smith .  ts - The TS context
4609d763cef2SBarry Smith 
4610d763cef2SBarry Smith    Output Parameter:
4611d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4612d763cef2SBarry Smith 
4613d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4614d763cef2SBarry Smith    sufficient length to hold the prefix.
4615d763cef2SBarry Smith 
4616d763cef2SBarry Smith    Level: intermediate
4617d763cef2SBarry Smith 
4618d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4619d763cef2SBarry Smith 
4620d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4621d763cef2SBarry Smith @*/
46227087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4623d763cef2SBarry Smith {
4624dfbe8321SBarry Smith   PetscErrorCode ierr;
4625d763cef2SBarry Smith 
4626d763cef2SBarry Smith   PetscFunctionBegin;
46270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46284482741eSBarry Smith   PetscValidPointer(prefix,2);
4629d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4630d763cef2SBarry Smith   PetscFunctionReturn(0);
4631d763cef2SBarry Smith }
4632d763cef2SBarry Smith 
4633d763cef2SBarry Smith /*@C
4634d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4635d763cef2SBarry Smith 
4636d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4637d763cef2SBarry Smith 
4638d763cef2SBarry Smith    Input Parameter:
4639d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4640d763cef2SBarry Smith 
4641d763cef2SBarry Smith    Output Parameters:
4642e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4643e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4644e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4645e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4646d763cef2SBarry Smith 
46470298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4648d763cef2SBarry Smith 
4649d763cef2SBarry Smith    Level: intermediate
4650d763cef2SBarry Smith 
465126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4652d763cef2SBarry Smith 
4653d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4654d763cef2SBarry Smith @*/
4655e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4656d763cef2SBarry Smith {
4657089b2837SJed Brown   PetscErrorCode ierr;
465824989b8cSPeter Brune   DM             dm;
4659089b2837SJed Brown 
4660d763cef2SBarry Smith   PetscFunctionBegin;
466123a57915SBarry Smith   if (Amat || Pmat) {
466223a57915SBarry Smith     SNES snes;
4663089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
466423a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4665e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
466623a57915SBarry Smith   }
466724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
466824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4669d763cef2SBarry Smith   PetscFunctionReturn(0);
4670d763cef2SBarry Smith }
4671d763cef2SBarry Smith 
46722eca1d9cSJed Brown /*@C
46732eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
46742eca1d9cSJed Brown 
46752eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
46762eca1d9cSJed Brown 
46772eca1d9cSJed Brown    Input Parameter:
46782eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
46792eca1d9cSJed Brown 
46802eca1d9cSJed Brown    Output Parameters:
4681e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4682e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
46832eca1d9cSJed Brown .  f   - The function to compute the matrices
46842eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
46852eca1d9cSJed Brown 
46860298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
46872eca1d9cSJed Brown 
46882eca1d9cSJed Brown    Level: advanced
46892eca1d9cSJed Brown 
46902eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
46912eca1d9cSJed Brown 
46922eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
46932eca1d9cSJed Brown @*/
4694e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
46952eca1d9cSJed Brown {
4696089b2837SJed Brown   PetscErrorCode ierr;
469724989b8cSPeter Brune   DM             dm;
46980910c330SBarry Smith 
46992eca1d9cSJed Brown   PetscFunctionBegin;
4700c0aab802Sstefano_zampini   if (Amat || Pmat) {
4701c0aab802Sstefano_zampini     SNES snes;
4702089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4703f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4704e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4705c0aab802Sstefano_zampini   }
470624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
470724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
47082eca1d9cSJed Brown   PetscFunctionReturn(0);
47092eca1d9cSJed Brown }
47102eca1d9cSJed Brown 
47116083293cSBarry Smith 
47121713a123SBarry Smith /*@C
471383a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
47141713a123SBarry Smith    VecView() for the solution at each timestep
47151713a123SBarry Smith 
47161713a123SBarry Smith    Collective on TS
47171713a123SBarry Smith 
47181713a123SBarry Smith    Input Parameters:
47191713a123SBarry Smith +  ts - the TS context
47201713a123SBarry Smith .  step - current time-step
4721142b95e3SSatish Balay .  ptime - current time
47220298fd71SBarry Smith -  dummy - either a viewer or NULL
47231713a123SBarry Smith 
472499fdda47SBarry Smith    Options Database:
472599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
472699fdda47SBarry Smith 
4727387f4636SBarry 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
472899fdda47SBarry Smith        will look bad
472999fdda47SBarry Smith 
47301713a123SBarry Smith    Level: intermediate
47311713a123SBarry Smith 
47321713a123SBarry Smith .keywords: TS,  vector, monitor, view
47331713a123SBarry Smith 
4734a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47351713a123SBarry Smith @*/
47360910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47371713a123SBarry Smith {
4738dfbe8321SBarry Smith   PetscErrorCode   ierr;
473983a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4740473a3ab2SBarry Smith   PetscDraw        draw;
47411713a123SBarry Smith 
47421713a123SBarry Smith   PetscFunctionBegin;
47436083293cSBarry Smith   if (!step && ictx->showinitial) {
47446083293cSBarry Smith     if (!ictx->initialsolution) {
47450910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
47461713a123SBarry Smith     }
47470910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
47486083293cSBarry Smith   }
4749b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
47500dcf80beSBarry Smith 
47516083293cSBarry Smith   if (ictx->showinitial) {
47526083293cSBarry Smith     PetscReal pause;
47536083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
47546083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
47556083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
47566083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
47576083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
47586083293cSBarry Smith   }
47590910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4760473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
476151fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4762473a3ab2SBarry Smith     char      time[32];
4763473a3ab2SBarry Smith 
4764473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
476585b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4766473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4767473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
476851fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4769473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4770473a3ab2SBarry Smith   }
4771473a3ab2SBarry Smith 
47726083293cSBarry Smith   if (ictx->showinitial) {
47736083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
47746083293cSBarry Smith   }
47751713a123SBarry Smith   PetscFunctionReturn(0);
47761713a123SBarry Smith }
47771713a123SBarry Smith 
47789110b2e7SHong Zhang /*@C
47799110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
47809110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
47819110b2e7SHong Zhang 
47829110b2e7SHong Zhang    Collective on TS
47839110b2e7SHong Zhang 
47849110b2e7SHong Zhang    Input Parameters:
47859110b2e7SHong Zhang +  ts - the TS context
47869110b2e7SHong Zhang .  step - current time-step
47879110b2e7SHong Zhang .  ptime - current time
47889110b2e7SHong Zhang .  u - current state
47899110b2e7SHong Zhang .  numcost - number of cost functions
47909110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
47919110b2e7SHong Zhang .  mu - sensitivities to parameters
47929110b2e7SHong Zhang -  dummy - either a viewer or NULL
47939110b2e7SHong Zhang 
47949110b2e7SHong Zhang    Level: intermediate
47959110b2e7SHong Zhang 
47969110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
47979110b2e7SHong Zhang 
47989110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
47999110b2e7SHong Zhang @*/
48009110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
48019110b2e7SHong Zhang {
48029110b2e7SHong Zhang   PetscErrorCode   ierr;
48039110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
48049110b2e7SHong Zhang   PetscDraw        draw;
4805a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4806a0046e2cSSatish Balay   char             time[32];
48079110b2e7SHong Zhang 
48089110b2e7SHong Zhang   PetscFunctionBegin;
48099110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48109110b2e7SHong Zhang 
48119110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
48129110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
48139110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
48149110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
48159110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
48169110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
48179110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
48189110b2e7SHong Zhang   PetscFunctionReturn(0);
48199110b2e7SHong Zhang }
48209110b2e7SHong Zhang 
48212d5ee99bSBarry Smith /*@C
48222d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
48232d5ee99bSBarry Smith 
48242d5ee99bSBarry Smith    Collective on TS
48252d5ee99bSBarry Smith 
48262d5ee99bSBarry Smith    Input Parameters:
48272d5ee99bSBarry Smith +  ts - the TS context
48282d5ee99bSBarry Smith .  step - current time-step
48292d5ee99bSBarry Smith .  ptime - current time
48302d5ee99bSBarry Smith -  dummy - either a viewer or NULL
48312d5ee99bSBarry Smith 
48322d5ee99bSBarry Smith    Level: intermediate
48332d5ee99bSBarry Smith 
48342d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
48352d5ee99bSBarry Smith 
48362d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
48372d5ee99bSBarry Smith @*/
48382d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
48392d5ee99bSBarry Smith {
48402d5ee99bSBarry Smith   PetscErrorCode    ierr;
48412d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
48422d5ee99bSBarry Smith   PetscDraw         draw;
48436934998bSLisandro Dalcin   PetscDrawAxis     axis;
48442d5ee99bSBarry Smith   PetscInt          n;
48452d5ee99bSBarry Smith   PetscMPIInt       size;
48466934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
48472d5ee99bSBarry Smith   char              time[32];
48482d5ee99bSBarry Smith   const PetscScalar *U;
48492d5ee99bSBarry Smith 
48502d5ee99bSBarry Smith   PetscFunctionBegin;
48516934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
48526934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
48532d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
48546934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
48552d5ee99bSBarry Smith 
48562d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
48576934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
48586934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
48596934998bSLisandro Dalcin   if (!step) {
48606934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
48616934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
48626934998bSLisandro Dalcin   }
48632d5ee99bSBarry Smith 
48642d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
48656934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
48666934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4867a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
48686934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
48692d5ee99bSBarry Smith 
48706934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
48716934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
48722d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
48734b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
487485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
48752d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
487651fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
48772d5ee99bSBarry Smith   }
48786934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
48792d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
48806934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
48812d5ee99bSBarry Smith   PetscFunctionReturn(0);
48822d5ee99bSBarry Smith }
48832d5ee99bSBarry Smith 
48841713a123SBarry Smith 
48856083293cSBarry Smith /*@C
488683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
48876083293cSBarry Smith 
48886083293cSBarry Smith    Collective on TS
48896083293cSBarry Smith 
48906083293cSBarry Smith    Input Parameters:
48916083293cSBarry Smith .    ctx - the monitor context
48926083293cSBarry Smith 
48936083293cSBarry Smith    Level: intermediate
48946083293cSBarry Smith 
48956083293cSBarry Smith .keywords: TS,  vector, monitor, view
48966083293cSBarry Smith 
489783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
48986083293cSBarry Smith @*/
489983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
49006083293cSBarry Smith {
49016083293cSBarry Smith   PetscErrorCode ierr;
49026083293cSBarry Smith 
49036083293cSBarry Smith   PetscFunctionBegin;
490483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
490583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
490683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
49076083293cSBarry Smith   PetscFunctionReturn(0);
49086083293cSBarry Smith }
49096083293cSBarry Smith 
49106083293cSBarry Smith /*@C
491183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
49126083293cSBarry Smith 
49136083293cSBarry Smith    Collective on TS
49146083293cSBarry Smith 
49156083293cSBarry Smith    Input Parameter:
49166083293cSBarry Smith .    ts - time-step context
49176083293cSBarry Smith 
49186083293cSBarry Smith    Output Patameter:
49196083293cSBarry Smith .    ctx - the monitor context
49206083293cSBarry Smith 
492199fdda47SBarry Smith    Options Database:
492299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
492399fdda47SBarry Smith 
49246083293cSBarry Smith    Level: intermediate
49256083293cSBarry Smith 
49266083293cSBarry Smith .keywords: TS,  vector, monitor, view
49276083293cSBarry Smith 
492883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
49296083293cSBarry Smith @*/
493083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
49316083293cSBarry Smith {
49326083293cSBarry Smith   PetscErrorCode   ierr;
49336083293cSBarry Smith 
49346083293cSBarry Smith   PetscFunctionBegin;
4935b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
493683a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4937e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4938bbd56ea5SKarl Rupp 
493983a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4940473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4941c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4942473a3ab2SBarry Smith 
4943473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4944c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
49456083293cSBarry Smith   PetscFunctionReturn(0);
49466083293cSBarry Smith }
49476083293cSBarry Smith 
49483a471f94SBarry Smith /*@C
494983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
49503a471f94SBarry Smith    VecView() for the error at each timestep
49513a471f94SBarry Smith 
49523a471f94SBarry Smith    Collective on TS
49533a471f94SBarry Smith 
49543a471f94SBarry Smith    Input Parameters:
49553a471f94SBarry Smith +  ts - the TS context
49563a471f94SBarry Smith .  step - current time-step
49573a471f94SBarry Smith .  ptime - current time
49580298fd71SBarry Smith -  dummy - either a viewer or NULL
49593a471f94SBarry Smith 
49603a471f94SBarry Smith    Level: intermediate
49613a471f94SBarry Smith 
49623a471f94SBarry Smith .keywords: TS,  vector, monitor, view
49633a471f94SBarry Smith 
49643a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
49653a471f94SBarry Smith @*/
49660910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49673a471f94SBarry Smith {
49683a471f94SBarry Smith   PetscErrorCode   ierr;
496983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
497083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
49713a471f94SBarry Smith   Vec              work;
49723a471f94SBarry Smith 
49733a471f94SBarry Smith   PetscFunctionBegin;
4974b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
49750910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
49763a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
49770910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
49783a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
49793a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
49803a471f94SBarry Smith   PetscFunctionReturn(0);
49813a471f94SBarry Smith }
49823a471f94SBarry Smith 
4983af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
49846c699258SBarry Smith /*@
49852a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
49866c699258SBarry Smith 
49873f9fe445SBarry Smith    Logically Collective on TS and DM
49886c699258SBarry Smith 
49896c699258SBarry Smith    Input Parameters:
49902a808120SBarry Smith +  ts - the ODE integrator object
49912a808120SBarry Smith -  dm - the dm, cannot be NULL
49926c699258SBarry Smith 
49936c699258SBarry Smith    Level: intermediate
49946c699258SBarry Smith 
49956c699258SBarry Smith 
49966c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
49976c699258SBarry Smith @*/
49987087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
49996c699258SBarry Smith {
50006c699258SBarry Smith   PetscErrorCode ierr;
5001089b2837SJed Brown   SNES           snes;
5002942e3340SBarry Smith   DMTS           tsdm;
50036c699258SBarry Smith 
50046c699258SBarry Smith   PetscFunctionBegin;
50050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50062a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
500770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
5008942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
50092a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
5010942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
5011942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
501224989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
501324989b8cSPeter Brune         tsdm->originaldm = dm;
501424989b8cSPeter Brune       }
501524989b8cSPeter Brune     }
50166bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
501724989b8cSPeter Brune   }
50186c699258SBarry Smith   ts->dm = dm;
5019bbd56ea5SKarl Rupp 
5020089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5021089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
50226c699258SBarry Smith   PetscFunctionReturn(0);
50236c699258SBarry Smith }
50246c699258SBarry Smith 
50256c699258SBarry Smith /*@
50266c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
50276c699258SBarry Smith 
50283f9fe445SBarry Smith    Not Collective
50296c699258SBarry Smith 
50306c699258SBarry Smith    Input Parameter:
50316c699258SBarry Smith . ts - the preconditioner context
50326c699258SBarry Smith 
50336c699258SBarry Smith    Output Parameter:
50346c699258SBarry Smith .  dm - the dm
50356c699258SBarry Smith 
50366c699258SBarry Smith    Level: intermediate
50376c699258SBarry Smith 
50386c699258SBarry Smith 
50396c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
50406c699258SBarry Smith @*/
50417087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
50426c699258SBarry Smith {
5043496e6a7aSJed Brown   PetscErrorCode ierr;
5044496e6a7aSJed Brown 
50456c699258SBarry Smith   PetscFunctionBegin;
50460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5047496e6a7aSJed Brown   if (!ts->dm) {
5048ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
5049496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
5050496e6a7aSJed Brown   }
50516c699258SBarry Smith   *dm = ts->dm;
50526c699258SBarry Smith   PetscFunctionReturn(0);
50536c699258SBarry Smith }
50541713a123SBarry Smith 
50550f5c6efeSJed Brown /*@
50560f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
50570f5c6efeSJed Brown 
50583f9fe445SBarry Smith    Logically Collective on SNES
50590f5c6efeSJed Brown 
50600f5c6efeSJed Brown    Input Parameter:
5061d42a1c89SJed Brown + snes - nonlinear solver
50620910c330SBarry Smith . U - the current state at which to evaluate the residual
5063d42a1c89SJed Brown - ctx - user context, must be a TS
50640f5c6efeSJed Brown 
50650f5c6efeSJed Brown    Output Parameter:
50660f5c6efeSJed Brown . F - the nonlinear residual
50670f5c6efeSJed Brown 
50680f5c6efeSJed Brown    Notes:
50690f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
50700f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
50710f5c6efeSJed Brown 
50720f5c6efeSJed Brown    Level: advanced
50730f5c6efeSJed Brown 
50740f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
50750f5c6efeSJed Brown @*/
50760910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
50770f5c6efeSJed Brown {
50780f5c6efeSJed Brown   TS             ts = (TS)ctx;
50790f5c6efeSJed Brown   PetscErrorCode ierr;
50800f5c6efeSJed Brown 
50810f5c6efeSJed Brown   PetscFunctionBegin;
50820f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
50830910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
50840f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
50850f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
50860910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
50870f5c6efeSJed Brown   PetscFunctionReturn(0);
50880f5c6efeSJed Brown }
50890f5c6efeSJed Brown 
50900f5c6efeSJed Brown /*@
50910f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
50920f5c6efeSJed Brown 
50930f5c6efeSJed Brown    Collective on SNES
50940f5c6efeSJed Brown 
50950f5c6efeSJed Brown    Input Parameter:
50960f5c6efeSJed Brown + snes - nonlinear solver
50970910c330SBarry Smith . U - the current state at which to evaluate the residual
50980f5c6efeSJed Brown - ctx - user context, must be a TS
50990f5c6efeSJed Brown 
51000f5c6efeSJed Brown    Output Parameter:
51010f5c6efeSJed Brown + A - the Jacobian
51020f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
51030f5c6efeSJed Brown - flag - indicates any structure change in the matrix
51040f5c6efeSJed Brown 
51050f5c6efeSJed Brown    Notes:
51060f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
51070f5c6efeSJed Brown 
51080f5c6efeSJed Brown    Level: developer
51090f5c6efeSJed Brown 
51100f5c6efeSJed Brown .seealso: SNESSetJacobian()
51110f5c6efeSJed Brown @*/
5112d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
51130f5c6efeSJed Brown {
51140f5c6efeSJed Brown   TS             ts = (TS)ctx;
51150f5c6efeSJed Brown   PetscErrorCode ierr;
51160f5c6efeSJed Brown 
51170f5c6efeSJed Brown   PetscFunctionBegin;
51180f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
51190910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
51200f5c6efeSJed Brown   PetscValidPointer(A,3);
512194ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
51220f5c6efeSJed Brown   PetscValidPointer(B,4);
512394ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
51240f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5125d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
51260f5c6efeSJed Brown   PetscFunctionReturn(0);
51270f5c6efeSJed Brown }
5128325fc9f4SBarry Smith 
51290e4ef248SJed Brown /*@C
51309ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
51310e4ef248SJed Brown 
51320e4ef248SJed Brown    Collective on TS
51330e4ef248SJed Brown 
51340e4ef248SJed Brown    Input Arguments:
51350e4ef248SJed Brown +  ts - time stepping context
51360e4ef248SJed Brown .  t - time at which to evaluate
51370910c330SBarry Smith .  U - state at which to evaluate
51380e4ef248SJed Brown -  ctx - context
51390e4ef248SJed Brown 
51400e4ef248SJed Brown    Output Arguments:
51410e4ef248SJed Brown .  F - right hand side
51420e4ef248SJed Brown 
51430e4ef248SJed Brown    Level: intermediate
51440e4ef248SJed Brown 
51450e4ef248SJed Brown    Notes:
51460e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
51470e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
51480e4ef248SJed Brown 
51490e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
51500e4ef248SJed Brown @*/
51510910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
51520e4ef248SJed Brown {
51530e4ef248SJed Brown   PetscErrorCode ierr;
51540e4ef248SJed Brown   Mat            Arhs,Brhs;
51550e4ef248SJed Brown 
51560e4ef248SJed Brown   PetscFunctionBegin;
51570e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5158d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
51590910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
51600e4ef248SJed Brown   PetscFunctionReturn(0);
51610e4ef248SJed Brown }
51620e4ef248SJed Brown 
51630e4ef248SJed Brown /*@C
51640e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
51650e4ef248SJed Brown 
51660e4ef248SJed Brown    Collective on TS
51670e4ef248SJed Brown 
51680e4ef248SJed Brown    Input Arguments:
51690e4ef248SJed Brown +  ts - time stepping context
51700e4ef248SJed Brown .  t - time at which to evaluate
51710910c330SBarry Smith .  U - state at which to evaluate
51720e4ef248SJed Brown -  ctx - context
51730e4ef248SJed Brown 
51740e4ef248SJed Brown    Output Arguments:
51750e4ef248SJed Brown +  A - pointer to operator
51760e4ef248SJed Brown .  B - pointer to preconditioning matrix
51770e4ef248SJed Brown -  flg - matrix structure flag
51780e4ef248SJed Brown 
51790e4ef248SJed Brown    Level: intermediate
51800e4ef248SJed Brown 
51810e4ef248SJed Brown    Notes:
51820e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
51830e4ef248SJed Brown 
51840e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
51850e4ef248SJed Brown @*/
5186d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
51870e4ef248SJed Brown {
51880e4ef248SJed Brown   PetscFunctionBegin;
51890e4ef248SJed Brown   PetscFunctionReturn(0);
51900e4ef248SJed Brown }
51910e4ef248SJed Brown 
51920026cea9SSean Farley /*@C
51930026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
51940026cea9SSean Farley 
51950026cea9SSean Farley    Collective on TS
51960026cea9SSean Farley 
51970026cea9SSean Farley    Input Arguments:
51980026cea9SSean Farley +  ts - time stepping context
51990026cea9SSean Farley .  t - time at which to evaluate
52000910c330SBarry Smith .  U - state at which to evaluate
52010910c330SBarry Smith .  Udot - time derivative of state vector
52020026cea9SSean Farley -  ctx - context
52030026cea9SSean Farley 
52040026cea9SSean Farley    Output Arguments:
52050026cea9SSean Farley .  F - left hand side
52060026cea9SSean Farley 
52070026cea9SSean Farley    Level: intermediate
52080026cea9SSean Farley 
52090026cea9SSean Farley    Notes:
52100910c330SBarry 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
52110026cea9SSean Farley    user is required to write their own TSComputeIFunction.
52120026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
52130026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
52140026cea9SSean Farley 
52159ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
52169ae8fd06SBarry Smith 
52179ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
52180026cea9SSean Farley @*/
52190910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
52200026cea9SSean Farley {
52210026cea9SSean Farley   PetscErrorCode ierr;
52220026cea9SSean Farley   Mat            A,B;
52230026cea9SSean Farley 
52240026cea9SSean Farley   PetscFunctionBegin;
52250298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5226d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
52270910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
52280026cea9SSean Farley   PetscFunctionReturn(0);
52290026cea9SSean Farley }
52300026cea9SSean Farley 
52310026cea9SSean Farley /*@C
5232b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
52330026cea9SSean Farley 
52340026cea9SSean Farley    Collective on TS
52350026cea9SSean Farley 
52360026cea9SSean Farley    Input Arguments:
52370026cea9SSean Farley +  ts - time stepping context
52380026cea9SSean Farley .  t - time at which to evaluate
52390910c330SBarry Smith .  U - state at which to evaluate
52400910c330SBarry Smith .  Udot - time derivative of state vector
52410026cea9SSean Farley .  shift - shift to apply
52420026cea9SSean Farley -  ctx - context
52430026cea9SSean Farley 
52440026cea9SSean Farley    Output Arguments:
52450026cea9SSean Farley +  A - pointer to operator
52460026cea9SSean Farley .  B - pointer to preconditioning matrix
52470026cea9SSean Farley -  flg - matrix structure flag
52480026cea9SSean Farley 
5249b41af12eSJed Brown    Level: advanced
52500026cea9SSean Farley 
52510026cea9SSean Farley    Notes:
52520026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
52530026cea9SSean Farley 
5254b41af12eSJed Brown    It is only appropriate for problems of the form
5255b41af12eSJed Brown 
5256b41af12eSJed Brown $     M Udot = F(U,t)
5257b41af12eSJed Brown 
5258b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5259b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5260b41af12eSJed Brown   an implicit operator of the form
5261b41af12eSJed Brown 
5262b41af12eSJed Brown $    shift*M + J
5263b41af12eSJed Brown 
5264b41af12eSJed 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
5265b41af12eSJed Brown   a copy of M or reassemble it when requested.
5266b41af12eSJed Brown 
52670026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
52680026cea9SSean Farley @*/
5269d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
52700026cea9SSean Farley {
5271b41af12eSJed Brown   PetscErrorCode ierr;
5272b41af12eSJed Brown 
52730026cea9SSean Farley   PetscFunctionBegin;
527494ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5275b41af12eSJed Brown   ts->ijacobian.shift = shift;
52760026cea9SSean Farley   PetscFunctionReturn(0);
52770026cea9SSean Farley }
5278b41af12eSJed Brown 
5279e817cc15SEmil Constantinescu /*@
5280e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5281e817cc15SEmil Constantinescu 
5282e817cc15SEmil Constantinescu    Not Collective
5283e817cc15SEmil Constantinescu 
5284e817cc15SEmil Constantinescu    Input Parameter:
5285e817cc15SEmil Constantinescu .  ts - the TS context
5286e817cc15SEmil Constantinescu 
5287e817cc15SEmil Constantinescu    Output Parameter:
52884e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5289e817cc15SEmil Constantinescu 
5290e817cc15SEmil Constantinescu    Level: beginner
5291e817cc15SEmil Constantinescu 
5292e817cc15SEmil Constantinescu .keywords: TS, equation type
5293e817cc15SEmil Constantinescu 
5294e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5295e817cc15SEmil Constantinescu @*/
5296e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5297e817cc15SEmil Constantinescu {
5298e817cc15SEmil Constantinescu   PetscFunctionBegin;
5299e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5300e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5301e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5302e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5303e817cc15SEmil Constantinescu }
5304e817cc15SEmil Constantinescu 
5305e817cc15SEmil Constantinescu /*@
5306e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5307e817cc15SEmil Constantinescu 
5308e817cc15SEmil Constantinescu    Not Collective
5309e817cc15SEmil Constantinescu 
5310e817cc15SEmil Constantinescu    Input Parameter:
5311e817cc15SEmil Constantinescu +  ts - the TS context
53121297b224SEmil Constantinescu -  equation_type - see TSEquationType
5313e817cc15SEmil Constantinescu 
5314e817cc15SEmil Constantinescu    Level: advanced
5315e817cc15SEmil Constantinescu 
5316e817cc15SEmil Constantinescu .keywords: TS, equation type
5317e817cc15SEmil Constantinescu 
5318e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5319e817cc15SEmil Constantinescu @*/
5320e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5321e817cc15SEmil Constantinescu {
5322e817cc15SEmil Constantinescu   PetscFunctionBegin;
5323e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5324e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5325e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5326e817cc15SEmil Constantinescu }
53270026cea9SSean Farley 
53284af1b03aSJed Brown /*@
53294af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
53304af1b03aSJed Brown 
53314af1b03aSJed Brown    Not Collective
53324af1b03aSJed Brown 
53334af1b03aSJed Brown    Input Parameter:
53344af1b03aSJed Brown .  ts - the TS context
53354af1b03aSJed Brown 
53364af1b03aSJed Brown    Output Parameter:
53374af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
53384af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
53394af1b03aSJed Brown 
5340487e0bb9SJed Brown    Level: beginner
53414af1b03aSJed Brown 
5342cd652676SJed Brown    Notes:
5343cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
53444af1b03aSJed Brown 
53454af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
53464af1b03aSJed Brown 
53474af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
53484af1b03aSJed Brown @*/
53494af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
53504af1b03aSJed Brown {
53514af1b03aSJed Brown   PetscFunctionBegin;
53524af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53534af1b03aSJed Brown   PetscValidPointer(reason,2);
53544af1b03aSJed Brown   *reason = ts->reason;
53554af1b03aSJed Brown   PetscFunctionReturn(0);
53564af1b03aSJed Brown }
53574af1b03aSJed Brown 
5358d6ad946cSShri Abhyankar /*@
5359d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5360d6ad946cSShri Abhyankar 
5361d6ad946cSShri Abhyankar    Not Collective
5362d6ad946cSShri Abhyankar 
5363d6ad946cSShri Abhyankar    Input Parameter:
5364d6ad946cSShri Abhyankar +  ts - the TS context
5365d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5366d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5367d6ad946cSShri Abhyankar 
5368f5abba47SShri Abhyankar    Level: advanced
5369d6ad946cSShri Abhyankar 
5370d6ad946cSShri Abhyankar    Notes:
5371d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5372d6ad946cSShri Abhyankar 
5373d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5374d6ad946cSShri Abhyankar 
5375d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5376d6ad946cSShri Abhyankar @*/
5377d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5378d6ad946cSShri Abhyankar {
5379d6ad946cSShri Abhyankar   PetscFunctionBegin;
5380d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5381d6ad946cSShri Abhyankar   ts->reason = reason;
5382d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5383d6ad946cSShri Abhyankar }
5384d6ad946cSShri Abhyankar 
5385cc708dedSBarry Smith /*@
5386cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5387cc708dedSBarry Smith 
5388cc708dedSBarry Smith    Not Collective
5389cc708dedSBarry Smith 
5390cc708dedSBarry Smith    Input Parameter:
5391cc708dedSBarry Smith .  ts - the TS context
5392cc708dedSBarry Smith 
5393cc708dedSBarry Smith    Output Parameter:
539463e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
5395cc708dedSBarry Smith 
5396487e0bb9SJed Brown    Level: beginner
5397cc708dedSBarry Smith 
5398cc708dedSBarry Smith    Notes:
5399cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5400cc708dedSBarry Smith 
5401cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5402cc708dedSBarry Smith 
5403cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5404cc708dedSBarry Smith @*/
5405cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5406cc708dedSBarry Smith {
5407cc708dedSBarry Smith   PetscFunctionBegin;
5408cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5409cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5410cc708dedSBarry Smith   *ftime = ts->solvetime;
5411cc708dedSBarry Smith   PetscFunctionReturn(0);
5412cc708dedSBarry Smith }
5413cc708dedSBarry Smith 
54142c18e0fdSBarry Smith /*@
54152c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
54162c18e0fdSBarry Smith 
54172c18e0fdSBarry Smith    Not Collective
54182c18e0fdSBarry Smith 
54192c18e0fdSBarry Smith    Input Parameter:
54202c18e0fdSBarry Smith .  ts - the TS context
54212c18e0fdSBarry Smith 
54222c18e0fdSBarry Smith    Output Parameter:
54232c18e0fdSBarry Smith .  steps - the number of steps
54242c18e0fdSBarry Smith 
54252c18e0fdSBarry Smith    Level: beginner
54262c18e0fdSBarry Smith 
54272c18e0fdSBarry Smith    Notes:
54282c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
54292c18e0fdSBarry Smith 
54302c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
54312c18e0fdSBarry Smith 
54322c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
54332c18e0fdSBarry Smith @*/
54342c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
54352c18e0fdSBarry Smith {
54362c18e0fdSBarry Smith   PetscFunctionBegin;
54372c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54382c18e0fdSBarry Smith   PetscValidPointer(steps,2);
54392c18e0fdSBarry Smith   *steps = ts->total_steps;
54402c18e0fdSBarry Smith   PetscFunctionReturn(0);
54412c18e0fdSBarry Smith }
54422c18e0fdSBarry Smith 
54439f67acb7SJed Brown /*@
54445ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
54459f67acb7SJed Brown    used by the time integrator.
54469f67acb7SJed Brown 
54479f67acb7SJed Brown    Not Collective
54489f67acb7SJed Brown 
54499f67acb7SJed Brown    Input Parameter:
54509f67acb7SJed Brown .  ts - TS context
54519f67acb7SJed Brown 
54529f67acb7SJed Brown    Output Parameter:
54539f67acb7SJed Brown .  nits - number of nonlinear iterations
54549f67acb7SJed Brown 
54559f67acb7SJed Brown    Notes:
54569f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
54579f67acb7SJed Brown 
54589f67acb7SJed Brown    Level: intermediate
54599f67acb7SJed Brown 
54609f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
54619f67acb7SJed Brown 
54625ef26d82SJed Brown .seealso:  TSGetKSPIterations()
54639f67acb7SJed Brown @*/
54645ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
54659f67acb7SJed Brown {
54669f67acb7SJed Brown   PetscFunctionBegin;
54679f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54689f67acb7SJed Brown   PetscValidIntPointer(nits,2);
54695ef26d82SJed Brown   *nits = ts->snes_its;
54709f67acb7SJed Brown   PetscFunctionReturn(0);
54719f67acb7SJed Brown }
54729f67acb7SJed Brown 
54739f67acb7SJed Brown /*@
54745ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
54759f67acb7SJed Brown    used by the time integrator.
54769f67acb7SJed Brown 
54779f67acb7SJed Brown    Not Collective
54789f67acb7SJed Brown 
54799f67acb7SJed Brown    Input Parameter:
54809f67acb7SJed Brown .  ts - TS context
54819f67acb7SJed Brown 
54829f67acb7SJed Brown    Output Parameter:
54839f67acb7SJed Brown .  lits - number of linear iterations
54849f67acb7SJed Brown 
54859f67acb7SJed Brown    Notes:
54869f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
54879f67acb7SJed Brown 
54889f67acb7SJed Brown    Level: intermediate
54899f67acb7SJed Brown 
54909f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
54919f67acb7SJed Brown 
54925ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
54939f67acb7SJed Brown @*/
54945ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
54959f67acb7SJed Brown {
54969f67acb7SJed Brown   PetscFunctionBegin;
54979f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54989f67acb7SJed Brown   PetscValidIntPointer(lits,2);
54995ef26d82SJed Brown   *lits = ts->ksp_its;
55009f67acb7SJed Brown   PetscFunctionReturn(0);
55019f67acb7SJed Brown }
55029f67acb7SJed Brown 
5503cef5090cSJed Brown /*@
5504cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5505cef5090cSJed Brown 
5506cef5090cSJed Brown    Not Collective
5507cef5090cSJed Brown 
5508cef5090cSJed Brown    Input Parameter:
5509cef5090cSJed Brown .  ts - TS context
5510cef5090cSJed Brown 
5511cef5090cSJed Brown    Output Parameter:
5512cef5090cSJed Brown .  rejects - number of steps rejected
5513cef5090cSJed Brown 
5514cef5090cSJed Brown    Notes:
5515cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5516cef5090cSJed Brown 
5517cef5090cSJed Brown    Level: intermediate
5518cef5090cSJed Brown 
5519cef5090cSJed Brown .keywords: TS, get, number
5520cef5090cSJed Brown 
55215ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5522cef5090cSJed Brown @*/
5523cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5524cef5090cSJed Brown {
5525cef5090cSJed Brown   PetscFunctionBegin;
5526cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5527cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5528cef5090cSJed Brown   *rejects = ts->reject;
5529cef5090cSJed Brown   PetscFunctionReturn(0);
5530cef5090cSJed Brown }
5531cef5090cSJed Brown 
5532cef5090cSJed Brown /*@
5533cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5534cef5090cSJed Brown 
5535cef5090cSJed Brown    Not Collective
5536cef5090cSJed Brown 
5537cef5090cSJed Brown    Input Parameter:
5538cef5090cSJed Brown .  ts - TS context
5539cef5090cSJed Brown 
5540cef5090cSJed Brown    Output Parameter:
5541cef5090cSJed Brown .  fails - number of failed nonlinear solves
5542cef5090cSJed Brown 
5543cef5090cSJed Brown    Notes:
5544cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5545cef5090cSJed Brown 
5546cef5090cSJed Brown    Level: intermediate
5547cef5090cSJed Brown 
5548cef5090cSJed Brown .keywords: TS, get, number
5549cef5090cSJed Brown 
55505ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5551cef5090cSJed Brown @*/
5552cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5553cef5090cSJed Brown {
5554cef5090cSJed Brown   PetscFunctionBegin;
5555cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5556cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5557cef5090cSJed Brown   *fails = ts->num_snes_failures;
5558cef5090cSJed Brown   PetscFunctionReturn(0);
5559cef5090cSJed Brown }
5560cef5090cSJed Brown 
5561cef5090cSJed Brown /*@
5562cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5563cef5090cSJed Brown 
5564cef5090cSJed Brown    Not Collective
5565cef5090cSJed Brown 
5566cef5090cSJed Brown    Input Parameter:
5567cef5090cSJed Brown +  ts - TS context
5568cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5569cef5090cSJed Brown 
5570cef5090cSJed Brown    Notes:
5571cef5090cSJed Brown    The counter is reset to zero for each step
5572cef5090cSJed Brown 
5573cef5090cSJed Brown    Options Database Key:
5574cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5575cef5090cSJed Brown 
5576cef5090cSJed Brown    Level: intermediate
5577cef5090cSJed Brown 
5578cef5090cSJed Brown .keywords: TS, set, maximum, number
5579cef5090cSJed Brown 
55805ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5581cef5090cSJed Brown @*/
5582cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5583cef5090cSJed Brown {
5584cef5090cSJed Brown   PetscFunctionBegin;
5585cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5586cef5090cSJed Brown   ts->max_reject = rejects;
5587cef5090cSJed Brown   PetscFunctionReturn(0);
5588cef5090cSJed Brown }
5589cef5090cSJed Brown 
5590cef5090cSJed Brown /*@
5591cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5592cef5090cSJed Brown 
5593cef5090cSJed Brown    Not Collective
5594cef5090cSJed Brown 
5595cef5090cSJed Brown    Input Parameter:
5596cef5090cSJed Brown +  ts - TS context
5597cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5598cef5090cSJed Brown 
5599cef5090cSJed Brown    Notes:
5600cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5601cef5090cSJed Brown 
5602cef5090cSJed Brown    Options Database Key:
5603cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5604cef5090cSJed Brown 
5605cef5090cSJed Brown    Level: intermediate
5606cef5090cSJed Brown 
5607cef5090cSJed Brown .keywords: TS, set, maximum, number
5608cef5090cSJed Brown 
56095ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5610cef5090cSJed Brown @*/
5611cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5612cef5090cSJed Brown {
5613cef5090cSJed Brown   PetscFunctionBegin;
5614cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5615cef5090cSJed Brown   ts->max_snes_failures = fails;
5616cef5090cSJed Brown   PetscFunctionReturn(0);
5617cef5090cSJed Brown }
5618cef5090cSJed Brown 
5619cef5090cSJed Brown /*@
5620cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5621cef5090cSJed Brown 
5622cef5090cSJed Brown    Not Collective
5623cef5090cSJed Brown 
5624cef5090cSJed Brown    Input Parameter:
5625cef5090cSJed Brown +  ts - TS context
5626cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5627cef5090cSJed Brown 
5628cef5090cSJed Brown    Options Database Key:
5629cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5630cef5090cSJed Brown 
5631cef5090cSJed Brown    Level: intermediate
5632cef5090cSJed Brown 
5633cef5090cSJed Brown .keywords: TS, set, error
5634cef5090cSJed Brown 
56355ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5636cef5090cSJed Brown @*/
5637cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5638cef5090cSJed Brown {
5639cef5090cSJed Brown   PetscFunctionBegin;
5640cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5641cef5090cSJed Brown   ts->errorifstepfailed = err;
5642cef5090cSJed Brown   PetscFunctionReturn(0);
5643cef5090cSJed Brown }
5644cef5090cSJed Brown 
5645fb1732b5SBarry Smith /*@C
5646fde5950dSBarry 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
5647fb1732b5SBarry Smith 
5648fb1732b5SBarry Smith    Collective on TS
5649fb1732b5SBarry Smith 
5650fb1732b5SBarry Smith    Input Parameters:
5651fb1732b5SBarry Smith +  ts - the TS context
5652fb1732b5SBarry Smith .  step - current time-step
5653fb1732b5SBarry Smith .  ptime - current time
56540910c330SBarry Smith .  u - current state
5655721cd6eeSBarry Smith -  vf - viewer and its format
5656fb1732b5SBarry Smith 
5657fb1732b5SBarry Smith    Level: intermediate
5658fb1732b5SBarry Smith 
5659fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5660fb1732b5SBarry Smith 
5661fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5662fb1732b5SBarry Smith @*/
5663721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5664fb1732b5SBarry Smith {
5665fb1732b5SBarry Smith   PetscErrorCode ierr;
5666fb1732b5SBarry Smith 
5667fb1732b5SBarry Smith   PetscFunctionBegin;
5668721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5669721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5670721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5671ed81e22dSJed Brown   PetscFunctionReturn(0);
5672ed81e22dSJed Brown }
5673ed81e22dSJed Brown 
5674ed81e22dSJed Brown /*@C
5675ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5676ed81e22dSJed Brown 
5677ed81e22dSJed Brown    Collective on TS
5678ed81e22dSJed Brown 
5679ed81e22dSJed Brown    Input Parameters:
5680ed81e22dSJed Brown +  ts - the TS context
5681ed81e22dSJed Brown .  step - current time-step
5682ed81e22dSJed Brown .  ptime - current time
56830910c330SBarry Smith .  u - current state
5684ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5685ed81e22dSJed Brown 
5686ed81e22dSJed Brown    Level: intermediate
5687ed81e22dSJed Brown 
5688ed81e22dSJed Brown    Notes:
5689ed81e22dSJed 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.
5690ed81e22dSJed Brown    These are named according to the file name template.
5691ed81e22dSJed Brown 
5692ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5693ed81e22dSJed Brown 
5694ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5695ed81e22dSJed Brown 
5696ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5697ed81e22dSJed Brown @*/
56980910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5699ed81e22dSJed Brown {
5700ed81e22dSJed Brown   PetscErrorCode ierr;
5701ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5702ed81e22dSJed Brown   PetscViewer    viewer;
5703ed81e22dSJed Brown 
5704ed81e22dSJed Brown   PetscFunctionBegin;
570563e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
57068caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5707ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
57080910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5709ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5710ed81e22dSJed Brown   PetscFunctionReturn(0);
5711ed81e22dSJed Brown }
5712ed81e22dSJed Brown 
5713ed81e22dSJed Brown /*@C
5714ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5715ed81e22dSJed Brown 
5716ed81e22dSJed Brown    Collective on TS
5717ed81e22dSJed Brown 
5718ed81e22dSJed Brown    Input Parameters:
5719ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5720ed81e22dSJed Brown 
5721ed81e22dSJed Brown    Level: intermediate
5722ed81e22dSJed Brown 
5723ed81e22dSJed Brown    Note:
5724ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5725ed81e22dSJed Brown 
5726ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5727ed81e22dSJed Brown 
5728ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5729ed81e22dSJed Brown @*/
5730ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5731ed81e22dSJed Brown {
5732ed81e22dSJed Brown   PetscErrorCode ierr;
5733ed81e22dSJed Brown 
5734ed81e22dSJed Brown   PetscFunctionBegin;
5735ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5736fb1732b5SBarry Smith   PetscFunctionReturn(0);
5737fb1732b5SBarry Smith }
5738fb1732b5SBarry Smith 
573984df9cb4SJed Brown /*@
5740552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
574184df9cb4SJed Brown 
5742ed81e22dSJed Brown    Collective on TS if controller has not been created yet
574384df9cb4SJed Brown 
574484df9cb4SJed Brown    Input Arguments:
5745ed81e22dSJed Brown .  ts - time stepping context
574684df9cb4SJed Brown 
574784df9cb4SJed Brown    Output Arguments:
5748ed81e22dSJed Brown .  adapt - adaptive controller
574984df9cb4SJed Brown 
575084df9cb4SJed Brown    Level: intermediate
575184df9cb4SJed Brown 
5752ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
575384df9cb4SJed Brown @*/
5754552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
575584df9cb4SJed Brown {
575684df9cb4SJed Brown   PetscErrorCode ierr;
575784df9cb4SJed Brown 
575884df9cb4SJed Brown   PetscFunctionBegin;
575984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5760bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
576184df9cb4SJed Brown   if (!ts->adapt) {
5762ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
57633bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
57641c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
576584df9cb4SJed Brown   }
5766bec58848SLisandro Dalcin   *adapt = ts->adapt;
576784df9cb4SJed Brown   PetscFunctionReturn(0);
576884df9cb4SJed Brown }
5769d6ebe24aSShri Abhyankar 
57701c3436cfSJed Brown /*@
57711c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
57721c3436cfSJed Brown 
57731c3436cfSJed Brown    Logically Collective
57741c3436cfSJed Brown 
57751c3436cfSJed Brown    Input Arguments:
57761c3436cfSJed Brown +  ts - time integration context
57771c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
57780298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
57791c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
57800298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
57811c3436cfSJed Brown 
5782a3cdaa26SBarry Smith    Options Database keys:
5783a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5784a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5785a3cdaa26SBarry Smith 
57863ff766beSShri Abhyankar    Notes:
57873ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
57883ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
57893ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
57903ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
57913ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
57923ff766beSShri Abhyankar    differential variables.
57933ff766beSShri Abhyankar 
57941c3436cfSJed Brown    Level: beginner
57951c3436cfSJed Brown 
5796c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
57971c3436cfSJed Brown @*/
57981c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
57991c3436cfSJed Brown {
58001c3436cfSJed Brown   PetscErrorCode ierr;
58011c3436cfSJed Brown 
58021c3436cfSJed Brown   PetscFunctionBegin;
5803c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
58041c3436cfSJed Brown   if (vatol) {
58051c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
58061c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
58071c3436cfSJed Brown     ts->vatol = vatol;
58081c3436cfSJed Brown   }
5809c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
58101c3436cfSJed Brown   if (vrtol) {
58111c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
58121c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
58131c3436cfSJed Brown     ts->vrtol = vrtol;
58141c3436cfSJed Brown   }
58151c3436cfSJed Brown   PetscFunctionReturn(0);
58161c3436cfSJed Brown }
58171c3436cfSJed Brown 
5818c5033834SJed Brown /*@
5819c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5820c5033834SJed Brown 
5821c5033834SJed Brown    Logically Collective
5822c5033834SJed Brown 
5823c5033834SJed Brown    Input Arguments:
5824c5033834SJed Brown .  ts - time integration context
5825c5033834SJed Brown 
5826c5033834SJed Brown    Output Arguments:
58270298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
58280298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
58290298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
58300298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5831c5033834SJed Brown 
5832c5033834SJed Brown    Level: beginner
5833c5033834SJed Brown 
5834c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5835c5033834SJed Brown @*/
5836c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5837c5033834SJed Brown {
5838c5033834SJed Brown   PetscFunctionBegin;
5839c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5840c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5841c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5842c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5843c5033834SJed Brown   PetscFunctionReturn(0);
5844c5033834SJed Brown }
5845c5033834SJed Brown 
58469c6b16b5SShri Abhyankar /*@
5847a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
58489c6b16b5SShri Abhyankar 
58499c6b16b5SShri Abhyankar    Collective on TS
58509c6b16b5SShri Abhyankar 
58519c6b16b5SShri Abhyankar    Input Arguments:
58529c6b16b5SShri Abhyankar +  ts - time stepping context
5853a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5854a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
58559c6b16b5SShri Abhyankar 
58569c6b16b5SShri Abhyankar    Output Arguments:
58577453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58587453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58597453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58609c6b16b5SShri Abhyankar 
58619c6b16b5SShri Abhyankar    Level: developer
58629c6b16b5SShri Abhyankar 
5863deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
58649c6b16b5SShri Abhyankar @*/
58657453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58669c6b16b5SShri Abhyankar {
58679c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
58689c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
58697453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
58707453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
58719c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
58727453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
58737453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
58747453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
58759c6b16b5SShri Abhyankar 
58769c6b16b5SShri Abhyankar   PetscFunctionBegin;
58779c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5878a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5879a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5880a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5881a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5882a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5883a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
58848a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
58858a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5886a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
58879c6b16b5SShri Abhyankar 
58889c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
58899c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
58909c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
58919c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58929c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58937453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
58947453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
58957453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
58969c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
58979c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
58989c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58999c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59009c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
59017453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59027453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59037453f775SEmil Constantinescu       if(tola>0.){
59047453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59057453f775SEmil Constantinescu         na_loc++;
59067453f775SEmil Constantinescu       }
59077453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59087453f775SEmil Constantinescu       if(tolr>0.){
59097453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59107453f775SEmil Constantinescu         nr_loc++;
59117453f775SEmil Constantinescu       }
59127453f775SEmil Constantinescu       tol=tola+tolr;
59137453f775SEmil Constantinescu       if(tol>0.){
59147453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59157453f775SEmil Constantinescu         n_loc++;
59167453f775SEmil Constantinescu       }
59179c6b16b5SShri Abhyankar     }
59189c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59199c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59209c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59219c6b16b5SShri Abhyankar     const PetscScalar *atol;
59229c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59239c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
59247453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59257453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
59267453f775SEmil Constantinescu       if(tola>0.){
59277453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59287453f775SEmil Constantinescu         na_loc++;
59297453f775SEmil Constantinescu       }
59307453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59317453f775SEmil Constantinescu       if(tolr>0.){
59327453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59337453f775SEmil Constantinescu         nr_loc++;
59347453f775SEmil Constantinescu       }
59357453f775SEmil Constantinescu       tol=tola+tolr;
59367453f775SEmil Constantinescu       if(tol>0.){
59377453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59387453f775SEmil Constantinescu         n_loc++;
59397453f775SEmil Constantinescu       }
59409c6b16b5SShri Abhyankar     }
59419c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59429c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59439c6b16b5SShri Abhyankar     const PetscScalar *rtol;
59449c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59459c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
59467453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59477453f775SEmil Constantinescu       tola = ts->atol;
59487453f775SEmil Constantinescu       if(tola>0.){
59497453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59507453f775SEmil Constantinescu         na_loc++;
59517453f775SEmil Constantinescu       }
59527453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59537453f775SEmil Constantinescu       if(tolr>0.){
59547453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59557453f775SEmil Constantinescu         nr_loc++;
59567453f775SEmil Constantinescu       }
59577453f775SEmil Constantinescu       tol=tola+tolr;
59587453f775SEmil Constantinescu       if(tol>0.){
59597453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59607453f775SEmil Constantinescu         n_loc++;
59617453f775SEmil Constantinescu       }
59629c6b16b5SShri Abhyankar     }
59639c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59649c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
59659c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
59667453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
59677453f775SEmil Constantinescu      tola = ts->atol;
59687453f775SEmil Constantinescu       if(tola>0.){
59697453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
59707453f775SEmil Constantinescu         na_loc++;
59717453f775SEmil Constantinescu       }
59727453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59737453f775SEmil Constantinescu       if(tolr>0.){
59747453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
59757453f775SEmil Constantinescu         nr_loc++;
59767453f775SEmil Constantinescu       }
59777453f775SEmil Constantinescu       tol=tola+tolr;
59787453f775SEmil Constantinescu       if(tol>0.){
59797453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
59807453f775SEmil Constantinescu         n_loc++;
59817453f775SEmil Constantinescu       }
59829c6b16b5SShri Abhyankar     }
59839c6b16b5SShri Abhyankar   }
59849c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59859c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59869c6b16b5SShri Abhyankar 
59877453f775SEmil Constantinescu   err_loc[0] = sum;
59887453f775SEmil Constantinescu   err_loc[1] = suma;
59897453f775SEmil Constantinescu   err_loc[2] = sumr;
59907453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59917453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59927453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59937453f775SEmil Constantinescu 
5994a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59957453f775SEmil Constantinescu 
59967453f775SEmil Constantinescu   gsum   = err_glb[0];
59977453f775SEmil Constantinescu   gsuma  = err_glb[1];
59987453f775SEmil Constantinescu   gsumr  = err_glb[2];
59997453f775SEmil Constantinescu   n_glb  = err_glb[3];
60007453f775SEmil Constantinescu   na_glb = err_glb[4];
60017453f775SEmil Constantinescu   nr_glb = err_glb[5];
60027453f775SEmil Constantinescu 
6003b1316ef9SEmil Constantinescu   *norm  = 0.;
6004b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
6005b1316ef9SEmil Constantinescu   *norma = 0.;
6006b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
6007b1316ef9SEmil Constantinescu   *normr = 0.;
6008b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
60099c6b16b5SShri Abhyankar 
60109c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60117453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60127453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60139c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60149c6b16b5SShri Abhyankar }
60159c6b16b5SShri Abhyankar 
60169c6b16b5SShri Abhyankar /*@
6017a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
60189c6b16b5SShri Abhyankar 
60199c6b16b5SShri Abhyankar    Collective on TS
60209c6b16b5SShri Abhyankar 
60219c6b16b5SShri Abhyankar    Input Arguments:
60229c6b16b5SShri Abhyankar +  ts - time stepping context
6023a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6024a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
60259c6b16b5SShri Abhyankar 
60269c6b16b5SShri Abhyankar    Output Arguments:
60277453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60287453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
60297453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60309c6b16b5SShri Abhyankar 
60319c6b16b5SShri Abhyankar    Level: developer
60329c6b16b5SShri Abhyankar 
6033deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
60349c6b16b5SShri Abhyankar @*/
60357453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60369c6b16b5SShri Abhyankar {
60379c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
60387453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
60399c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
60407453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
60417453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
60427453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
60439c6b16b5SShri Abhyankar 
60449c6b16b5SShri Abhyankar   PetscFunctionBegin;
60459c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6046a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6047a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6048a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6049a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6050a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6051a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
60528a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
60538a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
6054a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
60559c6b16b5SShri Abhyankar 
60569c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
60579c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
60589c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
60599c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60609c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60617453f775SEmil Constantinescu 
60627453f775SEmil Constantinescu   max=0.;
60637453f775SEmil Constantinescu   maxa=0.;
60647453f775SEmil Constantinescu   maxr=0.;
60657453f775SEmil Constantinescu 
60667453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
60679c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
60689c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60699c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60707453f775SEmil Constantinescu 
60717453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60727453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60737453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60747453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60757453f775SEmil Constantinescu       tol  = tola+tolr;
60767453f775SEmil Constantinescu       if(tola>0.){
60777453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60787453f775SEmil Constantinescu       }
60797453f775SEmil Constantinescu       if(tolr>0.){
60807453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
60817453f775SEmil Constantinescu       }
60827453f775SEmil Constantinescu       if(tol>0.){
60837453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
60847453f775SEmil Constantinescu       }
60859c6b16b5SShri Abhyankar     }
60869c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60879c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60889c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60899c6b16b5SShri Abhyankar     const PetscScalar *atol;
60909c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60917453f775SEmil Constantinescu     for (i=0; i<n; i++) {
60927453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
60937453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
60947453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60957453f775SEmil Constantinescu       tol  = tola+tolr;
60967453f775SEmil Constantinescu       if(tola>0.){
60977453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
60987453f775SEmil Constantinescu       }
60997453f775SEmil Constantinescu       if(tolr>0.){
61007453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
61017453f775SEmil Constantinescu       }
61027453f775SEmil Constantinescu       if(tol>0.){
61037453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
61047453f775SEmil Constantinescu       }
61059c6b16b5SShri Abhyankar     }
61069c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61079c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61089c6b16b5SShri Abhyankar     const PetscScalar *rtol;
61099c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61107453f775SEmil Constantinescu 
61117453f775SEmil Constantinescu     for (i=0; i<n; i++) {
61127453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61137453f775SEmil Constantinescu       tola = ts->atol;
61147453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61157453f775SEmil Constantinescu       tol  = tola+tolr;
61167453f775SEmil Constantinescu       if(tola>0.){
61177453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
61187453f775SEmil Constantinescu       }
61197453f775SEmil Constantinescu       if(tolr>0.){
61207453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
61217453f775SEmil Constantinescu       }
61227453f775SEmil Constantinescu       if(tol>0.){
61237453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
61247453f775SEmil Constantinescu       }
61259c6b16b5SShri Abhyankar     }
61269c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61279c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
61287453f775SEmil Constantinescu 
61297453f775SEmil Constantinescu     for (i=0; i<n; i++) {
61307453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
61317453f775SEmil Constantinescu       tola = ts->atol;
61327453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61337453f775SEmil Constantinescu       tol  = tola+tolr;
61347453f775SEmil Constantinescu       if(tola>0.){
61357453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
61367453f775SEmil Constantinescu       }
61377453f775SEmil Constantinescu       if(tolr>0.){
61387453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
61397453f775SEmil Constantinescu       }
61407453f775SEmil Constantinescu       if(tol>0.){
61417453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
61427453f775SEmil Constantinescu       }
61439c6b16b5SShri Abhyankar     }
61449c6b16b5SShri Abhyankar   }
61459c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61469c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61477453f775SEmil Constantinescu   err_loc[0] = max;
61487453f775SEmil Constantinescu   err_loc[1] = maxa;
61497453f775SEmil Constantinescu   err_loc[2] = maxr;
6150a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61517453f775SEmil Constantinescu   gmax   = err_glb[0];
61527453f775SEmil Constantinescu   gmaxa  = err_glb[1];
61537453f775SEmil Constantinescu   gmaxr  = err_glb[2];
61549c6b16b5SShri Abhyankar 
61559c6b16b5SShri Abhyankar   *norm = gmax;
61567453f775SEmil Constantinescu   *norma = gmaxa;
61577453f775SEmil Constantinescu   *normr = gmaxr;
61589c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61597453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61607453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61619c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
61629c6b16b5SShri Abhyankar }
61639c6b16b5SShri Abhyankar 
61641c3436cfSJed Brown /*@
61658a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
61661c3436cfSJed Brown 
61671c3436cfSJed Brown    Collective on TS
61681c3436cfSJed Brown 
61691c3436cfSJed Brown    Input Arguments:
61701c3436cfSJed Brown +  ts - time stepping context
6171a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6172a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6173a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61747619abb3SShri 
61751c3436cfSJed Brown    Output Arguments:
61768a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
61778a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
61788a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
6179a4868fbcSLisandro Dalcin 
6180a4868fbcSLisandro Dalcin    Options Database Keys:
6181a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6182a4868fbcSLisandro Dalcin 
61831c3436cfSJed Brown    Level: developer
61841c3436cfSJed Brown 
61858a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
61861c3436cfSJed Brown @*/
61877453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61881c3436cfSJed Brown {
61898beabaa1SBarry Smith   PetscErrorCode ierr;
61901c3436cfSJed Brown 
61911c3436cfSJed Brown   PetscFunctionBegin;
6192a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
61937453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6194a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
61957453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
6196a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61971c3436cfSJed Brown   PetscFunctionReturn(0);
61981c3436cfSJed Brown }
61991c3436cfSJed Brown 
62008a175baeSEmil Constantinescu 
62018a175baeSEmil Constantinescu /*@
62028a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
62038a175baeSEmil Constantinescu 
62048a175baeSEmil Constantinescu    Collective on TS
62058a175baeSEmil Constantinescu 
62068a175baeSEmil Constantinescu    Input Arguments:
62078a175baeSEmil Constantinescu +  ts - time stepping context
62088a175baeSEmil Constantinescu .  E - error vector
62098a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
62108a175baeSEmil Constantinescu -  Y - state vector, previous time step
62118a175baeSEmil Constantinescu 
62128a175baeSEmil Constantinescu    Output Arguments:
62138a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
62148a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
62158a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
62168a175baeSEmil Constantinescu 
62178a175baeSEmil Constantinescu    Level: developer
62188a175baeSEmil Constantinescu 
62198a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
62208a175baeSEmil Constantinescu @*/
62218a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62228a175baeSEmil Constantinescu {
62238a175baeSEmil Constantinescu   PetscErrorCode    ierr;
62248a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
62258a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
62268a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
62278a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
62288a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
62298a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
62308a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
62318a175baeSEmil Constantinescu 
62328a175baeSEmil Constantinescu   PetscFunctionBegin;
62338a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62348a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
62358a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
62368a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
62378a175baeSEmil Constantinescu   PetscValidType(E,2);
62388a175baeSEmil Constantinescu   PetscValidType(U,3);
62398a175baeSEmil Constantinescu   PetscValidType(Y,4);
62408a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
62418a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
62428a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
62438a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
62448a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
62458a175baeSEmil Constantinescu 
62468a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
62478a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
62488a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
62498a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
62508a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
62518a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
62528a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
62538a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
62548a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
62558a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
62568a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
62578a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62588a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62598a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62608a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62618a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62628a175baeSEmil Constantinescu       if(tola>0.){
62638a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62648a175baeSEmil Constantinescu         na_loc++;
62658a175baeSEmil Constantinescu       }
62668a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62678a175baeSEmil Constantinescu       if(tolr>0.){
62688a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62698a175baeSEmil Constantinescu         nr_loc++;
62708a175baeSEmil Constantinescu       }
62718a175baeSEmil Constantinescu       tol=tola+tolr;
62728a175baeSEmil Constantinescu       if(tol>0.){
62738a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62748a175baeSEmil Constantinescu         n_loc++;
62758a175baeSEmil Constantinescu       }
62768a175baeSEmil Constantinescu     }
62778a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62788a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
62798a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
62808a175baeSEmil Constantinescu     const PetscScalar *atol;
62818a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
62828a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
62838a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
62848a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
62858a175baeSEmil Constantinescu       if(tola>0.){
62868a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
62878a175baeSEmil Constantinescu         na_loc++;
62888a175baeSEmil Constantinescu       }
62898a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
62908a175baeSEmil Constantinescu       if(tolr>0.){
62918a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
62928a175baeSEmil Constantinescu         nr_loc++;
62938a175baeSEmil Constantinescu       }
62948a175baeSEmil Constantinescu       tol=tola+tolr;
62958a175baeSEmil Constantinescu       if(tol>0.){
62968a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
62978a175baeSEmil Constantinescu         n_loc++;
62988a175baeSEmil Constantinescu       }
62998a175baeSEmil Constantinescu     }
63008a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
63018a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
63028a175baeSEmil Constantinescu     const PetscScalar *rtol;
63038a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63048a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63058a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63068a175baeSEmil Constantinescu       tola = ts->atol;
63078a175baeSEmil Constantinescu       if(tola>0.){
63088a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
63098a175baeSEmil Constantinescu         na_loc++;
63108a175baeSEmil Constantinescu       }
63118a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63128a175baeSEmil Constantinescu       if(tolr>0.){
63138a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
63148a175baeSEmil Constantinescu         nr_loc++;
63158a175baeSEmil Constantinescu       }
63168a175baeSEmil Constantinescu       tol=tola+tolr;
63178a175baeSEmil Constantinescu       if(tol>0.){
63188a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
63198a175baeSEmil Constantinescu         n_loc++;
63208a175baeSEmil Constantinescu       }
63218a175baeSEmil Constantinescu     }
63228a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
63238a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
63248a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
63258a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
63268a175baeSEmil Constantinescu      tola = ts->atol;
63278a175baeSEmil Constantinescu       if(tola>0.){
63288a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
63298a175baeSEmil Constantinescu         na_loc++;
63308a175baeSEmil Constantinescu       }
63318a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
63328a175baeSEmil Constantinescu       if(tolr>0.){
63338a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
63348a175baeSEmil Constantinescu         nr_loc++;
63358a175baeSEmil Constantinescu       }
63368a175baeSEmil Constantinescu       tol=tola+tolr;
63378a175baeSEmil Constantinescu       if(tol>0.){
63388a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
63398a175baeSEmil Constantinescu         n_loc++;
63408a175baeSEmil Constantinescu       }
63418a175baeSEmil Constantinescu     }
63428a175baeSEmil Constantinescu   }
63438a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
63448a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
63458a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
63468a175baeSEmil Constantinescu 
63478a175baeSEmil Constantinescu   err_loc[0] = sum;
63488a175baeSEmil Constantinescu   err_loc[1] = suma;
63498a175baeSEmil Constantinescu   err_loc[2] = sumr;
63508a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
63518a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
63528a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
63538a175baeSEmil Constantinescu 
6354a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
63558a175baeSEmil Constantinescu 
63568a175baeSEmil Constantinescu   gsum   = err_glb[0];
63578a175baeSEmil Constantinescu   gsuma  = err_glb[1];
63588a175baeSEmil Constantinescu   gsumr  = err_glb[2];
63598a175baeSEmil Constantinescu   n_glb  = err_glb[3];
63608a175baeSEmil Constantinescu   na_glb = err_glb[4];
63618a175baeSEmil Constantinescu   nr_glb = err_glb[5];
63628a175baeSEmil Constantinescu 
63638a175baeSEmil Constantinescu   *norm  = 0.;
63648a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
63658a175baeSEmil Constantinescu   *norma = 0.;
63668a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
63678a175baeSEmil Constantinescu   *normr = 0.;
63688a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
63698a175baeSEmil Constantinescu 
63708a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
63718a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
63728a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
63738a175baeSEmil Constantinescu   PetscFunctionReturn(0);
63748a175baeSEmil Constantinescu }
63758a175baeSEmil Constantinescu 
63768a175baeSEmil Constantinescu /*@
63778a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
63788a175baeSEmil Constantinescu    Collective on TS
63798a175baeSEmil Constantinescu 
63808a175baeSEmil Constantinescu    Input Arguments:
63818a175baeSEmil Constantinescu +  ts - time stepping context
63828a175baeSEmil Constantinescu .  E - error vector
63838a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
63848a175baeSEmil Constantinescu -  Y - state vector, previous time step
63858a175baeSEmil Constantinescu 
63868a175baeSEmil Constantinescu    Output Arguments:
63878a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
63888a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
63898a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
63908a175baeSEmil Constantinescu 
63918a175baeSEmil Constantinescu    Level: developer
63928a175baeSEmil Constantinescu 
63938a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
63948a175baeSEmil Constantinescu @*/
63958a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
63968a175baeSEmil Constantinescu {
63978a175baeSEmil Constantinescu   PetscErrorCode    ierr;
63988a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
63998a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
64008a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
64018a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
64028a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
64038a175baeSEmil Constantinescu 
64048a175baeSEmil Constantinescu   PetscFunctionBegin;
64058a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64068a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
64078a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
64088a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
64098a175baeSEmil Constantinescu   PetscValidType(E,2);
64108a175baeSEmil Constantinescu   PetscValidType(U,3);
64118a175baeSEmil Constantinescu   PetscValidType(Y,4);
64128a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
64138a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
64148a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
64158a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
64168a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
64178a175baeSEmil Constantinescu 
64188a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
64198a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
64208a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
64218a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
64228a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
64238a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
64248a175baeSEmil Constantinescu 
64258a175baeSEmil Constantinescu   max=0.;
64268a175baeSEmil Constantinescu   maxa=0.;
64278a175baeSEmil Constantinescu   maxr=0.;
64288a175baeSEmil Constantinescu 
64298a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
64308a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
64318a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64328a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64338a175baeSEmil Constantinescu 
64348a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64358a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64368a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64378a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64388a175baeSEmil Constantinescu       tol  = tola+tolr;
64398a175baeSEmil Constantinescu       if(tola>0.){
64408a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64418a175baeSEmil Constantinescu       }
64428a175baeSEmil Constantinescu       if(tolr>0.){
64438a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64448a175baeSEmil Constantinescu       }
64458a175baeSEmil Constantinescu       if(tol>0.){
64468a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64478a175baeSEmil Constantinescu       }
64488a175baeSEmil Constantinescu     }
64498a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64508a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64518a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
64528a175baeSEmil Constantinescu     const PetscScalar *atol;
64538a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64548a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64558a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64568a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
64578a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64588a175baeSEmil Constantinescu       tol  = tola+tolr;
64598a175baeSEmil Constantinescu       if(tola>0.){
64608a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64618a175baeSEmil Constantinescu       }
64628a175baeSEmil Constantinescu       if(tolr>0.){
64638a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64648a175baeSEmil Constantinescu       }
64658a175baeSEmil Constantinescu       if(tol>0.){
64668a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64678a175baeSEmil Constantinescu       }
64688a175baeSEmil Constantinescu     }
64698a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
64708a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
64718a175baeSEmil Constantinescu     const PetscScalar *rtol;
64728a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64738a175baeSEmil Constantinescu 
64748a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64758a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64768a175baeSEmil Constantinescu       tola = ts->atol;
64778a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64788a175baeSEmil Constantinescu       tol  = tola+tolr;
64798a175baeSEmil Constantinescu       if(tola>0.){
64808a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64818a175baeSEmil Constantinescu       }
64828a175baeSEmil Constantinescu       if(tolr>0.){
64838a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
64848a175baeSEmil Constantinescu       }
64858a175baeSEmil Constantinescu       if(tol>0.){
64868a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
64878a175baeSEmil Constantinescu       }
64888a175baeSEmil Constantinescu     }
64898a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
64908a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
64918a175baeSEmil Constantinescu 
64928a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
64938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
64948a175baeSEmil Constantinescu       tola = ts->atol;
64958a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
64968a175baeSEmil Constantinescu       tol  = tola+tolr;
64978a175baeSEmil Constantinescu       if(tola>0.){
64988a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
64998a175baeSEmil Constantinescu       }
65008a175baeSEmil Constantinescu       if(tolr>0.){
65018a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
65028a175baeSEmil Constantinescu       }
65038a175baeSEmil Constantinescu       if(tol>0.){
65048a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
65058a175baeSEmil Constantinescu       }
65068a175baeSEmil Constantinescu     }
65078a175baeSEmil Constantinescu   }
65088a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
65098a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
65108a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
65118a175baeSEmil Constantinescu   err_loc[0] = max;
65128a175baeSEmil Constantinescu   err_loc[1] = maxa;
65138a175baeSEmil Constantinescu   err_loc[2] = maxr;
6514a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
65158a175baeSEmil Constantinescu   gmax   = err_glb[0];
65168a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
65178a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
65188a175baeSEmil Constantinescu 
65198a175baeSEmil Constantinescu   *norm = gmax;
65208a175baeSEmil Constantinescu   *norma = gmaxa;
65218a175baeSEmil Constantinescu   *normr = gmaxr;
65228a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
65238a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
65248a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
65258a175baeSEmil Constantinescu   PetscFunctionReturn(0);
65268a175baeSEmil Constantinescu }
65278a175baeSEmil Constantinescu 
65288a175baeSEmil Constantinescu /*@
65298a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
65308a175baeSEmil Constantinescu 
65318a175baeSEmil Constantinescu    Collective on TS
65328a175baeSEmil Constantinescu 
65338a175baeSEmil Constantinescu    Input Arguments:
65348a175baeSEmil Constantinescu +  ts - time stepping context
65358a175baeSEmil Constantinescu .  E - error vector
65368a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
65378a175baeSEmil Constantinescu .  Y - state vector, previous time step
65388a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
65398a175baeSEmil Constantinescu 
65408a175baeSEmil Constantinescu    Output Arguments:
65418a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
65428a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
65438a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
65448a175baeSEmil Constantinescu 
65458a175baeSEmil Constantinescu    Options Database Keys:
65468a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
65478a175baeSEmil Constantinescu 
65488a175baeSEmil Constantinescu    Level: developer
65498a175baeSEmil Constantinescu 
65508a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
65518a175baeSEmil Constantinescu @*/
65528a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
65538a175baeSEmil Constantinescu {
65548a175baeSEmil Constantinescu   PetscErrorCode ierr;
65558a175baeSEmil Constantinescu 
65568a175baeSEmil Constantinescu   PetscFunctionBegin;
65578a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
65588a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
65598a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
65608a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
65618a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
65628a175baeSEmil Constantinescu   PetscFunctionReturn(0);
65638a175baeSEmil Constantinescu }
65648a175baeSEmil Constantinescu 
65658a175baeSEmil Constantinescu 
65668d59e960SJed Brown /*@
65678d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
65688d59e960SJed Brown 
65698d59e960SJed Brown    Logically Collective on TS
65708d59e960SJed Brown 
65718d59e960SJed Brown    Input Arguments:
65728d59e960SJed Brown +  ts - time stepping context
65738d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
65748d59e960SJed Brown 
65758d59e960SJed Brown    Note:
65768d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
65778d59e960SJed Brown 
65788d59e960SJed Brown    Level: intermediate
65798d59e960SJed Brown 
65808d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
65818d59e960SJed Brown @*/
65828d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
65838d59e960SJed Brown {
65848d59e960SJed Brown   PetscFunctionBegin;
65858d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65868d59e960SJed Brown   ts->cfltime_local = cfltime;
65878d59e960SJed Brown   ts->cfltime       = -1.;
65888d59e960SJed Brown   PetscFunctionReturn(0);
65898d59e960SJed Brown }
65908d59e960SJed Brown 
65918d59e960SJed Brown /*@
65928d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
65938d59e960SJed Brown 
65948d59e960SJed Brown    Collective on TS
65958d59e960SJed Brown 
65968d59e960SJed Brown    Input Arguments:
65978d59e960SJed Brown .  ts - time stepping context
65988d59e960SJed Brown 
65998d59e960SJed Brown    Output Arguments:
66008d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
66018d59e960SJed Brown 
66028d59e960SJed Brown    Level: advanced
66038d59e960SJed Brown 
66048d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
66058d59e960SJed Brown @*/
66068d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
66078d59e960SJed Brown {
66088d59e960SJed Brown   PetscErrorCode ierr;
66098d59e960SJed Brown 
66108d59e960SJed Brown   PetscFunctionBegin;
66118d59e960SJed Brown   if (ts->cfltime < 0) {
6612b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
66138d59e960SJed Brown   }
66148d59e960SJed Brown   *cfltime = ts->cfltime;
66158d59e960SJed Brown   PetscFunctionReturn(0);
66168d59e960SJed Brown }
66178d59e960SJed Brown 
6618d6ebe24aSShri Abhyankar /*@
6619d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6620d6ebe24aSShri Abhyankar 
6621d6ebe24aSShri Abhyankar    Input Parameters:
6622d6ebe24aSShri Abhyankar .  ts   - the TS context.
6623d6ebe24aSShri Abhyankar .  xl   - lower bound.
6624d6ebe24aSShri Abhyankar .  xu   - upper bound.
6625d6ebe24aSShri Abhyankar 
6626d6ebe24aSShri Abhyankar    Notes:
6627d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6628e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6629d6ebe24aSShri Abhyankar 
66302bd2b0e6SSatish Balay    Level: advanced
66312bd2b0e6SSatish Balay 
6632d6ebe24aSShri Abhyankar @*/
6633d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6634d6ebe24aSShri Abhyankar {
6635d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6636d6ebe24aSShri Abhyankar   SNES           snes;
6637d6ebe24aSShri Abhyankar 
6638d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6639d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6640d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6641d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6642d6ebe24aSShri Abhyankar }
6643d6ebe24aSShri Abhyankar 
6644325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6645c6db04a5SJed Brown #include <mex.h>
6646325fc9f4SBarry Smith 
6647325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6648325fc9f4SBarry Smith 
6649325fc9f4SBarry Smith /*
6650325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6651325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6652325fc9f4SBarry Smith 
6653325fc9f4SBarry Smith    Collective on TS
6654325fc9f4SBarry Smith 
6655325fc9f4SBarry Smith    Input Parameters:
6656325fc9f4SBarry Smith +  snes - the TS context
66570910c330SBarry Smith -  u - input vector
6658325fc9f4SBarry Smith 
6659325fc9f4SBarry Smith    Output Parameter:
6660325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6661325fc9f4SBarry Smith 
6662325fc9f4SBarry Smith    Notes:
6663325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6664325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6665325fc9f4SBarry Smith    themselves.
6666325fc9f4SBarry Smith 
6667325fc9f4SBarry Smith    Level: developer
6668325fc9f4SBarry Smith 
6669325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6670325fc9f4SBarry Smith 
6671325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6672325fc9f4SBarry Smith */
66730910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6674325fc9f4SBarry Smith {
6675325fc9f4SBarry Smith   PetscErrorCode  ierr;
6676325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6677325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6678325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6679325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6680325fc9f4SBarry Smith 
6681325fc9f4SBarry Smith   PetscFunctionBegin;
6682325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
66830910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
66840910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6685325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
66860910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6687325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6688325fc9f4SBarry Smith 
6689325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
66900910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
66910910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
66920910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6693bbd56ea5SKarl Rupp 
6694325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6695325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6696325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6697325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6698325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6699325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6700325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6701325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6702325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6703325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6704325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6705325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6706325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6707325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6708325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6709325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6710325fc9f4SBarry Smith   PetscFunctionReturn(0);
6711325fc9f4SBarry Smith }
6712325fc9f4SBarry Smith 
6713325fc9f4SBarry Smith 
6714325fc9f4SBarry Smith /*
6715325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6716325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6717e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6718325fc9f4SBarry Smith 
6719325fc9f4SBarry Smith    Logically Collective on TS
6720325fc9f4SBarry Smith 
6721325fc9f4SBarry Smith    Input Parameters:
6722325fc9f4SBarry Smith +  ts - the TS context
6723325fc9f4SBarry Smith -  func - function evaluation routine
6724325fc9f4SBarry Smith 
6725325fc9f4SBarry Smith    Calling sequence of func:
67260910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6727325fc9f4SBarry Smith 
6728325fc9f4SBarry Smith    Level: beginner
6729325fc9f4SBarry Smith 
6730325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6731325fc9f4SBarry Smith 
6732325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6733325fc9f4SBarry Smith */
6734cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6735325fc9f4SBarry Smith {
6736325fc9f4SBarry Smith   PetscErrorCode  ierr;
6737325fc9f4SBarry Smith   TSMatlabContext *sctx;
6738325fc9f4SBarry Smith 
6739325fc9f4SBarry Smith   PetscFunctionBegin;
6740325fc9f4SBarry Smith   /* currently sctx is memory bleed */
674195dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6742325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6743325fc9f4SBarry Smith   /*
6744325fc9f4SBarry Smith      This should work, but it doesn't
6745325fc9f4SBarry Smith   sctx->ctx = ctx;
6746325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6747325fc9f4SBarry Smith   */
6748325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6749bbd56ea5SKarl Rupp 
67500298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6751325fc9f4SBarry Smith   PetscFunctionReturn(0);
6752325fc9f4SBarry Smith }
6753325fc9f4SBarry Smith 
6754325fc9f4SBarry Smith /*
6755325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6756325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6757325fc9f4SBarry Smith 
6758325fc9f4SBarry Smith    Collective on TS
6759325fc9f4SBarry Smith 
6760325fc9f4SBarry Smith    Input Parameters:
6761cdcf91faSSean Farley +  ts - the TS context
67620910c330SBarry Smith .  u - input vector
6763325fc9f4SBarry Smith .  A, B - the matrices
6764325fc9f4SBarry Smith -  ctx - user context
6765325fc9f4SBarry Smith 
6766325fc9f4SBarry Smith    Level: developer
6767325fc9f4SBarry Smith 
6768325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6769325fc9f4SBarry Smith 
6770325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6771325fc9f4SBarry Smith @*/
6772f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6773325fc9f4SBarry Smith {
6774325fc9f4SBarry Smith   PetscErrorCode  ierr;
6775325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6776325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6777325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6778325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6779325fc9f4SBarry Smith 
6780325fc9f4SBarry Smith   PetscFunctionBegin;
6781cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
67820910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6783325fc9f4SBarry Smith 
67840910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6785325fc9f4SBarry Smith 
6786cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
67870910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
67880910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
67890910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
67900910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6791bbd56ea5SKarl Rupp 
6792325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6793325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6794325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6795325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6796325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6797325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6798325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6799325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6800325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6801325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6802325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6803325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6804325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6805325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6806325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6807325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6808325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6809325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6810325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6811325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6812325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6813325fc9f4SBarry Smith   PetscFunctionReturn(0);
6814325fc9f4SBarry Smith }
6815325fc9f4SBarry Smith 
6816325fc9f4SBarry Smith 
6817325fc9f4SBarry Smith /*
6818325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6819e3c5b3baSBarry 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
6820325fc9f4SBarry Smith 
6821325fc9f4SBarry Smith    Logically Collective on TS
6822325fc9f4SBarry Smith 
6823325fc9f4SBarry Smith    Input Parameters:
6824cdcf91faSSean Farley +  ts - the TS context
6825325fc9f4SBarry Smith .  A,B - Jacobian matrices
6826325fc9f4SBarry Smith .  func - function evaluation routine
6827325fc9f4SBarry Smith -  ctx - user context
6828325fc9f4SBarry Smith 
6829325fc9f4SBarry Smith    Calling sequence of func:
68300910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6831325fc9f4SBarry Smith 
6832325fc9f4SBarry Smith 
6833325fc9f4SBarry Smith    Level: developer
6834325fc9f4SBarry Smith 
6835325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6836325fc9f4SBarry Smith 
6837325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6838325fc9f4SBarry Smith */
6839cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6840325fc9f4SBarry Smith {
6841325fc9f4SBarry Smith   PetscErrorCode  ierr;
6842325fc9f4SBarry Smith   TSMatlabContext *sctx;
6843325fc9f4SBarry Smith 
6844325fc9f4SBarry Smith   PetscFunctionBegin;
6845325fc9f4SBarry Smith   /* currently sctx is memory bleed */
684695dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6847325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6848325fc9f4SBarry Smith   /*
6849325fc9f4SBarry Smith      This should work, but it doesn't
6850325fc9f4SBarry Smith   sctx->ctx = ctx;
6851325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6852325fc9f4SBarry Smith   */
6853325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6854bbd56ea5SKarl Rupp 
6855cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6856325fc9f4SBarry Smith   PetscFunctionReturn(0);
6857325fc9f4SBarry Smith }
6858325fc9f4SBarry Smith 
6859b5b1a830SBarry Smith /*
6860b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6861b5b1a830SBarry Smith 
6862b5b1a830SBarry Smith    Collective on TS
6863b5b1a830SBarry Smith 
6864b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6865b5b1a830SBarry Smith @*/
68660910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6867b5b1a830SBarry Smith {
6868b5b1a830SBarry Smith   PetscErrorCode  ierr;
6869b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6870a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6871b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6872b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6873b5b1a830SBarry Smith 
6874b5b1a830SBarry Smith   PetscFunctionBegin;
6875cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
68760910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6877b5b1a830SBarry Smith 
6878cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
68790910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6880bbd56ea5SKarl Rupp 
6881b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6882b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6883b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6884b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6885b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6886b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6887b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6888b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6889b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6890b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6891b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6892b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6893b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6894b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6895b5b1a830SBarry Smith   PetscFunctionReturn(0);
6896b5b1a830SBarry Smith }
6897b5b1a830SBarry Smith 
6898b5b1a830SBarry Smith 
6899b5b1a830SBarry Smith /*
6900b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6901b5b1a830SBarry Smith 
6902b5b1a830SBarry Smith    Level: developer
6903b5b1a830SBarry Smith 
6904b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6905b5b1a830SBarry Smith 
6906b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6907b5b1a830SBarry Smith */
6908cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6909b5b1a830SBarry Smith {
6910b5b1a830SBarry Smith   PetscErrorCode  ierr;
6911b5b1a830SBarry Smith   TSMatlabContext *sctx;
6912b5b1a830SBarry Smith 
6913b5b1a830SBarry Smith   PetscFunctionBegin;
6914b5b1a830SBarry Smith   /* currently sctx is memory bleed */
691595dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6916b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6917b5b1a830SBarry Smith   /*
6918b5b1a830SBarry Smith      This should work, but it doesn't
6919b5b1a830SBarry Smith   sctx->ctx = ctx;
6920b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6921b5b1a830SBarry Smith   */
6922b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6923bbd56ea5SKarl Rupp 
69240298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6925b5b1a830SBarry Smith   PetscFunctionReturn(0);
6926b5b1a830SBarry Smith }
6927325fc9f4SBarry Smith #endif
6928b3603a34SBarry Smith 
6929b3603a34SBarry Smith /*@C
69304f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6931b3603a34SBarry Smith        in a time based line graph
6932b3603a34SBarry Smith 
6933b3603a34SBarry Smith    Collective on TS
6934b3603a34SBarry Smith 
6935b3603a34SBarry Smith    Input Parameters:
6936b3603a34SBarry Smith +  ts - the TS context
6937b3603a34SBarry Smith .  step - current time-step
6938b3603a34SBarry Smith .  ptime - current time
69397db568b7SBarry Smith .  u - current solution
69407db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6941b3603a34SBarry Smith 
6942b3d3934dSBarry Smith    Options Database:
69439ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6944b3d3934dSBarry Smith 
6945b3603a34SBarry Smith    Level: intermediate
6946b3603a34SBarry Smith 
69476934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
6948b3603a34SBarry Smith 
6949b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6950b3603a34SBarry Smith 
69517db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
69527db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
69537db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
69547db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6955b3603a34SBarry Smith @*/
69567db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6957b3603a34SBarry Smith {
6958b3603a34SBarry Smith   PetscErrorCode    ierr;
69597db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6960b3603a34SBarry Smith   const PetscScalar *yy;
696180666b62SBarry Smith   Vec               v;
6962b3603a34SBarry Smith 
6963b3603a34SBarry Smith   PetscFunctionBegin;
696463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
696558ff32f7SBarry Smith   if (!step) {
6966a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69676934998bSLisandro Dalcin     PetscInt      dim;
6968a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6969a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6970bab0a581SBarry Smith     if (!ctx->names) {
6971bab0a581SBarry Smith       PetscBool flg;
6972bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6973bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6974bab0a581SBarry Smith       if (flg) {
6975bab0a581SBarry Smith         PetscInt i,n;
6976bab0a581SBarry Smith         char     **names;
6977bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6978bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6979bab0a581SBarry Smith         for (i=0; i<n; i++) {
6980bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6981bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6982bab0a581SBarry Smith         }
6983bab0a581SBarry Smith         names[n] = NULL;
6984bab0a581SBarry Smith         ctx->names = names;
6985bab0a581SBarry Smith       }
6986bab0a581SBarry Smith     }
6987387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6988387f4636SBarry Smith       char      **displaynames;
6989387f4636SBarry Smith       PetscBool flg;
6990387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
699195dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6992387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6993c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6994387f4636SBarry Smith       if (flg) {
6995a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6996387f4636SBarry Smith       }
6997387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6998387f4636SBarry Smith     }
6999387f4636SBarry Smith     if (ctx->displaynames) {
7000387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
7001387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
7002387f4636SBarry Smith     } else if (ctx->names) {
70030910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
70040b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7005387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
7006b0bc92c6SBarry Smith     } else {
7007b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7008b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7009387f4636SBarry Smith     }
70100b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
701158ff32f7SBarry Smith   }
70126934998bSLisandro Dalcin 
70136934998bSLisandro Dalcin   if (!ctx->transform) v = u;
70146934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
701580666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
70166934998bSLisandro Dalcin   if (ctx->displaynames) {
70176934998bSLisandro Dalcin     PetscInt i;
70186934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
70196934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
70206934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
70216934998bSLisandro Dalcin   } else {
7022e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7023e3efe391SJed Brown     PetscInt  i,n;
70246934998bSLisandro Dalcin     PetscReal *yreal;
702580666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
7026785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7027e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
70280b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7029e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7030e3efe391SJed Brown #else
70310b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7032e3efe391SJed Brown #endif
703380666b62SBarry Smith   }
70346934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
70356934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
70366934998bSLisandro Dalcin 
7037b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
70380b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70396934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
70403923b477SBarry Smith   }
7041b3603a34SBarry Smith   PetscFunctionReturn(0);
7042b3603a34SBarry Smith }
7043b3603a34SBarry Smith 
7044387f4636SBarry Smith 
7045b037adc7SBarry Smith /*@C
704631152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7047b037adc7SBarry Smith 
7048b037adc7SBarry Smith    Collective on TS
7049b037adc7SBarry Smith 
7050b037adc7SBarry Smith    Input Parameters:
7051b037adc7SBarry Smith +  ts - the TS context
7052b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
7053b037adc7SBarry Smith 
7054b037adc7SBarry Smith    Level: intermediate
7055b037adc7SBarry Smith 
70567db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
70577db568b7SBarry Smith 
7058b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
7059b037adc7SBarry Smith 
7060a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
7061b037adc7SBarry Smith @*/
706231152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
7063b037adc7SBarry Smith {
7064b037adc7SBarry Smith   PetscErrorCode    ierr;
7065b037adc7SBarry Smith   PetscInt          i;
7066b037adc7SBarry Smith 
7067b037adc7SBarry Smith   PetscFunctionBegin;
7068b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7069b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
70705537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
7071b3d3934dSBarry Smith       break;
7072b3d3934dSBarry Smith     }
7073b3d3934dSBarry Smith   }
7074b3d3934dSBarry Smith   PetscFunctionReturn(0);
7075b3d3934dSBarry Smith }
7076b3d3934dSBarry Smith 
7077e673d494SBarry Smith /*@C
7078e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
7079e673d494SBarry Smith 
7080e673d494SBarry Smith    Collective on TS
7081e673d494SBarry Smith 
7082e673d494SBarry Smith    Input Parameters:
7083e673d494SBarry Smith +  ts - the TS context
7084e673d494SBarry Smith -  names - the names of the components, final string must be NULL
7085e673d494SBarry Smith 
7086e673d494SBarry Smith    Level: intermediate
7087e673d494SBarry Smith 
7088e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7089e673d494SBarry Smith 
7090a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
7091e673d494SBarry Smith @*/
7092e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
7093e673d494SBarry Smith {
7094e673d494SBarry Smith   PetscErrorCode    ierr;
7095e673d494SBarry Smith 
7096e673d494SBarry Smith   PetscFunctionBegin;
7097e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
7098e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
7099e673d494SBarry Smith   PetscFunctionReturn(0);
7100e673d494SBarry Smith }
7101e673d494SBarry Smith 
7102b3d3934dSBarry Smith /*@C
7103b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
7104b3d3934dSBarry Smith 
7105b3d3934dSBarry Smith    Collective on TS
7106b3d3934dSBarry Smith 
7107b3d3934dSBarry Smith    Input Parameter:
7108b3d3934dSBarry Smith .  ts - the TS context
7109b3d3934dSBarry Smith 
7110b3d3934dSBarry Smith    Output Parameter:
7111b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
7112b3d3934dSBarry Smith 
7113b3d3934dSBarry Smith    Level: intermediate
7114b3d3934dSBarry Smith 
71157db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71167db568b7SBarry Smith 
7117b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7118b3d3934dSBarry Smith 
7119b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7120b3d3934dSBarry Smith @*/
7121b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
7122b3d3934dSBarry Smith {
7123b3d3934dSBarry Smith   PetscInt       i;
7124b3d3934dSBarry Smith 
7125b3d3934dSBarry Smith   PetscFunctionBegin;
7126b3d3934dSBarry Smith   *names = NULL;
7127b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7128b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
71295537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
7130b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
7131b3d3934dSBarry Smith       break;
7132387f4636SBarry Smith     }
7133387f4636SBarry Smith   }
7134387f4636SBarry Smith   PetscFunctionReturn(0);
7135387f4636SBarry Smith }
7136387f4636SBarry Smith 
7137a66092f1SBarry Smith /*@C
7138a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
7139a66092f1SBarry Smith 
7140a66092f1SBarry Smith    Collective on TS
7141a66092f1SBarry Smith 
7142a66092f1SBarry Smith    Input Parameters:
7143a66092f1SBarry Smith +  ctx - the TSMonitorLG context
7144a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
7145a66092f1SBarry Smith 
7146a66092f1SBarry Smith    Level: intermediate
7147a66092f1SBarry Smith 
7148a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
7149a66092f1SBarry Smith 
7150a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7151a66092f1SBarry Smith @*/
7152a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
7153a66092f1SBarry Smith {
7154a66092f1SBarry Smith   PetscInt          j = 0,k;
7155a66092f1SBarry Smith   PetscErrorCode    ierr;
7156a66092f1SBarry Smith 
7157a66092f1SBarry Smith   PetscFunctionBegin;
7158a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
7159a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
7160a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
7161a66092f1SBarry Smith   while (displaynames[j]) j++;
7162a66092f1SBarry Smith   ctx->ndisplayvariables = j;
7163a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
7164a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
7165a66092f1SBarry Smith   j = 0;
7166a66092f1SBarry Smith   while (displaynames[j]) {
7167a66092f1SBarry Smith     k = 0;
7168a66092f1SBarry Smith     while (ctx->names[k]) {
7169a66092f1SBarry Smith       PetscBool flg;
7170a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
7171a66092f1SBarry Smith       if (flg) {
7172a66092f1SBarry Smith         ctx->displayvariables[j] = k;
7173a66092f1SBarry Smith         break;
7174a66092f1SBarry Smith       }
7175a66092f1SBarry Smith       k++;
7176a66092f1SBarry Smith     }
7177a66092f1SBarry Smith     j++;
7178a66092f1SBarry Smith   }
7179a66092f1SBarry Smith   PetscFunctionReturn(0);
7180a66092f1SBarry Smith }
7181a66092f1SBarry Smith 
7182a66092f1SBarry Smith 
7183387f4636SBarry Smith /*@C
7184387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
7185387f4636SBarry Smith 
7186387f4636SBarry Smith    Collective on TS
7187387f4636SBarry Smith 
7188387f4636SBarry Smith    Input Parameters:
7189387f4636SBarry Smith +  ts - the TS context
7190387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
7191387f4636SBarry Smith 
71927db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
71937db568b7SBarry Smith 
7194387f4636SBarry Smith    Level: intermediate
7195387f4636SBarry Smith 
7196387f4636SBarry Smith .keywords: TS,  vector, monitor, view
7197387f4636SBarry Smith 
7198387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
7199387f4636SBarry Smith @*/
7200387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
7201387f4636SBarry Smith {
7202a66092f1SBarry Smith   PetscInt          i;
7203387f4636SBarry Smith   PetscErrorCode    ierr;
7204387f4636SBarry Smith 
7205387f4636SBarry Smith   PetscFunctionBegin;
7206387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7207387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
72085537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
7209b3d3934dSBarry Smith       break;
7210b037adc7SBarry Smith     }
7211b037adc7SBarry Smith   }
7212b037adc7SBarry Smith   PetscFunctionReturn(0);
7213b037adc7SBarry Smith }
7214b037adc7SBarry Smith 
721580666b62SBarry Smith /*@C
721680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
721780666b62SBarry Smith 
721880666b62SBarry Smith    Collective on TS
721980666b62SBarry Smith 
722080666b62SBarry Smith    Input Parameters:
722180666b62SBarry Smith +  ts - the TS context
722280666b62SBarry Smith .  transform - the transform function
72237684fa3eSBarry Smith .  destroy - function to destroy the optional context
722480666b62SBarry Smith -  ctx - optional context used by transform function
722580666b62SBarry Smith 
72267db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
72277db568b7SBarry Smith 
722880666b62SBarry Smith    Level: intermediate
722980666b62SBarry Smith 
723080666b62SBarry Smith .keywords: TS,  vector, monitor, view
723180666b62SBarry Smith 
7232a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
723380666b62SBarry Smith @*/
72347684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
723580666b62SBarry Smith {
723680666b62SBarry Smith   PetscInt          i;
7237a66092f1SBarry Smith   PetscErrorCode    ierr;
723880666b62SBarry Smith 
723980666b62SBarry Smith   PetscFunctionBegin;
724080666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
724180666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
72425537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
724380666b62SBarry Smith     }
724480666b62SBarry Smith   }
724580666b62SBarry Smith   PetscFunctionReturn(0);
724680666b62SBarry Smith }
724780666b62SBarry Smith 
7248e673d494SBarry Smith /*@C
7249e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
7250e673d494SBarry Smith 
7251e673d494SBarry Smith    Collective on TSLGCtx
7252e673d494SBarry Smith 
7253e673d494SBarry Smith    Input Parameters:
7254e673d494SBarry Smith +  ts - the TS context
7255e673d494SBarry Smith .  transform - the transform function
72567684fa3eSBarry Smith .  destroy - function to destroy the optional context
7257e673d494SBarry Smith -  ctx - optional context used by transform function
7258e673d494SBarry Smith 
7259e673d494SBarry Smith    Level: intermediate
7260e673d494SBarry Smith 
7261e673d494SBarry Smith .keywords: TS,  vector, monitor, view
7262e673d494SBarry Smith 
7263a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
7264e673d494SBarry Smith @*/
72657684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
7266e673d494SBarry Smith {
7267e673d494SBarry Smith   PetscFunctionBegin;
7268e673d494SBarry Smith   ctx->transform    = transform;
72697684fa3eSBarry Smith   ctx->transformdestroy = destroy;
7270e673d494SBarry Smith   ctx->transformctx = tctx;
7271e673d494SBarry Smith   PetscFunctionReturn(0);
7272e673d494SBarry Smith }
7273e673d494SBarry Smith 
7274ef20d060SBarry Smith /*@C
72754f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
7276ef20d060SBarry Smith        in a time based line graph
7277ef20d060SBarry Smith 
7278ef20d060SBarry Smith    Collective on TS
7279ef20d060SBarry Smith 
7280ef20d060SBarry Smith    Input Parameters:
7281ef20d060SBarry Smith +  ts - the TS context
7282ef20d060SBarry Smith .  step - current time-step
7283ef20d060SBarry Smith .  ptime - current time
72847db568b7SBarry Smith .  u - current solution
72857db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
7286ef20d060SBarry Smith 
7287ef20d060SBarry Smith    Level: intermediate
7288ef20d060SBarry Smith 
72896934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
7290abd5a294SJed Brown 
7291abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
7292abd5a294SJed Brown 
7293abd5a294SJed Brown    Options Database Keys:
72944f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
7295ef20d060SBarry Smith 
7296ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
7297ef20d060SBarry Smith 
7298abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
7299ef20d060SBarry Smith @*/
73000910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
7301ef20d060SBarry Smith {
7302ef20d060SBarry Smith   PetscErrorCode    ierr;
73030b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
7304ef20d060SBarry Smith   const PetscScalar *yy;
7305ef20d060SBarry Smith   Vec               y;
7306ef20d060SBarry Smith 
7307ef20d060SBarry Smith   PetscFunctionBegin;
7308a9f9c1f6SBarry Smith   if (!step) {
7309a9f9c1f6SBarry Smith     PetscDrawAxis axis;
73106934998bSLisandro Dalcin     PetscInt      dim;
7311a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
73121ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
73130910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
7314a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
7315a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7316a9f9c1f6SBarry Smith   }
73170910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
7318ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
73190910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7320ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
7321e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
7322e3efe391SJed Brown   {
7323e3efe391SJed Brown     PetscReal *yreal;
7324e3efe391SJed Brown     PetscInt  i,n;
7325e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
7326785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
7327e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
73280b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
7329e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
7330e3efe391SJed Brown   }
7331e3efe391SJed Brown #else
73320b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
7333e3efe391SJed Brown #endif
7334ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
7335ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
7336b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
73370b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73386934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
73393923b477SBarry Smith   }
7340ef20d060SBarry Smith   PetscFunctionReturn(0);
7341ef20d060SBarry Smith }
7342ef20d060SBarry Smith 
7343201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7344201da799SBarry Smith {
7345201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7346201da799SBarry Smith   PetscReal      x   = ptime,y;
7347201da799SBarry Smith   PetscErrorCode ierr;
7348201da799SBarry Smith   PetscInt       its;
7349201da799SBarry Smith 
7350201da799SBarry Smith   PetscFunctionBegin;
735163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7352201da799SBarry Smith   if (!n) {
7353201da799SBarry Smith     PetscDrawAxis axis;
7354201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7355201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7356201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7357201da799SBarry Smith     ctx->snes_its = 0;
7358201da799SBarry Smith   }
7359201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7360201da799SBarry Smith   y    = its - ctx->snes_its;
7361201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
73623fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7363201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73646934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7365201da799SBarry Smith   }
7366201da799SBarry Smith   ctx->snes_its = its;
7367201da799SBarry Smith   PetscFunctionReturn(0);
7368201da799SBarry Smith }
7369201da799SBarry Smith 
7370201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7371201da799SBarry Smith {
7372201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7373201da799SBarry Smith   PetscReal      x   = ptime,y;
7374201da799SBarry Smith   PetscErrorCode ierr;
7375201da799SBarry Smith   PetscInt       its;
7376201da799SBarry Smith 
7377201da799SBarry Smith   PetscFunctionBegin;
737863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7379201da799SBarry Smith   if (!n) {
7380201da799SBarry Smith     PetscDrawAxis axis;
7381201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7382201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7383201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7384201da799SBarry Smith     ctx->ksp_its = 0;
7385201da799SBarry Smith   }
7386201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7387201da799SBarry Smith   y    = its - ctx->ksp_its;
7388201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
738999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7390201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
73916934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7392201da799SBarry Smith   }
7393201da799SBarry Smith   ctx->ksp_its = its;
7394201da799SBarry Smith   PetscFunctionReturn(0);
7395201da799SBarry Smith }
7396f9c1d6abSBarry Smith 
7397f9c1d6abSBarry Smith /*@
7398f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7399f9c1d6abSBarry Smith 
7400f9c1d6abSBarry Smith    Collective on TS and Vec
7401f9c1d6abSBarry Smith 
7402f9c1d6abSBarry Smith    Input Parameters:
7403f9c1d6abSBarry Smith +  ts - the TS context
7404f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7405f9c1d6abSBarry Smith 
7406f9c1d6abSBarry Smith    Output Parameters:
7407f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7408f9c1d6abSBarry Smith 
7409f9c1d6abSBarry Smith    Level: developer
7410f9c1d6abSBarry Smith 
7411f9c1d6abSBarry Smith .keywords: TS, compute
7412f9c1d6abSBarry Smith 
7413f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7414f9c1d6abSBarry Smith @*/
7415f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7416f9c1d6abSBarry Smith {
7417f9c1d6abSBarry Smith   PetscErrorCode ierr;
7418f9c1d6abSBarry Smith 
7419f9c1d6abSBarry Smith   PetscFunctionBegin;
7420f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7421ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7422f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7423f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7424f9c1d6abSBarry Smith }
742524655328SShri 
7426b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7427b3d3934dSBarry Smith /*@C
7428b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7429b3d3934dSBarry Smith 
7430b3d3934dSBarry Smith    Collective on TS
7431b3d3934dSBarry Smith 
7432b3d3934dSBarry Smith    Input Parameters:
7433b3d3934dSBarry Smith .  ts  - the ODE solver object
7434b3d3934dSBarry Smith 
7435b3d3934dSBarry Smith    Output Parameter:
7436b3d3934dSBarry Smith .  ctx - the context
7437b3d3934dSBarry Smith 
7438b3d3934dSBarry Smith    Level: intermediate
7439b3d3934dSBarry Smith 
7440b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7441b3d3934dSBarry Smith 
7442b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7443b3d3934dSBarry Smith 
7444b3d3934dSBarry Smith @*/
7445b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7446b3d3934dSBarry Smith {
7447b3d3934dSBarry Smith   PetscErrorCode ierr;
7448b3d3934dSBarry Smith 
7449b3d3934dSBarry Smith   PetscFunctionBegin;
7450a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7451b3d3934dSBarry Smith   PetscFunctionReturn(0);
7452b3d3934dSBarry Smith }
7453b3d3934dSBarry Smith 
7454b3d3934dSBarry Smith /*@C
7455b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7456b3d3934dSBarry Smith 
7457b3d3934dSBarry Smith    Collective on TS
7458b3d3934dSBarry Smith 
7459b3d3934dSBarry Smith    Input Parameters:
7460b3d3934dSBarry Smith +  ts - the TS context
7461b3d3934dSBarry Smith .  step - current time-step
7462b3d3934dSBarry Smith .  ptime - current time
74637db568b7SBarry Smith .  u  - current solution
74647db568b7SBarry Smith -  dctx - the envelope context
7465b3d3934dSBarry Smith 
7466b3d3934dSBarry Smith    Options Database:
7467b3d3934dSBarry Smith .  -ts_monitor_envelope
7468b3d3934dSBarry Smith 
7469b3d3934dSBarry Smith    Level: intermediate
7470b3d3934dSBarry Smith 
7471b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7472b3d3934dSBarry Smith 
7473b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7474b3d3934dSBarry Smith 
74757db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7476b3d3934dSBarry Smith @*/
74777db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7478b3d3934dSBarry Smith {
7479b3d3934dSBarry Smith   PetscErrorCode       ierr;
74807db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7481b3d3934dSBarry Smith 
7482b3d3934dSBarry Smith   PetscFunctionBegin;
7483b3d3934dSBarry Smith   if (!ctx->max) {
7484b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7485b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7486b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7487b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7488b3d3934dSBarry Smith   } else {
7489b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7490b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7491b3d3934dSBarry Smith   }
7492b3d3934dSBarry Smith   PetscFunctionReturn(0);
7493b3d3934dSBarry Smith }
7494b3d3934dSBarry Smith 
7495b3d3934dSBarry Smith 
7496b3d3934dSBarry Smith /*@C
7497b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7498b3d3934dSBarry Smith 
7499b3d3934dSBarry Smith    Collective on TS
7500b3d3934dSBarry Smith 
7501b3d3934dSBarry Smith    Input Parameter:
7502b3d3934dSBarry Smith .  ts - the TS context
7503b3d3934dSBarry Smith 
7504b3d3934dSBarry Smith    Output Parameter:
7505b3d3934dSBarry Smith +  max - the maximum values
7506b3d3934dSBarry Smith -  min - the minimum values
7507b3d3934dSBarry Smith 
75087db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
75097db568b7SBarry Smith 
7510b3d3934dSBarry Smith    Level: intermediate
7511b3d3934dSBarry Smith 
7512b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7513b3d3934dSBarry Smith 
7514b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7515b3d3934dSBarry Smith @*/
7516b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7517b3d3934dSBarry Smith {
7518b3d3934dSBarry Smith   PetscInt i;
7519b3d3934dSBarry Smith 
7520b3d3934dSBarry Smith   PetscFunctionBegin;
7521b3d3934dSBarry Smith   if (max) *max = NULL;
7522b3d3934dSBarry Smith   if (min) *min = NULL;
7523b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7524b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
75255537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7526b3d3934dSBarry Smith       if (max) *max = ctx->max;
7527b3d3934dSBarry Smith       if (min) *min = ctx->min;
7528b3d3934dSBarry Smith       break;
7529b3d3934dSBarry Smith     }
7530b3d3934dSBarry Smith   }
7531b3d3934dSBarry Smith   PetscFunctionReturn(0);
7532b3d3934dSBarry Smith }
7533b3d3934dSBarry Smith 
7534b3d3934dSBarry Smith /*@C
7535b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7536b3d3934dSBarry Smith 
7537b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7538b3d3934dSBarry Smith 
7539b3d3934dSBarry Smith    Input Parameter:
7540b3d3934dSBarry Smith .  ctx - the monitor context
7541b3d3934dSBarry Smith 
7542b3d3934dSBarry Smith    Level: intermediate
7543b3d3934dSBarry Smith 
7544b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7545b3d3934dSBarry Smith 
75467db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7547b3d3934dSBarry Smith @*/
7548b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7549b3d3934dSBarry Smith {
7550b3d3934dSBarry Smith   PetscErrorCode ierr;
7551b3d3934dSBarry Smith 
7552b3d3934dSBarry Smith   PetscFunctionBegin;
7553b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7554b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7555b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7556b3d3934dSBarry Smith   PetscFunctionReturn(0);
7557b3d3934dSBarry Smith }
7558f2dee214SBarry Smith 
755924655328SShri /*@
756024655328SShri    TSRollBack - Rolls back one time step
756124655328SShri 
756224655328SShri    Collective on TS
756324655328SShri 
756424655328SShri    Input Parameter:
756524655328SShri .  ts - the TS context obtained from TSCreate()
756624655328SShri 
756724655328SShri    Level: advanced
756824655328SShri 
756924655328SShri .keywords: TS, timestep, rollback
757024655328SShri 
757124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
757224655328SShri @*/
757324655328SShri PetscErrorCode  TSRollBack(TS ts)
757424655328SShri {
757524655328SShri   PetscErrorCode ierr;
757624655328SShri 
757724655328SShri   PetscFunctionBegin;
757824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7579b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
758024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
758124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
758224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
758324655328SShri   ts->ptime = ts->ptime_prev;
7584be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
7585be5899b3SLisandro Dalcin   ts->steps--; ts->total_steps--;
758610b82f12SShri Abhyankar   ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
7587b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
758824655328SShri   PetscFunctionReturn(0);
758924655328SShri }
7590aeb4809dSShri Abhyankar 
7591ff22ae23SHong Zhang /*@
7592ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7593ff22ae23SHong Zhang 
7594ff22ae23SHong Zhang    Input Parameter:
7595ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7596ff22ae23SHong Zhang 
7597ff22ae23SHong Zhang    Level: advanced
7598ff22ae23SHong Zhang 
7599ff22ae23SHong Zhang .keywords: TS, getstages
7600ff22ae23SHong Zhang 
7601ff22ae23SHong Zhang .seealso: TSCreate()
7602ff22ae23SHong Zhang @*/
7603ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7604ff22ae23SHong Zhang {
7605ff22ae23SHong Zhang   PetscErrorCode ierr;
7606ff22ae23SHong Zhang 
7607ff22ae23SHong Zhang   PetscFunctionBegin;
7608ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7609ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7610ff22ae23SHong Zhang 
7611ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7612ff22ae23SHong Zhang   else {
7613ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7614ff22ae23SHong Zhang   }
7615ff22ae23SHong Zhang   PetscFunctionReturn(0);
7616ff22ae23SHong Zhang }
7617ff22ae23SHong Zhang 
7618847ff0e1SMatthew G. Knepley /*@C
7619847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7620847ff0e1SMatthew G. Knepley 
7621847ff0e1SMatthew G. Knepley   Collective on SNES
7622847ff0e1SMatthew G. Knepley 
7623847ff0e1SMatthew G. Knepley   Input Parameters:
7624847ff0e1SMatthew G. Knepley + ts - the TS context
7625847ff0e1SMatthew G. Knepley . t - current timestep
7626847ff0e1SMatthew G. Knepley . U - state vector
7627847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7628847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7629847ff0e1SMatthew G. Knepley - ctx - an optional user context
7630847ff0e1SMatthew G. Knepley 
7631847ff0e1SMatthew G. Knepley   Output Parameters:
7632847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7633847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7634847ff0e1SMatthew G. Knepley 
7635847ff0e1SMatthew G. Knepley   Level: intermediate
7636847ff0e1SMatthew G. Knepley 
7637847ff0e1SMatthew G. Knepley   Notes:
7638847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7639847ff0e1SMatthew G. Knepley 
7640847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7641847ff0e1SMatthew G. Knepley 
7642847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7643847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7644847ff0e1SMatthew G. Knepley 
7645847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7646847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7647847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7648847ff0e1SMatthew G. Knepley 
7649847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7650847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7651847ff0e1SMatthew G. Knepley @*/
7652847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7653847ff0e1SMatthew G. Knepley {
7654847ff0e1SMatthew G. Knepley   SNES           snes;
7655847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7656847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7657847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7658847ff0e1SMatthew G. Knepley 
7659847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7660c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7661847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7662847ff0e1SMatthew G. Knepley   if (!color) {
7663847ff0e1SMatthew G. Knepley     DM         dm;
7664847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7665847ff0e1SMatthew G. Knepley 
7666847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7667847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7668847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7669847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7670847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7671847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7672847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7673847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7674847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7675847ff0e1SMatthew G. Knepley     } else {
7676847ff0e1SMatthew G. Knepley       MatColoring mc;
7677847ff0e1SMatthew G. Knepley 
7678847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7679847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7680847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7681847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7682847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7683847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7684847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7685847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7686847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7687847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7688847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7689847ff0e1SMatthew G. Knepley     }
7690847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7691847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7692847ff0e1SMatthew G. Knepley   }
7693847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7694847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7695847ff0e1SMatthew G. Knepley   if (J != B) {
7696847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7697847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7698847ff0e1SMatthew G. Knepley   }
7699847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7700847ff0e1SMatthew G. Knepley }
770193b34091SDebojyoti Ghosh 
7702cb9d8021SPierre Barbier de Reuille /*@
7703cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7704cb9d8021SPierre Barbier de Reuille 
7705cb9d8021SPierre Barbier de Reuille     Input Parameters:
7706cb9d8021SPierre Barbier de Reuille     ts - the TS context
7707cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7708cb9d8021SPierre Barbier de Reuille 
7709cb9d8021SPierre Barbier de Reuille     Level: intermediate
7710cb9d8021SPierre Barbier de Reuille 
7711cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7712cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7713cb9d8021SPierre Barbier de Reuille @*/
7714cb9d8021SPierre Barbier de Reuille 
7715d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7716cb9d8021SPierre Barbier de Reuille {
7717cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7718cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7719cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7720cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7721cb9d8021SPierre Barbier de Reuille }
7722cb9d8021SPierre Barbier de Reuille 
7723cb9d8021SPierre Barbier de Reuille /*@
7724cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7725cb9d8021SPierre Barbier de Reuille 
7726cb9d8021SPierre Barbier de Reuille     Input Parameters:
7727cb9d8021SPierre Barbier de Reuille     ts - the TS context
7728cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7729d183316bSPierre Barbier de Reuille     Y - state vector to check.
7730cb9d8021SPierre Barbier de Reuille 
7731cb9d8021SPierre Barbier de Reuille     Output Parameter:
7732cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7733cb9d8021SPierre Barbier de Reuille 
7734cb9d8021SPierre Barbier de Reuille     Note:
7735cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7736cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
773796a0c994SBarry Smith 
773896a0c994SBarry Smith     Level: advanced
7739cb9d8021SPierre Barbier de Reuille @*/
7740d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7741cb9d8021SPierre Barbier de Reuille {
7742cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7743cb9d8021SPierre Barbier de Reuille 
7744cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7745cb9d8021SPierre Barbier de Reuille 
7746cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7747cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7748cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7749d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7750cb9d8021SPierre Barbier de Reuille   }
7751cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7752cb9d8021SPierre Barbier de Reuille }
77531ceb14c0SBarry Smith 
775493b34091SDebojyoti Ghosh /*@C
7755e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
775693b34091SDebojyoti Ghosh 
775793b34091SDebojyoti Ghosh   Collective on MPI_Comm
775893b34091SDebojyoti Ghosh 
775993b34091SDebojyoti Ghosh   Input Parameter:
776093b34091SDebojyoti Ghosh . tsin    - The input TS
776193b34091SDebojyoti Ghosh 
776293b34091SDebojyoti Ghosh   Output Parameter:
7763e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
776493b34091SDebojyoti Ghosh 
77655eca1a21SEmil Constantinescu   Notes:
77665eca1a21SEmil 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.
77675eca1a21SEmil Constantinescu 
7768baa10174SEmil 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);
77695eca1a21SEmil Constantinescu 
77705eca1a21SEmil Constantinescu   Level: developer
777193b34091SDebojyoti Ghosh 
7772e5168f73SEmil Constantinescu .keywords: TS, clone
7773e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
777493b34091SDebojyoti Ghosh @*/
7775baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
777693b34091SDebojyoti Ghosh {
777793b34091SDebojyoti Ghosh   TS             t;
777893b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7779dc846ba4SSatish Balay   SNES           snes_start;
7780dc846ba4SSatish Balay   DM             dm;
7781dc846ba4SSatish Balay   TSType         type;
778293b34091SDebojyoti Ghosh 
778393b34091SDebojyoti Ghosh   PetscFunctionBegin;
778493b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
778593b34091SDebojyoti Ghosh   *tsout = NULL;
778693b34091SDebojyoti Ghosh 
77877a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
778893b34091SDebojyoti Ghosh 
778993b34091SDebojyoti Ghosh   /* General TS description */
779093b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
779193b34091SDebojyoti Ghosh   t->setupcalled       = 0;
779293b34091SDebojyoti Ghosh   t->ksp_its           = 0;
779393b34091SDebojyoti Ghosh   t->snes_its          = 0;
779493b34091SDebojyoti Ghosh   t->nwork             = 0;
779593b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
779693b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
779793b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
779893b34091SDebojyoti Ghosh 
779934561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
780034561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7801d15a3a53SEmil Constantinescu 
780293b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
780393b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
780493b34091SDebojyoti Ghosh 
780593b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
780651699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
780793b34091SDebojyoti Ghosh 
7808e7069c78SShri   t->trajectory = tsin->trajectory;
7809e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7810e7069c78SShri 
7811e7069c78SShri   t->event = tsin->event;
78126b10a48eSSatish Balay   if (t->event) t->event->refct++;
7813e7069c78SShri 
781493b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
781593b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7816e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
781793b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
781893b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
781993b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
7820e7069c78SShri   t->total_steps       = tsin->total_steps;
782193b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
782293b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
782393b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
782493b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
782593b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
782693b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
782793b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
782893b34091SDebojyoti Ghosh 
782993b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
783093b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
783193b34091SDebojyoti Ghosh 
783293b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
783393b34091SDebojyoti Ghosh 
783493b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
783593b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
783693b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
783793b34091SDebojyoti Ghosh 
783893b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
783993b34091SDebojyoti Ghosh 
78400d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
78410d4fed19SBarry Smith     PetscInt i;
78420d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
78430d4fed19SBarry Smith     for (i=0; i<10; i++) {
78440d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
78450d4fed19SBarry Smith     }
78460d4fed19SBarry Smith   }
784793b34091SDebojyoti Ghosh   *tsout = t;
784893b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
784993b34091SDebojyoti Ghosh }
7850