xref: /petsc/src/ts/interface/ts.c (revision 874c02e69f48d1935b4c95776ca7e85e939965e4)
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:
125cd11d68dSLisandro Dalcin +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGL, TSSSP
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) {
929bd373395SBarry Smith       if (ijacobian) {
930214bc6a2SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
931bd373395SBarry Smith       } else {
932bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
933214bc6a2SJed Brown       }
934d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
935e1244c69SJed Brown     }
93694ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
937e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
938e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
93994ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
94094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
94194ab13aaSBarry Smith       if (A != B) {
94294ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
94394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
944316643e7SJed Brown       }
945e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
946e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
947e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
94894ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
94994ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
95094ab13aaSBarry Smith         if (A != B) {
95194ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
95294ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
953214bc6a2SJed Brown         }
954316643e7SJed Brown       }
95594ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
95694ab13aaSBarry Smith       if (A != B) {
95794ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
958316643e7SJed Brown       }
959316643e7SJed Brown     }
960316643e7SJed Brown   }
96194ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
962316643e7SJed Brown   PetscFunctionReturn(0);
963316643e7SJed Brown }
964316643e7SJed Brown 
965316643e7SJed Brown #undef __FUNCT__
9664a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
967d763cef2SBarry Smith /*@C
968d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
969b5abc632SBarry Smith     where U_t = G(t,u).
970d763cef2SBarry Smith 
9713f9fe445SBarry Smith     Logically Collective on TS
972d763cef2SBarry Smith 
973d763cef2SBarry Smith     Input Parameters:
974d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9750298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
976d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
977d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9780298fd71SBarry Smith           function evaluation routine (may be NULL)
979d763cef2SBarry Smith 
980d763cef2SBarry Smith     Calling sequence of func:
98187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
982d763cef2SBarry Smith 
983d763cef2SBarry Smith +   t - current timestep
984d763cef2SBarry Smith .   u - input vector
985d763cef2SBarry Smith .   F - function vector
986d763cef2SBarry Smith -   ctx - [optional] user-defined function context
987d763cef2SBarry Smith 
988d763cef2SBarry Smith     Level: beginner
989d763cef2SBarry Smith 
9902bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9912bbac0d3SBarry Smith 
992d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
993d763cef2SBarry Smith 
994ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
995d763cef2SBarry Smith @*/
996089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
997d763cef2SBarry Smith {
998089b2837SJed Brown   PetscErrorCode ierr;
999089b2837SJed Brown   SNES           snes;
10000298fd71SBarry Smith   Vec            ralloc = NULL;
100124989b8cSPeter Brune   DM             dm;
1002d763cef2SBarry Smith 
1003089b2837SJed Brown   PetscFunctionBegin;
10040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1005ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
100624989b8cSPeter Brune 
100724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
100824989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1009089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1010e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1011e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1012e856ceecSJed Brown     r = ralloc;
1013e856ceecSJed Brown   }
1014089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1015e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1016d763cef2SBarry Smith   PetscFunctionReturn(0);
1017d763cef2SBarry Smith }
1018d763cef2SBarry Smith 
10194a2ae208SSatish Balay #undef __FUNCT__
1020ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
1021ef20d060SBarry Smith /*@C
1022abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1023ef20d060SBarry Smith 
1024ef20d060SBarry Smith     Logically Collective on TS
1025ef20d060SBarry Smith 
1026ef20d060SBarry Smith     Input Parameters:
1027ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1028ef20d060SBarry Smith .   f - routine for evaluating the solution
1029ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10300298fd71SBarry Smith           function evaluation routine (may be NULL)
1031ef20d060SBarry Smith 
1032ef20d060SBarry Smith     Calling sequence of func:
1033ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1034ef20d060SBarry Smith 
1035ef20d060SBarry Smith +   t - current timestep
1036ef20d060SBarry Smith .   u - output vector
1037ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1038ef20d060SBarry Smith 
1039abd5a294SJed Brown     Notes:
1040abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1041abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1042abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1043abd5a294SJed Brown 
10444f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1045abd5a294SJed Brown 
1046ef20d060SBarry Smith     Level: beginner
1047ef20d060SBarry Smith 
1048ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1049ef20d060SBarry Smith 
10509b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
1051ef20d060SBarry Smith @*/
1052ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1053ef20d060SBarry Smith {
1054ef20d060SBarry Smith   PetscErrorCode ierr;
1055ef20d060SBarry Smith   DM             dm;
1056ef20d060SBarry Smith 
1057ef20d060SBarry Smith   PetscFunctionBegin;
1058ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1059ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1060ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1061ef20d060SBarry Smith   PetscFunctionReturn(0);
1062ef20d060SBarry Smith }
1063ef20d060SBarry Smith 
1064ef20d060SBarry Smith #undef __FUNCT__
10659b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
10669b7cd975SBarry Smith /*@C
10679b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10689b7cd975SBarry Smith 
10699b7cd975SBarry Smith     Logically Collective on TS
10709b7cd975SBarry Smith 
10719b7cd975SBarry Smith     Input Parameters:
10729b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
10739b7cd975SBarry Smith .   f - routine for evaluating the forcing function
10749b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10750298fd71SBarry Smith           function evaluation routine (may be NULL)
10769b7cd975SBarry Smith 
10779b7cd975SBarry Smith     Calling sequence of func:
10789b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
10799b7cd975SBarry Smith 
10809b7cd975SBarry Smith +   t - current timestep
10819b7cd975SBarry Smith .   u - output vector
10829b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10839b7cd975SBarry Smith 
10849b7cd975SBarry Smith     Notes:
10859b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
10869b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
10879b7cd975SBarry Smith 
10889b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10899b7cd975SBarry Smith 
10909b7cd975SBarry Smith     Level: beginner
10919b7cd975SBarry Smith 
10929b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10939b7cd975SBarry Smith 
10949b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10959b7cd975SBarry Smith @*/
1096d56366bfSLisandro Dalcin PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction f,void *ctx)
10979b7cd975SBarry Smith {
10989b7cd975SBarry Smith   PetscErrorCode ierr;
10999b7cd975SBarry Smith   DM             dm;
11009b7cd975SBarry Smith 
11019b7cd975SBarry Smith   PetscFunctionBegin;
11029b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11039b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
11049b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
11059b7cd975SBarry Smith   PetscFunctionReturn(0);
11069b7cd975SBarry Smith }
11079b7cd975SBarry Smith 
11089b7cd975SBarry Smith #undef __FUNCT__
11094a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1110d763cef2SBarry Smith /*@C
1111f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1112b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1113d763cef2SBarry Smith 
11143f9fe445SBarry Smith    Logically Collective on TS
1115d763cef2SBarry Smith 
1116d763cef2SBarry Smith    Input Parameters:
1117d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1118e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1119e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1120d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1121d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11220298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1123d763cef2SBarry Smith 
1124f7ab8db6SBarry Smith    Calling sequence of f:
1125ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1126d763cef2SBarry Smith 
1127d763cef2SBarry Smith +  t - current timestep
1128d763cef2SBarry Smith .  u - input vector
1129e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1130e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1131d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1132d763cef2SBarry Smith 
11336cd88445SBarry Smith    Notes:
11346cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11356cd88445SBarry Smith 
11366cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1137ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1138d763cef2SBarry Smith 
1139d763cef2SBarry Smith    Level: beginner
1140d763cef2SBarry Smith 
1141d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1142d763cef2SBarry Smith 
1143ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1144d763cef2SBarry Smith 
1145d763cef2SBarry Smith @*/
1146e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1147d763cef2SBarry Smith {
1148277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1149089b2837SJed Brown   SNES           snes;
115024989b8cSPeter Brune   DM             dm;
115124989b8cSPeter Brune   TSIJacobian    ijacobian;
1152277b19d0SLisandro Dalcin 
1153d763cef2SBarry Smith   PetscFunctionBegin;
11540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1155e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1156e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1157e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1158e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1159d763cef2SBarry Smith 
116024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
116124989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1162e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1163e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1164e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1165e1244c69SJed Brown   }
11660298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1167089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11685f659677SPeter Brune   if (!ijacobian) {
1169e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11700e4ef248SJed Brown   }
1171e5d3d808SBarry Smith   if (Amat) {
1172e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11730e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1174e5d3d808SBarry Smith     ts->Arhs = Amat;
11750e4ef248SJed Brown   }
1176e5d3d808SBarry Smith   if (Pmat) {
1177e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11780e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1179e5d3d808SBarry Smith     ts->Brhs = Pmat;
11800e4ef248SJed Brown   }
1181d763cef2SBarry Smith   PetscFunctionReturn(0);
1182d763cef2SBarry Smith }
1183d763cef2SBarry Smith 
1184316643e7SJed Brown 
1185316643e7SJed Brown #undef __FUNCT__
1186316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1187316643e7SJed Brown /*@C
1188b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1189316643e7SJed Brown 
11903f9fe445SBarry Smith    Logically Collective on TS
1191316643e7SJed Brown 
1192316643e7SJed Brown    Input Parameters:
1193316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
11940298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1195316643e7SJed Brown .  f   - the function evaluation routine
11960298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1197316643e7SJed Brown 
1198316643e7SJed Brown    Calling sequence of f:
1199316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1200316643e7SJed Brown 
1201316643e7SJed Brown +  t   - time at step/stage being solved
1202316643e7SJed Brown .  u   - state vector
1203316643e7SJed Brown .  u_t - time derivative of state vector
1204316643e7SJed Brown .  F   - function vector
1205316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1206316643e7SJed Brown 
1207316643e7SJed Brown    Important:
12082bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1209316643e7SJed Brown 
1210316643e7SJed Brown    Level: beginner
1211316643e7SJed Brown 
1212316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1213316643e7SJed Brown 
1214d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1215316643e7SJed Brown @*/
121651699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1217316643e7SJed Brown {
1218089b2837SJed Brown   PetscErrorCode ierr;
1219089b2837SJed Brown   SNES           snes;
122051699248SLisandro Dalcin   Vec            ralloc = NULL;
122124989b8cSPeter Brune   DM             dm;
1222316643e7SJed Brown 
1223316643e7SJed Brown   PetscFunctionBegin;
12240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
122551699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
122624989b8cSPeter Brune 
122724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
122924989b8cSPeter Brune 
1230089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
123151699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
123251699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
123351699248SLisandro Dalcin     r  = ralloc;
1234e856ceecSJed Brown   }
123551699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
123651699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1237089b2837SJed Brown   PetscFunctionReturn(0);
1238089b2837SJed Brown }
1239089b2837SJed Brown 
1240089b2837SJed Brown #undef __FUNCT__
1241089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1242089b2837SJed Brown /*@C
1243089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1244089b2837SJed Brown 
1245089b2837SJed Brown    Not Collective
1246089b2837SJed Brown 
1247089b2837SJed Brown    Input Parameter:
1248089b2837SJed Brown .  ts - the TS context
1249089b2837SJed Brown 
1250089b2837SJed Brown    Output Parameter:
12510298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12520298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12530298fd71SBarry Smith -  ctx - the function context (or NULL)
1254089b2837SJed Brown 
1255089b2837SJed Brown    Level: advanced
1256089b2837SJed Brown 
1257089b2837SJed Brown .keywords: TS, nonlinear, get, function
1258089b2837SJed Brown 
1259089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1260089b2837SJed Brown @*/
1261089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1262089b2837SJed Brown {
1263089b2837SJed Brown   PetscErrorCode ierr;
1264089b2837SJed Brown   SNES           snes;
126524989b8cSPeter Brune   DM             dm;
1266089b2837SJed Brown 
1267089b2837SJed Brown   PetscFunctionBegin;
1268089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1269089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12700298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
127124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
127224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1273089b2837SJed Brown   PetscFunctionReturn(0);
1274089b2837SJed Brown }
1275089b2837SJed Brown 
1276089b2837SJed Brown #undef __FUNCT__
1277089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1278089b2837SJed Brown /*@C
1279089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1280089b2837SJed Brown 
1281089b2837SJed Brown    Not Collective
1282089b2837SJed Brown 
1283089b2837SJed Brown    Input Parameter:
1284089b2837SJed Brown .  ts - the TS context
1285089b2837SJed Brown 
1286089b2837SJed Brown    Output Parameter:
12870298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12880298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12890298fd71SBarry Smith -  ctx - the function context (or NULL)
1290089b2837SJed Brown 
1291089b2837SJed Brown    Level: advanced
1292089b2837SJed Brown 
1293089b2837SJed Brown .keywords: TS, nonlinear, get, function
1294089b2837SJed Brown 
12952bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1296089b2837SJed Brown @*/
1297089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1298089b2837SJed Brown {
1299089b2837SJed Brown   PetscErrorCode ierr;
1300089b2837SJed Brown   SNES           snes;
130124989b8cSPeter Brune   DM             dm;
1302089b2837SJed Brown 
1303089b2837SJed Brown   PetscFunctionBegin;
1304089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1305089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13060298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
130724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
130824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1309316643e7SJed Brown   PetscFunctionReturn(0);
1310316643e7SJed Brown }
1311316643e7SJed Brown 
1312316643e7SJed Brown #undef __FUNCT__
1313316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1314316643e7SJed Brown /*@C
1315a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1316ae8867d6SBarry Smith         provided with TSSetIFunction().
1317316643e7SJed Brown 
13183f9fe445SBarry Smith    Logically Collective on TS
1319316643e7SJed Brown 
1320316643e7SJed Brown    Input Parameters:
1321316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1322e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1323e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1324316643e7SJed Brown .  f   - the Jacobian evaluation routine
13250298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1326316643e7SJed Brown 
1327316643e7SJed Brown    Calling sequence of f:
1328ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1329316643e7SJed Brown 
1330316643e7SJed Brown +  t    - time at step/stage being solved
13311b4a444bSJed Brown .  U    - state vector
13321b4a444bSJed Brown .  U_t  - time derivative of state vector
1333316643e7SJed Brown .  a    - shift
1334e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1335e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1336316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1337316643e7SJed Brown 
1338316643e7SJed Brown    Notes:
1339e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1340316643e7SJed Brown 
1341895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1342895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1343895c21f2SBarry Smith 
1344a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1345b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1346a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1347a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1348a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1349a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1350a4f0a591SBarry Smith 
13516cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13526cd88445SBarry Smith 
13536cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1354ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1355ca5f011dSBarry Smith 
1356316643e7SJed Brown    Level: beginner
1357316643e7SJed Brown 
1358316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1359316643e7SJed Brown 
1360ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1361316643e7SJed Brown 
1362316643e7SJed Brown @*/
1363e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1364316643e7SJed Brown {
1365316643e7SJed Brown   PetscErrorCode ierr;
1366089b2837SJed Brown   SNES           snes;
136724989b8cSPeter Brune   DM             dm;
1368316643e7SJed Brown 
1369316643e7SJed Brown   PetscFunctionBegin;
13700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1371e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1372e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1373e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1374e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
137524989b8cSPeter Brune 
137624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
137824989b8cSPeter Brune 
1379089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1380e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1381316643e7SJed Brown   PetscFunctionReturn(0);
1382316643e7SJed Brown }
1383316643e7SJed Brown 
13844a2ae208SSatish Balay #undef __FUNCT__
1385e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1386e1244c69SJed Brown /*@
1387e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1388e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1389e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1390e1244c69SJed Brown    not been changed by the TS.
1391e1244c69SJed Brown 
1392e1244c69SJed Brown    Logically Collective
1393e1244c69SJed Brown 
1394e1244c69SJed Brown    Input Arguments:
1395e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1396e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1397e1244c69SJed Brown 
1398e1244c69SJed Brown    Level: intermediate
1399e1244c69SJed Brown 
1400e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1401e1244c69SJed Brown @*/
1402e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1403e1244c69SJed Brown {
1404e1244c69SJed Brown   PetscFunctionBegin;
1405e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1406e1244c69SJed Brown   PetscFunctionReturn(0);
1407e1244c69SJed Brown }
1408e1244c69SJed Brown 
1409e1244c69SJed Brown #undef __FUNCT__
141055849f57SBarry Smith #define __FUNCT__ "TSLoad"
141155849f57SBarry Smith /*@C
141255849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
141355849f57SBarry Smith 
141455849f57SBarry Smith   Collective on PetscViewer
141555849f57SBarry Smith 
141655849f57SBarry Smith   Input Parameters:
141755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
141855849f57SBarry Smith            some related function before a call to TSLoad().
141955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
142055849f57SBarry Smith 
142155849f57SBarry Smith    Level: intermediate
142255849f57SBarry Smith 
142355849f57SBarry Smith   Notes:
142455849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
142555849f57SBarry Smith 
142655849f57SBarry Smith   Notes for advanced users:
142755849f57SBarry Smith   Most users should not need to know the details of the binary storage
142855849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
142955849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
143055849f57SBarry Smith   format is
143155849f57SBarry Smith .vb
143255849f57SBarry Smith      has not yet been determined
143355849f57SBarry Smith .ve
143455849f57SBarry Smith 
143555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
143655849f57SBarry Smith @*/
1437f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
143855849f57SBarry Smith {
143955849f57SBarry Smith   PetscErrorCode ierr;
144055849f57SBarry Smith   PetscBool      isbinary;
144155849f57SBarry Smith   PetscInt       classid;
144255849f57SBarry Smith   char           type[256];
14432d53ad75SBarry Smith   DMTS           sdm;
1444ad6bc421SBarry Smith   DM             dm;
144555849f57SBarry Smith 
144655849f57SBarry Smith   PetscFunctionBegin;
1447f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
144855849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
144955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
145055849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
145155849f57SBarry Smith 
1452060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1453ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1454060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1455f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1456f2c2a1b9SBarry Smith   if (ts->ops->load) {
1457f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1458f2c2a1b9SBarry Smith   }
1459ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1460ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1461ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1462f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1463f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
14642d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14652d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
146655849f57SBarry Smith   PetscFunctionReturn(0);
146755849f57SBarry Smith }
146855849f57SBarry Smith 
14699804daf3SBarry Smith #include <petscdraw.h>
1470e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1471e04113cfSBarry Smith #include <petscviewersaws.h>
1472f05ece33SBarry Smith #endif
147355849f57SBarry Smith #undef __FUNCT__
14744a2ae208SSatish Balay #define __FUNCT__ "TSView"
14757e2c5f70SBarry Smith /*@C
1476d763cef2SBarry Smith     TSView - Prints the TS data structure.
1477d763cef2SBarry Smith 
14784c49b128SBarry Smith     Collective on TS
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith     Input Parameters:
1481d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1482d763cef2SBarry Smith -   viewer - visualization context
1483d763cef2SBarry Smith 
1484d763cef2SBarry Smith     Options Database Key:
1485d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1486d763cef2SBarry Smith 
1487d763cef2SBarry Smith     Notes:
1488d763cef2SBarry Smith     The available visualization contexts include
1489b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1490b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1491d763cef2SBarry Smith          output where only the first processor opens
1492d763cef2SBarry Smith          the file.  All other processors send their
1493d763cef2SBarry Smith          data to the first processor to print.
1494d763cef2SBarry Smith 
1495d763cef2SBarry Smith     The user can open an alternative visualization context with
1496b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1497d763cef2SBarry Smith 
1498d763cef2SBarry Smith     Level: beginner
1499d763cef2SBarry Smith 
1500d763cef2SBarry Smith .keywords: TS, timestep, view
1501d763cef2SBarry Smith 
1502b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1503d763cef2SBarry Smith @*/
15047087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1505d763cef2SBarry Smith {
1506dfbe8321SBarry Smith   PetscErrorCode ierr;
150719fd82e9SBarry Smith   TSType         type;
15082b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
15092d53ad75SBarry Smith   DMTS           sdm;
1510e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1511536b137fSBarry Smith   PetscBool      issaws;
1512f05ece33SBarry Smith #endif
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith   PetscFunctionBegin;
15150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15163050cee2SBarry Smith   if (!viewer) {
1517ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
15183050cee2SBarry Smith   }
15190700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1520c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1521fd16b177SBarry Smith 
1522251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1523251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
152455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
15252b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1526e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1527536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1528f05ece33SBarry Smith #endif
152932077d6dSBarry Smith   if (iascii) {
1530dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
153177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
15327c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1533d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
15345ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1535c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1536d763cef2SBarry Smith     }
15375ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1538193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
15392d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
15402d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1541d52bd9f3SBarry Smith     if (ts->ops->view) {
1542d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1543d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1544d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1545d52bd9f3SBarry Smith     }
15460f5bd95cSBarry Smith   } else if (isstring) {
1547a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1548b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
154955849f57SBarry Smith   } else if (isbinary) {
155055849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
155155849f57SBarry Smith     MPI_Comm    comm;
155255849f57SBarry Smith     PetscMPIInt rank;
155355849f57SBarry Smith     char        type[256];
155455849f57SBarry Smith 
155555849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
155655849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
155755849f57SBarry Smith     if (!rank) {
155855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
155955849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
156055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
156155849f57SBarry Smith     }
156255849f57SBarry Smith     if (ts->ops->view) {
156355849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
156455849f57SBarry Smith     }
1565f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1566f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
15672d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
15682d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
15692b0a91c0SBarry Smith   } else if (isdraw) {
15702b0a91c0SBarry Smith     PetscDraw draw;
15712b0a91c0SBarry Smith     char      str[36];
157289fd9fafSBarry Smith     PetscReal x,y,bottom,h;
15732b0a91c0SBarry Smith 
15742b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
15752b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
15762b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
15772b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
157851fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
157989fd9fafSBarry Smith     bottom = y - h;
15802b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
15812b0a91c0SBarry Smith     if (ts->ops->view) {
15822b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
15832b0a91c0SBarry Smith     }
15842b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1585e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1586536b137fSBarry Smith   } else if (issaws) {
1587d45a07a7SBarry Smith     PetscMPIInt rank;
15882657e9d9SBarry Smith     const char  *name;
15892657e9d9SBarry Smith 
15902657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1591d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1592d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1593d45a07a7SBarry Smith       char       dir[1024];
1594d45a07a7SBarry Smith 
1595e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1596a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
15972657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
15982657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
15992657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1600d763cef2SBarry Smith     }
16010acecf5bSBarry Smith     if (ts->ops->view) {
16020acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
16030acecf5bSBarry Smith     }
1604f05ece33SBarry Smith #endif
1605f05ece33SBarry Smith   }
1606f05ece33SBarry Smith 
1607b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1608251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1609b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1610d763cef2SBarry Smith   PetscFunctionReturn(0);
1611d763cef2SBarry Smith }
1612d763cef2SBarry Smith 
1613d763cef2SBarry Smith 
16144a2ae208SSatish Balay #undef __FUNCT__
16154a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1616b07ff414SBarry Smith /*@
1617d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1618d763cef2SBarry Smith    the timesteppers.
1619d763cef2SBarry Smith 
16203f9fe445SBarry Smith    Logically Collective on TS
1621d763cef2SBarry Smith 
1622d763cef2SBarry Smith    Input Parameters:
1623d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1624d763cef2SBarry Smith -  usrP - optional user context
1625d763cef2SBarry Smith 
1626daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1627daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1628daf670e6SBarry Smith 
1629d763cef2SBarry Smith    Level: intermediate
1630d763cef2SBarry Smith 
1631d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1632d763cef2SBarry Smith 
1633d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1634d763cef2SBarry Smith @*/
16357087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1636d763cef2SBarry Smith {
1637d763cef2SBarry Smith   PetscFunctionBegin;
16380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1639d763cef2SBarry Smith   ts->user = usrP;
1640d763cef2SBarry Smith   PetscFunctionReturn(0);
1641d763cef2SBarry Smith }
1642d763cef2SBarry Smith 
16434a2ae208SSatish Balay #undef __FUNCT__
16444a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1645b07ff414SBarry Smith /*@
1646d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1647d763cef2SBarry Smith     timestepper.
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith     Not Collective
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith     Input Parameter:
1652d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith     Output Parameter:
1655d763cef2SBarry Smith .   usrP - user context
1656d763cef2SBarry Smith 
1657daf670e6SBarry Smith    Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this
1658daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
1659daf670e6SBarry Smith 
1660d763cef2SBarry Smith     Level: intermediate
1661d763cef2SBarry Smith 
1662d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1663d763cef2SBarry Smith 
1664d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1665d763cef2SBarry Smith @*/
1666e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1667d763cef2SBarry Smith {
1668d763cef2SBarry Smith   PetscFunctionBegin;
16690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1670e71120c6SJed Brown   *(void**)usrP = ts->user;
1671d763cef2SBarry Smith   PetscFunctionReturn(0);
1672d763cef2SBarry Smith }
1673d763cef2SBarry Smith 
16744a2ae208SSatish Balay #undef __FUNCT__
16754a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1676d763cef2SBarry Smith /*@
1677b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Not Collective
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Input Parameter:
1682d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1683d763cef2SBarry Smith 
1684d763cef2SBarry Smith    Output Parameter:
1685b8123daeSJed Brown .  iter - number of steps completed so far
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Level: intermediate
1688d763cef2SBarry Smith 
1689d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
16909be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1691d763cef2SBarry Smith @*/
16927087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1693d763cef2SBarry Smith {
1694d763cef2SBarry Smith   PetscFunctionBegin;
16950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16964482741eSBarry Smith   PetscValidIntPointer(iter,2);
1697d763cef2SBarry Smith   *iter = ts->steps;
1698d763cef2SBarry Smith   PetscFunctionReturn(0);
1699d763cef2SBarry Smith }
1700d763cef2SBarry Smith 
17014a2ae208SSatish Balay #undef __FUNCT__
17024a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1703d763cef2SBarry Smith /*@
1704d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1705d763cef2SBarry Smith    as well as the initial time.
1706d763cef2SBarry Smith 
17073f9fe445SBarry Smith    Logically Collective on TS
1708d763cef2SBarry Smith 
1709d763cef2SBarry Smith    Input Parameters:
1710d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1711d763cef2SBarry Smith .  initial_time - the initial time
1712d763cef2SBarry Smith -  time_step - the size of the timestep
1713d763cef2SBarry Smith 
1714d763cef2SBarry Smith    Level: intermediate
1715d763cef2SBarry Smith 
1716d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1717d763cef2SBarry Smith 
1718d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1719d763cef2SBarry Smith @*/
17207087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1721d763cef2SBarry Smith {
1722e144a568SJed Brown   PetscErrorCode ierr;
1723e144a568SJed Brown 
1724d763cef2SBarry Smith   PetscFunctionBegin;
17250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1726e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1727d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1728d763cef2SBarry Smith   PetscFunctionReturn(0);
1729d763cef2SBarry Smith }
1730d763cef2SBarry Smith 
17314a2ae208SSatish Balay #undef __FUNCT__
17324a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1733d763cef2SBarry Smith /*@
1734d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1735d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1736d763cef2SBarry Smith 
17373f9fe445SBarry Smith    Logically Collective on TS
1738d763cef2SBarry Smith 
1739d763cef2SBarry Smith    Input Parameters:
1740d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1741d763cef2SBarry Smith -  time_step - the size of the timestep
1742d763cef2SBarry Smith 
1743d763cef2SBarry Smith    Level: intermediate
1744d763cef2SBarry Smith 
1745d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1746d763cef2SBarry Smith 
1747d763cef2SBarry Smith .keywords: TS, set, timestep
1748d763cef2SBarry Smith @*/
17497087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1750d763cef2SBarry Smith {
1751d763cef2SBarry Smith   PetscFunctionBegin;
17520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1753c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1754d763cef2SBarry Smith   ts->time_step      = time_step;
175531748224SBarry Smith   ts->time_step_orig = time_step;
1756d763cef2SBarry Smith   PetscFunctionReturn(0);
1757d763cef2SBarry Smith }
1758d763cef2SBarry Smith 
17594a2ae208SSatish Balay #undef __FUNCT__
1760a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1761a43b19c4SJed Brown /*@
176249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
176349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
176449354f04SShri Abhyankar      or just return at the final time TS computed.
1765a43b19c4SJed Brown 
1766a43b19c4SJed Brown   Logically Collective on TS
1767a43b19c4SJed Brown 
1768a43b19c4SJed Brown    Input Parameter:
1769a43b19c4SJed Brown +   ts - the time-step context
177049354f04SShri Abhyankar -   eftopt - exact final time option
1771a43b19c4SJed Brown 
1772feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
1773feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
1774feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
1775feed9e9dSBarry Smith 
1776feed9e9dSBarry Smith    Options Database:
1777feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
1778feed9e9dSBarry Smith 
1779ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
1780ee346746SBarry Smith     then the final time you selected.
1781ee346746SBarry Smith 
1782a43b19c4SJed Brown    Level: beginner
1783a43b19c4SJed Brown 
1784a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1785a43b19c4SJed Brown @*/
178649354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1787a43b19c4SJed Brown {
1788a43b19c4SJed Brown   PetscFunctionBegin;
1789a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
179049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
179149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1792a43b19c4SJed Brown   PetscFunctionReturn(0);
1793a43b19c4SJed Brown }
1794a43b19c4SJed Brown 
1795a43b19c4SJed Brown #undef __FUNCT__
17964a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1797d763cef2SBarry Smith /*@
1798d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1799d763cef2SBarry Smith 
1800d763cef2SBarry Smith    Not Collective
1801d763cef2SBarry Smith 
1802d763cef2SBarry Smith    Input Parameter:
1803d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1804d763cef2SBarry Smith 
1805d763cef2SBarry Smith    Output Parameter:
1806d763cef2SBarry Smith .  dt - the current timestep size
1807d763cef2SBarry Smith 
1808d763cef2SBarry Smith    Level: intermediate
1809d763cef2SBarry Smith 
1810d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1811d763cef2SBarry Smith 
1812d763cef2SBarry Smith .keywords: TS, get, timestep
1813d763cef2SBarry Smith @*/
18147087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1815d763cef2SBarry Smith {
1816d763cef2SBarry Smith   PetscFunctionBegin;
18170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1818f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1819d763cef2SBarry Smith   *dt = ts->time_step;
1820d763cef2SBarry Smith   PetscFunctionReturn(0);
1821d763cef2SBarry Smith }
1822d763cef2SBarry Smith 
18234a2ae208SSatish Balay #undef __FUNCT__
18244a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1825d8e5e3e6SSatish Balay /*@
1826d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1827d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1828d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1829d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1830d763cef2SBarry Smith 
1831d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1832d763cef2SBarry Smith 
1833d763cef2SBarry Smith    Input Parameter:
1834d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1835d763cef2SBarry Smith 
1836d763cef2SBarry Smith    Output Parameter:
1837d763cef2SBarry Smith .  v - the vector containing the solution
1838d763cef2SBarry Smith 
183963e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
184063e21af5SBarry Smith    final time. It returns the solution at the next timestep.
184163e21af5SBarry Smith 
1842d763cef2SBarry Smith    Level: intermediate
1843d763cef2SBarry Smith 
184463e21af5SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime()
1845d763cef2SBarry Smith 
1846d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1847d763cef2SBarry Smith @*/
18487087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1849d763cef2SBarry Smith {
1850d763cef2SBarry Smith   PetscFunctionBegin;
18510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18524482741eSBarry Smith   PetscValidPointer(v,2);
18538737fe31SLisandro Dalcin   *v = ts->vec_sol;
1854d763cef2SBarry Smith   PetscFunctionReturn(0);
1855d763cef2SBarry Smith }
1856d763cef2SBarry Smith 
1857c235aa8dSHong Zhang #undef __FUNCT__
1858dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients"
1859c235aa8dSHong Zhang /*@
1860dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
1861c235aa8dSHong Zhang 
1862c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1863c235aa8dSHong Zhang 
1864c235aa8dSHong Zhang    Input Parameter:
1865c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1866c235aa8dSHong Zhang 
1867c235aa8dSHong Zhang    Output Parameter:
1868abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
1869abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
1870c235aa8dSHong Zhang 
1871c235aa8dSHong Zhang    Level: intermediate
1872c235aa8dSHong Zhang 
1873c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1874c235aa8dSHong Zhang 
1875c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1876c235aa8dSHong Zhang @*/
1877dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
1878c235aa8dSHong Zhang {
1879c235aa8dSHong Zhang   PetscFunctionBegin;
1880c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1881abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
1882abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
1883abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
1884c235aa8dSHong Zhang   PetscFunctionReturn(0);
1885c235aa8dSHong Zhang }
1886c235aa8dSHong Zhang 
1887bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
18884a2ae208SSatish Balay #undef __FUNCT__
1889bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1890d8e5e3e6SSatish Balay /*@
1891bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1892d763cef2SBarry Smith 
1893bdad233fSMatthew Knepley   Not collective
1894d763cef2SBarry Smith 
1895bdad233fSMatthew Knepley   Input Parameters:
1896bdad233fSMatthew Knepley + ts   - The TS
1897bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1898d763cef2SBarry Smith .vb
18990910c330SBarry Smith          U_t - A U = 0      (linear)
19000910c330SBarry Smith          U_t - A(t) U = 0   (linear)
19010910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1902d763cef2SBarry Smith .ve
1903d763cef2SBarry Smith 
1904d763cef2SBarry Smith    Level: beginner
1905d763cef2SBarry Smith 
1906bdad233fSMatthew Knepley .keywords: TS, problem type
1907bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1908d763cef2SBarry Smith @*/
19097087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1910a7cc72afSBarry Smith {
19119e2a6581SJed Brown   PetscErrorCode ierr;
19129e2a6581SJed Brown 
1913d763cef2SBarry Smith   PetscFunctionBegin;
19140700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1915bdad233fSMatthew Knepley   ts->problem_type = type;
19169e2a6581SJed Brown   if (type == TS_LINEAR) {
19179e2a6581SJed Brown     SNES snes;
19189e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
19199e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
19209e2a6581SJed Brown   }
1921d763cef2SBarry Smith   PetscFunctionReturn(0);
1922d763cef2SBarry Smith }
1923d763cef2SBarry Smith 
1924bdad233fSMatthew Knepley #undef __FUNCT__
1925bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1926bdad233fSMatthew Knepley /*@C
1927bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1928bdad233fSMatthew Knepley 
1929bdad233fSMatthew Knepley   Not collective
1930bdad233fSMatthew Knepley 
1931bdad233fSMatthew Knepley   Input Parameter:
1932bdad233fSMatthew Knepley . ts   - The TS
1933bdad233fSMatthew Knepley 
1934bdad233fSMatthew Knepley   Output Parameter:
1935bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1936bdad233fSMatthew Knepley .vb
1937089b2837SJed Brown          M U_t = A U
1938089b2837SJed Brown          M(t) U_t = A(t) U
1939b5abc632SBarry Smith          F(t,U,U_t)
1940bdad233fSMatthew Knepley .ve
1941bdad233fSMatthew Knepley 
1942bdad233fSMatthew Knepley    Level: beginner
1943bdad233fSMatthew Knepley 
1944bdad233fSMatthew Knepley .keywords: TS, problem type
1945bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1946bdad233fSMatthew Knepley @*/
19477087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1948a7cc72afSBarry Smith {
1949bdad233fSMatthew Knepley   PetscFunctionBegin;
19500700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
19514482741eSBarry Smith   PetscValidIntPointer(type,2);
1952bdad233fSMatthew Knepley   *type = ts->problem_type;
1953bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1954bdad233fSMatthew Knepley }
1955d763cef2SBarry Smith 
19564a2ae208SSatish Balay #undef __FUNCT__
19574a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1958d763cef2SBarry Smith /*@
1959d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1960d763cef2SBarry Smith    of a timestepper.
1961d763cef2SBarry Smith 
1962d763cef2SBarry Smith    Collective on TS
1963d763cef2SBarry Smith 
1964d763cef2SBarry Smith    Input Parameter:
1965d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1966d763cef2SBarry Smith 
1967d763cef2SBarry Smith    Notes:
1968d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1969d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1970d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1971d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1972d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1973d763cef2SBarry Smith 
1974d763cef2SBarry Smith    Level: advanced
1975d763cef2SBarry Smith 
1976d763cef2SBarry Smith .keywords: TS, timestep, setup
1977d763cef2SBarry Smith 
1978d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1979d763cef2SBarry Smith @*/
19807087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1981d763cef2SBarry Smith {
1982dfbe8321SBarry Smith   PetscErrorCode ierr;
19836c6b9e74SPeter Brune   DM             dm;
19846c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1985d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
1986cd11d68dSLisandro Dalcin   TSIFunction    ifun;
19876c6b9e74SPeter Brune   TSIJacobian    ijac;
19886c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1989d763cef2SBarry Smith 
1990d763cef2SBarry Smith   PetscFunctionBegin;
19910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1992277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1993277b19d0SLisandro Dalcin 
19942c18e0fdSBarry Smith   ts->total_steps = 0;
19957adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
1996cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
1997cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
1998d763cef2SBarry Smith   }
1999277b19d0SLisandro Dalcin 
2000277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2001277b19d0SLisandro Dalcin 
2002e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2003e1244c69SJed Brown     Mat Amat,Pmat;
2004e1244c69SJed Brown     SNES snes;
2005e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2006e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2007e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2008e1244c69SJed Brown      * have displaced the RHS matrix */
2009e1244c69SJed Brown     if (Amat == ts->Arhs) {
2010e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2011e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2012e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2013e1244c69SJed Brown     }
2014e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2015e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2016e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2017e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2018e1244c69SJed Brown     }
2019e1244c69SJed Brown   }
2020277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2021000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2022277b19d0SLisandro Dalcin   }
2023277b19d0SLisandro Dalcin 
2024a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
20256c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
20266c6b9e74SPeter Brune    */
20276c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20280298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
20296c6b9e74SPeter Brune   if (!func) {
20306c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
20316c6b9e74SPeter Brune   }
2032a6772fa2SLisandro 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.
20336c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
20346c6b9e74SPeter Brune    */
20350298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
20360298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
20370298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
20386c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
20396c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
20406c6b9e74SPeter Brune   }
2041277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2042277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2043277b19d0SLisandro Dalcin }
2044277b19d0SLisandro Dalcin 
2045277b19d0SLisandro Dalcin #undef __FUNCT__
2046f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
2047f6a906c0SBarry Smith /*@
2048f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
2049f6a906c0SBarry Smith    of an adjoint solver
2050f6a906c0SBarry Smith 
2051f6a906c0SBarry Smith    Collective on TS
2052f6a906c0SBarry Smith 
2053f6a906c0SBarry Smith    Input Parameter:
2054f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
2055f6a906c0SBarry Smith 
2056f6a906c0SBarry Smith    Level: advanced
2057f6a906c0SBarry Smith 
2058f6a906c0SBarry Smith .keywords: TS, timestep, setup
2059f6a906c0SBarry Smith 
2060947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
2061f6a906c0SBarry Smith @*/
2062f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
2063f6a906c0SBarry Smith {
2064f6a906c0SBarry Smith   PetscErrorCode ierr;
2065f6a906c0SBarry Smith 
2066f6a906c0SBarry Smith   PetscFunctionBegin;
2067f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2068f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
2069dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
2070d8151eeaSBarry Smith 
2071947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
2072d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2073d8151eeaSBarry Smith     if (ts->vecs_sensip){
2074d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2075d8151eeaSBarry Smith     }
2076947abb85SHong Zhang   }
2077d8151eeaSBarry Smith 
207842f2b339SBarry Smith   if (ts->ops->adjointsetup) {
207942f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
2080f6a906c0SBarry Smith   }
2081f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
2082f6a906c0SBarry Smith   PetscFunctionReturn(0);
2083f6a906c0SBarry Smith }
2084f6a906c0SBarry Smith 
2085f6a906c0SBarry Smith #undef __FUNCT__
2086277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
2087277b19d0SLisandro Dalcin /*@
2088277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2089277b19d0SLisandro Dalcin 
2090277b19d0SLisandro Dalcin    Collective on TS
2091277b19d0SLisandro Dalcin 
2092277b19d0SLisandro Dalcin    Input Parameter:
2093277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2094277b19d0SLisandro Dalcin 
2095277b19d0SLisandro Dalcin    Level: beginner
2096277b19d0SLisandro Dalcin 
2097277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2098277b19d0SLisandro Dalcin 
2099277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2100277b19d0SLisandro Dalcin @*/
2101277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2102277b19d0SLisandro Dalcin {
2103277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2104277b19d0SLisandro Dalcin 
2105277b19d0SLisandro Dalcin   PetscFunctionBegin;
2106277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2107b18ea86cSHong Zhang 
2108277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2109277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2110277b19d0SLisandro Dalcin   }
2111277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2112e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2113bbd56ea5SKarl Rupp 
21144e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
21154e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2116214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
21176bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2118e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2119e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
212038637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2121bbd56ea5SKarl Rupp 
2122947abb85SHong Zhang  if (ts->vec_costintegral) {
2123d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2124d8151eeaSBarry Smith     if (ts->vecs_drdp){
2125d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2126d8151eeaSBarry Smith     }
2127947abb85SHong Zhang   }
2128d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2129d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2130ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
21312c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
213236eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2133277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2134d763cef2SBarry Smith   PetscFunctionReturn(0);
2135d763cef2SBarry Smith }
2136d763cef2SBarry Smith 
21374a2ae208SSatish Balay #undef __FUNCT__
21384a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2139d8e5e3e6SSatish Balay /*@
2140d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2141d763cef2SBarry Smith    with TSCreate().
2142d763cef2SBarry Smith 
2143d763cef2SBarry Smith    Collective on TS
2144d763cef2SBarry Smith 
2145d763cef2SBarry Smith    Input Parameter:
2146d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2147d763cef2SBarry Smith 
2148d763cef2SBarry Smith    Level: beginner
2149d763cef2SBarry Smith 
2150d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2151d763cef2SBarry Smith 
2152d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2153d763cef2SBarry Smith @*/
21546bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2155d763cef2SBarry Smith {
21566849ba73SBarry Smith   PetscErrorCode ierr;
2157d763cef2SBarry Smith 
2158d763cef2SBarry Smith   PetscFunctionBegin;
21596bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
21606bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
21616bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2162d763cef2SBarry Smith 
21636bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2164277b19d0SLisandro Dalcin 
2165e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2166e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
21676bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
21686d4c513bSLisandro Dalcin 
2169bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2170bc952696SBarry Smith 
217184df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
21726427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
21736427ac75SLisandro Dalcin 
21746bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
21756bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
21766bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
21770dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
21786d4c513bSLisandro Dalcin 
2179a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2180d763cef2SBarry Smith   PetscFunctionReturn(0);
2181d763cef2SBarry Smith }
2182d763cef2SBarry Smith 
21834a2ae208SSatish Balay #undef __FUNCT__
21844a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2185d8e5e3e6SSatish Balay /*@
2186d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2187d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2188d763cef2SBarry Smith 
2189d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith    Input Parameter:
2192d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2193d763cef2SBarry Smith 
2194d763cef2SBarry Smith    Output Parameter:
2195d763cef2SBarry Smith .  snes - the nonlinear solver context
2196d763cef2SBarry Smith 
2197d763cef2SBarry Smith    Notes:
2198d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2199d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
220094b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2201d763cef2SBarry Smith 
2202d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
22030298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2204d763cef2SBarry Smith 
2205d763cef2SBarry Smith    Level: beginner
2206d763cef2SBarry Smith 
2207d763cef2SBarry Smith .keywords: timestep, get, SNES
2208d763cef2SBarry Smith @*/
22097087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2210d763cef2SBarry Smith {
2211d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2212d372ba47SLisandro Dalcin 
2213d763cef2SBarry Smith   PetscFunctionBegin;
22140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22154482741eSBarry Smith   PetscValidPointer(snes,2);
2216d372ba47SLisandro Dalcin   if (!ts->snes) {
2217ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
22180298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
22193bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2220d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2221496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
22229e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
22239e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
22249e2a6581SJed Brown     }
2225d372ba47SLisandro Dalcin   }
2226d763cef2SBarry Smith   *snes = ts->snes;
2227d763cef2SBarry Smith   PetscFunctionReturn(0);
2228d763cef2SBarry Smith }
2229d763cef2SBarry Smith 
22304a2ae208SSatish Balay #undef __FUNCT__
2231deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2232deb2cd25SJed Brown /*@
2233deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2234deb2cd25SJed Brown 
2235deb2cd25SJed Brown    Collective
2236deb2cd25SJed Brown 
2237deb2cd25SJed Brown    Input Parameter:
2238deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2239deb2cd25SJed Brown -  snes - the nonlinear solver context
2240deb2cd25SJed Brown 
2241deb2cd25SJed Brown    Notes:
2242deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2243deb2cd25SJed Brown 
2244deb2cd25SJed Brown    Level: developer
2245deb2cd25SJed Brown 
2246deb2cd25SJed Brown .keywords: timestep, set, SNES
2247deb2cd25SJed Brown @*/
2248deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2249deb2cd25SJed Brown {
2250deb2cd25SJed Brown   PetscErrorCode ierr;
2251d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2252deb2cd25SJed Brown 
2253deb2cd25SJed Brown   PetscFunctionBegin;
2254deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2255deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2256deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2257deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2258bbd56ea5SKarl Rupp 
2259deb2cd25SJed Brown   ts->snes = snes;
2260bbd56ea5SKarl Rupp 
22610298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
22620298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2263740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
22640298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2265740132f1SEmil Constantinescu   }
2266deb2cd25SJed Brown   PetscFunctionReturn(0);
2267deb2cd25SJed Brown }
2268deb2cd25SJed Brown 
2269deb2cd25SJed Brown #undef __FUNCT__
227094b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2271d8e5e3e6SSatish Balay /*@
227294b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2273d763cef2SBarry Smith    a TS (timestepper) context.
2274d763cef2SBarry Smith 
227594b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2276d763cef2SBarry Smith 
2277d763cef2SBarry Smith    Input Parameter:
2278d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2279d763cef2SBarry Smith 
2280d763cef2SBarry Smith    Output Parameter:
228194b7f48cSBarry Smith .  ksp - the nonlinear solver context
2282d763cef2SBarry Smith 
2283d763cef2SBarry Smith    Notes:
228494b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2285d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2286d763cef2SBarry Smith    KSP and PC contexts as well.
2287d763cef2SBarry Smith 
228894b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
22890298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2290d763cef2SBarry Smith 
2291d763cef2SBarry Smith    Level: beginner
2292d763cef2SBarry Smith 
229394b7f48cSBarry Smith .keywords: timestep, get, KSP
2294d763cef2SBarry Smith @*/
22957087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2296d763cef2SBarry Smith {
2297d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2298089b2837SJed Brown   SNES           snes;
2299d372ba47SLisandro Dalcin 
2300d763cef2SBarry Smith   PetscFunctionBegin;
23010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23024482741eSBarry Smith   PetscValidPointer(ksp,2);
230317186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2304e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2305089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2306089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2307d763cef2SBarry Smith   PetscFunctionReturn(0);
2308d763cef2SBarry Smith }
2309d763cef2SBarry Smith 
2310d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2311d763cef2SBarry Smith 
23124a2ae208SSatish Balay #undef __FUNCT__
2313adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2314adb62b0dSMatthew Knepley /*@
2315adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2316adb62b0dSMatthew Knepley    maximum time for iteration.
2317adb62b0dSMatthew Knepley 
23183f9fe445SBarry Smith    Not Collective
2319adb62b0dSMatthew Knepley 
2320adb62b0dSMatthew Knepley    Input Parameters:
2321adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
23220298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
23230298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2324adb62b0dSMatthew Knepley 
2325adb62b0dSMatthew Knepley    Level: intermediate
2326adb62b0dSMatthew Knepley 
2327adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2328adb62b0dSMatthew Knepley @*/
23297087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2330adb62b0dSMatthew Knepley {
2331adb62b0dSMatthew Knepley   PetscFunctionBegin;
23320700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2333abc0a331SBarry Smith   if (maxsteps) {
23344482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2335adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2336adb62b0dSMatthew Knepley   }
2337abc0a331SBarry Smith   if (maxtime) {
23384482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2339adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2340adb62b0dSMatthew Knepley   }
2341adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2342adb62b0dSMatthew Knepley }
2343adb62b0dSMatthew Knepley 
2344adb62b0dSMatthew Knepley #undef __FUNCT__
23454a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2346d763cef2SBarry Smith /*@
2347d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2348d763cef2SBarry Smith    maximum time for iteration.
2349d763cef2SBarry Smith 
23503f9fe445SBarry Smith    Logically Collective on TS
2351d763cef2SBarry Smith 
2352d763cef2SBarry Smith    Input Parameters:
2353d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2354d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2355d763cef2SBarry Smith -  maxtime - final time to iterate to
2356d763cef2SBarry Smith 
2357d763cef2SBarry Smith    Options Database Keys:
2358d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
23593bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2360d763cef2SBarry Smith 
2361d763cef2SBarry Smith    Notes:
2362d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2363d763cef2SBarry Smith 
2364d763cef2SBarry Smith    Level: intermediate
2365d763cef2SBarry Smith 
2366d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2367a43b19c4SJed Brown 
2368a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2369d763cef2SBarry Smith @*/
23707087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2371d763cef2SBarry Smith {
2372d763cef2SBarry Smith   PetscFunctionBegin;
23730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2374c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2375c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
237639b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
237739b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2378d763cef2SBarry Smith   PetscFunctionReturn(0);
2379d763cef2SBarry Smith }
2380d763cef2SBarry Smith 
23814a2ae208SSatish Balay #undef __FUNCT__
23824a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2383d763cef2SBarry Smith /*@
2384d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2385d763cef2SBarry Smith    for use by the TS routines.
2386d763cef2SBarry Smith 
23873f9fe445SBarry Smith    Logically Collective on TS and Vec
2388d763cef2SBarry Smith 
2389d763cef2SBarry Smith    Input Parameters:
2390d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
23910910c330SBarry Smith -  u - the solution vector
2392d763cef2SBarry Smith 
2393d763cef2SBarry Smith    Level: beginner
2394d763cef2SBarry Smith 
2395d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2396d763cef2SBarry Smith @*/
23970910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2398d763cef2SBarry Smith {
23998737fe31SLisandro Dalcin   PetscErrorCode ierr;
2400496e6a7aSJed Brown   DM             dm;
24018737fe31SLisandro Dalcin 
2402d763cef2SBarry Smith   PetscFunctionBegin;
24030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24040910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
24050910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
24066bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
24070910c330SBarry Smith   ts->vec_sol = u;
2408bbd56ea5SKarl Rupp 
2409496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
24100910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2411d763cef2SBarry Smith   PetscFunctionReturn(0);
2412d763cef2SBarry Smith }
2413d763cef2SBarry Smith 
2414e74ef692SMatthew Knepley #undef __FUNCT__
241573b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
241673b18844SBarry Smith /*@
241773b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
241873b18844SBarry Smith 
241973b18844SBarry Smith    Logically Collective on TS
242073b18844SBarry Smith 
242173b18844SBarry Smith    Input Parameters:
242273b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
242373b18844SBarry Smith .  steps - number of steps to use
242473b18844SBarry Smith 
242573b18844SBarry Smith    Level: intermediate
242673b18844SBarry Smith 
242773b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
242873b18844SBarry Smith           so as to integrate back to less than the original timestep
242973b18844SBarry Smith 
243073b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
243173b18844SBarry Smith 
243273b18844SBarry Smith .seealso: TSSetExactFinalTime()
243373b18844SBarry Smith @*/
243473b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
243573b18844SBarry Smith {
243673b18844SBarry Smith   PetscFunctionBegin;
243773b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
243873b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
243973b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
244073b18844SBarry 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");
244173b18844SBarry Smith   ts->adjoint_max_steps = steps;
244273b18844SBarry Smith   PetscFunctionReturn(0);
244373b18844SBarry Smith }
244473b18844SBarry Smith 
244573b18844SBarry Smith #undef __FUNCT__
2446dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients"
2447c235aa8dSHong Zhang /*@
2448dfb21088SHong 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
24490cf82b59SBarry Smith       for use by the TSAdjoint routines.
2450c235aa8dSHong Zhang 
2451c235aa8dSHong Zhang    Logically Collective on TS and Vec
2452c235aa8dSHong Zhang 
2453c235aa8dSHong Zhang    Input Parameters:
2454c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2455abc2977eSBarry 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
2456abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2457c235aa8dSHong Zhang 
2458c235aa8dSHong Zhang    Level: beginner
2459c235aa8dSHong Zhang 
2460abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
24610cf82b59SBarry Smith 
2462c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2463c235aa8dSHong Zhang @*/
2464dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2465c235aa8dSHong Zhang {
2466c235aa8dSHong Zhang   PetscFunctionBegin;
2467c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2468abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2469abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2470abc2977eSBarry Smith   ts->vecs_sensip = mu;
2471dfb21088SHong 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");
2472abc2977eSBarry Smith   ts->numcost  = numcost;
2473ad8e2604SHong Zhang   PetscFunctionReturn(0);
2474ad8e2604SHong Zhang }
2475ad8e2604SHong Zhang 
2476ad8e2604SHong Zhang #undef __FUNCT__
24775bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2478ad8e2604SHong Zhang /*@C
24790cf82b59SBarry 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.
2480ad8e2604SHong Zhang 
2481ad8e2604SHong Zhang   Logically Collective on TS
2482ad8e2604SHong Zhang 
2483ad8e2604SHong Zhang   Input Parameters:
2484ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2485ad8e2604SHong Zhang - func - The function
2486ad8e2604SHong Zhang 
2487ad8e2604SHong Zhang   Calling sequence of func:
24880cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
248905755b9cSHong Zhang +   t - current timestep
24900cf82b59SBarry Smith .   y - input vector (current ODE solution)
249105755b9cSHong Zhang .   A - output matrix
249205755b9cSHong Zhang -   ctx - [optional] user-defined function context
2493ad8e2604SHong Zhang 
2494ad8e2604SHong Zhang   Level: intermediate
2495ad8e2604SHong Zhang 
24960cf82b59SBarry 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
24970cf82b59SBarry Smith 
2498ad8e2604SHong Zhang .keywords: TS, sensitivity
2499ad8e2604SHong Zhang .seealso:
2500ad8e2604SHong Zhang @*/
25015bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2502ad8e2604SHong Zhang {
2503ad8e2604SHong Zhang   PetscErrorCode ierr;
2504ad8e2604SHong Zhang 
2505ad8e2604SHong Zhang   PetscFunctionBegin;
2506ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2507ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2508ad8e2604SHong Zhang 
2509ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2510ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2511ad8e2604SHong Zhang   if(Amat) {
2512ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2513ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2514ad8e2604SHong Zhang     ts->Jacp = Amat;
2515ad8e2604SHong Zhang   }
2516ad8e2604SHong Zhang   PetscFunctionReturn(0);
2517ad8e2604SHong Zhang }
2518ad8e2604SHong Zhang 
2519ad8e2604SHong Zhang #undef __FUNCT__
25205bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
25210cf82b59SBarry Smith /*@C
25225bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2523ad8e2604SHong Zhang 
2524ad8e2604SHong Zhang   Collective on TS
2525ad8e2604SHong Zhang 
2526ad8e2604SHong Zhang   Input Parameters:
2527ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2528ad8e2604SHong Zhang 
2529ad8e2604SHong Zhang   Level: developer
2530ad8e2604SHong Zhang 
2531ad8e2604SHong Zhang .keywords: TS, sensitivity
25325bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2533ad8e2604SHong Zhang @*/
25345bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2535ad8e2604SHong Zhang {
2536ad8e2604SHong Zhang   PetscErrorCode ierr;
2537ad8e2604SHong Zhang 
2538ad8e2604SHong Zhang   PetscFunctionBegin;
2539ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2540ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2541ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2542ad8e2604SHong Zhang 
2543ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2544ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2545ad8e2604SHong Zhang   PetscStackPop;
2546c235aa8dSHong Zhang   PetscFunctionReturn(0);
2547c235aa8dSHong Zhang }
2548c235aa8dSHong Zhang 
2549c235aa8dSHong Zhang #undef __FUNCT__
2550dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand"
25516fd0887fSHong Zhang /*@C
2552dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
25536fd0887fSHong Zhang 
25546fd0887fSHong Zhang     Logically Collective on TS
25556fd0887fSHong Zhang 
25566fd0887fSHong Zhang     Input Parameters:
25576fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
2558abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
25590cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
2560b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2561b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2562b1cb36f3SHong Zhang .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
2563b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
25646fd0887fSHong Zhang 
25650cf82b59SBarry Smith     Calling sequence of rf:
25660cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
25676fd0887fSHong Zhang 
25686fd0887fSHong Zhang +   t - current timestep
25690cf82b59SBarry Smith .   y - input vector
25700cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
25716fd0887fSHong Zhang -   ctx - [optional] user-defined function context
25726fd0887fSHong Zhang 
2573b612ec54SBarry Smith    Calling sequence of drdyf:
25740cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
2575b612ec54SBarry Smith 
2576b612ec54SBarry Smith    Calling sequence of drdpf:
25770cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
2578b612ec54SBarry Smith 
2579b612ec54SBarry Smith     Level: intermediate
25806fd0887fSHong Zhang 
2581abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
25820cf82b59SBarry Smith 
25836fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
25846fd0887fSHong Zhang 
2585dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
25866fd0887fSHong Zhang @*/
2587dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
2588d8151eeaSBarry Smith                                                           PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2589b1cb36f3SHong Zhang                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
2590b1cb36f3SHong Zhang                                                           PetscBool fwd,void *ctx)
25916fd0887fSHong Zhang {
25926fd0887fSHong Zhang   PetscErrorCode ierr;
25936fd0887fSHong Zhang 
25946fd0887fSHong Zhang   PetscFunctionBegin;
25956fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2596dfb21088SHong 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()");
2597c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
25986fd0887fSHong Zhang 
2599b1cb36f3SHong Zhang   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
2600abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
26012c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
26020cf82b59SBarry Smith   ts->costintegrand    = rf;
260305755b9cSHong Zhang   ts->costintegrandctx = ctx;
2604b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
2605b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
26066fd0887fSHong Zhang   PetscFunctionReturn(0);
26076fd0887fSHong Zhang }
26086fd0887fSHong Zhang 
26096fd0887fSHong Zhang #undef __FUNCT__
2610dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral"
261136eaed60SHong Zhang /*@
2612dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
261336eaed60SHong Zhang    It is valid to call the routine after a backward run.
261436eaed60SHong Zhang 
261536eaed60SHong Zhang    Not Collective
261636eaed60SHong Zhang 
261736eaed60SHong Zhang    Input Parameter:
261836eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
261936eaed60SHong Zhang 
262036eaed60SHong Zhang    Output Parameter:
26212c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
262236eaed60SHong Zhang 
262336eaed60SHong Zhang    Level: intermediate
262436eaed60SHong Zhang 
2625dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
262636eaed60SHong Zhang 
262736eaed60SHong Zhang .keywords: TS, sensitivity analysis
262836eaed60SHong Zhang @*/
2629dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
263036eaed60SHong Zhang {
263136eaed60SHong Zhang   PetscFunctionBegin;
263236eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
263336eaed60SHong Zhang   PetscValidPointer(v,2);
26342c39e106SBarry Smith   *v = ts->vec_costintegral;
263536eaed60SHong Zhang   PetscFunctionReturn(0);
263636eaed60SHong Zhang }
263736eaed60SHong Zhang 
263836eaed60SHong Zhang #undef __FUNCT__
2639d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
26406fd0887fSHong Zhang /*@
26412c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
26426fd0887fSHong Zhang 
26436fd0887fSHong Zhang    Input Parameters:
26446fd0887fSHong Zhang +  ts - the TS context
26456fd0887fSHong Zhang .  t - current time
26460cf82b59SBarry Smith -  y - state vector, i.e. current solution
26476fd0887fSHong Zhang 
26486fd0887fSHong Zhang    Output Parameter:
2649abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
26506fd0887fSHong Zhang 
26516fd0887fSHong Zhang    Note:
26526fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
26536fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
26546fd0887fSHong Zhang 
26556fd0887fSHong Zhang    Level: developer
26566fd0887fSHong Zhang 
26576fd0887fSHong Zhang .keywords: TS, compute
26586fd0887fSHong Zhang 
2659dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
26606fd0887fSHong Zhang @*/
26610cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
26626fd0887fSHong Zhang {
26636fd0887fSHong Zhang   PetscErrorCode ierr;
26646fd0887fSHong Zhang 
26656fd0887fSHong Zhang   PetscFunctionBegin;
26666fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26670cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
26686fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
26696fd0887fSHong Zhang 
26700cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
2671e4680132SHong Zhang   if (ts->costintegrand) {
2672e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
26730cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
26746fd0887fSHong Zhang     PetscStackPop;
26756fd0887fSHong Zhang   } else {
26766fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
26776fd0887fSHong Zhang   }
26786fd0887fSHong Zhang 
26790cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
26806fd0887fSHong Zhang   PetscFunctionReturn(0);
26816fd0887fSHong Zhang }
26826fd0887fSHong Zhang 
26836fd0887fSHong Zhang #undef __FUNCT__
2684d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
268505755b9cSHong Zhang /*@
2686d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
268705755b9cSHong Zhang 
268805755b9cSHong Zhang   Collective on TS
268905755b9cSHong Zhang 
269005755b9cSHong Zhang   Input Parameters:
269105755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
269205755b9cSHong Zhang 
269305755b9cSHong Zhang   Notes:
2694d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
269505755b9cSHong Zhang   so most users would not generally call this routine themselves.
269605755b9cSHong Zhang 
269705755b9cSHong Zhang   Level: developer
269805755b9cSHong Zhang 
269905755b9cSHong Zhang .keywords: TS, sensitivity
2700d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
270105755b9cSHong Zhang @*/
27020cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
270305755b9cSHong Zhang {
270405755b9cSHong Zhang   PetscErrorCode ierr;
270505755b9cSHong Zhang 
270605755b9cSHong Zhang   PetscFunctionBegin;
270705755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27080cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
270905755b9cSHong Zhang 
271005755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
27110cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
271205755b9cSHong Zhang   PetscStackPop;
271305755b9cSHong Zhang   PetscFunctionReturn(0);
271405755b9cSHong Zhang }
271505755b9cSHong Zhang 
271605755b9cSHong Zhang #undef __FUNCT__
2717d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
271805755b9cSHong Zhang /*@
2719d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
272005755b9cSHong Zhang 
272105755b9cSHong Zhang   Collective on TS
272205755b9cSHong Zhang 
272305755b9cSHong Zhang   Input Parameters:
272405755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
272505755b9cSHong Zhang 
272605755b9cSHong Zhang   Notes:
272705755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
272805755b9cSHong Zhang   so most users would not generally call this routine themselves.
272905755b9cSHong Zhang 
273005755b9cSHong Zhang   Level: developer
273105755b9cSHong Zhang 
273205755b9cSHong Zhang .keywords: TS, sensitivity
2733d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
273405755b9cSHong Zhang @*/
27350cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
273605755b9cSHong Zhang {
273705755b9cSHong Zhang   PetscErrorCode ierr;
273805755b9cSHong Zhang 
273905755b9cSHong Zhang   PetscFunctionBegin;
274005755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27410cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
274205755b9cSHong Zhang 
274305755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
27440cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
274505755b9cSHong Zhang   PetscStackPop;
274605755b9cSHong Zhang   PetscFunctionReturn(0);
274705755b9cSHong Zhang }
274805755b9cSHong Zhang 
274905755b9cSHong Zhang #undef __FUNCT__
2750e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2751ac226902SBarry Smith /*@C
2752000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
27533f2090d5SJed Brown   called once at the beginning of each time step.
2754000e7ae3SMatthew Knepley 
27553f9fe445SBarry Smith   Logically Collective on TS
2756000e7ae3SMatthew Knepley 
2757000e7ae3SMatthew Knepley   Input Parameters:
2758000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2759000e7ae3SMatthew Knepley - func - The function
2760000e7ae3SMatthew Knepley 
2761000e7ae3SMatthew Knepley   Calling sequence of func:
2762000e7ae3SMatthew Knepley . func (TS ts);
2763000e7ae3SMatthew Knepley 
2764000e7ae3SMatthew Knepley   Level: intermediate
2765000e7ae3SMatthew Knepley 
2766b8123daeSJed Brown   Note:
2767b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2768b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2769b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2770b8123daeSJed Brown 
2771000e7ae3SMatthew Knepley .keywords: TS, timestep
27729be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2773000e7ae3SMatthew Knepley @*/
27747087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2775000e7ae3SMatthew Knepley {
2776000e7ae3SMatthew Knepley   PetscFunctionBegin;
27770700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2778ae60f76fSBarry Smith   ts->prestep = func;
2779000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2780000e7ae3SMatthew Knepley }
2781000e7ae3SMatthew Knepley 
2782e74ef692SMatthew Knepley #undef __FUNCT__
27833f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
278409ee8438SJed Brown /*@
27853f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
27863f2090d5SJed Brown 
27873f2090d5SJed Brown   Collective on TS
27883f2090d5SJed Brown 
27893f2090d5SJed Brown   Input Parameters:
27903f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
27913f2090d5SJed Brown 
27923f2090d5SJed Brown   Notes:
27933f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
27943f2090d5SJed Brown   so most users would not generally call this routine themselves.
27953f2090d5SJed Brown 
27963f2090d5SJed Brown   Level: developer
27973f2090d5SJed Brown 
27983f2090d5SJed Brown .keywords: TS, timestep
27999be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
28003f2090d5SJed Brown @*/
28017087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
28023f2090d5SJed Brown {
28033f2090d5SJed Brown   PetscErrorCode ierr;
28043f2090d5SJed Brown 
28053f2090d5SJed Brown   PetscFunctionBegin;
28060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2807ae60f76fSBarry Smith   if (ts->prestep) {
2808ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2809312ce896SJed Brown   }
28103f2090d5SJed Brown   PetscFunctionReturn(0);
28113f2090d5SJed Brown }
28123f2090d5SJed Brown 
28133f2090d5SJed Brown #undef __FUNCT__
2814b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2815b8123daeSJed Brown /*@C
2816b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2817b8123daeSJed Brown   called once at the beginning of each stage.
2818b8123daeSJed Brown 
2819b8123daeSJed Brown   Logically Collective on TS
2820b8123daeSJed Brown 
2821b8123daeSJed Brown   Input Parameters:
2822b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2823b8123daeSJed Brown - func - The function
2824b8123daeSJed Brown 
2825b8123daeSJed Brown   Calling sequence of func:
2826b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2827b8123daeSJed Brown 
2828b8123daeSJed Brown   Level: intermediate
2829b8123daeSJed Brown 
2830b8123daeSJed Brown   Note:
2831b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2832b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2833b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2834b8123daeSJed Brown 
2835b8123daeSJed Brown .keywords: TS, timestep
28369be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2837b8123daeSJed Brown @*/
2838b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2839b8123daeSJed Brown {
2840b8123daeSJed Brown   PetscFunctionBegin;
2841b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2842ae60f76fSBarry Smith   ts->prestage = func;
2843b8123daeSJed Brown   PetscFunctionReturn(0);
2844b8123daeSJed Brown }
2845b8123daeSJed Brown 
2846b8123daeSJed Brown #undef __FUNCT__
28479be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
28489be3e283SDebojyoti Ghosh /*@C
28499be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
28509be3e283SDebojyoti Ghosh   called once at the end of each stage.
28519be3e283SDebojyoti Ghosh 
28529be3e283SDebojyoti Ghosh   Logically Collective on TS
28539be3e283SDebojyoti Ghosh 
28549be3e283SDebojyoti Ghosh   Input Parameters:
28559be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
28569be3e283SDebojyoti Ghosh - func - The function
28579be3e283SDebojyoti Ghosh 
28589be3e283SDebojyoti Ghosh   Calling sequence of func:
28599be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
28609be3e283SDebojyoti Ghosh 
28619be3e283SDebojyoti Ghosh   Level: intermediate
28629be3e283SDebojyoti Ghosh 
28639be3e283SDebojyoti Ghosh   Note:
28649be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
28659be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
28669be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
28679be3e283SDebojyoti Ghosh 
28689be3e283SDebojyoti Ghosh .keywords: TS, timestep
28699be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
28709be3e283SDebojyoti Ghosh @*/
28719be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
28729be3e283SDebojyoti Ghosh {
28739be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28749be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
28759be3e283SDebojyoti Ghosh   ts->poststage = func;
28769be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28779be3e283SDebojyoti Ghosh }
28789be3e283SDebojyoti Ghosh 
28799be3e283SDebojyoti Ghosh #undef __FUNCT__
2880b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2881b8123daeSJed Brown /*@
2882b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2883b8123daeSJed Brown 
2884b8123daeSJed Brown   Collective on TS
2885b8123daeSJed Brown 
2886b8123daeSJed Brown   Input Parameters:
2887b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
28889be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2889b8123daeSJed Brown 
2890b8123daeSJed Brown   Notes:
2891b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2892b8123daeSJed Brown   most users would not generally call this routine themselves.
2893b8123daeSJed Brown 
2894b8123daeSJed Brown   Level: developer
2895b8123daeSJed Brown 
2896b8123daeSJed Brown .keywords: TS, timestep
28979be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2898b8123daeSJed Brown @*/
2899b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2900b8123daeSJed Brown {
2901b8123daeSJed Brown   PetscErrorCode ierr;
2902b8123daeSJed Brown 
2903b8123daeSJed Brown   PetscFunctionBegin;
2904b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2905ae60f76fSBarry Smith   if (ts->prestage) {
2906ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2907b8123daeSJed Brown   }
2908b8123daeSJed Brown   PetscFunctionReturn(0);
2909b8123daeSJed Brown }
2910b8123daeSJed Brown 
2911b8123daeSJed Brown #undef __FUNCT__
29129be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
29139be3e283SDebojyoti Ghosh /*@
29149be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
29159be3e283SDebojyoti Ghosh 
29169be3e283SDebojyoti Ghosh   Collective on TS
29179be3e283SDebojyoti Ghosh 
29189be3e283SDebojyoti Ghosh   Input Parameters:
29199be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
29209be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
29219be3e283SDebojyoti Ghosh   stageindex  - Stage number
29229be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
29239be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
29249be3e283SDebojyoti Ghosh 
29259be3e283SDebojyoti Ghosh   Notes:
29269be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
29279be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
29289be3e283SDebojyoti Ghosh 
29299be3e283SDebojyoti Ghosh   Level: developer
29309be3e283SDebojyoti Ghosh 
29319be3e283SDebojyoti Ghosh .keywords: TS, timestep
29329be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
29339be3e283SDebojyoti Ghosh @*/
29349be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
29359be3e283SDebojyoti Ghosh {
29369be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
29379be3e283SDebojyoti Ghosh 
29389be3e283SDebojyoti Ghosh   PetscFunctionBegin;
29399be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29404beae5d8SLisandro Dalcin   if (ts->poststage) {
29419be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
29429be3e283SDebojyoti Ghosh   }
29439be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
29449be3e283SDebojyoti Ghosh }
29459be3e283SDebojyoti Ghosh 
29469be3e283SDebojyoti Ghosh #undef __FUNCT__
2947e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2948ac226902SBarry Smith /*@C
2949000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
29503f2090d5SJed Brown   called once at the end of each time step.
2951000e7ae3SMatthew Knepley 
29523f9fe445SBarry Smith   Logically Collective on TS
2953000e7ae3SMatthew Knepley 
2954000e7ae3SMatthew Knepley   Input Parameters:
2955000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2956000e7ae3SMatthew Knepley - func - The function
2957000e7ae3SMatthew Knepley 
2958000e7ae3SMatthew Knepley   Calling sequence of func:
2959b8123daeSJed Brown $ func (TS ts);
2960000e7ae3SMatthew Knepley 
2961000e7ae3SMatthew Knepley   Level: intermediate
2962000e7ae3SMatthew Knepley 
2963000e7ae3SMatthew Knepley .keywords: TS, timestep
2964b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2965000e7ae3SMatthew Knepley @*/
29667087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2967000e7ae3SMatthew Knepley {
2968000e7ae3SMatthew Knepley   PetscFunctionBegin;
29690700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2970ae60f76fSBarry Smith   ts->poststep = func;
2971000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2972000e7ae3SMatthew Knepley }
2973000e7ae3SMatthew Knepley 
2974e74ef692SMatthew Knepley #undef __FUNCT__
29753f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
297609ee8438SJed Brown /*@
29773f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
29783f2090d5SJed Brown 
29793f2090d5SJed Brown   Collective on TS
29803f2090d5SJed Brown 
29813f2090d5SJed Brown   Input Parameters:
29823f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
29833f2090d5SJed Brown 
29843f2090d5SJed Brown   Notes:
29853f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
29863f2090d5SJed Brown   so most users would not generally call this routine themselves.
29873f2090d5SJed Brown 
29883f2090d5SJed Brown   Level: developer
29893f2090d5SJed Brown 
29903f2090d5SJed Brown .keywords: TS, timestep
29913f2090d5SJed Brown @*/
29927087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
29933f2090d5SJed Brown {
29943f2090d5SJed Brown   PetscErrorCode ierr;
29953f2090d5SJed Brown 
29963f2090d5SJed Brown   PetscFunctionBegin;
29970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2998ae60f76fSBarry Smith   if (ts->poststep) {
2999ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
300072ac3e02SJed Brown   }
30013f2090d5SJed Brown   PetscFunctionReturn(0);
30023f2090d5SJed Brown }
30033f2090d5SJed Brown 
3004d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3005d763cef2SBarry Smith 
30064a2ae208SSatish Balay #undef __FUNCT__
3007a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
3008d763cef2SBarry Smith /*@C
3009a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3010d763cef2SBarry Smith    timestep to display the iteration's  progress.
3011d763cef2SBarry Smith 
30123f9fe445SBarry Smith    Logically Collective on TS
3013d763cef2SBarry Smith 
3014d763cef2SBarry Smith    Input Parameters:
3015d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3016e213d8f1SJed Brown .  monitor - monitoring routine
3017329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
30180298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3019b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
30200298fd71SBarry Smith           (may be NULL)
3021d763cef2SBarry Smith 
3022e213d8f1SJed Brown    Calling sequence of monitor:
30230910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3024d763cef2SBarry Smith 
3025d763cef2SBarry Smith +    ts - the TS context
302663e21af5SBarry 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)
30271f06c33eSBarry Smith .    time - current time
30280910c330SBarry Smith .    u - current iterate
3029d763cef2SBarry Smith -    mctx - [optional] monitoring context
3030d763cef2SBarry Smith 
3031d763cef2SBarry Smith    Notes:
3032d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3033d763cef2SBarry Smith    already has been loaded.
3034d763cef2SBarry Smith 
3035025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
3036025f1a04SBarry Smith 
3037d763cef2SBarry Smith    Level: intermediate
3038d763cef2SBarry Smith 
3039d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3040d763cef2SBarry Smith 
3041a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3042d763cef2SBarry Smith @*/
3043c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3044d763cef2SBarry Smith {
3045d763cef2SBarry Smith   PetscFunctionBegin;
30460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
304717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3048d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
30498704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3050d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3051d763cef2SBarry Smith   PetscFunctionReturn(0);
3052d763cef2SBarry Smith }
3053d763cef2SBarry Smith 
30544a2ae208SSatish Balay #undef __FUNCT__
3055a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
3056d763cef2SBarry Smith /*@C
3057a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3058d763cef2SBarry Smith 
30593f9fe445SBarry Smith    Logically Collective on TS
3060d763cef2SBarry Smith 
3061d763cef2SBarry Smith    Input Parameters:
3062d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3063d763cef2SBarry Smith 
3064d763cef2SBarry Smith    Notes:
3065d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3066d763cef2SBarry Smith 
3067d763cef2SBarry Smith    Level: intermediate
3068d763cef2SBarry Smith 
3069d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3070d763cef2SBarry Smith 
3071a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3072d763cef2SBarry Smith @*/
30737087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3074d763cef2SBarry Smith {
3075d952e501SBarry Smith   PetscErrorCode ierr;
3076d952e501SBarry Smith   PetscInt       i;
3077d952e501SBarry Smith 
3078d763cef2SBarry Smith   PetscFunctionBegin;
30790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3080d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
30818704b422SBarry Smith     if (ts->monitordestroy[i]) {
30828704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3083d952e501SBarry Smith     }
3084d952e501SBarry Smith   }
3085d763cef2SBarry Smith   ts->numbermonitors = 0;
3086d763cef2SBarry Smith   PetscFunctionReturn(0);
3087d763cef2SBarry Smith }
3088d763cef2SBarry Smith 
30894a2ae208SSatish Balay #undef __FUNCT__
3090a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
3091721cd6eeSBarry Smith /*@C
3092721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
30935516499fSSatish Balay 
30945516499fSSatish Balay    Level: intermediate
309541251cbbSSatish Balay 
30965516499fSSatish Balay .keywords: TS, set, monitor
30975516499fSSatish Balay 
309863e21af5SBarry Smith .seealso:  TSMonitorSet()
309941251cbbSSatish Balay @*/
3100721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3101d763cef2SBarry Smith {
3102dfbe8321SBarry Smith   PetscErrorCode ierr;
3103721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
310441aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3105d132466eSBarry Smith 
3106d763cef2SBarry Smith   PetscFunctionBegin;
31074d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
310841aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
310941aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3110721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
311141aca3d6SBarry Smith   if (iascii) {
3112649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
311363e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
311463e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
311563e21af5SBarry Smith     } else {
31168392e04aSShri 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);
311763e21af5SBarry Smith     }
3118649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
311941aca3d6SBarry Smith   } else if (ibinary) {
312041aca3d6SBarry Smith     PetscMPIInt rank;
312141aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
312241aca3d6SBarry Smith     if (!rank) {
312341aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
312441aca3d6SBarry Smith     } else {
312541aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
312641aca3d6SBarry Smith     }
312741aca3d6SBarry Smith   }
3128721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3129d763cef2SBarry Smith   PetscFunctionReturn(0);
3130d763cef2SBarry Smith }
3131d763cef2SBarry Smith 
31324a2ae208SSatish Balay #undef __FUNCT__
31339110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet"
31349110b2e7SHong Zhang /*@C
31359110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
31369110b2e7SHong Zhang    timestep to display the iteration's  progress.
31379110b2e7SHong Zhang 
31389110b2e7SHong Zhang    Logically Collective on TS
31399110b2e7SHong Zhang 
31409110b2e7SHong Zhang    Input Parameters:
31419110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
31429110b2e7SHong Zhang .  adjointmonitor - monitoring routine
31439110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
31449110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
31459110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
31469110b2e7SHong Zhang           (may be NULL)
31479110b2e7SHong Zhang 
31489110b2e7SHong Zhang    Calling sequence of monitor:
31499110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
31509110b2e7SHong Zhang 
31519110b2e7SHong Zhang +    ts - the TS context
31529110b2e7SHong 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
31539110b2e7SHong Zhang                                been interpolated to)
31549110b2e7SHong Zhang .    time - current time
31559110b2e7SHong Zhang .    u - current iterate
31569110b2e7SHong Zhang .    numcost - number of cost functionos
31579110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
31589110b2e7SHong Zhang .    mu - sensitivities to parameters
31599110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
31609110b2e7SHong Zhang 
31619110b2e7SHong Zhang    Notes:
31629110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
31639110b2e7SHong Zhang    already has been loaded.
31649110b2e7SHong Zhang 
31659110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
31669110b2e7SHong Zhang 
31679110b2e7SHong Zhang    Level: intermediate
31689110b2e7SHong Zhang 
31699110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
31709110b2e7SHong Zhang 
31719110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
31729110b2e7SHong Zhang @*/
31739110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
31749110b2e7SHong Zhang {
31759110b2e7SHong Zhang   PetscFunctionBegin;
31769110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31779110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
31789110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
31799110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
31809110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
31819110b2e7SHong Zhang   PetscFunctionReturn(0);
31829110b2e7SHong Zhang }
31839110b2e7SHong Zhang 
31849110b2e7SHong Zhang #undef __FUNCT__
31859110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel"
31869110b2e7SHong Zhang /*@C
31879110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
31889110b2e7SHong Zhang 
31899110b2e7SHong Zhang    Logically Collective on TS
31909110b2e7SHong Zhang 
31919110b2e7SHong Zhang    Input Parameters:
31929110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
31939110b2e7SHong Zhang 
31949110b2e7SHong Zhang    Notes:
31959110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
31969110b2e7SHong Zhang 
31979110b2e7SHong Zhang    Level: intermediate
31989110b2e7SHong Zhang 
31999110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
32009110b2e7SHong Zhang 
32019110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
32029110b2e7SHong Zhang @*/
32039110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
32049110b2e7SHong Zhang {
32059110b2e7SHong Zhang   PetscErrorCode ierr;
32069110b2e7SHong Zhang   PetscInt       i;
32079110b2e7SHong Zhang 
32089110b2e7SHong Zhang   PetscFunctionBegin;
32099110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32109110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
32119110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
32129110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
32139110b2e7SHong Zhang     }
32149110b2e7SHong Zhang   }
32159110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
32169110b2e7SHong Zhang   PetscFunctionReturn(0);
32179110b2e7SHong Zhang }
32189110b2e7SHong Zhang 
32199110b2e7SHong Zhang #undef __FUNCT__
3220110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault"
3221721cd6eeSBarry Smith /*@C
3222721cd6eeSBarry Smith    TSAdjointMonitorDefault - the default monitor of adjoint computations
3223110eb670SHong Zhang 
3224110eb670SHong Zhang    Level: intermediate
3225110eb670SHong Zhang 
3226110eb670SHong Zhang .keywords: TS, set, monitor
3227110eb670SHong Zhang 
3228110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3229110eb670SHong Zhang @*/
3230721cd6eeSBarry Smith PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
3231110eb670SHong Zhang {
3232110eb670SHong Zhang   PetscErrorCode ierr;
3233721cd6eeSBarry Smith   PetscViewer    viewer = vf->viewer;
3234110eb670SHong Zhang 
3235110eb670SHong Zhang   PetscFunctionBegin;
3236110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3237721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3238110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3239110eb670SHong 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);
3240110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3241721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3242110eb670SHong Zhang   PetscFunctionReturn(0);
3243110eb670SHong Zhang }
3244110eb670SHong Zhang 
3245110eb670SHong Zhang #undef __FUNCT__
3246cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3247cd652676SJed Brown /*@
3248cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3249cd652676SJed Brown 
3250cd652676SJed Brown    Collective on TS
3251cd652676SJed Brown 
3252cd652676SJed Brown    Input Argument:
3253cd652676SJed Brown +  ts - time stepping context
3254cd652676SJed Brown -  t - time to interpolate to
3255cd652676SJed Brown 
3256cd652676SJed Brown    Output Argument:
32570910c330SBarry Smith .  U - state at given time
3258cd652676SJed Brown 
3259cd652676SJed Brown    Level: intermediate
3260cd652676SJed Brown 
3261cd652676SJed Brown    Developer Notes:
3262cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3263cd652676SJed Brown 
3264cd652676SJed Brown .keywords: TS, set
3265cd652676SJed Brown 
3266*874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3267cd652676SJed Brown @*/
32680910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3269cd652676SJed Brown {
3270cd652676SJed Brown   PetscErrorCode ierr;
3271cd652676SJed Brown 
3272cd652676SJed Brown   PetscFunctionBegin;
3273cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3274b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
32756712e2f1SBarry Smith   if (t < ts->ptime - ts->time_step_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-ts->time_step_prev),(double)ts->ptime);
3276ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
32770910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3278cd652676SJed Brown   PetscFunctionReturn(0);
3279cd652676SJed Brown }
3280cd652676SJed Brown 
3281cd652676SJed Brown #undef __FUNCT__
32824a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3283d763cef2SBarry Smith /*@
32846d9e5789SSean Farley    TSStep - Steps one time step
3285d763cef2SBarry Smith 
3286d763cef2SBarry Smith    Collective on TS
3287d763cef2SBarry Smith 
3288d763cef2SBarry Smith    Input Parameter:
3289d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3290d763cef2SBarry Smith 
329127829d71SBarry Smith    Level: developer
3292d763cef2SBarry Smith 
3293b8123daeSJed Brown    Notes:
329427829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
329527829d71SBarry Smith 
3296b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3297b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3298b8123daeSJed Brown 
329925cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
330025cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
330125cb2221SBarry Smith 
3302d763cef2SBarry Smith .keywords: TS, timestep, solve
3303d763cef2SBarry Smith 
33049be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3305d763cef2SBarry Smith @*/
3306193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3307d763cef2SBarry Smith {
33082fb8446cSMatthew G. Knepley   DM               dm;
3309dfbe8321SBarry Smith   PetscErrorCode   ierr;
3310fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3311d763cef2SBarry Smith 
3312d763cef2SBarry Smith   PetscFunctionBegin;
33130700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3314fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3315fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3316fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3317fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3318fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3319fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3320302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3321fffbeea8SBarry Smith 
33222fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3323d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
332468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3325d405a339SMatthew Knepley 
3326a6772fa2SLisandro 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()");
3327a6772fa2SLisandro Dalcin 
3328362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
332924655328SShri   ts->ptime_prev = ts->ptime;
3330bbd56ea5SKarl Rupp 
3331e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3332d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3333193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3334d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3335bbd56ea5SKarl Rupp 
333624655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3337362cd11cSLisandro Dalcin 
3338362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3339cef5090cSJed Brown     if (ts->errorifstepfailed) {
334008c7845fSBarry 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]);
334108c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3342d2daff3dSHong Zhang     }
334308c7845fSBarry Smith   } else if (!ts->reason) {
334408c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
334508c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
334608c7845fSBarry Smith   }
33472c18e0fdSBarry Smith   ts->total_steps++;
33488392e04aSShri Abhyankar   ts->steprollback = PETSC_FALSE;
334908c7845fSBarry Smith   PetscFunctionReturn(0);
335008c7845fSBarry Smith }
335108c7845fSBarry Smith 
335208c7845fSBarry Smith #undef __FUNCT__
335308c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
335408c7845fSBarry Smith /*@
3355b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
335608c7845fSBarry Smith 
335708c7845fSBarry Smith    Collective on TS
335808c7845fSBarry Smith 
335908c7845fSBarry Smith    Input Parameter:
336008c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
336108c7845fSBarry Smith 
33626a4d4014SLisandro Dalcin    Level: intermediate
33636a4d4014SLisandro Dalcin 
3364b957a604SHong Zhang .keywords: TS, adjoint, step
33656a4d4014SLisandro Dalcin 
3366b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
33676a4d4014SLisandro Dalcin @*/
336808c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
33696a4d4014SLisandro Dalcin {
337008c7845fSBarry Smith   DM               dm;
33716a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
33726a4d4014SLisandro Dalcin 
33736a4d4014SLisandro Dalcin   PetscFunctionBegin;
33744a2ae208SSatish Balay   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
337508c7845fSBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
337608c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3377d763cef2SBarry Smith 
3378d763cef2SBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
337908c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
338008c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm,ts->steps,ts->ptime);CHKERRQ(ierr);
3381685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr);
3382d763cef2SBarry Smith 
33838d0ad7a8SHong Zhang   ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
338442f2b339SBarry 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);
338542f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
33868d0ad7a8SHong Zhang   ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr);
3387d763cef2SBarry Smith 
3388cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3389cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm,ts->steps,ts->ptime);CHKERRQ(ierr);
3390362cd11cSLisandro Dalcin 
3391362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3392cef5090cSJed Brown     if (ts->errorifstepfailed) {
33936c4ed002SBarry 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]);
33946c4ed002SBarry 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]);
33956c4ed002SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3396cef5090cSJed Brown     }
3397362cd11cSLisandro Dalcin   } else if (!ts->reason) {
339873b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
3399362cd11cSLisandro Dalcin   }
34002c18e0fdSBarry Smith   ts->total_steps--;
3401d763cef2SBarry Smith   PetscFunctionReturn(0);
3402d763cef2SBarry Smith }
3403d763cef2SBarry Smith 
3404d763cef2SBarry Smith #undef __FUNCT__
34057cbde773SLisandro Dalcin #define __FUNCT__ "TSEvaluateWLTE"
34067cbde773SLisandro Dalcin /*@
34077cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
34087cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
34097cbde773SLisandro Dalcin 
34107cbde773SLisandro Dalcin    Collective on TS
34117cbde773SLisandro Dalcin 
34127cbde773SLisandro Dalcin    Input Arguments:
34137cbde773SLisandro Dalcin +  ts - time stepping context
34147cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
34157cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
34167cbde773SLisandro Dalcin 
34177cbde773SLisandro Dalcin    Output Arguments:
34187cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
34197cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
34207cbde773SLisandro Dalcin 
34217cbde773SLisandro Dalcin    Level: advanced
34227cbde773SLisandro Dalcin 
34237cbde773SLisandro Dalcin    Notes:
34247cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
34257cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
34267cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
34277cbde773SLisandro Dalcin 
34287cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
34297cbde773SLisandro Dalcin @*/
34307cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
34317cbde773SLisandro Dalcin {
34327cbde773SLisandro Dalcin   PetscErrorCode ierr;
34337cbde773SLisandro Dalcin 
34347cbde773SLisandro Dalcin   PetscFunctionBegin;
34357cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34367cbde773SLisandro Dalcin   PetscValidType(ts,1);
34377cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
34387cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
34397cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
34407cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
34417cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
34427cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
34437cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
34447cbde773SLisandro Dalcin   PetscFunctionReturn(0);
34457cbde773SLisandro Dalcin }
34467cbde773SLisandro Dalcin 
34477cbde773SLisandro Dalcin #undef __FUNCT__
344805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
344905175c85SJed Brown /*@
345005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
345105175c85SJed Brown 
34521c3436cfSJed Brown    Collective on TS
345305175c85SJed Brown 
345405175c85SJed Brown    Input Arguments:
34551c3436cfSJed Brown +  ts - time stepping context
34561c3436cfSJed Brown .  order - desired order of accuracy
34570298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
345805175c85SJed Brown 
345905175c85SJed Brown    Output Arguments:
34600910c330SBarry Smith .  U - state at the end of the current step
346105175c85SJed Brown 
346205175c85SJed Brown    Level: advanced
346305175c85SJed Brown 
3464108c343cSJed Brown    Notes:
3465108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3466108c343cSJed 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.
3467108c343cSJed Brown 
34681c3436cfSJed Brown .seealso: TSStep(), TSAdapt
346905175c85SJed Brown @*/
34700910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
347105175c85SJed Brown {
347205175c85SJed Brown   PetscErrorCode ierr;
347305175c85SJed Brown 
347405175c85SJed Brown   PetscFunctionBegin;
347505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
347605175c85SJed Brown   PetscValidType(ts,1);
34770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3478ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
34790910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
348005175c85SJed Brown   PetscFunctionReturn(0);
348105175c85SJed Brown }
348205175c85SJed Brown 
3483b1cb36f3SHong Zhang #undef __FUNCT__
3484b1cb36f3SHong Zhang #define __FUNCT__ "TSForwardCostIntegral"
3485b1cb36f3SHong Zhang /*@
3486b1cb36f3SHong Zhang  TSForwardCostIntegral - Evaluate the cost integral in the forward run.
3487b1cb36f3SHong Zhang 
3488b1cb36f3SHong Zhang  Collective on TS
3489b1cb36f3SHong Zhang 
3490b1cb36f3SHong Zhang  Input Arguments:
3491b1cb36f3SHong Zhang  .  ts - time stepping context
3492b1cb36f3SHong Zhang 
3493b1cb36f3SHong Zhang  Level: advanced
3494b1cb36f3SHong Zhang 
3495b1cb36f3SHong Zhang  Notes:
3496b1cb36f3SHong Zhang  This function cannot be called until TSStep() has been completed.
3497b1cb36f3SHong Zhang 
3498b1cb36f3SHong Zhang  .seealso: TSSolve(), TSAdjointCostIntegral()
3499b1cb36f3SHong Zhang  @*/
3500b1cb36f3SHong Zhang PetscErrorCode TSForwardCostIntegral(TS ts)
3501b1cb36f3SHong Zhang {
3502b1cb36f3SHong Zhang     PetscErrorCode ierr;
3503b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3504b1cb36f3SHong 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);
3505b1cb36f3SHong Zhang     ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr);
3506b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3507b1cb36f3SHong Zhang }
3508d67e68b7SBarry Smith 
350905175c85SJed Brown #undef __FUNCT__
3510d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
3511d763cef2SBarry Smith /*@
3512d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3513d763cef2SBarry Smith 
3514d763cef2SBarry Smith    Collective on TS
3515d763cef2SBarry Smith 
3516d763cef2SBarry Smith    Input Parameter:
3517d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
351863e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
351963e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
35205a3a76d0SJed Brown 
3521d763cef2SBarry Smith    Level: beginner
3522d763cef2SBarry Smith 
35235a3a76d0SJed Brown    Notes:
35245a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
35255a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
35265a3a76d0SJed Brown    stepped over the final time.
35275a3a76d0SJed Brown 
3528d763cef2SBarry Smith .keywords: TS, timestep, solve
3529d763cef2SBarry Smith 
353063e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
3531d763cef2SBarry Smith @*/
3532cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
3533d763cef2SBarry Smith {
3534b06615a5SLisandro Dalcin   Vec               solution;
3535d763cef2SBarry Smith   PetscErrorCode    ierr;
3536d763cef2SBarry Smith 
3537d763cef2SBarry Smith   PetscFunctionBegin;
3538d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3539f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3540feed9e9dSBarry Smith 
354149354f04SShri 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 */
3542b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
35430910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3544b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3545b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3546b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
35475a3a76d0SJed Brown     }
35480910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3549bbd56ea5SKarl Rupp   } else if (u) {
35500910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
35515a3a76d0SJed Brown   }
3552b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
355368bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3554a6772fa2SLisandro Dalcin 
3555a6772fa2SLisandro 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()");
3556a6772fa2SLisandro 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");
3557a6772fa2SLisandro Dalcin 
3558d763cef2SBarry Smith   /* reset time step and iteration counters */
3559193ac0bcSJed Brown   ts->steps             = 0;
35605ef26d82SJed Brown   ts->ksp_its           = 0;
35615ef26d82SJed Brown   ts->snes_its          = 0;
3562c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3563c610991cSLisandro Dalcin   ts->reject            = 0;
3564193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3565193ac0bcSJed Brown 
3566ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3567f05ece33SBarry Smith 
3568193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3569193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3570a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3571cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3572a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3573193ac0bcSJed Brown   } else {
3574d763cef2SBarry Smith     /* steps the requested number of timesteps. */
3575db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3576db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3577cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
35786427ac75SLisandro Dalcin     ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
35796427ac75SLisandro Dalcin 
3580e1a7a14fSJed Brown     while (!ts->reason) {
3581193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
35829687d888SLisandro Dalcin       if (!ts->steprollback) {
35839687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
35849687d888SLisandro Dalcin       }
3585193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
35869687d888SLisandro Dalcin       if (ts->vec_costintegral && ts->costintegralfwd) {
3587b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3588b1cb36f3SHong Zhang       }
35896427ac75SLisandro Dalcin       ierr = TSEventHandler(ts);CHKERRQ(ierr);
35908392e04aSShri Abhyankar       if (!ts->steprollback) {
3591cdf0de3dSShri Abhyankar         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
35921eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3593aeb4809dSShri Abhyankar       }
3594193ac0bcSJed Brown     }
359563e21af5SBarry Smith     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
35966427ac75SLisandro Dalcin 
359749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
35980910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3599cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3600b06615a5SLisandro Dalcin       solution = u;
360163e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
36020574a7fbSJed Brown     } else {
3603ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3604cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3605b06615a5SLisandro Dalcin       solution = ts->vec_sol;
36060574a7fbSJed Brown     }
3607193ac0bcSJed Brown   }
3608d2daff3dSHong Zhang 
3609ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
361063e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
361156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3612ef222394SBarry Smith   if (ts->adjoint_solve) {
3613ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
36142b0a91c0SBarry Smith   }
3615d763cef2SBarry Smith   PetscFunctionReturn(0);
3616d763cef2SBarry Smith }
3617d763cef2SBarry Smith 
3618d763cef2SBarry Smith #undef __FUNCT__
3619b1cb36f3SHong Zhang #define __FUNCT__ "TSAdjointCostIntegral"
3620b1cb36f3SHong Zhang /*@
3621b1cb36f3SHong Zhang  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.
3622b1cb36f3SHong Zhang 
3623b1cb36f3SHong Zhang  Collective on TS
3624b1cb36f3SHong Zhang 
3625b1cb36f3SHong Zhang  Input Arguments:
3626b1cb36f3SHong Zhang  .  ts - time stepping context
3627b1cb36f3SHong Zhang 
3628b1cb36f3SHong Zhang  Level: advanced
3629b1cb36f3SHong Zhang 
3630b1cb36f3SHong Zhang  Notes:
3631b1cb36f3SHong Zhang  This function cannot be called until TSAdjointStep() has been completed.
3632b1cb36f3SHong Zhang 
3633b1cb36f3SHong Zhang  .seealso: TSAdjointSolve(), TSAdjointStep
3634b1cb36f3SHong Zhang  @*/
3635b1cb36f3SHong Zhang PetscErrorCode TSAdjointCostIntegral(TS ts)
3636b1cb36f3SHong Zhang {
3637b1cb36f3SHong Zhang     PetscErrorCode ierr;
3638b1cb36f3SHong Zhang     PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3639b1cb36f3SHong 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);
3640b1cb36f3SHong Zhang     ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr);
3641b1cb36f3SHong Zhang     PetscFunctionReturn(0);
3642b1cb36f3SHong Zhang }
3643b1cb36f3SHong Zhang 
3644b1cb36f3SHong Zhang #undef __FUNCT__
3645c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3646c88f2d44SBarry Smith /*@
3647c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3648c88f2d44SBarry Smith 
3649c88f2d44SBarry Smith    Collective on TS
3650c88f2d44SBarry Smith 
3651c88f2d44SBarry Smith    Input Parameter:
36522c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3653c88f2d44SBarry Smith 
3654e87fd094SBarry Smith    Options Database:
3655e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
3656e87fd094SBarry Smith 
3657c88f2d44SBarry Smith    Level: intermediate
3658c88f2d44SBarry Smith 
3659c88f2d44SBarry Smith    Notes:
3660c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3661c88f2d44SBarry Smith 
366273b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
366373b18844SBarry Smith 
3664c88f2d44SBarry Smith .keywords: TS, timestep, solve
3665c88f2d44SBarry Smith 
3666b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
3667c88f2d44SBarry Smith @*/
36682c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3669c88f2d44SBarry Smith {
3670c88f2d44SBarry Smith   PetscErrorCode    ierr;
3671c88f2d44SBarry Smith 
3672c88f2d44SBarry Smith   PetscFunctionBegin;
3673c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3674c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
367568bece0bSHong Zhang 
3676c88f2d44SBarry Smith   /* reset time step and iteration counters */
3677c88f2d44SBarry Smith   ts->steps             = 0;
3678c88f2d44SBarry Smith   ts->ksp_its           = 0;
3679c88f2d44SBarry Smith   ts->snes_its          = 0;
3680c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3681c88f2d44SBarry Smith   ts->reject            = 0;
3682c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3683c88f2d44SBarry Smith 
368473b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3685c88f2d44SBarry Smith 
368673b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3687c88f2d44SBarry Smith   while (!ts->reason) {
368855c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
36899110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
36906427ac75SLisandro Dalcin     ierr = TSAdjointEventHandler(ts);CHKERRQ(ierr);
3691616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3692b1cb36f3SHong Zhang     if (ts->vec_costintegral && !ts->costintegralfwd) {
3693b1cb36f3SHong Zhang       ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr);
3694b1cb36f3SHong Zhang     }
3695c88f2d44SBarry Smith   }
369626656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
369726656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
3698c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3699e210cd0eSHong Zhang   ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");CHKERRQ(ierr);
3700685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
3701c88f2d44SBarry Smith   PetscFunctionReturn(0);
3702c88f2d44SBarry Smith }
3703c88f2d44SBarry Smith 
3704c88f2d44SBarry Smith #undef __FUNCT__
3705d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
37067db568b7SBarry Smith /*@C
3707228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3708228d79bcSJed Brown 
3709228d79bcSJed Brown    Collective on TS
3710228d79bcSJed Brown 
3711228d79bcSJed Brown    Input Parameters:
3712228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3713228d79bcSJed Brown .  step - step number that has just completed
3714228d79bcSJed Brown .  ptime - model time of the state
37150910c330SBarry Smith -  u - state at the current model time
3716228d79bcSJed Brown 
3717228d79bcSJed Brown    Notes:
37187db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
37197db568b7SBarry Smith    Users would almost never call this routine directly.
3720228d79bcSJed Brown 
372163e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
372263e21af5SBarry Smith 
37237db568b7SBarry Smith    Level: developer
3724228d79bcSJed Brown 
3725228d79bcSJed Brown .keywords: TS, timestep
3726228d79bcSJed Brown @*/
37270910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3728d763cef2SBarry Smith {
3729d6152f81SLisandro Dalcin   DM             dm;
3730a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3731d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3732d763cef2SBarry Smith 
3733d763cef2SBarry Smith   PetscFunctionBegin;
3734b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3735b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3736d6152f81SLisandro Dalcin 
3737d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3738d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3739d6152f81SLisandro Dalcin 
37405edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3741d763cef2SBarry Smith   for (i=0; i<n; i++) {
37420910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3743d763cef2SBarry Smith   }
37445edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3745d763cef2SBarry Smith   PetscFunctionReturn(0);
3746d763cef2SBarry Smith }
3747d763cef2SBarry Smith 
37489110b2e7SHong Zhang #undef __FUNCT__
37499110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor"
37507db568b7SBarry Smith /*@C
37519110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
37529110b2e7SHong Zhang 
37539110b2e7SHong Zhang    Collective on TS
37549110b2e7SHong Zhang 
37559110b2e7SHong Zhang    Input Parameters:
37569110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
37579110b2e7SHong Zhang .  step - step number that has just completed
37589110b2e7SHong Zhang .  ptime - model time of the state
37599110b2e7SHong Zhang .  u - state at the current model time
37609110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
37619110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
37629110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
37639110b2e7SHong Zhang 
37649110b2e7SHong Zhang    Notes:
37657db568b7SBarry Smith    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
37667db568b7SBarry Smith    Users would almost never call this routine directly.
37679110b2e7SHong Zhang 
37687db568b7SBarry Smith    Level: developer
37699110b2e7SHong Zhang 
37709110b2e7SHong Zhang .keywords: TS, timestep
37719110b2e7SHong Zhang @*/
37729110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
37739110b2e7SHong Zhang {
37749110b2e7SHong Zhang   PetscErrorCode ierr;
37759110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
37769110b2e7SHong Zhang 
37779110b2e7SHong Zhang   PetscFunctionBegin;
37789110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37799110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
37809110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
37819110b2e7SHong Zhang   for (i=0; i<n; i++) {
37829110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
37839110b2e7SHong Zhang   }
37849110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
37859110b2e7SHong Zhang   PetscFunctionReturn(0);
37869110b2e7SHong Zhang }
37879110b2e7SHong Zhang 
3788d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
37894a2ae208SSatish Balay #undef __FUNCT__
3790a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3791d763cef2SBarry Smith /*@C
37927db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3793a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3794d763cef2SBarry Smith 
3795d763cef2SBarry Smith    Collective on TS
3796d763cef2SBarry Smith 
3797d763cef2SBarry Smith    Input Parameters:
3798d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3799d763cef2SBarry Smith .  label - the title to put in the title bar
38007c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3801a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3802a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3803d763cef2SBarry Smith 
3804d763cef2SBarry Smith    Output Parameter:
38050b039ecaSBarry Smith .  ctx - the context
3806d763cef2SBarry Smith 
3807d763cef2SBarry Smith    Options Database Key:
38084f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
38097db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
38107db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
38117db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
38127db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3813b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3814d763cef2SBarry Smith 
3815d763cef2SBarry Smith    Notes:
3816a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3817d763cef2SBarry Smith 
38187db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
38197db568b7SBarry Smith 
38207db568b7SBarry 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
38217db568b7SBarry 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
38227db568b7SBarry Smith    as the first argument.
38237db568b7SBarry Smith 
38247db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
38257db568b7SBarry Smith 
38267db568b7SBarry Smith 
3827d763cef2SBarry Smith    Level: intermediate
3828d763cef2SBarry Smith 
38297db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3830d763cef2SBarry Smith 
38317db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
38327db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
38337db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
38347db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
38357db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
38367c922b88SBarry Smith 
3837d763cef2SBarry Smith @*/
3838a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3839d763cef2SBarry Smith {
38407f52daa2SLisandro Dalcin   PetscDraw      draw;
3841dfbe8321SBarry Smith   PetscErrorCode ierr;
3842d763cef2SBarry Smith 
3843d763cef2SBarry Smith   PetscFunctionBegin;
3844b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
38457f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
38467f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
38477f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3848287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
38497f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3850a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3851d763cef2SBarry Smith   PetscFunctionReturn(0);
3852d763cef2SBarry Smith }
3853d763cef2SBarry Smith 
38544a2ae208SSatish Balay #undef __FUNCT__
38554f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3856b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3857d763cef2SBarry Smith {
38580b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3859c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3860dfbe8321SBarry Smith   PetscErrorCode ierr;
3861d763cef2SBarry Smith 
3862d763cef2SBarry Smith   PetscFunctionBegin;
386363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
3864b06615a5SLisandro Dalcin   if (!step) {
3865a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3866a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
38676934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time Step");CHKERRQ(ierr);
3868a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3869a9f9c1f6SBarry Smith   }
3870c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
38710b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3872b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
38730b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
38746934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
38753923b477SBarry Smith   }
3876d763cef2SBarry Smith   PetscFunctionReturn(0);
3877d763cef2SBarry Smith }
3878d763cef2SBarry Smith 
38794a2ae208SSatish Balay #undef __FUNCT__
3880a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3881d763cef2SBarry Smith /*@C
3882a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3883a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3884d763cef2SBarry Smith 
38850b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3886d763cef2SBarry Smith 
3887d763cef2SBarry Smith    Input Parameter:
38880b039ecaSBarry Smith .  ctx - the monitor context
3889d763cef2SBarry Smith 
3890d763cef2SBarry Smith    Level: intermediate
3891d763cef2SBarry Smith 
3892d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3893d763cef2SBarry Smith 
38944f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3895d763cef2SBarry Smith @*/
3896a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3897d763cef2SBarry Smith {
3898dfbe8321SBarry Smith   PetscErrorCode ierr;
3899d763cef2SBarry Smith 
3900d763cef2SBarry Smith   PetscFunctionBegin;
39017684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
39027684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
39037684fa3eSBarry Smith   }
39040b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
390531152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3906387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3907387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3908387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
39090b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3910d763cef2SBarry Smith   PetscFunctionReturn(0);
3911d763cef2SBarry Smith }
3912d763cef2SBarry Smith 
39134a2ae208SSatish Balay #undef __FUNCT__
39144a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3915d763cef2SBarry Smith /*@
3916b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3917d763cef2SBarry Smith 
3918d763cef2SBarry Smith    Not Collective
3919d763cef2SBarry Smith 
3920d763cef2SBarry Smith    Input Parameter:
3921d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3922d763cef2SBarry Smith 
3923d763cef2SBarry Smith    Output Parameter:
392463e21af5SBarry Smith .  t  - the current time. This time may not corresponds to the final time set with TSSetDuration(), use TSGetSolveTime().
3925d763cef2SBarry Smith 
3926d763cef2SBarry Smith    Level: beginner
3927d763cef2SBarry Smith 
3928b8123daeSJed Brown    Note:
3929b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
39309be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3931b8123daeSJed Brown 
393263e21af5SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep(), TSGetSolveTime()
3933d763cef2SBarry Smith 
3934d763cef2SBarry Smith .keywords: TS, get, time
3935d763cef2SBarry Smith @*/
39367087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3937d763cef2SBarry Smith {
3938d763cef2SBarry Smith   PetscFunctionBegin;
39390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3940f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3941d763cef2SBarry Smith   *t = ts->ptime;
3942d763cef2SBarry Smith   PetscFunctionReturn(0);
3943d763cef2SBarry Smith }
3944d763cef2SBarry Smith 
39454a2ae208SSatish Balay #undef __FUNCT__
3946e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3947e5e524a1SHong Zhang /*@
3948e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3949e5e524a1SHong Zhang 
3950e5e524a1SHong Zhang    Not Collective
3951e5e524a1SHong Zhang 
3952e5e524a1SHong Zhang    Input Parameter:
3953e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3954e5e524a1SHong Zhang 
3955e5e524a1SHong Zhang    Output Parameter:
3956e5e524a1SHong Zhang .  t  - the previous time
3957e5e524a1SHong Zhang 
3958e5e524a1SHong Zhang    Level: beginner
3959e5e524a1SHong Zhang 
3960e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3961e5e524a1SHong Zhang 
3962e5e524a1SHong Zhang .keywords: TS, get, time
3963e5e524a1SHong Zhang @*/
3964e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3965e5e524a1SHong Zhang {
3966e5e524a1SHong Zhang   PetscFunctionBegin;
3967e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3968e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3969e5e524a1SHong Zhang   *t = ts->ptime_prev;
3970e5e524a1SHong Zhang   PetscFunctionReturn(0);
3971e5e524a1SHong Zhang }
3972e5e524a1SHong Zhang 
3973e5e524a1SHong Zhang #undef __FUNCT__
39746a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
39756a4d4014SLisandro Dalcin /*@
39766a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
39776a4d4014SLisandro Dalcin 
39783f9fe445SBarry Smith    Logically Collective on TS
39796a4d4014SLisandro Dalcin 
39806a4d4014SLisandro Dalcin    Input Parameters:
39816a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
39826a4d4014SLisandro Dalcin -  time - the time
39836a4d4014SLisandro Dalcin 
39846a4d4014SLisandro Dalcin    Level: intermediate
39856a4d4014SLisandro Dalcin 
39866a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
39876a4d4014SLisandro Dalcin 
39886a4d4014SLisandro Dalcin .keywords: TS, set, time
39896a4d4014SLisandro Dalcin @*/
39907087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
39916a4d4014SLisandro Dalcin {
39926a4d4014SLisandro Dalcin   PetscFunctionBegin;
39930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3994c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
39956a4d4014SLisandro Dalcin   ts->ptime = t;
39966a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
39976a4d4014SLisandro Dalcin }
39986a4d4014SLisandro Dalcin 
39996a4d4014SLisandro Dalcin #undef __FUNCT__
40004a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
4001d763cef2SBarry Smith /*@C
4002d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4003d763cef2SBarry Smith    TS options in the database.
4004d763cef2SBarry Smith 
40053f9fe445SBarry Smith    Logically Collective on TS
4006d763cef2SBarry Smith 
4007d763cef2SBarry Smith    Input Parameter:
4008d763cef2SBarry Smith +  ts     - The TS context
4009d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4010d763cef2SBarry Smith 
4011d763cef2SBarry Smith    Notes:
4012d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4013d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4014d763cef2SBarry Smith    hyphen.
4015d763cef2SBarry Smith 
4016d763cef2SBarry Smith    Level: advanced
4017d763cef2SBarry Smith 
4018d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4019d763cef2SBarry Smith 
4020d763cef2SBarry Smith .seealso: TSSetFromOptions()
4021d763cef2SBarry Smith 
4022d763cef2SBarry Smith @*/
40237087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4024d763cef2SBarry Smith {
4025dfbe8321SBarry Smith   PetscErrorCode ierr;
4026089b2837SJed Brown   SNES           snes;
4027d763cef2SBarry Smith 
4028d763cef2SBarry Smith   PetscFunctionBegin;
40290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4030d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4031089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4032089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4033d763cef2SBarry Smith   PetscFunctionReturn(0);
4034d763cef2SBarry Smith }
4035d763cef2SBarry Smith 
4036d763cef2SBarry Smith 
40374a2ae208SSatish Balay #undef __FUNCT__
40384a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
4039d763cef2SBarry Smith /*@C
4040d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4041d763cef2SBarry Smith    TS options in the database.
4042d763cef2SBarry Smith 
40433f9fe445SBarry Smith    Logically Collective on TS
4044d763cef2SBarry Smith 
4045d763cef2SBarry Smith    Input Parameter:
4046d763cef2SBarry Smith +  ts     - The TS context
4047d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4048d763cef2SBarry Smith 
4049d763cef2SBarry Smith    Notes:
4050d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4051d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4052d763cef2SBarry Smith    hyphen.
4053d763cef2SBarry Smith 
4054d763cef2SBarry Smith    Level: advanced
4055d763cef2SBarry Smith 
4056d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4057d763cef2SBarry Smith 
4058d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4059d763cef2SBarry Smith 
4060d763cef2SBarry Smith @*/
40617087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4062d763cef2SBarry Smith {
4063dfbe8321SBarry Smith   PetscErrorCode ierr;
4064089b2837SJed Brown   SNES           snes;
4065d763cef2SBarry Smith 
4066d763cef2SBarry Smith   PetscFunctionBegin;
40670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4068d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4069089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4070089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4071d763cef2SBarry Smith   PetscFunctionReturn(0);
4072d763cef2SBarry Smith }
4073d763cef2SBarry Smith 
40744a2ae208SSatish Balay #undef __FUNCT__
40754a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
4076d763cef2SBarry Smith /*@C
4077d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4078d763cef2SBarry Smith    TS options in the database.
4079d763cef2SBarry Smith 
4080d763cef2SBarry Smith    Not Collective
4081d763cef2SBarry Smith 
4082d763cef2SBarry Smith    Input Parameter:
4083d763cef2SBarry Smith .  ts - The TS context
4084d763cef2SBarry Smith 
4085d763cef2SBarry Smith    Output Parameter:
4086d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4087d763cef2SBarry Smith 
4088d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
4089d763cef2SBarry Smith    sufficient length to hold the prefix.
4090d763cef2SBarry Smith 
4091d763cef2SBarry Smith    Level: intermediate
4092d763cef2SBarry Smith 
4093d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4094d763cef2SBarry Smith 
4095d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4096d763cef2SBarry Smith @*/
40977087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4098d763cef2SBarry Smith {
4099dfbe8321SBarry Smith   PetscErrorCode ierr;
4100d763cef2SBarry Smith 
4101d763cef2SBarry Smith   PetscFunctionBegin;
41020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41034482741eSBarry Smith   PetscValidPointer(prefix,2);
4104d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4105d763cef2SBarry Smith   PetscFunctionReturn(0);
4106d763cef2SBarry Smith }
4107d763cef2SBarry Smith 
41084a2ae208SSatish Balay #undef __FUNCT__
41094a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
4110d763cef2SBarry Smith /*@C
4111d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4112d763cef2SBarry Smith 
4113d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4114d763cef2SBarry Smith 
4115d763cef2SBarry Smith    Input Parameter:
4116d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4117d763cef2SBarry Smith 
4118d763cef2SBarry Smith    Output Parameters:
4119e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4120e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4121e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4122e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4123d763cef2SBarry Smith 
41240298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
4125d763cef2SBarry Smith 
4126d763cef2SBarry Smith    Level: intermediate
4127d763cef2SBarry Smith 
412826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
4129d763cef2SBarry Smith 
4130d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4131d763cef2SBarry Smith @*/
4132e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4133d763cef2SBarry Smith {
4134089b2837SJed Brown   PetscErrorCode ierr;
4135089b2837SJed Brown   SNES           snes;
413624989b8cSPeter Brune   DM             dm;
4137089b2837SJed Brown 
4138d763cef2SBarry Smith   PetscFunctionBegin;
4139089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4140e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
414124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
414224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4143d763cef2SBarry Smith   PetscFunctionReturn(0);
4144d763cef2SBarry Smith }
4145d763cef2SBarry Smith 
41461713a123SBarry Smith #undef __FUNCT__
41472eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
41482eca1d9cSJed Brown /*@C
41492eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
41502eca1d9cSJed Brown 
41512eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
41522eca1d9cSJed Brown 
41532eca1d9cSJed Brown    Input Parameter:
41542eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
41552eca1d9cSJed Brown 
41562eca1d9cSJed Brown    Output Parameters:
4157e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4158e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
41592eca1d9cSJed Brown .  f   - The function to compute the matrices
41602eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
41612eca1d9cSJed Brown 
41620298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
41632eca1d9cSJed Brown 
41642eca1d9cSJed Brown    Level: advanced
41652eca1d9cSJed Brown 
41662eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
41672eca1d9cSJed Brown 
41682eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
41692eca1d9cSJed Brown @*/
4170e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
41712eca1d9cSJed Brown {
4172089b2837SJed Brown   PetscErrorCode ierr;
4173089b2837SJed Brown   SNES           snes;
417424989b8cSPeter Brune   DM             dm;
41750910c330SBarry Smith 
41762eca1d9cSJed Brown   PetscFunctionBegin;
4177089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4178f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4179e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
418024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
418124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
41822eca1d9cSJed Brown   PetscFunctionReturn(0);
41832eca1d9cSJed Brown }
41842eca1d9cSJed Brown 
41856083293cSBarry Smith 
41862eca1d9cSJed Brown #undef __FUNCT__
418783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
41881713a123SBarry Smith /*@C
418983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
41901713a123SBarry Smith    VecView() for the solution at each timestep
41911713a123SBarry Smith 
41921713a123SBarry Smith    Collective on TS
41931713a123SBarry Smith 
41941713a123SBarry Smith    Input Parameters:
41951713a123SBarry Smith +  ts - the TS context
41961713a123SBarry Smith .  step - current time-step
4197142b95e3SSatish Balay .  ptime - current time
41980298fd71SBarry Smith -  dummy - either a viewer or NULL
41991713a123SBarry Smith 
420099fdda47SBarry Smith    Options Database:
420199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
420299fdda47SBarry Smith 
4203387f4636SBarry 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
420499fdda47SBarry Smith        will look bad
420599fdda47SBarry Smith 
42061713a123SBarry Smith    Level: intermediate
42071713a123SBarry Smith 
42081713a123SBarry Smith .keywords: TS,  vector, monitor, view
42091713a123SBarry Smith 
4210a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42111713a123SBarry Smith @*/
42120910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42131713a123SBarry Smith {
4214dfbe8321SBarry Smith   PetscErrorCode   ierr;
421583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4216473a3ab2SBarry Smith   PetscDraw        draw;
42171713a123SBarry Smith 
42181713a123SBarry Smith   PetscFunctionBegin;
42196083293cSBarry Smith   if (!step && ictx->showinitial) {
42206083293cSBarry Smith     if (!ictx->initialsolution) {
42210910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
42221713a123SBarry Smith     }
42230910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
42246083293cSBarry Smith   }
4225b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42260dcf80beSBarry Smith 
42276083293cSBarry Smith   if (ictx->showinitial) {
42286083293cSBarry Smith     PetscReal pause;
42296083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
42306083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
42316083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
42326083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
42336083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
42346083293cSBarry Smith   }
42350910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4236473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
423751fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4238473a3ab2SBarry Smith     char      time[32];
4239473a3ab2SBarry Smith 
4240473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
424185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4242473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4243473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
424451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4245473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4246473a3ab2SBarry Smith   }
4247473a3ab2SBarry Smith 
42486083293cSBarry Smith   if (ictx->showinitial) {
42496083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
42506083293cSBarry Smith   }
42511713a123SBarry Smith   PetscFunctionReturn(0);
42521713a123SBarry Smith }
42531713a123SBarry Smith 
42542d5ee99bSBarry Smith #undef __FUNCT__
42559110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi"
42569110b2e7SHong Zhang /*@C
42579110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
42589110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
42599110b2e7SHong Zhang 
42609110b2e7SHong Zhang    Collective on TS
42619110b2e7SHong Zhang 
42629110b2e7SHong Zhang    Input Parameters:
42639110b2e7SHong Zhang +  ts - the TS context
42649110b2e7SHong Zhang .  step - current time-step
42659110b2e7SHong Zhang .  ptime - current time
42669110b2e7SHong Zhang .  u - current state
42679110b2e7SHong Zhang .  numcost - number of cost functions
42689110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
42699110b2e7SHong Zhang .  mu - sensitivities to parameters
42709110b2e7SHong Zhang -  dummy - either a viewer or NULL
42719110b2e7SHong Zhang 
42729110b2e7SHong Zhang    Level: intermediate
42739110b2e7SHong Zhang 
42749110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
42759110b2e7SHong Zhang 
42769110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
42779110b2e7SHong Zhang @*/
42789110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
42799110b2e7SHong Zhang {
42809110b2e7SHong Zhang   PetscErrorCode   ierr;
42819110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
42829110b2e7SHong Zhang   PetscDraw        draw;
4283a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4284a0046e2cSSatish Balay   char             time[32];
42859110b2e7SHong Zhang 
42869110b2e7SHong Zhang   PetscFunctionBegin;
42879110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42889110b2e7SHong Zhang 
42899110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
42909110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
42919110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
42929110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
42939110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
42949110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
42959110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
42969110b2e7SHong Zhang   PetscFunctionReturn(0);
42979110b2e7SHong Zhang }
42989110b2e7SHong Zhang 
42999110b2e7SHong Zhang #undef __FUNCT__
43002d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
43012d5ee99bSBarry Smith /*@C
43022d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43032d5ee99bSBarry Smith 
43042d5ee99bSBarry Smith    Collective on TS
43052d5ee99bSBarry Smith 
43062d5ee99bSBarry Smith    Input Parameters:
43072d5ee99bSBarry Smith +  ts - the TS context
43082d5ee99bSBarry Smith .  step - current time-step
43092d5ee99bSBarry Smith .  ptime - current time
43102d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43112d5ee99bSBarry Smith 
43122d5ee99bSBarry Smith    Level: intermediate
43132d5ee99bSBarry Smith 
43142d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
43152d5ee99bSBarry Smith 
43162d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43172d5ee99bSBarry Smith @*/
43182d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43192d5ee99bSBarry Smith {
43202d5ee99bSBarry Smith   PetscErrorCode    ierr;
43212d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
43222d5ee99bSBarry Smith   PetscDraw         draw;
43236934998bSLisandro Dalcin   PetscDrawAxis     axis;
43242d5ee99bSBarry Smith   PetscInt          n;
43252d5ee99bSBarry Smith   PetscMPIInt       size;
43266934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
43272d5ee99bSBarry Smith   char              time[32];
43282d5ee99bSBarry Smith   const PetscScalar *U;
43292d5ee99bSBarry Smith 
43302d5ee99bSBarry Smith   PetscFunctionBegin;
43316934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
43326934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
43332d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
43346934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
43352d5ee99bSBarry Smith 
43362d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43376934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
43386934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
43396934998bSLisandro Dalcin   if (!step) {
43406934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
43416934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
43426934998bSLisandro Dalcin   }
43432d5ee99bSBarry Smith 
43442d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
43456934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
43466934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4347a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
43486934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
43492d5ee99bSBarry Smith 
43506934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
43516934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
43522d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
43534b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
435485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43552d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
435651fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43572d5ee99bSBarry Smith   }
43586934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
43592d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
43606934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
43612d5ee99bSBarry Smith   PetscFunctionReturn(0);
43622d5ee99bSBarry Smith }
43632d5ee99bSBarry Smith 
43641713a123SBarry Smith 
43656c699258SBarry Smith #undef __FUNCT__
436683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
43676083293cSBarry Smith /*@C
436883a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
43696083293cSBarry Smith 
43706083293cSBarry Smith    Collective on TS
43716083293cSBarry Smith 
43726083293cSBarry Smith    Input Parameters:
43736083293cSBarry Smith .    ctx - the monitor context
43746083293cSBarry Smith 
43756083293cSBarry Smith    Level: intermediate
43766083293cSBarry Smith 
43776083293cSBarry Smith .keywords: TS,  vector, monitor, view
43786083293cSBarry Smith 
437983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
43806083293cSBarry Smith @*/
438183a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
43826083293cSBarry Smith {
43836083293cSBarry Smith   PetscErrorCode ierr;
43846083293cSBarry Smith 
43856083293cSBarry Smith   PetscFunctionBegin;
438683a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
438783a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
438883a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
43896083293cSBarry Smith   PetscFunctionReturn(0);
43906083293cSBarry Smith }
43916083293cSBarry Smith 
43926083293cSBarry Smith #undef __FUNCT__
439383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
43946083293cSBarry Smith /*@C
439583a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
43966083293cSBarry Smith 
43976083293cSBarry Smith    Collective on TS
43986083293cSBarry Smith 
43996083293cSBarry Smith    Input Parameter:
44006083293cSBarry Smith .    ts - time-step context
44016083293cSBarry Smith 
44026083293cSBarry Smith    Output Patameter:
44036083293cSBarry Smith .    ctx - the monitor context
44046083293cSBarry Smith 
440599fdda47SBarry Smith    Options Database:
440699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
440799fdda47SBarry Smith 
44086083293cSBarry Smith    Level: intermediate
44096083293cSBarry Smith 
44106083293cSBarry Smith .keywords: TS,  vector, monitor, view
44116083293cSBarry Smith 
441283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44136083293cSBarry Smith @*/
441483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44156083293cSBarry Smith {
44166083293cSBarry Smith   PetscErrorCode   ierr;
44176083293cSBarry Smith 
44186083293cSBarry Smith   PetscFunctionBegin;
4419b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
442083a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4421e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4422bbd56ea5SKarl Rupp 
442383a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4424473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4425c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4426473a3ab2SBarry Smith 
4427473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4428c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
44296083293cSBarry Smith   PetscFunctionReturn(0);
44306083293cSBarry Smith }
44316083293cSBarry Smith 
44326083293cSBarry Smith #undef __FUNCT__
443383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
44343a471f94SBarry Smith /*@C
443583a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
44363a471f94SBarry Smith    VecView() for the error at each timestep
44373a471f94SBarry Smith 
44383a471f94SBarry Smith    Collective on TS
44393a471f94SBarry Smith 
44403a471f94SBarry Smith    Input Parameters:
44413a471f94SBarry Smith +  ts - the TS context
44423a471f94SBarry Smith .  step - current time-step
44433a471f94SBarry Smith .  ptime - current time
44440298fd71SBarry Smith -  dummy - either a viewer or NULL
44453a471f94SBarry Smith 
44463a471f94SBarry Smith    Level: intermediate
44473a471f94SBarry Smith 
44483a471f94SBarry Smith .keywords: TS,  vector, monitor, view
44493a471f94SBarry Smith 
44503a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44513a471f94SBarry Smith @*/
44520910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44533a471f94SBarry Smith {
44543a471f94SBarry Smith   PetscErrorCode   ierr;
445583a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
445683a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
44573a471f94SBarry Smith   Vec              work;
44583a471f94SBarry Smith 
44593a471f94SBarry Smith   PetscFunctionBegin;
4460b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44610910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44623a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
44630910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
44643a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
44653a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
44663a471f94SBarry Smith   PetscFunctionReturn(0);
44673a471f94SBarry Smith }
44683a471f94SBarry Smith 
4469af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
44703a471f94SBarry Smith #undef __FUNCT__
44716c699258SBarry Smith #define __FUNCT__ "TSSetDM"
44726c699258SBarry Smith /*@
44736c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
44746c699258SBarry Smith 
44753f9fe445SBarry Smith    Logically Collective on TS and DM
44766c699258SBarry Smith 
44776c699258SBarry Smith    Input Parameters:
44786c699258SBarry Smith +  ts - the preconditioner context
44796c699258SBarry Smith -  dm - the dm
44806c699258SBarry Smith 
44816c699258SBarry Smith    Level: intermediate
44826c699258SBarry Smith 
44836c699258SBarry Smith 
44846c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
44856c699258SBarry Smith @*/
44867087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
44876c699258SBarry Smith {
44886c699258SBarry Smith   PetscErrorCode ierr;
4489089b2837SJed Brown   SNES           snes;
4490942e3340SBarry Smith   DMTS           tsdm;
44916c699258SBarry Smith 
44926c699258SBarry Smith   PetscFunctionBegin;
44930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
449470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4495942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
44962a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4497942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4498942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
449924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
450024989b8cSPeter Brune         tsdm->originaldm = dm;
450124989b8cSPeter Brune       }
450224989b8cSPeter Brune     }
45036bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
450424989b8cSPeter Brune   }
45056c699258SBarry Smith   ts->dm = dm;
4506bbd56ea5SKarl Rupp 
4507089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4508089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
45096c699258SBarry Smith   PetscFunctionReturn(0);
45106c699258SBarry Smith }
45116c699258SBarry Smith 
45126c699258SBarry Smith #undef __FUNCT__
45136c699258SBarry Smith #define __FUNCT__ "TSGetDM"
45146c699258SBarry Smith /*@
45156c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
45166c699258SBarry Smith 
45173f9fe445SBarry Smith    Not Collective
45186c699258SBarry Smith 
45196c699258SBarry Smith    Input Parameter:
45206c699258SBarry Smith . ts - the preconditioner context
45216c699258SBarry Smith 
45226c699258SBarry Smith    Output Parameter:
45236c699258SBarry Smith .  dm - the dm
45246c699258SBarry Smith 
45256c699258SBarry Smith    Level: intermediate
45266c699258SBarry Smith 
45276c699258SBarry Smith 
45286c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
45296c699258SBarry Smith @*/
45307087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
45316c699258SBarry Smith {
4532496e6a7aSJed Brown   PetscErrorCode ierr;
4533496e6a7aSJed Brown 
45346c699258SBarry Smith   PetscFunctionBegin;
45350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4536496e6a7aSJed Brown   if (!ts->dm) {
4537ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4538496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4539496e6a7aSJed Brown   }
45406c699258SBarry Smith   *dm = ts->dm;
45416c699258SBarry Smith   PetscFunctionReturn(0);
45426c699258SBarry Smith }
45431713a123SBarry Smith 
45440f5c6efeSJed Brown #undef __FUNCT__
45450f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
45460f5c6efeSJed Brown /*@
45470f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
45480f5c6efeSJed Brown 
45493f9fe445SBarry Smith    Logically Collective on SNES
45500f5c6efeSJed Brown 
45510f5c6efeSJed Brown    Input Parameter:
4552d42a1c89SJed Brown + snes - nonlinear solver
45530910c330SBarry Smith . U - the current state at which to evaluate the residual
4554d42a1c89SJed Brown - ctx - user context, must be a TS
45550f5c6efeSJed Brown 
45560f5c6efeSJed Brown    Output Parameter:
45570f5c6efeSJed Brown . F - the nonlinear residual
45580f5c6efeSJed Brown 
45590f5c6efeSJed Brown    Notes:
45600f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
45610f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
45620f5c6efeSJed Brown 
45630f5c6efeSJed Brown    Level: advanced
45640f5c6efeSJed Brown 
45650f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
45660f5c6efeSJed Brown @*/
45670910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
45680f5c6efeSJed Brown {
45690f5c6efeSJed Brown   TS             ts = (TS)ctx;
45700f5c6efeSJed Brown   PetscErrorCode ierr;
45710f5c6efeSJed Brown 
45720f5c6efeSJed Brown   PetscFunctionBegin;
45730f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
45740910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
45750f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
45760f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
45770910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
45780f5c6efeSJed Brown   PetscFunctionReturn(0);
45790f5c6efeSJed Brown }
45800f5c6efeSJed Brown 
45810f5c6efeSJed Brown #undef __FUNCT__
45820f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
45830f5c6efeSJed Brown /*@
45840f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
45850f5c6efeSJed Brown 
45860f5c6efeSJed Brown    Collective on SNES
45870f5c6efeSJed Brown 
45880f5c6efeSJed Brown    Input Parameter:
45890f5c6efeSJed Brown + snes - nonlinear solver
45900910c330SBarry Smith . U - the current state at which to evaluate the residual
45910f5c6efeSJed Brown - ctx - user context, must be a TS
45920f5c6efeSJed Brown 
45930f5c6efeSJed Brown    Output Parameter:
45940f5c6efeSJed Brown + A - the Jacobian
45950f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
45960f5c6efeSJed Brown - flag - indicates any structure change in the matrix
45970f5c6efeSJed Brown 
45980f5c6efeSJed Brown    Notes:
45990f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46000f5c6efeSJed Brown 
46010f5c6efeSJed Brown    Level: developer
46020f5c6efeSJed Brown 
46030f5c6efeSJed Brown .seealso: SNESSetJacobian()
46040f5c6efeSJed Brown @*/
4605d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
46060f5c6efeSJed Brown {
46070f5c6efeSJed Brown   TS             ts = (TS)ctx;
46080f5c6efeSJed Brown   PetscErrorCode ierr;
46090f5c6efeSJed Brown 
46100f5c6efeSJed Brown   PetscFunctionBegin;
46110f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46120910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46130f5c6efeSJed Brown   PetscValidPointer(A,3);
461494ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
46150f5c6efeSJed Brown   PetscValidPointer(B,4);
461694ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
46170f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4618d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
46190f5c6efeSJed Brown   PetscFunctionReturn(0);
46200f5c6efeSJed Brown }
4621325fc9f4SBarry Smith 
46220e4ef248SJed Brown #undef __FUNCT__
46230e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
46240e4ef248SJed Brown /*@C
46259ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
46260e4ef248SJed Brown 
46270e4ef248SJed Brown    Collective on TS
46280e4ef248SJed Brown 
46290e4ef248SJed Brown    Input Arguments:
46300e4ef248SJed Brown +  ts - time stepping context
46310e4ef248SJed Brown .  t - time at which to evaluate
46320910c330SBarry Smith .  U - state at which to evaluate
46330e4ef248SJed Brown -  ctx - context
46340e4ef248SJed Brown 
46350e4ef248SJed Brown    Output Arguments:
46360e4ef248SJed Brown .  F - right hand side
46370e4ef248SJed Brown 
46380e4ef248SJed Brown    Level: intermediate
46390e4ef248SJed Brown 
46400e4ef248SJed Brown    Notes:
46410e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
46420e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
46430e4ef248SJed Brown 
46440e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
46450e4ef248SJed Brown @*/
46460910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
46470e4ef248SJed Brown {
46480e4ef248SJed Brown   PetscErrorCode ierr;
46490e4ef248SJed Brown   Mat            Arhs,Brhs;
46500e4ef248SJed Brown 
46510e4ef248SJed Brown   PetscFunctionBegin;
46520e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4653d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
46540910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
46550e4ef248SJed Brown   PetscFunctionReturn(0);
46560e4ef248SJed Brown }
46570e4ef248SJed Brown 
46580e4ef248SJed Brown #undef __FUNCT__
46590e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
46600e4ef248SJed Brown /*@C
46610e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
46620e4ef248SJed Brown 
46630e4ef248SJed Brown    Collective on TS
46640e4ef248SJed Brown 
46650e4ef248SJed Brown    Input Arguments:
46660e4ef248SJed Brown +  ts - time stepping context
46670e4ef248SJed Brown .  t - time at which to evaluate
46680910c330SBarry Smith .  U - state at which to evaluate
46690e4ef248SJed Brown -  ctx - context
46700e4ef248SJed Brown 
46710e4ef248SJed Brown    Output Arguments:
46720e4ef248SJed Brown +  A - pointer to operator
46730e4ef248SJed Brown .  B - pointer to preconditioning matrix
46740e4ef248SJed Brown -  flg - matrix structure flag
46750e4ef248SJed Brown 
46760e4ef248SJed Brown    Level: intermediate
46770e4ef248SJed Brown 
46780e4ef248SJed Brown    Notes:
46790e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
46800e4ef248SJed Brown 
46810e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
46820e4ef248SJed Brown @*/
4683d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
46840e4ef248SJed Brown {
46850e4ef248SJed Brown   PetscFunctionBegin;
46860e4ef248SJed Brown   PetscFunctionReturn(0);
46870e4ef248SJed Brown }
46880e4ef248SJed Brown 
46890026cea9SSean Farley #undef __FUNCT__
46900026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
46910026cea9SSean Farley /*@C
46920026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
46930026cea9SSean Farley 
46940026cea9SSean Farley    Collective on TS
46950026cea9SSean Farley 
46960026cea9SSean Farley    Input Arguments:
46970026cea9SSean Farley +  ts - time stepping context
46980026cea9SSean Farley .  t - time at which to evaluate
46990910c330SBarry Smith .  U - state at which to evaluate
47000910c330SBarry Smith .  Udot - time derivative of state vector
47010026cea9SSean Farley -  ctx - context
47020026cea9SSean Farley 
47030026cea9SSean Farley    Output Arguments:
47040026cea9SSean Farley .  F - left hand side
47050026cea9SSean Farley 
47060026cea9SSean Farley    Level: intermediate
47070026cea9SSean Farley 
47080026cea9SSean Farley    Notes:
47090910c330SBarry 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
47100026cea9SSean Farley    user is required to write their own TSComputeIFunction.
47110026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
47120026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
47130026cea9SSean Farley 
47149ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
47159ae8fd06SBarry Smith 
47169ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
47170026cea9SSean Farley @*/
47180910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
47190026cea9SSean Farley {
47200026cea9SSean Farley   PetscErrorCode ierr;
47210026cea9SSean Farley   Mat            A,B;
47220026cea9SSean Farley 
47230026cea9SSean Farley   PetscFunctionBegin;
47240298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4725d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
47260910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
47270026cea9SSean Farley   PetscFunctionReturn(0);
47280026cea9SSean Farley }
47290026cea9SSean Farley 
47300026cea9SSean Farley #undef __FUNCT__
47310026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
47320026cea9SSean Farley /*@C
4733b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
47340026cea9SSean Farley 
47350026cea9SSean Farley    Collective on TS
47360026cea9SSean Farley 
47370026cea9SSean Farley    Input Arguments:
47380026cea9SSean Farley +  ts - time stepping context
47390026cea9SSean Farley .  t - time at which to evaluate
47400910c330SBarry Smith .  U - state at which to evaluate
47410910c330SBarry Smith .  Udot - time derivative of state vector
47420026cea9SSean Farley .  shift - shift to apply
47430026cea9SSean Farley -  ctx - context
47440026cea9SSean Farley 
47450026cea9SSean Farley    Output Arguments:
47460026cea9SSean Farley +  A - pointer to operator
47470026cea9SSean Farley .  B - pointer to preconditioning matrix
47480026cea9SSean Farley -  flg - matrix structure flag
47490026cea9SSean Farley 
4750b41af12eSJed Brown    Level: advanced
47510026cea9SSean Farley 
47520026cea9SSean Farley    Notes:
47530026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
47540026cea9SSean Farley 
4755b41af12eSJed Brown    It is only appropriate for problems of the form
4756b41af12eSJed Brown 
4757b41af12eSJed Brown $     M Udot = F(U,t)
4758b41af12eSJed Brown 
4759b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4760b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4761b41af12eSJed Brown   an implicit operator of the form
4762b41af12eSJed Brown 
4763b41af12eSJed Brown $    shift*M + J
4764b41af12eSJed Brown 
4765b41af12eSJed 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
4766b41af12eSJed Brown   a copy of M or reassemble it when requested.
4767b41af12eSJed Brown 
47680026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
47690026cea9SSean Farley @*/
4770d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
47710026cea9SSean Farley {
4772b41af12eSJed Brown   PetscErrorCode ierr;
4773b41af12eSJed Brown 
47740026cea9SSean Farley   PetscFunctionBegin;
477594ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4776b41af12eSJed Brown   ts->ijacobian.shift = shift;
47770026cea9SSean Farley   PetscFunctionReturn(0);
47780026cea9SSean Farley }
4779b41af12eSJed Brown 
4780e817cc15SEmil Constantinescu #undef __FUNCT__
4781e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4782e817cc15SEmil Constantinescu /*@
4783e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4784e817cc15SEmil Constantinescu 
4785e817cc15SEmil Constantinescu    Not Collective
4786e817cc15SEmil Constantinescu 
4787e817cc15SEmil Constantinescu    Input Parameter:
4788e817cc15SEmil Constantinescu .  ts - the TS context
4789e817cc15SEmil Constantinescu 
4790e817cc15SEmil Constantinescu    Output Parameter:
47914e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4792e817cc15SEmil Constantinescu 
4793e817cc15SEmil Constantinescu    Level: beginner
4794e817cc15SEmil Constantinescu 
4795e817cc15SEmil Constantinescu .keywords: TS, equation type
4796e817cc15SEmil Constantinescu 
4797e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4798e817cc15SEmil Constantinescu @*/
4799e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4800e817cc15SEmil Constantinescu {
4801e817cc15SEmil Constantinescu   PetscFunctionBegin;
4802e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4803e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4804e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4805e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4806e817cc15SEmil Constantinescu }
4807e817cc15SEmil Constantinescu 
4808e817cc15SEmil Constantinescu #undef __FUNCT__
4809e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4810e817cc15SEmil Constantinescu /*@
4811e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4812e817cc15SEmil Constantinescu 
4813e817cc15SEmil Constantinescu    Not Collective
4814e817cc15SEmil Constantinescu 
4815e817cc15SEmil Constantinescu    Input Parameter:
4816e817cc15SEmil Constantinescu +  ts - the TS context
48171297b224SEmil Constantinescu -  equation_type - see TSEquationType
4818e817cc15SEmil Constantinescu 
4819e817cc15SEmil Constantinescu    Level: advanced
4820e817cc15SEmil Constantinescu 
4821e817cc15SEmil Constantinescu .keywords: TS, equation type
4822e817cc15SEmil Constantinescu 
4823e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4824e817cc15SEmil Constantinescu @*/
4825e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4826e817cc15SEmil Constantinescu {
4827e817cc15SEmil Constantinescu   PetscFunctionBegin;
4828e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4829e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4830e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4831e817cc15SEmil Constantinescu }
48320026cea9SSean Farley 
48334af1b03aSJed Brown #undef __FUNCT__
48344af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
48354af1b03aSJed Brown /*@
48364af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
48374af1b03aSJed Brown 
48384af1b03aSJed Brown    Not Collective
48394af1b03aSJed Brown 
48404af1b03aSJed Brown    Input Parameter:
48414af1b03aSJed Brown .  ts - the TS context
48424af1b03aSJed Brown 
48434af1b03aSJed Brown    Output Parameter:
48444af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
48454af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
48464af1b03aSJed Brown 
4847487e0bb9SJed Brown    Level: beginner
48484af1b03aSJed Brown 
4849cd652676SJed Brown    Notes:
4850cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
48514af1b03aSJed Brown 
48524af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
48534af1b03aSJed Brown 
48544af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
48554af1b03aSJed Brown @*/
48564af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
48574af1b03aSJed Brown {
48584af1b03aSJed Brown   PetscFunctionBegin;
48594af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48604af1b03aSJed Brown   PetscValidPointer(reason,2);
48614af1b03aSJed Brown   *reason = ts->reason;
48624af1b03aSJed Brown   PetscFunctionReturn(0);
48634af1b03aSJed Brown }
48644af1b03aSJed Brown 
4865fb1732b5SBarry Smith #undef __FUNCT__
4866d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4867d6ad946cSShri Abhyankar /*@
4868d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4869d6ad946cSShri Abhyankar 
4870d6ad946cSShri Abhyankar    Not Collective
4871d6ad946cSShri Abhyankar 
4872d6ad946cSShri Abhyankar    Input Parameter:
4873d6ad946cSShri Abhyankar +  ts - the TS context
4874d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4875d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4876d6ad946cSShri Abhyankar 
4877f5abba47SShri Abhyankar    Level: advanced
4878d6ad946cSShri Abhyankar 
4879d6ad946cSShri Abhyankar    Notes:
4880d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4881d6ad946cSShri Abhyankar 
4882d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4883d6ad946cSShri Abhyankar 
4884d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4885d6ad946cSShri Abhyankar @*/
4886d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4887d6ad946cSShri Abhyankar {
4888d6ad946cSShri Abhyankar   PetscFunctionBegin;
4889d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4890d6ad946cSShri Abhyankar   ts->reason = reason;
4891d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4892d6ad946cSShri Abhyankar }
4893d6ad946cSShri Abhyankar 
4894d6ad946cSShri Abhyankar #undef __FUNCT__
4895cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4896cc708dedSBarry Smith /*@
4897cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4898cc708dedSBarry Smith 
4899cc708dedSBarry Smith    Not Collective
4900cc708dedSBarry Smith 
4901cc708dedSBarry Smith    Input Parameter:
4902cc708dedSBarry Smith .  ts - the TS context
4903cc708dedSBarry Smith 
4904cc708dedSBarry Smith    Output Parameter:
490563e21af5SBarry Smith .  ftime - the final time. This time corresponds to the final time set with TSSetDuration()
4906cc708dedSBarry Smith 
4907487e0bb9SJed Brown    Level: beginner
4908cc708dedSBarry Smith 
4909cc708dedSBarry Smith    Notes:
4910cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4911cc708dedSBarry Smith 
4912cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4913cc708dedSBarry Smith 
4914cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4915cc708dedSBarry Smith @*/
4916cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4917cc708dedSBarry Smith {
4918cc708dedSBarry Smith   PetscFunctionBegin;
4919cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4920cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4921cc708dedSBarry Smith   *ftime = ts->solvetime;
4922cc708dedSBarry Smith   PetscFunctionReturn(0);
4923cc708dedSBarry Smith }
4924cc708dedSBarry Smith 
4925cc708dedSBarry Smith #undef __FUNCT__
49262c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
49272c18e0fdSBarry Smith /*@
49282c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
49292c18e0fdSBarry Smith 
49302c18e0fdSBarry Smith    Not Collective
49312c18e0fdSBarry Smith 
49322c18e0fdSBarry Smith    Input Parameter:
49332c18e0fdSBarry Smith .  ts - the TS context
49342c18e0fdSBarry Smith 
49352c18e0fdSBarry Smith    Output Parameter:
49362c18e0fdSBarry Smith .  steps - the number of steps
49372c18e0fdSBarry Smith 
49382c18e0fdSBarry Smith    Level: beginner
49392c18e0fdSBarry Smith 
49402c18e0fdSBarry Smith    Notes:
49412c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
49422c18e0fdSBarry Smith 
49432c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
49442c18e0fdSBarry Smith 
49452c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
49462c18e0fdSBarry Smith @*/
49472c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
49482c18e0fdSBarry Smith {
49492c18e0fdSBarry Smith   PetscFunctionBegin;
49502c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49512c18e0fdSBarry Smith   PetscValidPointer(steps,2);
49522c18e0fdSBarry Smith   *steps = ts->total_steps;
49532c18e0fdSBarry Smith   PetscFunctionReturn(0);
49542c18e0fdSBarry Smith }
49552c18e0fdSBarry Smith 
49562c18e0fdSBarry Smith #undef __FUNCT__
49575ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
49589f67acb7SJed Brown /*@
49595ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49609f67acb7SJed Brown    used by the time integrator.
49619f67acb7SJed Brown 
49629f67acb7SJed Brown    Not Collective
49639f67acb7SJed Brown 
49649f67acb7SJed Brown    Input Parameter:
49659f67acb7SJed Brown .  ts - TS context
49669f67acb7SJed Brown 
49679f67acb7SJed Brown    Output Parameter:
49689f67acb7SJed Brown .  nits - number of nonlinear iterations
49699f67acb7SJed Brown 
49709f67acb7SJed Brown    Notes:
49719f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49729f67acb7SJed Brown 
49739f67acb7SJed Brown    Level: intermediate
49749f67acb7SJed Brown 
49759f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
49769f67acb7SJed Brown 
49775ef26d82SJed Brown .seealso:  TSGetKSPIterations()
49789f67acb7SJed Brown @*/
49795ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
49809f67acb7SJed Brown {
49819f67acb7SJed Brown   PetscFunctionBegin;
49829f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49839f67acb7SJed Brown   PetscValidIntPointer(nits,2);
49845ef26d82SJed Brown   *nits = ts->snes_its;
49859f67acb7SJed Brown   PetscFunctionReturn(0);
49869f67acb7SJed Brown }
49879f67acb7SJed Brown 
49889f67acb7SJed Brown #undef __FUNCT__
49895ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
49909f67acb7SJed Brown /*@
49915ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
49929f67acb7SJed Brown    used by the time integrator.
49939f67acb7SJed Brown 
49949f67acb7SJed Brown    Not Collective
49959f67acb7SJed Brown 
49969f67acb7SJed Brown    Input Parameter:
49979f67acb7SJed Brown .  ts - TS context
49989f67acb7SJed Brown 
49999f67acb7SJed Brown    Output Parameter:
50009f67acb7SJed Brown .  lits - number of linear iterations
50019f67acb7SJed Brown 
50029f67acb7SJed Brown    Notes:
50039f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50049f67acb7SJed Brown 
50059f67acb7SJed Brown    Level: intermediate
50069f67acb7SJed Brown 
50079f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
50089f67acb7SJed Brown 
50095ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
50109f67acb7SJed Brown @*/
50115ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
50129f67acb7SJed Brown {
50139f67acb7SJed Brown   PetscFunctionBegin;
50149f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50159f67acb7SJed Brown   PetscValidIntPointer(lits,2);
50165ef26d82SJed Brown   *lits = ts->ksp_its;
50179f67acb7SJed Brown   PetscFunctionReturn(0);
50189f67acb7SJed Brown }
50199f67acb7SJed Brown 
50209f67acb7SJed Brown #undef __FUNCT__
5021cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
5022cef5090cSJed Brown /*@
5023cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5024cef5090cSJed Brown 
5025cef5090cSJed Brown    Not Collective
5026cef5090cSJed Brown 
5027cef5090cSJed Brown    Input Parameter:
5028cef5090cSJed Brown .  ts - TS context
5029cef5090cSJed Brown 
5030cef5090cSJed Brown    Output Parameter:
5031cef5090cSJed Brown .  rejects - number of steps rejected
5032cef5090cSJed Brown 
5033cef5090cSJed Brown    Notes:
5034cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5035cef5090cSJed Brown 
5036cef5090cSJed Brown    Level: intermediate
5037cef5090cSJed Brown 
5038cef5090cSJed Brown .keywords: TS, get, number
5039cef5090cSJed Brown 
50405ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5041cef5090cSJed Brown @*/
5042cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5043cef5090cSJed Brown {
5044cef5090cSJed Brown   PetscFunctionBegin;
5045cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5046cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5047cef5090cSJed Brown   *rejects = ts->reject;
5048cef5090cSJed Brown   PetscFunctionReturn(0);
5049cef5090cSJed Brown }
5050cef5090cSJed Brown 
5051cef5090cSJed Brown #undef __FUNCT__
5052cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
5053cef5090cSJed Brown /*@
5054cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5055cef5090cSJed Brown 
5056cef5090cSJed Brown    Not Collective
5057cef5090cSJed Brown 
5058cef5090cSJed Brown    Input Parameter:
5059cef5090cSJed Brown .  ts - TS context
5060cef5090cSJed Brown 
5061cef5090cSJed Brown    Output Parameter:
5062cef5090cSJed Brown .  fails - number of failed nonlinear solves
5063cef5090cSJed Brown 
5064cef5090cSJed Brown    Notes:
5065cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5066cef5090cSJed Brown 
5067cef5090cSJed Brown    Level: intermediate
5068cef5090cSJed Brown 
5069cef5090cSJed Brown .keywords: TS, get, number
5070cef5090cSJed Brown 
50715ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5072cef5090cSJed Brown @*/
5073cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5074cef5090cSJed Brown {
5075cef5090cSJed Brown   PetscFunctionBegin;
5076cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5077cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5078cef5090cSJed Brown   *fails = ts->num_snes_failures;
5079cef5090cSJed Brown   PetscFunctionReturn(0);
5080cef5090cSJed Brown }
5081cef5090cSJed Brown 
5082cef5090cSJed Brown #undef __FUNCT__
5083cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
5084cef5090cSJed Brown /*@
5085cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5086cef5090cSJed Brown 
5087cef5090cSJed Brown    Not Collective
5088cef5090cSJed Brown 
5089cef5090cSJed Brown    Input Parameter:
5090cef5090cSJed Brown +  ts - TS context
5091cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5092cef5090cSJed Brown 
5093cef5090cSJed Brown    Notes:
5094cef5090cSJed Brown    The counter is reset to zero for each step
5095cef5090cSJed Brown 
5096cef5090cSJed Brown    Options Database Key:
5097cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5098cef5090cSJed Brown 
5099cef5090cSJed Brown    Level: intermediate
5100cef5090cSJed Brown 
5101cef5090cSJed Brown .keywords: TS, set, maximum, number
5102cef5090cSJed Brown 
51035ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5104cef5090cSJed Brown @*/
5105cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5106cef5090cSJed Brown {
5107cef5090cSJed Brown   PetscFunctionBegin;
5108cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5109cef5090cSJed Brown   ts->max_reject = rejects;
5110cef5090cSJed Brown   PetscFunctionReturn(0);
5111cef5090cSJed Brown }
5112cef5090cSJed Brown 
5113cef5090cSJed Brown #undef __FUNCT__
5114cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
5115cef5090cSJed Brown /*@
5116cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5117cef5090cSJed Brown 
5118cef5090cSJed Brown    Not Collective
5119cef5090cSJed Brown 
5120cef5090cSJed Brown    Input Parameter:
5121cef5090cSJed Brown +  ts - TS context
5122cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5123cef5090cSJed Brown 
5124cef5090cSJed Brown    Notes:
5125cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5126cef5090cSJed Brown 
5127cef5090cSJed Brown    Options Database Key:
5128cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5129cef5090cSJed Brown 
5130cef5090cSJed Brown    Level: intermediate
5131cef5090cSJed Brown 
5132cef5090cSJed Brown .keywords: TS, set, maximum, number
5133cef5090cSJed Brown 
51345ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5135cef5090cSJed Brown @*/
5136cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5137cef5090cSJed Brown {
5138cef5090cSJed Brown   PetscFunctionBegin;
5139cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5140cef5090cSJed Brown   ts->max_snes_failures = fails;
5141cef5090cSJed Brown   PetscFunctionReturn(0);
5142cef5090cSJed Brown }
5143cef5090cSJed Brown 
5144cef5090cSJed Brown #undef __FUNCT__
51454e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
5146cef5090cSJed Brown /*@
5147cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5148cef5090cSJed Brown 
5149cef5090cSJed Brown    Not Collective
5150cef5090cSJed Brown 
5151cef5090cSJed Brown    Input Parameter:
5152cef5090cSJed Brown +  ts - TS context
5153cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5154cef5090cSJed Brown 
5155cef5090cSJed Brown    Options Database Key:
5156cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5157cef5090cSJed Brown 
5158cef5090cSJed Brown    Level: intermediate
5159cef5090cSJed Brown 
5160cef5090cSJed Brown .keywords: TS, set, error
5161cef5090cSJed Brown 
51625ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5163cef5090cSJed Brown @*/
5164cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5165cef5090cSJed Brown {
5166cef5090cSJed Brown   PetscFunctionBegin;
5167cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5168cef5090cSJed Brown   ts->errorifstepfailed = err;
5169cef5090cSJed Brown   PetscFunctionReturn(0);
5170cef5090cSJed Brown }
5171cef5090cSJed Brown 
5172cef5090cSJed Brown #undef __FUNCT__
5173fde5950dSBarry Smith #define __FUNCT__ "TSMonitorSolution"
5174fb1732b5SBarry Smith /*@C
5175fde5950dSBarry 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
5176fb1732b5SBarry Smith 
5177fb1732b5SBarry Smith    Collective on TS
5178fb1732b5SBarry Smith 
5179fb1732b5SBarry Smith    Input Parameters:
5180fb1732b5SBarry Smith +  ts - the TS context
5181fb1732b5SBarry Smith .  step - current time-step
5182fb1732b5SBarry Smith .  ptime - current time
51830910c330SBarry Smith .  u - current state
5184721cd6eeSBarry Smith -  vf - viewer and its format
5185fb1732b5SBarry Smith 
5186fb1732b5SBarry Smith    Level: intermediate
5187fb1732b5SBarry Smith 
5188fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5189fb1732b5SBarry Smith 
5190fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5191fb1732b5SBarry Smith @*/
5192721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5193fb1732b5SBarry Smith {
5194fb1732b5SBarry Smith   PetscErrorCode ierr;
5195fb1732b5SBarry Smith 
5196fb1732b5SBarry Smith   PetscFunctionBegin;
5197721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5198721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5199721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5200ed81e22dSJed Brown   PetscFunctionReturn(0);
5201ed81e22dSJed Brown }
5202ed81e22dSJed Brown 
5203ed81e22dSJed Brown #undef __FUNCT__
5204ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
5205ed81e22dSJed Brown /*@C
5206ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5207ed81e22dSJed Brown 
5208ed81e22dSJed Brown    Collective on TS
5209ed81e22dSJed Brown 
5210ed81e22dSJed Brown    Input Parameters:
5211ed81e22dSJed Brown +  ts - the TS context
5212ed81e22dSJed Brown .  step - current time-step
5213ed81e22dSJed Brown .  ptime - current time
52140910c330SBarry Smith .  u - current state
5215ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5216ed81e22dSJed Brown 
5217ed81e22dSJed Brown    Level: intermediate
5218ed81e22dSJed Brown 
5219ed81e22dSJed Brown    Notes:
5220ed81e22dSJed 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.
5221ed81e22dSJed Brown    These are named according to the file name template.
5222ed81e22dSJed Brown 
5223ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5224ed81e22dSJed Brown 
5225ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5226ed81e22dSJed Brown 
5227ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5228ed81e22dSJed Brown @*/
52290910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5230ed81e22dSJed Brown {
5231ed81e22dSJed Brown   PetscErrorCode ierr;
5232ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5233ed81e22dSJed Brown   PetscViewer    viewer;
5234ed81e22dSJed Brown 
5235ed81e22dSJed Brown   PetscFunctionBegin;
523663e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
52378caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5238ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
52390910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5240ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5241ed81e22dSJed Brown   PetscFunctionReturn(0);
5242ed81e22dSJed Brown }
5243ed81e22dSJed Brown 
5244ed81e22dSJed Brown #undef __FUNCT__
5245ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
5246ed81e22dSJed Brown /*@C
5247ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5248ed81e22dSJed Brown 
5249ed81e22dSJed Brown    Collective on TS
5250ed81e22dSJed Brown 
5251ed81e22dSJed Brown    Input Parameters:
5252ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5253ed81e22dSJed Brown 
5254ed81e22dSJed Brown    Level: intermediate
5255ed81e22dSJed Brown 
5256ed81e22dSJed Brown    Note:
5257ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5258ed81e22dSJed Brown 
5259ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5260ed81e22dSJed Brown 
5261ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5262ed81e22dSJed Brown @*/
5263ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5264ed81e22dSJed Brown {
5265ed81e22dSJed Brown   PetscErrorCode ierr;
5266ed81e22dSJed Brown 
5267ed81e22dSJed Brown   PetscFunctionBegin;
5268ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5269fb1732b5SBarry Smith   PetscFunctionReturn(0);
5270fb1732b5SBarry Smith }
5271fb1732b5SBarry Smith 
527284df9cb4SJed Brown #undef __FUNCT__
5273552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
527484df9cb4SJed Brown /*@
5275552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
527684df9cb4SJed Brown 
5277ed81e22dSJed Brown    Collective on TS if controller has not been created yet
527884df9cb4SJed Brown 
527984df9cb4SJed Brown    Input Arguments:
5280ed81e22dSJed Brown .  ts - time stepping context
528184df9cb4SJed Brown 
528284df9cb4SJed Brown    Output Arguments:
5283ed81e22dSJed Brown .  adapt - adaptive controller
528484df9cb4SJed Brown 
528584df9cb4SJed Brown    Level: intermediate
528684df9cb4SJed Brown 
5287ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
528884df9cb4SJed Brown @*/
5289552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
529084df9cb4SJed Brown {
529184df9cb4SJed Brown   PetscErrorCode ierr;
529284df9cb4SJed Brown 
529384df9cb4SJed Brown   PetscFunctionBegin;
529484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5295bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
529684df9cb4SJed Brown   if (!ts->adapt) {
5297ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
52983bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
52991c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
530084df9cb4SJed Brown   }
5301bec58848SLisandro Dalcin   *adapt = ts->adapt;
530284df9cb4SJed Brown   PetscFunctionReturn(0);
530384df9cb4SJed Brown }
5304d6ebe24aSShri Abhyankar 
5305d6ebe24aSShri Abhyankar #undef __FUNCT__
53061c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
53071c3436cfSJed Brown /*@
53081c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
53091c3436cfSJed Brown 
53101c3436cfSJed Brown    Logically Collective
53111c3436cfSJed Brown 
53121c3436cfSJed Brown    Input Arguments:
53131c3436cfSJed Brown +  ts - time integration context
53141c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
53150298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
53161c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
53170298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
53181c3436cfSJed Brown 
5319a3cdaa26SBarry Smith    Options Database keys:
5320a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5321a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5322a3cdaa26SBarry Smith 
53233ff766beSShri Abhyankar    Notes:
53243ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
53253ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
53263ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
53273ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
53283ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
53293ff766beSShri Abhyankar    differential variables.
53303ff766beSShri Abhyankar 
53311c3436cfSJed Brown    Level: beginner
53321c3436cfSJed Brown 
5333c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
53341c3436cfSJed Brown @*/
53351c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
53361c3436cfSJed Brown {
53371c3436cfSJed Brown   PetscErrorCode ierr;
53381c3436cfSJed Brown 
53391c3436cfSJed Brown   PetscFunctionBegin;
5340c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
53411c3436cfSJed Brown   if (vatol) {
53421c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
53431c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
53441c3436cfSJed Brown     ts->vatol = vatol;
53451c3436cfSJed Brown   }
5346c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
53471c3436cfSJed Brown   if (vrtol) {
53481c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
53491c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
53501c3436cfSJed Brown     ts->vrtol = vrtol;
53511c3436cfSJed Brown   }
53521c3436cfSJed Brown   PetscFunctionReturn(0);
53531c3436cfSJed Brown }
53541c3436cfSJed Brown 
53551c3436cfSJed Brown #undef __FUNCT__
5356c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
5357c5033834SJed Brown /*@
5358c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5359c5033834SJed Brown 
5360c5033834SJed Brown    Logically Collective
5361c5033834SJed Brown 
5362c5033834SJed Brown    Input Arguments:
5363c5033834SJed Brown .  ts - time integration context
5364c5033834SJed Brown 
5365c5033834SJed Brown    Output Arguments:
53660298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53670298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53680298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53690298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5370c5033834SJed Brown 
5371c5033834SJed Brown    Level: beginner
5372c5033834SJed Brown 
5373c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5374c5033834SJed Brown @*/
5375c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5376c5033834SJed Brown {
5377c5033834SJed Brown   PetscFunctionBegin;
5378c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5379c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5380c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5381c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5382c5033834SJed Brown   PetscFunctionReturn(0);
5383c5033834SJed Brown }
5384c5033834SJed Brown 
5385c5033834SJed Brown #undef __FUNCT__
53869c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
53879c6b16b5SShri Abhyankar /*@
5388a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
53899c6b16b5SShri Abhyankar 
53909c6b16b5SShri Abhyankar    Collective on TS
53919c6b16b5SShri Abhyankar 
53929c6b16b5SShri Abhyankar    Input Arguments:
53939c6b16b5SShri Abhyankar +  ts - time stepping context
5394a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5395a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
53969c6b16b5SShri Abhyankar 
53979c6b16b5SShri Abhyankar    Output Arguments:
53989c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
53999c6b16b5SShri Abhyankar 
54009c6b16b5SShri Abhyankar    Level: developer
54019c6b16b5SShri Abhyankar 
5402deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
54039c6b16b5SShri Abhyankar @*/
5404a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm)
54059c6b16b5SShri Abhyankar {
54069c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54079c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
54089c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54099c6b16b5SShri Abhyankar   PetscReal         sum,gsum;
54109c6b16b5SShri Abhyankar   PetscReal         tol;
54119c6b16b5SShri Abhyankar 
54129c6b16b5SShri Abhyankar   PetscFunctionBegin;
54139c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5414a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5415a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5416a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5417a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5418a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5419a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5420a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
54219c6b16b5SShri Abhyankar 
54229c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
54239c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
54249c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
54259c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54269c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54279c6b16b5SShri Abhyankar   sum  = 0.;
54289c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
54299c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
54309c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54319c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54329c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54339c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54349c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54359c6b16b5SShri Abhyankar     }
54369c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54379c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54389c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54399c6b16b5SShri Abhyankar     const PetscScalar *atol;
54409c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54419c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54429c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54439c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54449c6b16b5SShri Abhyankar     }
54459c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54469c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54479c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54489c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54499c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54509c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54519c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54529c6b16b5SShri Abhyankar     }
54539c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54549c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
54559c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54569c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54579c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
54589c6b16b5SShri Abhyankar     }
54599c6b16b5SShri Abhyankar   }
54609c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
54619c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
54629c6b16b5SShri Abhyankar 
5463b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
54649c6b16b5SShri Abhyankar   *norm = PetscSqrtReal(gsum / N);
54659c6b16b5SShri Abhyankar 
54669c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
54679c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
54689c6b16b5SShri Abhyankar }
54699c6b16b5SShri Abhyankar 
54709c6b16b5SShri Abhyankar #undef __FUNCT__
54719c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
54729c6b16b5SShri Abhyankar /*@
5473a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
54749c6b16b5SShri Abhyankar 
54759c6b16b5SShri Abhyankar    Collective on TS
54769c6b16b5SShri Abhyankar 
54779c6b16b5SShri Abhyankar    Input Arguments:
54789c6b16b5SShri Abhyankar +  ts - time stepping context
5479a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5480a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
54819c6b16b5SShri Abhyankar 
54829c6b16b5SShri Abhyankar    Output Arguments:
54839c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
54849c6b16b5SShri Abhyankar 
54859c6b16b5SShri Abhyankar    Level: developer
54869c6b16b5SShri Abhyankar 
5487deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
54889c6b16b5SShri Abhyankar @*/
5489a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm)
54909c6b16b5SShri Abhyankar {
54919c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54929c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart,k;
54939c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54949c6b16b5SShri Abhyankar   PetscReal         max,gmax;
54959c6b16b5SShri Abhyankar   PetscReal         tol;
54969c6b16b5SShri Abhyankar 
54979c6b16b5SShri Abhyankar   PetscFunctionBegin;
54989c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5499a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5500a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5501a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5502a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5503a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5504a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5505a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55069c6b16b5SShri Abhyankar 
55079c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55089c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55099c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55109c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55119c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55129c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
55139c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55149c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55159c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55169c6b16b5SShri Abhyankar     k = 0;
55179c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55189c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55199c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55209c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55219c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55229c6b16b5SShri Abhyankar     }
55239c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55249c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55259c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55269c6b16b5SShri Abhyankar     const PetscScalar *atol;
55279c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55289c6b16b5SShri Abhyankar     k = 0;
55299c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55309c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55319c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55329c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55339c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55349c6b16b5SShri Abhyankar     }
55359c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55369c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55379c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55389c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55399c6b16b5SShri Abhyankar     k = 0;
55409c6b16b5SShri Abhyankar     tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55419c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55429c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55439c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55449c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55459c6b16b5SShri Abhyankar     }
55469c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55479c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
55489c6b16b5SShri Abhyankar     k = 0;
55499c6b16b5SShri Abhyankar     tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
55509c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
55519c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
55529c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55539c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
55549c6b16b5SShri Abhyankar     }
55559c6b16b5SShri Abhyankar   }
55569c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
55579c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
55589c6b16b5SShri Abhyankar 
5559b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
55609c6b16b5SShri Abhyankar   *norm = gmax;
55619c6b16b5SShri Abhyankar 
55629c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55639c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
55649c6b16b5SShri Abhyankar }
55659c6b16b5SShri Abhyankar 
55669c6b16b5SShri Abhyankar #undef __FUNCT__
55677619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
55681c3436cfSJed Brown /*@
5569a4868fbcSLisandro Dalcin    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors
55701c3436cfSJed Brown 
55711c3436cfSJed Brown    Collective on TS
55721c3436cfSJed Brown 
55731c3436cfSJed Brown    Input Arguments:
55741c3436cfSJed Brown +  ts - time stepping context
5575a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5576a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5577a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
55787619abb3SShri 
55791c3436cfSJed Brown    Output Arguments:
55801c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
55811c3436cfSJed Brown 
5582a4868fbcSLisandro Dalcin 
5583a4868fbcSLisandro Dalcin    Options Database Keys:
5584a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5585a4868fbcSLisandro Dalcin 
55861c3436cfSJed Brown    Level: developer
55871c3436cfSJed Brown 
5588deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
55891c3436cfSJed Brown @*/
5590a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm)
55911c3436cfSJed Brown {
55928beabaa1SBarry Smith   PetscErrorCode ierr;
55931c3436cfSJed Brown 
55941c3436cfSJed Brown   PetscFunctionBegin;
5595a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
5596a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr);
5597a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
5598a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr);
5599a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
56001c3436cfSJed Brown   PetscFunctionReturn(0);
56011c3436cfSJed Brown }
56021c3436cfSJed Brown 
56031c3436cfSJed Brown #undef __FUNCT__
56048d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
56058d59e960SJed Brown /*@
56068d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
56078d59e960SJed Brown 
56088d59e960SJed Brown    Logically Collective on TS
56098d59e960SJed Brown 
56108d59e960SJed Brown    Input Arguments:
56118d59e960SJed Brown +  ts - time stepping context
56128d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
56138d59e960SJed Brown 
56148d59e960SJed Brown    Note:
56158d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
56168d59e960SJed Brown 
56178d59e960SJed Brown    Level: intermediate
56188d59e960SJed Brown 
56198d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
56208d59e960SJed Brown @*/
56218d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
56228d59e960SJed Brown {
56238d59e960SJed Brown   PetscFunctionBegin;
56248d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56258d59e960SJed Brown   ts->cfltime_local = cfltime;
56268d59e960SJed Brown   ts->cfltime       = -1.;
56278d59e960SJed Brown   PetscFunctionReturn(0);
56288d59e960SJed Brown }
56298d59e960SJed Brown 
56308d59e960SJed Brown #undef __FUNCT__
56318d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
56328d59e960SJed Brown /*@
56338d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
56348d59e960SJed Brown 
56358d59e960SJed Brown    Collective on TS
56368d59e960SJed Brown 
56378d59e960SJed Brown    Input Arguments:
56388d59e960SJed Brown .  ts - time stepping context
56398d59e960SJed Brown 
56408d59e960SJed Brown    Output Arguments:
56418d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
56428d59e960SJed Brown 
56438d59e960SJed Brown    Level: advanced
56448d59e960SJed Brown 
56458d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
56468d59e960SJed Brown @*/
56478d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
56488d59e960SJed Brown {
56498d59e960SJed Brown   PetscErrorCode ierr;
56508d59e960SJed Brown 
56518d59e960SJed Brown   PetscFunctionBegin;
56528d59e960SJed Brown   if (ts->cfltime < 0) {
5653b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56548d59e960SJed Brown   }
56558d59e960SJed Brown   *cfltime = ts->cfltime;
56568d59e960SJed Brown   PetscFunctionReturn(0);
56578d59e960SJed Brown }
56588d59e960SJed Brown 
56598d59e960SJed Brown #undef __FUNCT__
5660d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5661d6ebe24aSShri Abhyankar /*@
5662d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5663d6ebe24aSShri Abhyankar 
5664d6ebe24aSShri Abhyankar    Input Parameters:
5665d6ebe24aSShri Abhyankar .  ts   - the TS context.
5666d6ebe24aSShri Abhyankar .  xl   - lower bound.
5667d6ebe24aSShri Abhyankar .  xu   - upper bound.
5668d6ebe24aSShri Abhyankar 
5669d6ebe24aSShri Abhyankar    Notes:
5670d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5671e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5672d6ebe24aSShri Abhyankar 
56732bd2b0e6SSatish Balay    Level: advanced
56742bd2b0e6SSatish Balay 
5675d6ebe24aSShri Abhyankar @*/
5676d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5677d6ebe24aSShri Abhyankar {
5678d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5679d6ebe24aSShri Abhyankar   SNES           snes;
5680d6ebe24aSShri Abhyankar 
5681d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5682d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5683d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5684d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5685d6ebe24aSShri Abhyankar }
5686d6ebe24aSShri Abhyankar 
5687325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5688c6db04a5SJed Brown #include <mex.h>
5689325fc9f4SBarry Smith 
5690325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5691325fc9f4SBarry Smith 
5692325fc9f4SBarry Smith #undef __FUNCT__
5693325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5694325fc9f4SBarry Smith /*
5695325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5696325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5697325fc9f4SBarry Smith 
5698325fc9f4SBarry Smith    Collective on TS
5699325fc9f4SBarry Smith 
5700325fc9f4SBarry Smith    Input Parameters:
5701325fc9f4SBarry Smith +  snes - the TS context
57020910c330SBarry Smith -  u - input vector
5703325fc9f4SBarry Smith 
5704325fc9f4SBarry Smith    Output Parameter:
5705325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5706325fc9f4SBarry Smith 
5707325fc9f4SBarry Smith    Notes:
5708325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5709325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5710325fc9f4SBarry Smith    themselves.
5711325fc9f4SBarry Smith 
5712325fc9f4SBarry Smith    Level: developer
5713325fc9f4SBarry Smith 
5714325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5715325fc9f4SBarry Smith 
5716325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5717325fc9f4SBarry Smith */
57180910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5719325fc9f4SBarry Smith {
5720325fc9f4SBarry Smith   PetscErrorCode  ierr;
5721325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5722325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5723325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5724325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5725325fc9f4SBarry Smith 
5726325fc9f4SBarry Smith   PetscFunctionBegin;
5727325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
57280910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
57290910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5730325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
57310910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5732325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5733325fc9f4SBarry Smith 
5734325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
57350910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
57360910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
57370910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5738bbd56ea5SKarl Rupp 
5739325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5740325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5741325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5742325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5743325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5744325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5745325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5746325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5747325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5748325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5749325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5750325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5751325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5752325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5753325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5754325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5755325fc9f4SBarry Smith   PetscFunctionReturn(0);
5756325fc9f4SBarry Smith }
5757325fc9f4SBarry Smith 
5758325fc9f4SBarry Smith 
5759325fc9f4SBarry Smith #undef __FUNCT__
5760325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5761325fc9f4SBarry Smith /*
5762325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5763325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5764e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5765325fc9f4SBarry Smith 
5766325fc9f4SBarry Smith    Logically Collective on TS
5767325fc9f4SBarry Smith 
5768325fc9f4SBarry Smith    Input Parameters:
5769325fc9f4SBarry Smith +  ts - the TS context
5770325fc9f4SBarry Smith -  func - function evaluation routine
5771325fc9f4SBarry Smith 
5772325fc9f4SBarry Smith    Calling sequence of func:
57730910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5774325fc9f4SBarry Smith 
5775325fc9f4SBarry Smith    Level: beginner
5776325fc9f4SBarry Smith 
5777325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5778325fc9f4SBarry Smith 
5779325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5780325fc9f4SBarry Smith */
5781cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5782325fc9f4SBarry Smith {
5783325fc9f4SBarry Smith   PetscErrorCode  ierr;
5784325fc9f4SBarry Smith   TSMatlabContext *sctx;
5785325fc9f4SBarry Smith 
5786325fc9f4SBarry Smith   PetscFunctionBegin;
5787325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5788325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5789325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5790325fc9f4SBarry Smith   /*
5791325fc9f4SBarry Smith      This should work, but it doesn't
5792325fc9f4SBarry Smith   sctx->ctx = ctx;
5793325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5794325fc9f4SBarry Smith   */
5795325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5796bbd56ea5SKarl Rupp 
57970298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5798325fc9f4SBarry Smith   PetscFunctionReturn(0);
5799325fc9f4SBarry Smith }
5800325fc9f4SBarry Smith 
5801325fc9f4SBarry Smith #undef __FUNCT__
5802325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5803325fc9f4SBarry Smith /*
5804325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5805325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5806325fc9f4SBarry Smith 
5807325fc9f4SBarry Smith    Collective on TS
5808325fc9f4SBarry Smith 
5809325fc9f4SBarry Smith    Input Parameters:
5810cdcf91faSSean Farley +  ts - the TS context
58110910c330SBarry Smith .  u - input vector
5812325fc9f4SBarry Smith .  A, B - the matrices
5813325fc9f4SBarry Smith -  ctx - user context
5814325fc9f4SBarry Smith 
5815325fc9f4SBarry Smith    Level: developer
5816325fc9f4SBarry Smith 
5817325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5818325fc9f4SBarry Smith 
5819325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5820325fc9f4SBarry Smith @*/
5821f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5822325fc9f4SBarry Smith {
5823325fc9f4SBarry Smith   PetscErrorCode  ierr;
5824325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5825325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5826325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5827325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5828325fc9f4SBarry Smith 
5829325fc9f4SBarry Smith   PetscFunctionBegin;
5830cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58310910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5832325fc9f4SBarry Smith 
58330910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5834325fc9f4SBarry Smith 
5835cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
58360910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
58370910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
58380910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
58390910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5840bbd56ea5SKarl Rupp 
5841325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5842325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5843325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5844325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5845325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5846325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5847325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5848325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5849325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5850325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5851325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5852325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5853325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5854325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5855325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5856325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5857325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5858325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5859325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5860325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5861325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5862325fc9f4SBarry Smith   PetscFunctionReturn(0);
5863325fc9f4SBarry Smith }
5864325fc9f4SBarry Smith 
5865325fc9f4SBarry Smith 
5866325fc9f4SBarry Smith #undef __FUNCT__
5867325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5868325fc9f4SBarry Smith /*
5869325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5870e3c5b3baSBarry 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
5871325fc9f4SBarry Smith 
5872325fc9f4SBarry Smith    Logically Collective on TS
5873325fc9f4SBarry Smith 
5874325fc9f4SBarry Smith    Input Parameters:
5875cdcf91faSSean Farley +  ts - the TS context
5876325fc9f4SBarry Smith .  A,B - Jacobian matrices
5877325fc9f4SBarry Smith .  func - function evaluation routine
5878325fc9f4SBarry Smith -  ctx - user context
5879325fc9f4SBarry Smith 
5880325fc9f4SBarry Smith    Calling sequence of func:
58810910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5882325fc9f4SBarry Smith 
5883325fc9f4SBarry Smith 
5884325fc9f4SBarry Smith    Level: developer
5885325fc9f4SBarry Smith 
5886325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5887325fc9f4SBarry Smith 
5888325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5889325fc9f4SBarry Smith */
5890cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5891325fc9f4SBarry Smith {
5892325fc9f4SBarry Smith   PetscErrorCode  ierr;
5893325fc9f4SBarry Smith   TSMatlabContext *sctx;
5894325fc9f4SBarry Smith 
5895325fc9f4SBarry Smith   PetscFunctionBegin;
5896325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5897325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5898325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5899325fc9f4SBarry Smith   /*
5900325fc9f4SBarry Smith      This should work, but it doesn't
5901325fc9f4SBarry Smith   sctx->ctx = ctx;
5902325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5903325fc9f4SBarry Smith   */
5904325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5905bbd56ea5SKarl Rupp 
5906cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5907325fc9f4SBarry Smith   PetscFunctionReturn(0);
5908325fc9f4SBarry Smith }
5909325fc9f4SBarry Smith 
5910b5b1a830SBarry Smith #undef __FUNCT__
5911b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5912b5b1a830SBarry Smith /*
5913b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5914b5b1a830SBarry Smith 
5915b5b1a830SBarry Smith    Collective on TS
5916b5b1a830SBarry Smith 
5917b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5918b5b1a830SBarry Smith @*/
59190910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5920b5b1a830SBarry Smith {
5921b5b1a830SBarry Smith   PetscErrorCode  ierr;
5922b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5923a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5924b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5925b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5926b5b1a830SBarry Smith 
5927b5b1a830SBarry Smith   PetscFunctionBegin;
5928cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
59290910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5930b5b1a830SBarry Smith 
5931cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
59320910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5933bbd56ea5SKarl Rupp 
5934b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5935b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5936b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5937b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5938b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5939b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5940b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5941b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5942b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5943b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5944b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5945b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5946b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5947b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5948b5b1a830SBarry Smith   PetscFunctionReturn(0);
5949b5b1a830SBarry Smith }
5950b5b1a830SBarry Smith 
5951b5b1a830SBarry Smith 
5952b5b1a830SBarry Smith #undef __FUNCT__
5953b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5954b5b1a830SBarry Smith /*
5955b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5956b5b1a830SBarry Smith 
5957b5b1a830SBarry Smith    Level: developer
5958b5b1a830SBarry Smith 
5959b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5960b5b1a830SBarry Smith 
5961b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5962b5b1a830SBarry Smith */
5963cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5964b5b1a830SBarry Smith {
5965b5b1a830SBarry Smith   PetscErrorCode  ierr;
5966b5b1a830SBarry Smith   TSMatlabContext *sctx;
5967b5b1a830SBarry Smith 
5968b5b1a830SBarry Smith   PetscFunctionBegin;
5969b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5970b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5971b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5972b5b1a830SBarry Smith   /*
5973b5b1a830SBarry Smith      This should work, but it doesn't
5974b5b1a830SBarry Smith   sctx->ctx = ctx;
5975b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5976b5b1a830SBarry Smith   */
5977b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5978bbd56ea5SKarl Rupp 
59790298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5980b5b1a830SBarry Smith   PetscFunctionReturn(0);
5981b5b1a830SBarry Smith }
5982325fc9f4SBarry Smith #endif
5983b3603a34SBarry Smith 
5984b3603a34SBarry Smith #undef __FUNCT__
59854f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5986b3603a34SBarry Smith /*@C
59874f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5988b3603a34SBarry Smith        in a time based line graph
5989b3603a34SBarry Smith 
5990b3603a34SBarry Smith    Collective on TS
5991b3603a34SBarry Smith 
5992b3603a34SBarry Smith    Input Parameters:
5993b3603a34SBarry Smith +  ts - the TS context
5994b3603a34SBarry Smith .  step - current time-step
5995b3603a34SBarry Smith .  ptime - current time
59967db568b7SBarry Smith .  u - current solution
59977db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
5998b3603a34SBarry Smith 
5999b3d3934dSBarry Smith    Options Database:
60009ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6001b3d3934dSBarry Smith 
6002b3603a34SBarry Smith    Level: intermediate
6003b3603a34SBarry Smith 
60046934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component solutions in a separate window
6005b3603a34SBarry Smith 
6006b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6007b3603a34SBarry Smith 
60087db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
60097db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
60107db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
60117db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6012b3603a34SBarry Smith @*/
60137db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6014b3603a34SBarry Smith {
6015b3603a34SBarry Smith   PetscErrorCode    ierr;
60167db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6017b3603a34SBarry Smith   const PetscScalar *yy;
601880666b62SBarry Smith   Vec               v;
6019b3603a34SBarry Smith 
6020b3603a34SBarry Smith   PetscFunctionBegin;
602163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
602258ff32f7SBarry Smith   if (!step) {
6023a9f9c1f6SBarry Smith     PetscDrawAxis axis;
60246934998bSLisandro Dalcin     PetscInt      dim;
6025a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6026a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6027387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6028387f4636SBarry Smith       char      **displaynames;
6029387f4636SBarry Smith       PetscBool flg;
6030387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6031387f4636SBarry Smith       ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr);
6032387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6033c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6034387f4636SBarry Smith       if (flg) {
6035a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6036387f4636SBarry Smith       }
6037387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6038387f4636SBarry Smith     }
6039387f4636SBarry Smith     if (ctx->displaynames) {
6040387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6041387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6042387f4636SBarry Smith     } else if (ctx->names) {
60430910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
60440b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6045387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6046b0bc92c6SBarry Smith     } else {
6047b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6048b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6049387f4636SBarry Smith     }
60500b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
605158ff32f7SBarry Smith   }
60526934998bSLisandro Dalcin 
60536934998bSLisandro Dalcin   if (!ctx->transform) v = u;
60546934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
605580666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
60566934998bSLisandro Dalcin   if (ctx->displaynames) {
60576934998bSLisandro Dalcin     PetscInt i;
60586934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
60596934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
60606934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
60616934998bSLisandro Dalcin   } else {
6062e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6063e3efe391SJed Brown     PetscInt  i,n;
60646934998bSLisandro Dalcin     PetscReal *yreal;
606580666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6066785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6067e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
60680b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6069e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6070e3efe391SJed Brown #else
60710b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6072e3efe391SJed Brown #endif
607380666b62SBarry Smith   }
60746934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
60756934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
60766934998bSLisandro Dalcin 
6077b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
60780b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
60796934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
60803923b477SBarry Smith   }
6081b3603a34SBarry Smith   PetscFunctionReturn(0);
6082b3603a34SBarry Smith }
6083b3603a34SBarry Smith 
6084387f4636SBarry Smith 
6085b3603a34SBarry Smith #undef __FUNCT__
608631152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
6087b037adc7SBarry Smith /*@C
608831152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6089b037adc7SBarry Smith 
6090b037adc7SBarry Smith    Collective on TS
6091b037adc7SBarry Smith 
6092b037adc7SBarry Smith    Input Parameters:
6093b037adc7SBarry Smith +  ts - the TS context
6094b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6095b037adc7SBarry Smith 
6096b037adc7SBarry Smith    Level: intermediate
6097b037adc7SBarry Smith 
60987db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
60997db568b7SBarry Smith 
6100b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6101b037adc7SBarry Smith 
6102a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6103b037adc7SBarry Smith @*/
610431152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6105b037adc7SBarry Smith {
6106b037adc7SBarry Smith   PetscErrorCode    ierr;
6107b037adc7SBarry Smith   PetscInt          i;
6108b037adc7SBarry Smith 
6109b037adc7SBarry Smith   PetscFunctionBegin;
6110b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6111b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
61125537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6113b3d3934dSBarry Smith       break;
6114b3d3934dSBarry Smith     }
6115b3d3934dSBarry Smith   }
6116b3d3934dSBarry Smith   PetscFunctionReturn(0);
6117b3d3934dSBarry Smith }
6118b3d3934dSBarry Smith 
6119b3d3934dSBarry Smith #undef __FUNCT__
6120e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
6121e673d494SBarry Smith /*@C
6122e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6123e673d494SBarry Smith 
6124e673d494SBarry Smith    Collective on TS
6125e673d494SBarry Smith 
6126e673d494SBarry Smith    Input Parameters:
6127e673d494SBarry Smith +  ts - the TS context
6128e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6129e673d494SBarry Smith 
6130e673d494SBarry Smith    Level: intermediate
6131e673d494SBarry Smith 
6132e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6133e673d494SBarry Smith 
6134a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6135e673d494SBarry Smith @*/
6136e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6137e673d494SBarry Smith {
6138e673d494SBarry Smith   PetscErrorCode    ierr;
6139e673d494SBarry Smith 
6140e673d494SBarry Smith   PetscFunctionBegin;
6141e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6142e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6143e673d494SBarry Smith   PetscFunctionReturn(0);
6144e673d494SBarry Smith }
6145e673d494SBarry Smith 
6146e673d494SBarry Smith #undef __FUNCT__
6147b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
6148b3d3934dSBarry Smith /*@C
6149b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6150b3d3934dSBarry Smith 
6151b3d3934dSBarry Smith    Collective on TS
6152b3d3934dSBarry Smith 
6153b3d3934dSBarry Smith    Input Parameter:
6154b3d3934dSBarry Smith .  ts - the TS context
6155b3d3934dSBarry Smith 
6156b3d3934dSBarry Smith    Output Parameter:
6157b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6158b3d3934dSBarry Smith 
6159b3d3934dSBarry Smith    Level: intermediate
6160b3d3934dSBarry Smith 
61617db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
61627db568b7SBarry Smith 
6163b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6164b3d3934dSBarry Smith 
6165b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6166b3d3934dSBarry Smith @*/
6167b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6168b3d3934dSBarry Smith {
6169b3d3934dSBarry Smith   PetscInt       i;
6170b3d3934dSBarry Smith 
6171b3d3934dSBarry Smith   PetscFunctionBegin;
6172b3d3934dSBarry Smith   *names = NULL;
6173b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6174b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
61755537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6176b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6177b3d3934dSBarry Smith       break;
6178387f4636SBarry Smith     }
6179387f4636SBarry Smith   }
6180387f4636SBarry Smith   PetscFunctionReturn(0);
6181387f4636SBarry Smith }
6182387f4636SBarry Smith 
6183387f4636SBarry Smith #undef __FUNCT__
6184a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
6185a66092f1SBarry Smith /*@C
6186a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6187a66092f1SBarry Smith 
6188a66092f1SBarry Smith    Collective on TS
6189a66092f1SBarry Smith 
6190a66092f1SBarry Smith    Input Parameters:
6191a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6192a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6193a66092f1SBarry Smith 
6194a66092f1SBarry Smith    Level: intermediate
6195a66092f1SBarry Smith 
6196a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6197a66092f1SBarry Smith 
6198a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6199a66092f1SBarry Smith @*/
6200a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6201a66092f1SBarry Smith {
6202a66092f1SBarry Smith   PetscInt          j = 0,k;
6203a66092f1SBarry Smith   PetscErrorCode    ierr;
6204a66092f1SBarry Smith 
6205a66092f1SBarry Smith   PetscFunctionBegin;
6206a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6207a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6208a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6209a66092f1SBarry Smith   while (displaynames[j]) j++;
6210a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6211a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6212a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6213a66092f1SBarry Smith   j = 0;
6214a66092f1SBarry Smith   while (displaynames[j]) {
6215a66092f1SBarry Smith     k = 0;
6216a66092f1SBarry Smith     while (ctx->names[k]) {
6217a66092f1SBarry Smith       PetscBool flg;
6218a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6219a66092f1SBarry Smith       if (flg) {
6220a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6221a66092f1SBarry Smith         break;
6222a66092f1SBarry Smith       }
6223a66092f1SBarry Smith       k++;
6224a66092f1SBarry Smith     }
6225a66092f1SBarry Smith     j++;
6226a66092f1SBarry Smith   }
6227a66092f1SBarry Smith   PetscFunctionReturn(0);
6228a66092f1SBarry Smith }
6229a66092f1SBarry Smith 
6230a66092f1SBarry Smith 
6231a66092f1SBarry Smith #undef __FUNCT__
6232387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
6233387f4636SBarry Smith /*@C
6234387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6235387f4636SBarry Smith 
6236387f4636SBarry Smith    Collective on TS
6237387f4636SBarry Smith 
6238387f4636SBarry Smith    Input Parameters:
6239387f4636SBarry Smith +  ts - the TS context
6240387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6241387f4636SBarry Smith 
62427db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
62437db568b7SBarry Smith 
6244387f4636SBarry Smith    Level: intermediate
6245387f4636SBarry Smith 
6246387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6247387f4636SBarry Smith 
6248387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6249387f4636SBarry Smith @*/
6250387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6251387f4636SBarry Smith {
6252a66092f1SBarry Smith   PetscInt          i;
6253387f4636SBarry Smith   PetscErrorCode    ierr;
6254387f4636SBarry Smith 
6255387f4636SBarry Smith   PetscFunctionBegin;
6256387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6257387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
62585537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6259b3d3934dSBarry Smith       break;
6260b037adc7SBarry Smith     }
6261b037adc7SBarry Smith   }
6262b037adc7SBarry Smith   PetscFunctionReturn(0);
6263b037adc7SBarry Smith }
6264b037adc7SBarry Smith 
6265b037adc7SBarry Smith #undef __FUNCT__
626680666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
626780666b62SBarry Smith /*@C
626880666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
626980666b62SBarry Smith 
627080666b62SBarry Smith    Collective on TS
627180666b62SBarry Smith 
627280666b62SBarry Smith    Input Parameters:
627380666b62SBarry Smith +  ts - the TS context
627480666b62SBarry Smith .  transform - the transform function
62757684fa3eSBarry Smith .  destroy - function to destroy the optional context
627680666b62SBarry Smith -  ctx - optional context used by transform function
627780666b62SBarry Smith 
62787db568b7SBarry Smith    Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
62797db568b7SBarry Smith 
628080666b62SBarry Smith    Level: intermediate
628180666b62SBarry Smith 
628280666b62SBarry Smith .keywords: TS,  vector, monitor, view
628380666b62SBarry Smith 
6284a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
628580666b62SBarry Smith @*/
62867684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
628780666b62SBarry Smith {
628880666b62SBarry Smith   PetscInt          i;
6289a66092f1SBarry Smith   PetscErrorCode    ierr;
629080666b62SBarry Smith 
629180666b62SBarry Smith   PetscFunctionBegin;
629280666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
629380666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
62945537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
629580666b62SBarry Smith     }
629680666b62SBarry Smith   }
629780666b62SBarry Smith   PetscFunctionReturn(0);
629880666b62SBarry Smith }
629980666b62SBarry Smith 
630080666b62SBarry Smith #undef __FUNCT__
6301e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
6302e673d494SBarry Smith /*@C
6303e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6304e673d494SBarry Smith 
6305e673d494SBarry Smith    Collective on TSLGCtx
6306e673d494SBarry Smith 
6307e673d494SBarry Smith    Input Parameters:
6308e673d494SBarry Smith +  ts - the TS context
6309e673d494SBarry Smith .  transform - the transform function
63107684fa3eSBarry Smith .  destroy - function to destroy the optional context
6311e673d494SBarry Smith -  ctx - optional context used by transform function
6312e673d494SBarry Smith 
6313e673d494SBarry Smith    Level: intermediate
6314e673d494SBarry Smith 
6315e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6316e673d494SBarry Smith 
6317a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6318e673d494SBarry Smith @*/
63197684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6320e673d494SBarry Smith {
6321e673d494SBarry Smith   PetscFunctionBegin;
6322e673d494SBarry Smith   ctx->transform    = transform;
63237684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6324e673d494SBarry Smith   ctx->transformctx = tctx;
6325e673d494SBarry Smith   PetscFunctionReturn(0);
6326e673d494SBarry Smith }
6327e673d494SBarry Smith 
6328b3603a34SBarry Smith #undef __FUNCT__
63294f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
6330ef20d060SBarry Smith /*@C
63314f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
6332ef20d060SBarry Smith        in a time based line graph
6333ef20d060SBarry Smith 
6334ef20d060SBarry Smith    Collective on TS
6335ef20d060SBarry Smith 
6336ef20d060SBarry Smith    Input Parameters:
6337ef20d060SBarry Smith +  ts - the TS context
6338ef20d060SBarry Smith .  step - current time-step
6339ef20d060SBarry Smith .  ptime - current time
63407db568b7SBarry Smith .  u - current solution
63417db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6342ef20d060SBarry Smith 
6343ef20d060SBarry Smith    Level: intermediate
6344ef20d060SBarry Smith 
63456934998bSLisandro Dalcin    Notes: Each process in a parallel run displays its component errors in a separate window
6346abd5a294SJed Brown 
6347abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6348abd5a294SJed Brown 
6349abd5a294SJed Brown    Options Database Keys:
63504f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6351ef20d060SBarry Smith 
6352ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6353ef20d060SBarry Smith 
6354abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6355ef20d060SBarry Smith @*/
63560910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6357ef20d060SBarry Smith {
6358ef20d060SBarry Smith   PetscErrorCode    ierr;
63590b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6360ef20d060SBarry Smith   const PetscScalar *yy;
6361ef20d060SBarry Smith   Vec               y;
6362ef20d060SBarry Smith 
6363ef20d060SBarry Smith   PetscFunctionBegin;
6364a9f9c1f6SBarry Smith   if (!step) {
6365a9f9c1f6SBarry Smith     PetscDrawAxis axis;
63666934998bSLisandro Dalcin     PetscInt      dim;
6367a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
63681ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
63690910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6370a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6371a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6372a9f9c1f6SBarry Smith   }
63730910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6374ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
63750910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6376ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6377e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6378e3efe391SJed Brown   {
6379e3efe391SJed Brown     PetscReal *yreal;
6380e3efe391SJed Brown     PetscInt  i,n;
6381e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6382785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6383e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
63840b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6385e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6386e3efe391SJed Brown   }
6387e3efe391SJed Brown #else
63880b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6389e3efe391SJed Brown #endif
6390ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6391ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6392b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
63930b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
63946934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
63953923b477SBarry Smith   }
6396ef20d060SBarry Smith   PetscFunctionReturn(0);
6397ef20d060SBarry Smith }
6398ef20d060SBarry Smith 
6399201da799SBarry Smith #undef __FUNCT__
6400201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
6401201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6402201da799SBarry Smith {
6403201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6404201da799SBarry Smith   PetscReal      x   = ptime,y;
6405201da799SBarry Smith   PetscErrorCode ierr;
6406201da799SBarry Smith   PetscInt       its;
6407201da799SBarry Smith 
6408201da799SBarry Smith   PetscFunctionBegin;
640963e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6410201da799SBarry Smith   if (!n) {
6411201da799SBarry Smith     PetscDrawAxis axis;
6412201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6413201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6414201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6415201da799SBarry Smith     ctx->snes_its = 0;
6416201da799SBarry Smith   }
6417201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6418201da799SBarry Smith   y    = its - ctx->snes_its;
6419201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
64203fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6421201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
64226934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6423201da799SBarry Smith   }
6424201da799SBarry Smith   ctx->snes_its = its;
6425201da799SBarry Smith   PetscFunctionReturn(0);
6426201da799SBarry Smith }
6427201da799SBarry Smith 
6428201da799SBarry Smith #undef __FUNCT__
6429201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
6430201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6431201da799SBarry Smith {
6432201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6433201da799SBarry Smith   PetscReal      x   = ptime,y;
6434201da799SBarry Smith   PetscErrorCode ierr;
6435201da799SBarry Smith   PetscInt       its;
6436201da799SBarry Smith 
6437201da799SBarry Smith   PetscFunctionBegin;
643863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6439201da799SBarry Smith   if (!n) {
6440201da799SBarry Smith     PetscDrawAxis axis;
6441201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6442201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6443201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6444201da799SBarry Smith     ctx->ksp_its = 0;
6445201da799SBarry Smith   }
6446201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6447201da799SBarry Smith   y    = its - ctx->ksp_its;
6448201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
644999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6450201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
64516934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6452201da799SBarry Smith   }
6453201da799SBarry Smith   ctx->ksp_its = its;
6454201da799SBarry Smith   PetscFunctionReturn(0);
6455201da799SBarry Smith }
6456f9c1d6abSBarry Smith 
6457f9c1d6abSBarry Smith #undef __FUNCT__
6458f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
6459f9c1d6abSBarry Smith /*@
6460f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6461f9c1d6abSBarry Smith 
6462f9c1d6abSBarry Smith    Collective on TS and Vec
6463f9c1d6abSBarry Smith 
6464f9c1d6abSBarry Smith    Input Parameters:
6465f9c1d6abSBarry Smith +  ts - the TS context
6466f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6467f9c1d6abSBarry Smith 
6468f9c1d6abSBarry Smith    Output Parameters:
6469f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6470f9c1d6abSBarry Smith 
6471f9c1d6abSBarry Smith    Level: developer
6472f9c1d6abSBarry Smith 
6473f9c1d6abSBarry Smith .keywords: TS, compute
6474f9c1d6abSBarry Smith 
6475f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6476f9c1d6abSBarry Smith @*/
6477f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6478f9c1d6abSBarry Smith {
6479f9c1d6abSBarry Smith   PetscErrorCode ierr;
6480f9c1d6abSBarry Smith 
6481f9c1d6abSBarry Smith   PetscFunctionBegin;
6482f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6483ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6484f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6485f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6486f9c1d6abSBarry Smith }
648724655328SShri 
6488b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6489b3d3934dSBarry Smith #undef __FUNCT__
6490b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
6491b3d3934dSBarry Smith /*@C
6492b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6493b3d3934dSBarry Smith 
6494b3d3934dSBarry Smith    Collective on TS
6495b3d3934dSBarry Smith 
6496b3d3934dSBarry Smith    Input Parameters:
6497b3d3934dSBarry Smith .  ts  - the ODE solver object
6498b3d3934dSBarry Smith 
6499b3d3934dSBarry Smith    Output Parameter:
6500b3d3934dSBarry Smith .  ctx - the context
6501b3d3934dSBarry Smith 
6502b3d3934dSBarry Smith    Level: intermediate
6503b3d3934dSBarry Smith 
6504b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6505b3d3934dSBarry Smith 
6506b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6507b3d3934dSBarry Smith 
6508b3d3934dSBarry Smith @*/
6509b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6510b3d3934dSBarry Smith {
6511b3d3934dSBarry Smith   PetscErrorCode ierr;
6512b3d3934dSBarry Smith 
6513b3d3934dSBarry Smith   PetscFunctionBegin;
6514a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6515b3d3934dSBarry Smith   PetscFunctionReturn(0);
6516b3d3934dSBarry Smith }
6517b3d3934dSBarry Smith 
6518b3d3934dSBarry Smith #undef __FUNCT__
6519b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
6520b3d3934dSBarry Smith /*@C
6521b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6522b3d3934dSBarry Smith 
6523b3d3934dSBarry Smith    Collective on TS
6524b3d3934dSBarry Smith 
6525b3d3934dSBarry Smith    Input Parameters:
6526b3d3934dSBarry Smith +  ts - the TS context
6527b3d3934dSBarry Smith .  step - current time-step
6528b3d3934dSBarry Smith .  ptime - current time
65297db568b7SBarry Smith .  u  - current solution
65307db568b7SBarry Smith -  dctx - the envelope context
6531b3d3934dSBarry Smith 
6532b3d3934dSBarry Smith    Options Database:
6533b3d3934dSBarry Smith .  -ts_monitor_envelope
6534b3d3934dSBarry Smith 
6535b3d3934dSBarry Smith    Level: intermediate
6536b3d3934dSBarry Smith 
6537b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
6538b3d3934dSBarry Smith 
6539b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6540b3d3934dSBarry Smith 
65417db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
6542b3d3934dSBarry Smith @*/
65437db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6544b3d3934dSBarry Smith {
6545b3d3934dSBarry Smith   PetscErrorCode       ierr;
65467db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
6547b3d3934dSBarry Smith 
6548b3d3934dSBarry Smith   PetscFunctionBegin;
6549b3d3934dSBarry Smith   if (!ctx->max) {
6550b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
6551b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
6552b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
6553b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
6554b3d3934dSBarry Smith   } else {
6555b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
6556b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
6557b3d3934dSBarry Smith   }
6558b3d3934dSBarry Smith   PetscFunctionReturn(0);
6559b3d3934dSBarry Smith }
6560b3d3934dSBarry Smith 
6561b3d3934dSBarry Smith 
6562b3d3934dSBarry Smith #undef __FUNCT__
6563b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
6564b3d3934dSBarry Smith /*@C
6565b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
6566b3d3934dSBarry Smith 
6567b3d3934dSBarry Smith    Collective on TS
6568b3d3934dSBarry Smith 
6569b3d3934dSBarry Smith    Input Parameter:
6570b3d3934dSBarry Smith .  ts - the TS context
6571b3d3934dSBarry Smith 
6572b3d3934dSBarry Smith    Output Parameter:
6573b3d3934dSBarry Smith +  max - the maximum values
6574b3d3934dSBarry Smith -  min - the minimum values
6575b3d3934dSBarry Smith 
65767db568b7SBarry Smith    Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
65777db568b7SBarry Smith 
6578b3d3934dSBarry Smith    Level: intermediate
6579b3d3934dSBarry Smith 
6580b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6581b3d3934dSBarry Smith 
6582b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6583b3d3934dSBarry Smith @*/
6584b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
6585b3d3934dSBarry Smith {
6586b3d3934dSBarry Smith   PetscInt i;
6587b3d3934dSBarry Smith 
6588b3d3934dSBarry Smith   PetscFunctionBegin;
6589b3d3934dSBarry Smith   if (max) *max = NULL;
6590b3d3934dSBarry Smith   if (min) *min = NULL;
6591b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6592b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
65935537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
6594b3d3934dSBarry Smith       if (max) *max = ctx->max;
6595b3d3934dSBarry Smith       if (min) *min = ctx->min;
6596b3d3934dSBarry Smith       break;
6597b3d3934dSBarry Smith     }
6598b3d3934dSBarry Smith   }
6599b3d3934dSBarry Smith   PetscFunctionReturn(0);
6600b3d3934dSBarry Smith }
6601b3d3934dSBarry Smith 
6602b3d3934dSBarry Smith #undef __FUNCT__
6603b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
6604b3d3934dSBarry Smith /*@C
6605b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
6606b3d3934dSBarry Smith 
6607b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
6608b3d3934dSBarry Smith 
6609b3d3934dSBarry Smith    Input Parameter:
6610b3d3934dSBarry Smith .  ctx - the monitor context
6611b3d3934dSBarry Smith 
6612b3d3934dSBarry Smith    Level: intermediate
6613b3d3934dSBarry Smith 
6614b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
6615b3d3934dSBarry Smith 
66167db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
6617b3d3934dSBarry Smith @*/
6618b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
6619b3d3934dSBarry Smith {
6620b3d3934dSBarry Smith   PetscErrorCode ierr;
6621b3d3934dSBarry Smith 
6622b3d3934dSBarry Smith   PetscFunctionBegin;
6623b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
6624b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
6625b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
6626b3d3934dSBarry Smith   PetscFunctionReturn(0);
6627b3d3934dSBarry Smith }
6628f2dee214SBarry Smith 
662924655328SShri #undef __FUNCT__
663024655328SShri #define __FUNCT__ "TSRollBack"
663124655328SShri /*@
663224655328SShri    TSRollBack - Rolls back one time step
663324655328SShri 
663424655328SShri    Collective on TS
663524655328SShri 
663624655328SShri    Input Parameter:
663724655328SShri .  ts - the TS context obtained from TSCreate()
663824655328SShri 
663924655328SShri    Level: advanced
664024655328SShri 
664124655328SShri .keywords: TS, timestep, rollback
664224655328SShri 
664324655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
664424655328SShri @*/
664524655328SShri PetscErrorCode  TSRollBack(TS ts)
664624655328SShri {
664724655328SShri   PetscErrorCode ierr;
664824655328SShri 
664924655328SShri   PetscFunctionBegin;
665024655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
665124655328SShri 
665224655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
665324655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
665424655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
665524655328SShri   ts->ptime = ts->ptime_prev;
66568392e04aSShri Abhyankar   ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */
665724655328SShri   PetscFunctionReturn(0);
665824655328SShri }
6659aeb4809dSShri Abhyankar 
6660ff22ae23SHong Zhang #undef __FUNCT__
6661ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
6662ff22ae23SHong Zhang /*@
6663ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
6664ff22ae23SHong Zhang 
6665ff22ae23SHong Zhang    Input Parameter:
6666ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
6667ff22ae23SHong Zhang 
6668ff22ae23SHong Zhang    Level: advanced
6669ff22ae23SHong Zhang 
6670ff22ae23SHong Zhang .keywords: TS, getstages
6671ff22ae23SHong Zhang 
6672ff22ae23SHong Zhang .seealso: TSCreate()
6673ff22ae23SHong Zhang @*/
6674ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
6675ff22ae23SHong Zhang {
6676ff22ae23SHong Zhang   PetscErrorCode ierr;
6677ff22ae23SHong Zhang 
6678ff22ae23SHong Zhang   PetscFunctionBegin;
6679ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6680ff22ae23SHong Zhang   PetscValidPointer(ns,2);
6681ff22ae23SHong Zhang 
6682ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
6683ff22ae23SHong Zhang   else {
6684ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
6685ff22ae23SHong Zhang   }
6686ff22ae23SHong Zhang   PetscFunctionReturn(0);
6687ff22ae23SHong Zhang }
6688ff22ae23SHong Zhang 
6689847ff0e1SMatthew G. Knepley #undef __FUNCT__
6690847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
6691847ff0e1SMatthew G. Knepley /*@C
6692847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
6693847ff0e1SMatthew G. Knepley 
6694847ff0e1SMatthew G. Knepley   Collective on SNES
6695847ff0e1SMatthew G. Knepley 
6696847ff0e1SMatthew G. Knepley   Input Parameters:
6697847ff0e1SMatthew G. Knepley + ts - the TS context
6698847ff0e1SMatthew G. Knepley . t - current timestep
6699847ff0e1SMatthew G. Knepley . U - state vector
6700847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
6701847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
6702847ff0e1SMatthew G. Knepley - ctx - an optional user context
6703847ff0e1SMatthew G. Knepley 
6704847ff0e1SMatthew G. Knepley   Output Parameters:
6705847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
6706847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
6707847ff0e1SMatthew G. Knepley 
6708847ff0e1SMatthew G. Knepley   Level: intermediate
6709847ff0e1SMatthew G. Knepley 
6710847ff0e1SMatthew G. Knepley   Notes:
6711847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
6712847ff0e1SMatthew G. Knepley 
6713847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
6714847ff0e1SMatthew G. Knepley 
6715847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
6716847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
6717847ff0e1SMatthew G. Knepley 
6718847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
6719847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
6720847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
6721847ff0e1SMatthew G. Knepley 
6722847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
6723847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
6724847ff0e1SMatthew G. Knepley @*/
6725847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
6726847ff0e1SMatthew G. Knepley {
6727847ff0e1SMatthew G. Knepley   SNES           snes;
6728847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6729847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6730847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6731847ff0e1SMatthew G. Knepley 
6732847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6733c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6734847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6735847ff0e1SMatthew G. Knepley   if (!color) {
6736847ff0e1SMatthew G. Knepley     DM         dm;
6737847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6738847ff0e1SMatthew G. Knepley 
6739847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6740847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6741847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6742847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6743847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6744847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6745847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6746847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6747847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6748847ff0e1SMatthew G. Knepley     } else {
6749847ff0e1SMatthew G. Knepley       MatColoring mc;
6750847ff0e1SMatthew G. Knepley 
6751847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
6752847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
6753847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
6754847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
6755847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
6756847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
6757847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6758847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6759847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6760847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6761847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6762847ff0e1SMatthew G. Knepley     }
6763847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
6764847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
6765847ff0e1SMatthew G. Knepley   }
6766847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
6767847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
6768847ff0e1SMatthew G. Knepley   if (J != B) {
6769847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6770847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6771847ff0e1SMatthew G. Knepley   }
6772847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
6773847ff0e1SMatthew G. Knepley }
677493b34091SDebojyoti Ghosh 
677593b34091SDebojyoti Ghosh #undef __FUNCT__
6776cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSSetFunctionDomainError"
6777cb9d8021SPierre Barbier de Reuille /*@
6778cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
6779cb9d8021SPierre Barbier de Reuille 
6780cb9d8021SPierre Barbier de Reuille     Input Parameters:
6781cb9d8021SPierre Barbier de Reuille     ts - the TS context
6782cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
6783cb9d8021SPierre Barbier de Reuille 
6784cb9d8021SPierre Barbier de Reuille     Level: intermediate
6785cb9d8021SPierre Barbier de Reuille 
6786cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
6787cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
6788cb9d8021SPierre Barbier de Reuille @*/
6789cb9d8021SPierre Barbier de Reuille 
6790d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
6791cb9d8021SPierre Barbier de Reuille {
6792cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6793cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6794cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
6795cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6796cb9d8021SPierre Barbier de Reuille }
6797cb9d8021SPierre Barbier de Reuille 
6798cb9d8021SPierre Barbier de Reuille #undef __FUNCT__
6799cb9d8021SPierre Barbier de Reuille #define __FUNCT__ "TSFunctionDomainError"
6800cb9d8021SPierre Barbier de Reuille /*@
6801cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
6802cb9d8021SPierre Barbier de Reuille 
6803cb9d8021SPierre Barbier de Reuille     Input Parameters:
6804cb9d8021SPierre Barbier de Reuille     ts - the TS context
6805cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
6806d183316bSPierre Barbier de Reuille     Y - state vector to check.
6807cb9d8021SPierre Barbier de Reuille 
6808cb9d8021SPierre Barbier de Reuille     Output Parameter:
6809cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
6810cb9d8021SPierre Barbier de Reuille 
6811cb9d8021SPierre Barbier de Reuille     Note:
6812cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
6813cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
681496a0c994SBarry Smith 
681596a0c994SBarry Smith     Level: advanced
6816cb9d8021SPierre Barbier de Reuille @*/
6817d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
6818cb9d8021SPierre Barbier de Reuille {
6819cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
6820cb9d8021SPierre Barbier de Reuille 
6821cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
6822cb9d8021SPierre Barbier de Reuille 
6823cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6824cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
6825cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
6826d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
6827cb9d8021SPierre Barbier de Reuille   }
6828cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
6829cb9d8021SPierre Barbier de Reuille }
68301ceb14c0SBarry Smith 
68311ceb14c0SBarry Smith #undef  __FUNCT__
6832e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
683393b34091SDebojyoti Ghosh /*@C
6834e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
683593b34091SDebojyoti Ghosh 
683693b34091SDebojyoti Ghosh   Collective on MPI_Comm
683793b34091SDebojyoti Ghosh 
683893b34091SDebojyoti Ghosh   Input Parameter:
683993b34091SDebojyoti Ghosh . tsin    - The input TS
684093b34091SDebojyoti Ghosh 
684193b34091SDebojyoti Ghosh   Output Parameter:
6842e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
684393b34091SDebojyoti Ghosh 
68445eca1a21SEmil Constantinescu   Notes:
68455eca1a21SEmil 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.
68465eca1a21SEmil Constantinescu 
6847baa10174SEmil 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);
68485eca1a21SEmil Constantinescu 
68495eca1a21SEmil Constantinescu   Level: developer
685093b34091SDebojyoti Ghosh 
6851e5168f73SEmil Constantinescu .keywords: TS, clone
6852e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
685393b34091SDebojyoti Ghosh @*/
6854baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
685593b34091SDebojyoti Ghosh {
685693b34091SDebojyoti Ghosh   TS             t;
685793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
6858dc846ba4SSatish Balay   SNES           snes_start;
6859dc846ba4SSatish Balay   DM             dm;
6860dc846ba4SSatish Balay   TSType         type;
686193b34091SDebojyoti Ghosh 
686293b34091SDebojyoti Ghosh   PetscFunctionBegin;
686393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
686493b34091SDebojyoti Ghosh   *tsout = NULL;
686593b34091SDebojyoti Ghosh 
68667a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
686793b34091SDebojyoti Ghosh 
686893b34091SDebojyoti Ghosh   /* General TS description */
686993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
687093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
687193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
687293b34091SDebojyoti Ghosh   t->snes_its          = 0;
687393b34091SDebojyoti Ghosh   t->nwork             = 0;
687493b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
687593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
687693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
687793b34091SDebojyoti Ghosh 
687834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
687934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
6880d15a3a53SEmil Constantinescu 
688193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
688293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
688393b34091SDebojyoti Ghosh 
688493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
688551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
688693b34091SDebojyoti Ghosh 
688793b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
688893b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
688993b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
689093b34091SDebojyoti Ghosh   t->time_step_orig    = tsin->time_step_orig;
689193b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
689293b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
689393b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
689493b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
689593b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
689693b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
689793b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
689893b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
689993b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
690093b34091SDebojyoti Ghosh 
690193b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
690293b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
690393b34091SDebojyoti Ghosh 
690493b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
690593b34091SDebojyoti Ghosh 
690693b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
690793b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
690893b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
690993b34091SDebojyoti Ghosh 
691093b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
691193b34091SDebojyoti Ghosh 
69120d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
69130d4fed19SBarry Smith     PetscInt i;
69140d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
69150d4fed19SBarry Smith     for (i=0; i<10; i++) {
69160d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
69170d4fed19SBarry Smith     }
69180d4fed19SBarry Smith   }
691993b34091SDebojyoti Ghosh   *tsout = t;
692093b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
692193b34091SDebojyoti Ghosh }
6922