xref: /petsc/src/ts/interface/ts.c (revision 4cdd57e5d74e338dd6722c4f54249377c94f1cf6)
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 
22bdad233fSMatthew Knepley #undef __FUNCT__
23fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSetFromOptions"
24fde5950dSBarry Smith /*@C
25fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
26fde5950dSBarry Smith 
27fde5950dSBarry Smith    Collective on TS
28fde5950dSBarry Smith 
29fde5950dSBarry Smith    Input Parameters:
30fde5950dSBarry Smith +  ts - TS object you wish to monitor
31fde5950dSBarry Smith .  name - the monitor type one is seeking
32fde5950dSBarry Smith .  help - message indicating what monitoring is done
33fde5950dSBarry Smith .  manual - manual page for the monitor
34fde5950dSBarry Smith .  monitor - the monitor function
35fde5950dSBarry 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
36fde5950dSBarry Smith 
37fde5950dSBarry Smith    Level: developer
38fde5950dSBarry Smith 
39fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
40fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
41fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
42fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
43fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
44fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
45fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
46fde5950dSBarry Smith @*/
47721cd6eeSBarry 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*))
48fde5950dSBarry Smith {
49fde5950dSBarry Smith   PetscErrorCode    ierr;
50fde5950dSBarry Smith   PetscViewer       viewer;
51fde5950dSBarry Smith   PetscViewerFormat format;
52fde5950dSBarry Smith   PetscBool         flg;
53fde5950dSBarry Smith 
54fde5950dSBarry Smith   PetscFunctionBegin;
55fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
56fde5950dSBarry Smith   if (flg) {
57721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
58721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
59721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
60fde5950dSBarry Smith     if (monitorsetup) {
61721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
62fde5950dSBarry Smith     }
63721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
64fde5950dSBarry Smith   }
65fde5950dSBarry Smith   PetscFunctionReturn(0);
66fde5950dSBarry Smith }
67fde5950dSBarry Smith 
68fde5950dSBarry Smith #undef __FUNCT__
69fde5950dSBarry Smith #define __FUNCT__ "TSAdjointMonitorSetFromOptions"
70fde5950dSBarry Smith /*@C
71fde5950dSBarry Smith    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
72fde5950dSBarry Smith 
73fde5950dSBarry Smith    Collective on TS
74fde5950dSBarry Smith 
75fde5950dSBarry Smith    Input Parameters:
76fde5950dSBarry Smith +  ts - TS object you wish to monitor
77fde5950dSBarry Smith .  name - the monitor type one is seeking
78fde5950dSBarry Smith .  help - message indicating what monitoring is done
79fde5950dSBarry Smith .  manual - manual page for the monitor
80fde5950dSBarry Smith .  monitor - the monitor function
81fde5950dSBarry 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
82fde5950dSBarry Smith 
83fde5950dSBarry Smith    Level: developer
84fde5950dSBarry Smith 
85fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
86fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
87fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
88fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
89fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
90fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
91fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
92fde5950dSBarry Smith @*/
93721cd6eeSBarry 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*))
94fde5950dSBarry Smith {
95fde5950dSBarry Smith   PetscErrorCode    ierr;
96fde5950dSBarry Smith   PetscViewer       viewer;
97fde5950dSBarry Smith   PetscViewerFormat format;
98fde5950dSBarry Smith   PetscBool         flg;
99fde5950dSBarry Smith 
100fde5950dSBarry Smith   PetscFunctionBegin;
101fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
102fde5950dSBarry Smith   if (flg) {
103721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
104721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
105721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
106fde5950dSBarry Smith     if (monitorsetup) {
107721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
108fde5950dSBarry Smith     }
109721cd6eeSBarry Smith     ierr = TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
110fde5950dSBarry Smith   }
111fde5950dSBarry Smith   PetscFunctionReturn(0);
112fde5950dSBarry Smith }
113fde5950dSBarry Smith 
114fde5950dSBarry Smith #undef __FUNCT__
115bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
116bdad233fSMatthew Knepley /*@
117bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
118bdad233fSMatthew Knepley 
119bdad233fSMatthew Knepley    Collective on TS
120bdad233fSMatthew Knepley 
121bdad233fSMatthew Knepley    Input Parameter:
122bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
123bdad233fSMatthew Knepley 
124bdad233fSMatthew Knepley    Options Database Keys:
125b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
126ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
1273e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
1283e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
1293e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
130a3cdaa26SBarry 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
131a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
132a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
133a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
134a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
135a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
136ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
137847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
138bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
139de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
140de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1416934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
142de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
143de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
144de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
145de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1463e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1473e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
148fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
149b3d3934dSBarry Smith .  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
150110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
151110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
15253ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
15353ea634cSHong Zhang 
15491b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
155bdad233fSMatthew Knepley 
156bdad233fSMatthew Knepley    Level: beginner
157bdad233fSMatthew Knepley 
158bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
159bdad233fSMatthew Knepley 
160a313700dSBarry Smith .seealso: TSGetType()
161bdad233fSMatthew Knepley @*/
1627087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
163bdad233fSMatthew Knepley {
164bc952696SBarry Smith   PetscBool              opt,flg,tflg;
165dfbe8321SBarry Smith   PetscErrorCode         ierr;
166eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
16731748224SBarry Smith   PetscReal              time_step;
16849354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
169d1212d36SBarry Smith   char                   dir[16];
170cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1716991f827SBarry Smith   const char             *defaultType;
1726991f827SBarry Smith   char                   typeName[256];
173bdad233fSMatthew Knepley 
174bdad233fSMatthew Knepley   PetscFunctionBegin;
1750700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1766991f827SBarry Smith 
1776991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
178cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
179cd11d68dSLisandro Dalcin 
180cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
181cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
182cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
183cd11d68dSLisandro Dalcin   else
184cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
1856991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1866991f827SBarry Smith   if (opt) {
1876991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1886991f827SBarry Smith   } else {
1896991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1906991f827SBarry Smith   }
191bdad233fSMatthew Knepley 
192bdad233fSMatthew Knepley   /* Handle generic TS options */
1930298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1940298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1950298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
19631748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
197cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
19849354f04SShri 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);
19949354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
2000298fd71SBarry 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);
2010298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
2020298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
2030298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
2040298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
205bdad233fSMatthew Knepley 
20656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
20756f85f32SBarry Smith   {
20856f85f32SBarry Smith   PetscBool set;
20956f85f32SBarry Smith   flg  = PETSC_FALSE;
21056f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
21156f85f32SBarry Smith   if (set) {
21256f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
21356f85f32SBarry Smith   }
21456f85f32SBarry Smith   }
21556f85f32SBarry Smith #endif
21656f85f32SBarry Smith 
217bdad233fSMatthew Knepley   /* Monitor options */
218cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
219fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
220fde5950dSBarry Smith   ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr);
221fde5950dSBarry Smith 
2225180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2235180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
2245180491cSLisandro Dalcin 
2254f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
226b3603a34SBarry Smith   if (opt) {
2270b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2283923b477SBarry Smith     PetscInt       howoften = 1;
229b3603a34SBarry Smith 
2300298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2316ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2324f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
233bdad233fSMatthew Knepley   }
2346ba87a44SLisandro Dalcin 
2354f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
236ef20d060SBarry Smith   if (opt) {
2370b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2383923b477SBarry Smith     PetscInt       howoften = 1;
239ef20d060SBarry Smith 
2400298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2416ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2424f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
243ef20d060SBarry Smith   }
2446934998bSLisandro Dalcin 
2456934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2466934998bSLisandro Dalcin   if (opt) {
2476934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2486934998bSLisandro Dalcin     PetscInt       howoften = 1;
2496934998bSLisandro Dalcin 
2506934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2516ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2526934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2536934998bSLisandro Dalcin   }
254201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
255201da799SBarry Smith   if (opt) {
256201da799SBarry Smith     TSMonitorLGCtx ctx;
257201da799SBarry Smith     PetscInt       howoften = 1;
258201da799SBarry Smith 
2590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2606ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
261201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
262201da799SBarry Smith   }
263201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
264201da799SBarry Smith   if (opt) {
265201da799SBarry Smith     TSMonitorLGCtx ctx;
266201da799SBarry Smith     PetscInt       howoften = 1;
267201da799SBarry Smith 
2680298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2696ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
270201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
271201da799SBarry Smith   }
2728189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2738189c53fSBarry Smith   if (opt) {
2748189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2758189c53fSBarry Smith     PetscInt          howoften = 1;
2768189c53fSBarry Smith 
2770298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2786934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2798189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2808189c53fSBarry Smith   }
281ef20d060SBarry Smith   opt  = PETSC_FALSE;
2820dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
283a7cc72afSBarry Smith   if (opt) {
28483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28583a4ac43SBarry Smith     PetscInt         howoften = 1;
286a80ad3e0SBarry Smith 
2870298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2886934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
28983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
290bdad233fSMatthew Knepley   }
291fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2929110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
2939110b2e7SHong Zhang   if (opt) {
2949110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
2959110b2e7SHong Zhang     PetscInt         howoften = 1;
2969110b2e7SHong Zhang 
2979110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
2986934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2999110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3009110b2e7SHong Zhang   }
3019110b2e7SHong Zhang   opt  = PETSC_FALSE;
3022d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
3032d5ee99bSBarry Smith   if (opt) {
3042d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
3052d5ee99bSBarry Smith     PetscReal        bounds[4];
3062d5ee99bSBarry Smith     PetscInt         n = 4;
3072d5ee99bSBarry Smith     PetscDraw        draw;
3086934998bSLisandro Dalcin     PetscDrawAxis    axis;
3092d5ee99bSBarry Smith 
3102d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
3112d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
3126934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
3132d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
3146934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
3156934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
3166934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
3172d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3182d5ee99bSBarry Smith   }
3192d5ee99bSBarry Smith   opt  = PETSC_FALSE;
3200dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
3213a471f94SBarry Smith   if (opt) {
32283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
32383a4ac43SBarry Smith     PetscInt         howoften = 1;
3243a471f94SBarry Smith 
3250298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
3266934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
32783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3283a471f94SBarry Smith   }
329fde5950dSBarry Smith 
330ed81e22dSJed Brown   opt  = PETSC_FALSE;
33191b97e58SBarry 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);
332ed81e22dSJed Brown   if (flg) {
333ed81e22dSJed Brown     const char *ptr,*ptr2;
334ed81e22dSJed Brown     char       *filetemplate;
335ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
336ed81e22dSJed Brown     /* Do some cursory validation of the input. */
337ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
338ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
339ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
340ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
341ce94432eSBarry 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");
342ed81e22dSJed Brown       if (ptr2) break;
343ed81e22dSJed Brown     }
344ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
345ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
346ed81e22dSJed Brown   }
347bdad233fSMatthew Knepley 
348d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
349d1212d36SBarry Smith   if (flg) {
350d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
351d1212d36SBarry Smith     int                  ray = 0;
352d1212d36SBarry Smith     DMDADirection        ddir;
353d1212d36SBarry Smith     DM                   da;
354d1212d36SBarry Smith     PetscMPIInt          rank;
355d1212d36SBarry Smith 
356ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
357d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
358d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
359ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
360d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
361d1212d36SBarry Smith 
362d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
363b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
364d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
365d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
366ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
367d1212d36SBarry Smith     if (!rank) {
368d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
369d1212d36SBarry Smith     }
37051b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
371d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
372d1212d36SBarry Smith   }
37351b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
37451b4a12fSMatthew G. Knepley   if (flg) {
37551b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
37651b4a12fSMatthew G. Knepley     int                 ray = 0;
37751b4a12fSMatthew G. Knepley     DMDADirection       ddir;
37851b4a12fSMatthew G. Knepley     DM                  da;
37951b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
380d1212d36SBarry Smith 
38151b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
38251b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
38351b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
38451b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
38551b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3861c3436cfSJed Brown 
38751b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
388b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
38951b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
39051b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
39151b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
39251b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
39351b4a12fSMatthew G. Knepley   }
394a7a1495cSBarry Smith 
395b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
396b3d3934dSBarry Smith   if (opt) {
397b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
398b3d3934dSBarry Smith 
399b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
400b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
401b3d3934dSBarry Smith   }
402b3d3934dSBarry Smith 
403847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
404847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
405847ff0e1SMatthew G. Knepley   if (flg) {
406847ff0e1SMatthew G. Knepley     DM   dm;
407847ff0e1SMatthew G. Knepley     DMTS tdm;
408847ff0e1SMatthew G. Knepley 
409847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
410847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
411847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
412847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
413847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
414847ff0e1SMatthew G. Knepley   }
415847ff0e1SMatthew G. Knepley 
416ee346746SBarry Smith   if (ts->adapt) {
417ee346746SBarry Smith     ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
418ee346746SBarry Smith   }
419d763cef2SBarry Smith 
420d763cef2SBarry Smith   /* Handle specific TS options */
421d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4226991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
423d763cef2SBarry Smith   }
424fbc52257SHong Zhang 
42568bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4264f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
42768bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
42868bece0bSHong Zhang   if (tflg) {
42968bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43068bece0bSHong Zhang   }
4314f122a70SLisandro Dalcin   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
43268bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
43368bece0bSHong Zhang   if (flg) {
43468bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
43568bece0bSHong Zhang     ts->adjoint_solve = tflg;
43668bece0bSHong Zhang   }
437d763cef2SBarry Smith 
438d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4390633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
440fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
441d763cef2SBarry Smith 
4424f122a70SLisandro Dalcin   if (ts->trajectory) {
4434f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
444d763cef2SBarry Smith   }
44568bece0bSHong Zhang 
4464f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4474f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4484f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
449d763cef2SBarry Smith   PetscFunctionReturn(0);
450d763cef2SBarry Smith }
451d763cef2SBarry Smith 
452d763cef2SBarry Smith #undef __FUNCT__
453bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
454d2daff3dSHong Zhang /*@
455bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
456d2daff3dSHong Zhang 
457d2daff3dSHong Zhang    Collective on TS
458d2daff3dSHong Zhang 
459d2daff3dSHong Zhang    Input Parameters:
460bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
461bc952696SBarry Smith 
46268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
463d2daff3dSHong Zhang 
464d2daff3dSHong Zhang    Level: intermediate
465d2daff3dSHong Zhang 
466bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
467d2daff3dSHong Zhang 
468bc952696SBarry Smith .keywords: TS, set, checkpoint,
469d2daff3dSHong Zhang @*/
470bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
471d2daff3dSHong Zhang {
472bc952696SBarry Smith   PetscErrorCode ierr;
473bc952696SBarry Smith 
474d2daff3dSHong Zhang   PetscFunctionBegin;
475d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
476bc952696SBarry Smith   if (!ts->trajectory) {
477bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
478972caf09SHong Zhang     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
479bc952696SBarry Smith   }
480d2daff3dSHong Zhang   PetscFunctionReturn(0);
481d2daff3dSHong Zhang }
482d2daff3dSHong Zhang 
483a7a1495cSBarry Smith #undef __FUNCT__
484a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
485a7a1495cSBarry Smith /*@
486a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
487a7a1495cSBarry Smith       set with TSSetRHSJacobian().
488a7a1495cSBarry Smith 
489a7a1495cSBarry Smith    Collective on TS and Vec
490a7a1495cSBarry Smith 
491a7a1495cSBarry Smith    Input Parameters:
492316643e7SJed Brown +  ts - the TS context
493a7a1495cSBarry Smith .  t - current timestep
4940910c330SBarry Smith -  U - input vector
495a7a1495cSBarry Smith 
496a7a1495cSBarry Smith    Output Parameters:
497a7a1495cSBarry Smith +  A - Jacobian matrix
498a7a1495cSBarry Smith .  B - optional preconditioning matrix
499a7a1495cSBarry Smith -  flag - flag indicating matrix structure
500a7a1495cSBarry Smith 
501a7a1495cSBarry Smith    Notes:
502a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
503a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
504a7a1495cSBarry Smith 
50594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
506a7a1495cSBarry Smith    flag parameter.
507a7a1495cSBarry Smith 
508a7a1495cSBarry Smith    Level: developer
509a7a1495cSBarry Smith 
510a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
511a7a1495cSBarry Smith 
51294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
513a7a1495cSBarry Smith @*/
514d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
515a7a1495cSBarry Smith {
516dfbe8321SBarry Smith   PetscErrorCode ierr;
517270bf2e7SJed Brown   PetscObjectState Ustate;
51824989b8cSPeter Brune   DM             dm;
519942e3340SBarry Smith   DMTS           tsdm;
52024989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
52124989b8cSPeter Brune   void           *ctx;
52224989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
523b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
524a7a1495cSBarry Smith 
525a7a1495cSBarry Smith   PetscFunctionBegin;
5260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5280910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
52924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
530942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
53124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5320298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
533b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
53459e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
535b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5360e4ef248SJed Brown     PetscFunctionReturn(0);
537f8ede8e7SJed Brown   }
538d90be118SSean Farley 
539ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
540d90be118SSean Farley 
541e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
54294ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54394ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
54494ab13aaSBarry Smith     if (A != B) {
54594ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54694ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
547e1244c69SJed Brown     }
548e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
549e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
550e1244c69SJed Brown   }
551e1244c69SJed Brown 
55224989b8cSPeter Brune   if (rhsjacobianfunc) {
5536cd88445SBarry Smith     PetscBool missing;
55494ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
555a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
556d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
557a7a1495cSBarry Smith     PetscStackPop;
55894ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5596cd88445SBarry Smith     if (A) {
5606cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5616cd88445SBarry 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");
5626cd88445SBarry Smith     }
5636cd88445SBarry Smith     if (B && B != A) {
5646cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5656cd88445SBarry 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");
5666cd88445SBarry Smith     }
567ef66eb69SBarry Smith   } else {
56894ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
56994ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
570ef66eb69SBarry Smith   }
5710e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5720910c330SBarry Smith   ts->rhsjacobian.X          = U;
57359e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
574a7a1495cSBarry Smith   PetscFunctionReturn(0);
575a7a1495cSBarry Smith }
576a7a1495cSBarry Smith 
5774a2ae208SSatish Balay #undef __FUNCT__
5784a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
579316643e7SJed Brown /*@
580d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
581d763cef2SBarry Smith 
582316643e7SJed Brown    Collective on TS and Vec
583316643e7SJed Brown 
584316643e7SJed Brown    Input Parameters:
585316643e7SJed Brown +  ts - the TS context
586316643e7SJed Brown .  t - current time
5870910c330SBarry Smith -  U - state vector
588316643e7SJed Brown 
589316643e7SJed Brown    Output Parameter:
590316643e7SJed Brown .  y - right hand side
591316643e7SJed Brown 
592316643e7SJed Brown    Note:
593316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
594316643e7SJed Brown    is used internally within the nonlinear solvers.
595316643e7SJed Brown 
596316643e7SJed Brown    Level: developer
597316643e7SJed Brown 
598316643e7SJed Brown .keywords: TS, compute
599316643e7SJed Brown 
600316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
601316643e7SJed Brown @*/
6020910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
603d763cef2SBarry Smith {
604dfbe8321SBarry Smith   PetscErrorCode ierr;
60524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60624989b8cSPeter Brune   TSIFunction    ifunction;
60724989b8cSPeter Brune   void           *ctx;
60824989b8cSPeter Brune   DM             dm;
60924989b8cSPeter Brune 
610d763cef2SBarry Smith   PetscFunctionBegin;
6110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6120910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6130700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
61424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6160298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
617d763cef2SBarry Smith 
618ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
619d763cef2SBarry Smith 
6200910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
62124989b8cSPeter Brune   if (rhsfunction) {
622d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6230910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
624d763cef2SBarry Smith     PetscStackPop;
625214bc6a2SJed Brown   } else {
626214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
627b2cd27e8SJed Brown   }
62844a41b28SSean Farley 
6290910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
630d763cef2SBarry Smith   PetscFunctionReturn(0);
631d763cef2SBarry Smith }
632d763cef2SBarry Smith 
6334a2ae208SSatish Balay #undef __FUNCT__
634ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
635ef20d060SBarry Smith /*@
636ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
637ef20d060SBarry Smith 
638ef20d060SBarry Smith    Collective on TS and Vec
639ef20d060SBarry Smith 
640ef20d060SBarry Smith    Input Parameters:
641ef20d060SBarry Smith +  ts - the TS context
642ef20d060SBarry Smith -  t - current time
643ef20d060SBarry Smith 
644ef20d060SBarry Smith    Output Parameter:
6450910c330SBarry Smith .  U - the solution
646ef20d060SBarry Smith 
647ef20d060SBarry Smith    Note:
648ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
649ef20d060SBarry Smith    is used internally within the nonlinear solvers.
650ef20d060SBarry Smith 
651ef20d060SBarry Smith    Level: developer
652ef20d060SBarry Smith 
653ef20d060SBarry Smith .keywords: TS, compute
654ef20d060SBarry Smith 
655abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
656ef20d060SBarry Smith @*/
6570910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
658ef20d060SBarry Smith {
659ef20d060SBarry Smith   PetscErrorCode     ierr;
660ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
661ef20d060SBarry Smith   void               *ctx;
662ef20d060SBarry Smith   DM                 dm;
663ef20d060SBarry Smith 
664ef20d060SBarry Smith   PetscFunctionBegin;
665ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6660910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
667ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
668ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
669ef20d060SBarry Smith 
670ef20d060SBarry Smith   if (solutionfunction) {
6719b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6720910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
673ef20d060SBarry Smith     PetscStackPop;
674ef20d060SBarry Smith   }
675ef20d060SBarry Smith   PetscFunctionReturn(0);
676ef20d060SBarry Smith }
6779b7cd975SBarry Smith #undef __FUNCT__
6789b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
6799b7cd975SBarry Smith /*@
6809b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6819b7cd975SBarry Smith 
6829b7cd975SBarry Smith    Collective on TS and Vec
6839b7cd975SBarry Smith 
6849b7cd975SBarry Smith    Input Parameters:
6859b7cd975SBarry Smith +  ts - the TS context
6869b7cd975SBarry Smith -  t - current time
6879b7cd975SBarry Smith 
6889b7cd975SBarry Smith    Output Parameter:
6899b7cd975SBarry Smith .  U - the function value
6909b7cd975SBarry Smith 
6919b7cd975SBarry Smith    Note:
6929b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6939b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6949b7cd975SBarry Smith 
6959b7cd975SBarry Smith    Level: developer
6969b7cd975SBarry Smith 
6979b7cd975SBarry Smith .keywords: TS, compute
6989b7cd975SBarry Smith 
6999b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7009b7cd975SBarry Smith @*/
7019b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7029b7cd975SBarry Smith {
7039b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7049b7cd975SBarry Smith   void               *ctx;
7059b7cd975SBarry Smith   DM                 dm;
7069b7cd975SBarry Smith 
7079b7cd975SBarry Smith   PetscFunctionBegin;
7089b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7099b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7109b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7119b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7129b7cd975SBarry Smith 
7139b7cd975SBarry Smith   if (forcing) {
7149b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7159b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7169b7cd975SBarry Smith     PetscStackPop;
7179b7cd975SBarry Smith   }
7189b7cd975SBarry Smith   PetscFunctionReturn(0);
7199b7cd975SBarry Smith }
720ef20d060SBarry Smith 
721ef20d060SBarry Smith #undef __FUNCT__
722214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
723214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
724214bc6a2SJed Brown {
7252dd45cf8SJed Brown   Vec            F;
726214bc6a2SJed Brown   PetscErrorCode ierr;
727214bc6a2SJed Brown 
728214bc6a2SJed Brown   PetscFunctionBegin;
7290298fd71SBarry Smith   *Frhs = NULL;
7300298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
731214bc6a2SJed Brown   if (!ts->Frhs) {
7322dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
733214bc6a2SJed Brown   }
734214bc6a2SJed Brown   *Frhs = ts->Frhs;
735214bc6a2SJed Brown   PetscFunctionReturn(0);
736214bc6a2SJed Brown }
737214bc6a2SJed Brown 
738214bc6a2SJed Brown #undef __FUNCT__
739214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
740214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
741214bc6a2SJed Brown {
742214bc6a2SJed Brown   Mat            A,B;
7432dd45cf8SJed Brown   PetscErrorCode ierr;
744214bc6a2SJed Brown 
745214bc6a2SJed Brown   PetscFunctionBegin;
746c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
747c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
7480298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
749214bc6a2SJed Brown   if (Arhs) {
750214bc6a2SJed Brown     if (!ts->Arhs) {
751214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
752214bc6a2SJed Brown     }
753214bc6a2SJed Brown     *Arhs = ts->Arhs;
754214bc6a2SJed Brown   }
755214bc6a2SJed Brown   if (Brhs) {
756214bc6a2SJed Brown     if (!ts->Brhs) {
757bdb70873SJed Brown       if (A != B) {
758214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
759bdb70873SJed Brown       } else {
760bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
76151699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
762bdb70873SJed Brown       }
763214bc6a2SJed Brown     }
764214bc6a2SJed Brown     *Brhs = ts->Brhs;
765214bc6a2SJed Brown   }
766214bc6a2SJed Brown   PetscFunctionReturn(0);
767214bc6a2SJed Brown }
768214bc6a2SJed Brown 
769214bc6a2SJed Brown #undef __FUNCT__
770316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
771316643e7SJed Brown /*@
7720910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
773316643e7SJed Brown 
774316643e7SJed Brown    Collective on TS and Vec
775316643e7SJed Brown 
776316643e7SJed Brown    Input Parameters:
777316643e7SJed Brown +  ts - the TS context
778316643e7SJed Brown .  t - current time
7790910c330SBarry Smith .  U - state vector
7800910c330SBarry Smith .  Udot - time derivative of state vector
781214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
782316643e7SJed Brown 
783316643e7SJed Brown    Output Parameter:
784316643e7SJed Brown .  Y - right hand side
785316643e7SJed Brown 
786316643e7SJed Brown    Note:
787316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
788316643e7SJed Brown    is used internally within the nonlinear solvers.
789316643e7SJed Brown 
790316643e7SJed Brown    If the user did did not write their equations in implicit form, this
791316643e7SJed Brown    function recasts them in implicit form.
792316643e7SJed Brown 
793316643e7SJed Brown    Level: developer
794316643e7SJed Brown 
795316643e7SJed Brown .keywords: TS, compute
796316643e7SJed Brown 
797316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
798316643e7SJed Brown @*/
7990910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
800316643e7SJed Brown {
801316643e7SJed Brown   PetscErrorCode ierr;
80224989b8cSPeter Brune   TSIFunction    ifunction;
80324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
80424989b8cSPeter Brune   void           *ctx;
80524989b8cSPeter Brune   DM             dm;
806316643e7SJed Brown 
807316643e7SJed Brown   PetscFunctionBegin;
8080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8090910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8100910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8110700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
812316643e7SJed Brown 
81324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81424989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8150298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
81624989b8cSPeter Brune 
817ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
818d90be118SSean Farley 
8190910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
82024989b8cSPeter Brune   if (ifunction) {
821316643e7SJed Brown     PetscStackPush("TS user implicit function");
8220910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
823316643e7SJed Brown     PetscStackPop;
824214bc6a2SJed Brown   }
825214bc6a2SJed Brown   if (imex) {
82624989b8cSPeter Brune     if (!ifunction) {
8270910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8282dd45cf8SJed Brown     }
82924989b8cSPeter Brune   } else if (rhsfunction) {
83024989b8cSPeter Brune     if (ifunction) {
831214bc6a2SJed Brown       Vec Frhs;
832214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8330910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
834214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8352dd45cf8SJed Brown     } else {
8360910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8370910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
838316643e7SJed Brown     }
8394a6899ffSJed Brown   }
8400910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
841316643e7SJed Brown   PetscFunctionReturn(0);
842316643e7SJed Brown }
843316643e7SJed Brown 
844316643e7SJed Brown #undef __FUNCT__
845316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
846316643e7SJed Brown /*@
847316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
848316643e7SJed Brown 
849316643e7SJed Brown    Collective on TS and Vec
850316643e7SJed Brown 
851316643e7SJed Brown    Input
852316643e7SJed Brown       Input Parameters:
853316643e7SJed Brown +  ts - the TS context
854316643e7SJed Brown .  t - current timestep
8550910c330SBarry Smith .  U - state vector
8560910c330SBarry Smith .  Udot - time derivative of state vector
857214bc6a2SJed Brown .  shift - shift to apply, see note below
858214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
859316643e7SJed Brown 
860316643e7SJed Brown    Output Parameters:
861316643e7SJed Brown +  A - Jacobian matrix
862316643e7SJed Brown .  B - optional preconditioning matrix
863316643e7SJed Brown -  flag - flag indicating matrix structure
864316643e7SJed Brown 
865316643e7SJed Brown    Notes:
8660910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
867316643e7SJed Brown 
8680910c330SBarry Smith    dF/dU + shift*dF/dUdot
869316643e7SJed Brown 
870316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
871316643e7SJed Brown    is used internally within the nonlinear solvers.
872316643e7SJed Brown 
873316643e7SJed Brown    Level: developer
874316643e7SJed Brown 
875316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
876316643e7SJed Brown 
877316643e7SJed Brown .seealso:  TSSetIJacobian()
87863495f91SJed Brown @*/
879d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
880316643e7SJed Brown {
881316643e7SJed Brown   PetscErrorCode ierr;
88224989b8cSPeter Brune   TSIJacobian    ijacobian;
88324989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
88424989b8cSPeter Brune   DM             dm;
88524989b8cSPeter Brune   void           *ctx;
886316643e7SJed Brown 
887316643e7SJed Brown   PetscFunctionBegin;
8880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8890910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8900910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
891316643e7SJed Brown   PetscValidPointer(A,6);
89294ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
893316643e7SJed Brown   PetscValidPointer(B,7);
89494ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
89524989b8cSPeter Brune 
89624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
89724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8980298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
89924989b8cSPeter Brune 
900ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
901316643e7SJed Brown 
90294ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
90324989b8cSPeter Brune   if (ijacobian) {
9046cd88445SBarry Smith     PetscBool missing;
905316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
906d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
907316643e7SJed Brown     PetscStackPop;
9086cd88445SBarry Smith     if (A) {
9096cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9106cd88445SBarry 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");
9116cd88445SBarry Smith     }
9126cd88445SBarry Smith     if (B && B != A) {
9136cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9146cd88445SBarry 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");
9156cd88445SBarry Smith     }
9164a6899ffSJed Brown   }
917214bc6a2SJed Brown   if (imex) {
918b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
91994ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
92094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
92194ab13aaSBarry Smith       if (A != B) {
92294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
92394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
924214bc6a2SJed Brown       }
925214bc6a2SJed Brown     }
926214bc6a2SJed Brown   } else {
927e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
928e1244c69SJed Brown     if (rhsjacobian) {
929214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
930d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
931e1244c69SJed Brown     }
93294ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
933e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
934e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
93594ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
93694ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
93794ab13aaSBarry Smith       if (A != B) {
93894ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
93994ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
940316643e7SJed Brown       }
941e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
942e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
943e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
94494ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
94594ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
94694ab13aaSBarry Smith         if (A != B) {
94794ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
94894ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
949214bc6a2SJed Brown         }
950316643e7SJed Brown       }
95194ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
95294ab13aaSBarry Smith       if (A != B) {
95394ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
954316643e7SJed Brown       }
955316643e7SJed Brown     }
956316643e7SJed Brown   }
95794ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
958316643e7SJed Brown   PetscFunctionReturn(0);
959316643e7SJed Brown }
960316643e7SJed Brown 
961316643e7SJed Brown #undef __FUNCT__
9624a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
963d763cef2SBarry Smith /*@C
964d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
965b5abc632SBarry Smith     where U_t = G(t,u).
966d763cef2SBarry Smith 
9673f9fe445SBarry Smith     Logically Collective on TS
968d763cef2SBarry Smith 
969d763cef2SBarry Smith     Input Parameters:
970d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9710298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
972d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
973d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9740298fd71SBarry Smith           function evaluation routine (may be NULL)
975d763cef2SBarry Smith 
976d763cef2SBarry Smith     Calling sequence of func:
97787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
978d763cef2SBarry Smith 
979d763cef2SBarry Smith +   t - current timestep
980d763cef2SBarry Smith .   u - input vector
981d763cef2SBarry Smith .   F - function vector
982d763cef2SBarry Smith -   ctx - [optional] user-defined function context
983d763cef2SBarry Smith 
984d763cef2SBarry Smith     Level: beginner
985d763cef2SBarry Smith 
9862bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9872bbac0d3SBarry Smith 
988d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
989d763cef2SBarry Smith 
990ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
991d763cef2SBarry Smith @*/
992089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
993d763cef2SBarry Smith {
994089b2837SJed Brown   PetscErrorCode ierr;
995089b2837SJed Brown   SNES           snes;
9960298fd71SBarry Smith   Vec            ralloc = NULL;
99724989b8cSPeter Brune   DM             dm;
998d763cef2SBarry Smith 
999089b2837SJed Brown   PetscFunctionBegin;
10000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1001ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
100224989b8cSPeter Brune 
100324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
100424989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1005089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1006e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1007e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1008e856ceecSJed Brown     r = ralloc;
1009e856ceecSJed Brown   }
1010089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1011e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1012d763cef2SBarry Smith   PetscFunctionReturn(0);
1013d763cef2SBarry Smith }
1014d763cef2SBarry Smith 
10154a2ae208SSatish Balay #undef __FUNCT__
1016ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
1017ef20d060SBarry Smith /*@C
1018abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1019ef20d060SBarry Smith 
1020ef20d060SBarry Smith     Logically Collective on TS
1021ef20d060SBarry Smith 
1022ef20d060SBarry Smith     Input Parameters:
1023ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1024ef20d060SBarry Smith .   f - routine for evaluating the solution
1025ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10260298fd71SBarry Smith           function evaluation routine (may be NULL)
1027ef20d060SBarry Smith 
1028ef20d060SBarry Smith     Calling sequence of func:
1029ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1030ef20d060SBarry Smith 
1031ef20d060SBarry Smith +   t - current timestep
1032ef20d060SBarry Smith .   u - output vector
1033ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1034ef20d060SBarry Smith 
1035abd5a294SJed Brown     Notes:
1036abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1037abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1038abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1039abd5a294SJed Brown 
10404f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1041abd5a294SJed Brown 
1042ef20d060SBarry Smith     Level: beginner
1043ef20d060SBarry Smith 
1044ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1045ef20d060SBarry Smith 
10469b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1047ef20d060SBarry Smith @*/
1048ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1049ef20d060SBarry Smith {
1050ef20d060SBarry Smith   PetscErrorCode ierr;
1051ef20d060SBarry Smith   DM             dm;
1052ef20d060SBarry Smith 
1053ef20d060SBarry Smith   PetscFunctionBegin;
1054ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1055ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1056ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1057ef20d060SBarry Smith   PetscFunctionReturn(0);
1058ef20d060SBarry Smith }
1059ef20d060SBarry Smith 
1060ef20d060SBarry Smith #undef __FUNCT__
10619b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
10629b7cd975SBarry Smith /*@C
10639b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10649b7cd975SBarry Smith 
10659b7cd975SBarry Smith     Logically Collective on TS
10669b7cd975SBarry Smith 
10679b7cd975SBarry Smith     Input Parameters:
10689b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
10699b7cd975SBarry Smith .   f - routine for evaluating the forcing function
10709b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10710298fd71SBarry Smith           function evaluation routine (may be NULL)
10729b7cd975SBarry Smith 
10739b7cd975SBarry Smith     Calling sequence of func:
10749b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
10759b7cd975SBarry Smith 
10769b7cd975SBarry Smith +   t - current timestep
10779b7cd975SBarry Smith .   u - output vector
10789b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10799b7cd975SBarry Smith 
10809b7cd975SBarry Smith     Notes:
10819b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
10829b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
10839b7cd975SBarry Smith 
10849b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10859b7cd975SBarry Smith 
10869b7cd975SBarry Smith     Level: beginner
10879b7cd975SBarry Smith 
10889b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10899b7cd975SBarry Smith 
10909b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10919b7cd975SBarry Smith @*/
1092d56366bfSLisandro Dalcin PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction f,void *ctx)
10939b7cd975SBarry Smith {
10949b7cd975SBarry Smith   PetscErrorCode ierr;
10959b7cd975SBarry Smith   DM             dm;
10969b7cd975SBarry Smith 
10979b7cd975SBarry Smith   PetscFunctionBegin;
10989b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10999b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
11009b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
11019b7cd975SBarry Smith   PetscFunctionReturn(0);
11029b7cd975SBarry Smith }
11039b7cd975SBarry Smith 
11049b7cd975SBarry Smith #undef __FUNCT__
11054a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1106d763cef2SBarry Smith /*@C
1107f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1108b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1109d763cef2SBarry Smith 
11103f9fe445SBarry Smith    Logically Collective on TS
1111d763cef2SBarry Smith 
1112d763cef2SBarry Smith    Input Parameters:
1113d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1114e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1115e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1116d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1117d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11180298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1119d763cef2SBarry Smith 
1120f7ab8db6SBarry Smith    Calling sequence of f:
1121ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1122d763cef2SBarry Smith 
1123d763cef2SBarry Smith +  t - current timestep
1124d763cef2SBarry Smith .  u - input vector
1125e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1126e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1127d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1128d763cef2SBarry Smith 
11296cd88445SBarry Smith    Notes:
11306cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11316cd88445SBarry Smith 
11326cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1133ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith    Level: beginner
1136d763cef2SBarry Smith 
1137d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1138d763cef2SBarry Smith 
1139ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1140d763cef2SBarry Smith 
1141d763cef2SBarry Smith @*/
1142e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1143d763cef2SBarry Smith {
1144277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1145089b2837SJed Brown   SNES           snes;
114624989b8cSPeter Brune   DM             dm;
114724989b8cSPeter Brune   TSIJacobian    ijacobian;
1148277b19d0SLisandro Dalcin 
1149d763cef2SBarry Smith   PetscFunctionBegin;
11500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1151e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1152e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1153e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1154e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1155d763cef2SBarry Smith 
115624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
115724989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1158e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1159e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1160e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1161e1244c69SJed Brown   }
11620298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1163089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11645f659677SPeter Brune   if (!ijacobian) {
1165e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11660e4ef248SJed Brown   }
1167e5d3d808SBarry Smith   if (Amat) {
1168e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11690e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1170e5d3d808SBarry Smith     ts->Arhs = Amat;
11710e4ef248SJed Brown   }
1172e5d3d808SBarry Smith   if (Pmat) {
1173e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11740e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1175e5d3d808SBarry Smith     ts->Brhs = Pmat;
11760e4ef248SJed Brown   }
1177d763cef2SBarry Smith   PetscFunctionReturn(0);
1178d763cef2SBarry Smith }
1179d763cef2SBarry Smith 
1180316643e7SJed Brown 
1181316643e7SJed Brown #undef __FUNCT__
1182316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1183316643e7SJed Brown /*@C
1184b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1185316643e7SJed Brown 
11863f9fe445SBarry Smith    Logically Collective on TS
1187316643e7SJed Brown 
1188316643e7SJed Brown    Input Parameters:
1189316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
11900298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1191316643e7SJed Brown .  f   - the function evaluation routine
11920298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1193316643e7SJed Brown 
1194316643e7SJed Brown    Calling sequence of f:
1195316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1196316643e7SJed Brown 
1197316643e7SJed Brown +  t   - time at step/stage being solved
1198316643e7SJed Brown .  u   - state vector
1199316643e7SJed Brown .  u_t - time derivative of state vector
1200316643e7SJed Brown .  F   - function vector
1201316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1202316643e7SJed Brown 
1203316643e7SJed Brown    Important:
12042bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1205316643e7SJed Brown 
1206316643e7SJed Brown    Level: beginner
1207316643e7SJed Brown 
1208316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1209316643e7SJed Brown 
1210d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1211316643e7SJed Brown @*/
121251699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1213316643e7SJed Brown {
1214089b2837SJed Brown   PetscErrorCode ierr;
1215089b2837SJed Brown   SNES           snes;
121651699248SLisandro Dalcin   Vec            ralloc = NULL;
121724989b8cSPeter Brune   DM             dm;
1218316643e7SJed Brown 
1219316643e7SJed Brown   PetscFunctionBegin;
12200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
122151699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
122224989b8cSPeter Brune 
122324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
122524989b8cSPeter Brune 
1226089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
122751699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
122851699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
122951699248SLisandro Dalcin     r  = ralloc;
1230e856ceecSJed Brown   }
123151699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
123251699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1233089b2837SJed Brown   PetscFunctionReturn(0);
1234089b2837SJed Brown }
1235089b2837SJed Brown 
1236089b2837SJed Brown #undef __FUNCT__
1237089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1238089b2837SJed Brown /*@C
1239089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1240089b2837SJed Brown 
1241089b2837SJed Brown    Not Collective
1242089b2837SJed Brown 
1243089b2837SJed Brown    Input Parameter:
1244089b2837SJed Brown .  ts - the TS context
1245089b2837SJed Brown 
1246089b2837SJed Brown    Output Parameter:
12470298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12480298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12490298fd71SBarry Smith -  ctx - the function context (or NULL)
1250089b2837SJed Brown 
1251089b2837SJed Brown    Level: advanced
1252089b2837SJed Brown 
1253089b2837SJed Brown .keywords: TS, nonlinear, get, function
1254089b2837SJed Brown 
1255089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1256089b2837SJed Brown @*/
1257089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1258089b2837SJed Brown {
1259089b2837SJed Brown   PetscErrorCode ierr;
1260089b2837SJed Brown   SNES           snes;
126124989b8cSPeter Brune   DM             dm;
1262089b2837SJed Brown 
1263089b2837SJed Brown   PetscFunctionBegin;
1264089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1265089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12660298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
126724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1269089b2837SJed Brown   PetscFunctionReturn(0);
1270089b2837SJed Brown }
1271089b2837SJed Brown 
1272089b2837SJed Brown #undef __FUNCT__
1273089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1274089b2837SJed Brown /*@C
1275089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1276089b2837SJed Brown 
1277089b2837SJed Brown    Not Collective
1278089b2837SJed Brown 
1279089b2837SJed Brown    Input Parameter:
1280089b2837SJed Brown .  ts - the TS context
1281089b2837SJed Brown 
1282089b2837SJed Brown    Output Parameter:
12830298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12840298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12850298fd71SBarry Smith -  ctx - the function context (or NULL)
1286089b2837SJed Brown 
1287089b2837SJed Brown    Level: advanced
1288089b2837SJed Brown 
1289089b2837SJed Brown .keywords: TS, nonlinear, get, function
1290089b2837SJed Brown 
12912bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1292089b2837SJed Brown @*/
1293089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1294089b2837SJed Brown {
1295089b2837SJed Brown   PetscErrorCode ierr;
1296089b2837SJed Brown   SNES           snes;
129724989b8cSPeter Brune   DM             dm;
1298089b2837SJed Brown 
1299089b2837SJed Brown   PetscFunctionBegin;
1300089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1301089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13020298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
130324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1305316643e7SJed Brown   PetscFunctionReturn(0);
1306316643e7SJed Brown }
1307316643e7SJed Brown 
1308316643e7SJed Brown #undef __FUNCT__
1309316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1310316643e7SJed Brown /*@C
1311a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1312ae8867d6SBarry Smith         provided with TSSetIFunction().
1313316643e7SJed Brown 
13143f9fe445SBarry Smith    Logically Collective on TS
1315316643e7SJed Brown 
1316316643e7SJed Brown    Input Parameters:
1317316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1318e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1319e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1320316643e7SJed Brown .  f   - the Jacobian evaluation routine
13210298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1322316643e7SJed Brown 
1323316643e7SJed Brown    Calling sequence of f:
1324ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1325316643e7SJed Brown 
1326316643e7SJed Brown +  t    - time at step/stage being solved
13271b4a444bSJed Brown .  U    - state vector
13281b4a444bSJed Brown .  U_t  - time derivative of state vector
1329316643e7SJed Brown .  a    - shift
1330e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1331e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1332316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1333316643e7SJed Brown 
1334316643e7SJed Brown    Notes:
1335e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1336316643e7SJed Brown 
1337895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1338895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1339895c21f2SBarry Smith 
1340a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1341b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1342a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1343a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1344a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1345a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1346a4f0a591SBarry Smith 
13476cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13486cd88445SBarry Smith 
13496cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1350ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1351ca5f011dSBarry Smith 
1352316643e7SJed Brown    Level: beginner
1353316643e7SJed Brown 
1354316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1355316643e7SJed Brown 
1356ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1357316643e7SJed Brown 
1358316643e7SJed Brown @*/
1359e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1360316643e7SJed Brown {
1361316643e7SJed Brown   PetscErrorCode ierr;
1362089b2837SJed Brown   SNES           snes;
136324989b8cSPeter Brune   DM             dm;
1364316643e7SJed Brown 
1365316643e7SJed Brown   PetscFunctionBegin;
13660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1367e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1368e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1369e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1370e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
137124989b8cSPeter Brune 
137224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137324989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
137424989b8cSPeter Brune 
1375089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1376e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1377316643e7SJed Brown   PetscFunctionReturn(0);
1378316643e7SJed Brown }
1379316643e7SJed Brown 
13804a2ae208SSatish Balay #undef __FUNCT__
1381e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1382e1244c69SJed Brown /*@
1383e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1384e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1385e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1386e1244c69SJed Brown    not been changed by the TS.
1387e1244c69SJed Brown 
1388e1244c69SJed Brown    Logically Collective
1389e1244c69SJed Brown 
1390e1244c69SJed Brown    Input Arguments:
1391e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1392e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1393e1244c69SJed Brown 
1394e1244c69SJed Brown    Level: intermediate
1395e1244c69SJed Brown 
1396e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1397e1244c69SJed Brown @*/
1398e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1399e1244c69SJed Brown {
1400e1244c69SJed Brown   PetscFunctionBegin;
1401e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1402e1244c69SJed Brown   PetscFunctionReturn(0);
1403e1244c69SJed Brown }
1404e1244c69SJed Brown 
1405e1244c69SJed Brown #undef __FUNCT__
1406efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Function"
1407efe9872eSLisandro Dalcin /*@C
1408efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1409efe9872eSLisandro Dalcin 
1410efe9872eSLisandro Dalcin    Logically Collective on TS
1411efe9872eSLisandro Dalcin 
1412efe9872eSLisandro Dalcin    Input Parameters:
1413efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1414efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1415efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1416efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1417efe9872eSLisandro Dalcin 
1418efe9872eSLisandro Dalcin    Calling sequence of fun:
1419efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1420efe9872eSLisandro Dalcin 
1421efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1422efe9872eSLisandro Dalcin .  U    - state vector
1423efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1424efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1425efe9872eSLisandro Dalcin .  F    - function vector
1426efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1427efe9872eSLisandro Dalcin 
1428efe9872eSLisandro Dalcin    Level: beginner
1429efe9872eSLisandro Dalcin 
1430efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1431efe9872eSLisandro Dalcin 
1432efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1433efe9872eSLisandro Dalcin @*/
1434efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1435efe9872eSLisandro Dalcin {
1436efe9872eSLisandro Dalcin   DM             dm;
1437efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1438efe9872eSLisandro Dalcin 
1439efe9872eSLisandro Dalcin   PetscFunctionBegin;
1440efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1441efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1442efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1443efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1444efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1445efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1446efe9872eSLisandro Dalcin }
1447efe9872eSLisandro Dalcin 
1448efe9872eSLisandro Dalcin #undef __FUNCT__
1449efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Function"
1450efe9872eSLisandro Dalcin /*@C
1451efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1452efe9872eSLisandro Dalcin 
1453efe9872eSLisandro Dalcin   Not Collective
1454efe9872eSLisandro Dalcin 
1455efe9872eSLisandro Dalcin   Input Parameter:
1456efe9872eSLisandro Dalcin . ts - the TS context
1457efe9872eSLisandro Dalcin 
1458efe9872eSLisandro Dalcin   Output Parameter:
1459efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1460efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1461efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1462efe9872eSLisandro Dalcin 
1463efe9872eSLisandro Dalcin   Level: advanced
1464efe9872eSLisandro Dalcin 
1465efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1466efe9872eSLisandro Dalcin 
1467efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1468efe9872eSLisandro Dalcin @*/
1469efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1470efe9872eSLisandro Dalcin {
1471efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1472efe9872eSLisandro Dalcin   SNES           snes;
1473efe9872eSLisandro Dalcin   DM             dm;
1474efe9872eSLisandro Dalcin 
1475efe9872eSLisandro Dalcin   PetscFunctionBegin;
1476efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1477efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1478efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1479efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1480efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1481efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1482efe9872eSLisandro Dalcin }
1483efe9872eSLisandro Dalcin 
1484efe9872eSLisandro Dalcin #undef __FUNCT__
1485efe9872eSLisandro Dalcin #define __FUNCT__ "TSSetI2Jacobian"
1486efe9872eSLisandro Dalcin /*@C
1487bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1488efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin    Logically Collective on TS
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin    Input Parameters:
1493efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1494efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1495efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1496efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1497efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1498efe9872eSLisandro Dalcin 
1499efe9872eSLisandro Dalcin    Calling sequence of jac:
1500bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1501efe9872eSLisandro Dalcin 
1502efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1503efe9872eSLisandro Dalcin .  U    - state vector
1504efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1505efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1506efe9872eSLisandro Dalcin .  v    - shift for U_t
1507efe9872eSLisandro Dalcin .  a    - shift for U_tt
1508efe9872eSLisandro 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
1509efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1510efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1511efe9872eSLisandro Dalcin 
1512efe9872eSLisandro Dalcin    Notes:
1513efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1514efe9872eSLisandro Dalcin 
1515efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1516efe9872eSLisandro 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.
1517efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1518bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin    Level: beginner
1521efe9872eSLisandro Dalcin 
1522efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1525efe9872eSLisandro Dalcin @*/
1526efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1527efe9872eSLisandro Dalcin {
1528efe9872eSLisandro Dalcin   DM             dm;
1529efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1530efe9872eSLisandro Dalcin 
1531efe9872eSLisandro Dalcin   PetscFunctionBegin;
1532efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1533efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1534efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1535efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1536efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1537efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1538efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1539efe9872eSLisandro Dalcin }
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin #undef __FUNCT__
1542efe9872eSLisandro Dalcin #define __FUNCT__ "TSGetI2Jacobian"
1543efe9872eSLisandro Dalcin /*@C
1544efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1545efe9872eSLisandro Dalcin 
1546efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1547efe9872eSLisandro Dalcin 
1548efe9872eSLisandro Dalcin   Input Parameter:
1549efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin   Output Parameters:
1552efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1553efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1554efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1555efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin   Notes: You can pass in NULL for any return argument you do not need.
1558efe9872eSLisandro Dalcin 
1559efe9872eSLisandro Dalcin   Level: advanced
1560efe9872eSLisandro Dalcin 
1561efe9872eSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
1562efe9872eSLisandro Dalcin 
1563efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1564efe9872eSLisandro Dalcin @*/
1565efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1566efe9872eSLisandro Dalcin {
1567efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1568efe9872eSLisandro Dalcin   SNES           snes;
1569efe9872eSLisandro Dalcin   DM             dm;
1570efe9872eSLisandro Dalcin 
1571efe9872eSLisandro Dalcin   PetscFunctionBegin;
1572efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1573efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1574efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1575efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1576efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1577efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1578efe9872eSLisandro Dalcin }
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin #undef __FUNCT__
1581efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Function"
1582efe9872eSLisandro Dalcin /*@
1583efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1584efe9872eSLisandro Dalcin 
1585efe9872eSLisandro Dalcin   Collective on TS and Vec
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin   Input Parameters:
1588efe9872eSLisandro Dalcin + ts - the TS context
1589efe9872eSLisandro Dalcin . t - current time
1590efe9872eSLisandro Dalcin . U - state vector
1591efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1592efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1593efe9872eSLisandro Dalcin 
1594efe9872eSLisandro Dalcin   Output Parameter:
1595efe9872eSLisandro Dalcin . F - the residual vector
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin   Note:
1598efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1599efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1600efe9872eSLisandro Dalcin 
1601efe9872eSLisandro Dalcin   Level: developer
1602efe9872eSLisandro Dalcin 
1603efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1604efe9872eSLisandro Dalcin 
1605efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1606efe9872eSLisandro Dalcin @*/
1607efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1608efe9872eSLisandro Dalcin {
1609efe9872eSLisandro Dalcin   DM             dm;
1610efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1611efe9872eSLisandro Dalcin   void           *ctx;
1612efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1613efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1614efe9872eSLisandro Dalcin 
1615efe9872eSLisandro Dalcin   PetscFunctionBegin;
1616efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1617efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1618efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1619efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1620efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1621efe9872eSLisandro Dalcin 
1622efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1623efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin 
1626efe9872eSLisandro Dalcin   if (!I2Function) {
1627efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1628efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1629efe9872eSLisandro Dalcin   }
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1632efe9872eSLisandro Dalcin 
1633efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1634efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1635efe9872eSLisandro Dalcin   PetscStackPop;
1636efe9872eSLisandro Dalcin 
1637efe9872eSLisandro Dalcin   if (rhsfunction) {
1638efe9872eSLisandro Dalcin     Vec Frhs;
1639efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1640efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1641efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1642efe9872eSLisandro Dalcin   }
1643efe9872eSLisandro Dalcin 
1644efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1645efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1646efe9872eSLisandro Dalcin }
1647efe9872eSLisandro Dalcin 
1648efe9872eSLisandro Dalcin #undef __FUNCT__
1649efe9872eSLisandro Dalcin #define __FUNCT__ "TSComputeI2Jacobian"
1650efe9872eSLisandro Dalcin /*@
1651efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   Collective on TS and Vec
1654efe9872eSLisandro Dalcin 
1655efe9872eSLisandro Dalcin   Input Parameters:
1656efe9872eSLisandro Dalcin + ts - the TS context
1657efe9872eSLisandro Dalcin . t - current timestep
1658efe9872eSLisandro Dalcin . U - state vector
1659efe9872eSLisandro Dalcin . V - time derivative of state vector
1660efe9872eSLisandro Dalcin . A - second time derivative of state vector
1661efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1662efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin   Output Parameters:
1665efe9872eSLisandro Dalcin + J - Jacobian matrix
1666efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1667efe9872eSLisandro Dalcin 
1668efe9872eSLisandro Dalcin   Notes:
1669efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1670efe9872eSLisandro Dalcin 
1671efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1674efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   Level: developer
1677efe9872eSLisandro Dalcin 
1678efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1679efe9872eSLisandro Dalcin 
1680efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1681efe9872eSLisandro Dalcin @*/
1682efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1683efe9872eSLisandro Dalcin {
1684efe9872eSLisandro Dalcin   DM             dm;
1685efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1686efe9872eSLisandro Dalcin   void           *ctx;
1687efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1688efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin   PetscFunctionBegin;
1691efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1692efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1693efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1694efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1695efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1696efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1697efe9872eSLisandro Dalcin 
1698efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1699efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1700efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1701efe9872eSLisandro Dalcin 
1702efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1703efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1704efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1705efe9872eSLisandro Dalcin   }
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1710efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1711efe9872eSLisandro Dalcin   PetscStackPop;
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   if (rhsjacobian) {
1714efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1715efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1716efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1717efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1718efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1719efe9872eSLisandro Dalcin   }
1720efe9872eSLisandro Dalcin 
1721efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1722efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1723efe9872eSLisandro Dalcin }
1724efe9872eSLisandro Dalcin 
1725efe9872eSLisandro Dalcin #undef __FUNCT__
1726efe9872eSLisandro Dalcin #define __FUNCT__ "TS2SetSolution"
1727efe9872eSLisandro Dalcin /*@
1728efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1729efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1730efe9872eSLisandro Dalcin 
1731efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1732efe9872eSLisandro Dalcin 
1733efe9872eSLisandro Dalcin    Input Parameters:
1734efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1735efe9872eSLisandro Dalcin .  u - the solution vector
1736efe9872eSLisandro Dalcin -  v - the time derivative vector
1737efe9872eSLisandro Dalcin 
1738efe9872eSLisandro Dalcin    Level: beginner
1739efe9872eSLisandro Dalcin 
1740efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1741efe9872eSLisandro Dalcin @*/
1742efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1743efe9872eSLisandro Dalcin {
1744efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1745efe9872eSLisandro Dalcin 
1746efe9872eSLisandro Dalcin   PetscFunctionBegin;
1747efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1748efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1749efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1750efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1751efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1752efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1753efe9872eSLisandro Dalcin   ts->vec_dot = v;
1754efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1755efe9872eSLisandro Dalcin }
1756efe9872eSLisandro Dalcin 
1757efe9872eSLisandro Dalcin #undef __FUNCT__
1758efe9872eSLisandro Dalcin #define __FUNCT__ "TS2GetSolution"
1759efe9872eSLisandro Dalcin /*@
1760efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1761efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1762efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1763efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1764efe9872eSLisandro Dalcin 
1765efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1766efe9872eSLisandro Dalcin 
1767efe9872eSLisandro Dalcin    Input Parameter:
1768efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1769efe9872eSLisandro Dalcin 
1770efe9872eSLisandro Dalcin    Output Parameter:
1771efe9872eSLisandro Dalcin +  u - the vector containing the solution
1772efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1773efe9872eSLisandro Dalcin 
1774efe9872eSLisandro Dalcin    Level: intermediate
1775efe9872eSLisandro Dalcin 
1776efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1777efe9872eSLisandro Dalcin 
1778efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1779efe9872eSLisandro Dalcin @*/
1780efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1781efe9872eSLisandro Dalcin {
1782efe9872eSLisandro Dalcin   PetscFunctionBegin;
1783efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1784efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1785efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1786efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1787efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1788efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1789efe9872eSLisandro Dalcin }
1790efe9872eSLisandro Dalcin 
1791efe9872eSLisandro Dalcin #undef __FUNCT__
179255849f57SBarry Smith #define __FUNCT__ "TSLoad"
179355849f57SBarry Smith /*@C
179455849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
179555849f57SBarry Smith 
179655849f57SBarry Smith   Collective on PetscViewer
179755849f57SBarry Smith 
179855849f57SBarry Smith   Input Parameters:
179955849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
180055849f57SBarry Smith            some related function before a call to TSLoad().
180155849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
180255849f57SBarry Smith 
180355849f57SBarry Smith    Level: intermediate
180455849f57SBarry Smith 
180555849f57SBarry Smith   Notes:
180655849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
180755849f57SBarry Smith 
180855849f57SBarry Smith   Notes for advanced users:
180955849f57SBarry Smith   Most users should not need to know the details of the binary storage
181055849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
181155849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
181255849f57SBarry Smith   format is
181355849f57SBarry Smith .vb
181455849f57SBarry Smith      has not yet been determined
181555849f57SBarry Smith .ve
181655849f57SBarry Smith 
181755849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
181855849f57SBarry Smith @*/
1819f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
182055849f57SBarry Smith {
182155849f57SBarry Smith   PetscErrorCode ierr;
182255849f57SBarry Smith   PetscBool      isbinary;
182355849f57SBarry Smith   PetscInt       classid;
182455849f57SBarry Smith   char           type[256];
18252d53ad75SBarry Smith   DMTS           sdm;
1826ad6bc421SBarry Smith   DM             dm;
182755849f57SBarry Smith 
182855849f57SBarry Smith   PetscFunctionBegin;
1829f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
183055849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
183155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
183255849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
183355849f57SBarry Smith 
1834060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1835ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1836060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1837f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1838f2c2a1b9SBarry Smith   if (ts->ops->load) {
1839f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1840f2c2a1b9SBarry Smith   }
1841ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1842ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1843ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1844f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1845f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18462d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18472d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
184855849f57SBarry Smith   PetscFunctionReturn(0);
184955849f57SBarry Smith }
185055849f57SBarry Smith 
18519804daf3SBarry Smith #include <petscdraw.h>
1852e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1853e04113cfSBarry Smith #include <petscviewersaws.h>
1854f05ece33SBarry Smith #endif
185555849f57SBarry Smith #undef __FUNCT__
18564a2ae208SSatish Balay #define __FUNCT__ "TSView"
18577e2c5f70SBarry Smith /*@C
1858d763cef2SBarry Smith     TSView - Prints the TS data structure.
1859d763cef2SBarry Smith 
18604c49b128SBarry Smith     Collective on TS
1861d763cef2SBarry Smith 
1862d763cef2SBarry Smith     Input Parameters:
1863d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1864d763cef2SBarry Smith -   viewer - visualization context
1865d763cef2SBarry Smith 
1866d763cef2SBarry Smith     Options Database Key:
1867d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1868d763cef2SBarry Smith 
1869d763cef2SBarry Smith     Notes:
1870d763cef2SBarry Smith     The available visualization contexts include
1871b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1872b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1873d763cef2SBarry Smith          output where only the first processor opens
1874d763cef2SBarry Smith          the file.  All other processors send their
1875d763cef2SBarry Smith          data to the first processor to print.
1876d763cef2SBarry Smith 
1877d763cef2SBarry Smith     The user can open an alternative visualization context with
1878b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1879d763cef2SBarry Smith 
1880d763cef2SBarry Smith     Level: beginner
1881d763cef2SBarry Smith 
1882d763cef2SBarry Smith .keywords: TS, timestep, view
1883d763cef2SBarry Smith 
1884b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1885d763cef2SBarry Smith @*/
18867087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1887d763cef2SBarry Smith {
1888dfbe8321SBarry Smith   PetscErrorCode ierr;
188919fd82e9SBarry Smith   TSType         type;
18902b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
18912d53ad75SBarry Smith   DMTS           sdm;
1892e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1893536b137fSBarry Smith   PetscBool      issaws;
1894f05ece33SBarry Smith #endif
1895d763cef2SBarry Smith 
1896d763cef2SBarry Smith   PetscFunctionBegin;
18970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18983050cee2SBarry Smith   if (!viewer) {
1899ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19003050cee2SBarry Smith   }
19010700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1902c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1903fd16b177SBarry Smith 
1904251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1905251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
190655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19072b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1908e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1909536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1910f05ece33SBarry Smith #endif
191132077d6dSBarry Smith   if (iascii) {
1912dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
191377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
19147c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1915d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
19165ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1917c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1918d763cef2SBarry Smith     }
19195ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1920193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1921a0af407cSBarry Smith     if (ts->vrtol) {
1922a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1923a0af407cSBarry Smith     } else {
1924a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1925a0af407cSBarry Smith     }
1926a0af407cSBarry Smith     if (ts->vatol) {
1927a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1928a0af407cSBarry Smith     } else {
1929a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1930a0af407cSBarry Smith     }
19312d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19322d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1933d52bd9f3SBarry Smith     if (ts->ops->view) {
1934d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1935d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1936d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1937d52bd9f3SBarry Smith     }
19380f5bd95cSBarry Smith   } else if (isstring) {
1939a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1940b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
194155849f57SBarry Smith   } else if (isbinary) {
194255849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
194355849f57SBarry Smith     MPI_Comm    comm;
194455849f57SBarry Smith     PetscMPIInt rank;
194555849f57SBarry Smith     char        type[256];
194655849f57SBarry Smith 
194755849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
194855849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
194955849f57SBarry Smith     if (!rank) {
195055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
195155849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
195255849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
195355849f57SBarry Smith     }
195455849f57SBarry Smith     if (ts->ops->view) {
195555849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
195655849f57SBarry Smith     }
1957f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1958f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19592d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19602d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19612b0a91c0SBarry Smith   } else if (isdraw) {
19622b0a91c0SBarry Smith     PetscDraw draw;
19632b0a91c0SBarry Smith     char      str[36];
196489fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19652b0a91c0SBarry Smith 
19662b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19672b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19682b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19692b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
197051fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
197189fd9fafSBarry Smith     bottom = y - h;
19722b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19732b0a91c0SBarry Smith     if (ts->ops->view) {
19742b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19752b0a91c0SBarry Smith     }
19762b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1977e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1978536b137fSBarry Smith   } else if (issaws) {
1979d45a07a7SBarry Smith     PetscMPIInt rank;
19802657e9d9SBarry Smith     const char  *name;
19812657e9d9SBarry Smith 
19822657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1983d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1984d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1985d45a07a7SBarry Smith       char       dir[1024];
1986d45a07a7SBarry Smith 
1987e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1988a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
19892657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
19902657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
19912657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1992d763cef2SBarry Smith     }
19930acecf5bSBarry Smith     if (ts->ops->view) {
19940acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19950acecf5bSBarry Smith     }
1996f05ece33SBarry Smith #endif
1997f05ece33SBarry Smith   }
1998f05ece33SBarry Smith 
1999b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2000251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2001b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2002d763cef2SBarry Smith   PetscFunctionReturn(0);
2003d763cef2SBarry Smith }
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith 
20064a2ae208SSatish Balay #undef __FUNCT__
20074a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
2008b07ff414SBarry Smith /*@
2009d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2010d763cef2SBarry Smith    the timesteppers.
2011d763cef2SBarry Smith 
20123f9fe445SBarry Smith    Logically Collective on TS
2013d763cef2SBarry Smith 
2014d763cef2SBarry Smith    Input Parameters:
2015d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2016d763cef2SBarry Smith -  usrP - optional user context
2017d763cef2SBarry Smith 
2018daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2019daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2020daf670e6SBarry Smith 
2021d763cef2SBarry Smith    Level: intermediate
2022d763cef2SBarry Smith 
2023d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2026d763cef2SBarry Smith @*/
20277087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2028d763cef2SBarry Smith {
2029d763cef2SBarry Smith   PetscFunctionBegin;
20300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2031d763cef2SBarry Smith   ts->user = usrP;
2032d763cef2SBarry Smith   PetscFunctionReturn(0);
2033d763cef2SBarry Smith }
2034d763cef2SBarry Smith 
20354a2ae208SSatish Balay #undef __FUNCT__
20364a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
2037b07ff414SBarry Smith /*@
2038d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2039d763cef2SBarry Smith     timestepper.
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith     Not Collective
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith     Input Parameter:
2044d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2045d763cef2SBarry Smith 
2046d763cef2SBarry Smith     Output Parameter:
2047d763cef2SBarry Smith .   usrP - user context
2048d763cef2SBarry Smith 
2049daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
2050daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2051daf670e6SBarry Smith 
2052d763cef2SBarry Smith     Level: intermediate
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2055d763cef2SBarry Smith 
2056d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2057d763cef2SBarry Smith @*/
2058e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2059d763cef2SBarry Smith {
2060d763cef2SBarry Smith   PetscFunctionBegin;
20610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2062e71120c6SJed Brown   *(void**)usrP = ts->user;
2063d763cef2SBarry Smith   PetscFunctionReturn(0);
2064d763cef2SBarry Smith }
2065d763cef2SBarry Smith 
20664a2ae208SSatish Balay #undef __FUNCT__
20674a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
2068d763cef2SBarry Smith /*@
2069b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
2070d763cef2SBarry Smith 
2071d763cef2SBarry Smith    Not Collective
2072d763cef2SBarry Smith 
2073d763cef2SBarry Smith    Input Parameter:
2074d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith    Output Parameter:
2077b8123daeSJed Brown .  iter - number of steps completed so far
2078d763cef2SBarry Smith 
2079d763cef2SBarry Smith    Level: intermediate
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
20829be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2083d763cef2SBarry Smith @*/
20847087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
2085d763cef2SBarry Smith {
2086d763cef2SBarry Smith   PetscFunctionBegin;
20870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20884482741eSBarry Smith   PetscValidIntPointer(iter,2);
2089d763cef2SBarry Smith   *iter = ts->steps;
2090d763cef2SBarry Smith   PetscFunctionReturn(0);
2091d763cef2SBarry Smith }
2092d763cef2SBarry Smith 
20934a2ae208SSatish Balay #undef __FUNCT__
20944a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
2095d763cef2SBarry Smith /*@
2096d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
2097d763cef2SBarry Smith    as well as the initial time.
2098d763cef2SBarry Smith 
20993f9fe445SBarry Smith    Logically Collective on TS
2100d763cef2SBarry Smith 
2101d763cef2SBarry Smith    Input Parameters:
2102d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2103d763cef2SBarry Smith .  initial_time - the initial time
2104d763cef2SBarry Smith -  time_step - the size of the timestep
2105d763cef2SBarry Smith 
2106d763cef2SBarry Smith    Level: intermediate
2107d763cef2SBarry Smith 
2108d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith .keywords: TS, set, initial, timestep
2111d763cef2SBarry Smith @*/
21127087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2113d763cef2SBarry Smith {
2114e144a568SJed Brown   PetscErrorCode ierr;
2115e144a568SJed Brown 
2116d763cef2SBarry Smith   PetscFunctionBegin;
21170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2118e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2119d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2120d763cef2SBarry Smith   PetscFunctionReturn(0);
2121d763cef2SBarry Smith }
2122d763cef2SBarry Smith 
21234a2ae208SSatish Balay #undef __FUNCT__
21244a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
2125d763cef2SBarry Smith /*@
2126d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2127d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2128d763cef2SBarry Smith 
21293f9fe445SBarry Smith    Logically Collective on TS
2130d763cef2SBarry Smith 
2131d763cef2SBarry Smith    Input Parameters:
2132d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2133d763cef2SBarry Smith -  time_step - the size of the timestep
2134d763cef2SBarry Smith 
2135d763cef2SBarry Smith    Level: intermediate
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith .keywords: TS, set, timestep
2140d763cef2SBarry Smith @*/
21417087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2142d763cef2SBarry Smith {
2143d763cef2SBarry Smith   PetscFunctionBegin;
21440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2145c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2146d763cef2SBarry Smith   ts->time_step = time_step;
2147d763cef2SBarry Smith   PetscFunctionReturn(0);
2148d763cef2SBarry Smith }
2149d763cef2SBarry Smith 
21504a2ae208SSatish Balay #undef __FUNCT__
2151a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
2152a43b19c4SJed Brown /*@
215349354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
215449354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
215549354f04SShri Abhyankar      or just return at the final time TS computed.
2156a43b19c4SJed Brown 
2157a43b19c4SJed Brown   Logically Collective on TS
2158a43b19c4SJed Brown 
2159a43b19c4SJed Brown    Input Parameter:
2160a43b19c4SJed Brown +   ts - the time-step context
216149354f04SShri Abhyankar -   eftopt - exact final time option
2162a43b19c4SJed Brown 
2163feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2164feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2165feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2166feed9e9dSBarry Smith 
2167feed9e9dSBarry Smith    Options Database:
2168feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2169feed9e9dSBarry Smith 
2170ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2171ee346746SBarry Smith     then the final time you selected.
2172ee346746SBarry Smith 
2173a43b19c4SJed Brown    Level: beginner
2174a43b19c4SJed Brown 
2175a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
2176a43b19c4SJed Brown @*/
217749354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2178a43b19c4SJed Brown {
2179a43b19c4SJed Brown   PetscFunctionBegin;
2180a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218149354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
218249354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2183a43b19c4SJed Brown   PetscFunctionReturn(0);
2184a43b19c4SJed Brown }
2185a43b19c4SJed Brown 
2186a43b19c4SJed Brown #undef __FUNCT__
21874a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
2188d763cef2SBarry Smith /*@
2189d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith    Not Collective
2192d763cef2SBarry Smith 
2193d763cef2SBarry Smith    Input Parameter:
2194d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2195d763cef2SBarry Smith 
2196d763cef2SBarry Smith    Output Parameter:
2197d763cef2SBarry Smith .  dt - the current timestep size
2198d763cef2SBarry Smith 
2199d763cef2SBarry Smith    Level: intermediate
2200d763cef2SBarry Smith 
2201d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2202d763cef2SBarry Smith 
2203d763cef2SBarry Smith .keywords: TS, get, timestep
2204d763cef2SBarry Smith @*/
22057087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2206d763cef2SBarry Smith {
2207d763cef2SBarry Smith   PetscFunctionBegin;
22080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2209f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2210d763cef2SBarry Smith   *dt = ts->time_step;
2211d763cef2SBarry Smith   PetscFunctionReturn(0);
2212d763cef2SBarry Smith }
2213d763cef2SBarry Smith 
22144a2ae208SSatish Balay #undef __FUNCT__
22154a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
2216d8e5e3e6SSatish Balay /*@
2217d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2218d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2219d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2220d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith    Input Parameter:
2225d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2226d763cef2SBarry Smith 
2227d763cef2SBarry Smith    Output Parameter:
2228d763cef2SBarry Smith .  v - the vector containing the solution
2229d763cef2SBarry Smith 
223063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
223163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
223263e21af5SBarry Smith 
2233d763cef2SBarry Smith    Level: intermediate
2234d763cef2SBarry Smith 
2235b2bf4f3aSDebojyoti Ghosh .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents()
2236d763cef2SBarry Smith 
2237d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2238d763cef2SBarry Smith @*/
22397087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2240d763cef2SBarry Smith {
2241d763cef2SBarry Smith   PetscFunctionBegin;
22420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22434482741eSBarry Smith   PetscValidPointer(v,2);
22448737fe31SLisandro Dalcin   *v = ts->vec_sol;
2245d763cef2SBarry Smith   PetscFunctionReturn(0);
2246d763cef2SBarry Smith }
2247d763cef2SBarry Smith 
2248c235aa8dSHong Zhang #undef __FUNCT__
2249b2bf4f3aSDebojyoti Ghosh #define __FUNCT__ "TSGetSolutionComponents"
225003fe5f5eSDebojyoti Ghosh /*@
2251b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
225203fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2253b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
225403fe5f5eSDebojyoti Ghosh    structure as the solution vector.
225503fe5f5eSDebojyoti Ghosh 
225603fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
225703fe5f5eSDebojyoti Ghosh 
225803fe5f5eSDebojyoti Ghosh    Parameters :
225903fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2260b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2261b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
226203fe5f5eSDebojyoti Ghosh        returned in v.
2263b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
226403fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2265b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
226603fe5f5eSDebojyoti Ghosh 
2267*4cdd57e5SDebojyoti Ghosh    Level: advanced
226803fe5f5eSDebojyoti Ghosh 
226903fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
227003fe5f5eSDebojyoti Ghosh 
227103fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
227203fe5f5eSDebojyoti Ghosh @*/
2273b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
227403fe5f5eSDebojyoti Ghosh {
227503fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
227603fe5f5eSDebojyoti Ghosh 
227703fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
227803fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2279b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
228003fe5f5eSDebojyoti Ghosh   else {
2281b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
228203fe5f5eSDebojyoti Ghosh   }
228303fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
228403fe5f5eSDebojyoti Ghosh }
228503fe5f5eSDebojyoti Ghosh 
228603fe5f5eSDebojyoti Ghosh #undef __FUNCT__
2287*4cdd57e5SDebojyoti Ghosh #define __FUNCT__ "TSGetAuxSolution"
2288*4cdd57e5SDebojyoti Ghosh /*@
2289*4cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
2290*4cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
2291*4cdd57e5SDebojyoti Ghosh 
2292*4cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
2293*4cdd57e5SDebojyoti Ghosh 
2294*4cdd57e5SDebojyoti Ghosh    Parameters :
2295*4cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2296*4cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
2297*4cdd57e5SDebojyoti Ghosh 
2298*4cdd57e5SDebojyoti Ghosh    Level: intermediate
2299*4cdd57e5SDebojyoti Ghosh 
2300*4cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
2301*4cdd57e5SDebojyoti Ghosh 
2302*4cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
2303*4cdd57e5SDebojyoti Ghosh @*/
2304*4cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
2305*4cdd57e5SDebojyoti Ghosh {
2306*4cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
2307*4cdd57e5SDebojyoti Ghosh 
2308*4cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
2309*4cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2310*4cdd57e5SDebojyoti Ghosh   ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2311*4cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
2312*4cdd57e5SDebojyoti Ghosh }
2313*4cdd57e5SDebojyoti Ghosh 
2314*4cdd57e5SDebojyoti Ghosh #undef __FUNCT__
2315*4cdd57e5SDebojyoti Ghosh #define __FUNCT__ "TSGetTimeError"
2316*4cdd57e5SDebojyoti Ghosh /*@
2317*4cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
2318*4cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
2319*4cdd57e5SDebojyoti Ghosh 
2320*4cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
2321*4cdd57e5SDebojyoti Ghosh 
2322*4cdd57e5SDebojyoti Ghosh    Parameters :
2323*4cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2324*4cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
2325*4cdd57e5SDebojyoti Ghosh 
2326*4cdd57e5SDebojyoti Ghosh    Level: intermediate
2327*4cdd57e5SDebojyoti Ghosh 
2328*4cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
2329*4cdd57e5SDebojyoti Ghosh 
2330*4cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
2331*4cdd57e5SDebojyoti Ghosh @*/
2332*4cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetTimeError(TS ts,Vec *v)
2333*4cdd57e5SDebojyoti Ghosh {
2334*4cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
2335*4cdd57e5SDebojyoti Ghosh 
2336*4cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
2337*4cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2338*4cdd57e5SDebojyoti Ghosh   ierr = (*ts->ops->gettimeerror)(ts,v);CHKERRQ(ierr);
2339*4cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
2340*4cdd57e5SDebojyoti Ghosh }
2341*4cdd57e5SDebojyoti Ghosh 
2342*4cdd57e5SDebojyoti Ghosh #undef __FUNCT__
2343dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients"
2344c235aa8dSHong Zhang /*@
2345dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
2346c235aa8dSHong Zhang 
2347c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
2348c235aa8dSHong Zhang 
2349c235aa8dSHong Zhang    Input Parameter:
2350c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
2351c235aa8dSHong Zhang 
2352c235aa8dSHong Zhang    Output Parameter:
2353abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
2354abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
2355c235aa8dSHong Zhang 
2356c235aa8dSHong Zhang    Level: intermediate
2357c235aa8dSHong Zhang 
2358c235aa8dSHong Zhang .seealso: TSGetTimeStep()
2359c235aa8dSHong Zhang 
2360c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
2361c235aa8dSHong Zhang @*/
2362dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
2363c235aa8dSHong Zhang {
2364c235aa8dSHong Zhang   PetscFunctionBegin;
2365c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2366abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
2367abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
2368abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
2369c235aa8dSHong Zhang   PetscFunctionReturn(0);
2370c235aa8dSHong Zhang }
2371c235aa8dSHong Zhang 
2372bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
23734a2ae208SSatish Balay #undef __FUNCT__
2374bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
2375d8e5e3e6SSatish Balay /*@
2376bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2377d763cef2SBarry Smith 
2378bdad233fSMatthew Knepley   Not collective
2379d763cef2SBarry Smith 
2380bdad233fSMatthew Knepley   Input Parameters:
2381bdad233fSMatthew Knepley + ts   - The TS
2382bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2383d763cef2SBarry Smith .vb
23840910c330SBarry Smith          U_t - A U = 0      (linear)
23850910c330SBarry Smith          U_t - A(t) U = 0   (linear)
23860910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2387d763cef2SBarry Smith .ve
2388d763cef2SBarry Smith 
2389d763cef2SBarry Smith    Level: beginner
2390d763cef2SBarry Smith 
2391bdad233fSMatthew Knepley .keywords: TS, problem type
2392bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2393d763cef2SBarry Smith @*/
23947087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2395a7cc72afSBarry Smith {
23969e2a6581SJed Brown   PetscErrorCode ierr;
23979e2a6581SJed Brown 
2398d763cef2SBarry Smith   PetscFunctionBegin;
23990700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2400bdad233fSMatthew Knepley   ts->problem_type = type;
24019e2a6581SJed Brown   if (type == TS_LINEAR) {
24029e2a6581SJed Brown     SNES snes;
24039e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24049e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24059e2a6581SJed Brown   }
2406d763cef2SBarry Smith   PetscFunctionReturn(0);
2407d763cef2SBarry Smith }
2408d763cef2SBarry Smith 
2409bdad233fSMatthew Knepley #undef __FUNCT__
2410bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
2411bdad233fSMatthew Knepley /*@C
2412bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2413bdad233fSMatthew Knepley 
2414bdad233fSMatthew Knepley   Not collective
2415bdad233fSMatthew Knepley 
2416bdad233fSMatthew Knepley   Input Parameter:
2417bdad233fSMatthew Knepley . ts   - The TS
2418bdad233fSMatthew Knepley 
2419bdad233fSMatthew Knepley   Output Parameter:
2420bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2421bdad233fSMatthew Knepley .vb
2422089b2837SJed Brown          M U_t = A U
2423089b2837SJed Brown          M(t) U_t = A(t) U
2424b5abc632SBarry Smith          F(t,U,U_t)
2425bdad233fSMatthew Knepley .ve
2426bdad233fSMatthew Knepley 
2427bdad233fSMatthew Knepley    Level: beginner
2428bdad233fSMatthew Knepley 
2429bdad233fSMatthew Knepley .keywords: TS, problem type
2430bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2431bdad233fSMatthew Knepley @*/
24327087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2433a7cc72afSBarry Smith {
2434bdad233fSMatthew Knepley   PetscFunctionBegin;
24350700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24364482741eSBarry Smith   PetscValidIntPointer(type,2);
2437bdad233fSMatthew Knepley   *type = ts->problem_type;
2438bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2439bdad233fSMatthew Knepley }
2440d763cef2SBarry Smith 
24414a2ae208SSatish Balay #undef __FUNCT__
24424a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
2443d763cef2SBarry Smith /*@
2444d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2445d763cef2SBarry Smith    of a timestepper.
2446d763cef2SBarry Smith 
2447d763cef2SBarry Smith    Collective on TS
2448d763cef2SBarry Smith 
2449d763cef2SBarry Smith    Input Parameter:
2450d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2451d763cef2SBarry Smith 
2452d763cef2SBarry Smith    Notes:
2453d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2454d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2455d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
2456d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2457d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
2458d763cef2SBarry Smith 
2459d763cef2SBarry Smith    Level: advanced
2460d763cef2SBarry Smith 
2461d763cef2SBarry Smith .keywords: TS, timestep, setup
2462d763cef2SBarry Smith 
2463d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
2464d763cef2SBarry Smith @*/
24657087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2466d763cef2SBarry Smith {
2467dfbe8321SBarry Smith   PetscErrorCode ierr;
24686c6b9e74SPeter Brune   DM             dm;
24696c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2470d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2471cd11d68dSLisandro Dalcin   TSIFunction    ifun;
24726c6b9e74SPeter Brune   TSIJacobian    ijac;
2473efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
24746c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
2475d763cef2SBarry Smith 
2476d763cef2SBarry Smith   PetscFunctionBegin;
24770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2478277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2479277b19d0SLisandro Dalcin 
24802c18e0fdSBarry Smith   ts->total_steps = 0;
24817adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2482cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2483cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2484d763cef2SBarry Smith   }
2485277b19d0SLisandro Dalcin 
2486277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2487277b19d0SLisandro Dalcin 
2488e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2489e1244c69SJed Brown     Mat Amat,Pmat;
2490e1244c69SJed Brown     SNES snes;
2491e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2492e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2493e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2494e1244c69SJed Brown      * have displaced the RHS matrix */
2495e1244c69SJed Brown     if (Amat == ts->Arhs) {
2496e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2497e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2498e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2499e1244c69SJed Brown     }
2500e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2501e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2502e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2503e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2504e1244c69SJed Brown     }
2505e1244c69SJed Brown   }
2506277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2507000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2508277b19d0SLisandro Dalcin   }
2509277b19d0SLisandro Dalcin 
2510a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25116c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25126c6b9e74SPeter Brune    */
25136c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25140298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25156c6b9e74SPeter Brune   if (!func) {
25166c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25176c6b9e74SPeter Brune   }
2518a6772fa2SLisandro 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.
25196c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25206c6b9e74SPeter Brune    */
25210298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25220298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2523efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25240298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2525efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25266c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25276c6b9e74SPeter Brune   }
2528c0517034SDebojyoti Ghosh 
2529c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2530c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2531c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2532c0517034SDebojyoti Ghosh   }
2533c0517034SDebojyoti Ghosh 
2534277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2535277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2536277b19d0SLisandro Dalcin }
2537277b19d0SLisandro Dalcin 
2538277b19d0SLisandro Dalcin #undef __FUNCT__
2539f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
2540f6a906c0SBarry Smith /*@
2541f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2542f6a906c0SBarry Smith    of an adjoint solver
2543f6a906c0SBarry Smith 
2544f6a906c0SBarry Smith    Collective on TS
2545f6a906c0SBarry Smith 
2546f6a906c0SBarry Smith    Input Parameter:
2547f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2548f6a906c0SBarry Smith 
2549f6a906c0SBarry Smith    Level: advanced
2550f6a906c0SBarry Smith 
2551f6a906c0SBarry Smith .keywords: TS, timestep, setup
2552f6a906c0SBarry Smith 
2553947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2554f6a906c0SBarry Smith @*/
2555f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2556f6a906c0SBarry Smith {
2557f6a906c0SBarry Smith   PetscErrorCode ierr;
2558f6a906c0SBarry Smith 
2559f6a906c0SBarry Smith   PetscFunctionBegin;
2560f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2561f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2562dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2563d8151eeaSBarry Smith 
2564947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2565d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2566d8151eeaSBarry Smith     if (ts->vecs_sensip){
2567d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2568d8151eeaSBarry Smith     }
2569947abb85SHong Zhang   }
2570d8151eeaSBarry Smith 
257142f2b339SBarry Smith   if (ts->ops->adjointsetup) {
257242f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2573f6a906c0SBarry Smith   }
2574f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2575f6a906c0SBarry Smith   PetscFunctionReturn(0);
2576f6a906c0SBarry Smith }
2577f6a906c0SBarry Smith 
2578f6a906c0SBarry Smith #undef __FUNCT__
2579277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
2580277b19d0SLisandro Dalcin /*@
2581277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2582277b19d0SLisandro Dalcin 
2583277b19d0SLisandro Dalcin    Collective on TS
2584277b19d0SLisandro Dalcin 
2585277b19d0SLisandro Dalcin    Input Parameter:
2586277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2587277b19d0SLisandro Dalcin 
2588277b19d0SLisandro Dalcin    Level: beginner
2589277b19d0SLisandro Dalcin 
2590277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2591277b19d0SLisandro Dalcin 
2592277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2593277b19d0SLisandro Dalcin @*/
2594277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2595277b19d0SLisandro Dalcin {
2596277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2597277b19d0SLisandro Dalcin 
2598277b19d0SLisandro Dalcin   PetscFunctionBegin;
2599277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2600b18ea86cSHong Zhang 
2601277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2602277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2603277b19d0SLisandro Dalcin   }
2604277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2605e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2606bbd56ea5SKarl Rupp 
26074e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26084e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2609214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26106bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2611efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2612e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2613e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
261438637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2615bbd56ea5SKarl Rupp 
2616947abb85SHong Zhang  if (ts->vec_costintegral) {
2617d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2618d8151eeaSBarry Smith     if (ts->vecs_drdp){
2619d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2620d8151eeaSBarry Smith     }
2621947abb85SHong Zhang   }
2622d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2623d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2624ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26252c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
262636eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2627277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2628d763cef2SBarry Smith   PetscFunctionReturn(0);
2629d763cef2SBarry Smith }
2630d763cef2SBarry Smith 
26314a2ae208SSatish Balay #undef __FUNCT__
26324a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2633d8e5e3e6SSatish Balay /*@
2634d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2635d763cef2SBarry Smith    with TSCreate().
2636d763cef2SBarry Smith 
2637d763cef2SBarry Smith    Collective on TS
2638d763cef2SBarry Smith 
2639d763cef2SBarry Smith    Input Parameter:
2640d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2641d763cef2SBarry Smith 
2642d763cef2SBarry Smith    Level: beginner
2643d763cef2SBarry Smith 
2644d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2645d763cef2SBarry Smith 
2646d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2647d763cef2SBarry Smith @*/
26486bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2649d763cef2SBarry Smith {
26506849ba73SBarry Smith   PetscErrorCode ierr;
2651d763cef2SBarry Smith 
2652d763cef2SBarry Smith   PetscFunctionBegin;
26536bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26546bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26556bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2656d763cef2SBarry Smith 
26576bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2658277b19d0SLisandro Dalcin 
2659e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2660e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26616bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26626d4c513bSLisandro Dalcin 
2663bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2664bc952696SBarry Smith 
266584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26666427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26676427ac75SLisandro Dalcin 
26686bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26696bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
26706bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
26710dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
26726d4c513bSLisandro Dalcin 
2673a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2674d763cef2SBarry Smith   PetscFunctionReturn(0);
2675d763cef2SBarry Smith }
2676d763cef2SBarry Smith 
26774a2ae208SSatish Balay #undef __FUNCT__
26784a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2679d8e5e3e6SSatish Balay /*@
2680d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2681d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2682d763cef2SBarry Smith 
2683d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2684d763cef2SBarry Smith 
2685d763cef2SBarry Smith    Input Parameter:
2686d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2687d763cef2SBarry Smith 
2688d763cef2SBarry Smith    Output Parameter:
2689d763cef2SBarry Smith .  snes - the nonlinear solver context
2690d763cef2SBarry Smith 
2691d763cef2SBarry Smith    Notes:
2692d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2693d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
269494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2695d763cef2SBarry Smith 
2696d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
26970298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2698d763cef2SBarry Smith 
2699d763cef2SBarry Smith    Level: beginner
2700d763cef2SBarry Smith 
2701d763cef2SBarry Smith .keywords: timestep, get, SNES
2702d763cef2SBarry Smith @*/
27037087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2704d763cef2SBarry Smith {
2705d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2706d372ba47SLisandro Dalcin 
2707d763cef2SBarry Smith   PetscFunctionBegin;
27080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27094482741eSBarry Smith   PetscValidPointer(snes,2);
2710d372ba47SLisandro Dalcin   if (!ts->snes) {
2711ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
27120298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27133bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2714d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2715496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27169e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27179e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27189e2a6581SJed Brown     }
2719d372ba47SLisandro Dalcin   }
2720d763cef2SBarry Smith   *snes = ts->snes;
2721d763cef2SBarry Smith   PetscFunctionReturn(0);
2722d763cef2SBarry Smith }
2723d763cef2SBarry Smith 
27244a2ae208SSatish Balay #undef __FUNCT__
2725deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2726deb2cd25SJed Brown /*@
2727deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2728deb2cd25SJed Brown 
2729deb2cd25SJed Brown    Collective
2730deb2cd25SJed Brown 
2731deb2cd25SJed Brown    Input Parameter:
2732deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2733deb2cd25SJed Brown -  snes - the nonlinear solver context
2734deb2cd25SJed Brown 
2735deb2cd25SJed Brown    Notes:
2736deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2737deb2cd25SJed Brown 
2738deb2cd25SJed Brown    Level: developer
2739deb2cd25SJed Brown 
2740deb2cd25SJed Brown .keywords: timestep, set, SNES
2741deb2cd25SJed Brown @*/
2742deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2743deb2cd25SJed Brown {
2744deb2cd25SJed Brown   PetscErrorCode ierr;
2745d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2746deb2cd25SJed Brown 
2747deb2cd25SJed Brown   PetscFunctionBegin;
2748deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2749deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2750deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2751deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2752bbd56ea5SKarl Rupp 
2753deb2cd25SJed Brown   ts->snes = snes;
2754bbd56ea5SKarl Rupp 
27550298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27560298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2757740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27580298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2759740132f1SEmil Constantinescu   }
2760deb2cd25SJed Brown   PetscFunctionReturn(0);
2761deb2cd25SJed Brown }
2762deb2cd25SJed Brown 
2763deb2cd25SJed Brown #undef __FUNCT__
276494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2765d8e5e3e6SSatish Balay /*@
276694b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2767d763cef2SBarry Smith    a TS (timestepper) context.
2768d763cef2SBarry Smith 
276994b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2770d763cef2SBarry Smith 
2771d763cef2SBarry Smith    Input Parameter:
2772d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2773d763cef2SBarry Smith 
2774d763cef2SBarry Smith    Output Parameter:
277594b7f48cSBarry Smith .  ksp - the nonlinear solver context
2776d763cef2SBarry Smith 
2777d763cef2SBarry Smith    Notes:
277894b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2779d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2780d763cef2SBarry Smith    KSP and PC contexts as well.
2781d763cef2SBarry Smith 
278294b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
27830298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2784d763cef2SBarry Smith 
2785d763cef2SBarry Smith    Level: beginner
2786d763cef2SBarry Smith 
278794b7f48cSBarry Smith .keywords: timestep, get, KSP
2788d763cef2SBarry Smith @*/
27897087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2790d763cef2SBarry Smith {
2791d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2792089b2837SJed Brown   SNES           snes;
2793d372ba47SLisandro Dalcin 
2794d763cef2SBarry Smith   PetscFunctionBegin;
27950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27964482741eSBarry Smith   PetscValidPointer(ksp,2);
279717186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2798e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2799089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2800089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2801d763cef2SBarry Smith   PetscFunctionReturn(0);
2802d763cef2SBarry Smith }
2803d763cef2SBarry Smith 
2804d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2805d763cef2SBarry Smith 
28064a2ae208SSatish Balay #undef __FUNCT__
2807adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2808adb62b0dSMatthew Knepley /*@
2809adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2810adb62b0dSMatthew Knepley    maximum time for iteration.
2811adb62b0dSMatthew Knepley 
28123f9fe445SBarry Smith    Not Collective
2813adb62b0dSMatthew Knepley 
2814adb62b0dSMatthew Knepley    Input Parameters:
2815adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
28160298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
28170298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2818adb62b0dSMatthew Knepley 
2819adb62b0dSMatthew Knepley    Level: intermediate
2820adb62b0dSMatthew Knepley 
2821adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2822adb62b0dSMatthew Knepley @*/
28237087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2824adb62b0dSMatthew Knepley {
2825adb62b0dSMatthew Knepley   PetscFunctionBegin;
28260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2827abc0a331SBarry Smith   if (maxsteps) {
28284482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2829adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2830adb62b0dSMatthew Knepley   }
2831abc0a331SBarry Smith   if (maxtime) {
28324482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2833adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2834adb62b0dSMatthew Knepley   }
2835adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2836adb62b0dSMatthew Knepley }
2837adb62b0dSMatthew Knepley 
2838adb62b0dSMatthew Knepley #undef __FUNCT__
28394a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2840d763cef2SBarry Smith /*@
2841d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2842d763cef2SBarry Smith    maximum time for iteration.
2843d763cef2SBarry Smith 
28443f9fe445SBarry Smith    Logically Collective on TS
2845d763cef2SBarry Smith 
2846d763cef2SBarry Smith    Input Parameters:
2847d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2848d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2849d763cef2SBarry Smith -  maxtime - final time to iterate to
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith    Options Database Keys:
2852d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
28533bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2854d763cef2SBarry Smith 
2855d763cef2SBarry Smith    Notes:
2856d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2857d763cef2SBarry Smith 
2858d763cef2SBarry Smith    Level: intermediate
2859d763cef2SBarry Smith 
2860d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2861a43b19c4SJed Brown 
2862a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2863d763cef2SBarry Smith @*/
28647087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2865d763cef2SBarry Smith {
2866d763cef2SBarry Smith   PetscFunctionBegin;
28670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2868c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2869c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
287039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
287139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2872d763cef2SBarry Smith   PetscFunctionReturn(0);
2873d763cef2SBarry Smith }
2874d763cef2SBarry Smith 
28754a2ae208SSatish Balay #undef __FUNCT__
28764a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2877d763cef2SBarry Smith /*@
2878d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2879d763cef2SBarry Smith    for use by the TS routines.
2880d763cef2SBarry Smith 
28813f9fe445SBarry Smith    Logically Collective on TS and Vec
2882d763cef2SBarry Smith 
2883d763cef2SBarry Smith    Input Parameters:
2884d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
28850910c330SBarry Smith -  u - the solution vector
2886d763cef2SBarry Smith 
2887d763cef2SBarry Smith    Level: beginner
2888d763cef2SBarry Smith 
2889d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2890d763cef2SBarry Smith @*/
28910910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2892d763cef2SBarry Smith {
28938737fe31SLisandro Dalcin   PetscErrorCode ierr;
2894496e6a7aSJed Brown   DM             dm;
28958737fe31SLisandro Dalcin 
2896d763cef2SBarry Smith   PetscFunctionBegin;
28970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28980910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
28990910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
29006bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
29010910c330SBarry Smith   ts->vec_sol = u;
2902bbd56ea5SKarl Rupp 
2903496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
29040910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2905d763cef2SBarry Smith   PetscFunctionReturn(0);
2906d763cef2SBarry Smith }
2907d763cef2SBarry Smith 
2908e74ef692SMatthew Knepley #undef __FUNCT__
290973b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
291073b18844SBarry Smith /*@
291173b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
291273b18844SBarry Smith 
291373b18844SBarry Smith    Logically Collective on TS
291473b18844SBarry Smith 
291573b18844SBarry Smith    Input Parameters:
291673b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
291773b18844SBarry Smith .  steps - number of steps to use
291873b18844SBarry Smith 
291973b18844SBarry Smith    Level: intermediate
292073b18844SBarry Smith 
292173b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
292273b18844SBarry Smith           so as to integrate back to less than the original timestep
292373b18844SBarry Smith 
292473b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
292573b18844SBarry Smith 
292673b18844SBarry Smith .seealso: TSSetExactFinalTime()
292773b18844SBarry Smith @*/
292873b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
292973b18844SBarry Smith {
293073b18844SBarry Smith   PetscFunctionBegin;
293173b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
293273b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
293373b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
293473b18844SBarry 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");
293573b18844SBarry Smith   ts->adjoint_max_steps = steps;
293673b18844SBarry Smith   PetscFunctionReturn(0);
293773b18844SBarry Smith }
293873b18844SBarry Smith 
293973b18844SBarry Smith #undef __FUNCT__
2940dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients"
2941c235aa8dSHong Zhang /*@
2942dfb21088SHong 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
29430cf82b59SBarry Smith       for use by the TSAdjoint routines.
2944c235aa8dSHong Zhang 
2945c235aa8dSHong Zhang    Logically Collective on TS and Vec
2946c235aa8dSHong Zhang 
2947c235aa8dSHong Zhang    Input Parameters:
2948c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2949abc2977eSBarry 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
2950abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2951c235aa8dSHong Zhang 
2952c235aa8dSHong Zhang    Level: beginner
2953c235aa8dSHong Zhang 
2954abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
29550cf82b59SBarry Smith 
2956c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2957c235aa8dSHong Zhang @*/
2958dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2959c235aa8dSHong Zhang {
2960c235aa8dSHong Zhang   PetscFunctionBegin;
2961c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2962abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2963abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2964abc2977eSBarry Smith   ts->vecs_sensip = mu;
2965dfb21088SHong 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");
2966abc2977eSBarry Smith   ts->numcost  = numcost;
2967ad8e2604SHong Zhang   PetscFunctionReturn(0);
2968ad8e2604SHong Zhang }
2969ad8e2604SHong Zhang 
2970ad8e2604SHong Zhang #undef __FUNCT__
29715bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2972ad8e2604SHong Zhang /*@C
29730cf82b59SBarry 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.
2974ad8e2604SHong Zhang 
2975ad8e2604SHong Zhang   Logically Collective on TS
2976ad8e2604SHong Zhang 
2977ad8e2604SHong Zhang   Input Parameters:
2978ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2979ad8e2604SHong Zhang - func - The function
2980ad8e2604SHong Zhang 
2981ad8e2604SHong Zhang   Calling sequence of func:
29820cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
298305755b9cSHong Zhang +   t - current timestep
29840cf82b59SBarry Smith .   y - input vector (current ODE solution)
298505755b9cSHong Zhang .   A - output matrix
298605755b9cSHong Zhang -   ctx - [optional] user-defined function context
2987ad8e2604SHong Zhang 
2988ad8e2604SHong Zhang   Level: intermediate
2989ad8e2604SHong Zhang 
29900cf82b59SBarry 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
29910cf82b59SBarry Smith 
2992ad8e2604SHong Zhang .keywords: TS, sensitivity
2993ad8e2604SHong Zhang .seealso:
2994ad8e2604SHong Zhang @*/
29955bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2996ad8e2604SHong Zhang {
2997ad8e2604SHong Zhang   PetscErrorCode ierr;
2998ad8e2604SHong Zhang 
2999ad8e2604SHong Zhang   PetscFunctionBegin;
3000ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3001ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
3002ad8e2604SHong Zhang 
3003ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
3004ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
3005ad8e2604SHong Zhang   if(Amat) {
3006ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
3007ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
3008ad8e2604SHong Zhang     ts->Jacp = Amat;
3009ad8e2604SHong Zhang   }
3010ad8e2604SHong Zhang   PetscFunctionReturn(0);
3011ad8e2604SHong Zhang }
3012ad8e2604SHong Zhang 
3013ad8e2604SHong Zhang #undef __FUNCT__
30145bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
30150cf82b59SBarry Smith /*@C
30165bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
3017ad8e2604SHong Zhang 
3018ad8e2604SHong Zhang   Collective on TS
3019ad8e2604SHong Zhang 
3020ad8e2604SHong Zhang   Input Parameters:
3021ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
3022ad8e2604SHong Zhang 
3023ad8e2604SHong Zhang   Level: developer
3024ad8e2604SHong Zhang 
3025ad8e2604SHong Zhang .keywords: TS, sensitivity
30265bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
3027ad8e2604SHong Zhang @*/
30285bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
3029ad8e2604SHong Zhang {
3030ad8e2604SHong Zhang   PetscErrorCode ierr;
3031ad8e2604SHong Zhang 
3032ad8e2604SHong Zhang   PetscFunctionBegin;
3033ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3034ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3035ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
3036ad8e2604SHong Zhang 
3037ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
3038ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
3039ad8e2604SHong Zhang   PetscStackPop;
3040c235aa8dSHong Zhang   PetscFunctionReturn(0);
3041c235aa8dSHong Zhang }
3042c235aa8dSHong Zhang 
3043c235aa8dSHong Zhang #undef __FUNCT__
3044dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand"
30456fd0887fSHong Zhang /*@C
3046dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
30476fd0887fSHong Zhang 
30486fd0887fSHong Zhang     Logically Collective on TS
30496fd0887fSHong Zhang 
30506fd0887fSHong Zhang     Input Parameters:
30516fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
3052abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
30530cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
3054b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
3055b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
3056feaa1ddbSSatish Balay .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
3057b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
30586fd0887fSHong Zhang 
30590cf82b59SBarry Smith     Calling sequence of rf:
30600cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
30616fd0887fSHong Zhang 
30626fd0887fSHong Zhang +   t - current timestep
30630cf82b59SBarry Smith .   y - input vector
30640cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
30656fd0887fSHong Zhang -   ctx - [optional] user-defined function context
30666fd0887fSHong Zhang 
3067b612ec54SBarry Smith    Calling sequence of drdyf:
30680cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
3069b612ec54SBarry Smith 
3070b612ec54SBarry Smith    Calling sequence of drdpf:
30710cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
3072b612ec54SBarry Smith 
3073b612ec54SBarry Smith     Level: intermediate
30746fd0887fSHong Zhang 
3075abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
30760cf82b59SBarry Smith 
30776fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
30786fd0887fSHong Zhang 
3079dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
30806fd0887fSHong Zhang @*/
3081dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
3082d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
3083b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
3084b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
30856fd0887fSHong Zhang {
30866fd0887fSHong Zhang   PetscErrorCode ierr;
30876fd0887fSHong Zhang 
30886fd0887fSHong Zhang   PetscFunctionBegin;
30896fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3090dfb21088SHong 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()");
3091c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
30926fd0887fSHong Zhang 
3093b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
3094abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
30952c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
30960cf82b59SBarry Smith   ts->costintegrand    = rf;
309705755b9cSHong Zhang   ts->costintegrandctx = ctx;
3098b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
3099b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
31006fd0887fSHong Zhang   PetscFunctionReturn(0);
31016fd0887fSHong Zhang }
31026fd0887fSHong Zhang 
31036fd0887fSHong Zhang #undef __FUNCT__
3104dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral"
310536eaed60SHong Zhang /*@
3106dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
310736eaed60SHong Zhang    It is valid to call the routine after a backward run.
310836eaed60SHong Zhang 
310936eaed60SHong Zhang    Not Collective
311036eaed60SHong Zhang 
311136eaed60SHong Zhang    Input Parameter:
311236eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
311336eaed60SHong Zhang 
311436eaed60SHong Zhang    Output Parameter:
31152c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
311636eaed60SHong Zhang 
311736eaed60SHong Zhang    Level: intermediate
311836eaed60SHong Zhang 
3119dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
312036eaed60SHong Zhang 
312136eaed60SHong Zhang .keywords: TS, sensitivity analysis
312236eaed60SHong Zhang @*/
3123dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
312436eaed60SHong Zhang {
312536eaed60SHong Zhang   PetscFunctionBegin;
312636eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
312736eaed60SHong Zhang   PetscValidPointer(v,2);
31282c39e106SBarry Smith   *v = ts->vec_costintegral;
312936eaed60SHong Zhang   PetscFunctionReturn(0);
313036eaed60SHong Zhang }
313136eaed60SHong Zhang 
313236eaed60SHong Zhang #undef __FUNCT__
3133d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
31346fd0887fSHong Zhang /*@
31352c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
31366fd0887fSHong Zhang 
31376fd0887fSHong Zhang    Input Parameters:
31386fd0887fSHong Zhang +  ts - the TS context
31396fd0887fSHong Zhang .  t - current time
31400cf82b59SBarry Smith -  y - state vector, i.e. current solution
31416fd0887fSHong Zhang 
31426fd0887fSHong Zhang    Output Parameter:
3143abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
31446fd0887fSHong Zhang 
31456fd0887fSHong Zhang    Note:
31466fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
31476fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
31486fd0887fSHong Zhang 
31496fd0887fSHong Zhang    Level: developer
31506fd0887fSHong Zhang 
31516fd0887fSHong Zhang .keywords: TS, compute
31526fd0887fSHong Zhang 
3153dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
31546fd0887fSHong Zhang @*/
31550cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
31566fd0887fSHong Zhang {
31576fd0887fSHong Zhang   PetscErrorCode ierr;
31586fd0887fSHong Zhang 
31596fd0887fSHong Zhang   PetscFunctionBegin;
31606fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31610cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
31626fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
31636fd0887fSHong Zhang 
31640cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
3165e4680132SHong Zhang   if (ts->costintegrand) {
3166e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
31670cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
31686fd0887fSHong Zhang     PetscStackPop;
31696fd0887fSHong Zhang   } else {
31706fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
31716fd0887fSHong Zhang   }
31726fd0887fSHong Zhang 
31730cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
31746fd0887fSHong Zhang   PetscFunctionReturn(0);
31756fd0887fSHong Zhang }
31766fd0887fSHong Zhang 
31776fd0887fSHong Zhang #undef __FUNCT__
3178d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
317905755b9cSHong Zhang /*@
3180d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
318105755b9cSHong Zhang 
318205755b9cSHong Zhang   Collective on TS
318305755b9cSHong Zhang 
318405755b9cSHong Zhang   Input Parameters:
318505755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
318605755b9cSHong Zhang 
318705755b9cSHong Zhang   Notes:
3188d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
318905755b9cSHong Zhang   so most users would not generally call this routine themselves.
319005755b9cSHong Zhang 
319105755b9cSHong Zhang   Level: developer
319205755b9cSHong Zhang 
319305755b9cSHong Zhang .keywords: TS, sensitivity
3194d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
319505755b9cSHong Zhang @*/
31960cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
319705755b9cSHong Zhang {
319805755b9cSHong Zhang   PetscErrorCode ierr;
319905755b9cSHong Zhang 
320005755b9cSHong Zhang   PetscFunctionBegin;
320105755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32020cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
320305755b9cSHong Zhang 
320405755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
32050cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
320605755b9cSHong Zhang   PetscStackPop;
320705755b9cSHong Zhang   PetscFunctionReturn(0);
320805755b9cSHong Zhang }
320905755b9cSHong Zhang 
321005755b9cSHong Zhang #undef __FUNCT__
3211d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
321205755b9cSHong Zhang /*@
3213d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
321405755b9cSHong Zhang 
321505755b9cSHong Zhang   Collective on TS
321605755b9cSHong Zhang 
321705755b9cSHong Zhang   Input Parameters:
321805755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
321905755b9cSHong Zhang 
322005755b9cSHong Zhang   Notes:
322105755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
322205755b9cSHong Zhang   so most users would not generally call this routine themselves.
322305755b9cSHong Zhang 
322405755b9cSHong Zhang   Level: developer
322505755b9cSHong Zhang 
322605755b9cSHong Zhang .keywords: TS, sensitivity
3227d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
322805755b9cSHong Zhang @*/
32290cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
323005755b9cSHong Zhang {
323105755b9cSHong Zhang   PetscErrorCode ierr;
323205755b9cSHong Zhang 
323305755b9cSHong Zhang   PetscFunctionBegin;
323405755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32350cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
323605755b9cSHong Zhang 
323705755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
32380cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
323905755b9cSHong Zhang   PetscStackPop;
324005755b9cSHong Zhang   PetscFunctionReturn(0);
324105755b9cSHong Zhang }
324205755b9cSHong Zhang 
324305755b9cSHong Zhang #undef __FUNCT__
3244e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
3245ac226902SBarry Smith /*@C
3246000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
32473f2090d5SJed Brown   called once at the beginning of each time step.
3248000e7ae3SMatthew Knepley 
32493f9fe445SBarry Smith   Logically Collective on TS
3250000e7ae3SMatthew Knepley 
3251000e7ae3SMatthew Knepley   Input Parameters:
3252000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3253000e7ae3SMatthew Knepley - func - The function
3254000e7ae3SMatthew Knepley 
3255000e7ae3SMatthew Knepley   Calling sequence of func:
3256000e7ae3SMatthew Knepley . func (TS ts);
3257000e7ae3SMatthew Knepley 
3258000e7ae3SMatthew Knepley   Level: intermediate
3259000e7ae3SMatthew Knepley 
3260b8123daeSJed Brown   Note:
3261b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
3262b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
3263b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
3264b8123daeSJed Brown 
3265000e7ae3SMatthew Knepley .keywords: TS, timestep
32669be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
3267000e7ae3SMatthew Knepley @*/
32687087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3269000e7ae3SMatthew Knepley {
3270000e7ae3SMatthew Knepley   PetscFunctionBegin;
32710700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3272ae60f76fSBarry Smith   ts->prestep = func;
3273000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3274000e7ae3SMatthew Knepley }
3275000e7ae3SMatthew Knepley 
3276e74ef692SMatthew Knepley #undef __FUNCT__
32773f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
327809ee8438SJed Brown /*@
32793f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32803f2090d5SJed Brown 
32813f2090d5SJed Brown   Collective on TS
32823f2090d5SJed Brown 
32833f2090d5SJed Brown   Input Parameters:
32843f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32853f2090d5SJed Brown 
32863f2090d5SJed Brown   Notes:
32873f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32883f2090d5SJed Brown   so most users would not generally call this routine themselves.
32893f2090d5SJed Brown 
32903f2090d5SJed Brown   Level: developer
32913f2090d5SJed Brown 
32923f2090d5SJed Brown .keywords: TS, timestep
32939be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32943f2090d5SJed Brown @*/
32957087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32963f2090d5SJed Brown {
32973f2090d5SJed Brown   PetscErrorCode ierr;
32983f2090d5SJed Brown 
32993f2090d5SJed Brown   PetscFunctionBegin;
33000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3301ae60f76fSBarry Smith   if (ts->prestep) {
3302ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
3303312ce896SJed Brown   }
33043f2090d5SJed Brown   PetscFunctionReturn(0);
33053f2090d5SJed Brown }
33063f2090d5SJed Brown 
33073f2090d5SJed Brown #undef __FUNCT__
3308b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
3309b8123daeSJed Brown /*@C
3310b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3311b8123daeSJed Brown   called once at the beginning of each stage.
3312b8123daeSJed Brown 
3313b8123daeSJed Brown   Logically Collective on TS
3314b8123daeSJed Brown 
3315b8123daeSJed Brown   Input Parameters:
3316b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3317b8123daeSJed Brown - func - The function
3318b8123daeSJed Brown 
3319b8123daeSJed Brown   Calling sequence of func:
3320b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3321b8123daeSJed Brown 
3322b8123daeSJed Brown   Level: intermediate
3323b8123daeSJed Brown 
3324b8123daeSJed Brown   Note:
3325b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
3326b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
3327b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3328b8123daeSJed Brown 
3329b8123daeSJed Brown .keywords: TS, timestep
33309be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3331b8123daeSJed Brown @*/
3332b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3333b8123daeSJed Brown {
3334b8123daeSJed Brown   PetscFunctionBegin;
3335b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3336ae60f76fSBarry Smith   ts->prestage = func;
3337b8123daeSJed Brown   PetscFunctionReturn(0);
3338b8123daeSJed Brown }
3339b8123daeSJed Brown 
3340b8123daeSJed Brown #undef __FUNCT__
33419be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
33429be3e283SDebojyoti Ghosh /*@C
33439be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
33449be3e283SDebojyoti Ghosh   called once at the end of each stage.
33459be3e283SDebojyoti Ghosh 
33469be3e283SDebojyoti Ghosh   Logically Collective on TS
33479be3e283SDebojyoti Ghosh 
33489be3e283SDebojyoti Ghosh   Input Parameters:
33499be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
33509be3e283SDebojyoti Ghosh - func - The function
33519be3e283SDebojyoti Ghosh 
33529be3e283SDebojyoti Ghosh   Calling sequence of func:
33539be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33549be3e283SDebojyoti Ghosh 
33559be3e283SDebojyoti Ghosh   Level: intermediate
33569be3e283SDebojyoti Ghosh 
33579be3e283SDebojyoti Ghosh   Note:
33589be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
33599be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
33609be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33619be3e283SDebojyoti Ghosh 
33629be3e283SDebojyoti Ghosh .keywords: TS, timestep
33639be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33649be3e283SDebojyoti Ghosh @*/
33659be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33669be3e283SDebojyoti Ghosh {
33679be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33689be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33699be3e283SDebojyoti Ghosh   ts->poststage = func;
33709be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33719be3e283SDebojyoti Ghosh }
33729be3e283SDebojyoti Ghosh 
33739be3e283SDebojyoti Ghosh #undef __FUNCT__
3374c688d042SShri Abhyankar #define __FUNCT__ "TSSetPostEvaluate"
3375c688d042SShri Abhyankar /*@C
3376c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3377c688d042SShri Abhyankar   called once at the end of each step evaluation.
3378c688d042SShri Abhyankar 
3379c688d042SShri Abhyankar   Logically Collective on TS
3380c688d042SShri Abhyankar 
3381c688d042SShri Abhyankar   Input Parameters:
3382c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3383c688d042SShri Abhyankar - func - The function
3384c688d042SShri Abhyankar 
3385c688d042SShri Abhyankar   Calling sequence of func:
3386c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3387c688d042SShri Abhyankar 
3388c688d042SShri Abhyankar   Level: intermediate
3389c688d042SShri Abhyankar 
3390c688d042SShri Abhyankar   Note:
33911785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33921785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3393e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3394e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3395e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3396c688d042SShri Abhyankar 
3397c688d042SShri Abhyankar .keywords: TS, timestep
3398c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3399c688d042SShri Abhyankar @*/
3400c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3401c688d042SShri Abhyankar {
3402c688d042SShri Abhyankar   PetscFunctionBegin;
3403c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3404c688d042SShri Abhyankar   ts->postevaluate = func;
3405c688d042SShri Abhyankar   PetscFunctionReturn(0);
3406c688d042SShri Abhyankar }
3407c688d042SShri Abhyankar 
3408c688d042SShri Abhyankar #undef __FUNCT__
3409b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
3410b8123daeSJed Brown /*@
3411b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3412b8123daeSJed Brown 
3413b8123daeSJed Brown   Collective on TS
3414b8123daeSJed Brown 
3415b8123daeSJed Brown   Input Parameters:
3416b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
34179be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3418b8123daeSJed Brown 
3419b8123daeSJed Brown   Notes:
3420b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3421b8123daeSJed Brown   most users would not generally call this routine themselves.
3422b8123daeSJed Brown 
3423b8123daeSJed Brown   Level: developer
3424b8123daeSJed Brown 
3425b8123daeSJed Brown .keywords: TS, timestep
34269be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3427b8123daeSJed Brown @*/
3428b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3429b8123daeSJed Brown {
3430b8123daeSJed Brown   PetscErrorCode ierr;
3431b8123daeSJed Brown 
3432b8123daeSJed Brown   PetscFunctionBegin;
3433b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3434ae60f76fSBarry Smith   if (ts->prestage) {
3435ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3436b8123daeSJed Brown   }
3437b8123daeSJed Brown   PetscFunctionReturn(0);
3438b8123daeSJed Brown }
3439b8123daeSJed Brown 
3440b8123daeSJed Brown #undef __FUNCT__
34419be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
34429be3e283SDebojyoti Ghosh /*@
34439be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
34449be3e283SDebojyoti Ghosh 
34459be3e283SDebojyoti Ghosh   Collective on TS
34469be3e283SDebojyoti Ghosh 
34479be3e283SDebojyoti Ghosh   Input Parameters:
34489be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
34499be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
34509be3e283SDebojyoti Ghosh   stageindex  - Stage number
34519be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
34529be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
34539be3e283SDebojyoti Ghosh 
34549be3e283SDebojyoti Ghosh   Notes:
34559be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
34569be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
34579be3e283SDebojyoti Ghosh 
34589be3e283SDebojyoti Ghosh   Level: developer
34599be3e283SDebojyoti Ghosh 
34609be3e283SDebojyoti Ghosh .keywords: TS, timestep
34619be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34629be3e283SDebojyoti Ghosh @*/
34639be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34649be3e283SDebojyoti Ghosh {
34659be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
34669be3e283SDebojyoti Ghosh 
34679be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34689be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34694beae5d8SLisandro Dalcin   if (ts->poststage) {
34709be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34719be3e283SDebojyoti Ghosh   }
34729be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34739be3e283SDebojyoti Ghosh }
34749be3e283SDebojyoti Ghosh 
34759be3e283SDebojyoti Ghosh #undef __FUNCT__
3476c688d042SShri Abhyankar #define __FUNCT__ "TSPostEvaluate"
3477c688d042SShri Abhyankar /*@
3478c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3479c688d042SShri Abhyankar 
3480c688d042SShri Abhyankar   Collective on TS
3481c688d042SShri Abhyankar 
3482c688d042SShri Abhyankar   Input Parameters:
3483c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3484c688d042SShri Abhyankar 
3485c688d042SShri Abhyankar   Notes:
3486c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3487c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3488c688d042SShri Abhyankar 
3489c688d042SShri Abhyankar   Level: developer
3490c688d042SShri Abhyankar 
3491c688d042SShri Abhyankar .keywords: TS, timestep
3492c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3493c688d042SShri Abhyankar @*/
3494c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3495c688d042SShri Abhyankar {
3496c688d042SShri Abhyankar   PetscErrorCode ierr;
3497c688d042SShri Abhyankar 
3498c688d042SShri Abhyankar   PetscFunctionBegin;
3499c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3500c688d042SShri Abhyankar   if (ts->postevaluate) {
3501c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3502c688d042SShri Abhyankar   }
3503c688d042SShri Abhyankar   PetscFunctionReturn(0);
3504c688d042SShri Abhyankar }
3505c688d042SShri Abhyankar 
3506c688d042SShri Abhyankar #undef __FUNCT__
3507e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
3508ac226902SBarry Smith /*@C
3509000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
35103f2090d5SJed Brown   called once at the end of each time step.
3511000e7ae3SMatthew Knepley 
35123f9fe445SBarry Smith   Logically Collective on TS
3513000e7ae3SMatthew Knepley 
3514000e7ae3SMatthew Knepley   Input Parameters:
3515000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3516000e7ae3SMatthew Knepley - func - The function
3517000e7ae3SMatthew Knepley 
3518000e7ae3SMatthew Knepley   Calling sequence of func:
3519b8123daeSJed Brown $ func (TS ts);
3520000e7ae3SMatthew Knepley 
35211785ff2aSShri Abhyankar   Notes:
35221785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
35231785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
35241785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
35251785ff2aSShri Abhyankar 
3526000e7ae3SMatthew Knepley   Level: intermediate
3527000e7ae3SMatthew Knepley 
3528000e7ae3SMatthew Knepley .keywords: TS, timestep
35291785ff2aSShri Abhyankar .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
3530000e7ae3SMatthew Knepley @*/
35317087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3532000e7ae3SMatthew Knepley {
3533000e7ae3SMatthew Knepley   PetscFunctionBegin;
35340700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3535ae60f76fSBarry Smith   ts->poststep = func;
3536000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3537000e7ae3SMatthew Knepley }
3538000e7ae3SMatthew Knepley 
3539e74ef692SMatthew Knepley #undef __FUNCT__
35403f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
354109ee8438SJed Brown /*@
35423f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
35433f2090d5SJed Brown 
35443f2090d5SJed Brown   Collective on TS
35453f2090d5SJed Brown 
35463f2090d5SJed Brown   Input Parameters:
35473f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
35483f2090d5SJed Brown 
35493f2090d5SJed Brown   Notes:
35503f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
35513f2090d5SJed Brown   so most users would not generally call this routine themselves.
35523f2090d5SJed Brown 
35533f2090d5SJed Brown   Level: developer
35543f2090d5SJed Brown 
35553f2090d5SJed Brown .keywords: TS, timestep
35563f2090d5SJed Brown @*/
35577087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35583f2090d5SJed Brown {
35593f2090d5SJed Brown   PetscErrorCode ierr;
35603f2090d5SJed Brown 
35613f2090d5SJed Brown   PetscFunctionBegin;
35620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3563ae60f76fSBarry Smith   if (ts->poststep) {
3564ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
356572ac3e02SJed Brown   }
35663f2090d5SJed Brown   PetscFunctionReturn(0);
35673f2090d5SJed Brown }
35683f2090d5SJed Brown 
3569d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3570d763cef2SBarry Smith 
35714a2ae208SSatish Balay #undef __FUNCT__
3572a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
3573d763cef2SBarry Smith /*@C
3574a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3575d763cef2SBarry Smith    timestep to display the iteration's  progress.
3576d763cef2SBarry Smith 
35773f9fe445SBarry Smith    Logically Collective on TS
3578d763cef2SBarry Smith 
3579d763cef2SBarry Smith    Input Parameters:
3580d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3581e213d8f1SJed Brown .  monitor - monitoring routine
3582329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35830298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3584b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35850298fd71SBarry Smith           (may be NULL)
3586d763cef2SBarry Smith 
3587e213d8f1SJed Brown    Calling sequence of monitor:
35880910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3589d763cef2SBarry Smith 
3590d763cef2SBarry Smith +    ts - the TS context
359163e21af5SBarry 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)
35921f06c33eSBarry Smith .    time - current time
35930910c330SBarry Smith .    u - current iterate
3594d763cef2SBarry Smith -    mctx - [optional] monitoring context
3595d763cef2SBarry Smith 
3596d763cef2SBarry Smith    Notes:
3597d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3598d763cef2SBarry Smith    already has been loaded.
3599d763cef2SBarry Smith 
3600025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3601025f1a04SBarry Smith 
3602d763cef2SBarry Smith    Level: intermediate
3603d763cef2SBarry Smith 
3604d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3605d763cef2SBarry Smith 
3606a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3607d763cef2SBarry Smith @*/
3608c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3609d763cef2SBarry Smith {
361078064530SBarry Smith   PetscErrorCode ierr;
361178064530SBarry Smith   PetscInt       i;
361278064530SBarry Smith   PetscBool      identical;
361378064530SBarry Smith 
3614d763cef2SBarry Smith   PetscFunctionBegin;
36150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
361678064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
361778064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
361878064530SBarry Smith     if (identical) PetscFunctionReturn(0);
361978064530SBarry Smith   }
362017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3621d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
36228704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3623d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3624d763cef2SBarry Smith   PetscFunctionReturn(0);
3625d763cef2SBarry Smith }
3626d763cef2SBarry Smith 
36274a2ae208SSatish Balay #undef __FUNCT__
3628a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
3629d763cef2SBarry Smith /*@C
3630a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3631d763cef2SBarry Smith 
36323f9fe445SBarry Smith    Logically Collective on TS
3633d763cef2SBarry Smith 
3634d763cef2SBarry Smith    Input Parameters:
3635d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3636d763cef2SBarry Smith 
3637d763cef2SBarry Smith    Notes:
3638d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3639d763cef2SBarry Smith 
3640d763cef2SBarry Smith    Level: intermediate
3641d763cef2SBarry Smith 
3642d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3643d763cef2SBarry Smith 
3644a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3645d763cef2SBarry Smith @*/
36467087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3647d763cef2SBarry Smith {
3648d952e501SBarry Smith   PetscErrorCode ierr;
3649d952e501SBarry Smith   PetscInt       i;
3650d952e501SBarry Smith 
3651d763cef2SBarry Smith   PetscFunctionBegin;
36520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3653d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36548704b422SBarry Smith     if (ts->monitordestroy[i]) {
36558704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3656d952e501SBarry Smith     }
3657d952e501SBarry Smith   }
3658d763cef2SBarry Smith   ts->numbermonitors = 0;
3659d763cef2SBarry Smith   PetscFunctionReturn(0);
3660d763cef2SBarry Smith }
3661d763cef2SBarry Smith 
36624a2ae208SSatish Balay #undef __FUNCT__
3663a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
3664721cd6eeSBarry Smith /*@C
3665721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36665516499fSSatish Balay 
36675516499fSSatish Balay    Level: intermediate
366841251cbbSSatish Balay 
36695516499fSSatish Balay .keywords: TS, set, monitor
36705516499fSSatish Balay 
367163e21af5SBarry Smith .seealso:  TSMonitorSet()
367241251cbbSSatish Balay @*/
3673721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3674d763cef2SBarry Smith {
3675dfbe8321SBarry Smith   PetscErrorCode ierr;
3676721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
367741aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3678d132466eSBarry Smith 
3679d763cef2SBarry Smith   PetscFunctionBegin;
36804d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
368141aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
368241aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3683721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
368441aca3d6SBarry Smith   if (iascii) {
3685649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
368663e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
368763e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
368863e21af5SBarry Smith     } else {
36898392e04aSShri 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);
369063e21af5SBarry Smith     }
3691649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
369241aca3d6SBarry Smith   } else if (ibinary) {
369341aca3d6SBarry Smith     PetscMPIInt rank;
369441aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
369541aca3d6SBarry Smith     if (!rank) {
3696450a797fSBarry Smith       PetscBool skipHeader;
3697450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3698450a797fSBarry Smith 
3699450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3700450a797fSBarry Smith       if (!skipHeader) {
3701450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3702450a797fSBarry Smith        }
370341aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
370441aca3d6SBarry Smith     } else {
370541aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
370641aca3d6SBarry Smith     }
370741aca3d6SBarry Smith   }
3708721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3709d763cef2SBarry Smith   PetscFunctionReturn(0);
3710d763cef2SBarry Smith }
3711d763cef2SBarry Smith 
37124a2ae208SSatish Balay #undef __FUNCT__
37139110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet"
37149110b2e7SHong Zhang /*@C
37159110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
37169110b2e7SHong Zhang    timestep to display the iteration's  progress.
37179110b2e7SHong Zhang 
37189110b2e7SHong Zhang    Logically Collective on TS
37199110b2e7SHong Zhang 
37209110b2e7SHong Zhang    Input Parameters:
37219110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
37229110b2e7SHong Zhang .  adjointmonitor - monitoring routine
37239110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
37249110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
37259110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
37269110b2e7SHong Zhang           (may be NULL)
37279110b2e7SHong Zhang 
37289110b2e7SHong Zhang    Calling sequence of monitor:
37299110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
37309110b2e7SHong Zhang 
37319110b2e7SHong Zhang +    ts - the TS context
37329110b2e7SHong 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
37339110b2e7SHong Zhang                                been interpolated to)
37349110b2e7SHong Zhang .    time - current time
37359110b2e7SHong Zhang .    u - current iterate
37369110b2e7SHong Zhang .    numcost - number of cost functionos
37379110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
37389110b2e7SHong Zhang .    mu - sensitivities to parameters
37399110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
37409110b2e7SHong Zhang 
37419110b2e7SHong Zhang    Notes:
37429110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
37439110b2e7SHong Zhang    already has been loaded.
37449110b2e7SHong Zhang 
37459110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
37469110b2e7SHong Zhang 
37479110b2e7SHong Zhang    Level: intermediate
37489110b2e7SHong Zhang 
37499110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
37509110b2e7SHong Zhang 
37519110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
37529110b2e7SHong Zhang @*/
37539110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
37549110b2e7SHong Zhang {
375578064530SBarry Smith   PetscErrorCode ierr;
375678064530SBarry Smith   PetscInt       i;
375778064530SBarry Smith   PetscBool      identical;
375878064530SBarry Smith 
37599110b2e7SHong Zhang   PetscFunctionBegin;
37609110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
376178064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
376278064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);CHKERRQ(ierr);
376378064530SBarry Smith     if (identical) PetscFunctionReturn(0);
376478064530SBarry Smith   }
37659110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
37669110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
37679110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
37689110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
37699110b2e7SHong Zhang   PetscFunctionReturn(0);
37709110b2e7SHong Zhang }
37719110b2e7SHong Zhang 
37729110b2e7SHong Zhang #undef __FUNCT__
37739110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel"
37749110b2e7SHong Zhang /*@C
37759110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
37769110b2e7SHong Zhang 
37779110b2e7SHong Zhang    Logically Collective on TS
37789110b2e7SHong Zhang 
37799110b2e7SHong Zhang    Input Parameters:
37809110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
37819110b2e7SHong Zhang 
37829110b2e7SHong Zhang    Notes:
37839110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
37849110b2e7SHong Zhang 
37859110b2e7SHong Zhang    Level: intermediate
37869110b2e7SHong Zhang 
37879110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
37889110b2e7SHong Zhang 
37899110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
37909110b2e7SHong Zhang @*/
37919110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
37929110b2e7SHong Zhang {
37939110b2e7SHong Zhang   PetscErrorCode ierr;
37949110b2e7SHong Zhang   PetscInt       i;
37959110b2e7SHong Zhang 
37969110b2e7SHong Zhang   PetscFunctionBegin;
37979110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37989110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
37999110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
38009110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
38019110b2e7SHong Zhang     }
38029110b2e7SHong Zhang   }
38039110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
38049110b2e7SHong Zhang   PetscFunctionReturn(0);
38059110b2e7SHong Zhang }
38069110b2e7SHong Zhang 
38079110b2e7SHong Zhang #undef __FUNCT__
3808110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault"
3809721cd6eeSBarry Smith /*@C
3810721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3811110eb670SHong Zhang 
3812110eb670SHong Zhang    Level: intermediate
3813110eb670SHong Zhang 
3814110eb670SHong Zhang .keywords: TS, set, monitor
3815110eb670SHong Zhang 
3816110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3817110eb670SHong Zhang @*/
3818721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3819110eb670SHong Zhang {
3820110eb670SHong Zhang   PetscErrorCode ierr;
3821721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3822110eb670SHong Zhang 
3823110eb670SHong Zhang   PetscFunctionBegin;
3824110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3825721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3826110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3827110eb670SHong 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);
3828110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3829721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3830110eb670SHong Zhang   PetscFunctionReturn(0);
3831110eb670SHong Zhang }
3832110eb670SHong Zhang 
3833110eb670SHong Zhang #undef __FUNCT__
3834cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3835cd652676SJed Brown /*@
3836cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3837cd652676SJed Brown 
3838cd652676SJed Brown    Collective on TS
3839cd652676SJed Brown 
3840cd652676SJed Brown    Input Argument:
3841cd652676SJed Brown +  ts - time stepping context
3842cd652676SJed Brown -  t - time to interpolate to
3843cd652676SJed Brown 
3844cd652676SJed Brown    Output Argument:
38450910c330SBarry Smith .  U - state at given time
3846cd652676SJed Brown 
3847cd652676SJed Brown    Level: intermediate
3848cd652676SJed Brown 
3849cd652676SJed Brown    Developer Notes:
3850cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3851cd652676SJed Brown 
3852cd652676SJed Brown .keywords: TS, set
3853cd652676SJed Brown 
3854874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3855cd652676SJed Brown @*/
38560910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3857cd652676SJed Brown {
3858cd652676SJed Brown   PetscErrorCode ierr;
3859cd652676SJed Brown 
3860cd652676SJed Brown   PetscFunctionBegin;
3861cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3862b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3863be5899b3SLisandro 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);
3864ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
38650910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3866cd652676SJed Brown   PetscFunctionReturn(0);
3867cd652676SJed Brown }
3868cd652676SJed Brown 
3869cd652676SJed Brown #undef __FUNCT__
38704a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3871d763cef2SBarry Smith /*@
38726d9e5789SSean Farley    TSStep - Steps one time step
3873d763cef2SBarry Smith 
3874d763cef2SBarry Smith    Collective on TS
3875d763cef2SBarry Smith 
3876d763cef2SBarry Smith    Input Parameter:
3877d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3878d763cef2SBarry Smith 
387927829d71SBarry Smith    Level: developer
3880d763cef2SBarry Smith 
3881b8123daeSJed Brown    Notes:
388227829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
388327829d71SBarry Smith 
3884b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3885b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3886b8123daeSJed Brown 
388725cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
388825cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
388925cb2221SBarry Smith 
3890d763cef2SBarry Smith .keywords: TS, timestep, solve
3891d763cef2SBarry Smith 
38929be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3893d763cef2SBarry Smith @*/
3894193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3895d763cef2SBarry Smith {
3896dfbe8321SBarry Smith   PetscErrorCode   ierr;
3897fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3898be5899b3SLisandro Dalcin   PetscReal        ptime;
3899d763cef2SBarry Smith 
3900d763cef2SBarry Smith   PetscFunctionBegin;
39010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3902fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3903fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3904fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3905fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3906fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3907fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3908302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3909fffbeea8SBarry Smith 
3910d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
391168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3912d405a339SMatthew Knepley 
3913a6772fa2SLisandro 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()");
3914be5899b3SLisandro 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");
3915a6772fa2SLisandro Dalcin 
3916be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3917362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3918be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
3919e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3920d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3921193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3922d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3923be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
3924be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps++;
3925be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
392628d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3927362cd11cSLisandro Dalcin 
3928362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3929cef5090cSJed Brown     if (ts->errorifstepfailed) {
393008c7845fSBarry 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]);
393108c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3932d2daff3dSHong Zhang     }
393308c7845fSBarry Smith   } else if (!ts->reason) {
393408c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
393508c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
393608c7845fSBarry Smith   }
393708c7845fSBarry Smith   PetscFunctionReturn(0);
393808c7845fSBarry Smith }
393908c7845fSBarry Smith 
394008c7845fSBarry Smith #undef __FUNCT__
394108c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
394208c7845fSBarry Smith /*@
3943b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
394408c7845fSBarry Smith 
394508c7845fSBarry Smith    Collective on TS
394608c7845fSBarry Smith 
394708c7845fSBarry Smith    Input Parameter:
394808c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
394908c7845fSBarry Smith 
39506a4d4014SLisandro Dalcin    Level: intermediate
39516a4d4014SLisandro Dalcin 
3952b957a604SHong Zhang .keywords: TS, adjoint, step
39536a4d4014SLisandro Dalcin 
3954b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
39556a4d4014SLisandro Dalcin @*/
395608c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
39576a4d4014SLisandro Dalcin {
395808c7845fSBarry Smith   DM               dm;
39596a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
39606a4d4014SLisandro Dalcin 
39616a4d4014SLisandro Dalcin   PetscFunctionBegin;
39624a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
396308c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
396408c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3965d763cef2SBarry Smith 
3966685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
3967d763cef2SBarry Smith 
3968be5899b3SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3969be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime;
397042f2b339SBarry 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);
3971be5899b3SLisandro Dalcin   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
397242f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
39738d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3974be5899b3SLisandro Dalcin   ts->steps++; ts->total_steps--;
3975362cd11cSLisandro Dalcin 
3976362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3977cef5090cSJed Brown     if (ts->errorifstepfailed) {
39786c4ed002SBarry 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]);
39796c4ed002SBarry 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]);
39806c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3981cef5090cSJed Brown     }
3982362cd11cSLisandro Dalcin   } else if (!ts->reason) {
398373b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
3984362cd11cSLisandro Dalcin   }
3985d763cef2SBarry Smith   PetscFunctionReturn(0);
3986d763cef2SBarry Smith }
3987d763cef2SBarry Smith 
3988d763cef2SBarry Smith #undef __FUNCT__
39897cbde773SLisandro Dalcin #define __FUNCT__ "TSEvaluateWLTE"
39907cbde773SLisandro Dalcin /*@
39917cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
39927cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
39937cbde773SLisandro Dalcin 
39947cbde773SLisandro Dalcin    Collective on TS
39957cbde773SLisandro Dalcin 
39967cbde773SLisandro Dalcin    Input Arguments:
39977cbde773SLisandro Dalcin +  ts - time stepping context
39987cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
39997cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
40007cbde773SLisandro Dalcin 
40017cbde773SLisandro Dalcin    Output Arguments:
40027cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
40037cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
40047cbde773SLisandro Dalcin 
40057cbde773SLisandro Dalcin    Level: advanced
40067cbde773SLisandro Dalcin 
40077cbde773SLisandro Dalcin    Notes:
40087cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
40097cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
40107cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
40117cbde773SLisandro Dalcin 
40127cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
40137cbde773SLisandro Dalcin @*/
40147cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
40157cbde773SLisandro Dalcin {
40167cbde773SLisandro Dalcin   PetscErrorCode ierr;
40177cbde773SLisandro Dalcin 
40187cbde773SLisandro Dalcin   PetscFunctionBegin;
40197cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40207cbde773SLisandro Dalcin   PetscValidType(ts,1);
40217cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
40227cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
40237cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
40247cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
40257cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
40267cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
40277cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
40287cbde773SLisandro Dalcin   PetscFunctionReturn(0);
40297cbde773SLisandro Dalcin }
40307cbde773SLisandro Dalcin 
40317cbde773SLisandro Dalcin #undef __FUNCT__
403205175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
403305175c85SJed Brown /*@
403405175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
403505175c85SJed Brown 
40361c3436cfSJed Brown    Collective on TS
403705175c85SJed Brown 
403805175c85SJed Brown    Input Arguments:
40391c3436cfSJed Brown +  ts - time stepping context
40401c3436cfSJed Brown .  order - desired order of accuracy
40410298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
404205175c85SJed Brown 
404305175c85SJed Brown    Output Arguments:
40440910c330SBarry Smith .  U - state at the end of the current step
404505175c85SJed Brown 
404605175c85SJed Brown    Level: advanced
404705175c85SJed Brown 
4048108c343cSJed Brown    Notes:
4049108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
4050108c343cSJed 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.
4051108c343cSJed Brown 
40521c3436cfSJed Brown .seealso: TSStep(), TSAdapt
405305175c85SJed Brown @*/
40540910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
405505175c85SJed Brown {
405605175c85SJed Brown   PetscErrorCode ierr;
405705175c85SJed Brown 
405805175c85SJed Brown   PetscFunctionBegin;
405905175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
406005175c85SJed Brown   PetscValidType(ts,1);
40610910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4062ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
40630910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
406405175c85SJed Brown   PetscFunctionReturn(0);
406505175c85SJed Brown }
406605175c85SJed Brown 
4067b1cb36f3SHong Zhang #undef __FUNCT__
4068b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral"
4069b1cb36f3SHong Zhang /*@
4070b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
4071b1cb36f3SHong Zhang 
4072b1cb36f3SHong Zhang  Collective on TS
4073b1cb36f3SHong Zhang 
4074b1cb36f3SHong Zhang  Input Arguments:
4075b1cb36f3SHong Zhang  .  ts - time stepping context
4076b1cb36f3SHong Zhang 
4077b1cb36f3SHong Zhang  Level: advanced
4078b1cb36f3SHong Zhang 
4079b1cb36f3SHong Zhang  Notes:
4080b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
4081b1cb36f3SHong Zhang 
4082b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
4083b1cb36f3SHong Zhang  @*/
4084b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
4085b1cb36f3SHong Zhang {
4086b1cb36f3SHong Zhang     PetscErrorCode ierr;
4087b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4088b1cb36f3SHong 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);
4089b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
4090b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4091b1cb36f3SHong Zhang }
4092d67e68b7SBarry Smith 
409305175c85SJed Brown #undef __FUNCT__
4094d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
4095d763cef2SBarry Smith /*@
4096d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
4097d763cef2SBarry Smith 
4098d763cef2SBarry Smith    Collective on TS
4099d763cef2SBarry Smith 
4100d763cef2SBarry Smith    Input Parameter:
4101d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
410263e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
410363e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
41045a3a76d0SJed Brown 
4105d763cef2SBarry Smith    Level: beginner
4106d763cef2SBarry Smith 
41075a3a76d0SJed Brown    Notes:
41085a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
41095a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
41105a3a76d0SJed Brown    stepped over the final time.
41115a3a76d0SJed Brown 
4112d763cef2SBarry Smith .keywords: TS, timestep, solve
4113d763cef2SBarry Smith 
411463e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
4115d763cef2SBarry Smith @*/
4116cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
4117d763cef2SBarry Smith {
4118b06615a5SLisandro Dalcin   Vec               solution;
4119d763cef2SBarry Smith   PetscErrorCode    ierr;
4120d763cef2SBarry Smith 
4121d763cef2SBarry Smith   PetscFunctionBegin;
4122d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4123f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
4124feed9e9dSBarry Smith 
412549354f04SShri 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 */
4126b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
41270910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
4128b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
4129b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
4130b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
41315a3a76d0SJed Brown     }
41320910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
4133bbd56ea5SKarl Rupp   } else if (u) {
41340910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
41355a3a76d0SJed Brown   }
4136b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
413768bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
4138a6772fa2SLisandro Dalcin 
4139a6772fa2SLisandro 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()");
4140a6772fa2SLisandro 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");
4141a6772fa2SLisandro Dalcin 
4142d763cef2SBarry Smith   /* reset time step and iteration counters */
4143193ac0bcSJed Brown   ts->steps             = 0;
41445ef26d82SJed Brown   ts->ksp_its           = 0;
41455ef26d82SJed Brown   ts->snes_its          = 0;
4146c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
4147c610991cSLisandro Dalcin   ts->reject            = 0;
4148193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
4149193ac0bcSJed Brown 
4150ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
4151f05ece33SBarry Smith 
4152193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
4153193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
4154a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4155cc708dedSBarry Smith     ts->solvetime = ts->ptime;
4156a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
4157be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
4158db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
4159db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
4160cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41616427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
416228d5b5d6SLisandro Dalcin     ts->steprollback = PETSC_FALSE;
416328d5b5d6SLisandro Dalcin     ts->steprestart  = PETSC_TRUE;
41646427ac75SLisandro Dalcin 
4165e1a7a14fSJed Brown     while (!ts->reason) {
4166193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41679687d888SLisandro Dalcin       if (!ts->steprollback) {
41689687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
41699687d888SLisandro Dalcin       }
4170193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
4171e783b05fSHong 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. */
4172b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
4173b1cb36f3SHong Zhang       }
4174e783b05fSHong 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. */
4175e783b05fSHong Zhang       if (!ts->steprollback) {
4176cdf0de3dSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41771eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
4178aeb4809dSShri Abhyankar       }
4179193ac0bcSJed Brown     }
418063e21af5SBarry Smith     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
41816427ac75SLisandro Dalcin 
418249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
41830910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
4184cc708dedSBarry Smith       ts->solvetime = ts->max_time;
4185b06615a5SLisandro Dalcin       solution = u;
418663e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
41870574a7fbSJed Brown     } else {
4188ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
4189cc708dedSBarry Smith       ts->solvetime = ts->ptime;
4190b06615a5SLisandro Dalcin       solution = ts->vec_sol;
41910574a7fbSJed Brown     }
4192193ac0bcSJed Brown   }
4193d2daff3dSHong Zhang 
4194ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
419563e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
419656f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
4197ef222394SBarry Smith   if (ts->adjoint_solve) {
4198ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
41992b0a91c0SBarry Smith   }
4200d763cef2SBarry Smith   PetscFunctionReturn(0);
4201d763cef2SBarry Smith }
4202d763cef2SBarry Smith 
4203d763cef2SBarry Smith #undef __FUNCT__
4204b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral"
4205b1cb36f3SHong Zhang /*@
4206b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
4207b1cb36f3SHong Zhang 
4208b1cb36f3SHong Zhang  Collective on TS
4209b1cb36f3SHong Zhang 
4210b1cb36f3SHong Zhang  Input Arguments:
4211b1cb36f3SHong Zhang  .  ts - time stepping context
4212b1cb36f3SHong Zhang 
4213b1cb36f3SHong Zhang  Level: advanced
4214b1cb36f3SHong Zhang 
4215b1cb36f3SHong Zhang  Notes:
4216b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
4217b1cb36f3SHong Zhang 
4218b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
4219b1cb36f3SHong Zhang  @*/
4220b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
4221b1cb36f3SHong Zhang {
4222b1cb36f3SHong Zhang     PetscErrorCode ierr;
4223b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4224b1cb36f3SHong 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);
4225b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
4226b1cb36f3SHong Zhang     PetscFunctionReturn(0);
4227b1cb36f3SHong Zhang }
4228b1cb36f3SHong Zhang 
4229b1cb36f3SHong Zhang #undef __FUNCT__
4230c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
4231c88f2d44SBarry Smith /*@
4232c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
4233c88f2d44SBarry Smith 
4234c88f2d44SBarry Smith    Collective on TS
4235c88f2d44SBarry Smith 
4236c88f2d44SBarry Smith    Input Parameter:
42372c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
4238c88f2d44SBarry Smith 
4239e87fd094SBarry Smith    Options Database:
4240e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
4241e87fd094SBarry Smith 
4242c88f2d44SBarry Smith    Level: intermediate
4243c88f2d44SBarry Smith 
4244c88f2d44SBarry Smith    Notes:
4245c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
4246c88f2d44SBarry Smith 
424773b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
424873b18844SBarry Smith 
4249c88f2d44SBarry Smith .keywords: TS, timestep, solve
4250c88f2d44SBarry Smith 
4251b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
4252c88f2d44SBarry Smith @*/
42532c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
4254c88f2d44SBarry Smith {
4255c88f2d44SBarry Smith   PetscErrorCode    ierr;
4256c88f2d44SBarry Smith 
4257c88f2d44SBarry Smith   PetscFunctionBegin;
4258c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4259c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
426068bece0bSHong Zhang 
4261c88f2d44SBarry Smith   /* reset time step and iteration counters */
4262c88f2d44SBarry Smith   ts->steps             = 0;
4263c88f2d44SBarry Smith   ts->ksp_its           = 0;
4264c88f2d44SBarry Smith   ts->snes_its          = 0;
4265c88f2d44SBarry Smith   ts->num_snes_failures = 0;
4266c88f2d44SBarry Smith   ts->reject            = 0;
4267c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
4268c88f2d44SBarry Smith 
426973b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
4270c88f2d44SBarry Smith 
427173b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
4272c88f2d44SBarry Smith   while (!ts->reason) {
427355c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
42749110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
42756427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
4276616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
4277b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
4278b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
4279b1cb36f3SHong Zhang     }
4280c88f2d44SBarry Smith   }
428126656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
428226656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
4283c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
4284e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
4285685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
4286c88f2d44SBarry Smith   PetscFunctionReturn(0);
4287c88f2d44SBarry Smith }
4288c88f2d44SBarry Smith 
4289c88f2d44SBarry Smith #undef __FUNCT__
4290d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
42917db568b7SBarry Smith /*@C
4292228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
4293228d79bcSJed Brown 
4294228d79bcSJed Brown    Collective on TS
4295228d79bcSJed Brown 
4296228d79bcSJed Brown    Input Parameters:
4297228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
4298228d79bcSJed Brown .  step - step number that has just completed
4299228d79bcSJed Brown .  ptime - model time of the state
43000910c330SBarry Smith -  u - state at the current model time
4301228d79bcSJed Brown 
4302228d79bcSJed Brown    Notes:
43037db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
43047db568b7SBarry Smith    Users would almost never call this routine directly.
4305228d79bcSJed Brown 
430663e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
430763e21af5SBarry Smith 
43087db568b7SBarry Smith    Level: developer
4309228d79bcSJed Brown 
4310228d79bcSJed Brown .keywords: TS, timestep
4311228d79bcSJed Brown @*/
43120910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
4313d763cef2SBarry Smith {
4314d6152f81SLisandro Dalcin   DM             dm;
4315a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
4316d6152f81SLisandro Dalcin   PetscErrorCode ierr;
4317d763cef2SBarry Smith 
4318d763cef2SBarry Smith   PetscFunctionBegin;
4319b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4320b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4321d6152f81SLisandro Dalcin 
4322d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4323d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4324d6152f81SLisandro Dalcin 
43255edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4326d763cef2SBarry Smith   for (i=0; i<n; i++) {
43270910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4328d763cef2SBarry Smith   }
43295edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4330d763cef2SBarry Smith   PetscFunctionReturn(0);
4331d763cef2SBarry Smith }
4332d763cef2SBarry Smith 
43339110b2e7SHong Zhang #undef __FUNCT__
43349110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor"
43357db568b7SBarry Smith /*@C
43369110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
43379110b2e7SHong Zhang 
43389110b2e7SHong Zhang    Collective on TS
43399110b2e7SHong Zhang 
43409110b2e7SHong Zhang    Input Parameters:
43419110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
43429110b2e7SHong Zhang .  step - step number that has just completed
43439110b2e7SHong Zhang .  ptime - model time of the state
43449110b2e7SHong Zhang .  u - state at the current model time
43459110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
43469110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
43479110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
43489110b2e7SHong Zhang 
43499110b2e7SHong Zhang    Notes:
43507db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
43517db568b7SBarry Smith    Users would almost never call this routine directly.
43529110b2e7SHong Zhang 
43537db568b7SBarry Smith    Level: developer
43549110b2e7SHong Zhang 
43559110b2e7SHong Zhang .keywords: TS, timestep
43569110b2e7SHong Zhang @*/
43579110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
43589110b2e7SHong Zhang {
43599110b2e7SHong Zhang   PetscErrorCode ierr;
43609110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
43619110b2e7SHong Zhang 
43629110b2e7SHong Zhang   PetscFunctionBegin;
43639110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43649110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
43659110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
43669110b2e7SHong Zhang   for (i=0; i<n; i++) {
43679110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
43689110b2e7SHong Zhang   }
43699110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
43709110b2e7SHong Zhang   PetscFunctionReturn(0);
43719110b2e7SHong Zhang }
43729110b2e7SHong Zhang 
4373d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
43744a2ae208SSatish Balay #undef __FUNCT__
4375a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
4376d763cef2SBarry Smith /*@C
43777db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4378a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4379d763cef2SBarry Smith 
4380d763cef2SBarry Smith    Collective on TS
4381d763cef2SBarry Smith 
4382d763cef2SBarry Smith    Input Parameters:
4383d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4384d763cef2SBarry Smith .  label - the title to put in the title bar
43857c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4386a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4387a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4388d763cef2SBarry Smith 
4389d763cef2SBarry Smith    Output Parameter:
43900b039ecaSBarry Smith .  ctx - the context
4391d763cef2SBarry Smith 
4392d763cef2SBarry Smith    Options Database Key:
43934f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
43947db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
43957db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
43967db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
43977db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4398b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4399d763cef2SBarry Smith 
4400d763cef2SBarry Smith    Notes:
4401a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4402d763cef2SBarry Smith 
44037db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
44047db568b7SBarry Smith 
44057db568b7SBarry 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
44067db568b7SBarry 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
44077db568b7SBarry Smith    as the first argument.
44087db568b7SBarry Smith 
44097db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
44107db568b7SBarry Smith 
44117db568b7SBarry Smith 
4412d763cef2SBarry Smith    Level: intermediate
4413d763cef2SBarry Smith 
44147db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4415d763cef2SBarry Smith 
44167db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
44177db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
44187db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
44197db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
44207db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
44217c922b88SBarry Smith 
4422d763cef2SBarry Smith @*/
4423a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4424d763cef2SBarry Smith {
44257f52daa2SLisandro Dalcin   PetscDraw      draw;
4426dfbe8321SBarry Smith   PetscErrorCode ierr;
4427d763cef2SBarry Smith 
4428d763cef2SBarry Smith   PetscFunctionBegin;
4429b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
44307f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
44317f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
44327f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4433287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
44347f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4435a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4436d763cef2SBarry Smith   PetscFunctionReturn(0);
4437d763cef2SBarry Smith }
4438d763cef2SBarry Smith 
44394a2ae208SSatish Balay #undef __FUNCT__
44404f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
4441b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4442d763cef2SBarry Smith {
44430b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4444c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4445dfbe8321SBarry Smith   PetscErrorCode ierr;
4446d763cef2SBarry Smith 
4447d763cef2SBarry Smith   PetscFunctionBegin;
444863e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4449b06615a5SLisandro Dalcin   if (!step) {
4450a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4451a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
44526934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
4453a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4454a9f9c1f6SBarry Smith   }
4455c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
44560b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4457b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
44580b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
44596934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
44603923b477SBarry Smith   }
4461d763cef2SBarry Smith   PetscFunctionReturn(0);
4462d763cef2SBarry Smith }
4463d763cef2SBarry Smith 
44644a2ae208SSatish Balay #undef __FUNCT__
4465a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
4466d763cef2SBarry Smith /*@C
4467a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4468a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4469d763cef2SBarry Smith 
44700b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4471d763cef2SBarry Smith 
4472d763cef2SBarry Smith    Input Parameter:
44730b039ecaSBarry Smith .  ctx - the monitor context
4474d763cef2SBarry Smith 
4475d763cef2SBarry Smith    Level: intermediate
4476d763cef2SBarry Smith 
4477d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4478d763cef2SBarry Smith 
44794f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4480d763cef2SBarry Smith @*/
4481a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4482d763cef2SBarry Smith {
4483dfbe8321SBarry Smith   PetscErrorCode ierr;
4484d763cef2SBarry Smith 
4485d763cef2SBarry Smith   PetscFunctionBegin;
44867684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
44877684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
44887684fa3eSBarry Smith   }
44890b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
449031152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4491387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4492387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4493387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
44940b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4495d763cef2SBarry Smith   PetscFunctionReturn(0);
4496d763cef2SBarry Smith }
4497d763cef2SBarry Smith 
44984a2ae208SSatish Balay #undef __FUNCT__
44994a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
4500d763cef2SBarry Smith /*@
4501b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4502d763cef2SBarry Smith 
4503d763cef2SBarry Smith    Not Collective
4504d763cef2SBarry Smith 
4505d763cef2SBarry Smith    Input Parameter:
4506d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4507d763cef2SBarry Smith 
4508d763cef2SBarry Smith    Output Parameter:
450963e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
4510d763cef2SBarry Smith 
4511d763cef2SBarry Smith    Level: beginner
4512d763cef2SBarry Smith 
4513b8123daeSJed Brown    Note:
4514b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
45159be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4516b8123daeSJed Brown 
451763e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
4518d763cef2SBarry Smith 
4519d763cef2SBarry Smith .keywords: TS, get, time
4520d763cef2SBarry Smith @*/
45217087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4522d763cef2SBarry Smith {
4523d763cef2SBarry Smith   PetscFunctionBegin;
45240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4525f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4526d763cef2SBarry Smith   *t = ts->ptime;
4527d763cef2SBarry Smith   PetscFunctionReturn(0);
4528d763cef2SBarry Smith }
4529d763cef2SBarry Smith 
45304a2ae208SSatish Balay #undef __FUNCT__
4531e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
4532e5e524a1SHong Zhang /*@
4533e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4534e5e524a1SHong Zhang 
4535e5e524a1SHong Zhang    Not Collective
4536e5e524a1SHong Zhang 
4537e5e524a1SHong Zhang    Input Parameter:
4538e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4539e5e524a1SHong Zhang 
4540e5e524a1SHong Zhang    Output Parameter:
4541e5e524a1SHong Zhang .  t  - the previous time
4542e5e524a1SHong Zhang 
4543e5e524a1SHong Zhang    Level: beginner
4544e5e524a1SHong Zhang 
4545e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
4546e5e524a1SHong Zhang 
4547e5e524a1SHong Zhang .keywords: TS, get, time
4548e5e524a1SHong Zhang @*/
4549e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4550e5e524a1SHong Zhang {
4551e5e524a1SHong Zhang   PetscFunctionBegin;
4552e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4553e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4554e5e524a1SHong Zhang   *t = ts->ptime_prev;
4555e5e524a1SHong Zhang   PetscFunctionReturn(0);
4556e5e524a1SHong Zhang }
4557e5e524a1SHong Zhang 
4558e5e524a1SHong Zhang #undef __FUNCT__
45596a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
45606a4d4014SLisandro Dalcin /*@
45616a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
45626a4d4014SLisandro Dalcin 
45633f9fe445SBarry Smith    Logically Collective on TS
45646a4d4014SLisandro Dalcin 
45656a4d4014SLisandro Dalcin    Input Parameters:
45666a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
45676a4d4014SLisandro Dalcin -  time - the time
45686a4d4014SLisandro Dalcin 
45696a4d4014SLisandro Dalcin    Level: intermediate
45706a4d4014SLisandro Dalcin 
45716a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
45726a4d4014SLisandro Dalcin 
45736a4d4014SLisandro Dalcin .keywords: TS, set, time
45746a4d4014SLisandro Dalcin @*/
45757087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
45766a4d4014SLisandro Dalcin {
45776a4d4014SLisandro Dalcin   PetscFunctionBegin;
45780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4579c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
45806a4d4014SLisandro Dalcin   ts->ptime = t;
45816a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
45826a4d4014SLisandro Dalcin }
45836a4d4014SLisandro Dalcin 
45846a4d4014SLisandro Dalcin #undef __FUNCT__
45854a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
4586d763cef2SBarry Smith /*@C
4587d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4588d763cef2SBarry Smith    TS options in the database.
4589d763cef2SBarry Smith 
45903f9fe445SBarry Smith    Logically Collective on TS
4591d763cef2SBarry Smith 
4592d763cef2SBarry Smith    Input Parameter:
4593d763cef2SBarry Smith +  ts     - The TS context
4594d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4595d763cef2SBarry Smith 
4596d763cef2SBarry Smith    Notes:
4597d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4598d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4599d763cef2SBarry Smith    hyphen.
4600d763cef2SBarry Smith 
4601d763cef2SBarry Smith    Level: advanced
4602d763cef2SBarry Smith 
4603d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4604d763cef2SBarry Smith 
4605d763cef2SBarry Smith .seealso: TSSetFromOptions()
4606d763cef2SBarry Smith 
4607d763cef2SBarry Smith @*/
46087087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4609d763cef2SBarry Smith {
4610dfbe8321SBarry Smith   PetscErrorCode ierr;
4611089b2837SJed Brown   SNES           snes;
4612d763cef2SBarry Smith 
4613d763cef2SBarry Smith   PetscFunctionBegin;
46140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4615d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4616089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4617089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4618d763cef2SBarry Smith   PetscFunctionReturn(0);
4619d763cef2SBarry Smith }
4620d763cef2SBarry Smith 
4621d763cef2SBarry Smith 
46224a2ae208SSatish Balay #undef __FUNCT__
46234a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
4624d763cef2SBarry Smith /*@C
4625d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4626d763cef2SBarry Smith    TS options in the database.
4627d763cef2SBarry Smith 
46283f9fe445SBarry Smith    Logically Collective on TS
4629d763cef2SBarry Smith 
4630d763cef2SBarry Smith    Input Parameter:
4631d763cef2SBarry Smith +  ts     - The TS context
4632d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4633d763cef2SBarry Smith 
4634d763cef2SBarry Smith    Notes:
4635d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4636d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4637d763cef2SBarry Smith    hyphen.
4638d763cef2SBarry Smith 
4639d763cef2SBarry Smith    Level: advanced
4640d763cef2SBarry Smith 
4641d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4642d763cef2SBarry Smith 
4643d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4644d763cef2SBarry Smith 
4645d763cef2SBarry Smith @*/
46467087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4647d763cef2SBarry Smith {
4648dfbe8321SBarry Smith   PetscErrorCode ierr;
4649089b2837SJed Brown   SNES           snes;
4650d763cef2SBarry Smith 
4651d763cef2SBarry Smith   PetscFunctionBegin;
46520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4653d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4654089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4655089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4656d763cef2SBarry Smith   PetscFunctionReturn(0);
4657d763cef2SBarry Smith }
4658d763cef2SBarry Smith 
46594a2ae208SSatish Balay #undef __FUNCT__
46604a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
4661d763cef2SBarry Smith /*@C
4662d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4663d763cef2SBarry Smith    TS options in the database.
4664d763cef2SBarry Smith 
4665d763cef2SBarry Smith    Not Collective
4666d763cef2SBarry Smith 
4667d763cef2SBarry Smith    Input Parameter:
4668d763cef2SBarry Smith .  ts - The TS context
4669d763cef2SBarry Smith 
4670d763cef2SBarry Smith    Output Parameter:
4671d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4672d763cef2SBarry Smith 
4673d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4674d763cef2SBarry Smith    sufficient length to hold the prefix.
4675d763cef2SBarry Smith 
4676d763cef2SBarry Smith    Level: intermediate
4677d763cef2SBarry Smith 
4678d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4679d763cef2SBarry Smith 
4680d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4681d763cef2SBarry Smith @*/
46827087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4683d763cef2SBarry Smith {
4684dfbe8321SBarry Smith   PetscErrorCode ierr;
4685d763cef2SBarry Smith 
4686d763cef2SBarry Smith   PetscFunctionBegin;
46870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46884482741eSBarry Smith   PetscValidPointer(prefix,2);
4689d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4690d763cef2SBarry Smith   PetscFunctionReturn(0);
4691d763cef2SBarry Smith }
4692d763cef2SBarry Smith 
46934a2ae208SSatish Balay #undef __FUNCT__
46944a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
4695d763cef2SBarry Smith /*@C
4696d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4697d763cef2SBarry Smith 
4698d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4699d763cef2SBarry Smith 
4700d763cef2SBarry Smith    Input Parameter:
4701d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4702d763cef2SBarry Smith 
4703d763cef2SBarry Smith    Output Parameters:
4704e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4705e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4706e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4707e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4708d763cef2SBarry Smith 
47090298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4710d763cef2SBarry Smith 
4711d763cef2SBarry Smith    Level: intermediate
4712d763cef2SBarry Smith 
471326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4714d763cef2SBarry Smith 
4715d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4716d763cef2SBarry Smith @*/
4717e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4718d763cef2SBarry Smith {
4719089b2837SJed Brown   PetscErrorCode ierr;
4720089b2837SJed Brown   SNES           snes;
472124989b8cSPeter Brune   DM             dm;
4722089b2837SJed Brown 
4723d763cef2SBarry Smith   PetscFunctionBegin;
4724089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4725e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
472624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
472724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4728d763cef2SBarry Smith   PetscFunctionReturn(0);
4729d763cef2SBarry Smith }
4730d763cef2SBarry Smith 
47311713a123SBarry Smith #undef __FUNCT__
47322eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
47332eca1d9cSJed Brown /*@C
47342eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
47352eca1d9cSJed Brown 
47362eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
47372eca1d9cSJed Brown 
47382eca1d9cSJed Brown    Input Parameter:
47392eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
47402eca1d9cSJed Brown 
47412eca1d9cSJed Brown    Output Parameters:
4742e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4743e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
47442eca1d9cSJed Brown .  f   - The function to compute the matrices
47452eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
47462eca1d9cSJed Brown 
47470298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
47482eca1d9cSJed Brown 
47492eca1d9cSJed Brown    Level: advanced
47502eca1d9cSJed Brown 
47512eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
47522eca1d9cSJed Brown 
47532eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
47542eca1d9cSJed Brown @*/
4755e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
47562eca1d9cSJed Brown {
4757089b2837SJed Brown   PetscErrorCode ierr;
4758089b2837SJed Brown   SNES           snes;
475924989b8cSPeter Brune   DM             dm;
47600910c330SBarry Smith 
47612eca1d9cSJed Brown   PetscFunctionBegin;
4762089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4763f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4764e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
476524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
476624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
47672eca1d9cSJed Brown   PetscFunctionReturn(0);
47682eca1d9cSJed Brown }
47692eca1d9cSJed Brown 
47706083293cSBarry Smith 
47712eca1d9cSJed Brown #undef __FUNCT__
477283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
47731713a123SBarry Smith /*@C
477483a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
47751713a123SBarry Smith    VecView() for the solution at each timestep
47761713a123SBarry Smith 
47771713a123SBarry Smith    Collective on TS
47781713a123SBarry Smith 
47791713a123SBarry Smith    Input Parameters:
47801713a123SBarry Smith +  ts - the TS context
47811713a123SBarry Smith .  step - current time-step
4782142b95e3SSatish Balay .  ptime - current time
47830298fd71SBarry Smith -  dummy - either a viewer or NULL
47841713a123SBarry Smith 
478599fdda47SBarry Smith    Options Database:
478699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
478799fdda47SBarry Smith 
4788387f4636SBarry 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
478999fdda47SBarry Smith        will look bad
479099fdda47SBarry Smith 
47911713a123SBarry Smith    Level: intermediate
47921713a123SBarry Smith 
47931713a123SBarry Smith .keywords: TS,  vector, monitor, view
47941713a123SBarry Smith 
4795a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
47961713a123SBarry Smith @*/
47970910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
47981713a123SBarry Smith {
4799dfbe8321SBarry Smith   PetscErrorCode   ierr;
480083a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4801473a3ab2SBarry Smith   PetscDraw        draw;
48021713a123SBarry Smith 
48031713a123SBarry Smith   PetscFunctionBegin;
48046083293cSBarry Smith   if (!step && ictx->showinitial) {
48056083293cSBarry Smith     if (!ictx->initialsolution) {
48060910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
48071713a123SBarry Smith     }
48080910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
48096083293cSBarry Smith   }
4810b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48110dcf80beSBarry Smith 
48126083293cSBarry Smith   if (ictx->showinitial) {
48136083293cSBarry Smith     PetscReal pause;
48146083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
48156083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
48166083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
48176083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
48186083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
48196083293cSBarry Smith   }
48200910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4821473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
482251fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4823473a3ab2SBarry Smith     char      time[32];
4824473a3ab2SBarry Smith 
4825473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
482685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4827473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4828473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
482951fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4830473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4831473a3ab2SBarry Smith   }
4832473a3ab2SBarry Smith 
48336083293cSBarry Smith   if (ictx->showinitial) {
48346083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
48356083293cSBarry Smith   }
48361713a123SBarry Smith   PetscFunctionReturn(0);
48371713a123SBarry Smith }
48381713a123SBarry Smith 
48392d5ee99bSBarry Smith #undef __FUNCT__
48409110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi"
48419110b2e7SHong Zhang /*@C
48429110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
48439110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
48449110b2e7SHong Zhang 
48459110b2e7SHong Zhang    Collective on TS
48469110b2e7SHong Zhang 
48479110b2e7SHong Zhang    Input Parameters:
48489110b2e7SHong Zhang +  ts - the TS context
48499110b2e7SHong Zhang .  step - current time-step
48509110b2e7SHong Zhang .  ptime - current time
48519110b2e7SHong Zhang .  u - current state
48529110b2e7SHong Zhang .  numcost - number of cost functions
48539110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
48549110b2e7SHong Zhang .  mu - sensitivities to parameters
48559110b2e7SHong Zhang -  dummy - either a viewer or NULL
48569110b2e7SHong Zhang 
48579110b2e7SHong Zhang    Level: intermediate
48589110b2e7SHong Zhang 
48599110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
48609110b2e7SHong Zhang 
48619110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
48629110b2e7SHong Zhang @*/
48639110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
48649110b2e7SHong Zhang {
48659110b2e7SHong Zhang   PetscErrorCode   ierr;
48669110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
48679110b2e7SHong Zhang   PetscDraw        draw;
4868a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4869a0046e2cSSatish Balay   char             time[32];
48709110b2e7SHong Zhang 
48719110b2e7SHong Zhang   PetscFunctionBegin;
48729110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
48739110b2e7SHong Zhang 
48749110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
48759110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
48769110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
48779110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
48789110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
48799110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
48809110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
48819110b2e7SHong Zhang   PetscFunctionReturn(0);
48829110b2e7SHong Zhang }
48839110b2e7SHong Zhang 
48849110b2e7SHong Zhang #undef __FUNCT__
48852d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
48862d5ee99bSBarry Smith /*@C
48872d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
48882d5ee99bSBarry Smith 
48892d5ee99bSBarry Smith    Collective on TS
48902d5ee99bSBarry Smith 
48912d5ee99bSBarry Smith    Input Parameters:
48922d5ee99bSBarry Smith +  ts - the TS context
48932d5ee99bSBarry Smith .  step - current time-step
48942d5ee99bSBarry Smith .  ptime - current time
48952d5ee99bSBarry Smith -  dummy - either a viewer or NULL
48962d5ee99bSBarry Smith 
48972d5ee99bSBarry Smith    Level: intermediate
48982d5ee99bSBarry Smith 
48992d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
49002d5ee99bSBarry Smith 
49012d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
49022d5ee99bSBarry Smith @*/
49032d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
49042d5ee99bSBarry Smith {
49052d5ee99bSBarry Smith   PetscErrorCode    ierr;
49062d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
49072d5ee99bSBarry Smith   PetscDraw         draw;
49086934998bSLisandro Dalcin   PetscDrawAxis     axis;
49092d5ee99bSBarry Smith   PetscInt          n;
49102d5ee99bSBarry Smith   PetscMPIInt       size;
49116934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
49122d5ee99bSBarry Smith   char              time[32];
49132d5ee99bSBarry Smith   const PetscScalar *U;
49142d5ee99bSBarry Smith 
49152d5ee99bSBarry Smith   PetscFunctionBegin;
49166934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
49176934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
49182d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
49196934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
49202d5ee99bSBarry Smith 
49212d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
49226934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
49236934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
49246934998bSLisandro Dalcin   if (!step) {
49256934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
49266934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
49276934998bSLisandro Dalcin   }
49282d5ee99bSBarry Smith 
49292d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
49306934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
49316934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4932a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
49336934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
49342d5ee99bSBarry Smith 
49356934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
49366934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
49372d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
49384b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
493985b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
49402d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
494151fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
49422d5ee99bSBarry Smith   }
49436934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
49442d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
49456934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
49462d5ee99bSBarry Smith   PetscFunctionReturn(0);
49472d5ee99bSBarry Smith }
49482d5ee99bSBarry Smith 
49491713a123SBarry Smith 
49506c699258SBarry Smith #undef __FUNCT__
495183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
49526083293cSBarry Smith /*@C
495383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
49546083293cSBarry Smith 
49556083293cSBarry Smith    Collective on TS
49566083293cSBarry Smith 
49576083293cSBarry Smith    Input Parameters:
49586083293cSBarry Smith .    ctx - the monitor context
49596083293cSBarry Smith 
49606083293cSBarry Smith    Level: intermediate
49616083293cSBarry Smith 
49626083293cSBarry Smith .keywords: TS,  vector, monitor, view
49636083293cSBarry Smith 
496483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
49656083293cSBarry Smith @*/
496683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
49676083293cSBarry Smith {
49686083293cSBarry Smith   PetscErrorCode ierr;
49696083293cSBarry Smith 
49706083293cSBarry Smith   PetscFunctionBegin;
497183a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
497283a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
497383a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
49746083293cSBarry Smith   PetscFunctionReturn(0);
49756083293cSBarry Smith }
49766083293cSBarry Smith 
49776083293cSBarry Smith #undef __FUNCT__
497883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
49796083293cSBarry Smith /*@C
498083a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
49816083293cSBarry Smith 
49826083293cSBarry Smith    Collective on TS
49836083293cSBarry Smith 
49846083293cSBarry Smith    Input Parameter:
49856083293cSBarry Smith .    ts - time-step context
49866083293cSBarry Smith 
49876083293cSBarry Smith    Output Patameter:
49886083293cSBarry Smith .    ctx - the monitor context
49896083293cSBarry Smith 
499099fdda47SBarry Smith    Options Database:
499199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
499299fdda47SBarry Smith 
49936083293cSBarry Smith    Level: intermediate
49946083293cSBarry Smith 
49956083293cSBarry Smith .keywords: TS,  vector, monitor, view
49966083293cSBarry Smith 
499783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
49986083293cSBarry Smith @*/
499983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
50006083293cSBarry Smith {
50016083293cSBarry Smith   PetscErrorCode   ierr;
50026083293cSBarry Smith 
50036083293cSBarry Smith   PetscFunctionBegin;
5004b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
500583a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
5006e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
5007bbd56ea5SKarl Rupp 
500883a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
5009473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
5010c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
5011473a3ab2SBarry Smith 
5012473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
5013c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
50146083293cSBarry Smith   PetscFunctionReturn(0);
50156083293cSBarry Smith }
50166083293cSBarry Smith 
50176083293cSBarry Smith #undef __FUNCT__
501883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
50193a471f94SBarry Smith /*@C
502083a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
50213a471f94SBarry Smith    VecView() for the error at each timestep
50223a471f94SBarry Smith 
50233a471f94SBarry Smith    Collective on TS
50243a471f94SBarry Smith 
50253a471f94SBarry Smith    Input Parameters:
50263a471f94SBarry Smith +  ts - the TS context
50273a471f94SBarry Smith .  step - current time-step
50283a471f94SBarry Smith .  ptime - current time
50290298fd71SBarry Smith -  dummy - either a viewer or NULL
50303a471f94SBarry Smith 
50313a471f94SBarry Smith    Level: intermediate
50323a471f94SBarry Smith 
50333a471f94SBarry Smith .keywords: TS,  vector, monitor, view
50343a471f94SBarry Smith 
50353a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
50363a471f94SBarry Smith @*/
50370910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
50383a471f94SBarry Smith {
50393a471f94SBarry Smith   PetscErrorCode   ierr;
504083a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
504183a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
50423a471f94SBarry Smith   Vec              work;
50433a471f94SBarry Smith 
50443a471f94SBarry Smith   PetscFunctionBegin;
5045b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
50460910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
50473a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
50480910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
50493a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
50503a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
50513a471f94SBarry Smith   PetscFunctionReturn(0);
50523a471f94SBarry Smith }
50533a471f94SBarry Smith 
5054af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
50553a471f94SBarry Smith #undef __FUNCT__
50566c699258SBarry Smith #define __FUNCT__ "TSSetDM"
50576c699258SBarry Smith /*@
50582a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
50596c699258SBarry Smith 
50603f9fe445SBarry Smith    Logically Collective on TS and DM
50616c699258SBarry Smith 
50626c699258SBarry Smith    Input Parameters:
50632a808120SBarry Smith +  ts - the ODE integrator object
50642a808120SBarry Smith -  dm - the dm, cannot be NULL
50656c699258SBarry Smith 
50666c699258SBarry Smith    Level: intermediate
50676c699258SBarry Smith 
50686c699258SBarry Smith 
50696c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
50706c699258SBarry Smith @*/
50717087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
50726c699258SBarry Smith {
50736c699258SBarry Smith   PetscErrorCode ierr;
5074089b2837SJed Brown   SNES           snes;
5075942e3340SBarry Smith   DMTS           tsdm;
50766c699258SBarry Smith 
50776c699258SBarry Smith   PetscFunctionBegin;
50780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50792a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
508070663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
5081942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
50822a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
5083942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
5084942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
508524989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
508624989b8cSPeter Brune         tsdm->originaldm = dm;
508724989b8cSPeter Brune       }
508824989b8cSPeter Brune     }
50896bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
509024989b8cSPeter Brune   }
50916c699258SBarry Smith   ts->dm = dm;
5092bbd56ea5SKarl Rupp 
5093089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5094089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
50956c699258SBarry Smith   PetscFunctionReturn(0);
50966c699258SBarry Smith }
50976c699258SBarry Smith 
50986c699258SBarry Smith #undef __FUNCT__
50996c699258SBarry Smith #define __FUNCT__ "TSGetDM"
51006c699258SBarry Smith /*@
51016c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
51026c699258SBarry Smith 
51033f9fe445SBarry Smith    Not Collective
51046c699258SBarry Smith 
51056c699258SBarry Smith    Input Parameter:
51066c699258SBarry Smith . ts - the preconditioner context
51076c699258SBarry Smith 
51086c699258SBarry Smith    Output Parameter:
51096c699258SBarry Smith .  dm - the dm
51106c699258SBarry Smith 
51116c699258SBarry Smith    Level: intermediate
51126c699258SBarry Smith 
51136c699258SBarry Smith 
51146c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
51156c699258SBarry Smith @*/
51167087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
51176c699258SBarry Smith {
5118496e6a7aSJed Brown   PetscErrorCode ierr;
5119496e6a7aSJed Brown 
51206c699258SBarry Smith   PetscFunctionBegin;
51210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5122496e6a7aSJed Brown   if (!ts->dm) {
5123ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
5124496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
5125496e6a7aSJed Brown   }
51266c699258SBarry Smith   *dm = ts->dm;
51276c699258SBarry Smith   PetscFunctionReturn(0);
51286c699258SBarry Smith }
51291713a123SBarry Smith 
51300f5c6efeSJed Brown #undef __FUNCT__
51310f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
51320f5c6efeSJed Brown /*@
51330f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
51340f5c6efeSJed Brown 
51353f9fe445SBarry Smith    Logically Collective on SNES
51360f5c6efeSJed Brown 
51370f5c6efeSJed Brown    Input Parameter:
5138d42a1c89SJed Brown + snes - nonlinear solver
51390910c330SBarry Smith . U - the current state at which to evaluate the residual
5140d42a1c89SJed Brown - ctx - user context, must be a TS
51410f5c6efeSJed Brown 
51420f5c6efeSJed Brown    Output Parameter:
51430f5c6efeSJed Brown . F - the nonlinear residual
51440f5c6efeSJed Brown 
51450f5c6efeSJed Brown    Notes:
51460f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
51470f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
51480f5c6efeSJed Brown 
51490f5c6efeSJed Brown    Level: advanced
51500f5c6efeSJed Brown 
51510f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
51520f5c6efeSJed Brown @*/
51530910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
51540f5c6efeSJed Brown {
51550f5c6efeSJed Brown   TS             ts = (TS)ctx;
51560f5c6efeSJed Brown   PetscErrorCode ierr;
51570f5c6efeSJed Brown 
51580f5c6efeSJed Brown   PetscFunctionBegin;
51590f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
51600910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
51610f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
51620f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
51630910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
51640f5c6efeSJed Brown   PetscFunctionReturn(0);
51650f5c6efeSJed Brown }
51660f5c6efeSJed Brown 
51670f5c6efeSJed Brown #undef __FUNCT__
51680f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
51690f5c6efeSJed Brown /*@
51700f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
51710f5c6efeSJed Brown 
51720f5c6efeSJed Brown    Collective on SNES
51730f5c6efeSJed Brown 
51740f5c6efeSJed Brown    Input Parameter:
51750f5c6efeSJed Brown + snes - nonlinear solver
51760910c330SBarry Smith . U - the current state at which to evaluate the residual
51770f5c6efeSJed Brown - ctx - user context, must be a TS
51780f5c6efeSJed Brown 
51790f5c6efeSJed Brown    Output Parameter:
51800f5c6efeSJed Brown + A - the Jacobian
51810f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
51820f5c6efeSJed Brown - flag - indicates any structure change in the matrix
51830f5c6efeSJed Brown 
51840f5c6efeSJed Brown    Notes:
51850f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
51860f5c6efeSJed Brown 
51870f5c6efeSJed Brown    Level: developer
51880f5c6efeSJed Brown 
51890f5c6efeSJed Brown .seealso: SNESSetJacobian()
51900f5c6efeSJed Brown @*/
5191d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
51920f5c6efeSJed Brown {
51930f5c6efeSJed Brown   TS             ts = (TS)ctx;
51940f5c6efeSJed Brown   PetscErrorCode ierr;
51950f5c6efeSJed Brown 
51960f5c6efeSJed Brown   PetscFunctionBegin;
51970f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
51980910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
51990f5c6efeSJed Brown   PetscValidPointer(A,3);
520094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
52010f5c6efeSJed Brown   PetscValidPointer(B,4);
520294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
52030f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
5204d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
52050f5c6efeSJed Brown   PetscFunctionReturn(0);
52060f5c6efeSJed Brown }
5207325fc9f4SBarry Smith 
52080e4ef248SJed Brown #undef __FUNCT__
52090e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
52100e4ef248SJed Brown /*@C
52119ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
52120e4ef248SJed Brown 
52130e4ef248SJed Brown    Collective on TS
52140e4ef248SJed Brown 
52150e4ef248SJed Brown    Input Arguments:
52160e4ef248SJed Brown +  ts - time stepping context
52170e4ef248SJed Brown .  t - time at which to evaluate
52180910c330SBarry Smith .  U - state at which to evaluate
52190e4ef248SJed Brown -  ctx - context
52200e4ef248SJed Brown 
52210e4ef248SJed Brown    Output Arguments:
52220e4ef248SJed Brown .  F - right hand side
52230e4ef248SJed Brown 
52240e4ef248SJed Brown    Level: intermediate
52250e4ef248SJed Brown 
52260e4ef248SJed Brown    Notes:
52270e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
52280e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
52290e4ef248SJed Brown 
52300e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
52310e4ef248SJed Brown @*/
52320910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
52330e4ef248SJed Brown {
52340e4ef248SJed Brown   PetscErrorCode ierr;
52350e4ef248SJed Brown   Mat            Arhs,Brhs;
52360e4ef248SJed Brown 
52370e4ef248SJed Brown   PetscFunctionBegin;
52380e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
5239d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
52400910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
52410e4ef248SJed Brown   PetscFunctionReturn(0);
52420e4ef248SJed Brown }
52430e4ef248SJed Brown 
52440e4ef248SJed Brown #undef __FUNCT__
52450e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
52460e4ef248SJed Brown /*@C
52470e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
52480e4ef248SJed Brown 
52490e4ef248SJed Brown    Collective on TS
52500e4ef248SJed Brown 
52510e4ef248SJed Brown    Input Arguments:
52520e4ef248SJed Brown +  ts - time stepping context
52530e4ef248SJed Brown .  t - time at which to evaluate
52540910c330SBarry Smith .  U - state at which to evaluate
52550e4ef248SJed Brown -  ctx - context
52560e4ef248SJed Brown 
52570e4ef248SJed Brown    Output Arguments:
52580e4ef248SJed Brown +  A - pointer to operator
52590e4ef248SJed Brown .  B - pointer to preconditioning matrix
52600e4ef248SJed Brown -  flg - matrix structure flag
52610e4ef248SJed Brown 
52620e4ef248SJed Brown    Level: intermediate
52630e4ef248SJed Brown 
52640e4ef248SJed Brown    Notes:
52650e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
52660e4ef248SJed Brown 
52670e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
52680e4ef248SJed Brown @*/
5269d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
52700e4ef248SJed Brown {
52710e4ef248SJed Brown   PetscFunctionBegin;
52720e4ef248SJed Brown   PetscFunctionReturn(0);
52730e4ef248SJed Brown }
52740e4ef248SJed Brown 
52750026cea9SSean Farley #undef __FUNCT__
52760026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
52770026cea9SSean Farley /*@C
52780026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
52790026cea9SSean Farley 
52800026cea9SSean Farley    Collective on TS
52810026cea9SSean Farley 
52820026cea9SSean Farley    Input Arguments:
52830026cea9SSean Farley +  ts - time stepping context
52840026cea9SSean Farley .  t - time at which to evaluate
52850910c330SBarry Smith .  U - state at which to evaluate
52860910c330SBarry Smith .  Udot - time derivative of state vector
52870026cea9SSean Farley -  ctx - context
52880026cea9SSean Farley 
52890026cea9SSean Farley    Output Arguments:
52900026cea9SSean Farley .  F - left hand side
52910026cea9SSean Farley 
52920026cea9SSean Farley    Level: intermediate
52930026cea9SSean Farley 
52940026cea9SSean Farley    Notes:
52950910c330SBarry 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
52960026cea9SSean Farley    user is required to write their own TSComputeIFunction.
52970026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
52980026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
52990026cea9SSean Farley 
53009ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
53019ae8fd06SBarry Smith 
53029ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
53030026cea9SSean Farley @*/
53040910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
53050026cea9SSean Farley {
53060026cea9SSean Farley   PetscErrorCode ierr;
53070026cea9SSean Farley   Mat            A,B;
53080026cea9SSean Farley 
53090026cea9SSean Farley   PetscFunctionBegin;
53100298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
5311d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
53120910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
53130026cea9SSean Farley   PetscFunctionReturn(0);
53140026cea9SSean Farley }
53150026cea9SSean Farley 
53160026cea9SSean Farley #undef __FUNCT__
53170026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
53180026cea9SSean Farley /*@C
5319b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
53200026cea9SSean Farley 
53210026cea9SSean Farley    Collective on TS
53220026cea9SSean Farley 
53230026cea9SSean Farley    Input Arguments:
53240026cea9SSean Farley +  ts - time stepping context
53250026cea9SSean Farley .  t - time at which to evaluate
53260910c330SBarry Smith .  U - state at which to evaluate
53270910c330SBarry Smith .  Udot - time derivative of state vector
53280026cea9SSean Farley .  shift - shift to apply
53290026cea9SSean Farley -  ctx - context
53300026cea9SSean Farley 
53310026cea9SSean Farley    Output Arguments:
53320026cea9SSean Farley +  A - pointer to operator
53330026cea9SSean Farley .  B - pointer to preconditioning matrix
53340026cea9SSean Farley -  flg - matrix structure flag
53350026cea9SSean Farley 
5336b41af12eSJed Brown    Level: advanced
53370026cea9SSean Farley 
53380026cea9SSean Farley    Notes:
53390026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
53400026cea9SSean Farley 
5341b41af12eSJed Brown    It is only appropriate for problems of the form
5342b41af12eSJed Brown 
5343b41af12eSJed Brown $     M Udot = F(U,t)
5344b41af12eSJed Brown 
5345b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
5346b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
5347b41af12eSJed Brown   an implicit operator of the form
5348b41af12eSJed Brown 
5349b41af12eSJed Brown $    shift*M + J
5350b41af12eSJed Brown 
5351b41af12eSJed 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
5352b41af12eSJed Brown   a copy of M or reassemble it when requested.
5353b41af12eSJed Brown 
53540026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
53550026cea9SSean Farley @*/
5356d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
53570026cea9SSean Farley {
5358b41af12eSJed Brown   PetscErrorCode ierr;
5359b41af12eSJed Brown 
53600026cea9SSean Farley   PetscFunctionBegin;
536194ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
5362b41af12eSJed Brown   ts->ijacobian.shift = shift;
53630026cea9SSean Farley   PetscFunctionReturn(0);
53640026cea9SSean Farley }
5365b41af12eSJed Brown 
5366e817cc15SEmil Constantinescu #undef __FUNCT__
5367e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
5368e817cc15SEmil Constantinescu /*@
5369e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
5370e817cc15SEmil Constantinescu 
5371e817cc15SEmil Constantinescu    Not Collective
5372e817cc15SEmil Constantinescu 
5373e817cc15SEmil Constantinescu    Input Parameter:
5374e817cc15SEmil Constantinescu .  ts - the TS context
5375e817cc15SEmil Constantinescu 
5376e817cc15SEmil Constantinescu    Output Parameter:
53774e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
5378e817cc15SEmil Constantinescu 
5379e817cc15SEmil Constantinescu    Level: beginner
5380e817cc15SEmil Constantinescu 
5381e817cc15SEmil Constantinescu .keywords: TS, equation type
5382e817cc15SEmil Constantinescu 
5383e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
5384e817cc15SEmil Constantinescu @*/
5385e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
5386e817cc15SEmil Constantinescu {
5387e817cc15SEmil Constantinescu   PetscFunctionBegin;
5388e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5389e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
5390e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
5391e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5392e817cc15SEmil Constantinescu }
5393e817cc15SEmil Constantinescu 
5394e817cc15SEmil Constantinescu #undef __FUNCT__
5395e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
5396e817cc15SEmil Constantinescu /*@
5397e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
5398e817cc15SEmil Constantinescu 
5399e817cc15SEmil Constantinescu    Not Collective
5400e817cc15SEmil Constantinescu 
5401e817cc15SEmil Constantinescu    Input Parameter:
5402e817cc15SEmil Constantinescu +  ts - the TS context
54031297b224SEmil Constantinescu -  equation_type - see TSEquationType
5404e817cc15SEmil Constantinescu 
5405e817cc15SEmil Constantinescu    Level: advanced
5406e817cc15SEmil Constantinescu 
5407e817cc15SEmil Constantinescu .keywords: TS, equation type
5408e817cc15SEmil Constantinescu 
5409e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
5410e817cc15SEmil Constantinescu @*/
5411e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5412e817cc15SEmil Constantinescu {
5413e817cc15SEmil Constantinescu   PetscFunctionBegin;
5414e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5415e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5416e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5417e817cc15SEmil Constantinescu }
54180026cea9SSean Farley 
54194af1b03aSJed Brown #undef __FUNCT__
54204af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
54214af1b03aSJed Brown /*@
54224af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
54234af1b03aSJed Brown 
54244af1b03aSJed Brown    Not Collective
54254af1b03aSJed Brown 
54264af1b03aSJed Brown    Input Parameter:
54274af1b03aSJed Brown .  ts - the TS context
54284af1b03aSJed Brown 
54294af1b03aSJed Brown    Output Parameter:
54304af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
54314af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
54324af1b03aSJed Brown 
5433487e0bb9SJed Brown    Level: beginner
54344af1b03aSJed Brown 
5435cd652676SJed Brown    Notes:
5436cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
54374af1b03aSJed Brown 
54384af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
54394af1b03aSJed Brown 
54404af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
54414af1b03aSJed Brown @*/
54424af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
54434af1b03aSJed Brown {
54444af1b03aSJed Brown   PetscFunctionBegin;
54454af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54464af1b03aSJed Brown   PetscValidPointer(reason,2);
54474af1b03aSJed Brown   *reason = ts->reason;
54484af1b03aSJed Brown   PetscFunctionReturn(0);
54494af1b03aSJed Brown }
54504af1b03aSJed Brown 
5451fb1732b5SBarry Smith #undef __FUNCT__
5452d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
5453d6ad946cSShri Abhyankar /*@
5454d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5455d6ad946cSShri Abhyankar 
5456d6ad946cSShri Abhyankar    Not Collective
5457d6ad946cSShri Abhyankar 
5458d6ad946cSShri Abhyankar    Input Parameter:
5459d6ad946cSShri Abhyankar +  ts - the TS context
5460d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5461d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5462d6ad946cSShri Abhyankar 
5463f5abba47SShri Abhyankar    Level: advanced
5464d6ad946cSShri Abhyankar 
5465d6ad946cSShri Abhyankar    Notes:
5466d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5467d6ad946cSShri Abhyankar 
5468d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5469d6ad946cSShri Abhyankar 
5470d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5471d6ad946cSShri Abhyankar @*/
5472d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5473d6ad946cSShri Abhyankar {
5474d6ad946cSShri Abhyankar   PetscFunctionBegin;
5475d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5476d6ad946cSShri Abhyankar   ts->reason = reason;
5477d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5478d6ad946cSShri Abhyankar }
5479d6ad946cSShri Abhyankar 
5480d6ad946cSShri Abhyankar #undef __FUNCT__
5481cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
5482cc708dedSBarry Smith /*@
5483cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5484cc708dedSBarry Smith 
5485cc708dedSBarry Smith    Not Collective
5486cc708dedSBarry Smith 
5487cc708dedSBarry Smith    Input Parameter:
5488cc708dedSBarry Smith .  ts - the TS context
5489cc708dedSBarry Smith 
5490cc708dedSBarry Smith    Output Parameter:
549163e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
5492cc708dedSBarry Smith 
5493487e0bb9SJed Brown    Level: beginner
5494cc708dedSBarry Smith 
5495cc708dedSBarry Smith    Notes:
5496cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5497cc708dedSBarry Smith 
5498cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5499cc708dedSBarry Smith 
5500cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5501cc708dedSBarry Smith @*/
5502cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5503cc708dedSBarry Smith {
5504cc708dedSBarry Smith   PetscFunctionBegin;
5505cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5506cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5507cc708dedSBarry Smith   *ftime = ts->solvetime;
5508cc708dedSBarry Smith   PetscFunctionReturn(0);
5509cc708dedSBarry Smith }
5510cc708dedSBarry Smith 
5511cc708dedSBarry Smith #undef __FUNCT__
55122c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
55132c18e0fdSBarry Smith /*@
55142c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
55152c18e0fdSBarry Smith 
55162c18e0fdSBarry Smith    Not Collective
55172c18e0fdSBarry Smith 
55182c18e0fdSBarry Smith    Input Parameter:
55192c18e0fdSBarry Smith .  ts - the TS context
55202c18e0fdSBarry Smith 
55212c18e0fdSBarry Smith    Output Parameter:
55222c18e0fdSBarry Smith .  steps - the number of steps
55232c18e0fdSBarry Smith 
55242c18e0fdSBarry Smith    Level: beginner
55252c18e0fdSBarry Smith 
55262c18e0fdSBarry Smith    Notes:
55272c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
55282c18e0fdSBarry Smith 
55292c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
55302c18e0fdSBarry Smith 
55312c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
55322c18e0fdSBarry Smith @*/
55332c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
55342c18e0fdSBarry Smith {
55352c18e0fdSBarry Smith   PetscFunctionBegin;
55362c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55372c18e0fdSBarry Smith   PetscValidPointer(steps,2);
55382c18e0fdSBarry Smith   *steps = ts->total_steps;
55392c18e0fdSBarry Smith   PetscFunctionReturn(0);
55402c18e0fdSBarry Smith }
55412c18e0fdSBarry Smith 
55422c18e0fdSBarry Smith #undef __FUNCT__
55435ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
55449f67acb7SJed Brown /*@
55455ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
55469f67acb7SJed Brown    used by the time integrator.
55479f67acb7SJed Brown 
55489f67acb7SJed Brown    Not Collective
55499f67acb7SJed Brown 
55509f67acb7SJed Brown    Input Parameter:
55519f67acb7SJed Brown .  ts - TS context
55529f67acb7SJed Brown 
55539f67acb7SJed Brown    Output Parameter:
55549f67acb7SJed Brown .  nits - number of nonlinear iterations
55559f67acb7SJed Brown 
55569f67acb7SJed Brown    Notes:
55579f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
55589f67acb7SJed Brown 
55599f67acb7SJed Brown    Level: intermediate
55609f67acb7SJed Brown 
55619f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
55629f67acb7SJed Brown 
55635ef26d82SJed Brown .seealso:  TSGetKSPIterations()
55649f67acb7SJed Brown @*/
55655ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
55669f67acb7SJed Brown {
55679f67acb7SJed Brown   PetscFunctionBegin;
55689f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55699f67acb7SJed Brown   PetscValidIntPointer(nits,2);
55705ef26d82SJed Brown   *nits = ts->snes_its;
55719f67acb7SJed Brown   PetscFunctionReturn(0);
55729f67acb7SJed Brown }
55739f67acb7SJed Brown 
55749f67acb7SJed Brown #undef __FUNCT__
55755ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
55769f67acb7SJed Brown /*@
55775ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
55789f67acb7SJed Brown    used by the time integrator.
55799f67acb7SJed Brown 
55809f67acb7SJed Brown    Not Collective
55819f67acb7SJed Brown 
55829f67acb7SJed Brown    Input Parameter:
55839f67acb7SJed Brown .  ts - TS context
55849f67acb7SJed Brown 
55859f67acb7SJed Brown    Output Parameter:
55869f67acb7SJed Brown .  lits - number of linear iterations
55879f67acb7SJed Brown 
55889f67acb7SJed Brown    Notes:
55899f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
55909f67acb7SJed Brown 
55919f67acb7SJed Brown    Level: intermediate
55929f67acb7SJed Brown 
55939f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
55949f67acb7SJed Brown 
55955ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
55969f67acb7SJed Brown @*/
55975ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
55989f67acb7SJed Brown {
55999f67acb7SJed Brown   PetscFunctionBegin;
56009f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56019f67acb7SJed Brown   PetscValidIntPointer(lits,2);
56025ef26d82SJed Brown   *lits = ts->ksp_its;
56039f67acb7SJed Brown   PetscFunctionReturn(0);
56049f67acb7SJed Brown }
56059f67acb7SJed Brown 
56069f67acb7SJed Brown #undef __FUNCT__
5607cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
5608cef5090cSJed Brown /*@
5609cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5610cef5090cSJed Brown 
5611cef5090cSJed Brown    Not Collective
5612cef5090cSJed Brown 
5613cef5090cSJed Brown    Input Parameter:
5614cef5090cSJed Brown .  ts - TS context
5615cef5090cSJed Brown 
5616cef5090cSJed Brown    Output Parameter:
5617cef5090cSJed Brown .  rejects - number of steps rejected
5618cef5090cSJed Brown 
5619cef5090cSJed Brown    Notes:
5620cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5621cef5090cSJed Brown 
5622cef5090cSJed Brown    Level: intermediate
5623cef5090cSJed Brown 
5624cef5090cSJed Brown .keywords: TS, get, number
5625cef5090cSJed Brown 
56265ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5627cef5090cSJed Brown @*/
5628cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5629cef5090cSJed Brown {
5630cef5090cSJed Brown   PetscFunctionBegin;
5631cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5632cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5633cef5090cSJed Brown   *rejects = ts->reject;
5634cef5090cSJed Brown   PetscFunctionReturn(0);
5635cef5090cSJed Brown }
5636cef5090cSJed Brown 
5637cef5090cSJed Brown #undef __FUNCT__
5638cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
5639cef5090cSJed Brown /*@
5640cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5641cef5090cSJed Brown 
5642cef5090cSJed Brown    Not Collective
5643cef5090cSJed Brown 
5644cef5090cSJed Brown    Input Parameter:
5645cef5090cSJed Brown .  ts - TS context
5646cef5090cSJed Brown 
5647cef5090cSJed Brown    Output Parameter:
5648cef5090cSJed Brown .  fails - number of failed nonlinear solves
5649cef5090cSJed Brown 
5650cef5090cSJed Brown    Notes:
5651cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5652cef5090cSJed Brown 
5653cef5090cSJed Brown    Level: intermediate
5654cef5090cSJed Brown 
5655cef5090cSJed Brown .keywords: TS, get, number
5656cef5090cSJed Brown 
56575ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5658cef5090cSJed Brown @*/
5659cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5660cef5090cSJed Brown {
5661cef5090cSJed Brown   PetscFunctionBegin;
5662cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5663cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5664cef5090cSJed Brown   *fails = ts->num_snes_failures;
5665cef5090cSJed Brown   PetscFunctionReturn(0);
5666cef5090cSJed Brown }
5667cef5090cSJed Brown 
5668cef5090cSJed Brown #undef __FUNCT__
5669cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
5670cef5090cSJed Brown /*@
5671cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5672cef5090cSJed Brown 
5673cef5090cSJed Brown    Not Collective
5674cef5090cSJed Brown 
5675cef5090cSJed Brown    Input Parameter:
5676cef5090cSJed Brown +  ts - TS context
5677cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5678cef5090cSJed Brown 
5679cef5090cSJed Brown    Notes:
5680cef5090cSJed Brown    The counter is reset to zero for each step
5681cef5090cSJed Brown 
5682cef5090cSJed Brown    Options Database Key:
5683cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5684cef5090cSJed Brown 
5685cef5090cSJed Brown    Level: intermediate
5686cef5090cSJed Brown 
5687cef5090cSJed Brown .keywords: TS, set, maximum, number
5688cef5090cSJed Brown 
56895ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5690cef5090cSJed Brown @*/
5691cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5692cef5090cSJed Brown {
5693cef5090cSJed Brown   PetscFunctionBegin;
5694cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5695cef5090cSJed Brown   ts->max_reject = rejects;
5696cef5090cSJed Brown   PetscFunctionReturn(0);
5697cef5090cSJed Brown }
5698cef5090cSJed Brown 
5699cef5090cSJed Brown #undef __FUNCT__
5700cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
5701cef5090cSJed Brown /*@
5702cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5703cef5090cSJed Brown 
5704cef5090cSJed Brown    Not Collective
5705cef5090cSJed Brown 
5706cef5090cSJed Brown    Input Parameter:
5707cef5090cSJed Brown +  ts - TS context
5708cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5709cef5090cSJed Brown 
5710cef5090cSJed Brown    Notes:
5711cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5712cef5090cSJed Brown 
5713cef5090cSJed Brown    Options Database Key:
5714cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5715cef5090cSJed Brown 
5716cef5090cSJed Brown    Level: intermediate
5717cef5090cSJed Brown 
5718cef5090cSJed Brown .keywords: TS, set, maximum, number
5719cef5090cSJed Brown 
57205ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5721cef5090cSJed Brown @*/
5722cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5723cef5090cSJed Brown {
5724cef5090cSJed Brown   PetscFunctionBegin;
5725cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5726cef5090cSJed Brown   ts->max_snes_failures = fails;
5727cef5090cSJed Brown   PetscFunctionReturn(0);
5728cef5090cSJed Brown }
5729cef5090cSJed Brown 
5730cef5090cSJed Brown #undef __FUNCT__
57314e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
5732cef5090cSJed Brown /*@
5733cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5734cef5090cSJed Brown 
5735cef5090cSJed Brown    Not Collective
5736cef5090cSJed Brown 
5737cef5090cSJed Brown    Input Parameter:
5738cef5090cSJed Brown +  ts - TS context
5739cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5740cef5090cSJed Brown 
5741cef5090cSJed Brown    Options Database Key:
5742cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5743cef5090cSJed Brown 
5744cef5090cSJed Brown    Level: intermediate
5745cef5090cSJed Brown 
5746cef5090cSJed Brown .keywords: TS, set, error
5747cef5090cSJed Brown 
57485ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5749cef5090cSJed Brown @*/
5750cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5751cef5090cSJed Brown {
5752cef5090cSJed Brown   PetscFunctionBegin;
5753cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5754cef5090cSJed Brown   ts->errorifstepfailed = err;
5755cef5090cSJed Brown   PetscFunctionReturn(0);
5756cef5090cSJed Brown }
5757cef5090cSJed Brown 
5758cef5090cSJed Brown #undef __FUNCT__
5759fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution"
5760fb1732b5SBarry Smith /*@C
5761fde5950dSBarry 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
5762fb1732b5SBarry Smith 
5763fb1732b5SBarry Smith    Collective on TS
5764fb1732b5SBarry Smith 
5765fb1732b5SBarry Smith    Input Parameters:
5766fb1732b5SBarry Smith +  ts - the TS context
5767fb1732b5SBarry Smith .  step - current time-step
5768fb1732b5SBarry Smith .  ptime - current time
57690910c330SBarry Smith .  u - current state
5770721cd6eeSBarry Smith -  vf - viewer and its format
5771fb1732b5SBarry Smith 
5772fb1732b5SBarry Smith    Level: intermediate
5773fb1732b5SBarry Smith 
5774fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5775fb1732b5SBarry Smith 
5776fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5777fb1732b5SBarry Smith @*/
5778721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5779fb1732b5SBarry Smith {
5780fb1732b5SBarry Smith   PetscErrorCode ierr;
5781fb1732b5SBarry Smith 
5782fb1732b5SBarry Smith   PetscFunctionBegin;
5783721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5784721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5785721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5786ed81e22dSJed Brown   PetscFunctionReturn(0);
5787ed81e22dSJed Brown }
5788ed81e22dSJed Brown 
5789ed81e22dSJed Brown #undef __FUNCT__
5790ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
5791ed81e22dSJed Brown /*@C
5792ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5793ed81e22dSJed Brown 
5794ed81e22dSJed Brown    Collective on TS
5795ed81e22dSJed Brown 
5796ed81e22dSJed Brown    Input Parameters:
5797ed81e22dSJed Brown +  ts - the TS context
5798ed81e22dSJed Brown .  step - current time-step
5799ed81e22dSJed Brown .  ptime - current time
58000910c330SBarry Smith .  u - current state
5801ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5802ed81e22dSJed Brown 
5803ed81e22dSJed Brown    Level: intermediate
5804ed81e22dSJed Brown 
5805ed81e22dSJed Brown    Notes:
5806ed81e22dSJed 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.
5807ed81e22dSJed Brown    These are named according to the file name template.
5808ed81e22dSJed Brown 
5809ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5810ed81e22dSJed Brown 
5811ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5812ed81e22dSJed Brown 
5813ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5814ed81e22dSJed Brown @*/
58150910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5816ed81e22dSJed Brown {
5817ed81e22dSJed Brown   PetscErrorCode ierr;
5818ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5819ed81e22dSJed Brown   PetscViewer    viewer;
5820ed81e22dSJed Brown 
5821ed81e22dSJed Brown   PetscFunctionBegin;
582263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
58238caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5824ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
58250910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5826ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5827ed81e22dSJed Brown   PetscFunctionReturn(0);
5828ed81e22dSJed Brown }
5829ed81e22dSJed Brown 
5830ed81e22dSJed Brown #undef __FUNCT__
5831ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
5832ed81e22dSJed Brown /*@C
5833ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5834ed81e22dSJed Brown 
5835ed81e22dSJed Brown    Collective on TS
5836ed81e22dSJed Brown 
5837ed81e22dSJed Brown    Input Parameters:
5838ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5839ed81e22dSJed Brown 
5840ed81e22dSJed Brown    Level: intermediate
5841ed81e22dSJed Brown 
5842ed81e22dSJed Brown    Note:
5843ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5844ed81e22dSJed Brown 
5845ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5846ed81e22dSJed Brown 
5847ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5848ed81e22dSJed Brown @*/
5849ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5850ed81e22dSJed Brown {
5851ed81e22dSJed Brown   PetscErrorCode ierr;
5852ed81e22dSJed Brown 
5853ed81e22dSJed Brown   PetscFunctionBegin;
5854ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5855fb1732b5SBarry Smith   PetscFunctionReturn(0);
5856fb1732b5SBarry Smith }
5857fb1732b5SBarry Smith 
585884df9cb4SJed Brown #undef __FUNCT__
5859552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
586084df9cb4SJed Brown /*@
5861552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
586284df9cb4SJed Brown 
5863ed81e22dSJed Brown    Collective on TS if controller has not been created yet
586484df9cb4SJed Brown 
586584df9cb4SJed Brown    Input Arguments:
5866ed81e22dSJed Brown .  ts - time stepping context
586784df9cb4SJed Brown 
586884df9cb4SJed Brown    Output Arguments:
5869ed81e22dSJed Brown .  adapt - adaptive controller
587084df9cb4SJed Brown 
587184df9cb4SJed Brown    Level: intermediate
587284df9cb4SJed Brown 
5873ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
587484df9cb4SJed Brown @*/
5875552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
587684df9cb4SJed Brown {
587784df9cb4SJed Brown   PetscErrorCode ierr;
587884df9cb4SJed Brown 
587984df9cb4SJed Brown   PetscFunctionBegin;
588084df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5881bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
588284df9cb4SJed Brown   if (!ts->adapt) {
5883ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
58843bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
58851c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
588684df9cb4SJed Brown   }
5887bec58848SLisandro Dalcin   *adapt = ts->adapt;
588884df9cb4SJed Brown   PetscFunctionReturn(0);
588984df9cb4SJed Brown }
5890d6ebe24aSShri Abhyankar 
5891d6ebe24aSShri Abhyankar #undef __FUNCT__
58921c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
58931c3436cfSJed Brown /*@
58941c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
58951c3436cfSJed Brown 
58961c3436cfSJed Brown    Logically Collective
58971c3436cfSJed Brown 
58981c3436cfSJed Brown    Input Arguments:
58991c3436cfSJed Brown +  ts - time integration context
59001c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
59010298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
59021c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
59030298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
59041c3436cfSJed Brown 
5905a3cdaa26SBarry Smith    Options Database keys:
5906a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5907a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5908a3cdaa26SBarry Smith 
59093ff766beSShri Abhyankar    Notes:
59103ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
59113ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
59123ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
59133ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
59143ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
59153ff766beSShri Abhyankar    differential variables.
59163ff766beSShri Abhyankar 
59171c3436cfSJed Brown    Level: beginner
59181c3436cfSJed Brown 
5919c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
59201c3436cfSJed Brown @*/
59211c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
59221c3436cfSJed Brown {
59231c3436cfSJed Brown   PetscErrorCode ierr;
59241c3436cfSJed Brown 
59251c3436cfSJed Brown   PetscFunctionBegin;
5926c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
59271c3436cfSJed Brown   if (vatol) {
59281c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
59291c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
59301c3436cfSJed Brown     ts->vatol = vatol;
59311c3436cfSJed Brown   }
5932c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
59331c3436cfSJed Brown   if (vrtol) {
59341c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
59351c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
59361c3436cfSJed Brown     ts->vrtol = vrtol;
59371c3436cfSJed Brown   }
59381c3436cfSJed Brown   PetscFunctionReturn(0);
59391c3436cfSJed Brown }
59401c3436cfSJed Brown 
59411c3436cfSJed Brown #undef __FUNCT__
5942c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
5943c5033834SJed Brown /*@
5944c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5945c5033834SJed Brown 
5946c5033834SJed Brown    Logically Collective
5947c5033834SJed Brown 
5948c5033834SJed Brown    Input Arguments:
5949c5033834SJed Brown .  ts - time integration context
5950c5033834SJed Brown 
5951c5033834SJed Brown    Output Arguments:
59520298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
59530298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
59540298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
59550298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5956c5033834SJed Brown 
5957c5033834SJed Brown    Level: beginner
5958c5033834SJed Brown 
5959c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5960c5033834SJed Brown @*/
5961c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5962c5033834SJed Brown {
5963c5033834SJed Brown   PetscFunctionBegin;
5964c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5965c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5966c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5967c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5968c5033834SJed Brown   PetscFunctionReturn(0);
5969c5033834SJed Brown }
5970c5033834SJed Brown 
5971c5033834SJed Brown #undef __FUNCT__
59729c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
59739c6b16b5SShri Abhyankar /*@
5974a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
59759c6b16b5SShri Abhyankar 
59769c6b16b5SShri Abhyankar    Collective on TS
59779c6b16b5SShri Abhyankar 
59789c6b16b5SShri Abhyankar    Input Arguments:
59799c6b16b5SShri Abhyankar +  ts - time stepping context
5980a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5981a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
59829c6b16b5SShri Abhyankar 
59839c6b16b5SShri Abhyankar    Output Arguments:
59849c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
59859c6b16b5SShri Abhyankar 
59869c6b16b5SShri Abhyankar    Level: developer
59879c6b16b5SShri Abhyankar 
5988deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
59899c6b16b5SShri Abhyankar @*/
5990a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm)
59919c6b16b5SShri Abhyankar {
59929c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
59939c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
59949c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
59959c6b16b5SShri Abhyankar   PetscReal         sum,gsum;
59969c6b16b5SShri Abhyankar   PetscReal         tol;
59979c6b16b5SShri Abhyankar 
59989c6b16b5SShri Abhyankar   PetscFunctionBegin;
59999c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6000a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6001a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6002a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6003a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6004a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6005a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
6006a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
60079c6b16b5SShri Abhyankar 
60089c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
60099c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
60109c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
60119c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60129c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60139c6b16b5SShri Abhyankar   sum  = 0.;
60149c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
60159c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
60169c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60179c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60189c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
60199c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60209c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
60219c6b16b5SShri Abhyankar     }
60229c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60239c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60249c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60259c6b16b5SShri Abhyankar     const PetscScalar *atol;
60269c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60279c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
60289c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60299c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
60309c6b16b5SShri Abhyankar     }
60319c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60329c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60339c6b16b5SShri Abhyankar     const PetscScalar *rtol;
60349c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60359c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
60369c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60379c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
60389c6b16b5SShri Abhyankar     }
60399c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60409c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
60419c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
60429c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60439c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
60449c6b16b5SShri Abhyankar     }
60459c6b16b5SShri Abhyankar   }
60469c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60479c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60489c6b16b5SShri Abhyankar 
6049b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60509c6b16b5SShri Abhyankar   *norm = PetscSqrtReal(gsum / N);
60519c6b16b5SShri Abhyankar 
60529c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60539c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
60549c6b16b5SShri Abhyankar }
60559c6b16b5SShri Abhyankar 
60569c6b16b5SShri Abhyankar #undef __FUNCT__
60579c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
60589c6b16b5SShri Abhyankar /*@
6059a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
60609c6b16b5SShri Abhyankar 
60619c6b16b5SShri Abhyankar    Collective on TS
60629c6b16b5SShri Abhyankar 
60639c6b16b5SShri Abhyankar    Input Arguments:
60649c6b16b5SShri Abhyankar +  ts - time stepping context
6065a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6066a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
60679c6b16b5SShri Abhyankar 
60689c6b16b5SShri Abhyankar    Output Arguments:
60699c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
60709c6b16b5SShri Abhyankar 
60719c6b16b5SShri Abhyankar    Level: developer
60729c6b16b5SShri Abhyankar 
6073deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
60749c6b16b5SShri Abhyankar @*/
6075a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm)
60769c6b16b5SShri Abhyankar {
60779c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
60789c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart,k;
60799c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
60809c6b16b5SShri Abhyankar   PetscReal         max,gmax;
60819c6b16b5SShri Abhyankar   PetscReal         tol;
60829c6b16b5SShri Abhyankar 
60839c6b16b5SShri Abhyankar   PetscFunctionBegin;
60849c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6085a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
6086a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
6087a4868fbcSLisandro Dalcin   PetscValidType(U,2);
6088a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
6089a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
6090a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
6091a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
60929c6b16b5SShri Abhyankar 
60939c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
60949c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
60959c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
60969c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60979c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60989c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
60999c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
61009c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61019c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61029c6b16b5SShri Abhyankar     k = 0;
61039c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
61049c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
61059c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
61069c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61079c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
61089c6b16b5SShri Abhyankar     }
61099c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61109c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61119c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61129c6b16b5SShri Abhyankar     const PetscScalar *atol;
61139c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61149c6b16b5SShri Abhyankar     k = 0;
61159c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
61169c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
61179c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
61189c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61199c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
61209c6b16b5SShri Abhyankar     }
61219c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61229c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61239c6b16b5SShri Abhyankar     const PetscScalar *rtol;
61249c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61259c6b16b5SShri Abhyankar     k = 0;
61269c6b16b5SShri Abhyankar     tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
61279c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
61289c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
61299c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61309c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
61319c6b16b5SShri Abhyankar     }
61329c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61339c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
61349c6b16b5SShri Abhyankar     k = 0;
61359c6b16b5SShri Abhyankar     tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
61369c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
61379c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
61389c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61399c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
61409c6b16b5SShri Abhyankar     }
61419c6b16b5SShri Abhyankar   }
61429c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61439c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61449c6b16b5SShri Abhyankar 
6145b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61469c6b16b5SShri Abhyankar   *norm = gmax;
61479c6b16b5SShri Abhyankar 
61489c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61499c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
61509c6b16b5SShri Abhyankar }
61519c6b16b5SShri Abhyankar 
61529c6b16b5SShri Abhyankar #undef __FUNCT__
61537619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
61541c3436cfSJed Brown /*@
6155a4868fbcSLisandro Dalcin    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors
61561c3436cfSJed Brown 
61571c3436cfSJed Brown    Collective on TS
61581c3436cfSJed Brown 
61591c3436cfSJed Brown    Input Arguments:
61601c3436cfSJed Brown +  ts - time stepping context
6161a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
6162a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
6163a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61647619abb3SShri 
61651c3436cfSJed Brown    Output Arguments:
61661c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
61671c3436cfSJed Brown 
6168a4868fbcSLisandro Dalcin 
6169a4868fbcSLisandro Dalcin    Options Database Keys:
6170a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
6171a4868fbcSLisandro Dalcin 
61721c3436cfSJed Brown    Level: developer
61731c3436cfSJed Brown 
6174deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
61751c3436cfSJed Brown @*/
6176a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm)
61771c3436cfSJed Brown {
61788beabaa1SBarry Smith   PetscErrorCode ierr;
61791c3436cfSJed Brown 
61801c3436cfSJed Brown   PetscFunctionBegin;
6181a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
6182a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr);
6183a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
6184a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr);
6185a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61861c3436cfSJed Brown   PetscFunctionReturn(0);
61871c3436cfSJed Brown }
61881c3436cfSJed Brown 
61891c3436cfSJed Brown #undef __FUNCT__
61908d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
61918d59e960SJed Brown /*@
61928d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
61938d59e960SJed Brown 
61948d59e960SJed Brown    Logically Collective on TS
61958d59e960SJed Brown 
61968d59e960SJed Brown    Input Arguments:
61978d59e960SJed Brown +  ts - time stepping context
61988d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
61998d59e960SJed Brown 
62008d59e960SJed Brown    Note:
62018d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
62028d59e960SJed Brown 
62038d59e960SJed Brown    Level: intermediate
62048d59e960SJed Brown 
62058d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
62068d59e960SJed Brown @*/
62078d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
62088d59e960SJed Brown {
62098d59e960SJed Brown   PetscFunctionBegin;
62108d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62118d59e960SJed Brown   ts->cfltime_local = cfltime;
62128d59e960SJed Brown   ts->cfltime       = -1.;
62138d59e960SJed Brown   PetscFunctionReturn(0);
62148d59e960SJed Brown }
62158d59e960SJed Brown 
62168d59e960SJed Brown #undef __FUNCT__
62178d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
62188d59e960SJed Brown /*@
62198d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
62208d59e960SJed Brown 
62218d59e960SJed Brown    Collective on TS
62228d59e960SJed Brown 
62238d59e960SJed Brown    Input Arguments:
62248d59e960SJed Brown .  ts - time stepping context
62258d59e960SJed Brown 
62268d59e960SJed Brown    Output Arguments:
62278d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
62288d59e960SJed Brown 
62298d59e960SJed Brown    Level: advanced
62308d59e960SJed Brown 
62318d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
62328d59e960SJed Brown @*/
62338d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
62348d59e960SJed Brown {
62358d59e960SJed Brown   PetscErrorCode ierr;
62368d59e960SJed Brown 
62378d59e960SJed Brown   PetscFunctionBegin;
62388d59e960SJed Brown   if (ts->cfltime < 0) {
6239b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62408d59e960SJed Brown   }
62418d59e960SJed Brown   *cfltime = ts->cfltime;
62428d59e960SJed Brown   PetscFunctionReturn(0);
62438d59e960SJed Brown }
62448d59e960SJed Brown 
62458d59e960SJed Brown #undef __FUNCT__
6246d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
6247d6ebe24aSShri Abhyankar /*@
6248d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6249d6ebe24aSShri Abhyankar 
6250d6ebe24aSShri Abhyankar    Input Parameters:
6251d6ebe24aSShri Abhyankar .  ts   - the TS context.
6252d6ebe24aSShri Abhyankar .  xl   - lower bound.
6253d6ebe24aSShri Abhyankar .  xu   - upper bound.
6254d6ebe24aSShri Abhyankar 
6255d6ebe24aSShri Abhyankar    Notes:
6256d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6257e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6258d6ebe24aSShri Abhyankar 
62592bd2b0e6SSatish Balay    Level: advanced
62602bd2b0e6SSatish Balay 
6261d6ebe24aSShri Abhyankar @*/
6262d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6263d6ebe24aSShri Abhyankar {
6264d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6265d6ebe24aSShri Abhyankar   SNES           snes;
6266d6ebe24aSShri Abhyankar 
6267d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6268d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6269d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6270d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6271d6ebe24aSShri Abhyankar }
6272d6ebe24aSShri Abhyankar 
6273325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6274c6db04a5SJed Brown #include <mex.h>
6275325fc9f4SBarry Smith 
6276325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6277325fc9f4SBarry Smith 
6278325fc9f4SBarry Smith #undef __FUNCT__
6279325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
6280325fc9f4SBarry Smith /*
6281325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6282325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6283325fc9f4SBarry Smith 
6284325fc9f4SBarry Smith    Collective on TS
6285325fc9f4SBarry Smith 
6286325fc9f4SBarry Smith    Input Parameters:
6287325fc9f4SBarry Smith +  snes - the TS context
62880910c330SBarry Smith -  u - input vector
6289325fc9f4SBarry Smith 
6290325fc9f4SBarry Smith    Output Parameter:
6291325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6292325fc9f4SBarry Smith 
6293325fc9f4SBarry Smith    Notes:
6294325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6295325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6296325fc9f4SBarry Smith    themselves.
6297325fc9f4SBarry Smith 
6298325fc9f4SBarry Smith    Level: developer
6299325fc9f4SBarry Smith 
6300325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6301325fc9f4SBarry Smith 
6302325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6303325fc9f4SBarry Smith */
63040910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6305325fc9f4SBarry Smith {
6306325fc9f4SBarry Smith   PetscErrorCode  ierr;
6307325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6308325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6309325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6310325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6311325fc9f4SBarry Smith 
6312325fc9f4SBarry Smith   PetscFunctionBegin;
6313325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
63140910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
63150910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6316325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
63170910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6318325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6319325fc9f4SBarry Smith 
6320325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
63210910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
63220910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
63230910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6324bbd56ea5SKarl Rupp 
6325325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6326325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6327325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6328325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6329325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6330325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6331325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6332325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6333325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6334325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6335325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6336325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6337325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6338325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6339325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6340325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6341325fc9f4SBarry Smith   PetscFunctionReturn(0);
6342325fc9f4SBarry Smith }
6343325fc9f4SBarry Smith 
6344325fc9f4SBarry Smith 
6345325fc9f4SBarry Smith #undef __FUNCT__
6346325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
6347325fc9f4SBarry Smith /*
6348325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6349325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6350e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6351325fc9f4SBarry Smith 
6352325fc9f4SBarry Smith    Logically Collective on TS
6353325fc9f4SBarry Smith 
6354325fc9f4SBarry Smith    Input Parameters:
6355325fc9f4SBarry Smith +  ts - the TS context
6356325fc9f4SBarry Smith -  func - function evaluation routine
6357325fc9f4SBarry Smith 
6358325fc9f4SBarry Smith    Calling sequence of func:
63590910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6360325fc9f4SBarry Smith 
6361325fc9f4SBarry Smith    Level: beginner
6362325fc9f4SBarry Smith 
6363325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6364325fc9f4SBarry Smith 
6365325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6366325fc9f4SBarry Smith */
6367cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6368325fc9f4SBarry Smith {
6369325fc9f4SBarry Smith   PetscErrorCode  ierr;
6370325fc9f4SBarry Smith   TSMatlabContext *sctx;
6371325fc9f4SBarry Smith 
6372325fc9f4SBarry Smith   PetscFunctionBegin;
6373325fc9f4SBarry Smith   /* currently sctx is memory bleed */
637495dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6375325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6376325fc9f4SBarry Smith   /*
6377325fc9f4SBarry Smith      This should work, but it doesn't
6378325fc9f4SBarry Smith   sctx->ctx = ctx;
6379325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6380325fc9f4SBarry Smith   */
6381325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6382bbd56ea5SKarl Rupp 
63830298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6384325fc9f4SBarry Smith   PetscFunctionReturn(0);
6385325fc9f4SBarry Smith }
6386325fc9f4SBarry Smith 
6387325fc9f4SBarry Smith #undef __FUNCT__
6388325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
6389325fc9f4SBarry Smith /*
6390325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6391325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6392325fc9f4SBarry Smith 
6393325fc9f4SBarry Smith    Collective on TS
6394325fc9f4SBarry Smith 
6395325fc9f4SBarry Smith    Input Parameters:
6396cdcf91faSSean Farley +  ts - the TS context
63970910c330SBarry Smith .  u - input vector
6398325fc9f4SBarry Smith .  A, B - the matrices
6399325fc9f4SBarry Smith -  ctx - user context
6400325fc9f4SBarry Smith 
6401325fc9f4SBarry Smith    Level: developer
6402325fc9f4SBarry Smith 
6403325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6404325fc9f4SBarry Smith 
6405325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6406325fc9f4SBarry Smith @*/
6407f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6408325fc9f4SBarry Smith {
6409325fc9f4SBarry Smith   PetscErrorCode  ierr;
6410325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6411325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6412325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6413325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6414325fc9f4SBarry Smith 
6415325fc9f4SBarry Smith   PetscFunctionBegin;
6416cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64170910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6418325fc9f4SBarry Smith 
64190910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6420325fc9f4SBarry Smith 
6421cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
64220910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
64230910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
64240910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
64250910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6426bbd56ea5SKarl Rupp 
6427325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6428325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6429325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6430325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6431325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6432325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6433325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6434325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6435325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6436325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6437325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6438325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6439325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6440325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6441325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6442325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6443325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6444325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6445325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6446325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6447325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6448325fc9f4SBarry Smith   PetscFunctionReturn(0);
6449325fc9f4SBarry Smith }
6450325fc9f4SBarry Smith 
6451325fc9f4SBarry Smith 
6452325fc9f4SBarry Smith #undef __FUNCT__
6453325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
6454325fc9f4SBarry Smith /*
6455325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6456e3c5b3baSBarry 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
6457325fc9f4SBarry Smith 
6458325fc9f4SBarry Smith    Logically Collective on TS
6459325fc9f4SBarry Smith 
6460325fc9f4SBarry Smith    Input Parameters:
6461cdcf91faSSean Farley +  ts - the TS context
6462325fc9f4SBarry Smith .  A,B - Jacobian matrices
6463325fc9f4SBarry Smith .  func - function evaluation routine
6464325fc9f4SBarry Smith -  ctx - user context
6465325fc9f4SBarry Smith 
6466325fc9f4SBarry Smith    Calling sequence of func:
64670910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6468325fc9f4SBarry Smith 
6469325fc9f4SBarry Smith 
6470325fc9f4SBarry Smith    Level: developer
6471325fc9f4SBarry Smith 
6472325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6473325fc9f4SBarry Smith 
6474325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6475325fc9f4SBarry Smith */
6476cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6477325fc9f4SBarry Smith {
6478325fc9f4SBarry Smith   PetscErrorCode  ierr;
6479325fc9f4SBarry Smith   TSMatlabContext *sctx;
6480325fc9f4SBarry Smith 
6481325fc9f4SBarry Smith   PetscFunctionBegin;
6482325fc9f4SBarry Smith   /* currently sctx is memory bleed */
648395dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6484325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6485325fc9f4SBarry Smith   /*
6486325fc9f4SBarry Smith      This should work, but it doesn't
6487325fc9f4SBarry Smith   sctx->ctx = ctx;
6488325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6489325fc9f4SBarry Smith   */
6490325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6491bbd56ea5SKarl Rupp 
6492cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6493325fc9f4SBarry Smith   PetscFunctionReturn(0);
6494325fc9f4SBarry Smith }
6495325fc9f4SBarry Smith 
6496b5b1a830SBarry Smith #undef __FUNCT__
6497b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
6498b5b1a830SBarry Smith /*
6499b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6500b5b1a830SBarry Smith 
6501b5b1a830SBarry Smith    Collective on TS
6502b5b1a830SBarry Smith 
6503b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6504b5b1a830SBarry Smith @*/
65050910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6506b5b1a830SBarry Smith {
6507b5b1a830SBarry Smith   PetscErrorCode  ierr;
6508b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6509a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6510b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6511b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6512b5b1a830SBarry Smith 
6513b5b1a830SBarry Smith   PetscFunctionBegin;
6514cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65150910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6516b5b1a830SBarry Smith 
6517cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
65180910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6519bbd56ea5SKarl Rupp 
6520b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6521b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6522b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6523b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6524b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6525b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6526b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6527b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6528b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6529b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6530b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6531b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6532b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6533b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6534b5b1a830SBarry Smith   PetscFunctionReturn(0);
6535b5b1a830SBarry Smith }
6536b5b1a830SBarry Smith 
6537b5b1a830SBarry Smith 
6538b5b1a830SBarry Smith #undef __FUNCT__
6539b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
6540b5b1a830SBarry Smith /*
6541b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6542b5b1a830SBarry Smith 
6543b5b1a830SBarry Smith    Level: developer
6544b5b1a830SBarry Smith 
6545b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6546b5b1a830SBarry Smith 
6547b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6548b5b1a830SBarry Smith */
6549cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6550b5b1a830SBarry Smith {
6551b5b1a830SBarry Smith   PetscErrorCode  ierr;
6552b5b1a830SBarry Smith   TSMatlabContext *sctx;
6553b5b1a830SBarry Smith 
6554b5b1a830SBarry Smith   PetscFunctionBegin;
6555b5b1a830SBarry Smith   /* currently sctx is memory bleed */
655695dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6557b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6558b5b1a830SBarry Smith   /*
6559b5b1a830SBarry Smith      This should work, but it doesn't
6560b5b1a830SBarry Smith   sctx->ctx = ctx;
6561b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6562b5b1a830SBarry Smith   */
6563b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6564bbd56ea5SKarl Rupp 
65650298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6566b5b1a830SBarry Smith   PetscFunctionReturn(0);
6567b5b1a830SBarry Smith }
6568325fc9f4SBarry Smith #endif
6569b3603a34SBarry Smith 
6570b3603a34SBarry Smith #undef __FUNCT__
65714f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
6572b3603a34SBarry Smith /*@C
65734f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6574b3603a34SBarry Smith        in a time based line graph
6575b3603a34SBarry Smith 
6576b3603a34SBarry Smith    Collective on TS
6577b3603a34SBarry Smith 
6578b3603a34SBarry Smith    Input Parameters:
6579b3603a34SBarry Smith +  ts - the TS context
6580b3603a34SBarry Smith .  step - current time-step
6581b3603a34SBarry Smith .  ptime - current time
65827db568b7SBarry Smith .  u - current solution
65837db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6584b3603a34SBarry Smith 
6585b3d3934dSBarry Smith    Options Database:
65869ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6587b3d3934dSBarry Smith 
6588b3603a34SBarry Smith    Level: intermediate
6589b3603a34SBarry Smith 
65906934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
6591b3603a34SBarry Smith 
6592b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6593b3603a34SBarry Smith 
65947db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65957db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65967db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65977db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6598b3603a34SBarry Smith @*/
65997db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6600b3603a34SBarry Smith {
6601b3603a34SBarry Smith   PetscErrorCode    ierr;
66027db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6603b3603a34SBarry Smith   const PetscScalar *yy;
660480666b62SBarry Smith   Vec               v;
6605b3603a34SBarry Smith 
6606b3603a34SBarry Smith   PetscFunctionBegin;
660763e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
660858ff32f7SBarry Smith   if (!step) {
6609a9f9c1f6SBarry Smith     PetscDrawAxis axis;
66106934998bSLisandro Dalcin     PetscInt      dim;
6611a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6612a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6613bab0a581SBarry Smith     if (!ctx->names) {
6614bab0a581SBarry Smith       PetscBool flg;
6615bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6616bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6617bab0a581SBarry Smith       if (flg) {
6618bab0a581SBarry Smith         PetscInt i,n;
6619bab0a581SBarry Smith         char     **names;
6620bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6621bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6622bab0a581SBarry Smith         for (i=0; i<n; i++) {
6623bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6624bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6625bab0a581SBarry Smith         }
6626bab0a581SBarry Smith         names[n] = NULL;
6627bab0a581SBarry Smith         ctx->names = names;
6628bab0a581SBarry Smith       }
6629bab0a581SBarry Smith     }
6630387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6631387f4636SBarry Smith       char      **displaynames;
6632387f4636SBarry Smith       PetscBool flg;
6633387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
663495dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6635387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6636c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6637387f4636SBarry Smith       if (flg) {
6638a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6639387f4636SBarry Smith       }
6640387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6641387f4636SBarry Smith     }
6642387f4636SBarry Smith     if (ctx->displaynames) {
6643387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6644387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6645387f4636SBarry Smith     } else if (ctx->names) {
66460910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
66470b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6648387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6649b0bc92c6SBarry Smith     } else {
6650b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6651b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6652387f4636SBarry Smith     }
66530b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
665458ff32f7SBarry Smith   }
66556934998bSLisandro Dalcin 
66566934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66576934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
665880666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66596934998bSLisandro Dalcin   if (ctx->displaynames) {
66606934998bSLisandro Dalcin     PetscInt i;
66616934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66626934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66636934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66646934998bSLisandro Dalcin   } else {
6665e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6666e3efe391SJed Brown     PetscInt  i,n;
66676934998bSLisandro Dalcin     PetscReal *yreal;
666880666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6669785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6670e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66710b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6672e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6673e3efe391SJed Brown #else
66740b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6675e3efe391SJed Brown #endif
667680666b62SBarry Smith   }
66776934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66786934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66796934998bSLisandro Dalcin 
6680b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66810b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66826934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66833923b477SBarry Smith   }
6684b3603a34SBarry Smith   PetscFunctionReturn(0);
6685b3603a34SBarry Smith }
6686b3603a34SBarry Smith 
6687387f4636SBarry Smith 
6688b3603a34SBarry Smith #undef __FUNCT__
668931152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
6690b037adc7SBarry Smith /*@C
669131152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6692b037adc7SBarry Smith 
6693b037adc7SBarry Smith    Collective on TS
6694b037adc7SBarry Smith 
6695b037adc7SBarry Smith    Input Parameters:
6696b037adc7SBarry Smith +  ts - the TS context
6697b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6698b037adc7SBarry Smith 
6699b037adc7SBarry Smith    Level: intermediate
6700b037adc7SBarry Smith 
67017db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67027db568b7SBarry Smith 
6703b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6704b037adc7SBarry Smith 
6705a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6706b037adc7SBarry Smith @*/
670731152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6708b037adc7SBarry Smith {
6709b037adc7SBarry Smith   PetscErrorCode    ierr;
6710b037adc7SBarry Smith   PetscInt          i;
6711b037adc7SBarry Smith 
6712b037adc7SBarry Smith   PetscFunctionBegin;
6713b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6714b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67155537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6716b3d3934dSBarry Smith       break;
6717b3d3934dSBarry Smith     }
6718b3d3934dSBarry Smith   }
6719b3d3934dSBarry Smith   PetscFunctionReturn(0);
6720b3d3934dSBarry Smith }
6721b3d3934dSBarry Smith 
6722b3d3934dSBarry Smith #undef __FUNCT__
6723e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
6724e673d494SBarry Smith /*@C
6725e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6726e673d494SBarry Smith 
6727e673d494SBarry Smith    Collective on TS
6728e673d494SBarry Smith 
6729e673d494SBarry Smith    Input Parameters:
6730e673d494SBarry Smith +  ts - the TS context
6731e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6732e673d494SBarry Smith 
6733e673d494SBarry Smith    Level: intermediate
6734e673d494SBarry Smith 
6735e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6736e673d494SBarry Smith 
6737a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6738e673d494SBarry Smith @*/
6739e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6740e673d494SBarry Smith {
6741e673d494SBarry Smith   PetscErrorCode    ierr;
6742e673d494SBarry Smith 
6743e673d494SBarry Smith   PetscFunctionBegin;
6744e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6745e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6746e673d494SBarry Smith   PetscFunctionReturn(0);
6747e673d494SBarry Smith }
6748e673d494SBarry Smith 
6749e673d494SBarry Smith #undef __FUNCT__
6750b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
6751b3d3934dSBarry Smith /*@C
6752b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6753b3d3934dSBarry Smith 
6754b3d3934dSBarry Smith    Collective on TS
6755b3d3934dSBarry Smith 
6756b3d3934dSBarry Smith    Input Parameter:
6757b3d3934dSBarry Smith .  ts - the TS context
6758b3d3934dSBarry Smith 
6759b3d3934dSBarry Smith    Output Parameter:
6760b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6761b3d3934dSBarry Smith 
6762b3d3934dSBarry Smith    Level: intermediate
6763b3d3934dSBarry Smith 
67647db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67657db568b7SBarry Smith 
6766b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6767b3d3934dSBarry Smith 
6768b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6769b3d3934dSBarry Smith @*/
6770b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6771b3d3934dSBarry Smith {
6772b3d3934dSBarry Smith   PetscInt       i;
6773b3d3934dSBarry Smith 
6774b3d3934dSBarry Smith   PetscFunctionBegin;
6775b3d3934dSBarry Smith   *names = NULL;
6776b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6777b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67785537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6779b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6780b3d3934dSBarry Smith       break;
6781387f4636SBarry Smith     }
6782387f4636SBarry Smith   }
6783387f4636SBarry Smith   PetscFunctionReturn(0);
6784387f4636SBarry Smith }
6785387f4636SBarry Smith 
6786387f4636SBarry Smith #undef __FUNCT__
6787a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
6788a66092f1SBarry Smith /*@C
6789a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6790a66092f1SBarry Smith 
6791a66092f1SBarry Smith    Collective on TS
6792a66092f1SBarry Smith 
6793a66092f1SBarry Smith    Input Parameters:
6794a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6795a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6796a66092f1SBarry Smith 
6797a66092f1SBarry Smith    Level: intermediate
6798a66092f1SBarry Smith 
6799a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6800a66092f1SBarry Smith 
6801a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6802a66092f1SBarry Smith @*/
6803a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6804a66092f1SBarry Smith {
6805a66092f1SBarry Smith   PetscInt          j = 0,k;
6806a66092f1SBarry Smith   PetscErrorCode    ierr;
6807a66092f1SBarry Smith 
6808a66092f1SBarry Smith   PetscFunctionBegin;
6809a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6810a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6811a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6812a66092f1SBarry Smith   while (displaynames[j]) j++;
6813a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6814a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6815a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6816a66092f1SBarry Smith   j = 0;
6817a66092f1SBarry Smith   while (displaynames[j]) {
6818a66092f1SBarry Smith     k = 0;
6819a66092f1SBarry Smith     while (ctx->names[k]) {
6820a66092f1SBarry Smith       PetscBool flg;
6821a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6822a66092f1SBarry Smith       if (flg) {
6823a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6824a66092f1SBarry Smith         break;
6825a66092f1SBarry Smith       }
6826a66092f1SBarry Smith       k++;
6827a66092f1SBarry Smith     }
6828a66092f1SBarry Smith     j++;
6829a66092f1SBarry Smith   }
6830a66092f1SBarry Smith   PetscFunctionReturn(0);
6831a66092f1SBarry Smith }
6832a66092f1SBarry Smith 
6833a66092f1SBarry Smith 
6834a66092f1SBarry Smith #undef __FUNCT__
6835387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
6836387f4636SBarry Smith /*@C
6837387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6838387f4636SBarry Smith 
6839387f4636SBarry Smith    Collective on TS
6840387f4636SBarry Smith 
6841387f4636SBarry Smith    Input Parameters:
6842387f4636SBarry Smith +  ts - the TS context
6843387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6844387f4636SBarry Smith 
68457db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68467db568b7SBarry Smith 
6847387f4636SBarry Smith    Level: intermediate
6848387f4636SBarry Smith 
6849387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6850387f4636SBarry Smith 
6851387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6852387f4636SBarry Smith @*/
6853387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6854387f4636SBarry Smith {
6855a66092f1SBarry Smith   PetscInt          i;
6856387f4636SBarry Smith   PetscErrorCode    ierr;
6857387f4636SBarry Smith 
6858387f4636SBarry Smith   PetscFunctionBegin;
6859387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6860387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68615537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6862b3d3934dSBarry Smith       break;
6863b037adc7SBarry Smith     }
6864b037adc7SBarry Smith   }
6865b037adc7SBarry Smith   PetscFunctionReturn(0);
6866b037adc7SBarry Smith }
6867b037adc7SBarry Smith 
6868b037adc7SBarry Smith #undef __FUNCT__
686980666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
687080666b62SBarry Smith /*@C
687180666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
687280666b62SBarry Smith 
687380666b62SBarry Smith    Collective on TS
687480666b62SBarry Smith 
687580666b62SBarry Smith    Input Parameters:
687680666b62SBarry Smith +  ts - the TS context
687780666b62SBarry Smith .  transform - the transform function
68787684fa3eSBarry Smith .  destroy - function to destroy the optional context
687980666b62SBarry Smith -  ctx - optional context used by transform function
688080666b62SBarry Smith 
68817db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68827db568b7SBarry Smith 
688380666b62SBarry Smith    Level: intermediate
688480666b62SBarry Smith 
688580666b62SBarry Smith .keywords: TS,  vector, monitor, view
688680666b62SBarry Smith 
6887a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
688880666b62SBarry Smith @*/
68897684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
689080666b62SBarry Smith {
689180666b62SBarry Smith   PetscInt          i;
6892a66092f1SBarry Smith   PetscErrorCode    ierr;
689380666b62SBarry Smith 
689480666b62SBarry Smith   PetscFunctionBegin;
689580666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
689680666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68975537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
689880666b62SBarry Smith     }
689980666b62SBarry Smith   }
690080666b62SBarry Smith   PetscFunctionReturn(0);
690180666b62SBarry Smith }
690280666b62SBarry Smith 
690380666b62SBarry Smith #undef __FUNCT__
6904e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
6905e673d494SBarry Smith /*@C
6906e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6907e673d494SBarry Smith 
6908e673d494SBarry Smith    Collective on TSLGCtx
6909e673d494SBarry Smith 
6910e673d494SBarry Smith    Input Parameters:
6911e673d494SBarry Smith +  ts - the TS context
6912e673d494SBarry Smith .  transform - the transform function
69137684fa3eSBarry Smith .  destroy - function to destroy the optional context
6914e673d494SBarry Smith -  ctx - optional context used by transform function
6915e673d494SBarry Smith 
6916e673d494SBarry Smith    Level: intermediate
6917e673d494SBarry Smith 
6918e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6919e673d494SBarry Smith 
6920a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6921e673d494SBarry Smith @*/
69227684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6923e673d494SBarry Smith {
6924e673d494SBarry Smith   PetscFunctionBegin;
6925e673d494SBarry Smith   ctx->transform    = transform;
69267684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6927e673d494SBarry Smith   ctx->transformctx = tctx;
6928e673d494SBarry Smith   PetscFunctionReturn(0);
6929e673d494SBarry Smith }
6930e673d494SBarry Smith 
6931b3603a34SBarry Smith #undef __FUNCT__
69324f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
6933ef20d060SBarry Smith /*@C
69344f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
6935ef20d060SBarry Smith        in a time based line graph
6936ef20d060SBarry Smith 
6937ef20d060SBarry Smith    Collective on TS
6938ef20d060SBarry Smith 
6939ef20d060SBarry Smith    Input Parameters:
6940ef20d060SBarry Smith +  ts - the TS context
6941ef20d060SBarry Smith .  step - current time-step
6942ef20d060SBarry Smith .  ptime - current time
69437db568b7SBarry Smith .  u - current solution
69447db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6945ef20d060SBarry Smith 
6946ef20d060SBarry Smith    Level: intermediate
6947ef20d060SBarry Smith 
69486934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
6949abd5a294SJed Brown 
6950abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6951abd5a294SJed Brown 
6952abd5a294SJed Brown    Options Database Keys:
69534f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6954ef20d060SBarry Smith 
6955ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6956ef20d060SBarry Smith 
6957abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6958ef20d060SBarry Smith @*/
69590910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6960ef20d060SBarry Smith {
6961ef20d060SBarry Smith   PetscErrorCode    ierr;
69620b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6963ef20d060SBarry Smith   const PetscScalar *yy;
6964ef20d060SBarry Smith   Vec               y;
6965ef20d060SBarry Smith 
6966ef20d060SBarry Smith   PetscFunctionBegin;
6967a9f9c1f6SBarry Smith   if (!step) {
6968a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69696934998bSLisandro Dalcin     PetscInt      dim;
6970a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
69711ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
69720910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6973a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6974a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6975a9f9c1f6SBarry Smith   }
69760910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6977ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69780910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6979ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6980e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6981e3efe391SJed Brown   {
6982e3efe391SJed Brown     PetscReal *yreal;
6983e3efe391SJed Brown     PetscInt  i,n;
6984e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6985785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6986e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69870b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6988e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6989e3efe391SJed Brown   }
6990e3efe391SJed Brown #else
69910b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6992e3efe391SJed Brown #endif
6993ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6994ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6995b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69960b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69976934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69983923b477SBarry Smith   }
6999ef20d060SBarry Smith   PetscFunctionReturn(0);
7000ef20d060SBarry Smith }
7001ef20d060SBarry Smith 
7002201da799SBarry Smith #undef __FUNCT__
7003201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
7004201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7005201da799SBarry Smith {
7006201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7007201da799SBarry Smith   PetscReal      x   = ptime,y;
7008201da799SBarry Smith   PetscErrorCode ierr;
7009201da799SBarry Smith   PetscInt       its;
7010201da799SBarry Smith 
7011201da799SBarry Smith   PetscFunctionBegin;
701263e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7013201da799SBarry Smith   if (!n) {
7014201da799SBarry Smith     PetscDrawAxis axis;
7015201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7016201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7017201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7018201da799SBarry Smith     ctx->snes_its = 0;
7019201da799SBarry Smith   }
7020201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7021201da799SBarry Smith   y    = its - ctx->snes_its;
7022201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70233fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7024201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70256934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7026201da799SBarry Smith   }
7027201da799SBarry Smith   ctx->snes_its = its;
7028201da799SBarry Smith   PetscFunctionReturn(0);
7029201da799SBarry Smith }
7030201da799SBarry Smith 
7031201da799SBarry Smith #undef __FUNCT__
7032201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
7033201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7034201da799SBarry Smith {
7035201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7036201da799SBarry Smith   PetscReal      x   = ptime,y;
7037201da799SBarry Smith   PetscErrorCode ierr;
7038201da799SBarry Smith   PetscInt       its;
7039201da799SBarry Smith 
7040201da799SBarry Smith   PetscFunctionBegin;
704163e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7042201da799SBarry Smith   if (!n) {
7043201da799SBarry Smith     PetscDrawAxis axis;
7044201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7045201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7046201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7047201da799SBarry Smith     ctx->ksp_its = 0;
7048201da799SBarry Smith   }
7049201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7050201da799SBarry Smith   y    = its - ctx->ksp_its;
7051201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
705299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7053201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70546934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7055201da799SBarry Smith   }
7056201da799SBarry Smith   ctx->ksp_its = its;
7057201da799SBarry Smith   PetscFunctionReturn(0);
7058201da799SBarry Smith }
7059f9c1d6abSBarry Smith 
7060f9c1d6abSBarry Smith #undef __FUNCT__
7061f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
7062f9c1d6abSBarry Smith /*@
7063f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7064f9c1d6abSBarry Smith 
7065f9c1d6abSBarry Smith    Collective on TS and Vec
7066f9c1d6abSBarry Smith 
7067f9c1d6abSBarry Smith    Input Parameters:
7068f9c1d6abSBarry Smith +  ts - the TS context
7069f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7070f9c1d6abSBarry Smith 
7071f9c1d6abSBarry Smith    Output Parameters:
7072f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7073f9c1d6abSBarry Smith 
7074f9c1d6abSBarry Smith    Level: developer
7075f9c1d6abSBarry Smith 
7076f9c1d6abSBarry Smith .keywords: TS, compute
7077f9c1d6abSBarry Smith 
7078f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7079f9c1d6abSBarry Smith @*/
7080f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7081f9c1d6abSBarry Smith {
7082f9c1d6abSBarry Smith   PetscErrorCode ierr;
7083f9c1d6abSBarry Smith 
7084f9c1d6abSBarry Smith   PetscFunctionBegin;
7085f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7086ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7087f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7088f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7089f9c1d6abSBarry Smith }
709024655328SShri 
7091b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7092b3d3934dSBarry Smith #undef __FUNCT__
7093b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
7094b3d3934dSBarry Smith /*@C
7095b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7096b3d3934dSBarry Smith 
7097b3d3934dSBarry Smith    Collective on TS
7098b3d3934dSBarry Smith 
7099b3d3934dSBarry Smith    Input Parameters:
7100b3d3934dSBarry Smith .  ts  - the ODE solver object
7101b3d3934dSBarry Smith 
7102b3d3934dSBarry Smith    Output Parameter:
7103b3d3934dSBarry Smith .  ctx - the context
7104b3d3934dSBarry Smith 
7105b3d3934dSBarry Smith    Level: intermediate
7106b3d3934dSBarry Smith 
7107b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7108b3d3934dSBarry Smith 
7109b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7110b3d3934dSBarry Smith 
7111b3d3934dSBarry Smith @*/
7112b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7113b3d3934dSBarry Smith {
7114b3d3934dSBarry Smith   PetscErrorCode ierr;
7115b3d3934dSBarry Smith 
7116b3d3934dSBarry Smith   PetscFunctionBegin;
7117a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7118b3d3934dSBarry Smith   PetscFunctionReturn(0);
7119b3d3934dSBarry Smith }
7120b3d3934dSBarry Smith 
7121b3d3934dSBarry Smith #undef __FUNCT__
7122b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
7123b3d3934dSBarry Smith /*@C
7124b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7125b3d3934dSBarry Smith 
7126b3d3934dSBarry Smith    Collective on TS
7127b3d3934dSBarry Smith 
7128b3d3934dSBarry Smith    Input Parameters:
7129b3d3934dSBarry Smith +  ts - the TS context
7130b3d3934dSBarry Smith .  step - current time-step
7131b3d3934dSBarry Smith .  ptime - current time
71327db568b7SBarry Smith .  u  - current solution
71337db568b7SBarry Smith -  dctx - the envelope context
7134b3d3934dSBarry Smith 
7135b3d3934dSBarry Smith    Options Database:
7136b3d3934dSBarry Smith .  -ts_monitor_envelope
7137b3d3934dSBarry Smith 
7138b3d3934dSBarry Smith    Level: intermediate
7139b3d3934dSBarry Smith 
7140b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7141b3d3934dSBarry Smith 
7142b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7143b3d3934dSBarry Smith 
71447db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7145b3d3934dSBarry Smith @*/
71467db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7147b3d3934dSBarry Smith {
7148b3d3934dSBarry Smith   PetscErrorCode       ierr;
71497db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7150b3d3934dSBarry Smith 
7151b3d3934dSBarry Smith   PetscFunctionBegin;
7152b3d3934dSBarry Smith   if (!ctx->max) {
7153b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7154b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7155b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7156b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7157b3d3934dSBarry Smith   } else {
7158b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7159b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7160b3d3934dSBarry Smith   }
7161b3d3934dSBarry Smith   PetscFunctionReturn(0);
7162b3d3934dSBarry Smith }
7163b3d3934dSBarry Smith 
7164b3d3934dSBarry Smith 
7165b3d3934dSBarry Smith #undef __FUNCT__
7166b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
7167b3d3934dSBarry Smith /*@C
7168b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7169b3d3934dSBarry Smith 
7170b3d3934dSBarry Smith    Collective on TS
7171b3d3934dSBarry Smith 
7172b3d3934dSBarry Smith    Input Parameter:
7173b3d3934dSBarry Smith .  ts - the TS context
7174b3d3934dSBarry Smith 
7175b3d3934dSBarry Smith    Output Parameter:
7176b3d3934dSBarry Smith +  max - the maximum values
7177b3d3934dSBarry Smith -  min - the minimum values
7178b3d3934dSBarry Smith 
71797db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71807db568b7SBarry Smith 
7181b3d3934dSBarry Smith    Level: intermediate
7182b3d3934dSBarry Smith 
7183b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7184b3d3934dSBarry Smith 
7185b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7186b3d3934dSBarry Smith @*/
7187b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7188b3d3934dSBarry Smith {
7189b3d3934dSBarry Smith   PetscInt i;
7190b3d3934dSBarry Smith 
7191b3d3934dSBarry Smith   PetscFunctionBegin;
7192b3d3934dSBarry Smith   if (max) *max = NULL;
7193b3d3934dSBarry Smith   if (min) *min = NULL;
7194b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7195b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
71965537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7197b3d3934dSBarry Smith       if (max) *max = ctx->max;
7198b3d3934dSBarry Smith       if (min) *min = ctx->min;
7199b3d3934dSBarry Smith       break;
7200b3d3934dSBarry Smith     }
7201b3d3934dSBarry Smith   }
7202b3d3934dSBarry Smith   PetscFunctionReturn(0);
7203b3d3934dSBarry Smith }
7204b3d3934dSBarry Smith 
7205b3d3934dSBarry Smith #undef __FUNCT__
7206b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
7207b3d3934dSBarry Smith /*@C
7208b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7209b3d3934dSBarry Smith 
7210b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7211b3d3934dSBarry Smith 
7212b3d3934dSBarry Smith    Input Parameter:
7213b3d3934dSBarry Smith .  ctx - the monitor context
7214b3d3934dSBarry Smith 
7215b3d3934dSBarry Smith    Level: intermediate
7216b3d3934dSBarry Smith 
7217b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7218b3d3934dSBarry Smith 
72197db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7220b3d3934dSBarry Smith @*/
7221b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7222b3d3934dSBarry Smith {
7223b3d3934dSBarry Smith   PetscErrorCode ierr;
7224b3d3934dSBarry Smith 
7225b3d3934dSBarry Smith   PetscFunctionBegin;
7226b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7227b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7228b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7229b3d3934dSBarry Smith   PetscFunctionReturn(0);
7230b3d3934dSBarry Smith }
7231f2dee214SBarry Smith 
723224655328SShri #undef __FUNCT__
723324655328SShri #define __FUNCT__ "TSRollBack"
723424655328SShri /*@
723524655328SShri    TSRollBack - Rolls back one time step
723624655328SShri 
723724655328SShri    Collective on TS
723824655328SShri 
723924655328SShri    Input Parameter:
724024655328SShri .  ts - the TS context obtained from TSCreate()
724124655328SShri 
724224655328SShri    Level: advanced
724324655328SShri 
724424655328SShri .keywords: TS, timestep, rollback
724524655328SShri 
724624655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
724724655328SShri @*/
724824655328SShri PetscErrorCode  TSRollBack(TS ts)
724924655328SShri {
725024655328SShri   PetscErrorCode ierr;
725124655328SShri 
725224655328SShri   PetscFunctionBegin;
725324655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7254b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
725524655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
725624655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
725724655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
725824655328SShri   ts->ptime = ts->ptime_prev;
7259be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
7260be5899b3SLisandro Dalcin   ts->steps--; ts->total_steps--;
7261b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
726224655328SShri   PetscFunctionReturn(0);
726324655328SShri }
7264aeb4809dSShri Abhyankar 
7265ff22ae23SHong Zhang #undef __FUNCT__
7266ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
7267ff22ae23SHong Zhang /*@
7268ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7269ff22ae23SHong Zhang 
7270ff22ae23SHong Zhang    Input Parameter:
7271ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7272ff22ae23SHong Zhang 
7273ff22ae23SHong Zhang    Level: advanced
7274ff22ae23SHong Zhang 
7275ff22ae23SHong Zhang .keywords: TS, getstages
7276ff22ae23SHong Zhang 
7277ff22ae23SHong Zhang .seealso: TSCreate()
7278ff22ae23SHong Zhang @*/
7279ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7280ff22ae23SHong Zhang {
7281ff22ae23SHong Zhang   PetscErrorCode ierr;
7282ff22ae23SHong Zhang 
7283ff22ae23SHong Zhang   PetscFunctionBegin;
7284ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7285ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7286ff22ae23SHong Zhang 
7287ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7288ff22ae23SHong Zhang   else {
7289ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7290ff22ae23SHong Zhang   }
7291ff22ae23SHong Zhang   PetscFunctionReturn(0);
7292ff22ae23SHong Zhang }
7293ff22ae23SHong Zhang 
7294847ff0e1SMatthew G. Knepley #undef __FUNCT__
7295847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
7296847ff0e1SMatthew G. Knepley /*@C
7297847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7298847ff0e1SMatthew G. Knepley 
7299847ff0e1SMatthew G. Knepley   Collective on SNES
7300847ff0e1SMatthew G. Knepley 
7301847ff0e1SMatthew G. Knepley   Input Parameters:
7302847ff0e1SMatthew G. Knepley + ts - the TS context
7303847ff0e1SMatthew G. Knepley . t - current timestep
7304847ff0e1SMatthew G. Knepley . U - state vector
7305847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7306847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7307847ff0e1SMatthew G. Knepley - ctx - an optional user context
7308847ff0e1SMatthew G. Knepley 
7309847ff0e1SMatthew G. Knepley   Output Parameters:
7310847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7311847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7312847ff0e1SMatthew G. Knepley 
7313847ff0e1SMatthew G. Knepley   Level: intermediate
7314847ff0e1SMatthew G. Knepley 
7315847ff0e1SMatthew G. Knepley   Notes:
7316847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7317847ff0e1SMatthew G. Knepley 
7318847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7319847ff0e1SMatthew G. Knepley 
7320847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7321847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7322847ff0e1SMatthew G. Knepley 
7323847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7324847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7325847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7326847ff0e1SMatthew G. Knepley 
7327847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7328847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7329847ff0e1SMatthew G. Knepley @*/
7330847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7331847ff0e1SMatthew G. Knepley {
7332847ff0e1SMatthew G. Knepley   SNES           snes;
7333847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7334847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7335847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7336847ff0e1SMatthew G. Knepley 
7337847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7338c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7339847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7340847ff0e1SMatthew G. Knepley   if (!color) {
7341847ff0e1SMatthew G. Knepley     DM         dm;
7342847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7343847ff0e1SMatthew G. Knepley 
7344847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7345847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7346847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7347847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7348847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7349847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7350847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7351847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7352847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7353847ff0e1SMatthew G. Knepley     } else {
7354847ff0e1SMatthew G. Knepley       MatColoring mc;
7355847ff0e1SMatthew G. Knepley 
7356847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7357847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7358847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7359847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7360847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7361847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7362847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7363847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7364847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7365847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7366847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7367847ff0e1SMatthew G. Knepley     }
7368847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7369847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7370847ff0e1SMatthew G. Knepley   }
7371847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7372847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7373847ff0e1SMatthew G. Knepley   if (J != B) {
7374847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7375847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7376847ff0e1SMatthew G. Knepley   }
7377847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7378847ff0e1SMatthew G. Knepley }
737993b34091SDebojyoti Ghosh 
738093b34091SDebojyoti Ghosh #undef __FUNCT__
7381cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError"
7382cb9d8021SPierre Barbier de Reuille /*@
7383cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7384cb9d8021SPierre Barbier de Reuille 
7385cb9d8021SPierre Barbier de Reuille     Input Parameters:
7386cb9d8021SPierre Barbier de Reuille     ts - the TS context
7387cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7388cb9d8021SPierre Barbier de Reuille 
7389cb9d8021SPierre Barbier de Reuille     Level: intermediate
7390cb9d8021SPierre Barbier de Reuille 
7391cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7392cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7393cb9d8021SPierre Barbier de Reuille @*/
7394cb9d8021SPierre Barbier de Reuille 
7395d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7396cb9d8021SPierre Barbier de Reuille {
7397cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7398cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7399cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7400cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7401cb9d8021SPierre Barbier de Reuille }
7402cb9d8021SPierre Barbier de Reuille 
7403cb9d8021SPierre Barbier de Reuille #undef __FUNCT__
7404cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError"
7405cb9d8021SPierre Barbier de Reuille /*@
7406cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7407cb9d8021SPierre Barbier de Reuille 
7408cb9d8021SPierre Barbier de Reuille     Input Parameters:
7409cb9d8021SPierre Barbier de Reuille     ts - the TS context
7410cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7411d183316bSPierre Barbier de Reuille     Y - state vector to check.
7412cb9d8021SPierre Barbier de Reuille 
7413cb9d8021SPierre Barbier de Reuille     Output Parameter:
7414cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7415cb9d8021SPierre Barbier de Reuille 
7416cb9d8021SPierre Barbier de Reuille     Note:
7417cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7418cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
741996a0c994SBarry Smith 
742096a0c994SBarry Smith     Level: advanced
7421cb9d8021SPierre Barbier de Reuille @*/
7422d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7423cb9d8021SPierre Barbier de Reuille {
7424cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7425cb9d8021SPierre Barbier de Reuille 
7426cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7427cb9d8021SPierre Barbier de Reuille 
7428cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7429cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7430cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7431d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7432cb9d8021SPierre Barbier de Reuille   }
7433cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7434cb9d8021SPierre Barbier de Reuille }
74351ceb14c0SBarry Smith 
74361ceb14c0SBarry Smith #undef  __FUNCT__
7437e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
743893b34091SDebojyoti Ghosh /*@C
7439e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
744093b34091SDebojyoti Ghosh 
744193b34091SDebojyoti Ghosh   Collective on MPI_Comm
744293b34091SDebojyoti Ghosh 
744393b34091SDebojyoti Ghosh   Input Parameter:
744493b34091SDebojyoti Ghosh . tsin    - The input TS
744593b34091SDebojyoti Ghosh 
744693b34091SDebojyoti Ghosh   Output Parameter:
7447e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
744893b34091SDebojyoti Ghosh 
74495eca1a21SEmil Constantinescu   Notes:
74505eca1a21SEmil 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.
74515eca1a21SEmil Constantinescu 
7452baa10174SEmil 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);
74535eca1a21SEmil Constantinescu 
74545eca1a21SEmil Constantinescu   Level: developer
745593b34091SDebojyoti Ghosh 
7456e5168f73SEmil Constantinescu .keywords: TS, clone
7457e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
745893b34091SDebojyoti Ghosh @*/
7459baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
746093b34091SDebojyoti Ghosh {
746193b34091SDebojyoti Ghosh   TS             t;
746293b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7463dc846ba4SSatish Balay   SNES           snes_start;
7464dc846ba4SSatish Balay   DM             dm;
7465dc846ba4SSatish Balay   TSType         type;
746693b34091SDebojyoti Ghosh 
746793b34091SDebojyoti Ghosh   PetscFunctionBegin;
746893b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
746993b34091SDebojyoti Ghosh   *tsout = NULL;
747093b34091SDebojyoti Ghosh 
74717a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
747293b34091SDebojyoti Ghosh 
747393b34091SDebojyoti Ghosh   /* General TS description */
747493b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
747593b34091SDebojyoti Ghosh   t->setupcalled       = 0;
747693b34091SDebojyoti Ghosh   t->ksp_its           = 0;
747793b34091SDebojyoti Ghosh   t->snes_its          = 0;
747893b34091SDebojyoti Ghosh   t->nwork             = 0;
747993b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
748093b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
748193b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
748293b34091SDebojyoti Ghosh 
748334561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
748434561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7485d15a3a53SEmil Constantinescu 
748693b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
748793b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
748893b34091SDebojyoti Ghosh 
748993b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
749051699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
749193b34091SDebojyoti Ghosh 
749293b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
749393b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
749493b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
749593b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
749693b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
749793b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
749893b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
749993b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
750093b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
750193b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
750293b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
750393b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
750493b34091SDebojyoti Ghosh 
750593b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
750693b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
750793b34091SDebojyoti Ghosh 
750893b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
750993b34091SDebojyoti Ghosh 
751093b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
751193b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
751293b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
751393b34091SDebojyoti Ghosh 
751493b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
751593b34091SDebojyoti Ghosh 
75160d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75170d4fed19SBarry Smith     PetscInt i;
75180d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75190d4fed19SBarry Smith     for (i=0; i<10; i++) {
75200d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75210d4fed19SBarry Smith     }
75220d4fed19SBarry Smith   }
752393b34091SDebojyoti Ghosh   *tsout = t;
752493b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
752593b34091SDebojyoti Ghosh }
7526